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 -190 2.7.2 → 2.0.12 View file →
@@ -5,9 +5,9 @@
5 5 *
6 6 * @since 1.2
7 7 */
8 8 class PLL_Admin_Filters_Term {
9 - public $links, $model, $options, $pref_lang;
9 + public $links, $model, $options, $curlang, $filter_lang, $pref_lang;
10 10 protected $pre_term_name; // Used to store the term name before creating a slug if needed
11 11 protected $post_id; // Used to store the current post_id when bulk editing posts
12 12
13 13 /**
@@ -18,22 +18,27 @@
18 18 public function __construct( &$polylang ) {
19 19 $this->links = &$polylang->links;
20 20 $this->model = &$polylang->model;
21 21 $this->options = &$polylang->options;
22 + $this->curlang = &$polylang->curlang;
23 + $this->filter_lang = &$polylang->filter_lang;
22 24 $this->pref_lang = &$polylang->pref_lang;
23 25
24 26 foreach ( $this->model->get_translated_taxonomies() as $tax ) {
25 27 // Adds the language field in the 'Categories' and 'Post Tags' panels
26 - add_action( $tax . '_add_form_fields', array( $this, 'add_term_form' ) );
28 + add_action( $tax.'_add_form_fields', array( $this, 'add_term_form' ) );
27 29
28 30 // Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
29 - 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' ) );
30 35 }
31 36
32 37 // Adds actions related to languages when creating or saving categories and post tags
33 38 add_filter( 'wp_dropdown_cats', array( $this, 'wp_dropdown_cats' ) );
34 - add_action( 'create_term', array( $this, 'save_term' ), 900, 3 );
35 - 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
36 41 add_action( 'pre_post_update', array( $this, 'pre_post_update' ) );
37 42 add_filter( 'pre_term_name', array( $this, 'pre_term_name' ) );
38 43 add_filter( 'pre_term_slug', array( $this, 'pre_term_slug' ), 10, 2 );
39 44
@@ -40,8 +45,14 @@
40 45 // Ajax response for edit term form
41 46 add_action( 'wp_ajax_term_lang_choice', array( $this, 'term_lang_choice' ) );
42 47 add_action( 'wp_ajax_pll_terms_not_translated', array( $this, 'ajax_terms_not_translated' ) );
43 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 +
44 55 // Allows to get the default categories in all languages
45 56 add_filter( 'option_default_category', array( $this, 'option_default_category' ) );
46 57 add_action( 'update_option_default_category', array( $this, 'update_option_default_category' ), 10, 2 );
47 58
@@ -54,64 +65,46 @@
54 65 *
55 66 * @since 0.1
56 67 */
57 68 public function add_term_form() {
58 - if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
59 - $taxonomy = sanitize_key( $_GET['taxonomy'] ); // phpcs:ignore WordPress.Security.NonceVerification
60 - }
69 + $taxonomy = $_GET['taxonomy'];
70 + $post_type = isset( $GLOBALS['post_type'] ) ? $GLOBALS['post_type'] : $_REQUEST['post_type'];
61 71
62 - if ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
63 - $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification
64 - }
65 -
66 - if ( isset( $GLOBALS['post_type'] ) ) {
67 - $post_type = $GLOBALS['post_type'];
68 - }
69 -
70 - if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) {
72 + if ( ! taxonomy_exists( $taxonomy ) || ! post_type_exists( $post_type ) ) {
71 73 return;
72 74 }
73 75
74 - $from_term_id = isset( $_GET['from_tag'] ) ? (int) $_GET['from_tag'] : 0; // phpcs:ignore WordPress.Security.NonceVerification
75 -
76 - $lang = isset( $_GET['new_lang'] ) ? $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ) : $this->pref_lang; // phpcs:ignore WordPress.Security.NonceVerification
77 -
76 + $lang = isset( $_GET['new_lang'] ) ? $this->model->get_language( $_GET['new_lang'] ) : $this->pref_lang;
78 77 $dropdown = new PLL_Walker_Dropdown();
79 78
80 - $dropdown_html = $dropdown->walk(
81 - $this->model->get_languages_list(),
82 - -1,
83 - array(
84 - 'name' => 'term_lang_choice',
85 - 'value' => 'term_id',
86 - 'selected' => $lang ? $lang->term_id : '',
87 - 'flag' => true,
88 - )
89 - );
90 -
91 79 wp_nonce_field( 'pll_language', '_pll_nonce' );
92 80
93 - printf(
94 - '<div class="form-field">
81 + printf( '
82 + <div class="form-field">
95 83 <label for="term_lang_choice">%s</label>
96 84 <div id="select-add-term-language">%s</div>
97 85 <p>%s</p>
98 86 </div>',
99 87 esc_html__( 'Language', 'polylang' ),
100 - $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 + ) ),
101 94 esc_html__( 'Sets the language', 'polylang' )
102 95 );
103 96
104 - if ( ! empty( $from_term_id ) ) {
105 - 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'] );
106 99 }
107 100
108 101 // Adds translation fields
109 102 echo '<div id="term-translations" class="form-field">';
110 103 if ( $lang ) {
111 - include PLL_ADMIN_INC . '/view-translations-term.php';
104 + include( PLL_ADMIN_INC.'/view-translations-term.php' );
112 105 }
113 - echo '</div>' . "\n";
106 + echo '</div>'."\n";
114 107 }
115 108
116 109 /**
117 110 * Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
@@ -116,51 +109,28 @@
116 109 /**
117 110 * Adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
118 111 *
119 112 * @since 0.1
120 - *
121 - * @param object $tag
122 113 */
123 114 public function edit_term_form( $tag ) {
124 - if ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
125 - $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification
126 - }
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'];
127 119
128 - if ( isset( $GLOBALS['post_type'] ) ) {
129 - $post_type = $GLOBALS['post_type'];
130 - }
131 -
132 120 if ( ! post_type_exists( $post_type ) ) {
133 121 return;
134 122 }
135 123
136 - $term_id = $tag->term_id;
137 - $taxonomy = $tag->taxonomy; // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable
124 + $dropdown = new PLL_Walker_Dropdown();
138 125
139 - $lang = $this->model->term->get_language( $term_id );
140 - $lang = empty( $lang ) ? $this->pref_lang : $lang;
141 -
142 126 // Disable the language dropdown and the translations input fields for default categories to prevent removal
143 127 $disabled = in_array( get_option( 'default_category' ), $this->model->term->get_translations( $term_id ) );
144 128
145 - $dropdown = new PLL_Walker_Dropdown();
146 -
147 - $dropdown_html = $dropdown->walk(
148 - $this->model->get_languages_list(),
149 - -1,
150 - array(
151 - 'name' => 'term_lang_choice',
152 - 'value' => 'term_id',
153 - 'selected' => $lang ? $lang->term_id : '',
154 - 'disabled' => $disabled,
155 - 'flag' => true,
156 - )
157 - );
158 -
159 129 wp_nonce_field( 'pll_language', '_pll_nonce' );
160 130
161 - printf(
162 - '<tr class="form-field">
131 + printf( '
132 + <tr class="form-field">
163 133 <th scope="row">
164 134 <label for="term_lang_choice">%s</label>
165 135 </th>
166 136 <td id="select-edit-term-language">
@@ -168,17 +138,23 @@
168 138 <p class="description">%s</p>
169 139 </td>
170 140 </tr>',
171 141 esc_html__( 'Language', 'polylang' ),
172 - $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 + ) ),
173 149 esc_html__( 'Sets the language', 'polylang' )
174 150 );
175 151
176 152 echo '<tr id="term-translations" class="form-field">';
177 153 if ( $lang ) {
178 - include PLL_ADMIN_INC . '/view-translations-term.php';
154 + include( PLL_ADMIN_INC.'/view-translations-term.php' );
179 155 }
180 - echo '</tr>' . "\n";
156 + echo '</tr>'."\n";
181 157 }
182 158
183 159 /**
184 160 * Translates term parent if exists when using "Add new" ( translation )
@@ -184,24 +160,16 @@
184 160 * Translates term parent if exists when using "Add new" ( translation )
185 161 *
186 162 * @since 0.7
187 163 *
188 - * @param string $output html markup for dropdown list of categories
164 + * @param string html markup for dropdown list of categories
189 165 * @return string modified html
190 166 */
191 167 public function wp_dropdown_cats( $output ) {
192 - if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
193 - $taxonomy = sanitize_key( $_GET['taxonomy'] ); // phpcs:ignore WordPress.Security.NonceVerification
194 - }
195 -
196 - if ( isset( $taxonomy, $_GET['from_tag'], $_GET['new_lang'] ) && taxonomy_exists( $taxonomy ) ) { // phpcs:ignore WordPress.Security.NonceVerification
197 - $term = get_term( (int) $_GET['from_tag'], $taxonomy ); // phpcs:ignore WordPress.Security.NonceVerification
198 -
199 - if ( $term && $id = $term->parent ) {
200 - $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
201 - if ( $parent = $this->model->term->get_translation( $id, $lang ) ) {
202 - return str_replace( '"' . $parent . '"', '"' . $parent . '" selected="selected"', $output );
203 - }
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 );
204 172 }
205 173 }
206 174 return $output;
207 175 }
@@ -206,9 +174,9 @@
206 174 return $output;
207 175 }
208 176
209 177 /**
210 - * 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
211 179 *
212 180 * @since 1.7
213 181 *
214 182 * @param int $post_id
@@ -213,14 +181,34 @@
213 181 *
214 182 * @param int $post_id
215 183 */
216 184 public function pre_post_update( $post_id ) {
217 - if ( isset( $_GET['bulk_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
185 + if ( isset( $_GET['bulk_edit'] ) ) {
218 186 $this->post_id = $post_id;
219 187 }
220 188 }
221 189
222 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 + /**
223 211 * Saves language
224 212 *
225 213 * @since 1.5
226 214 *
@@ -233,15 +221,16 @@
233 221 // as 'wp_update_term' can be called from outside WP admin
234 222
235 223 // Edit tags
236 224 if ( isset( $_POST['term_lang_choice'] ) ) {
237 - if ( isset( $_POST['action'] ) && sanitize_key( $_POST['action'] ) === 'add-' . $taxonomy ) { // phpcs:ignore WordPress.Security.NonceVerification
238 - check_ajax_referer( 'add-' . $taxonomy, '_ajax_nonce-add-' . $taxonomy ); // Category metabox
239 - } else {
240 - 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
241 227 }
228 + else {
229 + check_admin_referer( 'pll_language', '_pll_nonce' ); // edit tags or tags metabox
230 + }
242 231
243 - $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'] ) );
244 233 }
245 234
246 235 // *Post* bulk edit, in case a new term is created
247 236 elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) {
@@ -258,24 +247,19 @@
258 247 // FIXME backward compatibility WP < 4.2
259 248 // No WP function to get all terms with the exact same name so let's use a custom query
260 249 // $terms = get_terms( $taxonomy, array( 'name' => $term->name, 'hide_empty' => false, 'fields' => 'ids' ) ); should be OK in 4.2
261 250 // I may need to rework the loop below
262 - $terms = $wpdb->get_results(
263 - $wpdb->prepare(
264 - "SELECT t.term_id FROM $wpdb->terms AS t
265 - INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
266 - WHERE tt.taxonomy = %s AND t.name = %s",
267 - $taxonomy,
268 - $term->name
269 - )
270 - );
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 + ) );
271 257
272 258 // If we have several terms with the same name, they are translations of each other
273 259 if ( count( $terms ) > 1 ) {
274 - $translations = array();
275 -
276 260 foreach ( $terms as $term ) {
277 - $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;
278 262 }
279 263
280 264 $this->model->term->save_translations( $term_id, $translations );
281 265 }
@@ -281,9 +265,9 @@
281 265 }
282 266 }
283 267
284 268 else {
285 - $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'] ) );
286 270 }
287 271 }
288 272
289 273 // Quick edit
@@ -293,9 +277,9 @@
293 277 '_inline_edit'
294 278 );
295 279
296 280 $old_lang = $this->model->term->get_language( $term_id ); // Stores the old language
297 - $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
298 282 $translations = $this->model->term->get_translations( $term_id );
299 283
300 284 // Checks if the new language already exists in the translation group
301 285 if ( $old_lang && $old_lang->slug != $lang->slug ) {
@@ -314,10 +298,14 @@
314 298
315 299 // Edit post
316 300 elseif ( isset( $_POST['post_lang_choice'] ) ) { // FIXME should be useless now
317 301 check_admin_referer( 'pll_language', '_pll_nonce' );
318 - $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'] ) );
319 303 }
304 +
305 + else {
306 + $this->set_default_language( $term_id, $taxonomy );
307 + }
320 308 }
321 309
322 310 /**
323 311 * Save translations from our form
@@ -330,16 +318,12 @@
330 318 protected function save_translations( $term_id ) {
331 319 // Security check as 'wp_update_term' can be called from outside WP admin
332 320 check_admin_referer( 'pll_language', '_pll_nonce' );
333 321
334 - $translations = array();
335 -
336 322 // Save translations after checking the translated term is in the right language ( as well as cast id to int )
337 - if ( isset( $_POST['term_tr_lang'] ) ) {
338 - foreach ( array_map( 'absint', $_POST['term_tr_lang'] ) as $lang => $tr_id ) {
339 - $tr_lang = $this->model->term->get_language( $tr_id );
340 - $translations[ $lang ] = $tr_lang && $tr_lang->slug == $lang ? $tr_id : 0;
341 - }
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;
342 326 }
343 327
344 328 $this->model->term->save_translations( $term_id, $translations );
345 329
@@ -352,9 +336,9 @@
352 336 *
353 337 * @since 0.1
354 338 *
355 339 * @param int $term_id
356 - * @param int $tt_id term taxonomy id
340 + * @param int $tt_id term taxononomy id
357 341 * @param string $taxonomy
358 342 */
359 343 public function save_term( $term_id, $tt_id, $taxonomy ) {
360 344 // Does nothing except on taxonomies which are filterable
@@ -365,15 +349,31 @@
365 349 // Capability check
366 350 // As 'wp_update_term' can be called from outside WP admin
367 351 // 2nd test for creating tags when creating / editing a post
368 352 $tax = get_taxonomy( $taxonomy );
369 - 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 ) ) ) {
370 354 $this->save_language( $term_id, $taxonomy );
371 355
372 - if ( isset( $_POST['term_tr_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
373 - $this->save_translations( $term_id );
356 + if ( isset( $_POST['term_tr_lang'] ) ) {
357 + $translations = $this->save_translations( $term_id );
374 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 );
375 370 }
371 +
372 + // Attempts to set a default language even if no capability
373 + else {
374 + $this->set_default_language( $term_id, $taxonomy );
375 + }
376 376 }
377 377
378 378 /**
379 379 * Stores the term name for use in pre_term_slug
@@ -398,26 +398,34 @@
398 398 */
399 399 public function pre_term_slug( $slug, $taxonomy ) {
400 400 $name = sanitize_title( $this->pre_term_name );
401 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 +
402 409 // If the term already exists in another language
403 410 if ( ! $slug && $this->model->is_translated_taxonomy( $taxonomy ) && term_exists( $name, $taxonomy ) ) {
404 - if ( isset( $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
405 - $slug = $name . '-' . $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) )->slug; // phpcs:ignore WordPress.Security.NonceVerification
411 + if ( isset( $_POST['term_lang_choice'] ) ) {
412 + $slug = $name . '-' . $this->model->get_language( $_POST['term_lang_choice'] )->slug;
406 413 }
407 414
408 - elseif ( isset( $_POST['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
409 - $slug = $name . '-' . $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) )->slug; // phpcs:ignore WordPress.Security.NonceVerification
415 + elseif ( isset( $_POST['inline_lang_choice'] ) ) {
416 + $slug = $name . '-' . $this->model->get_language( $_POST['inline_lang_choice'] )->slug;
410 417 }
411 418
412 419 // *Post* bulk edit, in case a new term is created
413 - elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
420 + elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) {
414 421 // Bulk edit does not modify the language
415 - if ( -1 == $_GET['inline_lang_choice'] ) { // phpcs:ignore WordPress.Security.NonceVerification
416 - $slug = $name . '-' . $this->model->post->get_language( $this->post_id )->slug;
417 - } else {
418 - $slug = $name . '-' . $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) )->slug; // phpcs:ignore WordPress.Security.NonceVerification
422 + if ( -1 == $_GET['inline_lang_choice'] ) {
423 + $slug = $name . '-' . $this->model->post->get_language( $this->post_id )->slug;
419 424 }
425 + else {
426 + $slug = $name . '-' . $this->model->get_language( $_GET['inline_lang_choice'] )->slug;
427 + }
420 428 }
421 429 }
422 430
423 431 return $slug;
@@ -423,8 +431,21 @@
423 431 return $slug;
424 432 }
425 433
426 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 + /**
427 448 * Ajax response for edit term form
428 449 *
429 450 * @since 0.2
430 451 */
@@ -430,24 +451,20 @@
430 451 */
431 452 public function term_lang_choice() {
432 453 check_ajax_referer( 'pll_language', '_pll_nonce' );
433 454
434 - if ( ! isset( $_POST['taxonomy'], $_POST['post_type'], $_POST['lang'] ) ) {
435 - wp_die( 0 );
436 - }
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'];
437 459
438 - $lang = $this->model->get_language( sanitize_key( $_POST['lang'] ) );
439 - $term_id = isset( $_POST['term_id'] ) ? (int) $_POST['term_id'] : null; // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable
440 - $taxonomy = sanitize_key( $_POST['taxonomy'] ); // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable
441 - $post_type = sanitize_key( $_POST['post_type'] );
442 -
443 460 if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
444 - wp_die( 0 );
461 + die( 0 );
445 462 }
446 463
447 464 ob_start();
448 465 if ( $lang ) {
449 - include PLL_ADMIN_INC . '/view-translations-term.php';
466 + include( PLL_ADMIN_INC.'/view-translations-term.php' );
450 467 }
451 468 $x = new WP_Ajax_Response( array( 'what' => 'translations', 'data' => ob_get_contents() ) );
452 469 ob_end_clean();
453 470
@@ -454,16 +471,16 @@
454 471 // Parent dropdown list ( only for hierarchical taxonomies )
455 472 // $args copied from edit_tags.php except echo
456 473 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
457 474 $args = array(
458 - 'hide_empty' => 0,
459 - 'hide_if_empty' => false,
460 - 'taxonomy' => $taxonomy,
461 - 'name' => 'parent',
462 - 'orderby' => 'name',
463 - 'hierarchical' => true,
464 - 'show_option_none' => __( 'None', 'polylang' ),
465 - '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,
466 483 );
467 484 $x->Add( array( 'what' => 'parent', 'data' => wp_dropdown_categories( $args ) ) );
468 485 }
469 486
@@ -470,9 +487,9 @@
470 487 // Tag cloud
471 488 // Tests copied from edit_tags.php
472 489 else {
473 490 $tax = get_taxonomy( $taxonomy );
474 - if ( ! is_null( $tax->labels->popular_items ) ) {
491 + if ( ! is_null( $tax->labels->popular_items ) ) {
475 492 $args = array( 'taxonomy' => $taxonomy, 'echo' => false );
476 493 if ( current_user_can( $tax->cap->edit_terms ) ) {
477 494 $args = array_merge( $args, array( 'link' => 'edit' ) );
478 495 }
@@ -497,66 +514,119 @@
497 514 */
498 515 public function ajax_terms_not_translated() {
499 516 check_ajax_referer( 'pll_language', '_pll_nonce' );
500 517
501 - if ( ! isset( $_GET['term'], $_GET['post_type'], $_GET['taxonomy'], $_GET['term_language'], $_GET['translation_language'] ) ) {
502 - wp_die( 0 );
503 - }
518 + $s = wp_unslash( $_GET['term'] );
519 + $post_type = $_GET['post_type'];
520 + $taxonomy = $_GET['taxonomy'];
504 521
505 - $s = wp_unslash( $_GET['term'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
506 - $post_type = sanitize_key( $_GET['post_type'] );
507 - $taxonomy = sanitize_key( $_GET['taxonomy'] );
508 -
509 522 if ( ! post_type_exists( $post_type ) || ! taxonomy_exists( $taxonomy ) ) {
510 - wp_die( 0 );
523 + die( 0 );
511 524 }
512 525
513 - $term_language = $this->model->get_language( sanitize_key( $_GET['term_language'] ) );
514 - $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'] );
515 528
516 - $terms = array();
517 529 $return = array();
518 530
519 - // Add current translation in list.
520 - // Not in add term as term_id is not set.
521 - if ( isset( $_GET['term_id'] ) && 'undefined' !== $_GET['term_id'] && $term_id = $this->model->term->get_translation( (int) $_GET['term_id'], $translation_language ) ) {
522 - $terms = array( get_term( $term_id, $taxonomy ) );
523 - }
524 -
525 - // 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
526 532 foreach ( get_terms( $taxonomy, 'hide_empty=0&lang=0&name__like=' . $s ) as $term ) {
527 533 $lang = $this->model->term->get_language( $term->term_id );
528 534
529 535 if ( $lang && $lang->slug == $translation_language->slug && ! $this->model->term->get_translation( $term->term_id, $term_language ) ) {
530 - $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 + );
531 541 }
532 542 }
533 543
534 - // Format the ajax response.
535 - foreach ( $terms as $term ) {
536 - $return[] = array(
537 - 'id' => $term->term_id,
538 - 'value' => rtrim( // Trim the seperator added at the end by WP.
539 - get_term_parents_list(
540 - $term->term_id,
541 - $term->taxonomy,
542 - array(
543 - 'separator' => ' > ',
544 - 'link' => false,
545 - )
546 - ),
547 - ' >'
548 - ),
549 - 'link' => $this->links->edit_term_translation_link( $term->term_id, $term->taxonomy, $post_type ),
550 - );
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 + ) );
551 553 }
552 554
553 - wp_die( wp_json_encode( $return ) );
555 + wp_die( json_encode( $return ) );
554 556 }
555 557
556 558 /**
557 - * 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
558 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 + *
559 629 * @since 1.2
560 630 *
561 631 * @param int $value
562 632 * @return int
@@ -561,11 +631,30 @@
561 631 * @param int $value
562 632 * @return int
563 633 */
564 634 public function option_default_category( $value ) {
565 - 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 ) ) {
566 637 $value = $tr;
567 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 +
568 657 return $value;
569 658 }
570 659
571 660 /**
@@ -598,11 +687,11 @@
598 687 * Splits translations if these are shared terms too
599 688 *
600 689 * @since 1.7
601 690 *
602 - * @param int $term_id Shared term_id
603 - * @param int $new_term_id
604 - * @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
605 694 * @param string $taxonomy
606 695 */
607 696 public function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
608 697 if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
@@ -616,9 +705,8 @@
616 705 }
617 706
618 707 $avoid_recursion = true;
619 708 $lang = $this->model->term->get_language( $term_id );
620 - $translations = array();
621 709
622 710 foreach ( $this->model->term->get_translations( $term_id ) as $key => $tr_id ) {
623 711 if ( $lang->slug == $key ) {
624 712 $translations[ $key ] = $new_term_id;
@@ -627,9 +715,9 @@
627 715 $tr_term = get_term( $tr_id, $taxonomy );
628 716 $translations[ $key ] = _split_shared_term( $tr_id, $tr_term->term_taxonomy_id );
629 717
630 718 // Hack translation ids sent by the form to avoid overwrite in PLL_Admin_Filters_Term::save_translations
631 - 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 ) {
632 720 $_POST['term_tr_lang'][ $key ] = $translations[ $key ];
633 721 }
634 722 }
635 723 $this->model->term->set_language( $translations[ $key ], $key );