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