PluginProbe
Polylang / 2.5.4
Polylang v2.5.4
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 2.5.4, at modules/sync/sync-post-metas.php

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