| @@ -60,8 +60,10 @@ | ||
| 60 | 60 | '_pll' => true, |
| 61 | 61 | 'update_count_callback' => '_update_generic_term_count', // Count *all* objects to correctly detect unused terms. |
| 62 | 62 | ) |
| 63 | 63 | ); |
| 64 | + | |
| 65 | + $this->add_sanitization_hooks( $this->tax_translations ); | |
| 64 | 66 | } |
| 65 | 67 | |
| 66 | 68 | /** |
| 67 | 69 | * Returns the translations group taxonomy name. |
| @@ -90,9 +92,9 @@ | ||
| 90 | 92 | if ( ! parent::set_language( $id, $lang ) ) { |
| 91 | 93 | return false; |
| 92 | 94 | } |
| 93 | 95 | |
| 94 | - $id = $this->sanitize_int_id( $id ); | |
| 96 | + $id = pll_sanitize_id( $id ); | |
| 95 | 97 | |
| 96 | 98 | $translations = $this->get_translations( $id ); |
| 97 | 99 | |
| 98 | 100 | // Don't create translation groups with only 1 value. |
| @@ -115,9 +117,9 @@ | ||
| 115 | 117 | * |
| 116 | 118 | * @phpstan-return array<non-empty-string, positive-int> |
| 117 | 119 | */ |
| 118 | 120 | public function get_translations_from_term_id( $term_id ) { |
| 119 | - $term_id = $this->sanitize_int_id( $term_id ); | |
| 121 | + $term_id = pll_sanitize_id( $term_id ); | |
| 120 | 122 | |
| 121 | 123 | if ( empty( $term_id ) ) { |
| 122 | 124 | return array(); |
| 123 | 125 | } |
| @@ -146,9 +148,9 @@ | ||
| 146 | 148 | * |
| 147 | 149 | * @phpstan-return array<non-empty-string, positive-int> |
| 148 | 150 | */ |
| 149 | 151 | public function save_translations( $id, array $translations = array() ) { |
| 150 | - $id = $this->sanitize_int_id( $id ); | |
| 152 | + $id = pll_sanitize_id( $id ); | |
| 151 | 153 | |
| 152 | 154 | if ( empty( $id ) ) { |
| 153 | 155 | return array(); |
| 154 | 156 | } |
| @@ -181,9 +183,9 @@ | ||
| 181 | 183 | |
| 182 | 184 | if ( empty( $term ) ) { |
| 183 | 185 | // Create a new term if necessary. |
| 184 | 186 | $group = uniqid( 'pll_' ); |
| 185 | - wp_insert_term( $group, $this->tax_translations, array( 'description' => maybe_serialize( $translations ) ) ); | |
| 187 | + wp_insert_term( $group, $this->tax_translations, array( 'description' => (string) maybe_serialize( $translations ) ) ); | |
| 186 | 188 | } else { |
| 187 | 189 | // Take care not to overwrite extra data stored in the description field, if any. |
| 188 | 190 | $group = (int) $term->term_id; |
| 189 | 191 | $descr = maybe_unserialize( $term->description ); |
| @@ -188,9 +190,9 @@ | ||
| 188 | 190 | $group = (int) $term->term_id; |
| 189 | 191 | $descr = maybe_unserialize( $term->description ); |
| 190 | 192 | $descr = is_array( $descr ) ? array_diff_key( $descr, $old_translations ) : array(); // Remove old translations. |
| 191 | 193 | $descr = array_merge( $descr, $translations ); // Add new one. |
| 192 | - wp_update_term( $group, $this->tax_translations, array( 'description' => maybe_serialize( $descr ) ) ); | |
| 194 | + wp_update_term( $group, $this->tax_translations, array( 'description' => (string) maybe_serialize( $descr ) ) ); | |
| 193 | 195 | } |
| 194 | 196 | |
| 195 | 197 | // Link all translations to the new term. |
| 196 | 198 | foreach ( $translations as $p ) { |
| @@ -219,9 +221,9 @@ | ||
| 219 | 221 | * @param int $id Object ID. |
| 220 | 222 | * @return void |
| 221 | 223 | */ |
| 222 | 224 | public function delete_translation( $id ) { |
| 223 | - $id = $this->sanitize_int_id( $id ); | |
| 225 | + $id = pll_sanitize_id( $id ); | |
| 224 | 226 | |
| 225 | 227 | if ( empty( $id ) ) { |
| 226 | 228 | return; |
| 227 | 229 | } |
| @@ -234,19 +236,23 @@ | ||
| 234 | 236 | |
| 235 | 237 | $descr = maybe_unserialize( $term->description ); |
| 236 | 238 | |
| 237 | 239 | if ( ! empty( $descr ) && is_array( $descr ) ) { |
| 238 | - $slug = array_search( $id, $this->get_translations( $id ) ); // In case some plugin stores the same value with different key. | |
| 239 | - | |
| 240 | - if ( false !== $slug ) { | |
| 241 | - unset( $descr[ $slug ] ); | |
| 242 | - } | |
| 240 | + /* | |
| 241 | + * Search the ID to remove only among our language keys | |
| 242 | + * in case some plugin stores the same value with different key. | |
| 243 | + * Remove all keys with this ID as `get_translations()` may return | |
| 244 | + * temporarily 2 languages for the same ID (old and new language) | |
| 245 | + * when `set_language()` is called. | |
| 246 | + */ | |
| 247 | + $slugs = array_keys( $this->get_translations( $id ), $id, true ); | |
| 248 | + $descr = array_diff_key( $descr, array_flip( $slugs ) ); | |
| 243 | 249 | } |
| 244 | 250 | |
| 245 | 251 | if ( empty( $descr ) || ! is_array( $descr ) ) { |
| 246 | 252 | wp_delete_term( (int) $term->term_id, $this->tax_translations ); |
| 247 | 253 | } else { |
| 248 | - wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $descr ) ) ); | |
| 254 | + wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => (string) maybe_serialize( $descr ) ) ); | |
| 249 | 255 | } |
| 250 | 256 | } |
| 251 | 257 | |
| 252 | 258 | /** |
| @@ -259,9 +265,9 @@ | ||
| 259 | 265 | * |
| 260 | 266 | * @phpstan-return array<non-empty-string, positive-int> |
| 261 | 267 | */ |
| 262 | 268 | public function get_translations( $id ) { |
| 263 | - $id = $this->sanitize_int_id( $id ); | |
| 269 | + $id = pll_sanitize_id( $id ); | |
| 264 | 270 | |
| 265 | 271 | if ( empty( $id ) ) { |
| 266 | 272 | return array(); |
| 267 | 273 | } |
| @@ -280,9 +286,9 @@ | ||
| 280 | 286 | * |
| 281 | 287 | * @phpstan-return array<non-empty-string, positive-int> |
| 282 | 288 | */ |
| 283 | 289 | public function get_raw_translations( $id ) { |
| 284 | - $id = $this->sanitize_int_id( $id ); | |
| 290 | + $id = pll_sanitize_id( $id ); | |
| 285 | 291 | |
| 286 | 292 | if ( empty( $id ) ) { |
| 287 | 293 | return array(); |
| 288 | 294 | } |
| @@ -325,9 +331,9 @@ | ||
| 325 | 331 | * |
| 326 | 332 | * @phpstan-return int<0, max> |
| 327 | 333 | */ |
| 328 | 334 | public function get( $id, $lang ) { |
| 329 | - $id = $this->sanitize_int_id( $id ); | |
| 335 | + $id = pll_sanitize_id( $id ); | |
| 330 | 336 | |
| 331 | 337 | if ( empty( $id ) ) { |
| 332 | 338 | return 0; |
| 333 | 339 | } |
| @@ -355,9 +361,9 @@ | ||
| 355 | 361 | * @param int $id Object ID. |
| 356 | 362 | * @return bool |
| 357 | 363 | */ |
| 358 | 364 | public function current_user_can_synchronize( $id ) { |
| 359 | - $id = $this->sanitize_int_id( $id ); | |
| 365 | + $id = pll_sanitize_id( $id ); | |
| 360 | 366 | |
| 361 | 367 | if ( empty( $id ) ) { |
| 362 | 368 | return false; |
| 363 | 369 | } |
| @@ -404,11 +410,9 @@ | ||
| 404 | 410 | * |
| 405 | 411 | * @phpstan-param array<non-empty-string, positive-int> $translations |
| 406 | 412 | */ |
| 407 | 413 | protected function should_update_translation_group( $id, $translations ) { |
| 408 | - // Don't do anything if no translations have been added to the group. | |
| 409 | - $old_translations = $this->get_translations( $id ); // Includes at least $id itself. | |
| 410 | - return ! empty( array_diff_assoc( $translations, $old_translations ) ); | |
| 414 | + return count( $translations ) > 1; | |
| 411 | 415 | } |
| 412 | 416 | |
| 413 | 417 | /** |
| 414 | 418 | * Returns an array of valid translations for multiple objects. |
| @@ -498,9 +502,9 @@ | ||
| 498 | 502 | ); |
| 499 | 503 | |
| 500 | 504 | // Make sure values are clean before working with them. |
| 501 | 505 | /** @phpstan-var array<non-empty-string, positive-int> $translations */ |
| 502 | - $translations = $this->sanitize_int_ids_list( $translations ); | |
| 506 | + $translations = pll_sanitize_ids( $translations ); | |
| 503 | 507 | |
| 504 | 508 | if ( 'save' === $context ) { |
| 505 | 509 | /** |
| 506 | 510 | * Check that the translated objects are in the right language. |
| @@ -519,9 +523,9 @@ | ||
| 519 | 523 | |
| 520 | 524 | $translations = $valid_translations; |
| 521 | 525 | } |
| 522 | 526 | |
| 523 | - $id = $this->sanitize_int_id( $id ); | |
| 527 | + $id = pll_sanitize_id( $id ); | |
| 524 | 528 | |
| 525 | 529 | if ( empty( $id ) ) { |
| 526 | 530 | return $translations; |
| 527 | 531 | } |