| @@ -25,9 +25,9 @@ | ||
| 25 | 25 | * Stores the previous values when updating a meta. |
| 26 | 26 | * |
| 27 | 27 | * @var array |
| 28 | 28 | */ |
| 29 | - protected $prev_value; | |
| 29 | + protected $prev_value = array(); | |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * Stores the metas to synchronize before deleting them. |
| 33 | 33 | * |
| @@ -32,16 +32,16 @@ | ||
| 32 | 32 | * Stores the metas to synchronize before deleting them. |
| 33 | 33 | * |
| 34 | 34 | * @var array |
| 35 | 35 | */ |
| 36 | - protected $to_copy; | |
| 36 | + protected $to_copy = array(); | |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | - * Constructor | |
| 39 | + * Constructor. | |
| 40 | 40 | * |
| 41 | 41 | * @since 2.3 |
| 42 | 42 | * |
| 43 | - * @param object $polylang | |
| 43 | + * @param object $polylang The Polylang object. | |
| 44 | 44 | */ |
| 45 | 45 | public function __construct( &$polylang ) { |
| 46 | 46 | $this->model = &$polylang->model; |
| 47 | 47 | |
| @@ -61,9 +61,9 @@ | ||
| 61 | 61 | * |
| 62 | 62 | * @return void |
| 63 | 63 | */ |
| 64 | 64 | protected function remove_add_meta_action() { |
| 65 | - 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' ) ); | |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * Removes all meta synchronization actions and filters |
| @@ -71,16 +71,16 @@ | ||
| 71 | 71 | * @since 2.3 |
| 72 | 72 | * |
| 73 | 73 | * @return void |
| 74 | 74 | */ |
| 75 | - protected function remove_all_meta_actions() { | |
| 75 | + public function remove_all_meta_actions() { | |
| 76 | 76 | $this->remove_add_meta_action(); |
| 77 | 77 | |
| 78 | - remove_filter( "update_{$this->meta_type}_metadata", array( $this, 'update_metadata' ), 999, 5 ); | |
| 79 | - 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' ) ); | |
| 80 | 80 | |
| 81 | - remove_action( "delete_{$this->meta_type}_meta", array( $this, 'store_metas_to_sync' ), 10, 2 ); | |
| 82 | - 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' ) ); | |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
| 86 | 86 | * Adds "added_{$this->meta_type}_meta" action |
| @@ -99,9 +99,9 @@ | ||
| 99 | 99 | * @since 2.3 |
| 100 | 100 | * |
| 101 | 101 | * @return void |
| 102 | 102 | */ |
| 103 | - protected function add_all_meta_actions() { | |
| 103 | + public function add_all_meta_actions() { | |
| 104 | 104 | $this->restore_add_meta_action(); |
| 105 | 105 | |
| 106 | 106 | add_filter( "update_{$this->meta_type}_metadata", array( $this, 'update_metadata' ), 999, 5 ); // Very late in case a filter prevents the meta to be updated |
| 107 | 107 | add_action( "update_{$this->meta_type}_meta", array( $this, 'update_meta' ), 10, 4 ); |
| @@ -133,9 +133,9 @@ | ||
| 133 | 133 | * @param string $lang Language of target |
| 134 | 134 | * @param int $from Id of the source |
| 135 | 135 | * @param int $to Id of the target |
| 136 | 136 | */ |
| 137 | - return apply_filters( "pll_translate_{$this->meta_type}_meta", maybe_unserialize( $value ), $key, $lang, $from, $to ); | |
| 137 | + return apply_filters( "pll_translate_{$this->meta_type}_meta", $value, $key, $lang, $from, $to ); | |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | 141 | * Get the custom fields to copy or synchronize. |
| @@ -141,10 +141,10 @@ | ||
| 141 | 141 | * Get the custom fields to copy or synchronize. |
| 142 | 142 | * |
| 143 | 143 | * @since 2.3 |
| 144 | 144 | * |
| 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. | |
| 145 | + * @param int $from Id of the post from which we copy information. | |
| 146 | + * @param int $to Id of the post to which we paste information. | |
| 147 | 147 | * @param string $lang Language slug. |
| 148 | 148 | * @param bool $sync True if it is synchronization, false if it is a copy. |
| 149 | 149 | * @return string[] List of meta keys. |
| 150 | 150 | */ |
| @@ -156,10 +156,10 @@ | ||
| 156 | 156 | * @since 1.9.2 The `$from`, `$to`, `$lang` parameters were added. |
| 157 | 157 | * |
| 158 | 158 | * @param string[] $keys List of custom fields names. |
| 159 | 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. | |
| 160 | + * @param int $from Id of the post from which we copy information. | |
| 161 | + * @param int $to Id of the post to which we paste information. | |
| 162 | 162 | * @param string $lang Language slug. |
| 163 | 163 | */ |
| 164 | 164 | return array_unique( apply_filters( "pll_copy_{$this->meta_type}_metas", array(), $sync, $from, $to, $lang ) ); |
| 165 | 165 | } |
| @@ -254,8 +254,9 @@ | ||
| 254 | 254 | * @return void |
| 255 | 255 | */ |
| 256 | 256 | public function update_meta( $mid, $id, $meta_key, $meta_value ) { |
| 257 | 257 | static $avoid_recursion = false; |
| 258 | + $id = (int) $id; | |
| 258 | 259 | |
| 259 | 260 | if ( ! $avoid_recursion ) { |
| 260 | 261 | $avoid_recursion = true; |
| 261 | 262 | $hash = md5( "$id|$meta_key|" . maybe_serialize( $meta_value ) ); |
| @@ -347,14 +348,13 @@ | ||
| 347 | 348 | */ |
| 348 | 349 | public function copy( $from, $to, $lang, $sync = false ) { |
| 349 | 350 | $this->remove_all_meta_actions(); |
| 350 | 351 | |
| 351 | - remove_action( "delete_{$this->meta_type}_meta", array( $this, 'store_metas_to_sync' ), 10, 2 ); | |
| 352 | - remove_action( "deleted_{$this->meta_type}_meta", array( $this, 'delete_meta' ), 10, 4 ); | |
| 353 | - | |
| 354 | - $to_copy = $this->get_metas_to_copy( $from, $to, $lang, $sync ); | |
| 355 | - $metas = get_metadata( $this->meta_type, $from ); | |
| 352 | + $to_copy = $this->get_metas_to_copy( $from, $to, $lang, $sync ); | |
| 353 | + $metas = get_metadata( $this->meta_type, $from ); | |
| 354 | + $metas = is_array( $metas ) ? $metas : array(); | |
| 356 | 355 | $tr_metas = get_metadata( $this->meta_type, $to ); |
| 356 | + $tr_metas = is_array( $tr_metas ) ? $tr_metas : array(); | |
| 357 | 357 | |
| 358 | 358 | foreach ( $to_copy as $key ) { |
| 359 | 359 | if ( empty( $metas[ $key ] ) ) { |
| 360 | 360 | if ( ! empty( $tr_metas[ $key ] ) ) { |
| @@ -360,27 +360,25 @@ | ||
| 360 | 360 | if ( ! empty( $tr_metas[ $key ] ) ) { |
| 361 | 361 | // If the meta key is not present in the source object, delete all values |
| 362 | 362 | delete_metadata( $this->meta_type, $to, wp_slash( $key ) ); |
| 363 | 363 | } |
| 364 | + } elseif ( ! empty( $tr_metas[ $key ] ) && 1 === count( $metas[ $key ] ) && 1 === count( $tr_metas[ $key ] ) ) { | |
| 365 | + // One custom field to update | |
| 366 | + $value = reset( $metas[ $key ] ); | |
| 367 | + $value = maybe_unserialize( $value ); | |
| 368 | + $to_value = $this->maybe_translate_value( $value, $key, $from, $to, $lang ); | |
| 369 | + update_metadata( $this->meta_type, $to, wp_slash( $key ), is_object( $to_value ) ? $to_value : wp_slash( $to_value ) ); | |
| 364 | 370 | } else { |
| 365 | - if ( ! empty( $tr_metas[ $key ] ) && 1 === count( $metas[ $key ] ) && 1 === count( $tr_metas[ $key ] ) ) { | |
| 366 | - // One custom field to update | |
| 367 | - $value = reset( $metas[ $key ] ); | |
| 371 | + // Multiple custom fields, either in the source or the target | |
| 372 | + if ( ! empty( $tr_metas[ $key ] ) ) { | |
| 373 | + // The synchronization of multiple values custom fields is easier if we delete all metas first | |
| 374 | + delete_metadata( $this->meta_type, $to, wp_slash( $key ) ); | |
| 375 | + } | |
| 376 | + | |
| 377 | + foreach ( $metas[ $key ] as $value ) { | |
| 368 | 378 | $value = maybe_unserialize( $value ); |
| 369 | 379 | $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 ) ); | |
| 371 | - } else { | |
| 372 | - // Multiple custom fields, either in the source or the target | |
| 373 | - if ( ! empty( $tr_metas[ $key ] ) ) { | |
| 374 | - // 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 ) ); | |
| 376 | - } | |
| 377 | - | |
| 378 | - foreach ( $metas[ $key ] as $value ) { | |
| 379 | - $value = maybe_unserialize( $value ); | |
| 380 | - $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 ) ); | |
| 382 | - } | |
| 380 | + add_metadata( $this->meta_type, $to, wp_slash( $key ), is_object( $to_value ) ? $to_value : wp_slash( $to_value ) ); | |
| 383 | 381 | } |
| 384 | 382 | } |
| 385 | 383 | } |
| 386 | 384 | |