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