| @@ -40,9 +40,9 @@ | ||
| 40 | 40 | * Constructor |
| 41 | 41 | * |
| 42 | 42 | * @since 1.2 |
| 43 | 43 | * |
| 44 | - * @param object $polylang | |
| 44 | + * @param object $polylang The Polylang object. | |
| 45 | 45 | */ |
| 46 | 46 | public function __construct( &$polylang ) { |
| 47 | 47 | $this->model = &$polylang->model; |
| 48 | 48 | $this->options = &$polylang->options; |
| @@ -167,9 +167,9 @@ | ||
| 167 | 167 | |
| 168 | 168 | $tr_arr = $postarr; |
| 169 | 169 | unset( $tr_arr['post_parent'] ); |
| 170 | 170 | |
| 171 | - // Do not udpate the translation parent if the user set a parent with no translation. | |
| 171 | + // Do not update the translation parent if the user set a parent with no translation. | |
| 172 | 172 | if ( isset( $postarr['post_parent'] ) ) { |
| 173 | 173 | $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0; |
| 174 | 174 | if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) { |
| 175 | 175 | $tr_arr['post_parent'] = $post_parent; |
| @@ -187,9 +187,9 @@ | ||
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
| 191 | - * Synchronize term parent in translations | |
| 191 | + * Synchronize term parent in translations. | |
| 192 | 192 | * Calling clean_term_cache *after* this is mandatory otherwise the $taxonomy_children option is not correctly updated |
| 193 | 193 | * |
| 194 | 194 | * @since 2.3 |
| 195 | 195 | * |
| @@ -200,30 +200,40 @@ | ||
| 200 | 200 | */ |
| 201 | 201 | public function sync_term_parent( $term_id, $tt_id, $taxonomy ) { |
| 202 | 202 | global $wpdb; |
| 203 | 203 | |
| 204 | - if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) { | |
| 205 | - $term = get_term( $term_id ); | |
| 204 | + if ( ! is_taxonomy_hierarchical( $taxonomy ) || ! $this->model->is_translated_taxonomy( $taxonomy ) ) { | |
| 205 | + return; | |
| 206 | + } | |
| 206 | 207 | |
| 207 | - if ( $term instanceof WP_Term ) { | |
| 208 | - $translations = $this->model->term->get_translations( $term_id ); | |
| 208 | + $term = get_term( $term_id ); | |
| 209 | + if ( ! $term instanceof WP_Term ) { | |
| 210 | + return; | |
| 211 | + } | |
| 209 | 212 | |
| 210 | - foreach ( $translations as $lang => $tr_id ) { | |
| 211 | - if ( ! empty( $tr_id ) && $tr_id !== $term_id ) { | |
| 212 | - $tr_parent = $this->model->term->get_translation( $term->parent, $lang ); | |
| 213 | - $tr_term = get_term( (int) $tr_id, $taxonomy ); | |
| 213 | + $translations = $this->model->term->get_translations( $term_id ); | |
| 214 | 214 | |
| 215 | - if ( $tr_term instanceof WP_Term ) { | |
| 216 | - $wpdb->update( | |
| 217 | - $wpdb->term_taxonomy, | |
| 218 | - array( 'parent' => $tr_parent ? $tr_parent : 0 ), | |
| 219 | - array( 'term_taxonomy_id' => $tr_term->term_taxonomy_id ) | |
| 220 | - ); | |
| 215 | + foreach ( $translations as $lang => $tr_id ) { | |
| 216 | + if ( $tr_id === $term_id ) { | |
| 217 | + continue; | |
| 218 | + } | |
| 219 | + | |
| 220 | + $tr_parent = $this->model->term->get_translation( $term->parent, $lang ); | |
| 221 | + $tr_term = get_term( (int) $tr_id, $taxonomy ); | |
| 221 | 222 | |
| 222 | - clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9. | |
| 223 | - } | |
| 224 | - } | |
| 225 | - } | |
| 223 | + if ( str_starts_with( current_filter(), 'created_' ) && 0 === $tr_parent ) { | |
| 224 | + // Do not remove the existing hierarchy of translations when creating new term without parent. | |
| 225 | + continue; | |
| 226 | + } | |
| 227 | + | |
| 228 | + if ( $tr_term instanceof WP_Term && ! ( $term->parent && empty( $tr_parent ) ) ) { | |
| 229 | + $wpdb->update( | |
| 230 | + $wpdb->term_taxonomy, | |
| 231 | + array( 'parent' => $tr_parent ?: 0 ), | |
| 232 | + array( 'term_taxonomy_id' => $tr_term->term_taxonomy_id ) | |
| 233 | + ); | |
| 234 | + | |
| 235 | + clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9. | |
| 226 | 236 | } |
| 227 | 237 | } |
| 228 | 238 | } |
| 229 | 239 | |