| @@ -8,12 +8,35 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @since 2.3 |
| 10 | 10 | */ |
| 11 | 11 | abstract class PLL_Sync_Metas { |
| 12 | + /** | |
| 13 | + * @var PLL_Model | |
| 14 | + */ | |
| 12 | 15 | public $model; |
| 13 | - protected $meta_type, $prev_value, $to_copy; | |
| 14 | 16 | |
| 15 | 17 | /** |
| 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 = array(); | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Stores the metas to synchronize before deleting them. | |
| 33 | + * | |
| 34 | + * @var array | |
| 35 | + */ | |
| 36 | + protected $to_copy = array(); | |
| 37 | + | |
| 38 | + /** | |
| 16 | 39 | * Constructor |
| 17 | 40 | * |
| 18 | 41 | * @since 2.3 |
| 19 | 42 | * |
| @@ -34,11 +57,13 @@ | ||
| 34 | 57 | /** |
| 35 | 58 | * Removes "added_{$this->meta_type}_meta" action |
| 36 | 59 | * |
| 37 | 60 | * @since 2.3 |
| 61 | + * | |
| 62 | + * @return void | |
| 38 | 63 | */ |
| 39 | 64 | protected function remove_add_meta_action() { |
| 40 | - remove_action( "added_{$this->meta_type}_meta", array( $this, 'add_meta' ), 10, 4 ); | |
| 65 | + remove_action( "added_{$this->meta_type}_meta", array( $this, 'add_meta' ) ); | |
| 41 | 66 | } |
| 42 | 67 | |
| 43 | 68 | /** |
| 44 | 69 | * Removes all meta synchronization actions and filters |
| @@ -43,17 +68,19 @@ | ||
| 43 | 68 | /** |
| 44 | 69 | * Removes all meta synchronization actions and filters |
| 45 | 70 | * |
| 46 | 71 | * @since 2.3 |
| 72 | + * | |
| 73 | + * @return void | |
| 47 | 74 | */ |
| 48 | 75 | protected function remove_all_meta_actions() { |
| 49 | 76 | $this->remove_add_meta_action(); |
| 50 | 77 | |
| 51 | - remove_filter( "update_{$this->meta_type}_metadata", array( $this, 'update_metadata' ), 999, 5 ); | |
| 52 | - remove_action( "update_{$this->meta_type}_meta", array( $this, 'update_meta' ), 10, 4 ); | |
| 78 | + remove_filter( "update_{$this->meta_type}_metadata", array( $this, 'update_metadata' ), 999 ); | |
| 79 | + remove_action( "update_{$this->meta_type}_meta", array( $this, 'update_meta' ) ); | |
| 53 | 80 | |
| 54 | - remove_action( "delete_{$this->meta_type}_meta", array( $this, 'store_metas_to_sync' ), 10, 2 ); | |
| 55 | - remove_action( "deleted_{$this->meta_type}_meta", array( $this, 'delete_meta' ), 10, 4 ); | |
| 81 | + remove_action( "delete_{$this->meta_type}_meta", array( $this, 'store_metas_to_sync' ) ); | |
| 82 | + remove_action( "deleted_{$this->meta_type}_meta", array( $this, 'delete_meta' ) ); | |
| 56 | 83 | } |
| 57 | 84 | |
| 58 | 85 | /** |
| 59 | 86 | * Adds "added_{$this->meta_type}_meta" action |
| @@ -58,8 +85,10 @@ | ||
| 58 | 85 | /** |
| 59 | 86 | * Adds "added_{$this->meta_type}_meta" action |
| 60 | 87 | * |
| 61 | 88 | * @since 2.3 |
| 89 | + * | |
| 90 | + * @return void | |
| 62 | 91 | */ |
| 63 | 92 | protected function restore_add_meta_action() { |
| 64 | 93 | add_action( "added_{$this->meta_type}_meta", array( $this, 'add_meta' ), 10, 4 ); |
| 65 | 94 | } |
| @@ -67,8 +96,10 @@ | ||
| 67 | 96 | /** |
| 68 | 97 | * Adds meta synchronization actions and filters |
| 69 | 98 | * |
| 70 | 99 | * @since 2.3 |
| 100 | + * | |
| 101 | + * @return void | |
| 71 | 102 | */ |
| 72 | 103 | protected function add_all_meta_actions() { |
| 73 | 104 | $this->restore_add_meta_action(); |
| 74 | 105 | |
| @@ -106,30 +137,30 @@ | ||
| 106 | 137 | return apply_filters( "pll_translate_{$this->meta_type}_meta", maybe_unserialize( $value ), $key, $lang, $from, $to ); |
| 107 | 138 | } |
| 108 | 139 | |
| 109 | 140 | /** |
| 110 | - * Get the custom fields to copy or synchronize | |
| 141 | + * Get the custom fields to copy or synchronize. | |
| 111 | 142 | * |
| 112 | 143 | * @since 2.3 |
| 113 | 144 | * |
| 114 | - * @param int $from Id of the post from which we copy informations | |
| 115 | - * @param int $to Id of the post to which we paste informations | |
| 116 | - * @param string $lang Language slug | |
| 117 | - * @param bool $sync True if it is synchronization, false if it is a copy | |
| 118 | - * @return array List of meta keys | |
| 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. | |
| 119 | 150 | */ |
| 120 | 151 | protected function get_metas_to_copy( $from, $to, $lang, $sync = false ) { |
| 121 | 152 | /** |
| 122 | - * Filter the custom fields to copy or synchronize | |
| 153 | + * Filters the custom fields to copy or synchronize. | |
| 123 | 154 | * |
| 124 | 155 | * @since 0.6 |
| 125 | 156 | * @since 1.9.2 The `$from`, `$to`, `$lang` parameters were added. |
| 126 | 157 | * |
| 127 | - * @param array $keys List of custom fields names | |
| 128 | - * @param bool $sync True if it is synchronization, false if it is a copy | |
| 129 | - * @param int $from Id of the post from which we copy informations | |
| 130 | - * @param int $to Id of the post to which we paste informations | |
| 131 | - * @param string $lang Language slug | |
| 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. | |
| 132 | 163 | */ |
| 133 | 164 | return array_unique( apply_filters( "pll_copy_{$this->meta_type}_metas", array(), $sync, $from, $to, $lang ) ); |
| 134 | 165 | } |
| 135 | 166 | |
| @@ -167,8 +198,9 @@ | ||
| 167 | 198 | * @param int $mid Meta id. |
| 168 | 199 | * @param int $id Object ID. |
| 169 | 200 | * @param string $meta_key Meta key. |
| 170 | 201 | * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
| 202 | + * @return void | |
| 171 | 203 | */ |
| 172 | 204 | public function add_meta( $mid, $id, $meta_key, $meta_value ) { |
| 173 | 205 | static $avoid_recursion = false; |
| 174 | 206 | |
| @@ -218,11 +250,13 @@ | ||
| 218 | 250 | * @param int $mid Meta id. |
| 219 | 251 | * @param int $id Object ID. |
| 220 | 252 | * @param string $meta_key Meta key. |
| 221 | 253 | * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
| 254 | + * @return void | |
| 222 | 255 | */ |
| 223 | 256 | public function update_meta( $mid, $id, $meta_key, $meta_value ) { |
| 224 | 257 | static $avoid_recursion = false; |
| 258 | + $id = (int) $id; | |
| 225 | 259 | |
| 226 | 260 | if ( ! $avoid_recursion ) { |
| 227 | 261 | $avoid_recursion = true; |
| 228 | 262 | $hash = md5( "$id|$meta_key|" . maybe_serialize( $meta_value ) ); |
| @@ -250,14 +284,15 @@ | ||
| 250 | 284 | } |
| 251 | 285 | } |
| 252 | 286 | |
| 253 | 287 | /** |
| 254 | - * Store metas to synchronize before deleting them | |
| 288 | + * Store metas to synchronize before deleting them. | |
| 255 | 289 | * |
| 256 | 290 | * @since 2.3 |
| 257 | 291 | * |
| 258 | - * @param array $mids Not used | |
| 292 | + * @param int[] $mids Not used. | |
| 259 | 293 | * @param int $id Object ID. |
| 294 | + * @return void | |
| 260 | 295 | */ |
| 261 | 296 | public function store_metas_to_sync( $mids, $id ) { |
| 262 | 297 | $tr_ids = $this->model->{$this->meta_type}->get_translations( $id ); |
| 263 | 298 | |
| @@ -266,16 +301,17 @@ | ||
| 266 | 301 | } |
| 267 | 302 | } |
| 268 | 303 | |
| 269 | 304 | /** |
| 270 | - * Synchronize deleted meta across translations | |
| 305 | + * Synchronizes deleted meta across translations. | |
| 271 | 306 | * |
| 272 | 307 | * @since 2.3 |
| 273 | 308 | * |
| 274 | - * @param array $mids Not used | |
| 309 | + * @param int[] $mids Not used. | |
| 275 | 310 | * @param int $id Object ID. |
| 276 | 311 | * @param string $key Meta key. |
| 277 | 312 | * @param mixed $value Meta value. |
| 313 | + * @return void | |
| 278 | 314 | */ |
| 279 | 315 | public function delete_meta( $mids, $id, $key, $value ) { |
| 280 | 316 | static $avoid_recursion = false; |
| 281 | 317 | |
| @@ -307,15 +343,13 @@ | ||
| 307 | 343 | * @param int $from Id of the source object |
| 308 | 344 | * @param int $to Id of the target object |
| 309 | 345 | * @param string $lang Language code of the target object |
| 310 | 346 | * @param bool $sync Optional, defaults to true. True if it is synchronization, false if it is a copy |
| 347 | + * @return void | |
| 311 | 348 | */ |
| 312 | 349 | public function copy( $from, $to, $lang, $sync = false ) { |
| 313 | 350 | $this->remove_all_meta_actions(); |
| 314 | 351 | |
| 315 | - remove_action( "delete_{$this->meta_type}_meta", array( $this, 'store_metas_to_sync' ), 10, 2 ); | |
| 316 | - remove_action( "deleted_{$this->meta_type}_meta", array( $this, 'delete_meta' ), 10, 4 ); | |
| 317 | - | |
| 318 | 352 | $to_copy = $this->get_metas_to_copy( $from, $to, $lang, $sync ); |
| 319 | 353 | $metas = get_metadata( $this->meta_type, $from ); |
| 320 | 354 | $tr_metas = get_metadata( $this->meta_type, $to ); |
| 321 | 355 | |
| @@ -356,11 +390,12 @@ | ||
| 356 | 390 | * that saving a post (or term) will synchronize them. |
| 357 | 391 | * |
| 358 | 392 | * @since 2.3 |
| 359 | 393 | * |
| 360 | - * @param int $object_id Id of the object being asaved | |
| 361 | - * @param object $obj Not used | |
| 362 | - * @param array $translations The list of translations object ids | |
| 394 | + * @param int $object_id Id of the object being saved. | |
| 395 | + * @param object $obj Not used. | |
| 396 | + * @param int[] $translations The list of translations object ids. | |
| 397 | + * @return void | |
| 363 | 398 | */ |
| 364 | 399 | public function save_object( $object_id, $obj, $translations ) { |
| 365 | 400 | foreach ( $translations as $tr_lang => $tr_id ) { |
| 366 | 401 | if ( $tr_id != $object_id ) { |