| @@ -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,35 +8,38 @@ | ||
| 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 | - $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 |
| @@ -63,34 +63,35 @@ | ||
| 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 |
| 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 | /** |
| 81 | - * Delete a language. | |
| 83 | + * Delete a language | |
| 82 | 84 | * |
| 83 | 85 | * @since 1.2 |
| 84 | 86 | * |
| 85 | - * @param int $lang_id Language term_id. | |
| 86 | - * @return bool | |
| 87 | + * @param int $lang_id language term_id | |
| 87 | 88 | */ |
| 88 | 89 | public function delete_language( $lang_id ) { |
| 89 | 90 | $lang = $this->get_language( (int) $lang_id ); |
| 90 | 91 | |
| 91 | 92 | if ( empty( $lang ) ) { |
| 92 | - return false; | |
| 93 | + return; | |
| 93 | 94 | } |
| 94 | 95 | |
| 95 | 96 | // Oops ! we are deleting the default language... |
| 96 | 97 | // Need to do this before loosing the information for default category translations |
| @@ -125,9 +126,9 @@ | ||
| 125 | 126 | |
| 126 | 127 | // Delete menus locations |
| 127 | 128 | if ( ! empty( $this->options['nav_menus'] ) ) { |
| 128 | 129 | foreach ( $this->options['nav_menus'] as $theme => $locations ) { |
| 129 | - foreach ( array_keys( $locations ) as $location ) { | |
| 130 | + foreach ( $locations as $location => $languages ) { | |
| 130 | 131 | unset( $this->options['nav_menus'][ $theme ][ $location ][ $lang->slug ] ); |
| 131 | 132 | } |
| 132 | 133 | } |
| 133 | 134 | } |
| @@ -139,9 +140,9 @@ | ||
| 139 | 140 | } |
| 140 | 141 | |
| 141 | 142 | // Delete the string translations |
| 142 | 143 | $post = wpcom_vip_get_page_by_title( 'polylang_mo_' . $lang->term_id, OBJECT, 'polylang_mo' ); |
| 143 | - if ( $post instanceof WP_Post ) { | |
| 144 | + if ( ! empty( $post ) ) { | |
| 144 | 145 | wp_delete_post( $post->ID ); |
| 145 | 146 | } |
| 146 | 147 | |
| 147 | 148 | // Delete domain |
| @@ -155,33 +156,34 @@ | ||
| 155 | 156 | $this->clean_languages_cache(); |
| 156 | 157 | |
| 157 | 158 | update_option( 'polylang', $this->options ); |
| 158 | 159 | flush_rewrite_rules(); // refresh rewrite rules |
| 159 | - return true; | |
| 160 | + add_settings_error( 'general', 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'updated' ); | |
| 160 | 161 | } |
| 161 | 162 | |
| 162 | 163 | /** |
| 163 | - * Updates language properties. | |
| 164 | + * Update language properties | |
| 164 | 165 | * |
| 166 | + * List of arguments that $args must contain: | |
| 167 | + * lang_id -> term_id of the language to modify | |
| 168 | + * name -> language name ( used only for display ) | |
| 169 | + * slug -> language code ( ideally 2-letters ISO 639-1 language code | |
| 170 | + * locale -> WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded... | |
| 171 | + * rtl -> 1 if rtl language, 0 otherwise | |
| 172 | + * term_group -> language order when displayed | |
| 173 | + * | |
| 174 | + * Optional arguments that $args can contain: | |
| 175 | + * flag -> country code, see flags.php | |
| 176 | + * | |
| 165 | 177 | * @since 1.2 |
| 166 | 178 | * |
| 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. | |
| 179 | + * @param array $args | |
| 180 | + * @return bool true if success / false if failed | |
| 177 | 181 | */ |
| 178 | 182 | public function update_language( $args ) { |
| 179 | 183 | $lang = $this->get_language( (int) $args['lang_id'] ); |
| 180 | - | |
| 181 | - $errors = $this->validate_lang( $args, $lang ); | |
| 182 | - if ( $errors->get_error_code() ) { // Using has_errors() would be more meaningful but is available only since WP 5.0 | |
| 183 | - return $errors; | |
| 184 | + if ( ! $this->validate_lang( $args, $lang ) ) { | |
| 185 | + return false; | |
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | // Update links to this language in posts and terms in case the slug has been modified |
| 187 | 189 | $slug = $args['slug']; |
| @@ -208,9 +210,9 @@ | ||
| 208 | 210 | |
| 209 | 211 | // Update menus locations |
| 210 | 212 | if ( ! empty( $this->options['nav_menus'] ) ) { |
| 211 | 213 | foreach ( $this->options['nav_menus'] as $theme => $locations ) { |
| 212 | - foreach ( array_keys( $locations ) as $location ) { | |
| 214 | + foreach ( $locations as $location => $languages ) { | |
| 213 | 215 | if ( ! empty( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] ) ) { |
| 214 | 216 | $this->options['nav_menus'][ $theme ][ $location ][ $slug ] = $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ]; |
| 215 | 217 | unset( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] ); |
| 216 | 218 | } |
| @@ -232,54 +234,52 @@ | ||
| 232 | 234 | |
| 233 | 235 | update_option( 'polylang', $this->options ); |
| 234 | 236 | |
| 235 | 237 | // And finally update the language itself |
| 236 | - $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'] ) ); | |
| 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(); |
| 250 | 252 | flush_rewrite_rules(); // Refresh rewrite rules |
| 253 | + add_settings_error( 'general', 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'updated' ); | |
| 251 | 254 | return true; |
| 252 | 255 | } |
| 253 | 256 | |
| 254 | 257 | /** |
| 255 | - * Validates data entered when creating or updating a language. | |
| 258 | + * Validates data entered when creating or updating a language | |
| 259 | + * @see PLL_Admin_Model::add_language | |
| 256 | 260 | * |
| 257 | - * @see PLL_Admin_Model::add_language(). | |
| 258 | - * | |
| 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 | - $errors = new WP_Error(); | |
| 267 | - | |
| 268 | 268 | // Validate locale with the same pattern as WP 4.3. See #28303 |
| 269 | 269 | if ( ! preg_match( '#^[a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?$#', $args['locale'], $matches ) ) { |
| 270 | - $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' ) ); | |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | // Validate slug characters |
| 274 | 274 | if ( ! preg_match( '#^[a-z_-]+$#', $args['slug'] ) ) { |
| 275 | - $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' ) ); | |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | // Validate slug is unique |
| 279 | 279 | foreach ( $this->get_languages_list() as $language ) { |
| 280 | - if ( $language->slug === $args['slug'] && ( null === $lang || $lang->term_id !== $language->term_id ) ) { | |
| 281 | - $errors->add( 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) ); | |
| 280 | + if ( $language->slug === $args['slug'] && ( null === $lang || ( isset( $lang ) && $lang->term_id != $language->term_id ) ) ) { | |
| 281 | + add_settings_error( 'general', 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) ); | |
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | // Validate name |
| @@ -284,63 +284,47 @@ | ||
| 284 | 284 | |
| 285 | 285 | // Validate name |
| 286 | 286 | // No need to sanitize it as wp_insert_term will do it for us |
| 287 | 287 | if ( empty( $args['name'] ) ) { |
| 288 | - $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' ) ); | |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | // Validate flag |
| 292 | 292 | if ( ! empty( $args['flag'] ) && ! file_exists( POLYLANG_DIR . '/flags/' . $args['flag'] . '.png' ) ) { |
| 293 | - $flag = PLL_Language::get_flag_informations( $args['flag'] ); | |
| 294 | - | |
| 295 | - if ( ! empty( $flag['url'] ) ) { | |
| 296 | - $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'] ) ); | |
| 297 | - } | |
| 298 | - | |
| 299 | - if ( empty( $response ) || is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { | |
| 300 | - $errors->add( 'pll_invalid_flag', __( 'The flag does not exist', 'polylang' ) ); | |
| 301 | - } | |
| 293 | + add_settings_error( 'general', 'pll_invalid_flag', __( 'The flag does not exist', 'polylang' ) ); | |
| 302 | 294 | } |
| 303 | 295 | |
| 304 | - return $errors; | |
| 296 | + return get_settings_errors() ? false : true; | |
| 305 | 297 | } |
| 306 | 298 | |
| 307 | 299 | /** |
| 308 | - * Assigns a language to posts or terms in mass. | |
| 300 | + * Used to set the language of posts or terms in mass | |
| 309 | 301 | * |
| 310 | 302 | * @since 1.2 |
| 311 | 303 | * |
| 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 | |
| 304 | + * @param string $type either 'post' or 'term' | |
| 305 | + * @param array $ids array of post ids or term ids | |
| 306 | + * @param object|string $lang object or slug | |
| 316 | 307 | */ |
| 317 | 308 | public function set_language_in_mass( $type, $ids, $lang ) { |
| 318 | 309 | global $wpdb; |
| 319 | 310 | |
| 311 | + $ids = array_map( 'intval', $ids ); | |
| 320 | 312 | $lang = $this->get_language( $lang ); |
| 313 | + $tt_id = 'term' === $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id; | |
| 321 | 314 | |
| 322 | - if ( empty( $lang ) ) { | |
| 323 | - return; | |
| 324 | - } | |
| 325 | - | |
| 326 | - $tt_id = 'term' === $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id; | |
| 327 | - $values = array(); | |
| 328 | - $ids = array_map( 'intval', $ids ); | |
| 329 | - | |
| 330 | 315 | foreach ( $ids as $id ) { |
| 331 | 316 | $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id ); |
| 332 | 317 | } |
| 333 | 318 | |
| 334 | 319 | if ( ! empty( $values ) ) { |
| 335 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 336 | - $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 ) ); | |
| 337 | 322 | $lang->update_count(); // Updating term count is mandatory ( thanks to AndyDeGroo ) |
| 338 | 323 | } |
| 339 | 324 | |
| 340 | 325 | if ( 'term' === $type ) { |
| 341 | 326 | clean_term_cache( $ids, 'term_language' ); |
| 342 | - $translations = array(); | |
| 343 | 327 | |
| 344 | 328 | foreach ( $ids as $id ) { |
| 345 | 329 | $translations[] = array( $lang->slug => $id ); |
| 346 | 330 | } |
| @@ -353,44 +337,36 @@ | ||
| 353 | 337 | } |
| 354 | 338 | } |
| 355 | 339 | |
| 356 | 340 | /** |
| 357 | - * Creates translations groups in mass. | |
| 341 | + * Used to create a translations groups in mass | |
| 358 | 342 | * |
| 359 | 343 | * @since 1.6.3 |
| 360 | 344 | * |
| 361 | - * @param string $type Either 'post' or 'term' | |
| 362 | - * @param array $translations Array of translations arrays. | |
| 363 | - * @return void | |
| 345 | + * @param string $type either 'post' or 'term' | |
| 346 | + * @param array $translations array of translations arrays | |
| 364 | 347 | */ |
| 365 | 348 | public function set_translation_in_mass( $type, $translations ) { |
| 366 | 349 | global $wpdb; |
| 367 | 350 | |
| 368 | - $taxonomy = $type . '_translations'; | |
| 369 | - $terms = array(); | |
| 370 | - $slugs = array(); | |
| 371 | - $description = array(); | |
| 372 | - $count = array(); | |
| 351 | + $taxonomy = $type . '_translations'; | |
| 373 | 352 | |
| 374 | 353 | foreach ( $translations as $t ) { |
| 375 | 354 | $term = uniqid( 'pll_' ); // the term name |
| 376 | 355 | $terms[] = $wpdb->prepare( '( %s, %s )', $term, $term ); |
| 377 | 356 | $slugs[] = $wpdb->prepare( '%s', $term ); |
| 378 | - $description[ $term ] = maybe_serialize( $t ); | |
| 357 | + $description[ $term ] = serialize( $t ); | |
| 379 | 358 | $count[ $term ] = count( $t ); |
| 380 | 359 | } |
| 381 | 360 | |
| 382 | 361 | // Insert terms |
| 383 | 362 | if ( ! empty( $terms ) ) { |
| 384 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 385 | - $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 ) ); | |
| 386 | 365 | } |
| 387 | 366 | |
| 388 | 367 | // Get all terms with their term_id |
| 389 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 390 | - $terms = $wpdb->get_results( "SELECT term_id, slug FROM {$wpdb->terms} WHERE slug IN ( " . implode( ',', $slugs ) . ' )' ); | |
| 391 | - $term_ids = array(); | |
| 392 | - $tts = array(); | |
| 368 | + $terms = $wpdb->get_results( "SELECT term_id, slug FROM $wpdb->terms WHERE slug IN ( " . implode( ',', $slugs ) . ' )' ); | |
| 393 | 369 | |
| 394 | 370 | // Prepare terms taxonomy relationship |
| 395 | 371 | foreach ( $terms as $term ) { |
| 396 | 372 | $term_ids[] = $term->term_id; |
| @@ -398,25 +374,22 @@ | ||
| 398 | 374 | } |
| 399 | 375 | |
| 400 | 376 | // Insert term_taxonomy |
| 401 | 377 | if ( ! empty( $tts ) ) { |
| 402 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 403 | - $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 ) ); | |
| 404 | 380 | } |
| 405 | 381 | |
| 406 | 382 | // Get all terms with term_taxonomy_id |
| 407 | 383 | $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); |
| 408 | - $trs = array(); | |
| 409 | 384 | |
| 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 | - } | |
| 385 | + // Prepare objects relationships | |
| 386 | + foreach ( $terms as $term ) { | |
| 387 | + $t = unserialize( $term->description ); | |
| 388 | + if ( in_array( $t, $translations ) ) { | |
| 389 | + foreach ( $t as $object_id ) { | |
| 390 | + if ( ! empty( $object_id ) ) { | |
| 391 | + $trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id ); | |
| 419 | 392 | } |
| 420 | 393 | } |
| 421 | 394 | } |
| 422 | 395 | } |
| @@ -422,10 +395,9 @@ | ||
| 422 | 395 | } |
| 423 | 396 | |
| 424 | 397 | // Insert term_relationships |
| 425 | 398 | if ( ! empty( $trs ) ) { |
| 426 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 427 | - $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 ) ); | |
| 428 | 400 | $trs = array_unique( $trs ); |
| 429 | 401 | } |
| 430 | 402 | |
| 431 | 403 | clean_term_cache( $term_ids, $taxonomy ); |
| @@ -431,117 +403,98 @@ | ||
| 431 | 403 | clean_term_cache( $term_ids, $taxonomy ); |
| 432 | 404 | } |
| 433 | 405 | |
| 434 | 406 | /** |
| 435 | - * Returns untranslated posts and terms ids ( used in settings ). | |
| 407 | + * Returns untranslated posts and terms ids ( used in settings ) | |
| 436 | 408 | * |
| 437 | 409 | * @since 0.9 |
| 438 | - * @since 2.2.6 Add the $limit argument. | |
| 410 | + * @since 2.2.6 Add the $limit argument | |
| 439 | 411 | * |
| 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 | - * } | |
| 412 | + * @param in $limit Max number of posts or terms to return. Defaults to -1 (no limit). | |
| 413 | + * @return array Array made of an array of post ids and an array of term ids | |
| 447 | 414 | */ |
| 448 | 415 | public function get_objects_with_no_lang( $limit = -1 ) { |
| 449 | 416 | global $wpdb; |
| 450 | 417 | |
| 451 | 418 | /** |
| 452 | - * Filters the max number of posts or terms to return when searching objects with no language. | |
| 419 | + * Filters the max number of posts or terms to return when searching objects with no language | |
| 453 | 420 | * This filter can be used to decrease the memory usage in case the number of objects |
| 454 | 421 | * without language is too big. Using a negative value is equivalent to have no limit. |
| 455 | 422 | * |
| 456 | 423 | * @since 2.2.6 |
| 457 | 424 | * |
| 458 | - * @param int $limit Max number of posts or terms to retrieve from the database. | |
| 425 | + * @param int $limit Max number of posts or terms to retrieve from the database | |
| 459 | 426 | */ |
| 460 | 427 | $limit = (int) apply_filters( 'get_objects_with_no_lang_limit', $limit ); |
| 461 | 428 | |
| 462 | - $posts = get_posts( | |
| 463 | - array( | |
| 464 | - 'numberposts' => $limit, | |
| 465 | - 'nopaging' => $limit <= 0, | |
| 466 | - 'post_type' => $this->get_translated_post_types(), | |
| 467 | - 'post_status' => 'any', | |
| 468 | - 'fields' => 'ids', | |
| 469 | - 'tax_query' => array( | |
| 470 | - array( | |
| 471 | - 'taxonomy' => 'language', | |
| 472 | - 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ), | |
| 473 | - 'operator' => 'NOT IN', | |
| 474 | - ), | |
| 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', | |
| 475 | 440 | ), |
| 476 | - ) | |
| 477 | - ); | |
| 441 | + ), | |
| 442 | + ) ); | |
| 478 | 443 | |
| 479 | - // PHPCS:disable WordPress.DB.PreparedSQL | |
| 480 | - $terms = $wpdb->get_col( | |
| 481 | - sprintf( | |
| 482 | - "SELECT {$wpdb->term_taxonomy}.term_id FROM {$wpdb->term_taxonomy} | |
| 483 | - WHERE taxonomy IN ('%s') | |
| 484 | - AND {$wpdb->term_taxonomy}.term_id NOT IN ( | |
| 485 | - SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (%s) | |
| 486 | - ) | |
| 487 | - %s", | |
| 488 | - implode( "','", array_map( 'esc_sql', $this->get_translated_taxonomies() ) ), | |
| 489 | - implode( ',', array_map( 'intval', $this->get_languages_list( array( 'fields' => 'tl_term_taxonomy_id' ) ) ) ), | |
| 490 | - $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) | |
| 491 | 449 | ) |
| 492 | - ); | |
| 493 | - // 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 | + ) ); | |
| 494 | 455 | |
| 495 | 456 | /** |
| 496 | - * Filters the list of untranslated posts ids and terms ids | |
| 457 | + * Filter the list of untranslated posts ids and terms ids | |
| 497 | 458 | * |
| 498 | 459 | * @since 0.9 |
| 499 | 460 | * |
| 500 | - * @param array|false $objects false if no ids found, list of post and/or term ids otherwise. | |
| 461 | + * @param bool|array $objects false if no ids found, list of post and/or term ids otherwise | |
| 501 | 462 | */ |
| 502 | 463 | return apply_filters( 'pll_get_objects_with_no_lang', empty( $posts ) && empty( $terms ) ? false : array( 'posts' => $posts, 'terms' => $terms ) ); |
| 503 | 464 | } |
| 504 | 465 | |
| 505 | 466 | /** |
| 506 | - * Updates the translations when a language slug has been modified in settings | |
| 507 | - * or deletes them when a language is removed. | |
| 467 | + * Used to delete translations or update the translations when a language slug has been modified in settings | |
| 508 | 468 | * |
| 509 | 469 | * @since 0.5 |
| 510 | 470 | * |
| 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 | |
| 471 | + * @param string $old_slug the old language slug | |
| 472 | + * @param string $new_slug optional, the new language slug, if not set it means the correspondent has been deleted | |
| 514 | 473 | */ |
| 515 | 474 | public function update_translations( $old_slug, $new_slug = '' ) { |
| 516 | 475 | global $wpdb; |
| 517 | 476 | |
| 518 | - $terms = get_terms( array( 'post_translations', 'term_translations' ) ); | |
| 519 | - $term_ids = array(); | |
| 520 | - $dr = array(); | |
| 521 | - $dt = array(); | |
| 522 | - $ut = array(); | |
| 477 | + $terms = get_terms( array( 'post_translations', 'term_translations' ) ); | |
| 523 | 478 | |
| 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 ] ); | |
| 479 | + foreach ( $terms as $term ) { | |
| 480 | + $term_ids[ $term->taxonomy ][] = $term->term_id; | |
| 481 | + $tr = unserialize( $term->description ); | |
| 482 | + if ( ! empty( $tr[ $old_slug ] ) ) { | |
| 483 | + if ( $new_slug ) { | |
| 484 | + $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete | |
| 485 | + } else { | |
| 486 | + $dr['id'][] = (int) $tr[ $old_slug ]; | |
| 487 | + $dr['tt'][] = (int) $term->term_taxonomy_id; | |
| 488 | + } | |
| 489 | + unset( $tr[ $old_slug ] ); | |
| 536 | 490 | |
| 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 | - } | |
| 491 | + if ( empty( $tr ) || 1 == count( $tr ) ) { | |
| 492 | + $dt['t'][] = (int) $term->term_id; | |
| 493 | + $dt['tt'][] = (int) $term->term_taxonomy_id; | |
| 494 | + } else { | |
| 495 | + $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, serialize( $tr ) ); | |
| 496 | + $ut['in'][] = (int) $term->term_id; | |
| 544 | 497 | } |
| 545 | 498 | } |
| 546 | 499 | } |
| 547 | 500 | |
| @@ -546,32 +499,28 @@ | ||
| 546 | 499 | } |
| 547 | 500 | |
| 548 | 501 | // Delete relationships |
| 549 | 502 | if ( ! empty( $dr ) ) { |
| 550 | - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared | |
| 551 | - $wpdb->query( | |
| 552 | - "DELETE FROM $wpdb->term_relationships | |
| 503 | + $wpdb->query( " | |
| 504 | + DELETE FROM $wpdb->term_relationships | |
| 553 | 505 | WHERE object_id IN ( " . implode( ',', $dr['id'] ) . ' ) |
| 554 | - AND term_taxonomy_id IN ( ' . implode( ',', $dr['tt'] ) . ' )' | |
| 555 | - ); | |
| 556 | - // PHPCS:enable | |
| 506 | + AND term_taxonomy_id IN ( ' . implode( ',', $dr['tt'] ) . ' ) | |
| 507 | + ' ); | |
| 557 | 508 | } |
| 558 | 509 | |
| 559 | 510 | // Delete terms |
| 560 | 511 | if ( ! empty( $dt ) ) { |
| 561 | - $wpdb->query( "DELETE FROM $wpdb->terms WHERE term_id IN ( " . implode( ',', $dt['t'] ) . ' )' ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 562 | - $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'] ) . ' )' ); | |
| 563 | 514 | } |
| 564 | 515 | |
| 565 | 516 | // Update terms |
| 566 | 517 | if ( ! empty( $ut ) ) { |
| 567 | - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared | |
| 568 | - $wpdb->query( | |
| 569 | - "UPDATE $wpdb->term_taxonomy | |
| 518 | + $wpdb->query( " | |
| 519 | + UPDATE $wpdb->term_taxonomy | |
| 570 | 520 | SET description = ( CASE term_id " . implode( ' ', $ut['case'] ) . ' END ) |
| 571 | - WHERE term_id IN ( ' . implode( ',', $ut['in'] ) . ' )' | |
| 572 | - ); | |
| 573 | - // PHPCS:enable | |
| 521 | + WHERE term_id IN ( ' . implode( ',', $ut['in'] ) . ' ) | |
| 522 | + ' ); | |
| 574 | 523 | } |
| 575 | 524 | |
| 576 | 525 | if ( ! empty( $term_ids ) ) { |
| 577 | 526 | foreach ( $term_ids as $taxonomy => $ids ) { |
| @@ -581,21 +530,18 @@ | ||
| 581 | 530 | } |
| 582 | 531 | |
| 583 | 532 | /** |
| 584 | 533 | * Updates the default language |
| 585 | - * taking care to update the default category & the nav menu locations. | |
| 534 | + * taking care to update the default category & the nav menu locations | |
| 586 | 535 | * |
| 587 | 536 | * @since 1.8 |
| 588 | 537 | * |
| 589 | - * @param string $slug New language slug. | |
| 590 | - * @return void | |
| 538 | + * @param string $slug new language slug | |
| 591 | 539 | */ |
| 592 | 540 | public function update_default_lang( $slug ) { |
| 593 | 541 | // The nav menus stored in theme locations should be in the default language |
| 594 | 542 | $theme = get_stylesheet(); |
| 595 | 543 | if ( ! empty( $this->options['nav_menus'][ $theme ] ) ) { |
| 596 | - $menus = array(); | |
| 597 | - | |
| 598 | 544 | foreach ( $this->options['nav_menus'][ $theme ] as $key => $loc ) { |
| 599 | 545 | $menus[ $key ] = empty( $loc[ $slug ] ) ? 0 : $loc[ $slug ]; |
| 600 | 546 | } |
| 601 | 547 | set_theme_mod( 'nav_menu_locations', $menus ); |