PluginProbe
Polylang / 2.5
Polylang v2.5
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 +42 -100 3.0.32.5 View file →
@@ -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,54 +100,17 @@
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 + * When assigning terms to a post, assign translated terms to the translated posts (synchronisation)
120 105 *
121 - * @since 2.6
122 - *
123 - * @param int $object_id Source object ID.
124 - * @param int $tr_id Target object ID.
125 - * @param string $lang Target language.
126 - * @param array $terms An array of object terms.
127 - * @param string $taxonomy Taxonomy slug.
128 - * @param bool $append Whether to append new terms to the old terms.
129 - * @return void
130 - */
131 - protected function copy_object_terms( $object_id, $tr_id, $lang, $terms, $taxonomy, $append ) {
132 - $to_copy = $this->get_taxonomies_to_copy( true, $object_id, $tr_id, $lang );
133 -
134 - if ( in_array( $taxonomy, $to_copy ) ) {
135 - $newterms = $this->maybe_translate_terms( $object_id, $terms, $taxonomy, $lang );
136 -
137 - // For some reasons, the user may have untranslated terms in the translation. Don't forget them.
138 - if ( $this->model->is_translated_taxonomy( $taxonomy ) ) {
139 - $tr_terms = get_the_terms( $tr_id, $taxonomy );
140 - if ( is_array( $tr_terms ) ) {
141 - foreach ( $tr_terms as $term ) {
142 - if ( ! $this->model->term->get_translation( $term->term_id, $this->model->post->get_language( $object_id ) ) ) {
143 - $newterms[] = (int) $term->term_id;
144 - }
145 - }
146 - }
147 - }
148 -
149 - wp_set_object_terms( $tr_id, $newterms, $taxonomy, $append );
150 - }
151 -
152 - }
153 -
154 - /**
155 - * When assigning terms to a post, assign translated terms to the translated posts (synchronisation).
156 - *
157 106 * @since 2.3
158 107 *
159 108 * @param int $object_id Object ID.
160 109 * @param array $terms An array of object terms.
161 - * @param int[] $tt_ids An array of term taxonomy IDs.
110 + * @param array $tt_ids An array of term taxonomy IDs.
162 111 * @param string $taxonomy Taxonomy slug.
163 112 * @param bool $append Whether to append new terms to the old terms.
164 - * @return void
165 113 */
166 114 public function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append ) {
167 115 static $avoid_recursion = false;
168 116 $taxonomy_object = get_taxonomy( $taxonomy );
@@ -167,9 +115,9 @@
167 115 static $avoid_recursion = false;
168 116 $taxonomy_object = get_taxonomy( $taxonomy );
169 117
170 118 // 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' ) ) {
119 + if ( ! $avoid_recursion && array_filter( $taxonomy_object->object_type, 'post_type_exists' ) ) {
172 120 $avoid_recursion = true;
173 121
174 122 $tr_ids = $this->model->post->get_translations( $object_id );
175 123
@@ -174,24 +122,26 @@
174 122 $tr_ids = $this->model->post->get_translations( $object_id );
175 123
176 124 foreach ( $tr_ids as $lang => $tr_id ) {
177 125 if ( $tr_id !== $object_id ) {
178 - if ( $this->model->post->current_user_can_synchronize( $object_id ) ) {
179 - $this->copy_object_terms( $object_id, $tr_id, $lang, $terms, $taxonomy, $append );
180 - } else {
181 - // No permission to synchronize, so let's synchronize in reverse order
182 - $orig_lang = array_search( $object_id, $tr_ids );
183 - $tr_terms = get_the_terms( $tr_id, $taxonomy );
126 + $to_copy = $this->get_taxonomies_to_copy( true, $object_id, $tr_id, $lang );
184 127
185 - if ( false === $tr_terms ) {
186 - $tr_terms = array();
128 + if ( in_array( $taxonomy, $to_copy ) ) {
129 + $newterms = $this->maybe_translate_terms( $object_id, $terms, $taxonomy, $lang );
130 +
131 + // For some reasons, the user may have untranslated terms in the translation. Don't forget them.
132 + if ( $this->model->is_translated_taxonomy( $taxonomy ) ) {
133 + $tr_terms = get_the_terms( $tr_id, $taxonomy );
134 + if ( is_array( $tr_terms ) ) {
135 + foreach ( $tr_terms as $term ) {
136 + if ( ! $this->model->term->get_translation( $term->term_id, $this->model->post->get_language( $object_id ) ) ) {
137 + $newterms[] = (int) $term->term_id;
138 + }
139 + }
140 + }
187 141 }
188 142
189 - if ( is_array( $tr_terms ) ) {
190 - $tr_terms = wp_list_pluck( $tr_terms, 'term_id' );
191 - $this->copy_object_terms( $tr_id, $object_id, $orig_lang, $tr_terms, $taxonomy, $append );
192 - }
193 - break;
143 + wp_set_object_terms( $tr_id, $newterms, $taxonomy, $append );
194 144 }
195 145 }
196 146 }
197 147
@@ -206,9 +156,8 @@
206 156 *
207 157 * @param int $from Id of the source post
208 158 * @param int $to Id of the target post
209 159 * @param string $lang Language slug
210 - * @return void
211 160 */
212 161 public function copy( $from, $to, $lang ) {
213 162 remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 );
214 163
@@ -215,9 +164,9 @@
215 164 // Get taxonomies to sync for this post type
216 165 $taxonomies = array_intersect( get_post_taxonomies( $from ), $this->get_taxonomies_to_copy( false, $from, $to, $lang ) );
217 166
218 167 // Update the term cache to reduce the number of queries in the loop
219 - update_object_term_cache( array( $from ), get_post_type( $from ) );
168 + update_object_term_cache( $from, get_post_type( $from ) );
220 169
221 170 // Copy
222 171 foreach ( $taxonomies as $tax ) {
223 172 if ( $terms = get_the_terms( $from, $tax ) ) {
@@ -233,16 +182,15 @@
233 182 add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 );
234 183 }
235 184
236 185 /**
237 - * When creating a new term, associate it to posts having translations associated to the translated terms.
186 + * When creating a new term, associate it to posts having translations associated to the translated terms
238 187 *
239 188 * @since 2.3
240 189 *
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
190 + * @param int $term_id Id of the created term
191 + * @param string $taxonomy Taxonomy
192 + * @param array $translations Ids of the translations of the created term
245 193 */
246 194 public function create_term( $term_id, $taxonomy, $translations ) {
247 195 if ( doing_action( 'create_term' ) && in_array( $taxonomy, $this->get_taxonomies_to_copy( true ) ) ) {
248 196 // Get all posts associated to the translated terms
@@ -277,11 +225,9 @@
277 225
278 226 $posts = array_unique( $posts );
279 227
280 228 foreach ( $posts as $post_id ) {
281 - if ( current_user_can( 'assign_term', $term_id ) ) {
282 - wp_set_object_terms( $post_id, $term_id, $taxonomy, true );
283 - }
229 + wp_set_object_terms( $post_id, $term_id, $taxonomy, true );
284 230 }
285 231 }
286 232 }
287 233
@@ -289,10 +235,8 @@
289 235 * Deactivate the synchronization of terms before deleting a term
290 236 * to avoid translated terms to be removed from translated posts
291 237 *
292 238 * @since 2.3.2
293 - *
294 - * @return void
295 239 */
296 240 public function pre_delete_term() {
297 241 remove_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 );
298 242 }
@@ -300,10 +244,8 @@
300 244 /**
301 245 * Re-activate the synchronization of terms after a term is deleted
302 246 *
303 247 * @since 2.3.2
304 - *
305 - * @return void
306 248 */
307 249 public function delete_term() {
308 250 add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 5 );
309 251 }