PluginProbe
Polylang / 3.0.5
Polylang v3.0.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
polylang / modules / sync / sync-post-metas.php

sync-post-metas.php in Polylang 3.0.5, at modules/sync/sync-post-metas.php

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