PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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 +171 -124 3.0.32.3.6 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,10 @@
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 {
9 + public $taxonomies, $post_metas, $term_meta;
12 10
13 11 /**
14 12 * Constructor
15 13 *
@@ -17,14 +15,28 @@
17 15 *
18 16 * @param object $polylang
19 17 */
20 18 public function __construct( &$polylang ) {
21 - parent::__construct( $polylang );
19 + $this->model = &$polylang->model;
20 + $this->options = &$polylang->options;
22 21
22 + $this->taxonomies = new PLL_Sync_Tax( $polylang );
23 + $this->post_metas = new PLL_Sync_Post_Metas( $polylang );
24 + $this->term_metas = new PLL_Sync_Term_Metas( $polylang );
25 +
23 26 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
27 + add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 5, 2 ); // Before Types which populates custom fields in same hook with priority 10
28 +
29 + add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 );
30 + add_action( 'pll_save_term', array( $this, 'sync_term_parent' ), 10, 3 );
31 +
32 + if ( $this->options['media_support'] ) {
33 + add_action( 'pll_translate_media', array( $this->taxonomies, 'copy' ), 10, 3 );
34 + add_action( 'pll_translate_media', array( $this->post_metas, 'copy' ), 10, 3 );
35 + add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
36 + }
37 +
38 + add_filter( 'pre_update_option_sticky_posts', array( $this, 'sync_sticky_posts' ), 10, 2 );
27 39 }
28 40
29 41 /**
30 42 * Translate post parent if exists when using "Add new" ( translation )
@@ -36,152 +48,201 @@
36 48 * @param array $postarr Array of parsed post data
37 49 * @return int
38 50 */
39 51 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;
52 + // Make sure not to impact media translations created at the same time
53 + 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 54 }
49 55
50 56 /**
51 - * Copy menu order, comment, ping status and optionally the date when creating a new tanslation
57 + * Copy post metas, menu order, comment and ping status when using "Add new" ( translation )
58 + * formerly used dbx_post_advanced deprecated in WP 3.7
52 59 *
53 - * @since 2.5
60 + * @since 1.2
54 61 *
55 - * @param array $data An array of slashed post data.
56 - * @return array
62 + * @param string $post_type unused
63 + * @param object $post current post object
57 64 */
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' );
65 + public function add_meta_boxes( $post_type, $post ) {
66 + if ( 'post-new.php' == $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) && $this->model->is_translated_post_type( $post->post_type ) ) {
67 + // Capability check already done in post-new.php
68 + $from_post_id = (int) $_GET['from_post'];
69 + $from_post = get_post( $from_post_id );
70 + $lang = $this->model->get_language( $_GET['new_lang'] );
61 71
62 - $from_post_id = (int) $_GET['from_post'];
63 - $from_post = get_post( $from_post_id );
72 + if ( ! $from_post || ! $lang ) {
73 + return;
74 + }
64 75
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 - }
76 + $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug );
77 + $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug );
69 78
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 - }
79 + foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
80 + $post->$property = $from_post->$property;
75 81 }
82 +
83 + // Copy the date only if the synchronization is activated
84 + if ( in_array( 'post_date', $this->options['sync'] ) ) {
85 + $post->post_date = $from_post->post_date;
86 + $post->post_date_gmt = $from_post->post_date_gmt;
87 + }
88 +
89 + if ( is_sticky( $from_post_id ) ) {
90 + stick_post( $post->ID );
91 + }
76 92 }
77 -
78 - return $data;
79 93 }
80 94
81 95 /**
82 - * Copy post metas, and taxonomies when using "Add new" ( translation )
96 + * Synchronizes post fields in translations
83 97 *
84 - * @since 2.5
98 + * @since 1.2
85 99 *
86 - * @return void
100 + * @param int $post_id post id
101 + * @param object $post post object
102 + * @param array $translations post translations
87 103 */
88 - public function new_post_translation() {
89 - global $post;
90 - static $done = array();
104 + public function pll_save_post( $post_id, $post, $translations ) {
105 + global $wpdb;
91 106
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' );
107 + // Prepare properties to synchronize
108 + foreach ( array( 'comment_status', 'ping_status', 'menu_order' ) as $property ) {
109 + if ( in_array( $property, $this->options['sync'] ) ) {
110 + $postarr[ $property ] = $post->$property;
111 + }
112 + }
94 113
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'] ) );
114 + if ( in_array( 'post_date', $this->options['sync'] ) ) {
115 + // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32.
116 + if ( 'post-new.php' === $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) ) {
117 + $original = get_post( (int) $_GET['from_post'] );
118 + $wpdb->update(
119 + $wpdb->posts, array(
120 + 'post_date' => $original->post_date,
121 + 'post_date_gmt' => $original->post_date_gmt,
122 + ),
123 + array( 'ID' => $post_id )
124 + );
125 + } else {
126 + $postarr['post_date'] = $post->post_date;
127 + $postarr['post_date_gmt'] = $post->post_date_gmt;
128 + }
129 + }
98 130
99 - if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) {
100 - return;
131 + foreach ( $translations as $lang => $tr_id ) {
132 + if ( ! $tr_id || $tr_id === $post_id ) {
133 + continue;
101 134 }
102 135
103 - $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests.
136 + // Add comment status, ping status, menu order... to synchronization
137 + $tr_arr = empty( $postarr ) ? array() : $postarr;
104 138
105 - $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug );
106 - $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug );
139 + if ( isset( $GLOBALS['post_type'] ) ) {
140 + $post_type = $GLOBALS['post_type'];
141 + } elseif ( isset( $_REQUEST['post_type'] ) ) {
142 + $post_type = $_REQUEST['post_type']; // 2nd case for quick edit
143 + }
107 144
108 - if ( is_sticky( $from_post_id ) ) {
109 - stick_post( $post->ID );
145 + // Add post parent to synchronization
146 + // Make sure not to impact media translations when creating them at the same time as post
147 + // Do not udpate the translation parent if the user set a parent with no translation
148 + if ( in_array( 'post_parent', $this->options['sync'] ) && isset( $post_type ) && $post_type === $post->post_type ) {
149 + $post_parent = ( $parent_id = wp_get_post_parent_id( $post_id ) ) ? $this->model->post->get_translation( $parent_id, $lang ) : 0;
150 + if ( ! ( $parent_id && ! $post_parent ) ) {
151 + $tr_arr['post_parent'] = $post_parent;
152 + }
110 153 }
154 +
155 + // Update all the row at once
156 + // Don't use wp_update_post to avoid infinite loop
157 + if ( ! empty( $tr_arr ) ) {
158 + $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) );
159 + clean_post_cache( $tr_id );
160 + }
111 161 }
162 +
163 + // Sticky posts
164 + if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
165 + $stickies = get_option( 'sticky_posts' );
166 + if ( isset( $_REQUEST['sticky'] ) && 'sticky' === $_REQUEST['sticky'] ) {
167 + $stickies = array_merge( $stickies, array_values( $translations ) );
168 + } else {
169 + $stickies = array_diff( $stickies, array_values( $translations ) );
170 + }
171 + update_option( 'sticky_posts', array_unique( $stickies ) );
172 + }
112 173 }
113 174
114 175 /**
115 - * Get post fields to synchronize.
176 + * Synchronize term parent in translations
177 + * Calling clean_term_cache *after* this is mandatory otherwise the $taxonomy_children option is not correctly updated
178 + * 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
179 + * This is the reason to use the edit_term filter and not edited_term
116 180 *
117 - * @since 2.4
181 + * @since 2.3
118 182 *
119 - * @param WP_Post $post Post object.
120 - * @return array Fields to synchronize.
183 + * @param int $term_id Term id
184 + * @param string $taxonomy Taxonomy name
185 + * @param array $translations The list of translations term ids
121 186 */
122 - protected function get_fields_to_sync( $post ) {
187 + public function sync_term_parent( $term_id, $taxonomy, $translations ) {
123 188 global $wpdb;
124 189
125 - $postarr = parent::get_fields_to_sync( $post );
190 + if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) {
191 + $term = get_term( $term_id );
126 192
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' );
193 + foreach ( $translations as $lang => $tr_id ) {
194 + if ( ! empty( $tr_id ) && $tr_id !== $term_id ) {
195 + $tr_parent = $this->model->term->get_translation( $term->parent, $lang );
130 196
131 - unset( $postarr['post_date'] );
132 - unset( $postarr['post_date_gmt'] );
197 + $wpdb->update(
198 + $wpdb->term_taxonomy,
199 + array( 'parent' => isset( $tr_parent ) ? $tr_parent : 0 ),
200 + array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id )
201 + );
133 202
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 - );
203 + clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9
204 + }
145 205 }
146 206 }
207 + }
147 208
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 - }
153 -
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'] );
157 - }
158 -
159 - return $postarr;
209 + /**
210 + * Synchronizes terms and metas in translations for media
211 + *
212 + * @since 1.8
213 + *
214 + * @param int $post_id post id
215 + */
216 + public function edit_attachment( $post_id ) {
217 + $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) );
160 218 }
161 219
162 220 /**
163 - * Synchronizes post fields in translations.
221 + * Synchronize sticky posts
164 222 *
165 - * @since 1.2
223 + * @since 2.3
166 224 *
167 - * @param int $post_id Post id.
168 - * @param WP_Post $post Post object.
169 - * @param int[] $translations Post translations.
225 + * @param array $value New option value
226 + * @param array $old_value Old option value
227 + * @return array
170 228 */
171 - public function pll_save_post( $post_id, $post, $translations ) {
172 - parent::pll_save_post( $post_id, $post, $translations );
229 + public function sync_sticky_posts( $value, $old_value ) {
230 + if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
231 + // Stick post
232 + if ( $sticked = array_diff( $value, $old_value ) ) {
233 + $translations = $this->model->post->get_translations( reset( $sticked ) );
234 + $value = array_unique( array_merge( $value, array_values( $translations ) ) );
235 + }
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 + // Unstick post
238 + if ( $unsticked = array_diff( $old_value, $value ) ) {
239 + $translations = $this->model->post->get_translations( reset( $unsticked ) );
240 + $value = array_unique( array_diff( $value, array_values( $translations ) ) );
181 241 }
182 - update_option( 'sticky_posts', array_unique( $stickies ) );
183 242 }
243 +
244 + return $value;
184 245 }
185 246
186 247 /**
187 248 * Some backward compatibility with Polylang < 2.3
@@ -192,9 +253,8 @@
192 253 * @since 2.3
193 254 *
194 255 * @param string $func Function name
195 256 * @param array $args Function arguments
196 - * @return mixed|void
197 257 */
198 258 public function __call( $func, $args ) {
199 259 $obj = substr( $func, 5 );
200 260
@@ -199,32 +259,19 @@
199 259 $obj = substr( $func, 5 );
200 260
201 261 if ( is_object( $this->$obj ) && method_exists( $this->$obj, 'copy' ) ) {
202 262 if ( WP_DEBUG ) {
203 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
263 + $debug = debug_backtrace();
204 264 $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func
205 265
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 - );
266 + trigger_error( sprintf(
267 + '%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",
268 + $func, $obj, $debug[ $i ]['file'], $debug[ $i ]['line']
269 + ) );
215 270 }
216 271 return call_user_func_array( array( $this->$obj, 'copy' ), $args );
217 272 }
218 273
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 - );
274 + $debug = debug_backtrace();
275 + 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 );
229 276 }
230 277 }