PluginProbe
Polylang / 2.5
Polylang v2.5
3.8.10 3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 All 234 releases
← All changes | admin/admin-filters-term.php +117 -239 3.0.3 → 2.5 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Manages filters and actions related to terms on admin side
8 5 *
@@ -8,54 +5,13 @@
8 5 *
9 6 * @since 1.2
10 7 */
11 8 class PLL_Admin_Filters_Term {
12 - /**
13 - * Stores the plugin options.
14 - *
15 - * @var array
16 - */
17 - public $options;
9 + public $links, $model, $options, $pref_lang;
10 + protected $pre_term_name; // Used to store the term name before creating a slug if needed
11 + protected $post_id; // Used to store the current post_id when bulk editing posts
18 12
19 13 /**
20 - * @var PLL_Model
21 - */
22 - public $model;
23 -
24 - /**
25 - * @var PLL_Admin_Links
26 - */
27 - public $links;
28 -
29 - /**
30 - * Language selected in the admin language filter.
31 - *
32 - * @var PLL_Language
33 - */
34 - public $filter_lang;
35 -
36 - /**
37 - * Preferred language to assign to the new terms.
38 - *
39 - * @var PLL_Language
40 - */
41 - public $pref_lang;
42 -
43 - /**
44 - * Stores the term name before creating a slug if needed.
45 - *
46 - * @var string
47 - */
48 - protected $pre_term_name;
49 -
50 - /**
51 - * Stores the current post_id when bulk editing posts.
52 - *
53 - * @var int
54 - */
55 - protected $post_id;
56 -
57 - /**
58 14 * Constructor: setups filters and actions
59 15 *
60 16 * @param object $polylang
61 17 */
@@ -96,45 +52,20 @@
96 52 /**
97 53 * Adds the language field in the 'Categories' and 'Post Tags' panels
98 54 *
99 55 * @since 0.1
100 - *
101 - * @return void
102 56 */
103 57 public function add_term_form() {
104 - if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
105 - $taxonomy = sanitize_key( $_GET['taxonomy'] ); // phpcs:ignore WordPress.Security.NonceVerification
106 - }
58 + $taxonomy = $_GET['taxonomy'];
59 + $post_type = isset( $GLOBALS['post_type'] ) ? $GLOBALS['post_type'] : $_REQUEST['post_type'];
107 60
108 - if ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
109 - $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification
110 - }
111 -
112 - if ( isset( $GLOBALS['post_type'] ) ) {
113 - $post_type = $GLOBALS['post_type'];
114 - }
115 -
116 - if ( ! isset( $taxonomy, $post_type ) || ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) {
61 + if ( ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) {
117 62 return;
118 63 }
119 64
120 - $from_term_id = isset( $_GET['from_tag'] ) ? (int) $_GET['from_tag'] : 0; // phpcs:ignore WordPress.Security.NonceVerification
121 -
122 - $lang = isset( $_GET['new_lang'] ) ? $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ) : $this->pref_lang; // phpcs:ignore WordPress.Security.NonceVerification
123 -
65 + $lang = isset( $_GET['new_lang'] ) ? $this->model->get_language( $_GET['new_lang'] ) : $this->pref_lang;
124 66 $dropdown = new PLL_Walker_Dropdown();
125 67
126 - $dropdown_html = $dropdown->walk(
127 - $this->model->get_languages_list(),
128 - -1,
129 - array(
130 - 'name' => 'term_lang_choice',
131 - 'value' => 'term_id',
132 - 'selected' => $lang ? $lang->term_id : '',
133 - 'flag' => true,
134 - )
135 - );
136 -
137 68 wp_nonce_field( 'pll_language', '_pll_nonce' );
138 69
139 70 printf(
140 71 '<div class="form-field">
@@ -142,73 +73,61 @@
142 73 <div id="select-add-term-language">%s</div>
143 74 <p>%s</p>
144 75 </div>',
145 76 esc_html__( 'Language', 'polylang' ),
146 - $dropdown_html, // phpcs:ignore
77 + $dropdown->walk(
78 + $this->model->get_languages_list(),
79 + array(
80 + 'name' => 'term_lang_choice',
81 + 'value' => 'term_id',
82 + 'selected' => $lang ? $lang->term_id : '',
83 + 'flag' => true,
84 + )
85 + ),
147 86 esc_html__( 'Sets the language', 'polylang' )
148 87 );
149 88
150 - if ( ! empty( $from_term_id ) ) {
151 - printf( '<input type="hidden" name="from_tag" value="%d" />', (int) $from_term_id );
89 + if ( ! empty( $_GET['from_tag'] ) ) {
90 + printf( '<input type="hidden" name="from_tag" value="%d" />', (int) $_GET['from_tag'] );
152 91 }
153 92
154 93 // Adds translation fields
155 94 echo '<div id="term-translations" class="form-field">';
156 95 if ( $lang ) {
157 - include __DIR__ . '/view-translations-term.php';
96 + include PLL_ADMIN_INC . '/view-translations-term.php';
158 97 }
159 98 echo '</div>' . "\n";
160 99 }
161 100
162 101 /**
163 - * Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels.
102 + * Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
164 103 *
165 104 * @since 0.1
166 105 *
167 - * @param WP_Term $tag The term being edited.
168 - * @return void
106 + * @param object $tag
169 107 */
170 108 public function edit_term_form( $tag ) {
171 - if ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
172 - $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification
173 - }
109 + $post_type = isset( $GLOBALS['post_type'] ) ? $GLOBALS['post_type'] : $_REQUEST['post_type'];
174 110
175 - if ( isset( $GLOBALS['post_type'] ) ) {
176 - $post_type = $GLOBALS['post_type'];
177 - }
178 -
179 - if ( ! isset( $post_type ) || ! post_type_exists( $post_type ) ) {
111 + if ( ! post_type_exists( $post_type ) ) {
180 112 return;
181 113 }
182 114
183 - $term_id = $tag->term_id;
184 - $taxonomy = $tag->taxonomy; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
115 + $term_id = $tag->term_id;
116 + $taxonomy = $tag->taxonomy;
185 117
186 118 $lang = $this->model->term->get_language( $term_id );
187 119 $lang = empty( $lang ) ? $this->pref_lang : $lang;
188 120
121 + $dropdown = new PLL_Walker_Dropdown();
122 +
189 123 // Disable the language dropdown and the translations input fields for default categories to prevent removal
190 124 $disabled = in_array( get_option( 'default_category' ), $this->model->term->get_translations( $term_id ) );
191 125
192 - $dropdown = new PLL_Walker_Dropdown();
193 -
194 - $dropdown_html = $dropdown->walk(
195 - $this->model->get_languages_list(),
196 - -1,
197 - array(
198 - 'name' => 'term_lang_choice',
199 - 'value' => 'term_id',
200 - 'selected' => $lang->term_id,
201 - 'disabled' => $disabled,
202 - 'flag' => true,
203 - )
204 - );
205 -
206 - wp_nonce_field( 'pll_language', '_pll_nonce' );
207 -
208 126 printf(
209 127 '<tr class="form-field">
210 128 <th scope="row">
129 + %s
211 130 <label for="term_lang_choice">%s</label>
212 131 </th>
213 132 <td id="select-edit-term-language">
214 133 %s<br />
@@ -214,15 +133,27 @@
214 133 %s<br />
215 134 <p class="description">%s</p>
216 135 </td>
217 136 </tr>',
137 + wp_nonce_field( 'pll_language', '_pll_nonce', true, false ),
218 138 esc_html__( 'Language', 'polylang' ),
219 - $dropdown_html, // phpcs:ignore
139 + $dropdown->walk(
140 + $this->model->get_languages_list(),
141 + array(
142 + 'name' => 'term_lang_choice',
143 + 'value' => 'term_id',
144 + 'selected' => $lang ? $lang->term_id : '',
145 + 'disabled' => $disabled,
146 + 'flag' => true,
147 + )
148 + ),
220 149 esc_html__( 'Sets the language', 'polylang' )
221 150 );
222 151
223 152 echo '<tr id="term-translations" class="form-field">';
224 - include __DIR__ . '/view-translations-term.php';
153 + if ( $lang ) {
154 + include PLL_ADMIN_INC . '/view-translations-term.php';
155 + }
225 156 echo '</tr>' . "\n";
226 157 }
227 158
228 159 /**
@@ -233,17 +164,12 @@
233 164 * @param string $output html markup for dropdown list of categories
234 165 * @return string modified html
235 166 */
236 167 public function wp_dropdown_cats( $output ) {
237 - if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
238 - $taxonomy = sanitize_key( $_GET['taxonomy'] ); // phpcs:ignore WordPress.Security.NonceVerification
239 - }
240 -
241 - if ( isset( $taxonomy, $_GET['from_tag'], $_GET['new_lang'] ) && taxonomy_exists( $taxonomy ) ) { // phpcs:ignore WordPress.Security.NonceVerification
242 - $term = get_term( (int) $_GET['from_tag'], $taxonomy ); // phpcs:ignore WordPress.Security.NonceVerification
243 -
244 - if ( $term instanceof WP_Term && $id = $term->parent ) {
245 - $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
168 + if ( isset( $_GET['taxonomy'], $_GET['from_tag'], $_GET['new_lang'] ) && taxonomy_exists( $_GET['taxonomy'] ) ) {
169 + $term = get_term( (int) $_GET['from_tag'], $_GET['taxonomy'] );
170 + if ( $term && $id = $term->parent ) {
171 + $lang = $this->model->get_language( $_GET['new_lang'] );
246 172 if ( $parent = $this->model->term->get_translation( $id, $lang ) ) {
247 173 return str_replace( '"' . $parent . '"', '"' . $parent . '" selected="selected"', $output );
248 174 }
249 175 }
@@ -256,12 +182,11 @@
256 182 *
257 183 * @since 1.7
258 184 *
259 185 * @param int $post_id
260 - * @return void
261 186 */
262 187 public function pre_post_update( $post_id ) {
263 - if ( isset( $_GET['bulk_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
188 + if ( isset( $_GET['bulk_edit'] ) ) {
264 189 $this->post_id = $post_id;
265 190 }
266 191 }
267 192
@@ -271,9 +196,8 @@
271 196 * @since 1.5
272 197 *
273 198 * @param int $term_id
274 199 * @param string $taxonomy
275 - * @return void
276 200 */
277 201 protected function save_language( $term_id, $taxonomy ) {
278 202 global $wpdb;
279 203 // Security checks are necessary to accept language modifications
@@ -280,15 +204,16 @@
280 204 // as 'wp_update_term' can be called from outside WP admin
281 205
282 206 // Edit tags
283 207 if ( isset( $_POST['term_lang_choice'] ) ) {
284 - if ( isset( $_POST['action'] ) && sanitize_key( $_POST['action'] ) === 'add-' . $taxonomy ) { // phpcs:ignore WordPress.Security.NonceVerification
285 - check_ajax_referer( 'add-' . $taxonomy, '_ajax_nonce-add-' . $taxonomy ); // Category metabox
286 - } else {
287 - check_admin_referer( 'pll_language', '_pll_nonce' ); // Edit tags or tags metabox
208 + if ( 'add-' . $taxonomy == $_POST['action'] ) {
209 + check_ajax_referer( $_POST['action'], '_ajax_nonce-add-' . $taxonomy ); // category metabox
288 210 }
211 + else {
212 + check_admin_referer( 'pll_language', '_pll_nonce' ); // edit tags or tags metabox
213 + }
289 214
290 - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ) );
215 + $this->model->term->set_language( $term_id, $this->model->get_language( $_POST['term_lang_choice'] ) );
291 216 }
292 217
293 218 // *Post* bulk edit, in case a new term is created
294 219 elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) {
@@ -317,12 +242,10 @@
317 242 );
318 243
319 244 // If we have several terms with the same name, they are translations of each other
320 245 if ( count( $terms ) > 1 ) {
321 - $translations = array();
322 -
323 246 foreach ( $terms as $term ) {
324 - $translations[ $this->model->term->get_language( $term->term_id )->slug ] = $term->term_id;
247 + $translations[ $this->model->term->get_language( $term->term_id )->slug ] = $term->term_id;
325 248 }
326 249
327 250 $this->model->term->save_translations( $term_id, $translations );
328 251 }
@@ -328,9 +251,9 @@
328 251 }
329 252 }
330 253
331 254 else {
332 - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ) );
255 + $this->model->term->set_language( $term_id, $this->model->get_language( $_GET['inline_lang_choice'] ) );
333 256 }
334 257 }
335 258
336 259 // Quick edit
@@ -340,9 +263,9 @@
340 263 '_inline_edit'
341 264 );
342 265
343 266 $old_lang = $this->model->term->get_language( $term_id ); // Stores the old language
344 - $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); // New language
267 + $lang = $this->model->get_language( $_POST['inline_lang_choice'] ); // New language
345 268 $translations = $this->model->term->get_translations( $term_id );
346 269
347 270 // Checks if the new language already exists in the translation group
348 271 if ( $old_lang && $old_lang->slug != $lang->slug ) {
@@ -361,32 +284,28 @@
361 284
362 285 // Edit post
363 286 elseif ( isset( $_POST['post_lang_choice'] ) ) { // FIXME should be useless now
364 287 check_admin_referer( 'pll_language', '_pll_nonce' );
365 - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ) );
288 + $this->model->term->set_language( $term_id, $this->model->get_language( $_POST['post_lang_choice'] ) );
366 289 }
367 290 }
368 291
369 292 /**
370 - * Save translations from our form.
293 + * Save translations from our form
371 294 *
372 295 * @since 1.5
373 296 *
374 - * @param int $term_id The term id of teh term being saved.
375 - * @return int[] The array of translated term ids.
297 + * @param int $term_id
298 + * @return array
376 299 */
377 300 protected function save_translations( $term_id ) {
378 - // Security check as 'wp_update_term' can be called from outside WP admin.
301 + // Security check as 'wp_update_term' can be called from outside WP admin
379 302 check_admin_referer( 'pll_language', '_pll_nonce' );
380 303
381 - $translations = array();
382 -
383 - // Save translations after checking the translated term is in the right language ( as well as cast id to int ).
384 - if ( isset( $_POST['term_tr_lang'] ) ) {
385 - foreach ( array_map( 'absint', $_POST['term_tr_lang'] ) as $lang => $tr_id ) {
386 - $tr_lang = $this->model->term->get_language( $tr_id );
387 - $translations[ $lang ] = $tr_lang && $tr_lang->slug == $lang ? $tr_id : 0;
388 - }
304 + // Save translations after checking the translated term is in the right language ( as well as cast id to int )
305 + foreach ( $_POST['term_tr_lang'] as $lang => $tr_id ) {
306 + $tr_lang = $this->model->term->get_language( (int) $tr_id );
307 + $translations[ $lang ] = $tr_lang && $tr_lang->slug == $lang ? (int) $tr_id : 0;
389 308 }
390 309
391 310 $this->model->term->save_translations( $term_id, $translations );
392 311
@@ -401,9 +320,8 @@
401 320 *
402 321 * @param int $term_id
403 322 * @param int $tt_id term taxonomy id
404 323 * @param string $taxonomy
405 - * @return void
406 324 */
407 325 public function save_term( $term_id, $tt_id, $taxonomy ) {
408 326 // Does nothing except on taxonomies which are filterable
409 327 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
@@ -409,22 +327,17 @@
409 327 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
410 328 return;
411 329 }
412 330
413 - $tax = get_taxonomy( $taxonomy );
414 -
415 - if ( empty( $tax ) ) {
416 - return;
417 - }
418 -
419 331 // Capability check
420 332 // As 'wp_update_term' can be called from outside WP admin
421 333 // 2nd test for creating tags when creating / editing a post
422 - if ( current_user_can( $tax->cap->edit_terms ) || ( isset( $_POST['tax_input'][ $taxonomy ] ) && current_user_can( $tax->cap->assign_terms ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
334 + $tax = get_taxonomy( $taxonomy );
335 + if ( current_user_can( $tax->cap->edit_terms ) || ( isset( $_POST['tax_input'][ $taxonomy ] ) && current_user_can( $tax->cap->assign_terms ) ) ) {
423 336 $this->save_language( $term_id, $taxonomy );
424 337
425 - if ( isset( $_POST['term_tr_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
426 - $this->save_translations( $term_id );
338 + if ( isset( $_POST['term_tr_lang'] ) ) {
339 + $translations = $this->save_translations( $term_id );
427 340 }
428 341 }
429 342 }
430 343
@@ -453,29 +366,25 @@
453 366 $name = sanitize_title( $this->pre_term_name );
454 367
455 368 // If the term already exists in another language
456 369 if ( ! $slug && $this->model->is_translated_taxonomy( $taxonomy ) && term_exists( $name, $taxonomy ) ) {
457 - if ( isset( $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
458 - $lang = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
370 + if ( isset( $_POST['term_lang_choice'] ) ) {
371 + $slug = $name . '-' . $this->model->get_language( $_POST['term_lang_choice'] )->slug;
459 372 }
460 373
461 - elseif ( isset( $_POST['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
462 - $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
374 + elseif ( isset( $_POST['inline_lang_choice'] ) ) {
375 + $slug = $name . '-' . $this->model->get_language( $_POST['inline_lang_choice'] )->slug;
463 376 }
464 377
465 378 // *Post* bulk edit, in case a new term is created
466 - elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
379 + elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) {
467 380 // Bulk edit does not modify the language
468 - if ( -1 == $_GET['inline_lang_choice'] ) { // phpcs:ignore WordPress.Security.NonceVerification
469 - $lang = $this->model->post->get_language( $this->post_id );
381 + if ( -1 == $_GET['inline_lang_choice'] ) {
382 + $slug = $name . '-' . $this->model->post->get_language( $this->post_id )->slug;
470 383 } else {
471 - $lang = $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
384 + $slug = $name . '-' . $this->model->get_language( $_GET['inline_lang_choice'] )->slug;
472 385 }
473 386 }
474 -
475 - if ( ! empty( $lang ) && ! $this->model->term_exists_by_slug( $name, $lang, $taxonomy ) ) {
476 - $slug = $name . '-' . $lang->slug;
477 - }
478 387 }
479 388
480 389 return $slug;
481 390 }
@@ -483,29 +392,25 @@
483 392 /**
484 393 * Ajax response for edit term form
485 394 *
486 395 * @since 0.2
487 - *
488 - * @return void
489 396 */
490 397 public function term_lang_choice() {
491 398 check_ajax_referer( 'pll_language', '_pll_nonce' );
492 399
493 - if ( ! isset( $_POST['taxonomy'], $_POST['post_type'], $_POST['lang'] ) ) {
494 - wp_die( 0 );
495 - }
400 + $lang = $this->model->get_language( $_POST['lang'] );
401 + $term_id = isset( $_POST['term_id'] ) ? (int) $_POST['term_id'] : null;
402 + $taxonomy = $_POST['taxonomy'];
403 + $post_type = $_POST['post_type'];
496 404
497 - $lang = $this->model->get_language( sanitize_key( $_POST['lang'] ) );
498 - $term_id = isset( $_POST['term_id'] ) ? (int) $_POST['term_id'] : null; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
499 - $taxonomy = sanitize_key( $_POST['taxonomy'] );
500 - $post_type = sanitize_key( $_POST['post_type'] );
501 -
502 - if ( empty( $lang ) || ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
503 - wp_die( 0 );
405 + if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
406 + die( 0 );
504 407 }
505 408
506 409 ob_start();
507 - include __DIR__ . '/view-translations-term.php';
410 + if ( $lang ) {
411 + include PLL_ADMIN_INC . '/view-translations-term.php';
412 + }
508 413 $x = new WP_Ajax_Response( array( 'what' => 'translations', 'data' => ob_get_contents() ) );
509 414 ob_end_clean();
510 415
511 416 // Parent dropdown list ( only for hierarchical taxonomies )
@@ -517,9 +422,9 @@
517 422 'taxonomy' => $taxonomy,
518 423 'name' => 'parent',
519 424 'orderby' => 'name',
520 425 'hierarchical' => true,
521 - 'show_option_none' => __( 'None', 'polylang' ),
426 + 'show_option_none' => __( 'None' ),
522 427 'echo' => 0,
523 428 );
524 429 $x->Add( array( 'what' => 'parent', 'data' => wp_dropdown_categories( $args ) ) );
525 430 }
@@ -527,9 +432,9 @@
527 432 // Tag cloud
528 433 // Tests copied from edit_tags.php
529 434 else {
530 435 $tax = get_taxonomy( $taxonomy );
531 - if ( ! empty( $tax ) && ! is_null( $tax->labels->popular_items ) ) {
436 + if ( ! is_null( $tax->labels->popular_items ) ) {
532 437 $args = array( 'taxonomy' => $taxonomy, 'echo' => false );
533 438 if ( current_user_can( $tax->cap->edit_terms ) ) {
534 439 $args = array_merge( $args, array( 'link' => 'edit' ) );
535 440 }
@@ -550,73 +455,53 @@
550 455 /**
551 456 * Ajax response for input in translation autocomplete input box
552 457 *
553 458 * @since 1.5
554 - *
555 - * @return void
556 459 */
557 460 public function ajax_terms_not_translated() {
558 461 check_ajax_referer( 'pll_language', '_pll_nonce' );
559 462
560 - if ( ! isset( $_GET['term'], $_GET['post_type'], $_GET['taxonomy'], $_GET['term_language'], $_GET['translation_language'] ) ) {
561 - wp_die( 0 );
562 - }
463 + $s = wp_unslash( $_GET['term'] );
464 + $post_type = $_GET['post_type'];
465 + $taxonomy = $_GET['taxonomy'];
563 466
564 - /** @var string */
565 - $s = wp_unslash( $_GET['term'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
566 - $post_type = sanitize_key( $_GET['post_type'] );
567 - $taxonomy = sanitize_key( $_GET['taxonomy'] );
568 -
569 467 if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
570 - wp_die( 0 );
468 + die( 0 );
571 469 }
572 470
573 - $term_language = $this->model->get_language( sanitize_key( $_GET['term_language'] ) );
574 - $translation_language = $this->model->get_language( sanitize_key( $_GET['translation_language'] ) );
471 + $term_language = $this->model->get_language( $_GET['term_language'] );
472 + $translation_language = $this->model->get_language( $_GET['translation_language'] );
575 473
576 - $terms = array();
577 474 $return = array();
578 475
579 - // Add current translation in list.
580 - // Not in add term as term_id is not set.
581 - if ( isset( $_GET['term_id'] ) && 'undefined' !== $_GET['term_id'] && $term_id = $this->model->term->get_translation( (int) $_GET['term_id'], $translation_language ) ) {
582 - $terms = array( get_term( $term_id, $taxonomy ) );
583 - }
476 + // It is more efficient to use one common query for all languages as soon as there are more than 2
477 + foreach ( get_terms( $taxonomy, 'hide_empty=0&lang=0&name__like=' . $s ) as $term ) {
478 + $lang = $this->model->term->get_language( $term->term_id );
584 479
585 - // It is more efficient to use one common query for all languages as soon as there are more than 2.
586 - $all_terms = get_terms( $taxonomy, 'hide_empty=0&lang=0&name__like=' . $s );
587 - if ( is_array( $all_terms ) ) {
588 - foreach ( $all_terms as $term ) {
589 - $lang = $this->model->term->get_language( $term->term_id );
590 -
591 - if ( $lang && $lang->slug == $translation_language->slug && ! $this->model->term->get_translation( $term->term_id, $term_language ) ) {
592 - $terms[] = $term;
593 - }
594 - }
595 - }
596 -
597 - // Format the ajax response.
598 - foreach ( $terms as $term ) {
599 - if ( $term instanceof WP_Term ) {
480 + if ( $lang && $lang->slug == $translation_language->slug && ! $this->model->term->get_translation( $term->term_id, $term_language ) ) {
600 481 $return[] = array(
601 482 'id' => $term->term_id,
602 - 'value' => rtrim( // Trim the seperator added at the end by WP.
603 - get_term_parents_list(
604 - $term->term_id,
605 - $term->taxonomy,
606 - array(
607 - 'separator' => ' > ',
608 - 'link' => false,
609 - )
610 - ),
611 - ' >'
612 - ),
613 - 'link' => $this->links->edit_term_translation_link( $term->term_id, $term->taxonomy, $post_type ),
483 + 'value' => $term->name,
484 + 'link' => $this->links->edit_term_translation_link( $term->term_id, $taxonomy, $post_type ),
614 485 );
615 486 }
616 487 }
617 488
618 - wp_die( wp_json_encode( $return ) );
489 + // Add current translation in list
490 + // Not in add term for as term_id is not set
491 + if ( 'undefined' !== $_GET['term_id'] && $term_id = $this->model->term->get_translation( (int) $_GET['term_id'], $translation_language ) ) {
492 + $term = get_term( $term_id, $taxonomy );
493 + array_unshift(
494 + $return,
495 + array(
496 + 'id' => $term_id,
497 + 'value' => $term->name,
498 + 'link' => $this->links->edit_term_translation_link( $term->term_id, $taxonomy, $post_type ),
499 + )
500 + );
501 + }
502 +
503 + wp_die( json_encode( $return ) );
619 504 }
620 505
621 506 /**
622 507 * Filters the default category in note below the category list table and in settings->writing dropdown
@@ -640,9 +525,8 @@
640 525 * @since 1.7
641 526 *
642 527 * @param int $old_value
643 528 * @param int $value
644 - * @return void
645 529 */
646 530 public function update_option_default_category( $old_value, $value ) {
647 531 $default_cat_lang = $this->model->term->get_language( $value );
648 532
@@ -668,9 +552,8 @@
668 552 * @param int $term_id Shared term_id
669 553 * @param int $new_term_id
670 554 * @param int $term_taxonomy_id
671 555 * @param string $taxonomy
672 - * @return void
673 556 */
674 557 public function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
675 558 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
676 559 return;
@@ -681,16 +564,11 @@
681 564 if ( $avoid_recursion ) {
682 565 return;
683 566 }
684 567
568 + $avoid_recursion = true;
685 569 $lang = $this->model->term->get_language( $term_id );
686 - if ( empty( $lang ) ) {
687 - return;
688 - }
689 570
690 - $avoid_recursion = true;
691 - $translations = array();
692 -
693 571 foreach ( $this->model->term->get_translations( $term_id ) as $key => $tr_id ) {
694 572 if ( $lang->slug == $key ) {
695 573 $translations[ $key ] = $new_term_id;
696 574 }
@@ -698,9 +576,9 @@
698 576 $tr_term = get_term( $tr_id, $taxonomy );
699 577 $translations[ $key ] = _split_shared_term( $tr_id, $tr_term->term_taxonomy_id );
700 578
701 579 // Hack translation ids sent by the form to avoid overwrite in PLL_Admin_Filters_Term::save_translations
702 - if ( isset( $_POST['term_tr_lang'][ $key ] ) && $_POST['term_tr_lang'][ $key ] == $tr_id ) { // phpcs:ignore WordPress.Security.NonceVerification
580 + if ( isset( $_POST['term_tr_lang'][ $key ] ) && $_POST['term_tr_lang'][ $key ] == $tr_id ) {
703 581 $_POST['term_tr_lang'][ $key ] = $translations[ $key ];
704 582 }
705 583 }
706 584 $this->model->term->set_language( $translations[ $key ], $key );