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.php +75 -27 2.7.13.7.7 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * Manages copy and synchronization of terms and post metas on front
5 8 *
@@ -5,16 +8,41 @@
5 8 *
6 9 * @since 2.4
7 10 */
8 11 class PLL_Sync {
9 - public $taxonomies, $post_metas, $term_meta;
12 + /**
13 + * @var PLL_Sync_Tax
14 + */
15 + public $taxonomies;
10 16
11 17 /**
18 + * @var PLL_Sync_Post_Metas
19 + */
20 + public $post_metas;
21 +
22 + /**
23 + * @var PLL_Sync_Term_Metas
24 + */
25 + public $term_metas;
26 +
27 + /**
28 + * Stores the plugin options.
29 + *
30 + * @var array
31 + */
32 + protected $options;
33 +
34 + /**
35 + * @var PLL_Model
36 + */
37 + protected $model;
38 +
39 + /**
12 40 * Constructor
13 41 *
14 42 * @since 1.2
15 43 *
16 - * @param object $polylang
44 + * @param object $polylang The Polylang object.
17 45 */
18 46 public function __construct( &$polylang ) {
19 47 $this->model = &$polylang->model;
20 48 $this->options = &$polylang->options;
@@ -41,13 +69,13 @@
41 69 add_filter( 'pre_update_option_sticky_posts', array( $this, 'sync_sticky_posts' ), 10, 2 );
42 70 }
43 71
44 72 /**
45 - * Get post fields to synchornize
73 + * Get post fields to synchronize.
46 74 *
47 75 * @since 2.4
48 76 *
49 - * @param object $post Post object
77 + * @param WP_Post $post Post object.
50 78 * @return array
51 79 */
52 80 protected function get_fields_to_sync( $post ) {
53 81 $postarr = array();
@@ -115,15 +143,16 @@
115 143 return $data;
116 144 }
117 145
118 146 /**
119 - * Synchronizes post fields in translations
147 + * Synchronizes post fields in translations.
120 148 *
121 149 * @since 2.4
122 150 *
123 - * @param int $post_id post id
124 - * @param object $post post object
125 - * @param array $translations post translations
151 + * @param int $post_id Post id.
152 + * @param WP_Post $post Post object.
153 + * @param int[] $translations Post translations.
154 + * @return void
126 155 */
127 156 public function pll_save_post( $post_id, $post, $translations ) {
128 157 global $wpdb;
129 158
@@ -138,9 +167,9 @@
138 167
139 168 $tr_arr = $postarr;
140 169 unset( $tr_arr['post_parent'] );
141 170
142 - // Do not udpate the translation parent if the user set a parent with no translation.
171 + // Do not update the translation parent if the user set a parent with no translation.
143 172 if ( isset( $postarr['post_parent'] ) ) {
144 173 $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0;
145 174 if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) {
146 175 $tr_arr['post_parent'] = $post_parent;
@@ -158,9 +187,9 @@
158 187 }
159 188 }
160 189
161 190 /**
162 - * Synchronize term parent in translations
191 + * Synchronize term parent in translations.
163 192 * Calling clean_term_cache *after* this is mandatory otherwise the $taxonomy_children option is not correctly updated
164 193 *
165 194 * @since 2.3
166 195 *
@@ -166,28 +195,46 @@
166 195 *
167 196 * @param int $term_id Term id.
168 197 * @param int $tt_id Term taxonomy id, not used.
169 198 * @param string $taxonomy Taxonomy name.
199 + * @return void
170 200 */
171 201 public function sync_term_parent( $term_id, $tt_id, $taxonomy ) {
172 202 global $wpdb;
173 203
174 - if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) {
175 - $term = get_term( $term_id );
176 - $translations = $this->model->term->get_translations( $term_id );
204 + if ( ! is_taxonomy_hierarchical( $taxonomy ) || ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
205 + return;
206 + }
177 207
178 - foreach ( $translations as $lang => $tr_id ) {
179 - if ( ! empty( $tr_id ) && $tr_id !== $term_id ) {
180 - $tr_parent = $this->model->term->get_translation( $term->parent, $lang );
181 - $wpdb->update(
182 - $wpdb->term_taxonomy,
183 - array( 'parent' => isset( $tr_parent ) ? $tr_parent : 0 ),
184 - array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id )
185 - );
208 + $term = get_term( $term_id );
209 + if ( ! $term instanceof WP_Term ) {
210 + return;
211 + }
186 212
187 - clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9
188 - }
213 + $translations = $this->model->term->get_translations( $term_id );
214 +
215 + foreach ( $translations as $lang => $tr_id ) {
216 + if ( $tr_id === $term_id ) {
217 + continue;
189 218 }
219 +
220 + $tr_parent = $this->model->term->get_translation( $term->parent, $lang );
221 + $tr_term = get_term( (int) $tr_id, $taxonomy );
222 +
223 + if ( str_starts_with( current_filter(), 'created_' ) && 0 === $tr_parent ) {
224 + // Do not remove the existing hierarchy of translations when creating new term without parent.
225 + continue;
226 + }
227 +
228 + if ( $tr_term instanceof WP_Term && ! ( $term->parent && empty( $tr_parent ) ) ) {
229 + $wpdb->update(
230 + $wpdb->term_taxonomy,
231 + array( 'parent' => $tr_parent ?: 0 ),
232 + array( 'term_taxonomy_id' => $tr_term->term_taxonomy_id )
233 + );
234 +
235 + clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9.
236 + }
190 237 }
191 238 }
192 239
193 240 /**
@@ -195,8 +242,9 @@
195 242 *
196 243 * @since 1.8
197 244 *
198 245 * @param int $post_id post id
246 + * @return void
199 247 */
200 248 public function edit_attachment( $post_id ) {
201 249 $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) );
202 250 }
@@ -201,15 +249,15 @@
201 249 $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) );
202 250 }
203 251
204 252 /**
205 - * Synchronize sticky posts
253 + * Synchronize sticky posts.
206 254 *
207 255 * @since 2.3
208 256 *
209 - * @param array $value New option value
210 - * @param array $old_value Old option value
211 - * @return array
257 + * @param int[] $value New option value.
258 + * @param int[] $old_value Old option value.
259 + * @return int[]
212 260 */
213 261 public function sync_sticky_posts( $value, $old_value ) {
214 262 if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
215 263 // Stick post