class-wpml-taxonomy-translation-screen-data.php
7.14 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
class WPML_Taxonomy_Translation_Screen_Data extends WPML_WPDB_And_SP_User {
const WPML_TAXONOMY_TRANSLATION_MAX_TERMS_RESULTS_SET = 1000;
/** @var string $taxonomy */
private $taxonomy;
/**
* WPML_Taxonomy_Translation_Screen_Data constructor.
*
* @param SitePress $sitepress
* @param string $taxonomy
*/
public function __construct( &$sitepress, $taxonomy ) {
$wpdb = $sitepress->wpdb();
parent::__construct( $wpdb, $sitepress );
$this->taxonomy = $taxonomy;
}
/**
* The returned array from this function is indexed as follows.
* It holds an array of all terms to be displayed under [terms]
* and the count of all terms matching the filter under [count].
*
* The array under [terms] itself is index as such:
* [trid][lang]
*
* It holds in itself the terms objects of the to be displayed terms.
* These are ordered by their names alphabetically.
* Also their objects are amended by the index $term->translation_of holding the term_taxonomy_id of their original element
* and their level under $term->level in case of hierarchical terms.
*
* Also the index [trid][source_lang] holds the source language of the term group.
*
* @return array
*/
public function terms() {
$terms_data = array(
'truncated' => 0,
'terms' => array(),
);
$attributes_to_select = array();
$icl_translations_table_name = $this->wpdb->prefix . 'icl_translations';
$attributes_to_select[ $this->wpdb->terms ] = array(
'alias' => 't',
'vars' => array( 'name', 'slug', 'term_id' ),
);
$attributes_to_select[ $this->wpdb->term_taxonomy ] = array(
'alias' => 'tt',
'vars' => array(
'term_taxonomy_id',
'parent',
'description',
),
);
$attributes_to_select[ $icl_translations_table_name ] = array(
'alias' => 'i',
'vars' => array(
'language_code',
'trid',
'source_language_code',
),
);
$query_limit_cap = defined( 'WPML_TAXONOMY_TRANSLATION_MAX_TERMS_RESULTS_SET' ) ?
WPML_TAXONOMY_TRANSLATION_MAX_TERMS_RESULTS_SET : self::WPML_TAXONOMY_TRANSLATION_MAX_TERMS_RESULTS_SET;
$join_statements = array();
$as = $this->alias_statements( $attributes_to_select );
$join_statements[] = "{$as['t']} JOIN {$as['tt']} ON tt.term_id = t.term_id";
$join_statements[] = "{$as['i']} ON i.element_id = tt.term_taxonomy_id";
$from_clause = $this->build_from_clause( join( ' JOIN ', $join_statements ), $attributes_to_select, $query_limit_cap );
$select_clause = $this->build_select_vars( $attributes_to_select );
$where_clause = $this->build_where_clause( $attributes_to_select );
$full_statement = "SELECT SQL_CALC_FOUND_ROWS {$select_clause} FROM {$from_clause} WHERE {$where_clause}";
$all_terms = $this->wpdb->get_results( $full_statement );
$real_terms_count = (int) $this->wpdb->get_var( 'SELECT FOUND_ROWS()' );
if ( $real_terms_count > $query_limit_cap ) {
$terms_data['truncated'] = 1;
}
if ( ! is_array( $all_terms ) ) {
return $terms_data;
}
if ( function_exists( 'get_term_meta' ) ) {
$all_terms = $this->add_metadata( $all_terms );
}
if ( $all_terms ) {
$terms_data['terms'] = $this->order_terms_list( $this->index_terms_array( $all_terms ) );
}
return $terms_data;
}
/**
* @param array $terms
* Turn a numerical array of terms objects into an associative once,
* holding the same terms, but indexed by their term_id.
*
* @return array
*/
private function index_terms_array( $terms ) {
$terms_indexed = array();
foreach ( $terms as $term ) {
$terms_indexed[ $term->term_id ] = $term;
}
return $terms_indexed;
}
/**
* @param array $trid_group
* @param array $terms
* Transforms the term arrays generated by the Translation Tree class and turns them into
* standard WordPress terms objects.
*
* @return mixed
*/
private function set_language_information(
$trid_group,
$terms
) {
foreach ( $trid_group['elements'] as $lang => $term ) {
$term_object = $terms[ $term['term_id'] ];
$term_object->level = $term['level'];
$trid_group[ $lang ] = $term_object;
}
unset( $trid_group['elements'] );
return $trid_group;
}
/**
* Orders a list of terms alphabetically and hierarchy-wise
*
* @param array $terms
*
* @return array
*/
private function order_terms_list( $terms ) {
$terms_tree = new WPML_Translation_Tree(
$this->sitepress,
$this->taxonomy,
$terms
);
$ordered_terms = $terms_tree->get_alphabetically_ordered_list();
foreach ( $ordered_terms as $key => $trid_group ) {
$ordered_terms[ $key ] = self::set_language_information(
$trid_group,
$terms
);
}
return $ordered_terms;
}
/**
* @param array $selects
* Generates a list of to be selected variables in an sql query.
*
* @return string
*/
private function build_select_vars( $selects ) {
$output = '';
if ( is_array( $selects ) ) {
$coarse_selects = array();
foreach ( $selects as $select ) {
$vars = $select['vars'];
$table = $select['alias'];
foreach ( $vars as $key => $var ) {
$vars[ $key ] = $table . '.' . $var;
}
$coarse_selects[] = join( ', ', $vars );
}
$output = join( ', ', $coarse_selects );
}
return $output;
}
/**
* @param array $selects
* Returns an array of alias statements to be used in SQL queries with joins.
*
* @return array
*/
private function alias_statements( $selects ) {
$output = array();
foreach ( $selects as $key => $select ) {
$output[ $select['alias'] ] = $key . ' AS ' . $select['alias'];
}
return $output;
}
private function build_where_clause( $selects ) {
$where_clauses[] = $selects[ $this->wpdb->term_taxonomy ]['alias']
. $this->wpdb->prepare(
'.taxonomy = %s ',
$this->taxonomy
);
$where_clauses[] = $selects[ $this->wpdb->prefix . 'icl_translations' ]['alias']
. $this->wpdb->prepare(
'.element_type = %s ',
'tax_' . $this->taxonomy
);
$where_clause = join( ' AND ', $where_clauses );
return $where_clause;
}
/**
* @param string $from
* @param array $selects
* @param int $limit
*
* @return string
*/
private function build_from_clause( $from, $selects, $limit ) {
return $from . sprintf(
" INNER JOIN (
SELECT trid FROM %s WHERE element_type = '%s' AND source_language_code IS NULL LIMIT %d
) lm on lm.trid = %s.trid",
$this->wpdb->prefix . 'icl_translations',
'tax_' . $this->taxonomy,
$limit,
$selects[ $this->wpdb->prefix . 'icl_translations' ]['alias']
);
}
/**
* @param array $all_terms
*
* @return array
*/
private function add_metadata( $all_terms ) {
$setting_factory = $this->sitepress->core_tm()->settings_factory();
foreach ( $all_terms as $term ) {
$meta_data = get_term_meta( $term->term_id );
foreach ( $meta_data as $meta_key => $meta_data ) {
if ( in_array( $setting_factory->term_meta_setting( $meta_key )->status(), array( WPML_TRANSLATE_CUSTOM_FIELD, WPML_COPY_ONCE_CUSTOM_FIELD ), true ) ) {
$term->meta_data[ $meta_key ] = $meta_data;
}
}
}
return $all_terms;
}
}