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