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