| @@ -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 | * |
| @@ -8,35 +5,12 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 2.3 |
| 10 | 7 | */ |
| 11 | 8 | abstract class PLL_Sync_Metas { |
| 12 | - /** | |
| 13 | - * @var PLL_Model | |
| 14 | - */ | |
| 15 | 9 | public $model; |
| 10 | + protected $meta_type, $prev_value, $to_copy; | |
| 16 | 11 | |
| 17 | 12 | /** |
| 18 | - * Meta type. Typically 'post' or 'term'. | |
| 19 | - * | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - protected $meta_type; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Stores the previous values when updating a meta. | |
| 26 | - * | |
| 27 | - * @var array | |
| 28 | - */ | |
| 29 | - protected $prev_value; | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Stores the metas to synchronize before deleting them. | |
| 33 | - * | |
| 34 | - * @var array | |
| 35 | - */ | |
| 36 | - protected $to_copy; | |
| 37 | - | |
| 38 | - /** | |
| 39 | 13 | * Constructor |
| 40 | 14 | * |
| 41 | 15 | * @since 2.3 |
| 42 | 16 | * |
| @@ -57,10 +31,8 @@ | ||
| 57 | 31 | /** |
| 58 | 32 | * Removes "added_{$this->meta_type}_meta" action |
| 59 | 33 | * |
| 60 | 34 | * @since 2.3 |
| 61 | - * | |
| 62 | - * @return void | |
| 63 | 35 | */ |
| 64 | 36 | protected function remove_add_meta_action() { |
| 65 | 37 | remove_action( "added_{$this->meta_type}_meta", array( $this, 'add_meta' ), 10, 4 ); |
| 66 | 38 | } |
| @@ -68,10 +40,8 @@ | ||
| 68 | 40 | /** |
| 69 | 41 | * Removes all meta synchronization actions and filters |
| 70 | 42 | * |
| 71 | 43 | * @since 2.3 |
| 72 | - * | |
| 73 | - * @return void | |
| 74 | 44 | */ |
| 75 | 45 | protected function remove_all_meta_actions() { |
| 76 | 46 | $this->remove_add_meta_action(); |
| 77 | 47 | |
| @@ -85,10 +55,8 @@ | ||
| 85 | 55 | /** |
| 86 | 56 | * Adds "added_{$this->meta_type}_meta" action |
| 87 | 57 | * |
| 88 | 58 | * @since 2.3 |
| 89 | - * | |
| 90 | - * @return void | |
| 91 | 59 | */ |
| 92 | 60 | protected function restore_add_meta_action() { |
| 93 | 61 | add_action( "added_{$this->meta_type}_meta", array( $this, 'add_meta' ), 10, 4 ); |
| 94 | 62 | } |
| @@ -96,10 +64,8 @@ | ||
| 96 | 64 | /** |
| 97 | 65 | * Adds meta synchronization actions and filters |
| 98 | 66 | * |
| 99 | 67 | * @since 2.3 |
| 100 | - * | |
| 101 | - * @return void | |
| 102 | 68 | */ |
| 103 | 69 | protected function add_all_meta_actions() { |
| 104 | 70 | $this->restore_add_meta_action(); |
| 105 | 71 | |
| @@ -137,30 +103,30 @@ | ||
| 137 | 103 | return apply_filters( "pll_translate_{$this->meta_type}_meta", maybe_unserialize( $value ), $key, $lang, $from, $to ); |
| 138 | 104 | } |
| 139 | 105 | |
| 140 | 106 | /** |
| 141 | - * Get the custom fields to copy or synchronize. | |
| 107 | + * Get the custom fields to copy or synchronize | |
| 142 | 108 | * |
| 143 | 109 | * @since 2.3 |
| 144 | 110 | * |
| 145 | - * @param int $from Id of the post from which we copy informations. | |
| 146 | - * @param int $to Id of the post to which we paste informations. | |
| 147 | - * @param string $lang Language slug. | |
| 148 | - * @param bool $sync True if it is synchronization, false if it is a copy. | |
| 149 | - * @return string[] List of meta keys. | |
| 111 | + * @param int $from Id of the post from which we copy informations | |
| 112 | + * @param int $to Id of the post to which we paste informations | |
| 113 | + * @param string $lang Language slug | |
| 114 | + * @param bool $sync True if it is synchronization, false if it is a copy | |
| 115 | + * @return array List of meta keys | |
| 150 | 116 | */ |
| 151 | 117 | protected function get_metas_to_copy( $from, $to, $lang, $sync = false ) { |
| 152 | 118 | /** |
| 153 | - * Filters the custom fields to copy or synchronize. | |
| 119 | + * Filter the custom fields to copy or synchronize | |
| 154 | 120 | * |
| 155 | 121 | * @since 0.6 |
| 156 | 122 | * @since 1.9.2 The `$from`, `$to`, `$lang` parameters were added. |
| 157 | 123 | * |
| 158 | - * @param string[] $keys List of custom fields names. | |
| 159 | - * @param bool $sync True if it is synchronization, false if it is a copy. | |
| 160 | - * @param int $from Id of the post from which we copy informations. | |
| 161 | - * @param int $to Id of the post to which we paste informations. | |
| 162 | - * @param string $lang Language slug. | |
| 124 | + * @param array $keys List of custom fields names | |
| 125 | + * @param bool $sync True if it is synchronization, false if it is a copy | |
| 126 | + * @param int $from Id of the post from which we copy informations | |
| 127 | + * @param int $to Id of the post to which we paste informations | |
| 128 | + * @param string $lang Language slug | |
| 163 | 129 | */ |
| 164 | 130 | return array_unique( apply_filters( "pll_copy_{$this->meta_type}_metas", array(), $sync, $from, $to, $lang ) ); |
| 165 | 131 | } |
| 166 | 132 | |
| @@ -198,9 +164,8 @@ | ||
| 198 | 164 | * @param int $mid Meta id. |
| 199 | 165 | * @param int $id Object ID. |
| 200 | 166 | * @param string $meta_key Meta key. |
| 201 | 167 | * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
| 202 | - * @return void | |
| 203 | 168 | */ |
| 204 | 169 | public function add_meta( $mid, $id, $meta_key, $meta_value ) { |
| 205 | 170 | static $avoid_recursion = false; |
| 206 | 171 | |
| @@ -212,9 +177,9 @@ | ||
| 212 | 177 | if ( $tr_id != $id ) { |
| 213 | 178 | $to_copy = $this->get_metas_to_copy( $id, $tr_id, $lang, true ); |
| 214 | 179 | if ( in_array( $meta_key, $to_copy ) ) { |
| 215 | 180 | $meta_value = $this->maybe_translate_value( $meta_value, $meta_key, $id, $tr_id, $lang ); |
| 216 | - add_metadata( $this->meta_type, $tr_id, wp_slash( $meta_key ), is_object( $meta_value ) ? $meta_value : wp_slash( $meta_value ) ); | |
| 181 | + add_metadata( $this->meta_type, $tr_id, $meta_key, wp_slash( $meta_value ) ); | |
| 217 | 182 | } |
| 218 | 183 | } |
| 219 | 184 | } |
| 220 | 185 | |
| @@ -250,9 +215,8 @@ | ||
| 250 | 215 | * @param int $mid Meta id. |
| 251 | 216 | * @param int $id Object ID. |
| 252 | 217 | * @param string $meta_key Meta key. |
| 253 | 218 | * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
| 254 | - * @return void | |
| 255 | 219 | */ |
| 256 | 220 | public function update_meta( $mid, $id, $meta_key, $meta_value ) { |
| 257 | 221 | static $avoid_recursion = false; |
| 258 | 222 | |
| @@ -270,9 +234,9 @@ | ||
| 270 | 234 | if ( $tr_id != $id && in_array( $meta_key, $this->get_metas_to_copy( $id, $tr_id, $lang, true ) ) ) { |
| 271 | 235 | if ( empty( $this->prev_value[ $hash ] ) || $this->prev_value[ $hash ] === $prev_meta->meta_value ) { |
| 272 | 236 | $prev_value = $this->maybe_translate_value( $prev_meta->meta_value, $meta_key, $id, $tr_id, $lang ); |
| 273 | 237 | $meta_value = $this->maybe_translate_value( $meta_value, $meta_key, $id, $tr_id, $lang ); |
| 274 | - update_metadata( $this->meta_type, $tr_id, wp_slash( $meta_key ), is_object( $meta_value ) ? $meta_value : wp_slash( $meta_value ), $prev_value ); | |
| 238 | + update_metadata( $this->meta_type, $tr_id, $meta_key, wp_slash( $meta_value ), $prev_value ); | |
| 275 | 239 | } |
| 276 | 240 | } |
| 277 | 241 | } |
| 278 | 242 | $this->restore_add_meta_action(); |
| @@ -283,15 +247,14 @@ | ||
| 283 | 247 | } |
| 284 | 248 | } |
| 285 | 249 | |
| 286 | 250 | /** |
| 287 | - * Store metas to synchronize before deleting them. | |
| 251 | + * Store metas to synchronize before deleting them | |
| 288 | 252 | * |
| 289 | 253 | * @since 2.3 |
| 290 | 254 | * |
| 291 | - * @param int[] $mids Not used. | |
| 255 | + * @param array $mids Not used | |
| 292 | 256 | * @param int $id Object ID. |
| 293 | - * @return void | |
| 294 | 257 | */ |
| 295 | 258 | public function store_metas_to_sync( $mids, $id ) { |
| 296 | 259 | $tr_ids = $this->model->{$this->meta_type}->get_translations( $id ); |
| 297 | 260 | |
| @@ -300,17 +263,16 @@ | ||
| 300 | 263 | } |
| 301 | 264 | } |
| 302 | 265 | |
| 303 | 266 | /** |
| 304 | - * Synchronizes deleted meta across translations. | |
| 267 | + * Synchronize deleted meta across translations | |
| 305 | 268 | * |
| 306 | 269 | * @since 2.3 |
| 307 | 270 | * |
| 308 | - * @param int[] $mids Not used. | |
| 271 | + * @param array $mids Not used | |
| 309 | 272 | * @param int $id Object ID. |
| 310 | 273 | * @param string $key Meta key. |
| 311 | 274 | * @param mixed $value Meta value. |
| 312 | - * @return void | |
| 313 | 275 | */ |
| 314 | 276 | public function delete_meta( $mids, $id, $key, $value ) { |
| 315 | 277 | static $avoid_recursion = false; |
| 316 | 278 | |
| @@ -324,9 +286,9 @@ | ||
| 324 | 286 | if ( in_array( $key, $this->to_copy[ $id ][ $tr_id ] ) ) { |
| 325 | 287 | if ( '' !== $value && null !== $value && false !== $value ) { // Same test as WP |
| 326 | 288 | $value = $this->maybe_translate_value( $value, $key, $id, $tr_id, $lang ); |
| 327 | 289 | } |
| 328 | - delete_metadata( $this->meta_type, $tr_id, wp_slash( $key ), is_object( $value ) ? $value : wp_slash( $value ) ); | |
| 290 | + delete_metadata( $this->meta_type, $tr_id, $key, wp_slash( $value ) ); | |
| 329 | 291 | } |
| 330 | 292 | } |
| 331 | 293 | } |
| 332 | 294 | } |
| @@ -342,9 +304,8 @@ | ||
| 342 | 304 | * @param int $from Id of the source object |
| 343 | 305 | * @param int $to Id of the target object |
| 344 | 306 | * @param string $lang Language code of the target object |
| 345 | 307 | * @param bool $sync Optional, defaults to true. True if it is synchronization, false if it is a copy |
| 346 | - * @return void | |
| 347 | 308 | */ |
| 348 | 309 | public function copy( $from, $to, $lang, $sync = false ) { |
| 349 | 310 | $this->remove_all_meta_actions(); |
| 350 | 311 | |
| @@ -358,9 +319,9 @@ | ||
| 358 | 319 | foreach ( $to_copy as $key ) { |
| 359 | 320 | if ( empty( $metas[ $key ] ) ) { |
| 360 | 321 | if ( ! empty( $tr_metas[ $key ] ) ) { |
| 361 | 322 | // If the meta key is not present in the source object, delete all values |
| 362 | - delete_metadata( $this->meta_type, $to, wp_slash( $key ) ); | |
| 323 | + delete_metadata( $this->meta_type, $to, $key ); | |
| 363 | 324 | } |
| 364 | 325 | } else { |
| 365 | 326 | if ( ! empty( $tr_metas[ $key ] ) && 1 === count( $metas[ $key ] ) && 1 === count( $tr_metas[ $key ] ) ) { |
| 366 | 327 | // One custom field to update |
| @@ -366,20 +327,20 @@ | ||
| 366 | 327 | // One custom field to update |
| 367 | 328 | $value = reset( $metas[ $key ] ); |
| 368 | 329 | $value = maybe_unserialize( $value ); |
| 369 | 330 | $to_value = $this->maybe_translate_value( $value, $key, $from, $to, $lang ); |
| 370 | - update_metadata( $this->meta_type, $to, wp_slash( $key ), is_object( $to_value ) ? $to_value : wp_slash( $to_value ) ); | |
| 331 | + update_metadata( $this->meta_type, $to, $key, wp_slash( $to_value ) ); | |
| 371 | 332 | } else { |
| 372 | 333 | // Multiple custom fields, either in the source or the target |
| 373 | 334 | if ( ! empty( $tr_metas[ $key ] ) ) { |
| 374 | 335 | // The synchronization of multiple values custom fields is easier if we delete all metas first |
| 375 | - delete_metadata( $this->meta_type, $to, wp_slash( $key ) ); | |
| 336 | + delete_metadata( $this->meta_type, $to, $key ); | |
| 376 | 337 | } |
| 377 | 338 | |
| 378 | 339 | foreach ( $metas[ $key ] as $value ) { |
| 379 | 340 | $value = maybe_unserialize( $value ); |
| 380 | 341 | $to_value = $this->maybe_translate_value( $value, $key, $from, $to, $lang ); |
| 381 | - add_metadata( $this->meta_type, $to, wp_slash( $key ), is_object( $to_value ) ? $to_value : wp_slash( $to_value ) ); | |
| 342 | + add_metadata( $this->meta_type, $to, $key, wp_slash( $to_value ) ); | |
| 382 | 343 | } |
| 383 | 344 | } |
| 384 | 345 | } |
| 385 | 346 | } |
| @@ -392,14 +353,15 @@ | ||
| 392 | 353 | * that saving a post (or term) will synchronize them. |
| 393 | 354 | * |
| 394 | 355 | * @since 2.3 |
| 395 | 356 | * |
| 396 | - * @param int $object_id Id of the object being saved. | |
| 397 | - * @param object $obj Not used. | |
| 398 | - * @param int[] $translations The list of translations object ids. | |
| 399 | - * @return void | |
| 357 | + * @param int $object_id Id of the object being asaved | |
| 358 | + * @param object $obj Not used | |
| 359 | + * @param array $translations The list of translations object ids | |
| 400 | 360 | */ |
| 401 | 361 | public function save_object( $object_id, $obj, $translations ) { |
| 362 | + $src_lang = array_search( $object_id, $translations ); | |
| 363 | + | |
| 402 | 364 | foreach ( $translations as $tr_lang => $tr_id ) { |
| 403 | 365 | if ( $tr_id != $object_id ) { |
| 404 | 366 | $this->copy( $object_id, $tr_id, $tr_lang, true ); |
| 405 | 367 | } |