| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * A class to manage the sychronization of taxonomy terms across posts translations |
| 8 | 5 | * |
| @@ -22,10 +19,8 @@ | ||
| 22 | 19 | $this->options = &$polylang->options; |
| 23 | 20 | |
| 24 | 21 | add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 ); |
| 25 | 22 | add_action( 'pll_save_term', array( $this, 'create_term' ), 10, 3 ); |
| 26 | - add_action( 'pre_delete_term', array( $this, 'pre_delete_term' ) ); | |
| 27 | - add_action( 'delete_term', array( $this, 'delete_term' ) ); | |
| 28 | 23 | } |
| 29 | 24 | |
| 30 | 25 | /** |
| 31 | 26 | * Get the list of taxonomies to copy or to synchronize |
| @@ -103,43 +98,8 @@ | ||
| 103 | 98 | return $terms; // Empty $terms or untranslated taxonomy |
| 104 | 99 | } |
| 105 | 100 | |
| 106 | 101 | /** |
| 107 | - * Maybe copy taxonomy terms from one post to the other | |
| 108 | - * | |
| 109 | - * @since 2.6 | |
| 110 | - * | |
| 111 | - * @param int $object_id Source object ID. | |
| 112 | - * @param int $tr_id Target object ID. | |
| 113 | - * @param string $lang Target language. | |
| 114 | - * @param array $terms An array of object terms. | |
| 115 | - * @param string $taxonomy Taxonomy slug. | |
| 116 | - * @param bool $append Whether to append new terms to the old terms. | |
| 117 | - */ | |
| 118 | - protected function copy_object_terms( $object_id, $tr_id, $lang, $terms, $taxonomy, $append ) { | |
| 119 | - $to_copy = $this->get_taxonomies_to_copy( true, $object_id, $tr_id, $lang ); | |
| 120 | - | |
| 121 | - if ( in_array( $taxonomy, $to_copy ) ) { | |
| 122 | - $newterms = $this->maybe_translate_terms( $object_id, $terms, $taxonomy, $lang ); | |
| 123 | - | |
| 124 | - // For some reasons, the user may have untranslated terms in the translation. Don't forget them. | |
| 125 | - if ( $this->model->is_translated_taxonomy( $taxonomy ) ) { | |
| 126 | - $tr_terms = get_the_terms( $tr_id, $taxonomy ); | |
| 127 | - if ( is_array( $tr_terms ) ) { | |
| 128 | - foreach ( $tr_terms as $term ) { | |
| 129 | - if ( ! $this->model->term->get_translation( $term->term_id, $this->model->post->get_language( $object_id ) ) ) { | |
| 130 | - $newterms[] = (int) $term->term_id; | |
| 131 | - } | |
| 132 | - } | |
| 133 | - } | |
| 134 | - } | |
| 135 | - | |
| 136 | - wp_set_object_terms( $tr_id, $newterms, $taxonomy, $append ); | |
| 137 | - } | |
| 138 | - | |
| 139 | - } | |
| 140 | - | |
| 141 | - /** | |
| 142 | 102 | * When assigning terms to a post, assign translated terms to the translated posts (synchronisation) |
| 143 | 103 | * |
| 144 | 104 | * @since 2.3 |
| 145 | 105 | * |
| @@ -160,24 +120,26 @@ | ||
| 160 | 120 | $tr_ids = $this->model->post->get_translations( $object_id ); |
| 161 | 121 | |
| 162 | 122 | foreach ( $tr_ids as $lang => $tr_id ) { |
| 163 | 123 | if ( $tr_id !== $object_id ) { |
| 164 | - if ( $this->model->post->current_user_can_synchronize( $object_id ) ) { | |
| 165 | - $this->copy_object_terms( $object_id, $tr_id, $lang, $terms, $taxonomy, $append ); | |
| 166 | - } else { | |
| 167 | - // No permission to synchronize, so let's synchronize in reverse order | |
| 168 | - $orig_lang = array_search( $object_id, $tr_ids ); | |
| 169 | - $tr_terms = get_the_terms( $tr_id, $taxonomy ); | |
| 124 | + $to_copy = $this->get_taxonomies_to_copy( true, $object_id, $tr_id, $lang ); | |
| 170 | 125 | |
| 171 | - if ( false === $tr_terms ) { | |
| 172 | - $tr_terms = array(); | |
| 126 | + if ( in_array( $taxonomy, $to_copy ) ) { | |
| 127 | + $newterms = $this->maybe_translate_terms( $object_id, $terms, $taxonomy, $lang ); | |
| 128 | + | |
| 129 | + // For some reasons, the user may have untranslated terms in the translation. Don't forget them. | |
| 130 | + if ( $this->model->is_translated_taxonomy( $taxonomy ) ) { | |
| 131 | + $tr_terms = get_the_terms( $tr_id, $taxonomy ); | |
| 132 | + if ( is_array( $tr_terms ) ) { | |
| 133 | + foreach ( $tr_terms as $term ) { | |
| 134 | + if ( ! $this->model->term->get_translation( $term->term_id, $this->model->post->get_language( $object_id ) ) ) { | |
| 135 | + $newterms[] = (int) $term->term_id; | |
| 136 | + } | |
| 137 | + } | |
| 138 | + } | |
| 173 | 139 | } |
| 174 | 140 | |
| 175 | - if ( is_array( $tr_terms ) ) { | |
| 176 | - $tr_terms = wp_list_pluck( $tr_terms, 'term_id' ); | |
| 177 | - $this->copy_object_terms( $tr_id, $object_id, $orig_lang, $tr_terms, $taxonomy, $append ); | |
| 178 | - } | |
| 179 | - break; | |
| 141 | + wp_set_object_terms( $tr_id, $newterms, $taxonomy, $append ); | |
| 180 | 142 | } |
| 181 | 143 | } |
| 182 | 144 | } |
| 183 | 145 | |
| @@ -229,25 +191,23 @@ | ||
| 229 | 191 | */ |
| 230 | 192 | public function create_term( $term_id, $taxonomy, $translations ) { |
| 231 | 193 | if ( doing_action( 'create_term' ) && in_array( $taxonomy, $this->get_taxonomies_to_copy( true ) ) ) { |
| 232 | 194 | // Get all posts associated to the translated terms |
| 233 | - $tr_posts = get_posts( | |
| 234 | - array( | |
| 235 | - 'numberposts' => -1, | |
| 236 | - 'nopaging' => true, | |
| 237 | - 'post_type' => 'any', | |
| 238 | - 'post_status' => 'any', | |
| 239 | - 'fields' => 'ids', | |
| 240 | - 'tax_query' => array( | |
| 241 | - array( | |
| 242 | - 'taxonomy' => $taxonomy, | |
| 243 | - 'field' => 'id', | |
| 244 | - 'terms' => array_merge( array( $term_id ), array_values( $translations ) ), | |
| 245 | - 'include_children' => false, | |
| 246 | - ), | |
| 195 | + $tr_posts = get_posts( array( | |
| 196 | + 'numberposts' => -1, | |
| 197 | + 'nopaging' => true, | |
| 198 | + 'post_type' => 'any', | |
| 199 | + 'post_status' => 'any', | |
| 200 | + 'fields' => 'ids', | |
| 201 | + 'tax_query' => array( | |
| 202 | + array( | |
| 203 | + 'taxonomy' => $taxonomy, | |
| 204 | + 'field' => 'id', | |
| 205 | + 'terms' => array_merge( array( $term_id ), array_values( $translations ) ), | |
| 206 | + 'include_children' => false, | |
| 247 | 207 | ), |
| 248 | - ) | |
| 249 | - ); | |
| 208 | + ), | |
| 209 | + ) ); | |
| 250 | 210 | |
| 251 | 211 | $lang = $this->model->term->get_language( $term_id ); // Language of the created term |
| 252 | 212 | $posts = array(); |
| 253 | 213 | |
| @@ -261,30 +221,9 @@ | ||
| 261 | 221 | |
| 262 | 222 | $posts = array_unique( $posts ); |
| 263 | 223 | |
| 264 | 224 | foreach ( $posts as $post_id ) { |
| 265 | - if ( current_user_can( 'assign_term', $term_id ) ) { | |
| 266 | - wp_set_object_terms( $post_id, $term_id, $taxonomy, true ); | |
| 267 | - } | |
| 225 | + wp_set_object_terms( $post_id, $term_id, $taxonomy, true ); | |
| 268 | 226 | } |
| 269 | 227 | } |
| 270 | - } | |
| 271 | - | |
| 272 | - /** | |
| 273 | - * Deactivate the synchronization of terms before deleting a term | |
| 274 | - * to avoid translated terms to be removed from translated posts | |
| 275 | - * | |
| 276 | - * @since 2.3.2 | |
| 277 | - */ | |
| 278 | - public function pre_delete_term() { | |
| 279 | - remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 ); | |
| 280 | - } | |
| 281 | - | |
| 282 | - /** | |
| 283 | - * Re-activate the synchronization of terms after a term is deleted | |
| 284 | - * | |
| 285 | - * @since 2.3.2 | |
| 286 | - */ | |
| 287 | - public function delete_term() { | |
| 288 | - add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 ); | |
| 289 | 228 | } |
| 290 | 229 | } |