PluginProbe
Polylang / trunk
Polylang vtrunk
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 3.0.3 All 233 releases
← All changes | src/translated-term.php +42 -9 3.8.8 → trunk View file →
@@ -106,9 +106,9 @@
106 106 if ( ! parent::set_language( $id, $lang ) ) {
107 107 return false;
108 108 }
109 109
110 - $id = $this->sanitize_int_id( $id );
110 + $id = pll_sanitize_id( $id );
111 111
112 112 // Add translation group for correct WXR export.
113 113 $translations = $this->get_translations( $id );
114 114
@@ -154,9 +154,9 @@
154 154 */
155 155 public function delete_translation( $id ) {
156 156 global $wpdb;
157 157
158 - $id = $this->sanitize_int_id( $id );
158 + $id = pll_sanitize_id( $id );
159 159
160 160 if ( empty( $id ) ) {
161 161 return;
162 162 }
@@ -176,9 +176,9 @@
176 176
177 177 // Always keep a group for terms to allow relationships remap when importing from a WXR file.
178 178 $group = uniqid( 'pll_' );
179 179 $translations = array( $slug => $id );
180 - wp_insert_term( $group, $this->tax_translations, array( 'description' => maybe_serialize( $translations ) ) );
180 + wp_insert_term( $group, $this->tax_translations, array( 'description' => (string) maybe_serialize( $translations ) ) );
181 181 wp_set_object_terms( $id, $group, $this->tax_translations );
182 182 }
183 183
184 184 /**
@@ -262,9 +262,9 @@
262 262 *
263 263 * @phpstan-param array<positive-int> $ids
264 264 */
265 265 public function clean_term_cache( $ids ) {
266 - clean_object_term_cache( $this->sanitize_int_ids_list( $ids ), 'term' );
266 + clean_object_term_cache( pll_sanitize_ids( $ids ), 'term' );
267 267 }
268 268
269 269 /**
270 270 * Tells whether a translation term must be updated.
@@ -278,16 +278,14 @@
278 278 *
279 279 * @phpstan-param array<non-empty-string, positive-int> $translations
280 280 */
281 281 protected function should_update_translation_group( $id, $translations ) {
282 - // Don't do anything if no translations have been added to the group.
283 - $old_translations = $this->get_translations( $id );
284 - if ( count( $translations ) > 1 && ! empty( array_diff_assoc( $translations, $old_translations ) ) ) {
282 + if ( count( $translations ) > 1 ) {
285 283 return true;
286 284 }
287 285
288 - // But we need a translation group for terms to allow relationships remap when importing from a WXR file
289 - $term = $this->get_object_term( $id, $this->tax_translations );
286 + $old_translations = $this->get_translations( $id );
287 + $term = $this->get_object_term( $id, $this->tax_translations );
290 288 return empty( $term ) || ! empty( array_diff_assoc( $translations, $old_translations ) );
291 289 }
292 290
293 291 /**
@@ -448,8 +446,43 @@
448 446 $this->save_translations( $term_id, $args['translations'] );
449 447 }
450 448
451 449 return $term;
450 + }
451 +
452 + /**
453 + * Gets a translated term by term field
454 + *
455 + * @since 3.9
456 + *
457 + * @param string $field The term field. Accepted values are 'term_id', 'slug', 'name', 'term_taxonomy_id'.
458 + * @param int|string $value Search for this term value.
459 + * @param PLL_Language $lang Language (object, slug, or term ID).
460 + * @param string $taxonomy Taxonomy name. Optional if $field is 'term_id'.
461 + * @return int|string The $field of the translated term. Empty if not found.
462 + */
463 + public function get_by( string $field, $value, PLL_Language $lang, string $taxonomy = '' ) {
464 + if ( 'term_id' === $field ) {
465 + return $this->get( (int) $value, $lang );
466 + }
467 +
468 + $empty = 'term_taxonomy_id' === $field ? 0 : '';
469 +
470 + $terms = get_terms( array( 'taxonomy' => $taxonomy, $field => $value, 'hide_empty' => false, 'lang' => '' ) );
471 +
472 + if ( empty( $terms ) || ! is_array( $terms ) ) {
473 + return $empty;
474 + }
475 +
476 + $t = reset( $terms );
477 + if ( ! $t instanceof WP_Term ) {
478 + return $empty;
479 + }
480 +
481 + $tr_id = $this->get( $t->term_id, $lang );
482 + $tr_term = get_term( $tr_id );
483 +
484 + return $tr_term instanceof WP_Term ? $tr_term->$field : $empty;
452 485 }
453 486
454 487 /**
455 488 * Toggles Polylang term slug filters management.