PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | modules/sync/sync-tax.php +50 -27 2.73.1.4 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * A class to manage the sychronization of taxonomy terms across posts translations
5 8 *
@@ -7,8 +10,20 @@
7 10 */
8 11 class PLL_Sync_Tax {
9 12
10 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 + /**
11 26 * Constructor
12 27 *
13 28 * @since 2.3
14 29 *
@@ -14,9 +29,9 @@
14 29 *
15 30 * @param object $polylang
16 31 */
17 32 public function __construct( &$polylang ) {
18 - $this->model = &$polylang->model;
33 + $this->model = &$polylang->model;
19 34 $this->options = &$polylang->options;
20 35
21 36 add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 );
22 37 add_action( 'pll_save_term', array( $this, 'create_term' ), 10, 3 );
@@ -24,18 +39,18 @@
24 39 add_action( 'delete_term', array( $this, 'delete_term' ) );
25 40 }
26 41
27 42 /**
28 - * Get the list of taxonomies to copy or to synchronize
43 + * Get the list of taxonomies to copy or to synchronize.
29 44 *
30 45 * @since 1.7
31 46 * @since 2.1 The `$from`, `$to`, `$lang` parameters were added.
32 47 *
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
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.
38 53 */
39 54 protected function get_taxonomies_to_copy( $sync, $from = null, $to = null, $lang = null ) {
40 55 $taxonomies = ! $sync || in_array( 'taxonomies', $this->options['sync'] ) ? $this->model->get_translated_taxonomies() : array();
41 56 if ( ! $sync || in_array( 'post_format', $this->options['sync'] ) ) {
@@ -42,18 +57,18 @@
42 57 $taxonomies[] = 'post_format';
43 58 }
44 59
45 60 /**
46 - * Filter the taxonomies to copy or synchronize
61 + * Filters the taxonomies to copy or synchronize.
47 62 *
48 63 * @since 1.7
49 64 * @since 2.1 The `$from`, `$to`, `$lang` parameters were added.
50 65 *
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
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.
56 71 */
57 72 return array_unique( apply_filters( 'pll_copy_taxonomies', $taxonomies, $sync, $from, $to, $lang ) );
58 73 }
59 74
@@ -61,13 +76,13 @@
61 76 * When copying or synchronizing terms, translate terms in translatable taxonomies
62 77 *
63 78 * @since 2.3
64 79 *
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
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.
70 85 */
71 86 protected function maybe_translate_terms( $object_id, $terms, $taxonomy, $lang ) {
72 87 if ( is_array( $terms ) && $this->model->is_translated_taxonomy( $taxonomy ) ) {
73 88 $newterms = array();
@@ -100,9 +115,9 @@
100 115 return $terms; // Empty $terms or untranslated taxonomy
101 116 }
102 117
103 118 /**
104 - * Maybe copy taxonomy terms from one post to the other
119 + * Maybe copy taxonomy terms from one post to the other.
105 120 *
106 121 * @since 2.6
107 122 *
108 123 * @param int $object_id Source object ID.
@@ -110,8 +125,9 @@
110 125 * @param string $lang Target language.
111 126 * @param array $terms An array of object terms.
112 127 * @param string $taxonomy Taxonomy slug.
113 128 * @param bool $append Whether to append new terms to the old terms.
129 + * @return void
114 130 */
115 131 protected function copy_object_terms( $object_id, $tr_id, $lang, $terms, $taxonomy, $append ) {
116 132 $to_copy = $this->get_taxonomies_to_copy( true, $object_id, $tr_id, $lang );
117 133
@@ -135,17 +151,18 @@
135 151
136 152 }
137 153
138 154 /**
139 - * 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).
140 156 *
141 157 * @since 2.3
142 158 *
143 159 * @param int $object_id Object ID.
144 160 * @param array $terms An array of object terms.
145 - * @param array $tt_ids An array of term taxonomy IDs.
161 + * @param int[] $tt_ids An array of term taxonomy IDs.
146 162 * @param string $taxonomy Taxonomy slug.
147 163 * @param bool $append Whether to append new terms to the old terms.
164 + * @return void
148 165 */
149 166 public function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append ) {
150 167 static $avoid_recursion = false;
151 168 $taxonomy_object = get_taxonomy( $taxonomy );
@@ -150,9 +167,9 @@
150 167 static $avoid_recursion = false;
151 168 $taxonomy_object = get_taxonomy( $taxonomy );
152 169
153 170 // Make sure that the taxonomy is registered for a post type
154 - 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' ) ) {
155 172 $avoid_recursion = true;
156 173
157 174 $tr_ids = $this->model->post->get_translations( $object_id );
158 175
@@ -189,8 +206,9 @@
189 206 *
190 207 * @param int $from Id of the source post
191 208 * @param int $to Id of the target post
192 209 * @param string $lang Language slug
210 + * @return void
193 211 */
194 212 public function copy( $from, $to, $lang ) {
195 213 remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 );
196 214
@@ -197,9 +215,9 @@
197 215 // Get taxonomies to sync for this post type
198 216 $taxonomies = array_intersect( get_post_taxonomies( $from ), $this->get_taxonomies_to_copy( false, $from, $to, $lang ) );
199 217
200 218 // Update the term cache to reduce the number of queries in the loop
201 - update_object_term_cache( $from, get_post_type( $from ) );
219 + update_object_term_cache( array( $from ), get_post_type( $from ) );
202 220
203 221 // Copy
204 222 foreach ( $taxonomies as $tax ) {
205 223 if ( $terms = get_the_terms( $from, $tax ) ) {
@@ -215,15 +233,16 @@
215 233 add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 );
216 234 }
217 235
218 236 /**
219 - * 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.
220 238 *
221 239 * @since 2.3
222 240 *
223 - * @param int $term_id Id of the created term
224 - * @param string $taxonomy Taxonomy
225 - * @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
226 245 */
227 246 public function create_term( $term_id, $taxonomy, $translations ) {
228 247 if ( doing_action( 'create_term' ) && in_array( $taxonomy, $this->get_taxonomies_to_copy( true ) ) ) {
229 248 // Get all posts associated to the translated terms
@@ -270,8 +289,10 @@
270 289 * Deactivate the synchronization of terms before deleting a term
271 290 * to avoid translated terms to be removed from translated posts
272 291 *
273 292 * @since 2.3.2
293 + *
294 + * @return void
274 295 */
275 296 public function pre_delete_term() {
276 297 remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 );
277 298 }
@@ -279,8 +300,10 @@
279 300 /**
280 301 * Re-activate the synchronization of terms after a term is deleted
281 302 *
282 303 * @since 2.3.2
304 + *
305 + * @return void
283 306 */
284 307 public function delete_term() {
285 308 add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 );
286 309 }