class-wpml-tm-icl-translations.php
5.33 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
class WPML_TM_ICL_Translations extends WPML_TM_Record_User {
private $table = 'icl_translations';
private $fields = array();
private $related = array();
/** @var wpdb $wpdb */
private $wpdb;
private $translation_id = 0;
/** @var WPML_Frontend_Post_Actions | WPML_Admin_Post_Actions $post_translations */
private $post_translations;
/** @var WPML_Term_Translation $term_translations */
private $term_translations;
/**
* WPML_TM_ICL_Translations constructor.
*
* @throws InvalidArgumentException if given data does not correspond to a
* record in icl_translations
*
* @param WPML_TM_Records $tm_records
* @param int|array $id
* @param string $type translation id, trid_lang or id_prefix for now
*/
public function __construct( &$tm_records, $id, $type = 'translation_id' ) {
$this->wpdb = $tm_records->wpdb();
$this->post_translations = $tm_records->get_post_translations();
$this->term_translations = $tm_records->get_term_translations();
parent::__construct( $tm_records );
if ( $id > 0 && $type === 'translation_id' ) {
$this->{$type} = $id;
} elseif ( $type === 'id_type_prefix' && isset( $id['element_id'] ) && isset( $id['type_prefix'] ) ) {
$this->build_from_element_id( $id );
} elseif ( $type === 'trid_lang' && isset( $id['trid'] ) && isset( $id['language_code'] ) ) {
$this->build_from_trid( $id );
} else {
throw new InvalidArgumentException( 'Unknown column: ' . $type . ' or invalid id: ' . serialize( $id ) );
}
}
private function build_from_element_id( $id ) {
if ( 'post' === $id['type_prefix'] ) {
$this->translation_id = $this->post_translations->get_translation_id( $id['element_id'] );
}
if ( 'term' === $id['type_prefix'] ) {
$this->translation_id = $this->term_translations->get_translation_id( $id['element_id'] );
}
if ( ! $this->translation_id ) {
$this->select_translation_id(
' element_id = %d AND element_type LIKE %s ',
array( $id['element_id'], $id['type_prefix'] . '%' )
);
}
}
private function build_from_trid( $id ) {
$this->select_translation_id(
' trid = %d AND language_code = %s ',
array( $id['trid'], $id['language_code'] )
);
}
/**
* @return WPML_TM_ICL_Translations[]
*/
public function translations() {
if ( false === (bool) $this->related ) {
$trid = $this->trid();
$found = false;
$cache = new WPML_WP_Cache( 'WPML_TM_ICL_Translations::translations' );
$translation_ids = $cache->get( $trid, $found );
if ( ! $found ) {
$translation_ids = $this->wpdb->get_results(
"SELECT translation_id, language_code
FROM {$this->wpdb->prefix}{$this->table}
WHERE trid = " . $trid
);
$cache->set( $trid, $translation_ids );
}
foreach ( $translation_ids as $row ) {
$this->related[ $row->language_code ] = $this->tm_records
->icl_translations_by_translation_id( $row->translation_id );
}
}
return $this->related;
}
private function select_by( $function, $field ) {
$result = call_user_func( array( $this->post_translations, $function ), $this->translation_id );
$result = $result ? $result : call_user_func( array( $this->term_translations, $function ), $this->translation_id );
$result = $result ? $result : $this->select_field( $field );
return $result;
}
/**
* @return null|int
*/
public function trid() {
return $this->select_by( 'get_trid_from_translation_id', 'trid' );
}
/**
* @return int
*/
public function translation_id() {
return $this->translation_id;
}
/**
* @return null|int
*/
public function element_id() {
return $this->select_by( 'get_element_from_translation_id', 'element_id' );
}
/**
* @return string|null
*/
public function language_code() {
return $this->select_field( 'language_code' );
}
/**
* @return string|null
*/
public function source_language_code() {
$lang = $this->post_translations->get_source_lang_from_translation_id( $this->translation_id );
$lang = $lang['found'] ? $lang : $this->term_translations->get_source_lang_from_translation_id( $this->translation_id );
$lang = $lang['found'] ? $lang['code'] : $this->select_field( 'source_language_code' );
return $lang;
}
/**
*
* @return $this
*/
public function delete() {
$this->tm_records
->icl_translation_status_by_translation_id( $this->translation_id )
->delete();
$this->wpdb->delete(
$this->wpdb->prefix . $this->table,
$this->get_args()
);
return $this;
}
private function select_field( $field ) {
$this->fields[ $field ] = isset( $this->fields[ $field ] ) ? $this->fields[ $field ] : $this->wpdb->get_var(
$this->wpdb->prepare(
" SELECT {$field}
FROM {$this->wpdb->prefix}{$this->table}
WHERE translation_id = %d
LIMIT 1",
$this->translation_id
)
);
return $this->fields[ $field ];
}
private function get_args() {
return array( 'translation_id' => $this->translation_id );
}
private function select_translation_id( $where, $prepare_args ) {
$this->translation_id = $this->wpdb->get_var(
"SELECT translation_id FROM {$this->wpdb->prefix}{$this->table}
WHERE" . $this->wpdb->prepare( $where, $prepare_args )
. ' LIMIT 1'
);
if ( ! $this->translation_id ) {
throw new InvalidArgumentException( 'No translation entry found for query: ' . serialize( $where ) . serialize( $prepare_args ) );
}
}
}