PluginProbe
Polylang / 2.5
Polylang v2.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 / admin-sync.php

admin-sync.php in Polylang 2.5, at modules/sync/admin-sync.php

200 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages copy and synchronization of terms and post metas
5 *
6 * @since 1.2
7 */
8 class PLL_Admin_Sync extends PLL_Sync {
9
10 /**
11 * Constructor
12 *
13 * @since 1.2
14 *
15 * @param object $polylang
16 */
17 public function __construct( &$polylang ) {
18 parent::__construct( $polylang );
19
20 add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 );
21 add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) );
22 add_action( 'rest_api_init', array( $this, 'new_post_translation' ) ); // Block editor
23 add_action( 'add_meta_boxes', array( $this, 'new_post_translation' ), 5 ); // Classic editor, before Types which populates custom fields in same hook with priority 10
24 }
25
26 /**
27 * Translate post parent if exists when using "Add new" ( translation )
28 *
29 * @since 0.6
30 *
31 * @param int $post_parent Post parent ID
32 * @param int $post_id Post ID, unused
33 * @param array $postarr Array of parsed post data
34 * @return int
35 */
36 public function wp_insert_post_parent( $post_parent, $post_id, $postarr ) {
37 // Make sure not to impact media translations created at the same time
38 return isset( $_GET['from_post'], $_GET['new_lang'], $_GET['post_type'] ) && $_GET['post_type'] === $postarr['post_type'] && ( $id = wp_get_post_parent_id( (int) $_GET['from_post'] ) ) && ( $parent = $this->model->post->get_translation( $id, $_GET['new_lang'] ) ) ? $parent : $post_parent;
39 }
40
41 /**
42 * Copy menu order, comment, ping status and optionally the date when creating a new tanslation
43 *
44 * @since 2.5
45 *
46 * @param array $data An array of slashed post data.
47 * @return array
48 */
49 public function wp_insert_post_data( $data ) {
50 if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $data['post_type'] ) ) {
51 $from_post_id = (int) $_GET['from_post'];
52 $from_post = get_post( $from_post_id );
53
54 foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
55 $data[ $property ] = $from_post->$property;
56 }
57
58 // Copy the date only if the synchronization is activated
59 if ( in_array( 'post_date', PLL()->options['sync'] ) ) {
60 $data['post_date'] = $from_post->post_date;
61 $data['post_date_gmt'] = $from_post->post_date_gmt;
62 }
63 }
64
65 return $data;
66 }
67
68 /**
69 * Copy post metas, and taxonomies when using "Add new" ( translation )
70 *
71 * @since 2.5
72 */
73 public function new_post_translation() {
74 global $post;
75 static $done = array();
76
77 if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $post->post_type ) ) {
78 // Capability check already done in post-new.php
79 $from_post_id = (int) $_GET['from_post'];
80 $lang = $this->model->get_language( $_GET['new_lang'] );
81
82 if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) {
83 return;
84 }
85
86 $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests.
87
88 $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug );
89 $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug );
90
91 if ( is_sticky( $from_post_id ) ) {
92 stick_post( $post->ID );
93 }
94 }
95 }
96
97 /**
98 * Get post fields to synchronize
99 *
100 * @since 2.4
101 *
102 * @param object $post Post object
103 * @return array
104 */
105 protected function get_fields_to_sync( $post ) {
106 global $wpdb;
107
108 $postarr = parent::get_fields_to_sync( $post );
109
110 // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32.
111 if ( in_array( 'post_date', $this->options['sync'] ) && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) {
112 unset( $postarr['post_date'] );
113 unset( $postarr['post_date_gmt'] );
114
115 $original = get_post( (int) $_GET['from_post'] );
116 $wpdb->update(
117 $wpdb->posts,
118 array(
119 'post_date' => $original->post_date,
120 'post_date_gmt' => $original->post_date_gmt,
121 ),
122 array( 'ID' => $post->ID )
123 );
124 }
125
126 if ( isset( $GLOBALS['post_type'] ) ) {
127 $post_type = $GLOBALS['post_type'];
128 } elseif ( isset( $_REQUEST['post_type'] ) ) {
129 $post_type = $_REQUEST['post_type']; // 2nd case for quick edit
130 }
131
132 // Make sure not to impact media translations when creating them at the same time as post
133 if ( in_array( 'post_parent', $this->options['sync'] ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) {
134 unset( $postarr['post_parent'] );
135 }
136
137 return $postarr;
138 }
139
140 /**
141 * Synchronizes post fields in translations
142 *
143 * @since 1.2
144 *
145 * @param int $post_id post id
146 * @param object $post post object
147 * @param array $translations post translations
148 */
149 public function pll_save_post( $post_id, $post, $translations ) {
150 parent::pll_save_post( $post_id, $post, $translations );
151
152 // Sticky posts
153 if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
154 $stickies = get_option( 'sticky_posts' );
155 if ( isset( $_REQUEST['sticky'] ) && 'sticky' === $_REQUEST['sticky'] ) {
156 $stickies = array_merge( $stickies, array_values( $translations ) );
157 } else {
158 $stickies = array_diff( $stickies, array_values( $translations ) );
159 }
160 update_option( 'sticky_posts', array_unique( $stickies ) );
161 }
162 }
163
164 /**
165 * Some backward compatibility with Polylang < 2.3
166 * allows to call PLL()->sync->copy_post_metas() and PLL()->sync->copy_taxonomies()
167 * used for example in Polylang for WooCommerce
168 * the compatibility is however only partial as the 4th argument $sync is lost
169 *
170 * @since 2.3
171 *
172 * @param string $func Function name
173 * @param array $args Function arguments
174 */
175 public function __call( $func, $args ) {
176 $obj = substr( $func, 5 );
177
178 if ( is_object( $this->$obj ) && method_exists( $this->$obj, 'copy' ) ) {
179 if ( WP_DEBUG ) {
180 $debug = debug_backtrace();
181 $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func
182
183 trigger_error(
184 sprintf(
185 '%1$s was called incorrectly in %3$s on line %4$s: the call to PLL()->sync->%1$s() has been deprecated in Polylang 2.3, use PLL()->sync->%2$s->copy() instead.' . "\nError handler",
186 $func,
187 $obj,
188 $debug[ $i ]['file'],
189 $debug[ $i ]['line']
190 )
191 );
192 }
193 return call_user_func_array( array( $this->$obj, 'copy' ), $args );
194 }
195
196 $debug = debug_backtrace();
197 trigger_error( sprintf( 'Call to undefined function PLL()->sync->%1$s() in %2$s on line %3$s' . "\nError handler", $func, $debug[0]['file'], $debug[0]['line'] ), E_USER_ERROR );
198 }
199 }
200