diff --git a/sync/class.jetpack-sync-module-posts.php b/sync/class.jetpack-sync-module-posts.php index 8c68b16752ee4..6d19b39df543e 100644 --- a/sync/class.jetpack-sync-module-posts.php +++ b/sync/class.jetpack-sync-module-posts.php @@ -280,6 +280,24 @@ public function save_published( $new_status, $old_status, $post ) { $this->previous_status[ $post->ID ] = $old_status; } + /* + * When publishing or updating a post, the Gutenberg editor sends two requests: + * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` + * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` + * + * The 2nd request is to update post meta, which is not supported on WP REST API. + * When syncing post data, we will include if this was a meta box update. + */ + public function is_gutenberg_meta_box_update() { + return ( + isset( $_POST['action'], $_GET['classic-editor'], $_GET['meta_box'] ) && + 'editpost' === $_POST['action'] && + '1' === $_GET['classic-editor'] && + '1' === $_GET['meta_box'] && + Jetpack::is_gutenberg_available() + ); + } + public function wp_insert_post( $post_ID, $post = null, $update = null ) { if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { return; @@ -301,7 +319,8 @@ public function wp_insert_post( $post_ID, $post = null, $update = null ) { $state = array( 'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ), 'previous_status' => $previous_status, - 'just_published' => $just_published + 'just_published' => $just_published, + 'is_gutenberg_meta_box_update' => $this->is_gutenberg_meta_box_update(), ); /** * Filter that is used to add to the post flags ( meta data ) when a post gets published diff --git a/tests/php/sync/test_class.jetpack-sync-posts.php b/tests/php/sync/test_class.jetpack-sync-posts.php index d38ec22b79405..780e86f5fab8d 100644 --- a/tests/php/sync/test_class.jetpack-sync-posts.php +++ b/tests/php/sync/test_class.jetpack-sync-posts.php @@ -113,6 +113,17 @@ public function test_delete_post_syncs_event() { $this->assertEquals( $this->post->ID, $event->args[0] ); } + public function test_update_post_includes_gutenberg_info_in_state() { + $this->post->post_content = "Updated using classic editor"; + + wp_update_post( $this->post ); + + $this->sender->do_sync(); + $event = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_save_post' ); + + $this->assertEquals( false, $event->args[3]['is_gutenberg_meta_box_update'] ); + } + public function test_update_post_updates_data() { $this->post->post_content = "foo bar";