PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | admin/admin-model.php +71 -108 2.7.12.3.6 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,14 +249,14 @@
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 /**
257 258 * Validates data entered when creating or updating a language
258 - *
259 259 * @see PLL_Admin_Model::add_language
260 260 *
261 261 * @since 0.4
262 262 *
@@ -264,24 +264,22 @@
264 264 * @param object $lang optional the language currently updated, the language is created if not set
265 265 * @return bool true if success / false if failed
266 266 */
267 267 protected function validate_lang( $args, $lang = null ) {
268 - $errors = new WP_Error();
269 -
270 268 // Validate locale with the same pattern as WP 4.3. See #28303
271 269 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' ) );
270 + add_settings_error( 'general', 'pll_invalid_locale', __( 'Enter a valid WordPress locale', 'polylang' ) );
273 271 }
274 272
275 273 // Validate slug characters
276 274 if ( ! preg_match( '#^[a-z_-]+$#', $args['slug'] ) ) {
277 - $errors->add( 'pll_invalid_slug', __( 'The language code contains invalid characters', 'polylang' ) );
275 + add_settings_error( 'general', 'pll_invalid_slug', __( 'The language code contains invalid characters', 'polylang' ) );
278 276 }
279 277
280 278 // Validate slug is unique
281 279 foreach ( $this->get_languages_list() as $language ) {
282 280 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' ) );
281 + add_settings_error( 'general', 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) );
284 282 }
285 283 }
286 284
287 285 // Validate name
@@ -286,25 +284,17 @@
286 284
287 285 // Validate name
288 286 // No need to sanitize it as wp_insert_term will do it for us
289 287 if ( empty( $args['name'] ) ) {
290 - $errors->add( 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) );
288 + add_settings_error( 'general', 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) );
291 289 }
292 290
293 291 // Validate flag
294 292 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 - }
293 + add_settings_error( 'general', 'pll_invalid_flag', __( 'The flag does not exist', 'polylang' ) );
304 294 }
305 295
306 - return $errors;
296 + return get_settings_errors() ? false : true;
307 297 }
308 298
309 299 /**
310 300 * Used to set the language of posts or terms in mass
@@ -317,12 +307,11 @@
317 307 */
318 308 public function set_language_in_mass( $type, $ids, $lang ) {
319 309 global $wpdb;
320 310
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();
311 + $ids = array_map( 'intval', $ids );
312 + $lang = $this->get_language( $lang );
313 + $tt_id = 'term' === $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id;
325 314
326 315 foreach ( $ids as $id ) {
327 316 $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id );
328 317 }
@@ -327,16 +316,15 @@
327 316 $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id );
328 317 }
329 318
330 319 if ( ! empty( $values ) ) {
331 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
332 - $wpdb->query( "INSERT INTO {$wpdb->term_relationships} ( object_id, term_taxonomy_id ) VALUES " . implode( ',', array_unique( $values ) ) );
320 + $values = array_unique( $values );
321 + $wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $values ) );
333 322 $lang->update_count(); // Updating term count is mandatory ( thanks to AndyDeGroo )
334 323 }
335 324
336 325 if ( 'term' === $type ) {
337 326 clean_term_cache( $ids, 'term_language' );
338 - $translations = array();
339 327
340 328 foreach ( $ids as $id ) {
341 329 $translations[] = array( $lang->slug => $id );
342 330 }
@@ -359,33 +347,26 @@
359 347 */
360 348 public function set_translation_in_mass( $type, $translations ) {
361 349 global $wpdb;
362 350
363 - $taxonomy = $type . '_translations';
364 - $terms = array();
365 - $slugs = array();
366 - $description = array();
367 - $count = array();
351 + $taxonomy = $type . '_translations';
368 352
369 353 foreach ( $translations as $t ) {
370 354 $term = uniqid( 'pll_' ); // the term name
371 355 $terms[] = $wpdb->prepare( '( %s, %s )', $term, $term );
372 356 $slugs[] = $wpdb->prepare( '%s', $term );
373 - $description[ $term ] = maybe_serialize( $t );
357 + $description[ $term ] = serialize( $t );
374 358 $count[ $term ] = count( $t );
375 359 }
376 360
377 361 // Insert terms
378 362 if ( ! empty( $terms ) ) {
379 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
380 - $wpdb->query( "INSERT INTO {$wpdb->terms} ( slug, name ) VALUES " . implode( ',', array_unique( $terms ) ) );
363 + $terms = array_unique( $terms );
364 + $wpdb->query( "INSERT INTO $wpdb->terms ( slug, name ) VALUES " . implode( ',', $terms ) );
381 365 }
382 366
383 367 // Get all terms with their term_id
384 - // 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();
368 + $terms = $wpdb->get_results( "SELECT term_id, slug FROM $wpdb->terms WHERE slug IN ( " . implode( ',', $slugs ) . ' )' );
388 369
389 370 // Prepare terms taxonomy relationship
390 371 foreach ( $terms as $term ) {
391 372 $term_ids[] = $term->term_id;
@@ -393,19 +374,18 @@
393 374 }
394 375
395 376 // Insert term_taxonomy
396 377 if ( ! empty( $tts ) ) {
397 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
398 - $wpdb->query( "INSERT INTO {$wpdb->term_taxonomy} ( term_id, taxonomy, description, count ) VALUES " . implode( ',', array_unique( $tts ) ) );
378 + $tts = array_unique( $tts );
379 + $wpdb->query( "INSERT INTO $wpdb->term_taxonomy ( term_id, taxonomy, description, count ) VALUES " . implode( ',', $tts ) );
399 380 }
400 381
401 382 // Get all terms with term_taxonomy_id
402 383 $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
403 - $trs = array();
404 384
405 385 // Prepare objects relationships
406 386 foreach ( $terms as $term ) {
407 - $t = maybe_unserialize( $term->description );
387 + $t = unserialize( $term->description );
408 388 if ( in_array( $t, $translations ) ) {
409 389 foreach ( $t as $object_id ) {
410 390 if ( ! empty( $object_id ) ) {
411 391 $trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id );
@@ -415,10 +395,9 @@
415 395 }
416 396
417 397 // Insert term_relationships
418 398 if ( ! empty( $trs ) ) {
419 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
420 - $wpdb->query( "INSERT INTO {$wpdb->term_relationships} ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $trs ) );
399 + $wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $trs ) );
421 400 $trs = array_unique( $trs );
422 401 }
423 402
424 403 clean_term_cache( $term_ids, $taxonomy );
@@ -446,40 +425,34 @@
446 425 * @param int $limit Max number of posts or terms to retrieve from the database
447 426 */
448 427 $limit = (int) apply_filters( 'get_objects_with_no_lang_limit', $limit );
449 428
450 - $posts = get_posts(
451 - array(
452 - 'numberposts' => $limit,
453 - 'nopaging' => $limit <= 0,
454 - 'post_type' => $this->get_translated_post_types(),
455 - 'post_status' => 'any',
456 - 'fields' => 'ids',
457 - 'tax_query' => array(
458 - array(
459 - 'taxonomy' => 'language',
460 - 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ),
461 - 'operator' => 'NOT IN',
462 - ),
429 + $posts = get_posts( array(
430 + 'numberposts' => $limit,
431 + 'nopaging' => $limit <= 0,
432 + 'post_type' => $this->get_translated_post_types(),
433 + 'post_status' => 'any',
434 + 'fields' => 'ids',
435 + 'tax_query' => array(
436 + array(
437 + 'taxonomy' => 'language',
438 + 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ),
439 + 'operator' => 'NOT IN',
463 440 ),
464 - )
465 - );
441 + ),
442 + ) );
466 443
467 - // PHPCS:disable WordPress.DB.PreparedSQL
468 - $terms = $wpdb->get_col(
469 - sprintf(
470 - "SELECT {$wpdb->term_taxonomy}.term_id FROM {$wpdb->term_taxonomy}
471 - WHERE taxonomy IN ('%s')
472 - AND {$wpdb->term_taxonomy}.term_id NOT IN (
473 - SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (%s)
474 - )
475 - %s",
476 - implode( "','", array_map( 'esc_sql', $this->get_translated_taxonomies() ) ),
477 - implode( ',', array_map( 'intval', $this->get_languages_list( array( 'fields' => 'tl_term_taxonomy_id' ) ) ) ),
478 - $limit > 0 ? "LIMIT {$limit}" : ''
444 + $terms = $wpdb->get_col( sprintf( "
445 + SELECT {$wpdb->term_taxonomy}.term_id FROM {$wpdb->term_taxonomy}
446 + WHERE taxonomy IN ('%s')
447 + AND {$wpdb->term_taxonomy}.term_id NOT IN (
448 + SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (%s)
479 449 )
480 - );
481 - // PHPCS:enable
450 + %s",
451 + implode( "','", array_map( 'esc_sql', $this->get_translated_taxonomies() ) ),
452 + implode( ',', array_map( 'intval', $this->get_languages_list( array( 'fields' => 'tl_term_taxonomy_id' ) ) ) ),
453 + $limit > 0 ? "LIMIT {$limit}" : ''
454 + ) );
482 455
483 456 /**
484 457 * Filter the list of untranslated posts ids and terms ids
485 458 *
@@ -500,17 +473,13 @@
500 473 */
501 474 public function update_translations( $old_slug, $new_slug = '' ) {
502 475 global $wpdb;
503 476
504 - $terms = get_terms( array( 'post_translations', 'term_translations' ) );
505 - $term_ids = array();
506 - $dr = array();
507 - $dt = array();
508 - $ut = array();
477 + $terms = get_terms( array( 'post_translations', 'term_translations' ) );
509 478
510 479 foreach ( $terms as $term ) {
511 480 $term_ids[ $term->taxonomy ][] = $term->term_id;
512 - $tr = maybe_unserialize( $term->description );
481 + $tr = unserialize( $term->description );
513 482 if ( ! empty( $tr[ $old_slug ] ) ) {
514 483 if ( $new_slug ) {
515 484 $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete
516 485 } else {
@@ -522,9 +491,9 @@
522 491 if ( empty( $tr ) || 1 == count( $tr ) ) {
523 492 $dt['t'][] = (int) $term->term_id;
524 493 $dt['tt'][] = (int) $term->term_taxonomy_id;
525 494 } else {
526 - $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, maybe_serialize( $tr ) );
495 + $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, serialize( $tr ) );
527 496 $ut['in'][] = (int) $term->term_id;
528 497 }
529 498 }
530 499 }
@@ -530,32 +499,28 @@
530 499 }
531 500
532 501 // Delete relationships
533 502 if ( ! empty( $dr ) ) {
534 - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared
535 - $wpdb->query(
536 - "DELETE FROM $wpdb->term_relationships
503 + $wpdb->query( "
504 + DELETE FROM $wpdb->term_relationships
537 505 WHERE object_id IN ( " . implode( ',', $dr['id'] ) . ' )
538 - AND term_taxonomy_id IN ( ' . implode( ',', $dr['tt'] ) . ' )'
539 - );
540 - // PHPCS:enable
506 + AND term_taxonomy_id IN ( ' . implode( ',', $dr['tt'] ) . ' )
507 + ' );
541 508 }
542 509
543 510 // Delete terms
544 511 if ( ! empty( $dt ) ) {
545 - $wpdb->query( "DELETE FROM $wpdb->terms WHERE term_id IN ( " . implode( ',', $dt['t'] ) . ' )' ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
546 - $wpdb->query( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ( " . implode( ',', $dt['tt'] ) . ' )' ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
512 + $wpdb->query( "DELETE FROM $wpdb->terms WHERE term_id IN ( " . implode( ',', $dt['t'] ) . ' )' );
513 + $wpdb->query( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ( " . implode( ',', $dt['tt'] ) . ' )' );
547 514 }
548 515
549 516 // Update terms
550 517 if ( ! empty( $ut ) ) {
551 - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared
552 - $wpdb->query(
553 - "UPDATE $wpdb->term_taxonomy
518 + $wpdb->query( "
519 + UPDATE $wpdb->term_taxonomy
554 520 SET description = ( CASE term_id " . implode( ' ', $ut['case'] ) . ' END )
555 - WHERE term_id IN ( ' . implode( ',', $ut['in'] ) . ' )'
556 - );
557 - // PHPCS:enable
521 + WHERE term_id IN ( ' . implode( ',', $ut['in'] ) . ' )
522 + ' );
558 523 }
559 524
560 525 if ( ! empty( $term_ids ) ) {
561 526 foreach ( $term_ids as $taxonomy => $ids ) {
@@ -575,10 +540,8 @@
575 540 public function update_default_lang( $slug ) {
576 541 // The nav menus stored in theme locations should be in the default language
577 542 $theme = get_stylesheet();
578 543 if ( ! empty( $this->options['nav_menus'][ $theme ] ) ) {
579 - $menus = array();
580 -
581 544 foreach ( $this->options['nav_menus'][ $theme ] as $key => $loc ) {
582 545 $menus[ $key ] = empty( $loc[ $slug ] ) ? 0 : $loc[ $slug ];
583 546 }
584 547 set_theme_mod( 'nav_menu_locations', $menus );