PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.10 3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 All 234 releases
← All changes | src/translated-object.php +23 -21 3.8.10 → trunk View file →
@@ -92,9 +92,9 @@
92 92 if ( ! parent::set_language( $id, $lang ) ) {
93 93 return false;
94 94 }
95 95
96 - $id = $this->sanitize_int_id( $id );
96 + $id = pll_sanitize_id( $id );
97 97
98 98 $translations = $this->get_translations( $id );
99 99
100 100 // Don't create translation groups with only 1 value.
@@ -117,9 +117,9 @@
117 117 *
118 118 * @phpstan-return array<non-empty-string, positive-int>
119 119 */
120 120 public function get_translations_from_term_id( $term_id ) {
121 - $term_id = $this->sanitize_int_id( $term_id );
121 + $term_id = pll_sanitize_id( $term_id );
122 122
123 123 if ( empty( $term_id ) ) {
124 124 return array();
125 125 }
@@ -148,9 +148,9 @@
148 148 *
149 149 * @phpstan-return array<non-empty-string, positive-int>
150 150 */
151 151 public function save_translations( $id, array $translations = array() ) {
152 - $id = $this->sanitize_int_id( $id );
152 + $id = pll_sanitize_id( $id );
153 153
154 154 if ( empty( $id ) ) {
155 155 return array();
156 156 }
@@ -183,9 +183,9 @@
183 183
184 184 if ( empty( $term ) ) {
185 185 // Create a new term if necessary.
186 186 $group = uniqid( 'pll_' );
187 - 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 ) ) );
188 188 } else {
189 189 // Take care not to overwrite extra data stored in the description field, if any.
190 190 $group = (int) $term->term_id;
191 191 $descr = maybe_unserialize( $term->description );
@@ -190,9 +190,9 @@
190 190 $group = (int) $term->term_id;
191 191 $descr = maybe_unserialize( $term->description );
192 192 $descr = is_array( $descr ) ? array_diff_key( $descr, $old_translations ) : array(); // Remove old translations.
193 193 $descr = array_merge( $descr, $translations ); // Add new one.
194 - 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 ) ) );
195 195 }
196 196
197 197 // Link all translations to the new term.
198 198 foreach ( $translations as $p ) {
@@ -221,9 +221,9 @@
221 221 * @param int $id Object ID.
222 222 * @return void
223 223 */
224 224 public function delete_translation( $id ) {
225 - $id = $this->sanitize_int_id( $id );
225 + $id = pll_sanitize_id( $id );
226 226
227 227 if ( empty( $id ) ) {
228 228 return;
229 229 }
@@ -236,19 +236,23 @@
236 236
237 237 $descr = maybe_unserialize( $term->description );
238 238
239 239 if ( ! empty( $descr ) && is_array( $descr ) ) {
240 - $slug = array_search( $id, $this->get_translations( $id ) ); // In case some plugin stores the same value with different key.
241 -
242 - if ( false !== $slug ) {
243 - unset( $descr[ $slug ] );
244 - }
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 ) );
245 249 }
246 250
247 251 if ( empty( $descr ) || ! is_array( $descr ) ) {
248 252 wp_delete_term( (int) $term->term_id, $this->tax_translations );
249 253 } else {
250 - 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 ) ) );
251 255 }
252 256 }
253 257
254 258 /**
@@ -261,9 +265,9 @@
261 265 *
262 266 * @phpstan-return array<non-empty-string, positive-int>
263 267 */
264 268 public function get_translations( $id ) {
265 - $id = $this->sanitize_int_id( $id );
269 + $id = pll_sanitize_id( $id );
266 270
267 271 if ( empty( $id ) ) {
268 272 return array();
269 273 }
@@ -282,9 +286,9 @@
282 286 *
283 287 * @phpstan-return array<non-empty-string, positive-int>
284 288 */
285 289 public function get_raw_translations( $id ) {
286 - $id = $this->sanitize_int_id( $id );
290 + $id = pll_sanitize_id( $id );
287 291
288 292 if ( empty( $id ) ) {
289 293 return array();
290 294 }
@@ -327,9 +331,9 @@
327 331 *
328 332 * @phpstan-return int<0, max>
329 333 */
330 334 public function get( $id, $lang ) {
331 - $id = $this->sanitize_int_id( $id );
335 + $id = pll_sanitize_id( $id );
332 336
333 337 if ( empty( $id ) ) {
334 338 return 0;
335 339 }
@@ -357,9 +361,9 @@
357 361 * @param int $id Object ID.
358 362 * @return bool
359 363 */
360 364 public function current_user_can_synchronize( $id ) {
361 - $id = $this->sanitize_int_id( $id );
365 + $id = pll_sanitize_id( $id );
362 366
363 367 if ( empty( $id ) ) {
364 368 return false;
365 369 }
@@ -406,11 +410,9 @@
406 410 *
407 411 * @phpstan-param array<non-empty-string, positive-int> $translations
408 412 */
409 413 protected function should_update_translation_group( $id, $translations ) {
410 - // Don't do anything if no translations have been added to the group.
411 - $old_translations = $this->get_translations( $id ); // Includes at least $id itself.
412 - return ! empty( array_diff_assoc( $translations, $old_translations ) );
414 + return count( $translations ) > 1;
413 415 }
414 416
415 417 /**
416 418 * Returns an array of valid translations for multiple objects.
@@ -500,9 +502,9 @@
500 502 );
501 503
502 504 // Make sure values are clean before working with them.
503 505 /** @phpstan-var array<non-empty-string, positive-int> $translations */
504 - $translations = $this->sanitize_int_ids_list( $translations );
506 + $translations = pll_sanitize_ids( $translations );
505 507
506 508 if ( 'save' === $context ) {
507 509 /**
508 510 * Check that the translated objects are in the right language.
@@ -521,9 +523,9 @@
521 523
522 524 $translations = $valid_translations;
523 525 }
524 526
525 - $id = $this->sanitize_int_id( $id );
527 + $id = pll_sanitize_id( $id );
526 528
527 529 if ( empty( $id ) ) {
528 530 return $translations;
529 531 }