PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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 +58 -38 2.93.7.7 View file →
@@ -3,9 +3,9 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * A class to manage the sychronization of taxonomy terms across posts translations
7 + * A class to manage the synchronization of taxonomy terms across posts translations
8 8 *
9 9 * @since 2.3
10 10 */
11 11 class PLL_Sync_Tax {
@@ -10,16 +10,28 @@
10 10 */
11 11 class PLL_Sync_Tax {
12 12
13 13 /**
14 - * Constructor
14 + * Stores the plugin options.
15 15 *
16 + * @var array
17 + */
18 + protected $options;
19 +
20 + /**
21 + * @var PLL_Model
22 + */
23 + protected $model;
24 +
25 + /**
26 + * Constructor.
27 + *
16 28 * @since 2.3
17 29 *
18 - * @param object $polylang
30 + * @param object $polylang The Polylang object.
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 information, optional, defaults to null.
51 + * @param int $to Id of the post to which we paste information, 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 information.
70 + * @param int $to Id of the post to which we paste information.
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();
@@ -91,9 +104,9 @@
91 104 * @param int $tr_term Translated term id
92 105 * @param int $term Source term id
93 106 * @param string $lang Language slug
94 107 */
95 - if ( $term_id = apply_filters( 'pll_maybe_translate_term', $this->model->term->get_translation( $term, $lang ), $term, $lang ) ) {
108 + if ( $term_id = apply_filters( 'pll_maybe_translate_term', (int) $this->model->term->get_translation( $term, $lang ), $term, $lang ) ) {
96 109 $newterms[] = (int) $term_id; // Cast is important otherwise we get 'numeric' tags
97 110 }
98 111 }
99 112
@@ -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
@@ -134,21 +148,21 @@
134 148 }
135 149
136 150 wp_set_object_terms( $tr_id, $newterms, $taxonomy, $append );
137 151 }
138 -
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
@@ -171,9 +185,9 @@
171 185 if ( false === $tr_terms ) {
172 186 $tr_terms = array();
173 187 }
174 188
175 - if ( is_array( $tr_terms ) ) {
189 + if ( is_string( $orig_lang ) && is_array( $tr_terms ) ) {
176 190 $tr_terms = wp_list_pluck( $tr_terms, 'term_id' );
177 191 $this->copy_object_terms( $tr_id, $object_id, $orig_lang, $tr_terms, $taxonomy, $append );
178 192 }
179 193 break;
@@ -185,9 +199,9 @@
185 199 }
186 200 }
187 201
188 202 /**
189 - * Copy terms fron one post to a translation, does not sync
203 + * Copy terms from one post to a translation, does not sync
190 204 *
191 205 * @since 2.3
192 206 *
193 207 * @param int $from Id of the source post
@@ -192,17 +206,18 @@
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 - remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 );
213 + remove_action( 'set_object_terms', array( $this, 'set_object_terms' ) );
199 214
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 ) ) {
@@ -214,19 +229,20 @@
214 229 }
215 230 }
216 231 }
217 232
218 - add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 );
233 + add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 );
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,11 +289,13 @@
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 - remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 );
297 + remove_action( 'set_object_terms', array( $this, 'set_object_terms' ) );
280 298 }
281 299
282 300 /**
283 301 * Re-activate the synchronization of terms after a term is deleted
@@ -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 }