PluginProbe
Polylang / 2.7.3
Polylang v2.7.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 +88 -105 3.0.22.7.3 View file →
@@ -1,11 +1,8 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 - * Extends the PLL_Model class with methods needed only in Polylang settings pages.
4 + * Extends the PLL_Model class with methods needed only in Polylang settings pages
8 5 *
9 6 * @since 1.2
10 7 */
11 8 class PLL_Admin_Model extends PLL_Model {
@@ -11,22 +8,25 @@
11 8 class PLL_Admin_Model extends PLL_Model {
12 9
13 10 /**
14 11 * Adds a new language
15 - * and creates a default category for this language.
12 + * Creates a default category for this language
16 13 *
14 + * List of arguments that $args must contain:
15 + * name -> language name ( used only for display )
16 + * slug -> language code ( ideally 2-letters ISO 639-1 language code )
17 + * locale -> WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
18 + * rtl -> 1 if rtl language, 0 otherwise
19 + * term_group -> language order when displayed
20 + *
21 + * Optional arguments that $args can contain:
22 + * no_default_cat -> if set, no default category will be created for this language
23 + * flag -> country code, see flags.php
24 + *
17 25 * @since 1.2
18 26 *
19 - * @param array $args {
20 - * @type string $name Language name ( used only for display ).
21 - * @type string $slug Language code ( ideally 2-letters ISO 639-1 language code ).
22 - * @type string $locale WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
23 - * @type int $rtl 1 if rtl language, 0 otherwise.
24 - * @type int $term_group Language order when displayed.
25 - * @type string $no_default_cat Optional, if set, no default category will be created for this language.
26 - * @type string $flag Optional, country code, @see flags.php.
27 - * }
28 - * @return WP_Error|true true if success / WP_Error if failed.
27 + * @param array $args
28 + * @return bool true if success / false if failed
29 29 */
30 30 public function add_language( $args ) {
31 31 $errors = $this->validate_lang( $args );
32 32 if ( $errors->get_error_code() ) { // Using has_errors() would be more meaningful but is available only since WP 5.0
@@ -63,13 +63,13 @@
63 63 $mo = new PLL_MO();
64 64 $mo->export_to_db( $this->get_language( $args['slug'] ) );
65 65
66 66 /**
67 - * Fires when a language is added.
67 + * Fires when a language is added
68 68 *
69 69 * @since 1.9
70 70 *
71 - * @param array $args Arguments used to create the language. @see PLL_Admin_Model::add_language().
71 + * @param array $args arguments used to create the language
72 72 */
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
@@ -77,14 +77,13 @@
77 77 return true;
78 78 }
79 79
80 80 /**
81 - * Delete a language.
81 + * Delete a language
82 82 *
83 83 * @since 1.2
84 84 *
85 - * @param int $lang_id Language term_id.
86 - * @return bool
85 + * @param int $lang_id language term_id
87 86 */
88 87 public function delete_language( $lang_id ) {
89 88 $lang = $this->get_language( (int) $lang_id );
90 89
@@ -139,9 +138,9 @@
139 138 }
140 139
141 140 // Delete the string translations
142 141 $post = wpcom_vip_get_page_by_title( 'polylang_mo_' . $lang->term_id, OBJECT, 'polylang_mo' );
143 - if ( $post instanceof WP_Post ) {
142 + if ( ! empty( $post ) ) {
144 143 wp_delete_post( $post->ID );
145 144 }
146 145
147 146 // Delete domain
@@ -159,22 +158,25 @@
159 158 return true;
160 159 }
161 160
162 161 /**
163 - * Updates language properties.
162 + * Update language properties
164 163 *
164 + * List of arguments that $args must contain:
165 + * lang_id -> term_id of the language to modify
166 + * name -> language name ( used only for display )
167 + * slug -> language code ( ideally 2-letters ISO 639-1 language code
168 + * locale -> WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
169 + * rtl -> 1 if rtl language, 0 otherwise
170 + * term_group -> language order when displayed
171 + *
172 + * Optional arguments that $args can contain:
173 + * flag -> country code, see flags.php
174 + *
165 175 * @since 1.2
166 176 *
167 - * @param array $args {
168 - * @type int $lang_id Id of the language to modify.
169 - * @type string $name Language name ( used only for display ).
170 - * @type string $slug Language code ( ideally 2-letters ISO 639-1 language code ).
171 - * @type string $locale WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
172 - * @type int $rtl 1 if rtl language, 0 otherwise.
173 - * @type int $term_group Language order when displayed.
174 - * @type string $flag Optional, country code, @see flags.php.
175 - * }
176 - * @return WP_Error|true true if success / WP_Error if failed.
177 + * @param array $args
178 + * @return bool true if success / false if failed
177 179 */
178 180 public function update_language( $args ) {
179 181 $lang = $this->get_language( (int) $args['lang_id'] );
180 182
@@ -237,13 +239,13 @@
237 239 wp_update_term( (int) $lang->term_id, 'language', array( 'slug' => $slug, 'name' => $args['name'], 'description' => $description, 'term_group' => (int) $args['term_group'] ) );
238 240 wp_update_term( (int) $lang->tl_term_id, 'term_language', array( 'slug' => 'pll_' . $slug, 'name' => $args['name'] ) );
239 241
240 242 /**
241 - * Fires when a language is updated.
243 + * Fires when a language is added
242 244 *
243 245 * @since 1.9
244 246 *
245 - * @param array $args Arguments used to modify the language. @see PLL_Admin_Model::update_language().
247 + * @param array $args arguments used to modify the language
246 248 */
247 249 do_action( 'pll_update_language', $args );
248 250
249 251 $this->clean_languages_cache();
@@ -251,17 +253,17 @@
251 253 return true;
252 254 }
253 255
254 256 /**
255 - * Validates data entered when creating or updating a language.
257 + * Validates data entered when creating or updating a language
256 258 *
257 - * @see PLL_Admin_Model::add_language().
259 + * @see PLL_Admin_Model::add_language
258 260 *
259 261 * @since 0.4
260 262 *
261 - * @param array $args Parameters of {@see PLL_Admin_Model::add_language() or @see PLL_Admin_Model::update_language()}.
262 - * @param PLL_Language $lang Optional the language currently updated, the language is created if not set.
263 - * @return WP_Error
263 + * @param array $args
264 + * @param object $lang optional the language currently updated, the language is created if not set
265 + * @return bool true if success / false if failed
264 266 */
265 267 protected function validate_lang( $args, $lang = null ) {
266 268 $errors = new WP_Error();
267 269
@@ -276,9 +278,9 @@
276 278 }
277 279
278 280 // Validate slug is unique
279 281 foreach ( $this->get_languages_list() as $language ) {
280 - if ( $language->slug === $args['slug'] && ( null === $lang || $lang->term_id !== $language->term_id ) ) {
282 + if ( $language->slug === $args['slug'] && ( null === $lang || ( isset( $lang ) && $lang->term_id != $language->term_id ) ) ) {
281 283 $errors->add( 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) );
282 284 }
283 285 }
284 286
@@ -304,29 +306,23 @@
304 306 return $errors;
305 307 }
306 308
307 309 /**
308 - * Assigns a language to posts or terms in mass.
310 + * Used to set the language of posts or terms in mass
309 311 *
310 312 * @since 1.2
311 313 *
312 - * @param string $type Either 'post' or 'term'.
313 - * @param int[] $ids Array of post ids or term ids.
314 - * @param PLL_Language|string $lang Language to assign to the posts or terms.
315 - * @return void
314 + * @param string $type either 'post' or 'term'
315 + * @param array $ids array of post ids or term ids
316 + * @param object|string $lang object or slug
316 317 */
317 318 public function set_language_in_mass( $type, $ids, $lang ) {
318 319 global $wpdb;
319 320
320 - $lang = $this->get_language( $lang );
321 -
322 - if ( empty( $lang ) ) {
323 - return;
324 - }
325 -
321 + $ids = array_map( 'intval', $ids );
322 + $lang = $this->get_language( $lang );
326 323 $tt_id = 'term' === $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id;
327 324 $values = array();
328 - $ids = array_map( 'intval', $ids );
329 325
330 326 foreach ( $ids as $id ) {
331 327 $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id );
332 328 }
@@ -353,15 +349,14 @@
353 349 }
354 350 }
355 351
356 352 /**
357 - * Creates translations groups in mass.
353 + * Used to create a translations groups in mass
358 354 *
359 355 * @since 1.6.3
360 356 *
361 - * @param string $type Either 'post' or 'term'
362 - * @param array $translations Array of translations arrays.
363 - * @return void
357 + * @param string $type either 'post' or 'term'
358 + * @param array $translations array of translations arrays
364 359 */
365 360 public function set_translation_in_mass( $type, $translations ) {
366 361 global $wpdb;
367 362
@@ -406,17 +401,15 @@
406 401 // Get all terms with term_taxonomy_id
407 402 $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
408 403 $trs = array();
409 404
410 - // Prepare objects relationships.
411 - if ( is_array( $terms ) ) {
412 - foreach ( $terms as $term ) {
413 - $t = maybe_unserialize( $term->description );
414 - if ( in_array( $t, $translations ) ) {
415 - foreach ( $t as $object_id ) {
416 - if ( ! empty( $object_id ) ) {
417 - $trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id );
418 - }
405 + // Prepare objects relationships
406 + foreach ( $terms as $term ) {
407 + $t = maybe_unserialize( $term->description );
408 + if ( in_array( $t, $translations ) ) {
409 + foreach ( $t as $object_id ) {
410 + if ( ! empty( $object_id ) ) {
411 + $trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id );
419 412 }
420 413 }
421 414 }
422 415 }
@@ -431,32 +424,27 @@
431 424 clean_term_cache( $term_ids, $taxonomy );
432 425 }
433 426
434 427 /**
435 - * Returns untranslated posts and terms ids ( used in settings ).
428 + * Returns untranslated posts and terms ids ( used in settings )
436 429 *
437 430 * @since 0.9
438 - * @since 2.2.6 Add the $limit argument.
431 + * @since 2.2.6 Add the $limit argument
439 432 *
440 - * @param int $limit Max number of posts or terms to return. Defaults to -1 (no limit).
441 - * @return array {
442 - * Objects without language.
443 - *
444 - * @type int[] $posts Array of post ids.
445 - * @type int[] $terms Array of term ids.
446 - * }
433 + * @param in $limit Max number of posts or terms to return. Defaults to -1 (no limit).
434 + * @return array Array made of an array of post ids and an array of term ids
447 435 */
448 436 public function get_objects_with_no_lang( $limit = -1 ) {
449 437 global $wpdb;
450 438
451 439 /**
452 - * Filters the max number of posts or terms to return when searching objects with no language.
440 + * Filters the max number of posts or terms to return when searching objects with no language
453 441 * This filter can be used to decrease the memory usage in case the number of objects
454 442 * without language is too big. Using a negative value is equivalent to have no limit.
455 443 *
456 444 * @since 2.2.6
457 445 *
458 - * @param int $limit Max number of posts or terms to retrieve from the database.
446 + * @param int $limit Max number of posts or terms to retrieve from the database
459 447 */
460 448 $limit = (int) apply_filters( 'get_objects_with_no_lang_limit', $limit );
461 449
462 450 $posts = get_posts(
@@ -492,26 +480,24 @@
492 480 );
493 481 // PHPCS:enable
494 482
495 483 /**
496 - * Filters the list of untranslated posts ids and terms ids
484 + * Filter the list of untranslated posts ids and terms ids
497 485 *
498 486 * @since 0.9
499 487 *
500 - * @param array|false $objects false if no ids found, list of post and/or term ids otherwise.
488 + * @param bool|array $objects false if no ids found, list of post and/or term ids otherwise
501 489 */
502 490 return apply_filters( 'pll_get_objects_with_no_lang', empty( $posts ) && empty( $terms ) ? false : array( 'posts' => $posts, 'terms' => $terms ) );
503 491 }
504 492
505 493 /**
506 - * Updates the translations when a language slug has been modified in settings
507 - * or deletes them when a language is removed.
494 + * Used to delete translations or update the translations when a language slug has been modified in settings
508 495 *
509 496 * @since 0.5
510 497 *
511 - * @param string $old_slug The old language slug.
512 - * @param string $new_slug Optional, the new language slug, if not set it means that the language has been deleted.
513 - * @return void
498 + * @param string $old_slug the old language slug
499 + * @param string $new_slug optional, the new language slug, if not set it means the correspondent has been deleted
514 500 */
515 501 public function update_translations( $old_slug, $new_slug = '' ) {
516 502 global $wpdb;
517 503
@@ -520,28 +506,26 @@
520 506 $dr = array();
521 507 $dt = array();
522 508 $ut = array();
523 509
524 - if ( is_array( $terms ) ) {
525 - foreach ( $terms as $term ) {
526 - $term_ids[ $term->taxonomy ][] = $term->term_id;
527 - $tr = maybe_unserialize( $term->description );
528 - if ( ! empty( $tr[ $old_slug ] ) ) {
529 - if ( $new_slug ) {
530 - $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete
531 - } else {
532 - $dr['id'][] = (int) $tr[ $old_slug ];
533 - $dr['tt'][] = (int) $term->term_taxonomy_id;
534 - }
535 - unset( $tr[ $old_slug ] );
510 + foreach ( $terms as $term ) {
511 + $term_ids[ $term->taxonomy ][] = $term->term_id;
512 + $tr = maybe_unserialize( $term->description );
513 + if ( ! empty( $tr[ $old_slug ] ) ) {
514 + if ( $new_slug ) {
515 + $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete
516 + } else {
517 + $dr['id'][] = (int) $tr[ $old_slug ];
518 + $dr['tt'][] = (int) $term->term_taxonomy_id;
519 + }
520 + unset( $tr[ $old_slug ] );
536 521
537 - if ( empty( $tr ) || 1 == count( $tr ) ) {
538 - $dt['t'][] = (int) $term->term_id;
539 - $dt['tt'][] = (int) $term->term_taxonomy_id;
540 - } else {
541 - $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, maybe_serialize( $tr ) );
542 - $ut['in'][] = (int) $term->term_id;
543 - }
522 + if ( empty( $tr ) || 1 == count( $tr ) ) {
523 + $dt['t'][] = (int) $term->term_id;
524 + $dt['tt'][] = (int) $term->term_taxonomy_id;
525 + } else {
526 + $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, maybe_serialize( $tr ) );
527 + $ut['in'][] = (int) $term->term_id;
544 528 }
545 529 }
546 530 }
547 531
@@ -581,14 +565,13 @@
581 565 }
582 566
583 567 /**
584 568 * Updates the default language
585 - * taking care to update the default category & the nav menu locations.
569 + * taking care to update the default category & the nav menu locations
586 570 *
587 571 * @since 1.8
588 572 *
589 - * @param string $slug New language slug.
590 - * @return void
573 + * @param string $slug new language slug
591 574 */
592 575 public function update_default_lang( $slug ) {
593 576 // The nav menus stored in theme locations should be in the default language
594 577 $theme = get_stylesheet();