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