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-metas.php +18 -49 2.92.5 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Abstract class to manage the copy and synchronization of metas
8 5 *
@@ -21,12 +18,8 @@
21 18 */
22 19 public function __construct( &$polylang ) {
23 20 $this->model = &$polylang->model;
24 21
25 - add_filter( "add_{$this->meta_type}_metadata", array( $this, 'can_synchronize_metadata' ), 1, 3 );
26 - add_filter( "update_{$this->meta_type}_metadata", array( $this, 'can_synchronize_metadata' ), 1, 3 );
27 - add_filter( "delete_{$this->meta_type}_metadata", array( $this, 'can_synchronize_metadata' ), 1, 3 );
28 -
29 22 $this->add_all_meta_actions();
30 23
31 24 add_action( "pll_save_{$this->meta_type}", array( $this, 'save_object' ), 10, 3 );
32 25 }
@@ -133,34 +126,8 @@
133 126 return array_unique( apply_filters( "pll_copy_{$this->meta_type}_metas", array(), $sync, $from, $to, $lang ) );
134 127 }
135 128
136 129 /**
137 - * Disallow modifying synchronized meta if the current user can not modify translations
138 - *
139 - * @since 2.6
140 - *
141 - * @param null|bool $check Whether to allow adding/updating/deleting metadata.
142 - * @param int $id Object ID.
143 - * @param string $meta_key Meta key.
144 - * @return null|bool
145 - */
146 - public function can_synchronize_metadata( $check, $id, $meta_key ) {
147 - if ( ! $this->model->{$this->meta_type}->current_user_can_synchronize( $id ) ) {
148 - $tr_ids = $this->model->{$this->meta_type}->get_translations( $id );
149 -
150 - foreach ( $tr_ids as $lang => $tr_id ) {
151 - if ( $tr_id != $id ) {
152 - $to_copy = $this->get_metas_to_copy( $id, $tr_id, $lang, true );
153 - if ( in_array( $meta_key, $to_copy ) ) {
154 - return false;
155 - }
156 - }
157 - }
158 - }
159 - return $check;
160 - }
161 -
162 - /**
163 130 * Synchronize added metas across translations
164 131 *
165 132 * @since 2.3
166 133 *
@@ -180,9 +147,9 @@
180 147 if ( $tr_id != $id ) {
181 148 $to_copy = $this->get_metas_to_copy( $id, $tr_id, $lang, true );
182 149 if ( in_array( $meta_key, $to_copy ) ) {
183 150 $meta_value = $this->maybe_translate_value( $meta_value, $meta_key, $id, $tr_id, $lang );
184 - add_metadata( $this->meta_type, $tr_id, wp_slash( $meta_key ), is_object( $meta_value ) ? $meta_value : wp_slash( $meta_value ) );
151 + add_metadata( $this->meta_type, $tr_id, $meta_key, $meta_value );
185 152 }
186 153 }
187 154 }
188 155
@@ -226,24 +193,24 @@
226 193 if ( ! $avoid_recursion ) {
227 194 $avoid_recursion = true;
228 195 $hash = md5( "$id|$meta_key|" . maybe_serialize( $meta_value ) );
229 196
230 - $prev_meta = get_metadata_by_mid( $this->meta_type, $mid );
197 + $tr_ids = $this->model->{$this->meta_type}->get_translations( $id );
231 198
232 - if ( $prev_meta ) {
233 - $this->remove_add_meta_action(); // We don't want to sync back the new metas
234 - $tr_ids = $this->model->{$this->meta_type}->get_translations( $id );
235 -
236 - foreach ( $tr_ids as $lang => $tr_id ) {
237 - if ( $tr_id != $id && in_array( $meta_key, $this->get_metas_to_copy( $id, $tr_id, $lang, true ) ) ) {
199 + foreach ( $tr_ids as $lang => $tr_id ) {
200 + if ( $tr_id != $id ) {
201 + $to_copy = $this->get_metas_to_copy( $id, $tr_id, $lang, true );
202 + if ( in_array( $meta_key, $to_copy ) ) {
203 + $meta_value = $this->maybe_translate_value( $meta_value, $meta_key, $id, $tr_id, $lang );
204 + $prev_meta = get_metadata_by_mid( $this->meta_type, $mid );
238 205 if ( empty( $this->prev_value[ $hash ] ) || $this->prev_value[ $hash ] === $prev_meta->meta_value ) {
239 206 $prev_value = $this->maybe_translate_value( $prev_meta->meta_value, $meta_key, $id, $tr_id, $lang );
240 - $meta_value = $this->maybe_translate_value( $meta_value, $meta_key, $id, $tr_id, $lang );
241 - update_metadata( $this->meta_type, $tr_id, wp_slash( $meta_key ), is_object( $meta_value ) ? $meta_value : wp_slash( $meta_value ), $prev_value );
207 + $this->remove_add_meta_action(); // We don't want to sync back the new metas
208 + update_metadata( $this->meta_type, $tr_id, $meta_key, $meta_value, $prev_value );
209 + $this->restore_add_meta_action();
242 210 }
243 211 }
244 212 }
245 - $this->restore_add_meta_action();
246 213 }
247 214
248 215 unset( $this->prev_value[ $hash ] );
249 216 $avoid_recursion = false;
@@ -289,9 +256,9 @@
289 256 if ( in_array( $key, $this->to_copy[ $id ][ $tr_id ] ) ) {
290 257 if ( '' !== $value && null !== $value && false !== $value ) { // Same test as WP
291 258 $value = $this->maybe_translate_value( $value, $key, $id, $tr_id, $lang );
292 259 }
293 - delete_metadata( $this->meta_type, $tr_id, wp_slash( $key ), is_object( $value ) ? $value : wp_slash( $value ) );
260 + delete_metadata( $this->meta_type, $tr_id, $key, $value );
294 261 }
295 262 }
296 263 }
297 264 }
@@ -322,9 +289,9 @@
322 289 foreach ( $to_copy as $key ) {
323 290 if ( empty( $metas[ $key ] ) ) {
324 291 if ( ! empty( $tr_metas[ $key ] ) ) {
325 292 // If the meta key is not present in the source object, delete all values
326 - delete_metadata( $this->meta_type, $to, wp_slash( $key ) );
293 + delete_metadata( $this->meta_type, $to, $key );
327 294 }
328 295 } else {
329 296 if ( ! empty( $tr_metas[ $key ] ) && 1 === count( $metas[ $key ] ) && 1 === count( $tr_metas[ $key ] ) ) {
330 297 // One custom field to update
@@ -330,20 +297,20 @@
330 297 // One custom field to update
331 298 $value = reset( $metas[ $key ] );
332 299 $value = maybe_unserialize( $value );
333 300 $to_value = $this->maybe_translate_value( $value, $key, $from, $to, $lang );
334 - update_metadata( $this->meta_type, $to, wp_slash( $key ), is_object( $to_value ) ? $to_value : wp_slash( $to_value ) );
301 + update_metadata( $this->meta_type, $to, $key, $to_value );
335 302 } else {
336 303 // Multiple custom fields, either in the source or the target
337 304 if ( ! empty( $tr_metas[ $key ] ) ) {
338 305 // The synchronization of multiple values custom fields is easier if we delete all metas first
339 - delete_metadata( $this->meta_type, $to, wp_slash( $key ) );
306 + delete_metadata( $this->meta_type, $to, $key );
340 307 }
341 308
342 309 foreach ( $metas[ $key ] as $value ) {
343 310 $value = maybe_unserialize( $value );
344 311 $to_value = $this->maybe_translate_value( $value, $key, $from, $to, $lang );
345 - add_metadata( $this->meta_type, $to, wp_slash( $key ), is_object( $to_value ) ? $to_value : wp_slash( $to_value ) );
312 + add_metadata( $this->meta_type, $to, $key, $to_value );
346 313 }
347 314 }
348 315 }
349 316 }
@@ -361,8 +328,10 @@
361 328 * @param object $obj Not used
362 329 * @param array $translations The list of translations object ids
363 330 */
364 331 public function save_object( $object_id, $obj, $translations ) {
332 + $src_lang = array_search( $object_id, $translations );
333 +
365 334 foreach ( $translations as $tr_lang => $tr_id ) {
366 335 if ( $tr_id != $object_id ) {
367 336 $this->copy( $object_id, $tr_id, $tr_lang, true );
368 337 }