| @@ -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 | * |
| @@ -10,20 +7,8 @@ | ||
| 10 | 7 | */ |
| 11 | 8 | class PLL_Sync_Tax { |
| 12 | 9 | |
| 13 | 10 | /** |
| 14 | - * Stores the plugin options. | |
| 15 | - * | |
| 16 | - * @var array | |
| 17 | - */ | |
| 18 | - protected $options; | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * @var PLL_Model | |
| 22 | - */ | |
| 23 | - protected $model; | |
| 24 | - | |
| 25 | - /** | |
| 26 | 11 | * Constructor |
| 27 | 12 | * |
| 28 | 13 | * @since 2.3 |
| 29 | 14 | * |
| @@ -29,9 +14,9 @@ | ||
| 29 | 14 | * |
| 30 | 15 | * @param object $polylang |
| 31 | 16 | */ |
| 32 | 17 | public function __construct( &$polylang ) { |
| 33 | - $this->model = &$polylang->model; | |
| 18 | + $this->model = &$polylang->model; | |
| 34 | 19 | $this->options = &$polylang->options; |
| 35 | 20 | |
| 36 | 21 | add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 ); |
| 37 | 22 | add_action( 'pll_save_term', array( $this, 'create_term' ), 10, 3 ); |
| @@ -39,18 +24,18 @@ | ||
| 39 | 24 | add_action( 'delete_term', array( $this, 'delete_term' ) ); |
| 40 | 25 | } |
| 41 | 26 | |
| 42 | 27 | /** |
| 43 | - * Get the list of taxonomies to copy or to synchronize. | |
| 28 | + * Get the list of taxonomies to copy or to synchronize | |
| 44 | 29 | * |
| 45 | 30 | * @since 1.7 |
| 46 | 31 | * @since 2.1 The `$from`, `$to`, `$lang` parameters were added. |
| 47 | 32 | * |
| 48 | - * @param bool $sync True if it is synchronization, false if it is a copy. | |
| 49 | - * @param int $from Id of the post from which we copy informations, optional, defaults to null. | |
| 50 | - * @param int $to Id of the post to which we paste informations, optional, defaults to null. | |
| 51 | - * @param string $lang Language slug, optional, defaults to null. | |
| 52 | - * @return string[] List of taxonomy names. | |
| 33 | + * @param bool $sync True if it is synchronization, false if it is a copy | |
| 34 | + * @param int $from Id of the post from which we copy informations, optional, defaults to null | |
| 35 | + * @param int $to Id of the post to which we paste informations, optional, defaults to null | |
| 36 | + * @param string $lang Language slug, optional, defaults to null | |
| 37 | + * @return array List of taxonomy names | |
| 53 | 38 | */ |
| 54 | 39 | protected function get_taxonomies_to_copy( $sync, $from = null, $to = null, $lang = null ) { |
| 55 | 40 | $taxonomies = ! $sync || in_array( 'taxonomies', $this->options['sync'] ) ? $this->model->get_translated_taxonomies() : array(); |
| 56 | 41 | if ( ! $sync || in_array( 'post_format', $this->options['sync'] ) ) { |
| @@ -57,18 +42,18 @@ | ||
| 57 | 42 | $taxonomies[] = 'post_format'; |
| 58 | 43 | } |
| 59 | 44 | |
| 60 | 45 | /** |
| 61 | - * Filters the taxonomies to copy or synchronize. | |
| 46 | + * Filter the taxonomies to copy or synchronize | |
| 62 | 47 | * |
| 63 | 48 | * @since 1.7 |
| 64 | 49 | * @since 2.1 The `$from`, `$to`, `$lang` parameters were added. |
| 65 | 50 | * |
| 66 | - * @param string[] $taxonomies List of taxonomy names. | |
| 67 | - * @param bool $sync True if it is synchronization, false if it is a copy. | |
| 68 | - * @param int $from Id of the post from which we copy informations. | |
| 69 | - * @param int $to Id of the post to which we paste informations. | |
| 70 | - * @param string $lang Language slug. | |
| 51 | + * @param array $taxonomies List of taxonomy names | |
| 52 | + * @param bool $sync True if it is synchronization, false if it is a copy | |
| 53 | + * @param int $from Id of the post from which we copy informations | |
| 54 | + * @param int $to Id of the post to which we paste informations | |
| 55 | + * @param string $lang Language slug | |
| 71 | 56 | */ |
| 72 | 57 | return array_unique( apply_filters( 'pll_copy_taxonomies', $taxonomies, $sync, $from, $to, $lang ) ); |
| 73 | 58 | } |
| 74 | 59 | |
| @@ -76,13 +61,13 @@ | ||
| 76 | 61 | * When copying or synchronizing terms, translate terms in translatable taxonomies |
| 77 | 62 | * |
| 78 | 63 | * @since 2.3 |
| 79 | 64 | * |
| 80 | - * @param int $object_id Object ID. | |
| 81 | - * @param int[] $terms List of terms ids assigned to the source post. | |
| 82 | - * @param string $taxonomy Taxonomy name. | |
| 83 | - * @param string $lang Language slug. | |
| 84 | - * @return int[] List of terms ids to assign to the target post. | |
| 65 | + * @param array $object_id Object ID | |
| 66 | + * @param array $terms List of terms ids assigned to the source post | |
| 67 | + * @param string $taxonomy Taxonomy name | |
| 68 | + * @param string $lang Language slug | |
| 69 | + * @return array List of terms ids to assign to the target post | |
| 85 | 70 | */ |
| 86 | 71 | protected function maybe_translate_terms( $object_id, $terms, $taxonomy, $lang ) { |
| 87 | 72 | if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomy ) ) { |
| 88 | 73 | $newterms = array(); |
| @@ -115,9 +100,9 @@ | ||
| 115 | 100 | return $terms; // Empty $terms or untranslated taxonomy |
| 116 | 101 | } |
| 117 | 102 | |
| 118 | 103 | /** |
| 119 | - * Maybe copy taxonomy terms from one post to the other. | |
| 104 | + * Maybe copy taxonomy terms from one post to the other | |
| 120 | 105 | * |
| 121 | 106 | * @since 2.6 |
| 122 | 107 | * |
| 123 | 108 | * @param int $object_id Source object ID. |
| @@ -125,9 +110,8 @@ | ||
| 125 | 110 | * @param string $lang Target language. |
| 126 | 111 | * @param array $terms An array of object terms. |
| 127 | 112 | * @param string $taxonomy Taxonomy slug. |
| 128 | 113 | * @param bool $append Whether to append new terms to the old terms. |
| 129 | - * @return void | |
| 130 | 114 | */ |
| 131 | 115 | protected function copy_object_terms( $object_id, $tr_id, $lang, $terms, $taxonomy, $append ) { |
| 132 | 116 | $to_copy = $this->get_taxonomies_to_copy( true, $object_id, $tr_id, $lang ); |
| 133 | 117 | |
| @@ -151,18 +135,17 @@ | ||
| 151 | 135 | |
| 152 | 136 | } |
| 153 | 137 | |
| 154 | 138 | /** |
| 155 | - * When assigning terms to a post, assign translated terms to the translated posts (synchronisation). | |
| 139 | + * When assigning terms to a post, assign translated terms to the translated posts (synchronisation) | |
| 156 | 140 | * |
| 157 | 141 | * @since 2.3 |
| 158 | 142 | * |
| 159 | 143 | * @param int $object_id Object ID. |
| 160 | 144 | * @param array $terms An array of object terms. |
| 161 | - * @param int[] $tt_ids An array of term taxonomy IDs. | |
| 145 | + * @param array $tt_ids An array of term taxonomy IDs. | |
| 162 | 146 | * @param string $taxonomy Taxonomy slug. |
| 163 | 147 | * @param bool $append Whether to append new terms to the old terms. |
| 164 | - * @return void | |
| 165 | 148 | */ |
| 166 | 149 | public function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append ) { |
| 167 | 150 | static $avoid_recursion = false; |
| 168 | 151 | $taxonomy_object = get_taxonomy( $taxonomy ); |
| @@ -167,9 +150,9 @@ | ||
| 167 | 150 | static $avoid_recursion = false; |
| 168 | 151 | $taxonomy_object = get_taxonomy( $taxonomy ); |
| 169 | 152 | |
| 170 | 153 | // Make sure that the taxonomy is registered for a post type |
| 171 | - if ( ! $avoid_recursion && ! empty( $taxonomy_object ) && array_filter( $taxonomy_object->object_type, 'post_type_exists' ) ) { | |
| 154 | + if ( ! $avoid_recursion && array_filter( $taxonomy_object->object_type, 'post_type_exists' ) ) { | |
| 172 | 155 | $avoid_recursion = true; |
| 173 | 156 | |
| 174 | 157 | $tr_ids = $this->model->post->get_translations( $object_id ); |
| 175 | 158 | |
| @@ -179,14 +162,9 @@ | ||
| 179 | 162 | $this->copy_object_terms( $object_id, $tr_id, $lang, $terms, $taxonomy, $append ); |
| 180 | 163 | } else { |
| 181 | 164 | // No permission to synchronize, so let's synchronize in reverse order |
| 182 | 165 | $orig_lang = array_search( $object_id, $tr_ids ); |
| 183 | - $tr_terms = get_the_terms( $tr_id, $taxonomy ); | |
| 184 | - | |
| 185 | - if ( false === $tr_terms ) { | |
| 186 | - $tr_terms = array(); | |
| 187 | - } | |
| 188 | - | |
| 166 | + $tr_terms = (array) get_the_terms( $tr_id, $taxonomy ); | |
| 189 | 167 | if ( is_array( $tr_terms ) ) { |
| 190 | 168 | $tr_terms = wp_list_pluck( $tr_terms, 'term_id' ); |
| 191 | 169 | $this->copy_object_terms( $tr_id, $object_id, $orig_lang, $tr_terms, $taxonomy, $append ); |
| 192 | 170 | } |
| @@ -206,9 +184,8 @@ | ||
| 206 | 184 | * |
| 207 | 185 | * @param int $from Id of the source post |
| 208 | 186 | * @param int $to Id of the target post |
| 209 | 187 | * @param string $lang Language slug |
| 210 | - * @return void | |
| 211 | 188 | */ |
| 212 | 189 | public function copy( $from, $to, $lang ) { |
| 213 | 190 | remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 ); |
| 214 | 191 | |
| @@ -215,9 +192,9 @@ | ||
| 215 | 192 | // Get taxonomies to sync for this post type |
| 216 | 193 | $taxonomies = array_intersect( get_post_taxonomies( $from ), $this->get_taxonomies_to_copy( false, $from, $to, $lang ) ); |
| 217 | 194 | |
| 218 | 195 | // Update the term cache to reduce the number of queries in the loop |
| 219 | - update_object_term_cache( array( $from ), get_post_type( $from ) ); | |
| 196 | + update_object_term_cache( $from, get_post_type( $from ) ); | |
| 220 | 197 | |
| 221 | 198 | // Copy |
| 222 | 199 | foreach ( $taxonomies as $tax ) { |
| 223 | 200 | if ( $terms = get_the_terms( $from, $tax ) ) { |
| @@ -233,16 +210,15 @@ | ||
| 233 | 210 | add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 ); |
| 234 | 211 | } |
| 235 | 212 | |
| 236 | 213 | /** |
| 237 | - * When creating a new term, associate it to posts having translations associated to the translated terms. | |
| 214 | + * When creating a new term, associate it to posts having translations associated to the translated terms | |
| 238 | 215 | * |
| 239 | 216 | * @since 2.3 |
| 240 | 217 | * |
| 241 | - * @param int $term_id Id of the created term. | |
| 242 | - * @param string $taxonomy Taxonomy. | |
| 243 | - * @param int[] $translations Ids of the translations of the created term. | |
| 244 | - * @return void | |
| 218 | + * @param int $term_id Id of the created term | |
| 219 | + * @param string $taxonomy Taxonomy | |
| 220 | + * @param array $translations Ids of the translations of the created term | |
| 245 | 221 | */ |
| 246 | 222 | public function create_term( $term_id, $taxonomy, $translations ) { |
| 247 | 223 | if ( doing_action( 'create_term' ) && in_array( $taxonomy, $this->get_taxonomies_to_copy( true ) ) ) { |
| 248 | 224 | // Get all posts associated to the translated terms |
| @@ -289,10 +265,8 @@ | ||
| 289 | 265 | * Deactivate the synchronization of terms before deleting a term |
| 290 | 266 | * to avoid translated terms to be removed from translated posts |
| 291 | 267 | * |
| 292 | 268 | * @since 2.3.2 |
| 293 | - * | |
| 294 | - * @return void | |
| 295 | 269 | */ |
| 296 | 270 | public function pre_delete_term() { |
| 297 | 271 | remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 ); |
| 298 | 272 | } |
| @@ -300,10 +274,8 @@ | ||
| 300 | 274 | /** |
| 301 | 275 | * Re-activate the synchronization of terms after a term is deleted |
| 302 | 276 | * |
| 303 | 277 | * @since 2.3.2 |
| 304 | - * | |
| 305 | - * @return void | |
| 306 | 278 | */ |
| 307 | 279 | public function delete_term() { |
| 308 | 280 | add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 ); |
| 309 | 281 | } |