PluginProbe
Polylang / 2.1.6
Polylang v2.1.6
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
polylang / admin / admin-filters-term.php

admin-filters-term.php in Polylang 2.1.6, at admin/admin-filters-term.php

728 lines 23.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages filters and actions related to terms on admin side
5 *
6 * @since 1.2
7 */
8 class PLL_Admin_Filters_Term {
9 public $links, $model, $options, $curlang, $filter_lang, $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
12
13 /**
14 * Constructor: setups filters and actions
15 *
16 * @param object $polylang
17 */
18 public function __construct( &$polylang ) {
19 $this->links = &$polylang->links;
20 $this->model = &$polylang->model;
21 $this->options = &$polylang->options;
22 $this->curlang = &$polylang->curlang;
23 $this->filter_lang = &$polylang->filter_lang;
24 $this->pref_lang = &$polylang->pref_lang;
25
26 foreach ( $this->model->get_translated_taxonomies() as $tax ) {
27 // Adds the language field in the 'Categories' and 'Post Tags' panels
28 add_action( $tax.'_add_form_fields', array( $this, 'add_term_form' ) );
29
30 // Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
31 add_action( $tax.'_edit_form_fields', array( $this, 'edit_term_form' ) );
32
33 // Adds action related to languages when deleting categories and post tags
34 add_action( 'delete_'.$tax, array( $this, 'delete_term' ) );
35 }
36
37 // Adds actions related to languages when creating or saving categories and post tags
38 add_filter( 'wp_dropdown_cats', array( $this, 'wp_dropdown_cats' ) );
39 add_action( 'create_term', array( $this, 'save_term' ), 999, 3 );
40 add_action( 'edit_term', array( $this, 'save_term' ), 999, 3 ); // late as it may conflict with other plugins, see http://wordpress.org/support/topic/polylang-and-wordpress-seo-by-yoast
41 add_action( 'pre_post_update', array( $this, 'pre_post_update' ) );
42 add_filter( 'pre_term_name', array( $this, 'pre_term_name' ) );
43 add_filter( 'pre_term_slug', array( $this, 'pre_term_slug' ), 10, 2 );
44
45 // Ajax response for edit term form
46 add_action( 'wp_ajax_term_lang_choice', array( $this, 'term_lang_choice' ) );
47 add_action( 'wp_ajax_pll_terms_not_translated', array( $this, 'ajax_terms_not_translated' ) );
48
49 // Adds cache domain when querying terms
50 add_filter( 'get_terms_args', array( $this, 'get_terms_args' ), 10, 2 );
51
52 // Filters categories and post tags by language
53 add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 3 );
54
55 // Allows to get the default categories in all languages
56 add_filter( 'option_default_category', array( $this, 'option_default_category' ) );
57 add_action( 'update_option_default_category', array( $this, 'update_option_default_category' ), 10, 2 );
58
59 // Updates the translations term ids when splitting a shared term
60 add_action( 'split_shared_term', array( $this, 'split_shared_term' ), 10, 4 ); // WP 4.2
61 }
62
63 /**
64 * Adds the language field in the 'Categories' and 'Post Tags' panels
65 *
66 * @since 0.1
67 */
68 public function add_term_form() {
69 $taxonomy = $_GET['taxonomy'];
70 $post_type = isset( $GLOBALS['post_type'] ) ? $GLOBALS['post_type'] : $_REQUEST['post_type'];
71
72 if ( ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) {
73 return;
74 }
75
76 $lang = isset( $_GET['new_lang'] ) ? $this->model->get_language( $_GET['new_lang'] ) : $this->pref_lang;
77 $dropdown = new PLL_Walker_Dropdown();
78
79 wp_nonce_field( 'pll_language', '_pll_nonce' );
80
81 printf( '
82 <div class="form-field">
83 <label for="term_lang_choice">%s</label>
84 <div id="select-add-term-language">%s</div>
85 <p>%s</p>
86 </div>',
87 esc_html__( 'Language', 'polylang' ),
88 $dropdown->walk( $this->model->get_languages_list(), array(
89 'name' => 'term_lang_choice',
90 'value' => 'term_id',
91 'selected' => $lang ? $lang->term_id : '',
92 'flag' => true,
93 ) ),
94 esc_html__( 'Sets the language', 'polylang' )
95 );
96
97 if ( ! empty( $_GET['from_tag'] ) ) {
98 printf( '<input type="hidden" name="from_tag" value="%d" />', (int) $_GET['from_tag'] );
99 }
100
101 // Adds translation fields
102 echo '<div id="term-translations" class="form-field">';
103 if ( $lang ) {
104 include PLL_ADMIN_INC . '/view-translations-term.php';
105 }
106 echo '</div>'."\n";
107 }
108
109 /**
110 * Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
111 *
112 * @since 0.1
113 */
114 public function edit_term_form( $tag ) {
115 $post_type = isset( $GLOBALS['post_type'] ) ? $GLOBALS['post_type'] : $_REQUEST['post_type'];
116
117 if ( ! post_type_exists( $post_type ) ) {
118 return;
119 }
120
121 $term_id = $tag->term_id;
122 $taxonomy = $tag->taxonomy;
123
124 $lang = $this->model->term->get_language( $term_id );
125 $lang = empty( $lang ) ? $this->pref_lang : $lang;
126
127 $dropdown = new PLL_Walker_Dropdown();
128
129 // Disable the language dropdown and the translations input fields for default categories to prevent removal
130 $disabled = in_array( get_option( 'default_category' ), $this->model->term->get_translations( $term_id ) );
131
132 wp_nonce_field( 'pll_language', '_pll_nonce' );
133
134 printf( '
135 <tr class="form-field">
136 <th scope="row">
137 <label for="term_lang_choice">%s</label>
138 </th>
139 <td id="select-edit-term-language">
140 %s<br />
141 <p class="description">%s</p>
142 </td>
143 </tr>',
144 esc_html__( 'Language', 'polylang' ),
145 $dropdown->walk( $this->model->get_languages_list(), array(
146 'name' => 'term_lang_choice',
147 'value' => 'term_id',
148 'selected' => $lang ? $lang->term_id : '',
149 'disabled' => $disabled,
150 'flag' => true,
151 ) ),
152 esc_html__( 'Sets the language', 'polylang' )
153 );
154
155 echo '<tr id="term-translations" class="form-field">';
156 if ( $lang ) {
157 include PLL_ADMIN_INC . '/view-translations-term.php';
158 }
159 echo '</tr>'."\n";
160 }
161
162 /**
163 * Translates term parent if exists when using "Add new" ( translation )
164 *
165 * @since 0.7
166 *
167 * @param string html markup for dropdown list of categories
168 * @return string modified html
169 */
170 public function wp_dropdown_cats( $output ) {
171 if ( isset( $_GET['taxonomy'], $_GET['from_tag'], $_GET['new_lang'] ) && taxonomy_exists( $_GET['taxonomy'] ) ) {
172 $term = get_term( (int) $_GET['from_tag'], $_GET['taxonomy'] );
173 if ( $term && $id = $term->parent ) {
174 $lang = $this->model->get_language( $_GET['new_lang'] );
175 if ( $parent = $this->model->term->get_translation( $id, $lang ) ) {
176 return str_replace( '"' . $parent . '"', '"' . $parent . '" selected="selected"', $output );
177 }
178 }
179 }
180 return $output;
181 }
182
183 /**
184 * stores the current post_id when bulk editing posts for use in save_language and pre_term_slug
185 *
186 * @since 1.7
187 *
188 * @param int $post_id
189 */
190 public function pre_post_update( $post_id ) {
191 if ( isset( $_GET['bulk_edit'] ) ) {
192 $this->post_id = $post_id;
193 }
194 }
195
196 /**
197 * Allows to set a language by default for terms if it has no language yet
198 *
199 * @since 1.5.4
200 *
201 * @param int $term_id
202 * @param string $taxonomy
203 */
204 protected function set_default_language( $term_id, $taxonomy ) {
205 if ( ! $this->model->term->get_language( $term_id ) ) {
206 // Sets language from term parent if exists thanks to Scott Kingsley Clark
207 if ( ( $term = get_term( $term_id, $taxonomy ) ) && ! empty( $term->parent ) && $parent_lang = $this->model->term->get_language( $term->parent ) ) {
208 $this->model->term->set_language( $term_id, $parent_lang );
209 }
210 else {
211 $this->model->term->set_language( $term_id, $this->pref_lang );
212 }
213 }
214 }
215
216 /**
217 * Saves language
218 *
219 * @since 1.5
220 *
221 * @param int $term_id
222 * @param string $taxonomy
223 */
224 protected function save_language( $term_id, $taxonomy ) {
225 global $wpdb;
226 // Security checks are necessary to accept language modifications
227 // as 'wp_update_term' can be called from outside WP admin
228
229 // Edit tags
230 if ( isset( $_POST['term_lang_choice'] ) ) {
231 if ( 'add-' . $taxonomy == $_POST['action'] ) {
232 check_ajax_referer( $_POST['action'], '_ajax_nonce-add-' . $taxonomy ); // category metabox
233 }
234 else {
235 check_admin_referer( 'pll_language', '_pll_nonce' ); // edit tags or tags metabox
236 }
237
238 $this->model->term->set_language( $term_id, $this->model->get_language( $_POST['term_lang_choice'] ) );
239 }
240
241 // *Post* bulk edit, in case a new term is created
242 elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) {
243 check_admin_referer( 'bulk-posts' );
244
245 // Bulk edit does not modify the language
246 // So we possibly create a tag in several languages
247 if ( -1 == $_GET['inline_lang_choice'] ) {
248 // The language of the current term is set a according to the language of the current post
249 $this->model->term->set_language( $term_id, $this->model->post->get_language( $this->post_id ) );
250 $term = get_term( $term_id, $taxonomy );
251
252 // Get all terms with the same name
253 // FIXME backward compatibility WP < 4.2
254 // No WP function to get all terms with the exact same name so let's use a custom query
255 // $terms = get_terms( $taxonomy, array( 'name' => $term->name, 'hide_empty' => false, 'fields' => 'ids' ) ); should be OK in 4.2
256 // I may need to rework the loop below
257 $terms = $wpdb->get_results( $wpdb->prepare( "
258 SELECT t.term_id FROM $wpdb->terms AS t
259 INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
260 WHERE tt.taxonomy = %s AND t.name = %s",
261 $taxonomy, $term->name
262 ) );
263
264 // If we have several terms with the same name, they are translations of each other
265 if ( count( $terms ) > 1 ) {
266 foreach ( $terms as $term ) {
267 $translations[ $this->model->term->get_language( $term->term_id )->slug ] = $term->term_id;
268 }
269
270 $this->model->term->save_translations( $term_id, $translations );
271 }
272 }
273
274 else {
275 $this->model->term->set_language( $term_id, $this->model->get_language( $_GET['inline_lang_choice'] ) );
276 }
277 }
278
279 // Quick edit
280 elseif ( isset( $_POST['inline_lang_choice'] ) ) {
281 check_ajax_referer(
282 isset( $_POST['action'] ) && 'inline-save' == $_POST['action'] ? 'inlineeditnonce' : 'taxinlineeditnonce', // Post quick edit or tag quick edit ?
283 '_inline_edit'
284 );
285
286 $old_lang = $this->model->term->get_language( $term_id ); // Stores the old language
287 $lang = $this->model->get_language( $_POST['inline_lang_choice'] ); // New language
288 $translations = $this->model->term->get_translations( $term_id );
289
290 // Checks if the new language already exists in the translation group
291 if ( $old_lang && $old_lang->slug != $lang->slug ) {
292 if ( array_key_exists( $lang->slug, $translations ) ) {
293 $this->model->term->delete_translation( $term_id );
294 }
295
296 elseif ( array_key_exists( $old_lang->slug, $translations ) ) {
297 unset( $translations[ $old_lang->slug ] );
298 $this->model->term->save_translations( $term_id, $translations );
299 }
300 }
301
302 $this->model->term->set_language( $term_id, $lang ); // Set new language
303 }
304
305 // Edit post
306 elseif ( isset( $_POST['post_lang_choice'] ) ) { // FIXME should be useless now
307 check_admin_referer( 'pll_language', '_pll_nonce' );
308 $this->model->term->set_language( $term_id, $this->model->get_language( $_POST['post_lang_choice'] ) );
309 }
310
311 else {
312 $this->set_default_language( $term_id, $taxonomy );
313 }
314 }
315
316 /**
317 * Save translations from our form
318 *
319 * @since 1.5
320 *
321 * @param int $term_id
322 * @return array
323 */
324 protected function save_translations( $term_id ) {
325 // Security check as 'wp_update_term' can be called from outside WP admin
326 check_admin_referer( 'pll_language', '_pll_nonce' );
327
328 // Save translations after checking the translated term is in the right language ( as well as cast id to int )
329 foreach ( $_POST['term_tr_lang'] as $lang => $tr_id ) {
330 $tr_lang = $this->model->term->get_language( (int) $tr_id );
331 $translations[ $lang ] = $tr_lang && $tr_lang->slug == $lang ? (int) $tr_id : 0;
332 }
333
334 $this->model->term->save_translations( $term_id, $translations );
335
336 return $translations;
337 }
338
339 /**
340 * Called when a category or post tag is created or edited
341 * Saves language and translations
342 *
343 * @since 0.1
344 *
345 * @param int $term_id
346 * @param int $tt_id term taxononomy id
347 * @param string $taxonomy
348 */
349 public function save_term( $term_id, $tt_id, $taxonomy ) {
350 // Does nothing except on taxonomies which are filterable
351 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
352 return;
353 }
354
355 // Capability check
356 // As 'wp_update_term' can be called from outside WP admin
357 // 2nd test for creating tags when creating / editing a post
358 $tax = get_taxonomy( $taxonomy );
359 if ( current_user_can( $tax->cap->edit_terms ) || ( isset( $_POST['tax_input'][ $taxonomy ] ) && current_user_can( $tax->cap->assign_terms ) ) ) {
360 $this->save_language( $term_id, $taxonomy );
361
362 if ( isset( $_POST['term_tr_lang'] ) ) {
363 $translations = $this->save_translations( $term_id );
364 }
365
366 /**
367 * Fires after the term language and translations are saved
368 *
369 * @since 1.2
370 *
371 * @param int $term_id term id
372 * @param string $taxonomy taxonomy name
373 * @param array $translations the list of translations term ids
374 */
375 do_action( 'pll_save_term', $term_id, $taxonomy, empty( $translations ) ? $this->model->term->get_translations( $term_id ) : $translations );
376 }
377
378 // Attempts to set a default language even if no capability
379 else {
380 $this->set_default_language( $term_id, $taxonomy );
381 }
382 }
383
384 /**
385 * Stores the term name for use in pre_term_slug
386 *
387 * @since 0.9.5
388 *
389 * @param string $name term name
390 * @return string unmodified term name
391 */
392 public function pre_term_name( $name ) {
393 return $this->pre_term_name = $name;
394 }
395
396 /**
397 * Creates the term slug in case the term already exists in another language
398 *
399 * @since 0.9.5
400 *
401 * @param string $slug
402 * @param string $taxonomy
403 * @return string
404 */
405 public function pre_term_slug( $slug, $taxonomy ) {
406 $name = sanitize_title( $this->pre_term_name );
407
408 // If the term already exists in another language
409 if ( ! $slug && $this->model->is_translated_taxonomy( $taxonomy ) && term_exists( $name, $taxonomy ) ) {
410 if ( isset( $_POST['term_lang_choice'] ) ) {
411 $slug = $name . '-' . $this->model->get_language( $_POST['term_lang_choice'] )->slug;
412 }
413
414 elseif ( isset( $_POST['inline_lang_choice'] ) ) {
415 $slug = $name . '-' . $this->model->get_language( $_POST['inline_lang_choice'] )->slug;
416 }
417
418 // *Post* bulk edit, in case a new term is created
419 elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) {
420 // Bulk edit does not modify the language
421 if ( -1 == $_GET['inline_lang_choice'] ) {
422 $slug = $name . '-' . $this->model->post->get_language( $this->post_id )->slug;
423 } else {
424 $slug = $name . '-' . $this->model->get_language( $_GET['inline_lang_choice'] )->slug;
425 }
426 }
427 }
428
429 return $slug;
430 }
431
432 /**
433 * Called when a category or post tag is deleted
434 * Deletes language and translations
435 *
436 * @since 0.1
437 *
438 * @param int $term_id
439 */
440 public function delete_term( $term_id ) {
441 $this->model->term->delete_translation( $term_id );
442 $this->model->term->delete_language( $term_id );
443 }
444
445 /**
446 * Ajax response for edit term form
447 *
448 * @since 0.2
449 */
450 public function term_lang_choice() {
451 check_ajax_referer( 'pll_language', '_pll_nonce' );
452
453 $lang = $this->model->get_language( $_POST['lang'] );
454 $term_id = isset( $_POST['term_id'] ) ? (int) $_POST['term_id'] : null;
455 $taxonomy = $_POST['taxonomy'];
456 $post_type = $_POST['post_type'];
457
458 if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
459 die( 0 );
460 }
461
462 ob_start();
463 if ( $lang ) {
464 include PLL_ADMIN_INC . '/view-translations-term.php';
465 }
466 $x = new WP_Ajax_Response( array( 'what' => 'translations', 'data' => ob_get_contents() ) );
467 ob_end_clean();
468
469 // Parent dropdown list ( only for hierarchical taxonomies )
470 // $args copied from edit_tags.php except echo
471 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
472 $args = array(
473 'hide_empty' => 0,
474 'hide_if_empty' => false,
475 'taxonomy' => $taxonomy,
476 'name' => 'parent',
477 'orderby' => 'name',
478 'hierarchical' => true,
479 'show_option_none' => __( 'None' ),
480 'echo' => 0,
481 );
482 $x->Add( array( 'what' => 'parent', 'data' => wp_dropdown_categories( $args ) ) );
483 }
484
485 // Tag cloud
486 // Tests copied from edit_tags.php
487 else {
488 $tax = get_taxonomy( $taxonomy );
489 if ( ! is_null( $tax->labels->popular_items ) ) {
490 $args = array( 'taxonomy' => $taxonomy, 'echo' => false );
491 if ( current_user_can( $tax->cap->edit_terms ) ) {
492 $args = array_merge( $args, array( 'link' => 'edit' ) );
493 }
494
495 if ( $tag_cloud = wp_tag_cloud( $args ) ) {
496 $html = sprintf( '<div class="tagcloud"><h2>%1$s</h2>%2$s</div>', esc_html( $tax->labels->popular_items ), $tag_cloud );
497 $x->Add( array( 'what' => 'tag_cloud', 'data' => $html ) );
498 }
499 }
500 }
501
502 // Flag
503 $x->Add( array( 'what' => 'flag', 'data' => empty( $lang->flag ) ? esc_html( $lang->slug ) : $lang->flag ) );
504
505 $x->send();
506 }
507
508 /**
509 * Ajax response for input in translation autocomplete input box
510 *
511 * @since 1.5
512 */
513 public function ajax_terms_not_translated() {
514 check_ajax_referer( 'pll_language', '_pll_nonce' );
515
516 $s = wp_unslash( $_GET['term'] );
517 $post_type = $_GET['post_type'];
518 $taxonomy = $_GET['taxonomy'];
519
520 if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
521 die( 0 );
522 }
523
524 $term_language = $this->model->get_language( $_GET['term_language'] );
525 $translation_language = $this->model->get_language( $_GET['translation_language'] );
526
527 $return = array();
528
529 // It is more efficient to use one common query for all languages as soon as there are more than 2
530 foreach ( get_terms( $taxonomy, 'hide_empty=0&lang=0&name__like=' . $s ) as $term ) {
531 $lang = $this->model->term->get_language( $term->term_id );
532
533 if ( $lang && $lang->slug == $translation_language->slug && ! $this->model->term->get_translation( $term->term_id, $term_language ) ) {
534 $return[] = array(
535 'id' => $term->term_id,
536 'value' => $term->name,
537 'link' => $this->links->edit_term_translation_link( $term->term_id, $taxonomy, $post_type ),
538 );
539 }
540 }
541
542 // Add current translation in list
543 // Not in add term for as term_id is not set
544 if ( 'undefined' !== $_GET['term_id'] && $term_id = $this->model->term->get_translation( (int) $_GET['term_id'], $translation_language ) ) {
545 $term = get_term( $term_id, $taxonomy );
546 array_unshift( $return, array(
547 'id' => $term_id,
548 'value' => $term->name,
549 'link' => $this->links->edit_term_translation_link( $term->term_id, $taxonomy, $post_type ),
550 ) );
551 }
552
553 wp_die( json_encode( $return ) );
554 }
555
556 /**
557 * Get the language(s) to filter get_terms
558 *
559 * @since 1.7.6
560 *
561 * @param array $taxonomies queried taxonomies
562 * @param array $args get_terms arguments
563 * @return object|string|bool the language(s) to use in the filter, false otherwise
564 */
565 protected function get_queried_language( $taxonomies, $args ) {
566 // Does nothing except on taxonomies which are filterable
567 // Since WP 4.7, make sure not to filter wp_get_object_terms()
568 if ( ! $this->model->is_translated_taxonomy( $taxonomies ) || ! empty( $args['object_ids'] ) ) {
569 return false;
570 }
571
572 // If get_terms is queried with a 'lang' parameter
573 if ( isset( $args['lang'] ) ) {
574 return $args['lang'];
575 }
576
577 // On tags page, everything should be filtered according to the admin language filter except the parent dropdown
578 if ( 'edit-tags.php' === $GLOBALS['pagenow'] && empty( $args['class'] ) ) {
579 return $this->filter_lang;
580 }
581
582 return $this->curlang;
583 }
584
585 /**
586 * Adds language dependent cache domain when querying terms
587 * Useful as the 'lang' parameter is not included in cache key by WordPress
588 *
589 * @since 1.3
590 *
591 * @param array $args
592 * @param array $taxonomies
593 * @return array modified arguments
594 */
595 public function get_terms_args( $args, $taxonomies ) {
596 // don't break _get_term_hierarchy()
597 if ( 'all' === $args['get'] && 'id' === $args['orderby'] && 'id=>parent' === $args['fields'] ) {
598 $args['lang'] = '';
599 }
600
601 if ( $lang = $this->get_queried_language( $taxonomies, $args ) ) {
602 $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug );
603 $args['cache_domain'] = empty( $args['cache_domain'] ) ? 'pll' . $key : $args['cache_domain'] . $key;
604 }
605 return $args;
606 }
607
608 /**
609 * Filters categories and post tags by language(s) when needed on admin side
610 *
611 * @since 0.5
612 *
613 * @param array $clauses list of sql clauses
614 * @param array $taxonomies list of taxonomies
615 * @param array $args get_terms arguments
616 * @return array modified sql clauses
617 */
618 public function terms_clauses( $clauses, $taxonomies, $args ) {
619 $lang = $this->get_queried_language( $taxonomies, $args );
620 return ! empty( $lang ) ? $this->model->terms_clauses( $clauses, $lang ) : $clauses; // adds our clauses to filter by current language
621 }
622
623 /**
624 * Hack to avoid displaying delete link for the default category in all languages
625 * Also returns the default category in the right language when called from wp_delete_term
626 *
627 * @since 1.2
628 *
629 * @param int $value
630 * @return int
631 */
632 public function option_default_category( $value ) {
633 // Filters the default category in note below the category list table and in settings->writing dropdown
634 if ( isset( $this->pref_lang ) && $tr = $this->model->term->get( $value, $this->pref_lang ) ) {
635 $value = $tr;
636 }
637
638 // FIXME backward compatibility with WP < 4.7
639 if ( version_compare( $GLOBALS['wp_version'], '4.7alpha', '<' ) ) {
640 $traces = debug_backtrace();
641 $n = version_compare( PHP_VERSION, '7', '>=' ) ? 3 : 4; // PHP 7 does not include call_user_func_array
642
643 if ( isset( $traces[ $n ] ) ) {
644 // FIXME 'column_name' for backward compatibility with WP < 4.3
645 if ( in_array( $traces[ $n ]['function'], array( 'column_cb', 'column_name', 'handle_row_actions' ) ) && in_array( $traces[ $n ]['args'][0]->term_id, $this->model->term->get_translations( $value ) ) ) {
646 return $traces[ $n ]['args'][0]->term_id;
647 }
648
649 if ( 'wp_delete_term' == $traces[ $n ]['function'] ) {
650 return $this->model->term->get( $value, $this->model->term->get_language( $traces[ $n ]['args'][0] ) );
651 }
652 }
653 }
654
655 return $value;
656 }
657
658 /**
659 * Checks if the new default category is translated in all languages
660 * If not, create the translations
661 *
662 * @since 1.7
663 *
664 * @param int $old_value
665 * @param int $value
666 */
667 public function update_option_default_category( $old_value, $value ) {
668 $default_cat_lang = $this->model->term->get_language( $value );
669
670 // Assign a default language to default category
671 if ( ! $default_cat_lang ) {
672 $default_cat_lang = $this->model->get_language( $this->options['default_lang'] );
673 $this->model->term->set_language( (int) $value, $default_cat_lang );
674 }
675
676 foreach ( $this->model->get_languages_list() as $language ) {
677 if ( $language->slug != $default_cat_lang->slug && ! $this->model->term->get_translation( $value, $language ) ) {
678 $this->model->create_default_category( $language );
679 }
680 }
681 }
682
683 /**
684 * Updates the translations term ids when splitting a shared term
685 * Splits translations if these are shared terms too
686 *
687 * @since 1.7
688 *
689 * @param int $term_id shared term_id
690 * @param int $new_term_id
691 * @param int $term_taxonomy_id
692 * @param string $taxonomy
693 */
694 public function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
695 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
696 return;
697 }
698
699 // Avoid recursion
700 static $avoid_recursion = false;
701 if ( $avoid_recursion ) {
702 return;
703 }
704
705 $avoid_recursion = true;
706 $lang = $this->model->term->get_language( $term_id );
707
708 foreach ( $this->model->term->get_translations( $term_id ) as $key => $tr_id ) {
709 if ( $lang->slug == $key ) {
710 $translations[ $key ] = $new_term_id;
711 }
712 else {
713 $tr_term = get_term( $tr_id, $taxonomy );
714 $translations[ $key ] = _split_shared_term( $tr_id, $tr_term->term_taxonomy_id );
715
716 // Hack translation ids sent by the form to avoid overwrite in PLL_Admin_Filters_Term::save_translations
717 if ( isset( $_POST['term_tr_lang'][ $key ] ) && $_POST['term_tr_lang'][ $key ] == $tr_id ) {
718 $_POST['term_tr_lang'][ $key ] = $translations[ $key ];
719 }
720 }
721 $this->model->term->set_language( $translations[ $key ], $key );
722 }
723
724 $this->model->term->save_translations( $new_term_id, $translations );
725 $avoid_recursion = false;
726 }
727 }
728