class-wpml-st-package-storage.php
2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Created by PhpStorm.
* User: bruce
* Date: 16/06/17
* Time: 10:57 AM
*/
class WPML_ST_Package_Storage {
private $package_id;
private $wpdb;
/**
* WPML_ST_Package_Storage constructor.
*
* @param int $package_id
* @param \wpdb $wpdb
*/
public function __construct( $package_id, wpdb $wpdb ) {
$this->package_id = $package_id;
$this->wpdb = $wpdb;
}
/**
* @param string $string_title
* @param string $string_type
* @param string $string_value
* @param int $string_id
*
* @return bool
*/
public function update( $string_title, $string_type, $string_value, $string_id ) {
$update_where = array( 'id' => $string_id );
$update_data = array(
'type' => $string_type,
'title' => $this->truncate_long_string( $string_title ),
);
$type_or_title_updated = $this->wpdb->update( $this->wpdb->prefix . 'icl_strings', $update_data, $update_where );
$update_data = array(
'string_package_id' => $this->package_id,
'value' => $string_value,
);
$package_id_or_value_updated = $this->wpdb->update( $this->wpdb->prefix . 'icl_strings', $update_data, $update_where );
if ( $package_id_or_value_updated ) {
$this->set_string_status_to_needs_update_if_translated( $string_id );
$this->set_translations_to_needs_update();
}
return $type_or_title_updated || $package_id_or_value_updated;
}
private function set_string_status_to_needs_update_if_translated( $string_id ) {
$this->wpdb->query(
$this->wpdb->prepare(
"UPDATE {$this->wpdb->prefix}icl_strings
SET status=%d
WHERE id=%d AND status<>%d",
ICL_TM_NEEDS_UPDATE,
$string_id,
ICL_TM_NOT_TRANSLATED
)
);
$this->wpdb->query(
$this->wpdb->prepare(
"UPDATE {$this->wpdb->prefix}icl_string_translations
SET status=%d
WHERE string_id=%d AND status<>%d",
ICL_TM_NEEDS_UPDATE,
$string_id,
ICL_TM_NOT_TRANSLATED
)
);
}
private function set_translations_to_needs_update() {
$translation_ids = $this->wpdb->get_col(
$this->wpdb->prepare(
"SELECT translation_id
FROM {$this->wpdb->prefix}icl_translations
WHERE trid = ( SELECT trid
FROM {$this->wpdb->prefix}icl_translations
WHERE element_id = %d
AND element_type LIKE 'package%%'
LIMIT 1 )",
$this->package_id
)
);
if ( ! empty( $translation_ids ) ) {
$this->wpdb->query(
"UPDATE {$this->wpdb->prefix}icl_translation_status
SET needs_update = 1
WHERE translation_id IN (" . wpml_prepare_in( $translation_ids, '%d' ) . ' ) '
);
}
}
private function truncate_long_string( $string ) {
return strlen( $string ) > WPML_STRING_TABLE_NAME_CONTEXT_LENGTH
? mb_substr( $string, 0, WPML_STRING_TABLE_NAME_CONTEXT_LENGTH )
: $string;
}
}