| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* A class to manage copy and synchronization of post metas |
| 8 |
* |
| 9 |
* @since 2.3 |
| 10 |
*/ |
| 11 |
class PLL_Sync_Post_Metas extends PLL_Sync_Metas { |
| 12 |
public $options; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor |
| 16 |
* |
| 17 |
* @since 2.3 |
| 18 |
* |
| 19 |
* @param object $polylang |
| 20 |
*/ |
| 21 |
public function __construct( &$polylang ) { |
| 22 |
$this->meta_type = 'post'; |
| 23 |
|
| 24 |
parent::__construct( $polylang ); |
| 25 |
|
| 26 |
$this->options = &$polylang->options; |
| 27 |
|
| 28 |
add_filter( 'pll_translate_post_meta', array( $this, 'translate_thumbnail_id' ), 10, 3 ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get the custom fields to copy or synchronize |
| 33 |
* |
| 34 |
* @since 2.3 |
| 35 |
* |
| 36 |
* @param int $from Id of the post from which we copy informations |
| 37 |
* @param int $to Id of the post to which we paste informations |
| 38 |
* @param string $lang Language slug |
| 39 |
* @param bool $sync True if it is synchronization, false if it is a copy |
| 40 |
* @return array List of meta keys |
| 41 |
*/ |
| 42 |
protected function get_metas_to_copy( $from, $to, $lang, $sync = false ) { |
| 43 |
// Copy or synchronize post metas and allow plugins to do the same |
| 44 |
$metas = get_post_custom( $from ); |
| 45 |
$keys = array(); |
| 46 |
|
| 47 |
// Get public meta keys ( including from translated post in case we just deleted a custom field ) |
| 48 |
if ( ! $sync || in_array( 'post_meta', $this->options['sync'] ) ) { |
| 49 |
foreach ( $keys = array_unique( array_merge( array_keys( $metas ), array_keys( get_post_custom( $to ) ) ) ) as $k => $meta_key ) { |
| 50 |
if ( is_protected_meta( $meta_key ) ) { |
| 51 |
unset( $keys[ $k ] ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Add page template and featured image |
| 57 |
foreach ( array( '_wp_page_template', '_thumbnail_id' ) as $meta ) { |
| 58 |
if ( ! $sync || in_array( $meta, $this->options['sync'] ) ) { |
| 59 |
$keys[] = $meta; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if ( $this->options['media_support'] ) { |
| 64 |
$keys[] = '_wp_attached_file'; |
| 65 |
$keys[] = '_wp_attachment_metadata'; |
| 66 |
$keys[] = '_wp_attachment_backup_sizes'; |
| 67 |
$keys[] = '_wp_attachment_is_custom_header'; // Random header image |
| 68 |
} |
| 69 |
|
| 70 |
/** This filter is documented in modules/sync/sync-metas.php */ |
| 71 |
return array_unique( apply_filters( 'pll_copy_post_metas', $keys, $sync, $from, $to, $lang ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Translates the thumbnail id |
| 76 |
* |
| 77 |
* @since 2.3 |
| 78 |
* |
| 79 |
* @param int $value Thumbnail id |
| 80 |
* @param string $key Meta key |
| 81 |
* @param string $lang Language code |
| 82 |
* @return int |
| 83 |
*/ |
| 84 |
public function translate_thumbnail_id( $value, $key, $lang ) { |
| 85 |
return ( $this->options['media_support'] && '_thumbnail_id' === $key && $to_value = $this->model->post->get_translation( $value, $lang ) ) ? $to_value : $value; |
| 86 |
} |
| 87 |
} |
| 88 |
|