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