PluginProbe
Polylang / 2.5.2
Polylang v2.5.2
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 +38 -129 3.0.32.5.2 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Manages copy and synchronization of terms and post metas on front
8 5 *
@@ -8,36 +5,11 @@
8 5 *
9 6 * @since 2.4
10 7 */
11 8 class PLL_Sync {
12 - /**
13 - * @var PLL_Sync_Tax
14 - */
15 - public $taxonomies;
9 + public $taxonomies, $post_metas, $term_meta;
16 10
17 11 /**
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 - /**
40 12 * Constructor
41 13 *
42 14 * @since 1.2
43 15 *
@@ -50,11 +22,8 @@
50 22 $this->taxonomies = new PLL_Sync_Tax( $polylang );
51 23 $this->post_metas = new PLL_Sync_Post_Metas( $polylang );
52 24 $this->term_metas = new PLL_Sync_Term_Metas( $polylang );
53 25
54 - add_filter( 'wp_insert_post_parent', array( $this, 'can_sync_post_parent' ), 10, 3 );
55 - add_filter( 'wp_insert_post_data', array( $this, 'can_sync_post_data' ), 10, 2 );
56 -
57 26 add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 );
58 27 add_action( 'created_term', array( $this, 'sync_term_parent' ), 10, 3 );
59 28 add_action( 'edited_term', array( $this, 'sync_term_parent' ), 10, 3 );
60 29
@@ -69,13 +38,13 @@
69 38 add_filter( 'pre_update_option_sticky_posts', array( $this, 'sync_sticky_posts' ), 10, 2 );
70 39 }
71 40
72 41 /**
73 - * Get post fields to synchronize.
42 + * Get post fields to synchornize
74 43 *
75 44 * @since 2.4
76 45 *
77 - * @param WP_Post $post Post object.
46 + * @param object $post Post object
78 47 * @return array
79 48 */
80 49 protected function get_fields_to_sync( $post ) {
81 50 $postarr = array();
@@ -98,92 +67,42 @@
98 67 return $postarr;
99 68 }
100 69
101 70 /**
102 - * Prevents synchronized post parent modification if the current user hasn't enough rights
71 + * Synchronizes post fields in translations
103 72 *
104 - * @since 2.6
105 - *
106 - * @param int $post_parent Post parent ID
107 - * @param int $post_id Post ID, unused
108 - * @param array $postarr Array of parsed post data
109 - * @return int
110 - */
111 - public function can_sync_post_parent( $post_parent, $post_id, $postarr ) {
112 - if ( ! empty( $postarr['ID'] ) && ! $this->model->post->current_user_can_synchronize( $postarr['ID'] ) ) {
113 - $tr_ids = $this->model->post->get_translations( $postarr['ID'] );
114 - foreach ( $tr_ids as $tr_id ) {
115 - if ( $tr_id !== $postarr['ID'] && $post = get_post( $tr_id ) ) {
116 - $post_parent = $post->post_parent;
117 - break;
118 - }
119 - }
120 - }
121 - return $post_parent;
122 - }
123 -
124 - /**
125 - * Prevents synchronized post data modification if the current user hasn't enough rights
126 - *
127 - * @since 2.6
128 - *
129 - * @param array $data An array of slashed post data.
130 - * @param array $postarr An array of sanitized, but otherwise unmodified post data.
131 - * @return array
132 - */
133 - public function can_sync_post_data( $data, $postarr ) {
134 - if ( ! empty( $postarr['ID'] ) && ! $this->model->post->current_user_can_synchronize( $postarr['ID'] ) ) {
135 - foreach ( $this->model->post->get_translations( $postarr['ID'] ) as $tr_id ) {
136 - if ( $tr_id !== $postarr['ID'] && $post = get_post( $tr_id ) ) {
137 - $to_sync = $this->get_fields_to_sync( $post );
138 - $data = array_merge( $data, $to_sync );
139 - break;
140 - }
141 - }
142 - }
143 - return $data;
144 - }
145 -
146 - /**
147 - * Synchronizes post fields in translations.
148 - *
149 73 * @since 2.4
150 74 *
151 - * @param int $post_id Post id.
152 - * @param WP_Post $post Post object.
153 - * @param int[] $translations Post translations.
154 - * @return void
75 + * @param int $post_id post id
76 + * @param object $post post object
77 + * @param array $translations post translations
155 78 */
156 79 public function pll_save_post( $post_id, $post, $translations ) {
157 80 global $wpdb;
158 81
159 - if ( $this->model->post->current_user_can_synchronize( $post_id ) ) {
160 - $postarr = $this->get_fields_to_sync( $post );
82 + $postarr = $this->get_fields_to_sync( $post );
161 83
162 - if ( ! empty( $postarr ) ) {
163 - foreach ( $translations as $lang => $tr_id ) {
164 - if ( ! $tr_id || $tr_id === $post_id ) {
165 - continue;
166 - }
84 + if ( ! empty( $postarr ) ) {
85 + foreach ( $translations as $lang => $tr_id ) {
86 + if ( ! $tr_id || $tr_id === $post_id ) {
87 + continue;
88 + }
167 89
168 - $tr_arr = $postarr;
169 - unset( $tr_arr['post_parent'] );
90 + $tr_arr = $postarr;
91 + unset( $tr_arr['post_parent'] );
170 92
171 - // Do not udpate the translation parent if the user set a parent with no translation.
172 - if ( isset( $postarr['post_parent'] ) ) {
173 - $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0;
174 - if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) {
175 - $tr_arr['post_parent'] = $post_parent;
176 - }
93 + // Do not udpate the translation parent if the user set a parent with no translation
94 + if ( isset( $postarr['post_parent'] ) ) {
95 + $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0;
96 + if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) {
97 + $tr_arr['post_parent'] = $post_parent;
177 98 }
99 + }
178 100
179 - // Update all the rows at once.
180 - if ( ! empty( $tr_arr ) ) {
181 - // Don't use wp_update_post to avoid infinite loop.
182 - $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) );
183 - clean_post_cache( $tr_id );
184 - }
185 - }
101 + // Update all the row at once
102 + // Don't use wp_update_post to avoid infinite loop
103 + $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) );
104 + clean_post_cache( $tr_id );
186 105 }
187 106 }
188 107 }
189 108
@@ -195,9 +114,8 @@
195 114 *
196 115 * @param int $term_id Term id.
197 116 * @param int $tt_id Term taxonomy id, not used.
198 117 * @param string $taxonomy Taxonomy name.
199 - * @return void
200 118 */
201 119 public function sync_term_parent( $term_id, $tt_id, $taxonomy ) {
202 120 global $wpdb;
203 121
@@ -202,27 +120,19 @@
202 120 global $wpdb;
203 121
204 122 if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) {
205 123 $term = get_term( $term_id );
124 + $translations = $this->model->term->get_translations( $term_id );
206 125
207 - if ( $term instanceof WP_Term ) {
208 - $translations = $this->model->term->get_translations( $term_id );
126 + foreach ( $translations as $lang => $tr_id ) {
127 + if ( ! empty( $tr_id ) && $tr_id !== $term_id && $tr_parent = $this->model->term->get_translation( $term->parent, $lang ) ) {
128 + $wpdb->update(
129 + $wpdb->term_taxonomy,
130 + array( 'parent' => isset( $tr_parent ) ? $tr_parent : 0 ),
131 + array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id )
132 + );
209 133
210 - foreach ( $translations as $lang => $tr_id ) {
211 - if ( ! empty( $tr_id ) && $tr_id !== $term_id ) {
212 - $tr_parent = $this->model->term->get_translation( $term->parent, $lang );
213 - $tr_term = get_term( (int) $tr_id, $taxonomy );
214 -
215 - if ( $tr_term instanceof WP_Term ) {
216 - $wpdb->update(
217 - $wpdb->term_taxonomy,
218 - array( 'parent' => $tr_parent ? $tr_parent : 0 ),
219 - array( 'term_taxonomy_id' => $tr_term->term_taxonomy_id )
220 - );
221 -
222 - clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9.
223 - }
224 - }
134 + clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9
225 135 }
226 136 }
227 137 }
228 138 }
@@ -232,9 +142,8 @@
232 142 *
233 143 * @since 1.8
234 144 *
235 145 * @param int $post_id post id
236 - * @return void
237 146 */
238 147 public function edit_attachment( $post_id ) {
239 148 $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) );
240 149 }
@@ -239,15 +148,15 @@
239 148 $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) );
240 149 }
241 150
242 151 /**
243 - * Synchronize sticky posts.
152 + * Synchronize sticky posts
244 153 *
245 154 * @since 2.3
246 155 *
247 - * @param int[] $value New option value.
248 - * @param int[] $old_value Old option value.
249 - * @return int[]
156 + * @param array $value New option value
157 + * @param array $old_value Old option value
158 + * @return array
250 159 */
251 160 public function sync_sticky_posts( $value, $old_value ) {
252 161 if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
253 162 // Stick post