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 +84 -66 3.0.23.7.1 View file →
@@ -8,8 +8,12 @@
8 8 *
9 9 * @since 1.2
10 10 */
11 11 class PLL_Admin_Sync extends PLL_Sync {
12 + /**
13 + * @var PLL_Admin_Links
14 + */
15 + private $links;
12 16
13 17 /**
14 18 * Constructor
15 19 *
@@ -14,42 +18,55 @@
14 18 * Constructor
15 19 *
16 20 * @since 1.2
17 21 *
18 - * @param object $polylang
22 + * @param object $polylang The Polylang object.
19 23 */
20 24 public function __construct( &$polylang ) {
21 25 parent::__construct( $polylang );
22 26
27 + $this->links = &$polylang->links;
28 +
23 29 add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 );
24 30 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
31 + add_filter( 'use_block_editor_for_post', array( $this, 'new_post_translation' ), 5000 ); // After content duplication.
27 32 }
28 33
29 34 /**
30 - * Translate post parent if exists when using "Add new" ( translation )
35 + * Translates the post parent if it exists when using "Add new" (translation).
31 36 *
32 37 * @since 0.6
33 38 *
34 - * @param int $post_parent Post parent ID
35 - * @param int $post_id Post ID, unused
36 - * @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.
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 - }
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;
46 49 }
47 - 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;
48 65 }
49 66
50 67 /**
51 - * 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.
52 69 *
53 70 * @since 2.5
54 71 *
55 72 * @param array $data An array of slashed post data.
@@ -55,25 +72,22 @@
55 72 * @param array $data An array of slashed post data.
56 73 * @return array
57 74 */
58 75 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' );
76 + $context_data = $this->links->get_data_from_new_post_translation_request( $data['post_type'] ?? '' );
61 77
62 - $from_post_id = (int) $_GET['from_post'];
63 - $from_post = get_post( $from_post_id );
78 + if ( empty( $context_data ) ) {
79 + return $data;
80 + }
64 81
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 - }
82 + foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
83 + $data[ $property ] = $context_data['from_post']->$property;
84 + }
69 85
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 - }
75 - }
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;
76 90 }
77 91
78 92 return $data;
79 93 }
@@ -78,38 +92,46 @@
78 92 return $data;
79 93 }
80 94
81 95 /**
82 - * Copy post metas, and taxonomies when using "Add new" ( translation )
96 + * Copies post metas and taxonomies when using "Add new" (translation).
83 97 *
84 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.
85 100 *
86 - * @return void
101 + * @param bool $is_block_editor Whether the post can be edited or not.
102 + * @return bool
87 103 */
88 - public function new_post_translation() {
104 + public function new_post_translation( $is_block_editor ) {
89 105 global $post;
90 106 static $done = array();
91 107
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' );
108 + if ( empty( $post ) ) {
109 + return $is_block_editor;
110 + }
94 111
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'] ) );
112 + $context_data = $this->links->get_data_from_new_post_translation_request( $post->post_type );
98 113
99 - if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) {
100 - return;
101 - }
114 + if ( empty( $context_data ) || ! empty( $done[ $context_data['from_post']->ID ] ) ) {
115 + return $is_block_editor;
116 + }
102 117
103 - $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'] );
104 119
105 - $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug );
106 - $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug );
120 + if ( empty( $lang ) ) {
121 + return $is_block_editor;
122 + }
107 123
108 - if ( is_sticky( $from_post_id ) ) {
109 - stick_post( $post->ID );
110 - }
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 );
111 131 }
132 +
133 + return $is_block_editor;
112 134 }
113 135
114 136 /**
115 137 * Get post fields to synchronize.
@@ -121,39 +143,35 @@
121 143 */
122 144 protected function get_fields_to_sync( $post ) {
123 145 global $wpdb;
124 146
125 - $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 );
126 149
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 -
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 ) ) {
131 152 unset( $postarr['post_date'] );
132 153 unset( $postarr['post_date_gmt'] );
133 154
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 - );
145 - }
155 + $wpdb->update(
156 + $wpdb->posts,
157 + array(
158 + 'post_date' => $context_data['from_post']->post_date,
159 + 'post_date_gmt' => $context_data['from_post']->post_date_gmt,
160 + ),
161 + array( 'ID' => $post->ID )
162 + );
146 163 }
147 164
148 165 if ( isset( $GLOBALS['post_type'] ) ) {
149 166 $post_type = $GLOBALS['post_type'];
150 - } elseif ( isset( $_REQUEST['post_type'] ) ) {
151 - $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
152 170 }
153 171
154 172 // 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 ) ) {
173 + if ( in_array( 'post_parent', $this->options['sync'], true ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) {
156 174 unset( $postarr['post_parent'] );
157 175 }
158 176
159 177 return $postarr;