PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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 3.0.3 All 233 releases
← All changes | admin/admin-filters-term.php +263 -193 2.93.7.7 View file →
@@ -2,8 +2,10 @@
2 2 /**
3 3 * @package Polylang
4 4 */
5 5
6 +defined( 'ABSPATH' ) || exit;
7 +
6 8 /**
7 9 * Manages filters and actions related to terms on admin side
8 10 *
9 11 * @since 1.2
@@ -8,22 +10,60 @@
8 10 *
9 11 * @since 1.2
10 12 */
11 13 class PLL_Admin_Filters_Term {
12 - public $links, $model, $options, $pref_lang;
13 - protected $pre_term_name; // Used to store the term name before creating a slug if needed
14 - protected $post_id; // Used to store the current post_id when bulk editing posts
14 + /**
15 + * @var PLL_Model
16 + */
17 + public $model;
15 18
16 19 /**
17 - * Constructor: setups filters and actions
20 + * @var PLL_Admin_Links
21 + */
22 + public $links;
23 +
24 + /**
25 + * Language selected in the admin language filter.
18 26 *
19 - * @param object $polylang
27 + * @var PLL_Language
20 28 */
29 + public $filter_lang;
30 +
31 + /**
32 + * Preferred language to assign to the new terms.
33 + *
34 + * @var PLL_Language
35 + */
36 + public $pref_lang;
37 +
38 + /**
39 + * Stores the current post_id when bulk editing posts.
40 + *
41 + * @var int
42 + */
43 + protected $post_id = 0;
44 +
45 + /**
46 + * A reference to the PLL_Admin_Default_Term instance.
47 + *
48 + * @since 2.8
49 + *
50 + * @var PLL_Admin_Default_Term|null
51 + */
52 + protected $default_term;
53 +
54 + /**
55 + * Constructor: setups filters and actions.
56 + *
57 + * @since 1.2
58 + *
59 + * @param object $polylang The Polylang object.
60 + */
21 61 public function __construct( &$polylang ) {
22 - $this->links = &$polylang->links;
23 - $this->model = &$polylang->model;
24 - $this->options = &$polylang->options;
25 - $this->pref_lang = &$polylang->pref_lang;
62 + $this->links = &$polylang->links;
63 + $this->model = &$polylang->model;
64 + $this->pref_lang = &$polylang->pref_lang;
65 + $this->default_term = &$polylang->default_term;
26 66
27 67 foreach ( $this->model->get_translated_taxonomies() as $tax ) {
28 68 // Adds the language field in the 'Categories' and 'Post Tags' panels
29 69 add_action( $tax . '_add_form_fields', array( $this, 'add_term_form' ) );
@@ -36,19 +76,15 @@
36 76 add_filter( 'wp_dropdown_cats', array( $this, 'wp_dropdown_cats' ) );
37 77 add_action( 'create_term', array( $this, 'save_term' ), 900, 3 );
38 78 add_action( 'edit_term', array( $this, 'save_term' ), 900, 3 ); // Late as it may conflict with other plugins, see http://wordpress.org/support/topic/polylang-and-wordpress-seo-by-yoast
39 79 add_action( 'pre_post_update', array( $this, 'pre_post_update' ) );
40 - add_filter( 'pre_term_name', array( $this, 'pre_term_name' ) );
41 - add_filter( 'pre_term_slug', array( $this, 'pre_term_slug' ), 10, 2 );
80 + add_filter( 'pll_inserted_term_language', array( $this, 'get_inserted_term_language' ) );
81 + add_filter( 'pll_inserted_term_parent', array( $this, 'get_inserted_term_parent' ), 10, 2 );
42 82
43 83 // Ajax response for edit term form
44 84 add_action( 'wp_ajax_term_lang_choice', array( $this, 'term_lang_choice' ) );
45 85 add_action( 'wp_ajax_pll_terms_not_translated', array( $this, 'ajax_terms_not_translated' ) );
46 86
47 - // Allows to get the default categories in all languages
48 - add_filter( 'option_default_category', array( $this, 'option_default_category' ) );
49 - add_action( 'update_option_default_category', array( $this, 'update_option_default_category' ), 10, 2 );
50 -
51 87 // Updates the translations term ids when splitting a shared term
52 88 add_action( 'split_shared_term', array( $this, 'split_shared_term' ), 10, 4 ); // WP 4.2
53 89 }
54 90
@@ -55,8 +91,10 @@
55 91 /**
56 92 * Adds the language field in the 'Categories' and 'Post Tags' panels
57 93 *
58 94 * @since 0.1
95 + *
96 + * @return void
59 97 */
60 98 public function add_term_form() {
61 99 if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
62 100 $taxonomy = sanitize_key( $_GET['taxonomy'] ); // phpcs:ignore WordPress.Security.NonceVerification
@@ -69,9 +107,9 @@
69 107 if ( isset( $GLOBALS['post_type'] ) ) {
70 108 $post_type = $GLOBALS['post_type'];
71 109 }
72 110
73 - if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) {
111 + if ( ! isset( $taxonomy, $post_type ) || ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) {
74 112 return;
75 113 }
76 114
77 115 $from_term_id = isset( $_GET['from_tag'] ) ? (int) $_GET['from_tag'] : 0; // phpcs:ignore WordPress.Security.NonceVerification
@@ -116,13 +154,14 @@
116 154 echo '</div>' . "\n";
117 155 }
118 156
119 157 /**
120 - * Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
158 + * Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels.
121 159 *
122 160 * @since 0.1
123 161 *
124 - * @param object $tag
162 + * @param WP_Term $tag The term being edited.
163 + * @return void
125 164 */
126 165 public function edit_term_form( $tag ) {
127 166 if ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
128 167 $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification
@@ -131,9 +170,9 @@
131 170 if ( isset( $GLOBALS['post_type'] ) ) {
132 171 $post_type = $GLOBALS['post_type'];
133 172 }
134 173
135 - if ( ! post_type_exists( $post_type ) ) {
174 + if ( ! isset( $post_type ) || ! post_type_exists( $post_type ) ) {
136 175 return;
137 176 }
138 177
139 178 $term_id = $tag->term_id;
@@ -141,10 +180,10 @@
141 180
142 181 $lang = $this->model->term->get_language( $term_id );
143 182 $lang = empty( $lang ) ? $this->pref_lang : $lang;
144 183
145 - // Disable the language dropdown and the translations input fields for default categories to prevent removal
146 - $disabled = in_array( get_option( 'default_category' ), $this->model->term->get_translations( $term_id ) );
184 + // Disable the language dropdown and the translations input fields for default terms to prevent removal
185 + $disabled = $this->default_term->is_default_term( $term_id );
147 186
148 187 $dropdown = new PLL_Walker_Dropdown();
149 188
150 189 $dropdown_html = $dropdown->walk(
@@ -152,9 +191,9 @@
152 191 -1,
153 192 array(
154 193 'name' => 'term_lang_choice',
155 194 'value' => 'term_id',
156 - 'selected' => $lang ? $lang->term_id : '',
195 + 'selected' => $lang->term_id,
157 196 'disabled' => $disabled,
158 197 'flag' => true,
159 198 )
160 199 );
@@ -176,11 +215,9 @@
176 215 esc_html__( 'Sets the language', 'polylang' )
177 216 );
178 217
179 218 echo '<tr id="term-translations" class="form-field">';
180 - if ( $lang ) {
181 - include __DIR__ . '/view-translations-term.php';
182 - }
219 + include __DIR__ . '/view-translations-term.php';
183 220 echo '</tr>' . "\n";
184 221 }
185 222
186 223 /**
@@ -198,9 +235,9 @@
198 235
199 236 if ( isset( $taxonomy, $_GET['from_tag'], $_GET['new_lang'] ) && taxonomy_exists( $taxonomy ) ) { // phpcs:ignore WordPress.Security.NonceVerification
200 237 $term = get_term( (int) $_GET['from_tag'], $taxonomy ); // phpcs:ignore WordPress.Security.NonceVerification
201 238
202 - if ( $term && $id = $term->parent ) {
239 + if ( $term instanceof WP_Term && $id = $term->parent ) {
203 240 $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
204 241 if ( $parent = $this->model->term->get_translation( $id, $lang ) ) {
205 242 return str_replace( '"' . $parent . '"', '"' . $parent . '" selected="selected"', $output );
206 243 }
@@ -209,13 +246,14 @@
209 246 return $output;
210 247 }
211 248
212 249 /**
213 - * Stores the current post_id when bulk editing posts for use in save_language and pre_term_slug
250 + * Stores the current post_id when bulk editing posts for use in save_language and get_inserted_term_language.
214 251 *
215 252 * @since 1.7
216 253 *
217 - * @param int $post_id
254 + * @param int $post_id Post ID.
255 + * @return void
218 256 */
219 257 public function pre_post_update( $post_id ) {
220 258 if ( isset( $_GET['bulk_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
221 259 $this->post_id = $post_id;
@@ -222,14 +260,15 @@
222 260 }
223 261 }
224 262
225 263 /**
226 - * Saves language
264 + * Saves the language of a term.
227 265 *
228 266 * @since 1.5
229 267 *
230 - * @param int $term_id
231 - * @param string $taxonomy
268 + * @param int $term_id Term ID.
269 + * @param string $taxonomy Taxonomy name.
270 + * @return void
232 271 */
233 272 protected function save_language( $term_id, $taxonomy ) {
234 273 global $wpdb;
235 274 // Security checks are necessary to accept language modifications
@@ -242,9 +281,13 @@
242 281 } else {
243 282 check_admin_referer( 'pll_language', '_pll_nonce' ); // Edit tags or tags metabox
244 283 }
245 284
246 - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ) );
285 + $language = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) );
286 +
287 + if ( ! empty( $language ) ) {
288 + $this->model->term->set_language( $term_id, $language );
289 + }
247 290 }
248 291
249 292 // *Post* bulk edit, in case a new term is created
250 293 elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) {
@@ -251,27 +294,36 @@
251 294 check_admin_referer( 'bulk-posts' );
252 295
253 296 // Bulk edit does not modify the language
254 297 // So we possibly create a tag in several languages
255 - if ( -1 == $_GET['inline_lang_choice'] ) {
256 - // The language of the current term is set a according to the language of the current post
257 - $this->model->term->set_language( $term_id, $this->model->post->get_language( $this->post_id ) );
258 - $term = get_term( $term_id, $taxonomy );
298 + if ( -1 === (int) $_GET['inline_lang_choice'] ) {
299 + // The language of the current term is set a according to the language of the current post.
300 + $language = $this->model->post->get_language( $this->post_id );
259 301
302 + if ( empty( $language ) ) {
303 + return;
304 + }
305 +
306 + $this->model->term->set_language( $term_id, $language );
307 + $term = get_term( $term_id, $taxonomy );
308 + $terms = array();
309 +
260 310 // Get all terms with the same name
261 311 // FIXME backward compatibility WP < 4.2
262 312 // No WP function to get all terms with the exact same name so let's use a custom query
263 313 // $terms = get_terms( $taxonomy, array( 'name' => $term->name, 'hide_empty' => false, 'fields' => 'ids' ) ); should be OK in 4.2
264 314 // I may need to rework the loop below
265 - $terms = $wpdb->get_results(
266 - $wpdb->prepare(
267 - "SELECT t.term_id FROM $wpdb->terms AS t
268 - INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
269 - WHERE tt.taxonomy = %s AND t.name = %s",
270 - $taxonomy,
271 - $term->name
272 - )
273 - );
315 + if ( $term instanceof WP_Term ) {
316 + $terms = $wpdb->get_results(
317 + $wpdb->prepare(
318 + "SELECT t.term_id FROM $wpdb->terms AS t
319 + INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
320 + WHERE tt.taxonomy = %s AND t.name = %s",
321 + $taxonomy,
322 + $term->name
323 + )
324 + );
325 + }
274 326
275 327 // If we have several terms with the same name, they are translations of each other
276 328 if ( count( $terms ) > 1 ) {
277 329 $translations = array();
@@ -283,9 +335,9 @@
283 335 $this->model->term->save_translations( $term_id, $translations );
284 336 }
285 337 }
286 338
287 - else {
339 + elseif ( current_user_can( 'edit_term', $term_id ) ) {
288 340 $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ) );
289 341 }
290 342 }
291 343
@@ -295,49 +347,39 @@
295 347 isset( $_POST['action'] ) && 'inline-save' == $_POST['action'] ? 'inlineeditnonce' : 'taxinlineeditnonce', // Post quick edit or tag quick edit ?
296 348 '_inline_edit'
297 349 );
298 350
299 - $old_lang = $this->model->term->get_language( $term_id ); // Stores the old language
300 - $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); // New language
301 - $translations = $this->model->term->get_translations( $term_id );
302 -
303 - // Checks if the new language already exists in the translation group
304 - if ( $old_lang && $old_lang->slug != $lang->slug ) {
305 - if ( array_key_exists( $lang->slug, $translations ) ) {
306 - $this->model->term->delete_translation( $term_id );
307 - }
308 -
309 - elseif ( array_key_exists( $old_lang->slug, $translations ) ) {
310 - unset( $translations[ $old_lang->slug ] );
311 - $this->model->term->save_translations( $term_id, $translations );
312 - }
313 - }
314 -
315 - $this->model->term->set_language( $term_id, $lang ); // Set new language
351 + $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) );
352 + $this->model->term->set_language( $term_id, $lang );
316 353 }
317 354
318 355 // Edit post
319 356 elseif ( isset( $_POST['post_lang_choice'] ) ) { // FIXME should be useless now
320 357 check_admin_referer( 'pll_language', '_pll_nonce' );
321 - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ) );
358 +
359 + $language = $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) );
360 +
361 + if ( ! empty( $language ) ) {
362 + $this->model->term->set_language( $term_id, $language );
363 + }
322 364 }
323 365 }
324 366
325 367 /**
326 - * Save translations from our form
368 + * Save translations from our form.
327 369 *
328 370 * @since 1.5
329 371 *
330 - * @param int $term_id
331 - * @return array
372 + * @param int $term_id The term id of the term being saved.
373 + * @return int[] The array of translated term ids.
332 374 */
333 375 protected function save_translations( $term_id ) {
334 - // Security check as 'wp_update_term' can be called from outside WP admin
376 + // Security check as 'wp_update_term' can be called from outside WP admin.
335 377 check_admin_referer( 'pll_language', '_pll_nonce' );
336 378
337 379 $translations = array();
338 380
339 - // Save translations after checking the translated term is in the right language ( as well as cast id to int )
381 + // Save translations after checking the translated term is in the right language ( as well as cast id to int ).
340 382 if ( isset( $_POST['term_tr_lang'] ) ) {
341 383 foreach ( array_map( 'absint', $_POST['term_tr_lang'] ) as $lang => $tr_id ) {
342 384 $tr_lang = $this->model->term->get_language( $tr_id );
343 385 $translations[ $lang ] = $tr_lang && $tr_lang->slug == $lang ? $tr_id : 0;
@@ -354,11 +396,12 @@
354 396 * Saves language and translations
355 397 *
356 398 * @since 0.1
357 399 *
358 - * @param int $term_id
359 - * @param int $tt_id term taxonomy id
360 - * @param string $taxonomy
400 + * @param int $term_id Term ID.
401 + * @param int $tt_id Term taxonomy ID.
402 + * @param string $taxonomy Taxonomy name.
403 + * @return void
361 404 */
362 405 public function save_term( $term_id, $tt_id, $taxonomy ) {
363 406 // Does nothing except on taxonomies which are filterable
364 407 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
@@ -364,12 +407,17 @@
364 407 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
365 408 return;
366 409 }
367 410
411 + $tax = get_taxonomy( $taxonomy );
412 +
413 + if ( empty( $tax ) ) {
414 + return;
415 + }
416 +
368 417 // Capability check
369 418 // As 'wp_update_term' can be called from outside WP admin
370 419 // 2nd test for creating tags when creating / editing a post
371 - $tax = get_taxonomy( $taxonomy );
372 420 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
373 421 $this->save_language( $term_id, $taxonomy );
374 422
375 423 if ( isset( $_POST['term_tr_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
@@ -378,63 +426,13 @@
378 426 }
379 427 }
380 428
381 429 /**
382 - * Stores the term name for use in pre_term_slug
383 - *
384 - * @since 0.9.5
385 - *
386 - * @param string $name term name
387 - * @return string unmodified term name
388 - */
389 - public function pre_term_name( $name ) {
390 - return $this->pre_term_name = $name;
391 - }
392 -
393 - /**
394 - * Creates the term slug in case the term already exists in another language
395 - *
396 - * @since 0.9.5
397 - *
398 - * @param string $slug
399 - * @param string $taxonomy
400 - * @return string
401 - */
402 - public function pre_term_slug( $slug, $taxonomy ) {
403 - $name = sanitize_title( $this->pre_term_name );
404 -
405 - // If the term already exists in another language
406 - if ( ! $slug && $this->model->is_translated_taxonomy( $taxonomy ) && term_exists( $name, $taxonomy ) ) {
407 - if ( isset( $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
408 - $lang = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
409 - }
410 -
411 - elseif ( isset( $_POST['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
412 - $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
413 - }
414 -
415 - // *Post* bulk edit, in case a new term is created
416 - elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
417 - // Bulk edit does not modify the language
418 - if ( -1 == $_GET['inline_lang_choice'] ) { // phpcs:ignore WordPress.Security.NonceVerification
419 - $lang = $this->model->post->get_language( $this->post_id );
420 - } else {
421 - $lang = $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
422 - }
423 - }
424 -
425 - if ( ! empty( $lang ) && ! $this->model->term_exists_by_slug( $name, $lang, $taxonomy ) ) {
426 - $slug = $name . '-' . $lang->slug;
427 - }
428 - }
429 -
430 - return $slug;
431 - }
432 -
433 - /**
434 430 * Ajax response for edit term form
435 431 *
436 432 * @since 0.2
433 + *
434 + * @return void
437 435 */
438 436 public function term_lang_choice() {
439 437 check_ajax_referer( 'pll_language', '_pll_nonce' );
440 438
@@ -446,16 +444,14 @@
446 444 $term_id = isset( $_POST['term_id'] ) ? (int) $_POST['term_id'] : null; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
447 445 $taxonomy = sanitize_key( $_POST['taxonomy'] );
448 446 $post_type = sanitize_key( $_POST['post_type'] );
449 447
450 - if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
448 + if ( empty( $lang ) || ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
451 449 wp_die( 0 );
452 450 }
453 451
454 452 ob_start();
455 - if ( $lang ) {
456 - include __DIR__ . '/view-translations-term.php';
457 - }
453 + include __DIR__ . '/view-translations-term.php';
458 454 $x = new WP_Ajax_Response( array( 'what' => 'translations', 'data' => ob_get_contents() ) );
459 455 ob_end_clean();
460 456
461 457 // Parent dropdown list ( only for hierarchical taxonomies )
@@ -477,15 +473,18 @@
477 473 // Tag cloud
478 474 // Tests copied from edit_tags.php
479 475 else {
480 476 $tax = get_taxonomy( $taxonomy );
481 - if ( ! is_null( $tax->labels->popular_items ) ) {
477 + if ( ! empty( $tax ) && ! is_null( $tax->labels->popular_items ) ) {
482 478 $args = array( 'taxonomy' => $taxonomy, 'echo' => false );
483 479 if ( current_user_can( $tax->cap->edit_terms ) ) {
484 480 $args = array_merge( $args, array( 'link' => 'edit' ) );
485 481 }
486 482
487 - if ( $tag_cloud = wp_tag_cloud( $args ) ) {
483 + $tag_cloud = wp_tag_cloud( $args );
484 +
485 + if ( ! empty( $tag_cloud ) ) {
486 + /** @phpstan-var non-falsy-string $tag_cloud */
488 487 $html = sprintf( '<div class="tagcloud"><h2>%1$s</h2>%2$s</div>', esc_html( $tax->labels->popular_items ), $tag_cloud );
489 488 $x->Add( array( 'what' => 'tag_cloud', 'data' => $html ) );
490 489 }
491 490 }
@@ -497,11 +496,13 @@
497 496 $x->send();
498 497 }
499 498
500 499 /**
501 - * Ajax response for input in translation autocomplete input box
500 + * Ajax response for input in translation autocomplete input box.
502 501 *
503 502 * @since 1.5
503 + *
504 + * @return void
504 505 */
505 506 public function ajax_terms_not_translated() {
506 507 check_ajax_referer( 'pll_language', '_pll_nonce' );
507 508
@@ -508,8 +509,9 @@
508 509 if ( ! isset( $_GET['term'], $_GET['post_type'], $_GET['taxonomy'], $_GET['term_language'], $_GET['translation_language'] ) ) {
509 510 wp_die( 0 );
510 511 }
511 512
513 + /** @var string */
512 514 $s = wp_unslash( $_GET['term'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
513 515 $post_type = sanitize_key( $_GET['post_type'] );
514 516 $taxonomy = sanitize_key( $_GET['taxonomy'] );
515 517
@@ -529,31 +531,41 @@
529 531 $terms = array( get_term( $term_id, $taxonomy ) );
530 532 }
531 533
532 534 // It is more efficient to use one common query for all languages as soon as there are more than 2.
533 - foreach ( get_terms( $taxonomy, 'hide_empty=0&lang=0&name__like=' . $s ) as $term ) {
534 - $lang = $this->model->term->get_language( $term->term_id );
535 + $all_terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'lang' => '', 'name__like' => $s ) );
536 + if ( is_array( $all_terms ) ) {
537 + foreach ( $all_terms as $term ) {
538 + $lang = $this->model->term->get_language( $term->term_id );
535 539
536 - if ( $lang && $lang->slug == $translation_language->slug && ! $this->model->term->get_translation( $term->term_id, $term_language ) ) {
537 - $terms[] = $term;
540 + if ( $lang && $lang->slug == $translation_language->slug && ! $this->model->term->get_translation( $term->term_id, $term_language ) ) {
541 + $terms[] = $term;
542 + }
538 543 }
539 544 }
540 545
541 546 // Format the ajax response.
542 547 foreach ( $terms as $term ) {
548 + if ( ! $term instanceof WP_Term ) {
549 + continue;
550 + }
551 +
552 + $parents_list = get_term_parents_list(
553 + $term->term_id,
554 + $term->taxonomy,
555 + array(
556 + 'separator' => ' > ',
557 + 'link' => false,
558 + )
559 + );
560 +
561 + if ( ! is_string( $parents_list ) ) {
562 + continue;
563 + }
564 +
543 565 $return[] = array(
544 566 'id' => $term->term_id,
545 - 'value' => rtrim( // Trim the seperator added at the end by WP.
546 - get_term_parents_list(
547 - $term->term_id,
548 - $term->taxonomy,
549 - array(
550 - 'separator' => ' > ',
551 - 'link' => false,
552 - )
553 - ),
554 - ' >'
555 - ),
567 + 'value' => rtrim( $parents_list, ' >' ), // Trim the separator added at the end by WP.
556 568 'link' => $this->links->edit_term_translation_link( $term->term_id, $term->taxonomy, $post_type ),
557 569 );
558 570 }
559 571
@@ -560,57 +572,18 @@
560 572 wp_die( wp_json_encode( $return ) );
561 573 }
562 574
563 575 /**
564 - * Filters the default category in note below the category list table and in settings->writing dropdown
565 - *
566 - * @since 1.2
567 - *
568 - * @param int $value
569 - * @return int
570 - */
571 - public function option_default_category( $value ) {
572 - if ( isset( $this->pref_lang ) && $tr = $this->model->term->get( $value, $this->pref_lang ) ) {
573 - $value = $tr;
574 - }
575 - return $value;
576 - }
577 -
578 - /**
579 - * Checks if the new default category is translated in all languages
580 - * If not, create the translations
581 - *
582 - * @since 1.7
583 - *
584 - * @param int $old_value
585 - * @param int $value
586 - */
587 - public function update_option_default_category( $old_value, $value ) {
588 - $default_cat_lang = $this->model->term->get_language( $value );
589 -
590 - // Assign a default language to default category
591 - if ( ! $default_cat_lang ) {
592 - $default_cat_lang = $this->model->get_language( $this->options['default_lang'] );
593 - $this->model->term->set_language( (int) $value, $default_cat_lang );
594 - }
595 -
596 - foreach ( $this->model->get_languages_list() as $language ) {
597 - if ( $language->slug != $default_cat_lang->slug && ! $this->model->term->get_translation( $value, $language ) ) {
598 - $this->model->create_default_category( $language );
599 - }
600 - }
601 - }
602 -
603 - /**
604 576 * Updates the translations term ids when splitting a shared term
605 577 * Splits translations if these are shared terms too
606 578 *
607 579 * @since 1.7
608 580 *
609 - * @param int $term_id Shared term_id
610 - * @param int $new_term_id
611 - * @param int $term_taxonomy_id
612 - * @param string $taxonomy
581 + * @param int $term_id ID of the formerly shared term.
582 + * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
583 + * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
584 + * @param string $taxonomy Taxonomy name.
585 + * @return void
613 586 */
614 587 public function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
615 588 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
616 589 return;
@@ -621,10 +594,14 @@
621 594 if ( $avoid_recursion ) {
622 595 return;
623 596 }
624 597
598 + $lang = $this->model->term->get_language( $term_id );
599 + if ( empty( $lang ) ) {
600 + return;
601 + }
602 +
625 603 $avoid_recursion = true;
626 - $lang = $this->model->term->get_language( $term_id );
627 604 $translations = array();
628 605
629 606 foreach ( $this->model->term->get_translations( $term_id ) as $key => $tr_id ) {
630 607 if ( $lang->slug == $key ) {
@@ -631,18 +608,111 @@
631 608 $translations[ $key ] = $new_term_id;
632 609 }
633 610 else {
634 611 $tr_term = get_term( $tr_id, $taxonomy );
635 - $translations[ $key ] = _split_shared_term( $tr_id, $tr_term->term_taxonomy_id );
636 612
613 + if ( ! $tr_term instanceof WP_Term ) {
614 + continue;
615 + }
616 +
617 + $split_term_id = _split_shared_term( $tr_id, $tr_term->term_taxonomy_id );
618 +
619 + if ( is_int( $split_term_id ) ) {
620 + $translations[ $key ] = $split_term_id;
621 + } else {
622 + $translations[ $key ] = $tr_id;
623 + }
624 +
637 625 // Hack translation ids sent by the form to avoid overwrite in PLL_Admin_Filters_Term::save_translations
638 626 if ( isset( $_POST['term_tr_lang'][ $key ] ) && $_POST['term_tr_lang'][ $key ] == $tr_id ) { // phpcs:ignore WordPress.Security.NonceVerification
639 627 $_POST['term_tr_lang'][ $key ] = $translations[ $key ];
640 628 }
641 629 }
630 +
642 631 $this->model->term->set_language( $translations[ $key ], $key );
643 632 }
644 633
645 634 $this->model->term->save_translations( $new_term_id, $translations );
646 635 $avoid_recursion = false;
636 + }
637 +
638 + /**
639 + * Returns the language for subsequently inserted term in admin.
640 + *
641 + * @since 3.3
642 + *
643 + * @param PLL_Language|null $lang Term language object if found, null otherwise.
644 + * @return PLL_Language|null Language object, null if none found.
645 + */
646 + public function get_inserted_term_language( $lang ) {
647 + if ( $lang instanceof PLL_Language ) {
648 + return $lang;
649 + }
650 +
651 + if ( ! empty( $_POST['term_lang_choice'] ) && is_string( $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
652 + $lang_slug = sanitize_key( $_POST['term_lang_choice'] ); // phpcs:ignore WordPress.Security.NonceVerification
653 + $lang = $this->model->get_language( $lang_slug );
654 + return $lang instanceof PLL_Language ? $lang : null;
655 + }
656 +
657 + if ( ! empty( $_POST['inline_lang_choice'] ) && is_string( $_POST['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
658 + $lang_slug = sanitize_key( $_POST['inline_lang_choice'] ); // phpcs:ignore WordPress.Security.NonceVerification
659 + $lang = $this->model->get_language( $lang_slug );
660 + return $lang instanceof PLL_Language ? $lang : null;
661 + }
662 +
663 + // *Post* bulk edit, in case a new term is created
664 + if ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
665 + // Bulk edit does not modify the language
666 + if ( -1 === (int) $_GET['inline_lang_choice'] ) { // phpcs:ignore WordPress.Security.NonceVerification
667 + $lang = $this->model->post->get_language( $this->post_id );
668 + return $lang instanceof PLL_Language ? $lang : null;
669 + } elseif ( is_string( $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
670 + $lang_slug = sanitize_key( $_GET['inline_lang_choice'] ); // phpcs:ignore WordPress.Security.NonceVerification
671 + $lang = $this->model->get_language( $lang_slug );
672 + return $lang instanceof PLL_Language ? $lang : null;
673 + }
674 + }
675 +
676 + // Special cases for default categories as the select is disabled.
677 + $default_term = get_option( 'default_category' );
678 +
679 + if ( ! is_numeric( $default_term ) ) {
680 + return null;
681 + }
682 +
683 + if ( ! empty( $_POST['tag_ID'] ) && in_array( (int) $default_term, $this->model->term->get_translations( (int) $_POST['tag_ID'] ), true ) ) { // phpcs:ignore WordPress.Security.NonceVerification
684 + $lang = $this->model->term->get_language( (int) $_POST['tag_ID'] ); // phpcs:ignore WordPress.Security.NonceVerification
685 + return $lang instanceof PLL_Language ? $lang : null;
686 + }
687 +
688 + if ( ! empty( $_POST['tax_ID'] ) && in_array( (int) $default_term, $this->model->term->get_translations( (int) $_POST['tax_ID'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
689 + $lang = $this->model->term->get_language( (int) $_POST['tax_ID'] ); // phpcs:ignore WordPress.Security.NonceVerification
690 + return $lang instanceof PLL_Language ? $lang : null;
691 + }
692 +
693 + return null;
694 + }
695 +
696 + /**
697 + * Filters the subsequently inserted term parent in admin.
698 + *
699 + * @since 3.3
700 + *
701 + * @param int $parent Parent term ID, 0 if none found.
702 + * @param string $taxonomy Term taxonomy.
703 + * @return int Parent term ID if found, 0 otherwise.
704 + */
705 + public function get_inserted_term_parent( $parent, $taxonomy ) {
706 + if ( $parent ) {
707 + return $parent;
708 + }
709 +
710 + if ( isset( $_POST['parent'], $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
711 + $parent = intval( $_POST['parent'] ); // phpcs:ignore WordPress.Security.NonceVerification
712 + } elseif ( isset( $_POST[ "new{$taxonomy}_parent" ], $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
713 + $parent = intval( $_POST[ "new{$taxonomy}_parent" ] ); // phpcs:ignore WordPress.Security.NonceVerification
714 + }
715 +
716 + return $parent;
647 717 }
648 718 }