PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
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 +90 -61 2.73.7.1 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * Manages copy and synchronization of terms and post metas
5 8 *
@@ -5,8 +8,12 @@
5 8 *
6 9 * @since 1.2
7 10 */
8 11 class PLL_Admin_Sync extends PLL_Sync {
12 + /**
13 + * @var PLL_Admin_Links
14 + */
15 + private $links;
9 16
10 17 /**
11 18 * Constructor
12 19 *
@@ -11,42 +18,55 @@
11 18 * Constructor
12 19 *
13 20 * @since 1.2
14 21 *
15 - * @param object $polylang
22 + * @param object $polylang The Polylang object.
16 23 */
17 24 public function __construct( &$polylang ) {
18 25 parent::__construct( $polylang );
19 26
27 + $this->links = &$polylang->links;
28 +
20 29 add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 );
21 30 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
31 + add_filter( 'use_block_editor_for_post', array( $this, 'new_post_translation' ), 5000 ); // After content duplication.
24 32 }
25 33
26 34 /**
27 - * Translate post parent if exists when using "Add new" ( translation )
35 + * Translates the post parent if it exists when using "Add new" (translation).
28 36 *
29 37 * @since 0.6
30 38 *
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
39 + * @param int $post_parent Post parent ID.
40 + * @param int $post_id Post ID, unused.
41 + * @param array $postarr Array of parsed post data.
34 42 * @return int
35 43 */
36 44 public function wp_insert_post_parent( $post_parent, $post_id, $postarr ) {
37 - if ( isset( $_GET['from_post'], $_GET['new_lang'], $_GET['post_type'] ) ) {
38 - check_admin_referer( 'new-post-translation' );
39 - // Make sure not to impact media translations created at the same time
40 - 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'] ) ) ) {
41 - $post_parent = $parent;
42 - }
45 + $context_data = $this->links->get_data_from_new_post_translation_request( $postarr['post_type'] ?? '' );
46 +
47 + if ( empty( $context_data ) ) {
48 + return $post_parent;
43 49 }
44 - return $post_parent;
50 +
51 + // Make sure not to impact media translations created at the same time.
52 + $parent_id = wp_get_post_parent_id( $context_data['from_post'] );
53 +
54 + if ( empty( $parent_id ) ) {
55 + return $post_parent;
56 + }
57 +
58 + $tr_parent = $this->model->post->get_translation( $parent_id, $context_data['new_lang'] );
59 +
60 + if ( empty( $tr_parent ) ) {
61 + return $post_parent;
62 + }
63 +
64 + return $tr_parent;
45 65 }
46 66
47 67 /**
48 - * Copy menu order, comment, ping status and optionally the date when creating a new tanslation
68 + * Copies menu order, comment, ping status and optionally the date when creating a new translation.
49 69 *
50 70 * @since 2.5
51 71 *
52 72 * @param array $data An array of slashed post data.
@@ -52,23 +72,22 @@
52 72 * @param array $data An array of slashed post data.
53 73 * @return array
54 74 */
55 75 public function wp_insert_post_data( $data ) {
56 - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $data['post_type'] ) ) {
57 - check_admin_referer( 'new-post-translation' );
76 + $context_data = $this->links->get_data_from_new_post_translation_request( $data['post_type'] ?? '' );
58 77
59 - $from_post_id = (int) $_GET['from_post'];
60 - $from_post = get_post( $from_post_id );
78 + if ( empty( $context_data ) ) {
79 + return $data;
80 + }
61 81
62 - foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
63 - $data[ $property ] = $from_post->$property;
64 - }
82 + foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
83 + $data[ $property ] = $context_data['from_post']->$property;
84 + }
65 85
66 - // Copy the date only if the synchronization is activated
67 - if ( in_array( 'post_date', $this->options['sync'] ) ) {
68 - $data['post_date'] = $from_post->post_date;
69 - $data['post_date_gmt'] = $from_post->post_date_gmt;
70 - }
86 + // Copy the date only if the synchronization is activated.
87 + if ( in_array( 'post_date', $this->options['sync'], true ) ) {
88 + $data['post_date'] = $context_data['from_post']->post_date;
89 + $data['post_date_gmt'] = $context_data['from_post']->post_date_gmt;
71 90 }
72 91
73 92 return $data;
74 93 }
@@ -73,64 +92,72 @@
73 92 return $data;
74 93 }
75 94
76 95 /**
77 - * Copy post metas, and taxonomies when using "Add new" ( translation )
96 + * Copies post metas and taxonomies when using "Add new" (translation).
78 97 *
79 98 * @since 2.5
99 + * @since 3.1 Use of use_block_editor_for_post filter instead of rest_api_init which is triggered too early in WP 5.8.
100 + *
101 + * @param bool $is_block_editor Whether the post can be edited or not.
102 + * @return bool
80 103 */
81 - public function new_post_translation() {
104 + public function new_post_translation( $is_block_editor ) {
82 105 global $post;
83 106 static $done = array();
84 107
85 - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $post->post_type ) ) {
86 - check_admin_referer( 'new-post-translation' );
108 + if ( empty( $post ) ) {
109 + return $is_block_editor;
110 + }
87 111
88 - // Capability check already done in post-new.php
89 - $from_post_id = (int) $_GET['from_post'];
90 - $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) );
112 + $context_data = $this->links->get_data_from_new_post_translation_request( $post->post_type );
91 113
92 - if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) {
93 - return;
94 - }
114 + if ( empty( $context_data ) || ! empty( $done[ $context_data['from_post']->ID ] ) ) {
115 + return $is_block_editor;
116 + }
95 117
96 - $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests.
118 + $lang = $this->model->get_language( $context_data['new_lang'] );
97 119
98 - $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug );
99 - $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug );
120 + if ( empty( $lang ) ) {
121 + return $is_block_editor;
122 + }
100 123
101 - if ( is_sticky( $from_post_id ) ) {
102 - stick_post( $post->ID );
103 - }
124 + $done[ $context_data['from_post']->ID ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests.
125 +
126 + $this->taxonomies->copy( $context_data['from_post']->ID, $post->ID, $lang->slug );
127 + $this->post_metas->copy( $context_data['from_post']->ID, $post->ID, $lang->slug );
128 +
129 + if ( is_sticky( $context_data['from_post']->ID ) ) {
130 + stick_post( $post->ID );
104 131 }
132 +
133 + return $is_block_editor;
105 134 }
106 135
107 136 /**
108 - * Get post fields to synchronize
137 + * Get post fields to synchronize.
109 138 *
110 139 * @since 2.4
111 140 *
112 - * @param object $post Post object
113 - * @return array
141 + * @param WP_Post $post Post object.
142 + * @return array Fields to synchronize.
114 143 */
115 144 protected function get_fields_to_sync( $post ) {
116 145 global $wpdb;
117 146
118 - $postarr = parent::get_fields_to_sync( $post );
147 + $postarr = parent::get_fields_to_sync( $post );
148 + $context_data = $this->links->get_data_from_new_post_translation_request( $post->post_type );
119 149
120 - // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32.
121 - if ( in_array( 'post_date', $this->options['sync'] ) && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) {
122 - check_admin_referer( 'new-post-translation' );
123 -
150 + // For new drafts, save the date now otherwise it is overridden by WP. Thanks to JoryHogeveen. See #32.
151 + if ( ! empty( $context_data ) && in_array( 'post_date', $this->options['sync'], true ) ) {
124 152 unset( $postarr['post_date'] );
125 153 unset( $postarr['post_date_gmt'] );
126 154
127 - $original = get_post( (int) $_GET['from_post'] );
128 155 $wpdb->update(
129 156 $wpdb->posts,
130 157 array(
131 - 'post_date' => $original->post_date,
132 - 'post_date_gmt' => $original->post_date_gmt,
158 + 'post_date' => $context_data['from_post']->post_date,
159 + 'post_date_gmt' => $context_data['from_post']->post_date_gmt,
133 160 ),
134 161 array( 'ID' => $post->ID )
135 162 );
136 163 }
@@ -136,14 +163,15 @@
136 163 }
137 164
138 165 if ( isset( $GLOBALS['post_type'] ) ) {
139 166 $post_type = $GLOBALS['post_type'];
140 - } elseif ( isset( $_REQUEST['post_type'] ) ) {
141 - $post_type = sanitize_key( $_REQUEST['post_type'] ); // 2nd case for quick edit
167 + } elseif ( isset( $_REQUEST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
168 + // 2nd case for quick edit.
169 + $post_type = sanitize_key( $_REQUEST['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
142 170 }
143 171
144 172 // Make sure not to impact media translations when creating them at the same time as post
145 - if ( in_array( 'post_parent', $this->options['sync'] ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) {
173 + if ( in_array( 'post_parent', $this->options['sync'], true ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) {
146 174 unset( $postarr['post_parent'] );
147 175 }
148 176
149 177 return $postarr;
@@ -149,15 +177,15 @@
149 177 return $postarr;
150 178 }
151 179
152 180 /**
153 - * Synchronizes post fields in translations
181 + * Synchronizes post fields in translations.
154 182 *
155 183 * @since 1.2
156 184 *
157 - * @param int $post_id post id
158 - * @param object $post post object
159 - * @param array $translations post translations
185 + * @param int $post_id Post id.
186 + * @param WP_Post $post Post object.
187 + * @param int[] $translations Post translations.
160 188 */
161 189 public function pll_save_post( $post_id, $post, $translations ) {
162 190 parent::pll_save_post( $post_id, $post, $translations );
163 191
@@ -182,8 +210,9 @@
182 210 * @since 2.3
183 211 *
184 212 * @param string $func Function name
185 213 * @param array $args Function arguments
214 + * @return mixed|void
186 215 */
187 216 public function __call( $func, $args ) {
188 217 $obj = substr( $func, 5 );
189 218