| @@ -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 |
| @@ -45,9 +45,9 @@ | ||
| 45 | 45 | // The term_language taxonomy |
| 46 | 46 | // Don't want shared terms so use a different slug |
| 47 | 47 | wp_insert_term( $args['name'], 'term_language', array( 'slug' => 'pll_' . $args['slug'] ) ); |
| 48 | 48 | |
| 49 | - $this->clean_languages_cache(); // Update the languages list now ! | |
| 49 | + $this->clean_languages_cache(); // Udpate the languages list now ! | |
| 50 | 50 | |
| 51 | 51 | if ( ! isset( $this->options['default_lang'] ) ) { |
| 52 | 52 | // If this is the first language created, set it as default language |
| 53 | 53 | $this->options['default_lang'] = $args['slug']; |
| @@ -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 | /** |
| @@ -86,12 +88,8 @@ | ||
| 86 | 88 | */ |
| 87 | 89 | public function delete_language( $lang_id ) { |
| 88 | 90 | $lang = $this->get_language( (int) $lang_id ); |
| 89 | 91 | |
| 90 | - if ( empty( $lang ) ) { | |
| 91 | - return false; | |
| 92 | - } | |
| 93 | - | |
| 94 | 92 | // Oops ! we are deleting the default language... |
| 95 | 93 | // Need to do this before loosing the information for default category translations |
| 96 | 94 | if ( $this->options['default_lang'] == $lang->slug ) { |
| 97 | 95 | $slugs = $this->get_languages_list( array( 'fields' => 'slug' ) ); |
| @@ -124,9 +122,9 @@ | ||
| 124 | 122 | |
| 125 | 123 | // Delete menus locations |
| 126 | 124 | if ( ! empty( $this->options['nav_menus'] ) ) { |
| 127 | 125 | foreach ( $this->options['nav_menus'] as $theme => $locations ) { |
| 128 | - foreach ( array_keys( $locations ) as $location ) { | |
| 126 | + foreach ( $locations as $location => $languages ) { | |
| 129 | 127 | unset( $this->options['nav_menus'][ $theme ][ $location ][ $lang->slug ] ); |
| 130 | 128 | } |
| 131 | 129 | } |
| 132 | 130 | } |
| @@ -132,10 +130,11 @@ | ||
| 132 | 130 | } |
| 133 | 131 | |
| 134 | 132 | // Delete users options |
| 135 | 133 | foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) { |
| 134 | + delete_user_meta( $user_id, 'user_lang', $lang->locale ); | |
| 136 | 135 | delete_user_meta( $user_id, 'pll_filter_content', $lang->slug ); |
| 137 | - delete_user_meta( $user_id, 'description_' . $lang->slug ); | |
| 136 | + delete_user_meta( $user_id, 'description_'.$lang->slug ); | |
| 138 | 137 | } |
| 139 | 138 | |
| 140 | 139 | // Delete the string translations |
| 141 | 140 | $post = wpcom_vip_get_page_by_title( 'polylang_mo_' . $lang->term_id, OBJECT, 'polylang_mo' ); |
| @@ -154,9 +153,9 @@ | ||
| 154 | 153 | $this->clean_languages_cache(); |
| 155 | 154 | |
| 156 | 155 | update_option( 'polylang', $this->options ); |
| 157 | 156 | flush_rewrite_rules(); // refresh rewrite rules |
| 158 | - return true; | |
| 157 | + add_settings_error( 'general', 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'updated' ); | |
| 159 | 158 | } |
| 160 | 159 | |
| 161 | 160 | /** |
| 162 | 161 | * Update language properties |
| @@ -178,12 +177,10 @@ | ||
| 178 | 177 | * @return bool true if success / false if failed |
| 179 | 178 | */ |
| 180 | 179 | public function update_language( $args ) { |
| 181 | 180 | $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; | |
| 181 | + if ( ! $this->validate_lang( $args, $lang ) ) { | |
| 182 | + return false; | |
| 186 | 183 | } |
| 187 | 184 | |
| 188 | 185 | // Update links to this language in posts and terms in case the slug has been modified |
| 189 | 186 | $slug = $args['slug']; |
| @@ -210,9 +207,9 @@ | ||
| 210 | 207 | |
| 211 | 208 | // Update menus locations |
| 212 | 209 | if ( ! empty( $this->options['nav_menus'] ) ) { |
| 213 | 210 | foreach ( $this->options['nav_menus'] as $theme => $locations ) { |
| 214 | - foreach ( array_keys( $locations ) as $location ) { | |
| 211 | + foreach ( $locations as $location => $languages ) { | |
| 215 | 212 | if ( ! empty( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] ) ) { |
| 216 | 213 | $this->options['nav_menus'][ $theme ][ $location ][ $slug ] = $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ]; |
| 217 | 214 | unset( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] ); |
| 218 | 215 | } |
| @@ -234,9 +231,9 @@ | ||
| 234 | 231 | |
| 235 | 232 | update_option( 'polylang', $this->options ); |
| 236 | 233 | |
| 237 | 234 | // 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'] ) ); | |
| 235 | + $description = serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) ); | |
| 239 | 236 | wp_update_term( (int) $lang->term_id, 'language', array( 'slug' => $slug, 'name' => $args['name'], 'description' => $description, 'term_group' => (int) $args['term_group'] ) ); |
| 240 | 237 | wp_update_term( (int) $lang->tl_term_id, 'term_language', array( 'slug' => 'pll_' . $slug, 'name' => $args['name'] ) ); |
| 241 | 238 | |
| 242 | 239 | /** |
| @@ -249,14 +246,14 @@ | ||
| 249 | 246 | do_action( 'pll_update_language', $args ); |
| 250 | 247 | |
| 251 | 248 | $this->clean_languages_cache(); |
| 252 | 249 | flush_rewrite_rules(); // Refresh rewrite rules |
| 250 | + add_settings_error( 'general', 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'updated' ); | |
| 253 | 251 | return true; |
| 254 | 252 | } |
| 255 | 253 | |
| 256 | 254 | /** |
| 257 | 255 | * Validates data entered when creating or updating a language |
| 258 | - * | |
| 259 | 256 | * @see PLL_Admin_Model::add_language |
| 260 | 257 | * |
| 261 | 258 | * @since 0.4 |
| 262 | 259 | * |
| @@ -264,47 +261,35 @@ | ||
| 264 | 261 | * @param object $lang optional the language currently updated, the language is created if not set |
| 265 | 262 | * @return bool true if success / false if failed |
| 266 | 263 | */ |
| 267 | 264 | protected function validate_lang( $args, $lang = null ) { |
| 268 | - $errors = new WP_Error(); | |
| 269 | - | |
| 270 | 265 | // Validate locale with the same pattern as WP 4.3. See #28303 |
| 271 | 266 | 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' ) ); | |
| 267 | + add_settings_error( 'general', 'pll_invalid_locale', __( 'Enter a valid WordPress locale', 'polylang' ) ); | |
| 273 | 268 | } |
| 274 | 269 | |
| 275 | 270 | // Validate slug characters |
| 276 | 271 | if ( ! preg_match( '#^[a-z_-]+$#', $args['slug'] ) ) { |
| 277 | - $errors->add( 'pll_invalid_slug', __( 'The language code contains invalid characters', 'polylang' ) ); | |
| 272 | + add_settings_error( 'general', 'pll_invalid_slug', __( 'The language code contains invalid characters', 'polylang' ) ); | |
| 278 | 273 | } |
| 279 | 274 | |
| 280 | 275 | // Validate slug is unique |
| 281 | - foreach ( $this->get_languages_list() as $language ) { | |
| 282 | - 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' ) ); | |
| 284 | - } | |
| 276 | + if ( $this->get_language( $args['slug'] ) && ( null === $lang || ( isset( $lang ) && $lang->slug != $args['slug'] ) ) ) { | |
| 277 | + add_settings_error( 'general', 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) ); | |
| 285 | 278 | } |
| 286 | 279 | |
| 287 | 280 | // Validate name |
| 288 | 281 | // No need to sanitize it as wp_insert_term will do it for us |
| 289 | 282 | if ( empty( $args['name'] ) ) { |
| 290 | - $errors->add( 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) ); | |
| 283 | + add_settings_error( 'general', 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) ); | |
| 291 | 284 | } |
| 292 | 285 | |
| 293 | 286 | // Validate flag |
| 294 | 287 | 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 | - } | |
| 288 | + add_settings_error( 'general', 'pll_invalid_flag', __( 'The flag does not exist', 'polylang' ) ); | |
| 304 | 289 | } |
| 305 | 290 | |
| 306 | - return $errors; | |
| 291 | + return get_settings_errors() ? false : true; | |
| 307 | 292 | } |
| 308 | 293 | |
| 309 | 294 | /** |
| 310 | 295 | * Used to set the language of posts or terms in mass |
| @@ -317,12 +302,11 @@ | ||
| 317 | 302 | */ |
| 318 | 303 | public function set_language_in_mass( $type, $ids, $lang ) { |
| 319 | 304 | global $wpdb; |
| 320 | 305 | |
| 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(); | |
| 306 | + $ids = array_map( 'intval', $ids ); | |
| 307 | + $lang = $this->get_language( $lang ); | |
| 308 | + $tt_id = 'term' == $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id; | |
| 325 | 309 | |
| 326 | 310 | foreach ( $ids as $id ) { |
| 327 | 311 | $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id ); |
| 328 | 312 | } |
| @@ -327,17 +311,14 @@ | ||
| 327 | 311 | $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id ); |
| 328 | 312 | } |
| 329 | 313 | |
| 330 | 314 | 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 ) ) ); | |
| 315 | + $values = array_unique( $values ); | |
| 316 | + $wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $values ) ); | |
| 333 | 317 | $lang->update_count(); // Updating term count is mandatory ( thanks to AndyDeGroo ) |
| 334 | 318 | } |
| 335 | 319 | |
| 336 | - if ( 'term' === $type ) { | |
| 337 | - clean_term_cache( $ids, 'term_language' ); | |
| 338 | - $translations = array(); | |
| 339 | - | |
| 320 | + if ( 'term' == $type ) { | |
| 340 | 321 | foreach ( $ids as $id ) { |
| 341 | 322 | $translations[] = array( $lang->slug => $id ); |
| 342 | 323 | } |
| 343 | 324 | |
| @@ -343,10 +324,8 @@ | ||
| 343 | 324 | |
| 344 | 325 | if ( ! empty( $translations ) ) { |
| 345 | 326 | $this->set_translation_in_mass( 'term', $translations ); |
| 346 | 327 | } |
| 347 | - } else { | |
| 348 | - clean_term_cache( $ids, 'language' ); | |
| 349 | 328 | } |
| 350 | 329 | } |
| 351 | 330 | |
| 352 | 331 | /** |
| @@ -359,53 +338,42 @@ | ||
| 359 | 338 | */ |
| 360 | 339 | public function set_translation_in_mass( $type, $translations ) { |
| 361 | 340 | global $wpdb; |
| 362 | 341 | |
| 363 | - $taxonomy = $type . '_translations'; | |
| 364 | - $terms = array(); | |
| 365 | - $slugs = array(); | |
| 366 | - $description = array(); | |
| 367 | - $count = array(); | |
| 368 | - | |
| 369 | 342 | foreach ( $translations as $t ) { |
| 370 | 343 | $term = uniqid( 'pll_' ); // the term name |
| 371 | - $terms[] = $wpdb->prepare( '( %s, %s )', $term, $term ); | |
| 372 | - $slugs[] = $wpdb->prepare( '%s', $term ); | |
| 373 | - $description[ $term ] = maybe_serialize( $t ); | |
| 344 | + $terms[] = $wpdb->prepare( '( "%1$s", "%1$s" )', $term ); | |
| 345 | + $slugs[] = $wpdb->prepare( '"%s"', $term ); | |
| 346 | + $description[ $term ] = serialize( $t ); | |
| 374 | 347 | $count[ $term ] = count( $t ); |
| 375 | 348 | } |
| 376 | 349 | |
| 377 | 350 | // Insert terms |
| 378 | 351 | if ( ! empty( $terms ) ) { |
| 379 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 380 | - $wpdb->query( "INSERT INTO {$wpdb->terms} ( slug, name ) VALUES " . implode( ',', array_unique( $terms ) ) ); | |
| 352 | + $terms = array_unique( $terms ); | |
| 353 | + $wpdb->query( "INSERT INTO $wpdb->terms ( slug, name ) VALUES " . implode( ',', $terms ) ); | |
| 381 | 354 | } |
| 382 | 355 | |
| 383 | 356 | // 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(); | |
| 357 | + $terms = $wpdb->get_results( "SELECT term_id, slug FROM $wpdb->terms WHERE slug IN ( " . implode( ',', $slugs ) . " )" ); | |
| 388 | 358 | |
| 389 | 359 | // Prepare terms taxonomy relationship |
| 390 | 360 | foreach ( $terms as $term ) { |
| 391 | - $term_ids[] = $term->term_id; | |
| 392 | - $tts[] = $wpdb->prepare( '( %d, %s, %s, %d )', $term->term_id, $taxonomy, $description[ $term->slug ], $count[ $term->slug ] ); | |
| 361 | + $tts[] = $wpdb->prepare( '( %d, "%s", "%s", %d )', $term->term_id, $type . '_translations', $description[ $term->slug ], $count[ $term->slug ] ); | |
| 393 | 362 | } |
| 394 | 363 | |
| 395 | 364 | // Insert term_taxonomy |
| 396 | 365 | 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 ) ) ); | |
| 366 | + $tts = array_unique( $tts ); | |
| 367 | + $wpdb->query( "INSERT INTO $wpdb->term_taxonomy ( term_id, taxonomy, description, count ) VALUES " . implode( ',', $tts ) ); | |
| 399 | 368 | } |
| 400 | 369 | |
| 401 | 370 | // Get all terms with term_taxonomy_id |
| 402 | - $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); | |
| 403 | - $trs = array(); | |
| 371 | + $terms = get_terms( $type . '_translations', array( 'hide_empty' => false ) ); | |
| 404 | 372 | |
| 405 | 373 | // Prepare objects relationships |
| 406 | 374 | foreach ( $terms as $term ) { |
| 407 | - $t = maybe_unserialize( $term->description ); | |
| 375 | + $t = unserialize( $term->description ); | |
| 408 | 376 | if ( in_array( $t, $translations ) ) { |
| 409 | 377 | foreach ( $t as $object_id ) { |
| 410 | 378 | if ( ! empty( $object_id ) ) { |
| 411 | 379 | $trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id ); |
| @@ -415,72 +383,40 @@ | ||
| 415 | 383 | } |
| 416 | 384 | |
| 417 | 385 | // Insert term_relationships |
| 418 | 386 | 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 ) ); | |
| 387 | + $wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $trs ) ); | |
| 421 | 388 | $trs = array_unique( $trs ); |
| 422 | 389 | } |
| 423 | - | |
| 424 | - clean_term_cache( $term_ids, $taxonomy ); | |
| 425 | 390 | } |
| 426 | 391 | |
| 427 | 392 | /** |
| 428 | - * Returns untranslated posts and terms ids ( used in settings ) | |
| 393 | + * Returns unstranslated posts and terms ids ( used in settings ) | |
| 429 | 394 | * |
| 430 | 395 | * @since 0.9 |
| 431 | - * @since 2.2.6 Add the $limit argument | |
| 432 | 396 | * |
| 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 | |
| 397 | + * @return array array made of an array of post ids and an array of term ids | |
| 435 | 398 | */ |
| 436 | - public function get_objects_with_no_lang( $limit = -1 ) { | |
| 437 | - global $wpdb; | |
| 399 | + public function get_objects_with_no_lang() { | |
| 400 | + $posts = get_posts( array( | |
| 401 | + 'numberposts' => -1, | |
| 402 | + 'nopaging' => true, | |
| 403 | + 'post_type' => $this->get_translated_post_types(), | |
| 404 | + 'post_status' => 'any', | |
| 405 | + 'fields' => 'ids', | |
| 406 | + 'tax_query' => array( array( | |
| 407 | + 'taxonomy' => 'language', | |
| 408 | + 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ), | |
| 409 | + 'operator' => 'NOT IN', | |
| 410 | + ) ) | |
| 411 | + ) ); | |
| 438 | 412 | |
| 439 | - /** | |
| 440 | - * Filters the max number of posts or terms to return when searching objects with no language | |
| 441 | - * This filter can be used to decrease the memory usage in case the number of objects | |
| 442 | - * without language is too big. Using a negative value is equivalent to have no limit. | |
| 443 | - * | |
| 444 | - * @since 2.2.6 | |
| 445 | - * | |
| 446 | - * @param int $limit Max number of posts or terms to retrieve from the database | |
| 447 | - */ | |
| 448 | - $limit = (int) apply_filters( 'get_objects_with_no_lang_limit', $limit ); | |
| 413 | + $terms = get_terms( $this->get_translated_taxonomies(), array( 'get' => 'all', 'fields' => 'ids' ) ); | |
| 414 | + $groups = $this->get_languages_list( array( 'fields' => 'tl_term_id' ) ); | |
| 415 | + $tr_terms = get_objects_in_term( $groups, 'term_language' ); | |
| 416 | + $terms = array_unique( array_diff( $terms, $tr_terms ) ); // array_unique to avoid duplicates if a term is in more than one taxonomy | |
| 417 | + $terms = array_map( 'intval', $terms ); | |
| 449 | 418 | |
| 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 | - ), | |
| 463 | - ), | |
| 464 | - ) | |
| 465 | - ); | |
| 466 | - | |
| 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}" : '' | |
| 479 | - ) | |
| 480 | - ); | |
| 481 | - // PHPCS:enable | |
| 482 | - | |
| 483 | 419 | /** |
| 484 | 420 | * Filter the list of untranslated posts ids and terms ids |
| 485 | 421 | * |
| 486 | 422 | * @since 0.9 |
| @@ -495,22 +431,17 @@ | ||
| 495 | 431 | * |
| 496 | 432 | * @since 0.5 |
| 497 | 433 | * |
| 498 | 434 | * @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 | |
| 435 | + * @param string $new_slug optional, the new language slug, if not set it means the correspondant has been deleted | |
| 500 | 436 | */ |
| 501 | 437 | public function update_translations( $old_slug, $new_slug = '' ) { |
| 502 | 438 | global $wpdb; |
| 503 | 439 | |
| 504 | - $terms = get_terms( array( 'post_translations', 'term_translations' ) ); | |
| 505 | - $term_ids = array(); | |
| 506 | - $dr = array(); | |
| 507 | - $dt = array(); | |
| 508 | - $ut = array(); | |
| 440 | + $terms = get_terms( array( 'post_translations', 'term_translations' ) ); | |
| 509 | 441 | |
| 510 | 442 | foreach ( $terms as $term ) { |
| 511 | - $term_ids[ $term->taxonomy ][] = $term->term_id; | |
| 512 | - $tr = maybe_unserialize( $term->description ); | |
| 443 | + $tr = unserialize( $term->description ); | |
| 513 | 444 | if ( ! empty( $tr[ $old_slug ] ) ) { |
| 514 | 445 | if ( $new_slug ) { |
| 515 | 446 | $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete |
| 516 | 447 | } else { |
| @@ -522,9 +453,9 @@ | ||
| 522 | 453 | if ( empty( $tr ) || 1 == count( $tr ) ) { |
| 523 | 454 | $dt['t'][] = (int) $term->term_id; |
| 524 | 455 | $dt['tt'][] = (int) $term->term_taxonomy_id; |
| 525 | 456 | } else { |
| 526 | - $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, maybe_serialize( $tr ) ); | |
| 457 | + $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, serialize( $tr ) ); | |
| 527 | 458 | $ut['in'][] = (int) $term->term_id; |
| 528 | 459 | } |
| 529 | 460 | } |
| 530 | 461 | } |
| @@ -530,38 +461,32 @@ | ||
| 530 | 461 | } |
| 531 | 462 | |
| 532 | 463 | // Delete relationships |
| 533 | 464 | if ( ! empty( $dr ) ) { |
| 534 | - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared | |
| 535 | - $wpdb->query( | |
| 536 | - "DELETE FROM $wpdb->term_relationships | |
| 537 | - WHERE object_id IN ( " . implode( ',', $dr['id'] ) . ' ) | |
| 538 | - AND term_taxonomy_id IN ( ' . implode( ',', $dr['tt'] ) . ' )' | |
| 539 | - ); | |
| 540 | - // PHPCS:enable | |
| 465 | + $wpdb->query( " | |
| 466 | + DELETE FROM $wpdb->term_relationships | |
| 467 | + WHERE object_id IN ( " . implode( ',', $dr['id'] ) . " ) | |
| 468 | + AND term_taxonomy_id IN ( " . implode( ',', $dr['tt'] ) . " ) | |
| 469 | + " ); | |
| 541 | 470 | } |
| 542 | 471 | |
| 543 | 472 | // Delete terms |
| 544 | 473 | 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 | |
| 474 | + $wpdb->query( "DELETE FROM $wpdb->terms WHERE term_id IN ( " . implode( ',', $dt['t'] ) . " ) " ); | |
| 475 | + $wpdb->query( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ( " . implode( ',', $dt['tt'] ) . " ) " ); | |
| 547 | 476 | } |
| 548 | 477 | |
| 549 | 478 | // Update terms |
| 550 | 479 | if ( ! empty( $ut ) ) { |
| 551 | - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared | |
| 552 | - $wpdb->query( | |
| 553 | - "UPDATE $wpdb->term_taxonomy | |
| 554 | - SET description = ( CASE term_id " . implode( ' ', $ut['case'] ) . ' END ) | |
| 555 | - WHERE term_id IN ( ' . implode( ',', $ut['in'] ) . ' )' | |
| 556 | - ); | |
| 557 | - // PHPCS:enable | |
| 480 | + $wpdb->query( " | |
| 481 | + UPDATE $wpdb->term_taxonomy | |
| 482 | + SET description = ( CASE term_id " . implode( ' ', $ut['case'] ) . " END ) | |
| 483 | + WHERE term_id IN ( " . implode( ',', $ut['in'] ) . " ) | |
| 484 | + " ); | |
| 558 | 485 | } |
| 559 | 486 | |
| 560 | - if ( ! empty( $term_ids ) ) { | |
| 561 | - foreach ( $term_ids as $taxonomy => $ids ) { | |
| 562 | - clean_term_cache( $ids, $taxonomy ); | |
| 563 | - } | |
| 487 | + foreach ( $terms as $term ) { | |
| 488 | + clean_term_cache( $term->term_id, $term->taxonomy ); | |
| 564 | 489 | } |
| 565 | 490 | } |
| 566 | 491 | |
| 567 | 492 | /** |
| @@ -575,10 +500,8 @@ | ||
| 575 | 500 | public function update_default_lang( $slug ) { |
| 576 | 501 | // The nav menus stored in theme locations should be in the default language |
| 577 | 502 | $theme = get_stylesheet(); |
| 578 | 503 | if ( ! empty( $this->options['nav_menus'][ $theme ] ) ) { |
| 579 | - $menus = array(); | |
| 580 | - | |
| 581 | 504 | foreach ( $this->options['nav_menus'][ $theme ] as $key => $loc ) { |
| 582 | 505 | $menus[ $key ] = empty( $loc[ $slug ] ) ? 0 : $loc[ $slug ]; |
| 583 | 506 | } |
| 584 | 507 | set_theme_mod( 'nav_menu_locations', $menus ); |