PluginProbe
Polylang / 3.3.2
Polylang v3.3.2
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.3.2, at modules/sync/sync-post-metas.php

95 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 $keys = array();
49
50 // Get public meta keys ( including from translated post in case we just deleted a custom field ).
51 if ( ! $sync || in_array( 'post_meta', $this->options['sync'] ) ) {
52 $from_keys = (array) get_post_custom_keys( $from );
53 $to_keys = (array) get_post_custom_keys( $to );
54
55 $keys = array_unique( array_merge( $from_keys, $to_keys ) );
56 foreach ( $keys as $k => $meta_key ) {
57 if ( is_protected_meta( $meta_key ) ) {
58 unset( $keys[ $k ] );
59 }
60 }
61 }
62
63 // Add page template and featured image.
64 foreach ( array( '_wp_page_template', '_thumbnail_id' ) as $meta ) {
65 if ( ! $sync || in_array( $meta, $this->options['sync'] ) ) {
66 $keys[] = $meta;
67 }
68 }
69
70 if ( $this->options['media_support'] ) {
71 $keys[] = '_wp_attached_file';
72 $keys[] = '_wp_attachment_metadata';
73 $keys[] = '_wp_attachment_backup_sizes';
74 $keys[] = '_wp_attachment_is_custom_header'; // Random header image
75 }
76
77 /** This filter is documented in modules/sync/sync-metas.php */
78 return array_unique( apply_filters( 'pll_copy_post_metas', $keys, $sync, $from, $to, $lang ) );
79 }
80
81 /**
82 * Translates the thumbnail id
83 *
84 * @since 2.3
85 *
86 * @param int $value Thumbnail id
87 * @param string $key Meta key
88 * @param string $lang Language code
89 * @return int
90 */
91 public function translate_thumbnail_id( $value, $key, $lang ) {
92 return ( $this->options['media_support'] && '_thumbnail_id' === $key && $to_value = $this->model->post->get_translation( $value, $lang ) ) ? $to_value : $value;
93 }
94 }
95