PluginProbe
Polylang / 3.1
Polylang v3.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 +38 -27 2.93.1 View file →
@@ -21,10 +21,9 @@
21 21 parent::__construct( $polylang );
22 22
23 23 add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 );
24 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
25 + add_filter( 'use_block_editor_for_post', array( $this, 'new_post_translation' ), 5000 ); // After content duplication.
27 26 }
28 27
29 28 /**
30 29 * Translate post parent if exists when using "Add new" ( translation )
@@ -61,16 +60,18 @@
61 60
62 61 $from_post_id = (int) $_GET['from_post'];
63 62 $from_post = get_post( $from_post_id );
64 63
65 - foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
66 - $data[ $property ] = $from_post->$property;
67 - }
64 + if ( $from_post instanceof WP_Post ) {
65 + foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
66 + $data[ $property ] = $from_post->$property;
67 + }
68 68
69 - // Copy the date only if the synchronization is activated
70 - if ( in_array( 'post_date', $this->options['sync'] ) ) {
71 - $data['post_date'] = $from_post->post_date;
72 - $data['post_date_gmt'] = $from_post->post_date_gmt;
69 + // Copy the date only if the synchronization is activated
70 + if ( in_array( 'post_date', $this->options['sync'] ) ) {
71 + $data['post_date'] = $from_post->post_date;
72 + $data['post_date_gmt'] = $from_post->post_date_gmt;
73 + }
73 74 }
74 75 }
75 76
76 77 return $data;
@@ -79,14 +80,18 @@
79 80 /**
80 81 * Copy post metas, and taxonomies when using "Add new" ( translation )
81 82 *
82 83 * @since 2.5
84 + * @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 + *
86 + * @param bool $is_block_editor Whether the post can be edited or not.
87 + * @return bool
83 88 */
84 - public function new_post_translation() {
89 + public function new_post_translation( $is_block_editor ) {
85 90 global $post;
86 91 static $done = array();
87 92
88 - 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 + if ( ! empty( $post ) && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $post->post_type ) ) {
89 94 check_admin_referer( 'new-post-translation' );
90 95
91 96 // Capability check already done in post-new.php
92 97 $from_post_id = (int) $_GET['from_post'];
@@ -92,9 +97,9 @@
92 97 $from_post_id = (int) $_GET['from_post'];
93 98 $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) );
94 99
95 100 if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) {
96 - return;
101 + return $is_block_editor;
97 102 }
98 103
99 104 $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests.
100 105
@@ -104,17 +109,19 @@
104 109 if ( is_sticky( $from_post_id ) ) {
105 110 stick_post( $post->ID );
106 111 }
107 112 }
113 +
114 + return $is_block_editor;
108 115 }
109 116
110 117 /**
111 - * Get post fields to synchronize
118 + * Get post fields to synchronize.
112 119 *
113 120 * @since 2.4
114 121 *
115 - * @param object $post Post object
116 - * @return array
122 + * @param WP_Post $post Post object.
123 + * @return array Fields to synchronize.
117 124 */
118 125 protected function get_fields_to_sync( $post ) {
119 126 global $wpdb;
120 127
@@ -127,16 +134,19 @@
127 134 unset( $postarr['post_date'] );
128 135 unset( $postarr['post_date_gmt'] );
129 136
130 137 $original = get_post( (int) $_GET['from_post'] );
131 - $wpdb->update(
132 - $wpdb->posts,
133 - array(
134 - 'post_date' => $original->post_date,
135 - 'post_date_gmt' => $original->post_date_gmt,
136 - ),
137 - array( 'ID' => $post->ID )
138 - );
138 +
139 + if ( $original instanceof WP_Post ) {
140 + $wpdb->update(
141 + $wpdb->posts,
142 + array(
143 + 'post_date' => $original->post_date,
144 + 'post_date_gmt' => $original->post_date_gmt,
145 + ),
146 + array( 'ID' => $post->ID )
147 + );
148 + }
139 149 }
140 150
141 151 if ( isset( $GLOBALS['post_type'] ) ) {
142 152 $post_type = $GLOBALS['post_type'];
@@ -152,15 +162,15 @@
152 162 return $postarr;
153 163 }
154 164
155 165 /**
156 - * Synchronizes post fields in translations
166 + * Synchronizes post fields in translations.
157 167 *
158 168 * @since 1.2
159 169 *
160 - * @param int $post_id post id
161 - * @param object $post post object
162 - * @param array $translations post translations
170 + * @param int $post_id Post id.
171 + * @param WP_Post $post Post object.
172 + * @param int[] $translations Post translations.
163 173 */
164 174 public function pll_save_post( $post_id, $post, $translations ) {
165 175 parent::pll_save_post( $post_id, $post, $translations );
166 176
@@ -185,8 +195,9 @@
185 195 * @since 2.3
186 196 *
187 197 * @param string $func Function name
188 198 * @param array $args Function arguments
199 + * @return mixed|void
189 200 */
190 201 public function __call( $func, $args ) {
191 202 $obj = substr( $func, 5 );
192 203