| @@ -1,8 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | - * 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. | |
| 5 | 8 | * |
| 6 | 9 | * @since 1.2 |
| 7 | 10 | */ |
| 8 | 11 | class PLL_Admin_Model extends PLL_Model { |
| @@ -8,35 +11,39 @@ | ||
| 8 | 11 | class PLL_Admin_Model extends PLL_Model { |
| 9 | 12 | |
| 10 | 13 | /** |
| 11 | 14 | * Adds a new language |
| 12 | - * Creates a default category for this language | |
| 15 | + * and creates a default category for this language. | |
| 13 | 16 | * |
| 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 | - * | |
| 25 | 17 | * @since 1.2 |
| 26 | 18 | * |
| 27 | - * @param array $args | |
| 28 | - * @return bool true if success / false if failed | |
| 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 | |
| 23 | + * not be loaded... | |
| 24 | + * @type int $rtl 1 if rtl language, 0 otherwise. | |
| 25 | + * @type int $term_group Language order when displayed. | |
| 26 | + * @type string $no_default_cat Optional, if set, no default category will be created for this language. | |
| 27 | + * @type string $flag Optional, country code, {@see settings/flags.php}. | |
| 28 | + * } | |
| 29 | + * @return WP_Error|true true if success / WP_Error if failed. | |
| 29 | 30 | */ |
| 30 | 31 | public function add_language( $args ) { |
| 31 | 32 | $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 | + if ( $errors->has_errors() ) { | |
| 33 | 34 | return $errors; |
| 34 | 35 | } |
| 35 | 36 | |
| 36 | 37 | // First the language taxonomy |
| 37 | - $description = maybe_serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) ); | |
| 38 | - $r = wp_insert_term( $args['name'], 'language', array( 'slug' => $args['slug'], 'description' => $description ) ); | |
| 38 | + $r = wp_insert_term( | |
| 39 | + $args['name'], | |
| 40 | + 'language', | |
| 41 | + array( | |
| 42 | + 'slug' => $args['slug'], | |
| 43 | + 'description' => $this->build_language_metas( $args ), | |
| 44 | + ) | |
| 45 | + ); | |
| 39 | 46 | if ( is_wp_error( $r ) ) { |
| 40 | 47 | // Avoid an ugly fatal error if something went wrong ( reported once in the forum ) |
| 41 | 48 | return new WP_Error( 'pll_add_language', __( 'Impossible to add the language.', 'polylang' ) ); |
| 42 | 49 | } |
| @@ -41,49 +48,42 @@ | ||
| 41 | 48 | return new WP_Error( 'pll_add_language', __( 'Impossible to add the language.', 'polylang' ) ); |
| 42 | 49 | } |
| 43 | 50 | 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 | 51 | |
| 45 | - // The term_language taxonomy | |
| 46 | - // Don't want shared terms so use a different slug | |
| 47 | - wp_insert_term( $args['name'], 'term_language', array( 'slug' => 'pll_' . $args['slug'] ) ); | |
| 52 | + // The other language taxonomies. | |
| 53 | + $this->update_secondary_language_terms( $args['slug'], $args['name'] ); | |
| 48 | 54 | |
| 49 | - $this->clean_languages_cache(); // Update the languages list now ! | |
| 50 | - | |
| 51 | 55 | if ( ! isset( $this->options['default_lang'] ) ) { |
| 52 | 56 | // If this is the first language created, set it as default language |
| 53 | 57 | $this->options['default_lang'] = $args['slug']; |
| 54 | 58 | update_option( 'polylang', $this->options ); |
| 59 | + } | |
| 55 | 60 | |
| 56 | - // And assign default language to default category | |
| 57 | - $this->term->set_language( (int) get_option( 'default_category' ), (int) $r['term_id'] ); | |
| 58 | - } elseif ( empty( $args['no_default_cat'] ) ) { | |
| 59 | - $this->create_default_category( $args['slug'] ); | |
| 60 | - } | |
| 61 | + // Refresh languages. | |
| 62 | + $this->clean_languages_cache(); | |
| 63 | + $this->get_languages_list(); | |
| 61 | 64 | |
| 62 | - // Init a mo_id for this language | |
| 63 | - $mo = new PLL_MO(); | |
| 64 | - $mo->export_to_db( $this->get_language( $args['slug'] ) ); | |
| 65 | + flush_rewrite_rules(); // Refresh rewrite rules. | |
| 65 | 66 | |
| 66 | 67 | /** |
| 67 | - * Fires when a language is added | |
| 68 | + * Fires when a language is added. | |
| 68 | 69 | * |
| 69 | 70 | * @since 1.9 |
| 70 | 71 | * |
| 71 | - * @param array $args arguments used to create the language | |
| 72 | + * @param array $args Arguments used to create the language. @see PLL_Admin_Model::add_language(). | |
| 72 | 73 | */ |
| 73 | 74 | do_action( 'pll_add_language', $args ); |
| 74 | 75 | |
| 75 | - $this->clean_languages_cache(); // Again to set add mo_id in the cached languages list | |
| 76 | - flush_rewrite_rules(); // Refresh rewrite rules | |
| 77 | 76 | return true; |
| 78 | 77 | } |
| 79 | 78 | |
| 80 | 79 | /** |
| 81 | - * Delete a language | |
| 80 | + * Delete a language. | |
| 82 | 81 | * |
| 83 | 82 | * @since 1.2 |
| 84 | 83 | * |
| 85 | - * @param int $lang_id language term_id | |
| 84 | + * @param int $lang_id Language term_id. | |
| 85 | + * @return bool | |
| 86 | 86 | */ |
| 87 | 87 | public function delete_language( $lang_id ) { |
| 88 | 88 | $lang = $this->get_language( (int) $lang_id ); |
| 89 | 89 | |
| @@ -92,9 +92,9 @@ | ||
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | // Oops ! we are deleting the default language... |
| 95 | 95 | // Need to do this before loosing the information for default category translations |
| 96 | - if ( $this->options['default_lang'] == $lang->slug ) { | |
| 96 | + if ( $lang->is_default ) { | |
| 97 | 97 | $slugs = $this->get_languages_list( array( 'fields' => 'slug' ) ); |
| 98 | 98 | $slugs = array_diff( $slugs, array( $lang->slug ) ); |
| 99 | 99 | |
| 100 | 100 | if ( ! empty( $slugs ) ) { |
| @@ -131,28 +131,29 @@ | ||
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | // Delete users options |
| 135 | - foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) { | |
| 136 | - delete_user_meta( $user_id, 'pll_filter_content', $lang->slug ); | |
| 137 | - delete_user_meta( $user_id, 'description_' . $lang->slug ); | |
| 138 | - } | |
| 135 | + delete_metadata( 'user', 0, 'pll_filter_content', '', true ); | |
| 136 | + delete_metadata( 'user', 0, "description_{$lang->slug}", '', true ); | |
| 139 | 137 | |
| 140 | - // Delete the string translations | |
| 141 | - $post = wpcom_vip_get_page_by_title( 'polylang_mo_' . $lang->term_id, OBJECT, 'polylang_mo' ); | |
| 142 | - if ( ! empty( $post ) ) { | |
| 143 | - wp_delete_post( $post->ID ); | |
| 144 | - } | |
| 145 | - | |
| 146 | 138 | // Delete domain |
| 147 | 139 | unset( $this->options['domains'][ $lang->slug ] ); |
| 148 | 140 | |
| 149 | - // Delete the language itself | |
| 150 | - wp_delete_term( $lang->term_id, 'language' ); | |
| 151 | - wp_delete_term( $lang->tl_term_id, 'term_language' ); | |
| 141 | + /* | |
| 142 | + * Delete the language itself. | |
| 143 | + * | |
| 144 | + * Reverses the language taxonomies order is required to make sure 'language' is deleted in last. | |
| 145 | + * | |
| 146 | + * The initial order with the 'language' taxonomy at the beginning of 'PLL_Language::term_props' property | |
| 147 | + * is done by {@see PLL_Model::filter_language_terms_orderby()} | |
| 148 | + */ | |
| 149 | + foreach ( array_reverse( $lang->get_tax_props( 'term_id' ) ) as $taxonomy_name => $term_id ) { | |
| 150 | + wp_delete_term( $term_id, $taxonomy_name ); | |
| 151 | + } | |
| 152 | 152 | |
| 153 | - // Update languages list | |
| 153 | + // Refresh languages. | |
| 154 | 154 | $this->clean_languages_cache(); |
| 155 | + $this->get_languages_list(); | |
| 155 | 156 | |
| 156 | 157 | update_option( 'polylang', $this->options ); |
| 157 | 158 | flush_rewrite_rules(); // refresh rewrite rules |
| 158 | 159 | return true; |
| @@ -158,29 +159,30 @@ | ||
| 158 | 159 | return true; |
| 159 | 160 | } |
| 160 | 161 | |
| 161 | 162 | /** |
| 162 | - * Update language properties | |
| 163 | + * Updates language properties. | |
| 163 | 164 | * |
| 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 | - * | |
| 175 | 165 | * @since 1.2 |
| 176 | 166 | * |
| 177 | - * @param array $args | |
| 178 | - * @return bool true if success / false if failed | |
| 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 | 177 | */ |
| 180 | 178 | public function update_language( $args ) { |
| 181 | 179 | $lang = $this->get_language( (int) $args['lang_id'] ); |
| 182 | 180 | |
| 181 | + if ( empty( $lang ) ) { | |
| 182 | + return new WP_Error( 'pll_invalid_language_id', __( 'The language does not seem to exist.', 'polylang' ) ); | |
| 183 | + } | |
| 184 | + | |
| 183 | 185 | $errors = $this->validate_lang( $args, $lang ); |
| 184 | 186 | if ( $errors->get_error_code() ) { // Using has_errors() would be more meaningful but is available only since WP 5.0 |
| 185 | 187 | return $errors; |
| 186 | 188 | } |
| @@ -226,9 +228,9 @@ | ||
| 226 | 228 | unset( $this->options['domains'][ $old_slug ] ); |
| 227 | 229 | } |
| 228 | 230 | |
| 229 | 231 | // Update the default language option if necessary |
| 230 | - if ( $this->options['default_lang'] == $old_slug ) { | |
| 232 | + if ( $lang->is_default ) { | |
| 231 | 233 | $this->options['default_lang'] = $slug; |
| 232 | 234 | } |
| 233 | 235 | } |
| 234 | 236 | |
| @@ -233,37 +235,130 @@ | ||
| 233 | 235 | } |
| 234 | 236 | |
| 235 | 237 | update_option( 'polylang', $this->options ); |
| 236 | 238 | |
| 237 | - // 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'] ) ); | |
| 239 | - wp_update_term( (int) $lang->term_id, 'language', array( 'slug' => $slug, 'name' => $args['name'], 'description' => $description, 'term_group' => (int) $args['term_group'] ) ); | |
| 240 | - wp_update_term( (int) $lang->tl_term_id, 'term_language', array( 'slug' => 'pll_' . $slug, 'name' => $args['name'] ) ); | |
| 239 | + // And finally update the language itself. | |
| 240 | + $this->update_secondary_language_terms( $args['slug'], $args['name'], $lang ); | |
| 241 | 241 | |
| 242 | + $description = $this->build_language_metas( $args ); | |
| 243 | + wp_update_term( $lang->get_tax_prop( 'language', 'term_id' ), 'language', array( 'slug' => $slug, 'name' => $args['name'], 'description' => $description, 'term_group' => (int) $args['term_group'] ) ); | |
| 244 | + | |
| 245 | + // Refresh languages. | |
| 246 | + $this->clean_languages_cache(); | |
| 247 | + $this->get_languages_list(); | |
| 248 | + | |
| 249 | + // Refresh rewrite rules. | |
| 250 | + flush_rewrite_rules(); | |
| 251 | + | |
| 242 | 252 | /** |
| 243 | - * Fires when a language is added | |
| 253 | + * Fires after a language is updated. | |
| 244 | 254 | * |
| 245 | 255 | * @since 1.9 |
| 256 | + * @since 3.2 Added $lang parameter. | |
| 246 | 257 | * |
| 247 | - * @param array $args arguments used to modify the language | |
| 258 | + * @param array $args { | |
| 259 | + * Arguments used to modify the language. @see PLL_Admin_Model::update_language(). | |
| 260 | + * | |
| 261 | + * @type string $name Language name (used only for display). | |
| 262 | + * @type string $slug Language code (ideally 2-letters ISO 639-1 language code). | |
| 263 | + * @type string $locale WordPress locale. | |
| 264 | + * @type int $rtl 1 if rtl language, 0 otherwise. | |
| 265 | + * @type int $term_group Language order when displayed. | |
| 266 | + * @type string $no_default_cat Optional, if set, no default category has been created for this language. | |
| 267 | + * @type string $flag Optional, country code, @see flags.php. | |
| 268 | + * } | |
| 269 | + * @param PLL_Language $lang Previous value of the language being edited. | |
| 248 | 270 | */ |
| 249 | - do_action( 'pll_update_language', $args ); | |
| 271 | + do_action( 'pll_update_language', $args, $lang ); | |
| 250 | 272 | |
| 251 | - $this->clean_languages_cache(); | |
| 252 | - flush_rewrite_rules(); // Refresh rewrite rules | |
| 253 | 273 | return true; |
| 254 | 274 | } |
| 255 | 275 | |
| 256 | 276 | /** |
| 257 | - * Validates data entered when creating or updating a language | |
| 277 | + * Builds the language metas into an array and serializes it, to be stored in the term description. | |
| 258 | 278 | * |
| 259 | - * @see PLL_Admin_Model::add_language | |
| 279 | + * @since 3.4 | |
| 260 | 280 | * |
| 281 | + * @param array $args { | |
| 282 | + * @type string $name Language name (used only for display). | |
| 283 | + * @type string $slug Language code (ideally 2-letters ISO 639-1 language code). | |
| 284 | + * @type string $locale WordPress locale. If something wrong is used for the locale, the .mo files will not be | |
| 285 | + * loaded... | |
| 286 | + * @type int $rtl 1 if rtl language, 0 otherwise. | |
| 287 | + * @type int $term_group Language order when displayed. | |
| 288 | + * @type int $lang_id Optional, ID of the language to modify. An empty value means the language is being | |
| 289 | + * created. | |
| 290 | + * @type string $flag Optional, country code, {@see settings/flags.php}. | |
| 291 | + * } | |
| 292 | + * @return string The serialized description array updated. | |
| 293 | + */ | |
| 294 | + protected function build_language_metas( array $args ) { | |
| 295 | + if ( ! empty( $args['lang_id'] ) ) { | |
| 296 | + $language_term = get_term( (int) $args['lang_id'] ); | |
| 297 | + | |
| 298 | + if ( $language_term instanceof WP_Term ) { | |
| 299 | + $old_data = maybe_unserialize( $language_term->description ); | |
| 300 | + } | |
| 301 | + } | |
| 302 | + | |
| 303 | + if ( empty( $old_data ) || ! is_array( $old_data ) ) { | |
| 304 | + $old_data = array(); | |
| 305 | + } | |
| 306 | + | |
| 307 | + $new_data = array( | |
| 308 | + 'locale' => $args['locale'], | |
| 309 | + 'rtl' => ! empty( $args['rtl'] ) ? 1 : 0, | |
| 310 | + 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'], | |
| 311 | + ); | |
| 312 | + | |
| 313 | + /** | |
| 314 | + * Allow to add data to store for a language. | |
| 315 | + * `$locale`, `$rtl`, and `$flag_code` cannot be overwritten. | |
| 316 | + * | |
| 317 | + * @since 3.4 | |
| 318 | + * | |
| 319 | + * @param mixed[] $add_data Data to add. | |
| 320 | + * @param mixed[] $args { | |
| 321 | + * Arguments used to create the language. | |
| 322 | + * | |
| 323 | + * @type string $name Language name (used only for display). | |
| 324 | + * @type string $slug Language code (ideally 2-letters ISO 639-1 language code). | |
| 325 | + * @type string $locale WordPress locale. If something wrong is used for the locale, the .mo files will | |
| 326 | + * not be loaded... | |
| 327 | + * @type int $rtl 1 if rtl language, 0 otherwise. | |
| 328 | + * @type int $term_group Language order when displayed. | |
| 329 | + * @type int $lang_id Optional, ID of the language to modify. An empty value means the language is | |
| 330 | + * being created. | |
| 331 | + * @type string $flag Optional, country code, {@see settings/flags.php}. | |
| 332 | + * } | |
| 333 | + * @param mixed[] $new_data New data. | |
| 334 | + * @param mixed[] $old_data { | |
| 335 | + * Original data. Contains at least the following: | |
| 336 | + * | |
| 337 | + * @type string $locale WordPress locale. | |
| 338 | + * @type int $rtl 1 if rtl language, 0 otherwise. | |
| 339 | + * @type string $flag_code Country code. | |
| 340 | + * } | |
| 341 | + */ | |
| 342 | + $add_data = apply_filters( 'pll_language_metas', array(), $args, $new_data, $old_data ); | |
| 343 | + // Don't allow to overwrite `$locale`, `$rtl`, and `$flag_code`. | |
| 344 | + $new_data = array_merge( $old_data, $add_data, $new_data ); | |
| 345 | + | |
| 346 | + /** @var non-empty-string $serialized maybe_serialize() cannot return anything else than a string when fed by an array. */ | |
| 347 | + $serialized = maybe_serialize( $new_data ); | |
| 348 | + return $serialized; | |
| 349 | + } | |
| 350 | + | |
| 351 | + /** | |
| 352 | + * Validates data entered when creating or updating a language. | |
| 353 | + * | |
| 354 | + * @see PLL_Admin_Model::add_language(). | |
| 355 | + * | |
| 261 | 356 | * @since 0.4 |
| 262 | 357 | * |
| 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 | |
| 358 | + * @param array $args Parameters of {@see PLL_Admin_Model::add_language() or @see PLL_Admin_Model::update_language()}. | |
| 359 | + * @param PLL_Language|null $lang Optional the language currently updated, the language is created if not set. | |
| 360 | + * @return WP_Error | |
| 266 | 361 | */ |
| 267 | 362 | protected function validate_lang( $args, $lang = null ) { |
| 268 | 363 | $errors = new WP_Error(); |
| 269 | 364 | |
| @@ -278,9 +373,9 @@ | ||
| 278 | 373 | } |
| 279 | 374 | |
| 280 | 375 | // Validate slug is unique |
| 281 | 376 | foreach ( $this->get_languages_list() as $language ) { |
| 282 | - if ( $language->slug === $args['slug'] && ( null === $lang || ( isset( $lang ) && $lang->term_id != $language->term_id ) ) ) { | |
| 377 | + if ( $language->slug === $args['slug'] && ( null === $lang || $lang->term_id !== $language->term_id ) ) { | |
| 283 | 378 | $errors->add( 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) ); |
| 284 | 379 | } |
| 285 | 380 | } |
| 286 | 381 | |
| @@ -290,9 +385,9 @@ | ||
| 290 | 385 | $errors->add( 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) ); |
| 291 | 386 | } |
| 292 | 387 | |
| 293 | 388 | // Validate flag |
| 294 | - if ( ! empty( $args['flag'] ) && ! file_exists( POLYLANG_DIR . '/flags/' . $args['flag'] . '.png' ) ) { | |
| 389 | + if ( ! empty( $args['flag'] ) && ! is_readable( POLYLANG_DIR . '/flags/' . $args['flag'] . '.png' ) ) { | |
| 295 | 390 | $flag = PLL_Language::get_flag_informations( $args['flag'] ); |
| 296 | 391 | |
| 297 | 392 | if ( ! empty( $flag['url'] ) ) { |
| 298 | 393 | $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'] ) ); |
| @@ -306,226 +401,67 @@ | ||
| 306 | 401 | return $errors; |
| 307 | 402 | } |
| 308 | 403 | |
| 309 | 404 | /** |
| 310 | - * Used to set the language of posts or terms in mass | |
| 405 | + * Updates the translations when a language slug has been modified in settings | |
| 406 | + * or deletes them when a language is removed. | |
| 311 | 407 | * |
| 312 | - * @since 1.2 | |
| 313 | - * | |
| 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 | |
| 317 | - */ | |
| 318 | - public function set_language_in_mass( $type, $ids, $lang ) { | |
| 319 | - global $wpdb; | |
| 320 | - | |
| 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(); | |
| 325 | - | |
| 326 | - foreach ( $ids as $id ) { | |
| 327 | - $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id ); | |
| 328 | - } | |
| 329 | - | |
| 330 | - 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 ) ) ); | |
| 333 | - $lang->update_count(); // Updating term count is mandatory ( thanks to AndyDeGroo ) | |
| 334 | - } | |
| 335 | - | |
| 336 | - if ( 'term' === $type ) { | |
| 337 | - clean_term_cache( $ids, 'term_language' ); | |
| 338 | - $translations = array(); | |
| 339 | - | |
| 340 | - foreach ( $ids as $id ) { | |
| 341 | - $translations[] = array( $lang->slug => $id ); | |
| 342 | - } | |
| 343 | - | |
| 344 | - if ( ! empty( $translations ) ) { | |
| 345 | - $this->set_translation_in_mass( 'term', $translations ); | |
| 346 | - } | |
| 347 | - } else { | |
| 348 | - clean_term_cache( $ids, 'language' ); | |
| 349 | - } | |
| 350 | - } | |
| 351 | - | |
| 352 | - /** | |
| 353 | - * Used to create a translations groups in mass | |
| 354 | - * | |
| 355 | - * @since 1.6.3 | |
| 356 | - * | |
| 357 | - * @param string $type either 'post' or 'term' | |
| 358 | - * @param array $translations array of translations arrays | |
| 359 | - */ | |
| 360 | - public function set_translation_in_mass( $type, $translations ) { | |
| 361 | - global $wpdb; | |
| 362 | - | |
| 363 | - $taxonomy = $type . '_translations'; | |
| 364 | - $terms = array(); | |
| 365 | - $slugs = array(); | |
| 366 | - $description = array(); | |
| 367 | - $count = array(); | |
| 368 | - | |
| 369 | - foreach ( $translations as $t ) { | |
| 370 | - $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 ); | |
| 374 | - $count[ $term ] = count( $t ); | |
| 375 | - } | |
| 376 | - | |
| 377 | - // Insert terms | |
| 378 | - if ( ! empty( $terms ) ) { | |
| 379 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 380 | - $wpdb->query( "INSERT INTO {$wpdb->terms} ( slug, name ) VALUES " . implode( ',', array_unique( $terms ) ) ); | |
| 381 | - } | |
| 382 | - | |
| 383 | - // 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(); | |
| 388 | - | |
| 389 | - // Prepare terms taxonomy relationship | |
| 390 | - 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 ] ); | |
| 393 | - } | |
| 394 | - | |
| 395 | - // Insert term_taxonomy | |
| 396 | - 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 ) ) ); | |
| 399 | - } | |
| 400 | - | |
| 401 | - // Get all terms with term_taxonomy_id | |
| 402 | - $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); | |
| 403 | - $trs = array(); | |
| 404 | - | |
| 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 ); | |
| 412 | - } | |
| 413 | - } | |
| 414 | - } | |
| 415 | - } | |
| 416 | - | |
| 417 | - // Insert term_relationships | |
| 418 | - 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 ) ); | |
| 421 | - $trs = array_unique( $trs ); | |
| 422 | - } | |
| 423 | - | |
| 424 | - clean_term_cache( $term_ids, $taxonomy ); | |
| 425 | - } | |
| 426 | - | |
| 427 | - /** | |
| 428 | - * Returns untranslated posts and terms ids ( used in settings ) | |
| 429 | - * | |
| 430 | - * @since 0.9 | |
| 431 | - * @since 2.2.6 Add the $limit argument | |
| 432 | - * | |
| 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 | |
| 435 | - */ | |
| 436 | - public function get_objects_with_no_lang( $limit = -1 ) { | |
| 437 | - global $wpdb; | |
| 438 | - | |
| 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 ); | |
| 449 | - | |
| 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 | - /** | |
| 484 | - * Filter the list of untranslated posts ids and terms ids | |
| 485 | - * | |
| 486 | - * @since 0.9 | |
| 487 | - * | |
| 488 | - * @param bool|array $objects false if no ids found, list of post and/or term ids otherwise | |
| 489 | - */ | |
| 490 | - return apply_filters( 'pll_get_objects_with_no_lang', empty( $posts ) && empty( $terms ) ? false : array( 'posts' => $posts, 'terms' => $terms ) ); | |
| 491 | - } | |
| 492 | - | |
| 493 | - /** | |
| 494 | - * Used to delete translations or update the translations when a language slug has been modified in settings | |
| 495 | - * | |
| 496 | 408 | * @since 0.5 |
| 497 | 409 | * |
| 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 | |
| 410 | + * @param string $old_slug The old language slug. | |
| 411 | + * @param string $new_slug Optional, the new language slug, if not set it means that the language has been deleted. | |
| 412 | + * @return void | |
| 500 | 413 | */ |
| 501 | 414 | public function update_translations( $old_slug, $new_slug = '' ) { |
| 502 | 415 | global $wpdb; |
| 503 | 416 | |
| 504 | - $terms = get_terms( array( 'post_translations', 'term_translations' ) ); | |
| 505 | 417 | $term_ids = array(); |
| 506 | 418 | $dr = array(); |
| 507 | 419 | $dt = array(); |
| 508 | 420 | $ut = array(); |
| 509 | 421 | |
| 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 ] ); | |
| 422 | + $taxonomies = $this->translatable_objects->get_taxonomy_names( array( 'translations' ) ); | |
| 423 | + $terms = get_terms( array( 'taxonomy' => $taxonomies ) ); | |
| 521 | 424 | |
| 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; | |
| 425 | + if ( is_array( $terms ) ) { | |
| 426 | + foreach ( $terms as $term ) { | |
| 427 | + $term_ids[ $term->taxonomy ][] = $term->term_id; | |
| 428 | + $tr = maybe_unserialize( $term->description ); | |
| 429 | + | |
| 430 | + /** | |
| 431 | + * Filters the unserialized translation group description before it is | |
| 432 | + * updated when a language is deleted or a language slug is changed. | |
| 433 | + * | |
| 434 | + * @since 3.2 | |
| 435 | + * | |
| 436 | + * @param (int|string[])[] $tr { | |
| 437 | + * List of translations with lang codes as array keys and IDs as array values. | |
| 438 | + * Also in this array: | |
| 439 | + * | |
| 440 | + * @type string[] $sync List of synchronized translations with lang codes as array keys and array values. | |
| 441 | + * } | |
| 442 | + * @param string $old_slug The old language slug. | |
| 443 | + * @param string $new_slug The new language slug. | |
| 444 | + * @param WP_Term $term The term containing the post or term translation group. | |
| 445 | + */ | |
| 446 | + $tr = apply_filters( 'update_translation_group', $tr, $old_slug, $new_slug, $term ); | |
| 447 | + | |
| 448 | + if ( ! empty( $tr[ $old_slug ] ) ) { | |
| 449 | + if ( $new_slug ) { | |
| 450 | + $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete | |
| 451 | + } else { | |
| 452 | + $dr['id'][] = (int) $tr[ $old_slug ]; | |
| 453 | + $dr['tt'][] = (int) $term->term_taxonomy_id; | |
| 454 | + } | |
| 455 | + unset( $tr[ $old_slug ] ); | |
| 456 | + | |
| 457 | + if ( empty( $tr ) || 1 == count( $tr ) ) { | |
| 458 | + $dt['t'][] = (int) $term->term_id; | |
| 459 | + $dt['tt'][] = (int) $term->term_taxonomy_id; | |
| 460 | + } else { | |
| 461 | + $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, maybe_serialize( $tr ) ); | |
| 462 | + $ut['in'][] = (int) $term->term_id; | |
| 463 | + } | |
| 528 | 464 | } |
| 529 | 465 | } |
| 530 | 466 | } |
| 531 | 467 | |
| @@ -565,13 +501,14 @@ | ||
| 565 | 501 | } |
| 566 | 502 | |
| 567 | 503 | /** |
| 568 | 504 | * Updates the default language |
| 569 | - * taking care to update the default category & the nav menu locations | |
| 505 | + * taking care to update the default category & the nav menu locations. | |
| 570 | 506 | * |
| 571 | 507 | * @since 1.8 |
| 572 | 508 | * |
| 573 | - * @param string $slug new language slug | |
| 509 | + * @param string $slug New language slug. | |
| 510 | + * @return void | |
| 574 | 511 | */ |
| 575 | 512 | public function update_default_lang( $slug ) { |
| 576 | 513 | // The nav menus stored in theme locations should be in the default language |
| 577 | 514 | $theme = get_stylesheet(); |
| @@ -583,13 +520,16 @@ | ||
| 583 | 520 | } |
| 584 | 521 | set_theme_mod( 'nav_menu_locations', $menus ); |
| 585 | 522 | } |
| 586 | 523 | |
| 587 | - // The default category should be in the default language | |
| 588 | - $default_cats = $this->term->get_translations( get_option( 'default_category' ) ); | |
| 589 | - if ( isset( $default_cats[ $slug ] ) ) { | |
| 590 | - update_option( 'default_category', $default_cats[ $slug ] ); | |
| 591 | - } | |
| 524 | + /** | |
| 525 | + * Fires when a default language is updated. | |
| 526 | + * | |
| 527 | + * @since 3.1 | |
| 528 | + * | |
| 529 | + * @param string $slug Slug. | |
| 530 | + */ | |
| 531 | + do_action( 'pll_update_default_lang', $slug ); | |
| 592 | 532 | |
| 593 | 533 | // Update options |
| 594 | 534 | $this->options['default_lang'] = $slug; |
| 595 | 535 | update_option( 'polylang', $this->options ); |