PluginProbe
Polylang / 2.5.2
Polylang v2.5.2
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-model.php +32 -59 2.82.5.2 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Extends the PLL_Model class with methods needed only in Polylang settings pages
8 5 *
@@ -30,19 +27,19 @@
30 27 * @param array $args
31 28 * @return bool true if success / false if failed
32 29 */
33 30 public function add_language( $args ) {
34 - $errors = $this->validate_lang( $args );
35 - if ( $errors->get_error_code() ) { // Using has_errors() would be more meaningful but is available only since WP 5.0
36 - return $errors;
31 + if ( ! $this->validate_lang( $args ) ) {
32 + return false;
37 33 }
38 34
39 35 // First the language taxonomy
40 - $description = maybe_serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) );
36 + $description = serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) );
41 37 $r = wp_insert_term( $args['name'], 'language', array( 'slug' => $args['slug'], 'description' => $description ) );
42 38 if ( is_wp_error( $r ) ) {
43 39 // Avoid an ugly fatal error if something went wrong ( reported once in the forum )
44 - return new WP_Error( 'pll_add_language', __( 'Impossible to add the language.', 'polylang' ) );
40 + add_settings_error( 'general', 'pll_add_language', __( 'Impossible to add the language.', 'polylang' ) );
41 + return false;
45 42 }
46 43 wp_update_term( (int) $r['term_id'], 'language', array( 'term_group' => (int) $args['term_group'] ) ); // can't set the term group directly in wp_insert_term
47 44
48 45 // The term_language taxonomy
@@ -76,8 +73,10 @@
76 73 do_action( 'pll_add_language', $args );
77 74
78 75 $this->clean_languages_cache(); // Again to set add mo_id in the cached languages list
79 76 flush_rewrite_rules(); // Refresh rewrite rules
77 +
78 + add_settings_error( 'general', 'pll_languages_created', __( 'Language added.', 'polylang' ), 'updated' );
80 79 return true;
81 80 }
82 81
83 82 /**
@@ -90,9 +89,9 @@
90 89 public function delete_language( $lang_id ) {
91 90 $lang = $this->get_language( (int) $lang_id );
92 91
93 92 if ( empty( $lang ) ) {
94 - return false;
93 + return;
95 94 }
96 95
97 96 // Oops ! we are deleting the default language...
98 97 // Need to do this before loosing the information for default category translations
@@ -127,9 +126,9 @@
127 126
128 127 // Delete menus locations
129 128 if ( ! empty( $this->options['nav_menus'] ) ) {
130 129 foreach ( $this->options['nav_menus'] as $theme => $locations ) {
131 - foreach ( array_keys( $locations ) as $location ) {
130 + foreach ( $locations as $location => $languages ) {
132 131 unset( $this->options['nav_menus'][ $theme ][ $location ][ $lang->slug ] );
133 132 }
134 133 }
135 134 }
@@ -157,9 +156,9 @@
157 156 $this->clean_languages_cache();
158 157
159 158 update_option( 'polylang', $this->options );
160 159 flush_rewrite_rules(); // refresh rewrite rules
161 - return true;
160 + add_settings_error( 'general', 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'updated' );
162 161 }
163 162
164 163 /**
165 164 * Update language properties
@@ -181,12 +180,10 @@
181 180 * @return bool true if success / false if failed
182 181 */
183 182 public function update_language( $args ) {
184 183 $lang = $this->get_language( (int) $args['lang_id'] );
185 -
186 - $errors = $this->validate_lang( $args, $lang );
187 - if ( $errors->get_error_code() ) { // Using has_errors() would be more meaningful but is available only since WP 5.0
188 - return $errors;
184 + if ( ! $this->validate_lang( $args, $lang ) ) {
185 + return false;
189 186 }
190 187
191 188 // Update links to this language in posts and terms in case the slug has been modified
192 189 $slug = $args['slug'];
@@ -213,9 +210,9 @@
213 210
214 211 // Update menus locations
215 212 if ( ! empty( $this->options['nav_menus'] ) ) {
216 213 foreach ( $this->options['nav_menus'] as $theme => $locations ) {
217 - foreach ( array_keys( $locations ) as $location ) {
214 + foreach ( $locations as $location => $languages ) {
218 215 if ( ! empty( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] ) ) {
219 216 $this->options['nav_menus'][ $theme ][ $location ][ $slug ] = $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ];
220 217 unset( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] );
221 218 }
@@ -237,9 +234,9 @@
237 234
238 235 update_option( 'polylang', $this->options );
239 236
240 237 // And finally update the language itself
241 - $description = maybe_serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) );
238 + $description = serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) );
242 239 wp_update_term( (int) $lang->term_id, 'language', array( 'slug' => $slug, 'name' => $args['name'], 'description' => $description, 'term_group' => (int) $args['term_group'] ) );
243 240 wp_update_term( (int) $lang->tl_term_id, 'term_language', array( 'slug' => 'pll_' . $slug, 'name' => $args['name'] ) );
244 241
245 242 /**
@@ -252,8 +249,9 @@
252 249 do_action( 'pll_update_language', $args );
253 250
254 251 $this->clean_languages_cache();
255 252 flush_rewrite_rules(); // Refresh rewrite rules
253 + add_settings_error( 'general', 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'updated' );
256 254 return true;
257 255 }
258 256
259 257 /**
@@ -267,24 +265,22 @@
267 265 * @param object $lang optional the language currently updated, the language is created if not set
268 266 * @return bool true if success / false if failed
269 267 */
270 268 protected function validate_lang( $args, $lang = null ) {
271 - $errors = new WP_Error();
272 -
273 269 // Validate locale with the same pattern as WP 4.3. See #28303
274 270 if ( ! preg_match( '#^[a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?$#', $args['locale'], $matches ) ) {
275 - $errors->add( 'pll_invalid_locale', __( 'Enter a valid WordPress locale', 'polylang' ) );
271 + add_settings_error( 'general', 'pll_invalid_locale', __( 'Enter a valid WordPress locale', 'polylang' ) );
276 272 }
277 273
278 274 // Validate slug characters
279 275 if ( ! preg_match( '#^[a-z_-]+$#', $args['slug'] ) ) {
280 - $errors->add( 'pll_invalid_slug', __( 'The language code contains invalid characters', 'polylang' ) );
276 + add_settings_error( 'general', 'pll_invalid_slug', __( 'The language code contains invalid characters', 'polylang' ) );
281 277 }
282 278
283 279 // Validate slug is unique
284 280 foreach ( $this->get_languages_list() as $language ) {
285 281 if ( $language->slug === $args['slug'] && ( null === $lang || ( isset( $lang ) && $lang->term_id != $language->term_id ) ) ) {
286 - $errors->add( 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) );
282 + add_settings_error( 'general', 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) );
287 283 }
288 284 }
289 285
290 286 // Validate name
@@ -289,25 +285,17 @@
289 285
290 286 // Validate name
291 287 // No need to sanitize it as wp_insert_term will do it for us
292 288 if ( empty( $args['name'] ) ) {
293 - $errors->add( 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) );
289 + add_settings_error( 'general', 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) );
294 290 }
295 291
296 292 // Validate flag
297 293 if ( ! empty( $args['flag'] ) && ! file_exists( POLYLANG_DIR . '/flags/' . $args['flag'] . '.png' ) ) {
298 - $flag = PLL_Language::get_flag_informations( $args['flag'] );
299 -
300 - if ( ! empty( $flag['url'] ) ) {
301 - $response = function_exists( 'vip_safe_wp_remote_get' ) ? vip_safe_wp_remote_get( esc_url_raw( $flag['url'] ) ) : wp_remote_get( esc_url_raw( $flag['url'] ) );
302 - }
303 -
304 - if ( empty( $response ) || is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
305 - $errors->add( 'pll_invalid_flag', __( 'The flag does not exist', 'polylang' ) );
306 - }
294 + add_settings_error( 'general', 'pll_invalid_flag', __( 'The flag does not exist', 'polylang' ) );
307 295 }
308 296
309 - return $errors;
297 + return get_settings_errors() ? false : true;
310 298 }
311 299
312 300 /**
313 301 * Used to set the language of posts or terms in mass
@@ -320,12 +308,11 @@
320 308 */
321 309 public function set_language_in_mass( $type, $ids, $lang ) {
322 310 global $wpdb;
323 311
324 - $ids = array_map( 'intval', $ids );
325 - $lang = $this->get_language( $lang );
326 - $tt_id = 'term' === $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id;
327 - $values = array();
312 + $ids = array_map( 'intval', $ids );
313 + $lang = $this->get_language( $lang );
314 + $tt_id = 'term' === $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id;
328 315
329 316 foreach ( $ids as $id ) {
330 317 $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id );
331 318 }
@@ -337,9 +324,8 @@
337 324 }
338 325
339 326 if ( 'term' === $type ) {
340 327 clean_term_cache( $ids, 'term_language' );
341 - $translations = array();
342 328
343 329 foreach ( $ids as $id ) {
344 330 $translations[] = array( $lang->slug => $id );
345 331 }
@@ -362,19 +348,15 @@
362 348 */
363 349 public function set_translation_in_mass( $type, $translations ) {
364 350 global $wpdb;
365 351
366 - $taxonomy = $type . '_translations';
367 - $terms = array();
368 - $slugs = array();
369 - $description = array();
370 - $count = array();
352 + $taxonomy = $type . '_translations';
371 353
372 354 foreach ( $translations as $t ) {
373 355 $term = uniqid( 'pll_' ); // the term name
374 356 $terms[] = $wpdb->prepare( '( %s, %s )', $term, $term );
375 357 $slugs[] = $wpdb->prepare( '%s', $term );
376 - $description[ $term ] = maybe_serialize( $t );
358 + $description[ $term ] = serialize( $t );
377 359 $count[ $term ] = count( $t );
378 360 }
379 361
380 362 // Insert terms
@@ -384,11 +366,9 @@
384 366 }
385 367
386 368 // Get all terms with their term_id
387 369 // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
388 - $terms = $wpdb->get_results( "SELECT term_id, slug FROM {$wpdb->terms} WHERE slug IN ( " . implode( ',', $slugs ) . ' )' );
389 - $term_ids = array();
390 - $tts = array();
370 + $terms = $wpdb->get_results( "SELECT term_id, slug FROM {$wpdb->terms} WHERE slug IN ( " . implode( ',', $slugs ) . ' )' );
391 371
392 372 // Prepare terms taxonomy relationship
393 373 foreach ( $terms as $term ) {
394 374 $term_ids[] = $term->term_id;
@@ -402,13 +382,12 @@
402 382 }
403 383
404 384 // Get all terms with term_taxonomy_id
405 385 $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
406 - $trs = array();
407 386
408 387 // Prepare objects relationships
409 388 foreach ( $terms as $term ) {
410 - $t = maybe_unserialize( $term->description );
389 + $t = unserialize( $term->description );
411 390 if ( in_array( $t, $translations ) ) {
412 391 foreach ( $t as $object_id ) {
413 392 if ( ! empty( $object_id ) ) {
414 393 $trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id );
@@ -466,9 +445,9 @@
466 445 ),
467 446 )
468 447 );
469 448
470 - // PHPCS:disable WordPress.DB.PreparedSQL
449 + // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared
471 450 $terms = $wpdb->get_col(
472 451 sprintf(
473 452 "SELECT {$wpdb->term_taxonomy}.term_id FROM {$wpdb->term_taxonomy}
474 453 WHERE taxonomy IN ('%s')
@@ -503,17 +482,13 @@
503 482 */
504 483 public function update_translations( $old_slug, $new_slug = '' ) {
505 484 global $wpdb;
506 485
507 - $terms = get_terms( array( 'post_translations', 'term_translations' ) );
508 - $term_ids = array();
509 - $dr = array();
510 - $dt = array();
511 - $ut = array();
486 + $terms = get_terms( array( 'post_translations', 'term_translations' ) );
512 487
513 488 foreach ( $terms as $term ) {
514 489 $term_ids[ $term->taxonomy ][] = $term->term_id;
515 - $tr = maybe_unserialize( $term->description );
490 + $tr = unserialize( $term->description );
516 491 if ( ! empty( $tr[ $old_slug ] ) ) {
517 492 if ( $new_slug ) {
518 493 $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete
519 494 } else {
@@ -525,9 +500,9 @@
525 500 if ( empty( $tr ) || 1 == count( $tr ) ) {
526 501 $dt['t'][] = (int) $term->term_id;
527 502 $dt['tt'][] = (int) $term->term_taxonomy_id;
528 503 } else {
529 - $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, maybe_serialize( $tr ) );
504 + $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, serialize( $tr ) );
530 505 $ut['in'][] = (int) $term->term_id;
531 506 }
532 507 }
533 508 }
@@ -578,10 +553,8 @@
578 553 public function update_default_lang( $slug ) {
579 554 // The nav menus stored in theme locations should be in the default language
580 555 $theme = get_stylesheet();
581 556 if ( ! empty( $this->options['nav_menus'][ $theme ] ) ) {
582 - $menus = array();
583 -
584 557 foreach ( $this->options['nav_menus'][ $theme ] as $key => $loc ) {
585 558 $menus[ $key ] = empty( $loc[ $slug ] ) ? 0 : $loc[ $slug ];
586 559 }
587 560 set_theme_mod( 'nav_menu_locations', $menus );