PluginProbe
Polylang / 2.0.12
Polylang v2.0.12
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
← All changes | modules/sync/admin-sync.php +268 -136 3.02.0.12 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Manages copy and synchronization of terms and post metas
8 5 *
@@ -7,9 +4,9 @@
7 4 * Manages copy and synchronization of terms and post metas
8 5 *
9 6 * @since 1.2
10 7 */
11 -class PLL_Admin_Sync extends PLL_Sync {
8 +class PLL_Admin_Sync {
12 9
13 10 /**
14 11 * Constructor
15 12 *
@@ -17,14 +14,22 @@
17 14 *
18 15 * @param object $polylang
19 16 */
20 17 public function __construct( &$polylang ) {
21 - parent::__construct( $polylang );
18 + $this->model = &$polylang->model;
19 + $this->options = &$polylang->options;
22 20
23 21 add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 );
24 - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) );
25 - add_action( 'rest_api_init', array( $this, 'new_post_translation' ) ); // Block editor
26 - 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
22 + add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 5, 2 ); // Before Types which populates custom fields in same hook with priority 10
23 +
24 + add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 );
25 + add_action( 'pll_save_term', array( $this, 'pll_save_term' ), 10, 3 );
26 +
27 + if ( $this->options['media_support'] ) {
28 + add_action( 'pll_translate_media', array( $this, 'copy_taxonomies' ), 10, 3 );
29 + add_action( 'pll_translate_media', array( $this, 'copy_post_metas' ), 10, 3 );
30 + add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
31 + }
27 32 }
28 33
29 34 /**
30 35 * Translate post parent if exists when using "Add new" ( translation )
@@ -36,195 +41,322 @@
36 41 * @param array $postarr Array of parsed post data
37 42 * @return int
38 43 */
39 44 public function wp_insert_post_parent( $post_parent, $post_id, $postarr ) {
40 - if ( isset( $_GET['from_post'], $_GET['new_lang'], $_GET['post_type'] ) ) {
41 - check_admin_referer( 'new-post-translation' );
42 - // Make sure not to impact media translations created at the same time
43 - if ( $_GET['post_type'] === $postarr['post_type'] && ( $id = wp_get_post_parent_id( (int) $_GET['from_post'] ) ) && $parent = $this->model->post->get_translation( $id, sanitize_key( $_GET['new_lang'] ) ) ) {
44 - $post_parent = $parent;
45 - }
46 - }
47 - return $post_parent;
45 + // Make sure not to impact media translations created at the same time
46 + 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;
48 47 }
49 48
50 49 /**
51 - * Copy menu order, comment, ping status and optionally the date when creating a new tanslation
50 + * Copy post metas, menu order, comment and ping status when using "Add new" ( translation )
51 + * formerly used dbx_post_advanced deprecated in WP 3.7
52 52 *
53 - * @since 2.5
53 + * @since 1.2
54 54 *
55 - * @param array $data An array of slashed post data.
56 - * @return array
55 + * @param string $post_type unused
56 + * @param object $post current post object
57 57 */
58 - public function wp_insert_post_data( $data ) {
59 - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $data['post_type'] ) ) {
60 - check_admin_referer( 'new-post-translation' );
58 + public function add_meta_boxes( $post_type, $post ) {
59 + if ( 'post-new.php' == $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) && $this->model->is_translated_post_type( $post->post_type ) ) {
60 + // Capability check already done in post-new.php
61 + $from_post_id = (int) $_GET['from_post'];
62 + $from_post = get_post( $from_post_id );
63 + $lang = $this->model->get_language( $_GET['new_lang'] );
61 64
62 - $from_post_id = (int) $_GET['from_post'];
63 - $from_post = get_post( $from_post_id );
65 + if ( ! $from_post || ! $lang ) {
66 + return;
67 + }
64 68
65 - if ( $from_post instanceof WP_Post ) {
66 - foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
67 - $data[ $property ] = $from_post->$property;
68 - }
69 + $this->copy_taxonomies( $from_post_id, $post->ID, $lang->slug );
70 + $this->copy_post_metas( $from_post_id, $post->ID, $lang->slug );
69 71
70 - // Copy the date only if the synchronization is activated
71 - if ( in_array( 'post_date', $this->options['sync'] ) ) {
72 - $data['post_date'] = $from_post->post_date;
73 - $data['post_date_gmt'] = $from_post->post_date_gmt;
74 - }
72 + foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
73 + $post->$property = $from_post->$property;
75 74 }
75 +
76 + if ( is_sticky( $from_post_id ) ) {
77 + stick_post( $post->ID );
78 + }
76 79 }
80 + }
77 81
78 - return $data;
82 + /**
83 + * Get the list of taxonomies to copy or to synchronize
84 + *
85 + * @since 1.7
86 + *
87 + * @param bool $sync true if it is synchronization, false if it is a copy
88 + * @return array list of taxonomy names
89 + */
90 + public function get_taxonomies_to_copy( $sync ) {
91 + $taxonomies = ! $sync || in_array( 'taxonomies', $this->options['sync'] ) ? $this->model->get_translated_taxonomies() : array();
92 + if ( ! $sync || in_array( 'post_format', $this->options['sync'] ) ) {
93 + $taxonomies[] = 'post_format';
94 + }
95 +
96 + /**
97 + * Filter the taxonomies to copy or synchronize
98 + *
99 + * @since 1.7
100 + *
101 + * @param array $taxonomies list of taxonomy names
102 + * @param bool $sync true if it is synchronization, false if it is a copy
103 + */
104 + return array_unique( apply_filters( 'pll_copy_taxonomies', $taxonomies, $sync ) );
79 105 }
80 106
81 107 /**
82 - * Copy post metas, and taxonomies when using "Add new" ( translation )
108 + * Copy or synchronize terms
83 109 *
84 - * @since 2.5
110 + * @since 1.8
85 111 *
86 - * @return void
112 + * @param int $from id of the post from which we copy informations
113 + * @param int $to id of the post to which we paste informations
114 + * @param string $lang language slug
115 + * @param bool $sync true if it is synchronization, false if it is a copy, defaults to false
87 116 */
88 - public function new_post_translation() {
89 - global $post;
90 - static $done = array();
117 + public function copy_taxonomies( $from, $to, $lang, $sync = false ) {
118 + // Get taxonomies to sync for this post type
119 + $taxonomies = array_intersect( get_post_taxonomies( $from ), $this->get_taxonomies_to_copy( $sync ) );
91 120
92 - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $post->post_type ) ) {
93 - check_admin_referer( 'new-post-translation' );
121 + // Update the term cache to reduce the number of queries in the loop
122 + update_object_term_cache( $sync ? array( $from, $to ) : $from, get_post_type( $from ) );
94 123
95 - // Capability check already done in post-new.php
96 - $from_post_id = (int) $_GET['from_post'];
97 - $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) );
124 + // Copy or synchronize terms
125 + // FIXME quite a lot of query in foreach
126 + foreach ( $taxonomies as $tax ) {
127 + $terms = get_the_terms( $from, $tax );
98 128
99 - if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) {
100 - return;
101 - }
129 + // Translated taxonomy
130 + if ( $this->model->is_translated_taxonomy( $tax ) ) {
131 + $newterms = array();
132 + if ( is_array( $terms ) ) {
133 + foreach ( $terms as $term ) {
134 + if ( $term_id = $this->model->term->get_translation( $term->term_id, $lang ) ) {
135 + $newterms[] = (int) $term_id; // Cast is important otherwise we get 'numeric' tags
136 + }
137 + }
138 + }
102 139
103 - $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests.
140 + // For some reasons, the user may have untranslated terms in the translation. don't forget them.
141 + if ( $sync ) {
142 + $tr_terms = get_the_terms( $to, $tax );
143 + if ( is_array( $tr_terms ) ) {
144 + foreach ( $tr_terms as $term ) {
145 + if ( ! $this->model->term->get_translation( $term->term_id, $this->model->post->get_language( $from ) ) ) {
146 + $newterms[] = (int) $term->term_id;
147 + }
148 + }
149 + }
150 + }
104 151
105 - $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug );
106 - $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug );
152 + if ( ! empty( $newterms ) || $sync ) {
153 + wp_set_object_terms( $to, $newterms, $tax ); // replace terms in translation
154 + }
155 + }
107 156
108 - if ( is_sticky( $from_post_id ) ) {
109 - stick_post( $post->ID );
157 + // Untranslated taxonomy ( post format )
158 + // Don't use simple get_post_format / set_post_format to generalize the case to other taxonomies
159 + else {
160 + wp_set_object_terms( $to, is_array( $terms ) ? array_map( 'intval', wp_list_pluck( $terms, 'term_id' ) ) : null, $tax );
110 161 }
111 162 }
112 163 }
113 164
114 165 /**
115 - * Get post fields to synchronize.
166 + * Copy or synchronize metas (custom fields)
116 167 *
117 - * @since 2.4
168 + * @since 0.9
118 169 *
119 - * @param WP_Post $post Post object.
120 - * @return array Fields to synchronize.
170 + * @param int $from id of the post from which we copy informations
171 + * @param int $to id of the post to which we paste informations
172 + * @param string $lang language slug
173 + * @param bool $sync true if it is synchronization, false if it is a copy, defaults to false
121 174 */
122 - protected function get_fields_to_sync( $post ) {
123 - global $wpdb;
175 + public function copy_post_metas( $from, $to, $lang, $sync = false ) {
176 + // Copy or synchronize post metas and allow plugins to do the same
177 + $metas = get_post_custom( $from );
178 + $keys = array();
124 179
125 - $postarr = parent::get_fields_to_sync( $post );
180 + // Get public meta keys ( including from translated post in case we just deleted a custom field )
181 + if ( ! $sync || in_array( 'post_meta', $this->options['sync'] ) ) {
182 + foreach ( $keys = array_unique( array_merge( array_keys( $metas ), array_keys( get_post_custom( $to ) ) ) ) as $k => $meta_key ) {
183 + if ( is_protected_meta( $meta_key ) ) {
184 + unset( $keys[ $k ] );
185 + }
186 + }
187 + }
126 188
127 - // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32.
128 - if ( in_array( 'post_date', $this->options['sync'] ) && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) {
129 - check_admin_referer( 'new-post-translation' );
130 -
131 - unset( $postarr['post_date'] );
132 - unset( $postarr['post_date_gmt'] );
133 -
134 - $original = get_post( (int) $_GET['from_post'] );
135 -
136 - if ( $original instanceof WP_Post ) {
137 - $wpdb->update(
138 - $wpdb->posts,
139 - array(
140 - 'post_date' => $original->post_date,
141 - 'post_date_gmt' => $original->post_date_gmt,
142 - ),
143 - array( 'ID' => $post->ID )
144 - );
189 + // Add page template and featured image
190 + foreach ( array( '_wp_page_template', '_thumbnail_id' ) as $meta ) {
191 + if ( ! $sync || in_array( $meta, $this->options['sync'] ) ) {
192 + $keys[] = $meta;
145 193 }
146 194 }
147 195
148 - if ( isset( $GLOBALS['post_type'] ) ) {
149 - $post_type = $GLOBALS['post_type'];
150 - } elseif ( isset( $_REQUEST['post_type'] ) ) {
151 - $post_type = sanitize_key( $_REQUEST['post_type'] ); // 2nd case for quick edit
152 - }
196 + /**
197 + * Filter the custom fields to copy or synchronize
198 + *
199 + * @since 0.6
200 + * @since 1.9.2 The `$from`, `$to`, `$lang` parameters were added.
201 + *
202 + * @param array $keys list of custom fields names
203 + * @param bool $sync true if it is synchronization, false if it is a copy
204 + * @param int $from id of the post from which we copy informations
205 + * @param int $to id of the post to which we paste informations
206 + * @param string $lang language slug
207 + */
208 + $keys = array_unique( apply_filters( 'pll_copy_post_metas', $keys, $sync, $from, $to, $lang ) );
153 209
154 - // Make sure not to impact media translations when creating them at the same time as post
155 - if ( in_array( 'post_parent', $this->options['sync'] ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) {
156 - unset( $postarr['post_parent'] );
210 + // And now copy / synchronize
211 + foreach ( $keys as $key ) {
212 + delete_post_meta( $to, $key ); // The synchronization process of multiple values custom fields is easier if we delete all metas first
213 + if ( isset( $metas[ $key ] ) ) {
214 + foreach ( $metas[ $key ] as $value ) {
215 + // Important: always maybe_unserialize value coming from get_post_custom. See codex.
216 + // Thanks to goncalveshugo http://wordpress.org/support/topic/plugin-polylang-pll_copy_post_meta
217 + $value = maybe_unserialize( $value );
218 + // Special case for featured images which can be translated
219 + add_post_meta( $to, $key, ( '_thumbnail_id' == $key && $tr_value = $this->model->post->get_translation( $value, $lang ) ) ? $tr_value : $value );
220 + }
221 + }
157 222 }
158 -
159 - return $postarr;
160 223 }
161 224
162 225 /**
163 - * Synchronizes post fields in translations.
226 + * Synchronizes terms and metas in translations
164 227 *
165 228 * @since 1.2
166 229 *
167 - * @param int $post_id Post id.
168 - * @param WP_Post $post Post object.
169 - * @param int[] $translations Post translations.
230 + * @param int $post_id post id
231 + * @param object $post post object
232 + * @param array $translations post translations
170 233 */
171 234 public function pll_save_post( $post_id, $post, $translations ) {
172 - parent::pll_save_post( $post_id, $post, $translations );
235 + global $wpdb, $post_type;
173 236
174 - // Sticky posts
175 - if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
176 - $stickies = get_option( 'sticky_posts' );
177 - if ( isset( $_REQUEST['sticky'] ) && 'sticky' === $_REQUEST['sticky'] ) { // phpcs:ignore WordPress.Security.NonceVerification
178 - $stickies = array_merge( $stickies, array_values( $translations ) );
179 - } else {
180 - $stickies = array_diff( $stickies, array_values( $translations ) );
237 + // Prepare properties to synchronize
238 + foreach ( array( 'comment_status', 'ping_status', 'menu_order', 'post_date' ) as $property ) {
239 + if ( in_array( $property, $this->options['sync'] ) ) {
240 + $postarr[ $property ] = $post->$property;
181 241 }
182 - update_option( 'sticky_posts', array_unique( $stickies ) );
183 242 }
243 +
244 + if ( in_array( 'post_date', $this->options['sync'] ) ) {
245 + $postarr['post_date_gmt'] = $post->post_date_gmt;
246 + }
247 +
248 + // Synchronize terms and metas in translations
249 + foreach ( $translations as $lang => $tr_id ) {
250 + if ( ! $tr_id || $tr_id === $post_id ) {
251 + continue;
252 + }
253 +
254 + // Synchronize terms and metas
255 + $this->copy_taxonomies( $post_id, $tr_id, $lang, true );
256 + $this->copy_post_metas( $post_id, $tr_id, $lang, true );
257 +
258 + // Sticky posts
259 + if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
260 + isset( $_REQUEST['sticky'] ) ? stick_post( $tr_id ) : unstick_post( $tr_id );
261 + }
262 +
263 + // Add comment status, ping status, menu order... to synchronization
264 + $tr_arr = empty( $postarr ) ? array() : $postarr;
265 +
266 + // Add post parent to synchronization
267 + // Make sure not to impact media translations when creating them at the same time as post
268 + // Do not udpate the translation parent if the user set a parent with no translation
269 + if ( in_array( 'post_parent', $this->options['sync'] ) && $post_type === $post->post_type ) {
270 + $post_parent = ( $parent_id = wp_get_post_parent_id( $post_id ) ) ? $this->model->post->get_translation( $parent_id, $lang ) : 0;
271 + if ( ! ( $parent_id && ! $post_parent ) ) {
272 + $tr_arr['post_parent'] = $post_parent;
273 + }
274 + }
275 +
276 + // Update all the row at once
277 + // Don't use wp_update_post to avoid infinite loop
278 + if ( ! empty( $tr_arr ) ) {
279 + $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) );
280 + clean_post_cache( $tr_id );
281 + }
282 + }
184 283 }
185 284
186 285 /**
187 - * Some backward compatibility with Polylang < 2.3
188 - * allows to call PLL()->sync->copy_post_metas() and PLL()->sync->copy_taxonomies()
189 - * used for example in Polylang for WooCommerce
190 - * the compatibility is however only partial as the 4th argument $sync is lost
286 + * Synchronize translations of a term in all posts
191 287 *
192 - * @since 2.3
288 + * @since 1.2
193 289 *
194 - * @param string $func Function name
195 - * @param array $args Function arguments
196 - * @return mixed|void
290 + * @param int $term_id term id
291 + * @param string $taxonomy taxonomy name of the term
292 + * @param array $translations translations of the term
197 293 */
198 - public function __call( $func, $args ) {
199 - $obj = substr( $func, 5 );
294 + public function pll_save_term( $term_id, $taxonomy, $translations ) {
295 + // Check if the taxonomy is synchronized
296 + if ( ! $this->model->is_translated_taxonomy( $taxonomy ) || ! in_array( $taxonomy, $this->get_taxonomies_to_copy( true ) ) ) {
297 + return;
298 + }
200 299
201 - if ( is_object( $this->$obj ) && method_exists( $this->$obj, 'copy' ) ) {
202 - if ( WP_DEBUG ) {
203 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
204 - $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func
300 + // Get all posts associated to this term
301 + $posts = get_posts( array(
302 + 'numberposts' => -1,
303 + 'nopaging' => true,
304 + 'post_type' => 'any',
305 + 'post_status' => 'any',
306 + 'fields' => 'ids',
307 + 'tax_query' => array( array(
308 + 'taxonomy' => $taxonomy,
309 + 'field' => 'id',
310 + 'terms' => array_merge( array( $term_id ), array_values( $translations ) ),
311 + 'include_children' => false,
312 + ) ),
313 + ) );
205 314
206 - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
207 - sprintf(
208 - '%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",
209 - esc_html( $func ),
210 - esc_html( $obj ),
211 - esc_html( $debug[ $i ]['file'] ),
212 - absint( $debug[ $i ]['line'] )
213 - )
214 - );
315 + // Associate translated term to translated post
316 + // FIXME quite a lot of query in foreach
317 + foreach ( $this->model->get_languages_list() as $language ) {
318 + if ( $translated_term = $this->model->term->get( $term_id, $language ) ) {
319 + foreach ( $posts as $post_id ) {
320 + if ( $translated_post = $this->model->post->get( $post_id, $language ) ) {
321 + wp_set_object_terms( $translated_post, $translated_term, $taxonomy, true );
322 + }
323 + }
215 324 }
216 - return call_user_func_array( array( $this->$obj, 'copy' ), $args );
217 325 }
218 326
219 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
220 - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
221 - sprintf(
222 - 'Call to undefined function PLL()->sync->%1$s() in %2$s on line %3$s' . "\nError handler",
223 - esc_html( $func ),
224 - esc_html( $debug[0]['file'] ),
225 - absint( $debug[0]['line'] )
226 - ),
227 - E_USER_ERROR
228 - );
327 + // Synchronize parent in translations
328 + // Calling clean_term_cache *after* this is mandatory otherwise the $taxonomy_children option is not correctly updated
329 + // Before WP 3.9 clean_term_cache could be called ( efficiently ) only one time due to static array which prevented to update the option more than once
330 + // This is the reason to use the edit_term filter and not edited_term
331 + // Take care that $_POST contains the only valid values for the current term
332 + // FIXME can I synchronize parent without using $_POST instead?
333 + if ( isset( $_POST['term_tr_lang'] ) ) {
334 + foreach ( $_POST['term_tr_lang'] as $lang => $tr_id ) {
335 + if ( $tr_id ) {
336 + if ( isset( $_POST['parent'] ) && -1 != $_POST['parent'] ) { // Since WP 3.1
337 + $term_parent = $this->model->term->get_translation( (int) $_POST['parent'], $lang );
338 + }
339 +
340 + global $wpdb;
341 + $wpdb->update( $wpdb->term_taxonomy,
342 + array( 'parent' => isset( $term_parent ) ? $term_parent : 0 ),
343 + array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id )
344 + );
345 +
346 + clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9
347 + }
348 + }
349 + }
350 + }
351 +
352 + /**
353 + * Synchronizes terms and metas in translations for media
354 + *
355 + * @since 1.8
356 + *
357 + * @param int $post_id post id
358 + */
359 + public function edit_attachment( $post_id ) {
360 + $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) );
229 361 }
230 362 }