From 0cc6ed6284cf07385ffcddbf079a7caeaeb39c23 Mon Sep 17 00:00:00 2001 From: Rocco Tripaldi Date: Tue, 11 Sep 2018 14:20:59 -0400 Subject: [PATCH 1/3] Sync: send gutenberg info when syncing posts --- sync/class.jetpack-sync-module-posts.php | 20 +++++++++++++- .../sync/test_class.jetpack-sync-posts.php | 27 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/sync/class.jetpack-sync-module-posts.php b/sync/class.jetpack-sync-module-posts.php index 8c68b16752ee4..63b3844fc45f0 100644 --- a/sync/class.jetpack-sync-module-posts.php +++ b/sync/class.jetpack-sync-module-posts.php @@ -280,6 +280,23 @@ 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'] + ); + } + public function wp_insert_post( $post_ID, $post = null, $update = null ) { if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { return; @@ -301,7 +318,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..8d4b8134bcf11 100644 --- a/tests/php/sync/test_class.jetpack-sync-posts.php +++ b/tests/php/sync/test_class.jetpack-sync-posts.php @@ -113,6 +113,33 @@ 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(); + + $this->assertEquals( false, $event->args[3]['is_gutenberg_meta_box_update'] ); + + $this->post->post_content = "Updated using Gutenberg editor"; + $_POST['action'] = 'editpost'; + $_GET['classic-editor'] = '1'; + $_GET['meta_box'] = '1'; + + wp_update_post( $this->post ); + + $this->sender->do_sync(); + $event = $this->server_event_storage->get_most_recent_event(); + + $this->assertEquals( true, $event->args[3]['is_gutenberg_meta_box_update'] ); + + unset( $_POST['action'] ); + unset( $_GET['classic-editor'] ); + unset( $_GET['meta_box'] ); + } + public function test_update_post_updates_data() { $this->post->post_content = "foo bar"; From e3e197c9ded3341964464f906383690e9e9a82f2 Mon Sep 17 00:00:00 2001 From: Rocco Tripaldi Date: Tue, 11 Sep 2018 15:57:39 -0400 Subject: [PATCH 2/3] Also check for active Gutenberg --- sync/class.jetpack-sync-module-posts.php | 3 ++- .../sync/test_class.jetpack-sync-posts.php | 22 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/sync/class.jetpack-sync-module-posts.php b/sync/class.jetpack-sync-module-posts.php index 63b3844fc45f0..09c9d8f9f7432 100644 --- a/sync/class.jetpack-sync-module-posts.php +++ b/sync/class.jetpack-sync-module-posts.php @@ -293,7 +293,8 @@ public function is_gutenberg_meta_box_update() { isset( $_POST['action'], $_GET['classic-editor'], $_GET['meta_box'] ) && 'editpost' === $_POST['action'] && '1' === $_GET['classic-editor'] && - '1' === $_GET['meta_box'] + '1' === $_GET['meta_box'] && + is_plugin_active( 'gutenberg/gutenberg.php' ) ); } diff --git a/tests/php/sync/test_class.jetpack-sync-posts.php b/tests/php/sync/test_class.jetpack-sync-posts.php index 8d4b8134bcf11..50ae5441dd6b2 100644 --- a/tests/php/sync/test_class.jetpack-sync-posts.php +++ b/tests/php/sync/test_class.jetpack-sync-posts.php @@ -119,11 +119,23 @@ public function test_update_post_includes_gutenberg_info_in_state() { wp_update_post( $this->post ); $this->sender->do_sync(); - $event = $this->server_event_storage->get_most_recent_event(); + $event = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_save_post' ); $this->assertEquals( false, $event->args[3]['is_gutenberg_meta_box_update'] ); $this->post->post_content = "Updated using Gutenberg editor"; + $plugins = is_multisite() ? + get_site_option( 'active_sitewide_plugins') : + get_option( 'active_plugins' ); + + $updated_plugins = array_merge( $plugins, array( 'gutenberg/gutenberg.php' ) ); + + if ( is_multisite() ) { + update_site_option( 'active_sitewide_plugins', $updated_plugins ); + } else { + update_option( 'active_plugins', $updated_plugins ); + } + $_POST['action'] = 'editpost'; $_GET['classic-editor'] = '1'; $_GET['meta_box'] = '1'; @@ -131,13 +143,19 @@ public function test_update_post_includes_gutenberg_info_in_state() { wp_update_post( $this->post ); $this->sender->do_sync(); - $event = $this->server_event_storage->get_most_recent_event(); + $event = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_save_post' ); $this->assertEquals( true, $event->args[3]['is_gutenberg_meta_box_update'] ); unset( $_POST['action'] ); unset( $_GET['classic-editor'] ); unset( $_GET['meta_box'] ); + + if ( is_multisite() ) { + update_site_option( 'active_sitewide_plugins', $plugins ); + } else { + update_option( 'active_plugins', $plugins ); + } } public function test_update_post_updates_data() { From 33edbb0387f651a7a80fc2d5b9dba0827677bcb3 Mon Sep 17 00:00:00 2001 From: Rocco Tripaldi Date: Tue, 11 Sep 2018 16:26:03 -0400 Subject: [PATCH 3/3] Use jetpack helper method --- sync/class.jetpack-sync-module-posts.php | 2 +- .../sync/test_class.jetpack-sync-posts.php | 34 ------------------- 2 files changed, 1 insertion(+), 35 deletions(-) diff --git a/sync/class.jetpack-sync-module-posts.php b/sync/class.jetpack-sync-module-posts.php index 09c9d8f9f7432..6d19b39df543e 100644 --- a/sync/class.jetpack-sync-module-posts.php +++ b/sync/class.jetpack-sync-module-posts.php @@ -294,7 +294,7 @@ public function is_gutenberg_meta_box_update() { 'editpost' === $_POST['action'] && '1' === $_GET['classic-editor'] && '1' === $_GET['meta_box'] && - is_plugin_active( 'gutenberg/gutenberg.php' ) + Jetpack::is_gutenberg_available() ); } diff --git a/tests/php/sync/test_class.jetpack-sync-posts.php b/tests/php/sync/test_class.jetpack-sync-posts.php index 50ae5441dd6b2..780e86f5fab8d 100644 --- a/tests/php/sync/test_class.jetpack-sync-posts.php +++ b/tests/php/sync/test_class.jetpack-sync-posts.php @@ -122,40 +122,6 @@ public function test_update_post_includes_gutenberg_info_in_state() { $event = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_save_post' ); $this->assertEquals( false, $event->args[3]['is_gutenberg_meta_box_update'] ); - - $this->post->post_content = "Updated using Gutenberg editor"; - $plugins = is_multisite() ? - get_site_option( 'active_sitewide_plugins') : - get_option( 'active_plugins' ); - - $updated_plugins = array_merge( $plugins, array( 'gutenberg/gutenberg.php' ) ); - - if ( is_multisite() ) { - update_site_option( 'active_sitewide_plugins', $updated_plugins ); - } else { - update_option( 'active_plugins', $updated_plugins ); - } - - $_POST['action'] = 'editpost'; - $_GET['classic-editor'] = '1'; - $_GET['meta_box'] = '1'; - - wp_update_post( $this->post ); - - $this->sender->do_sync(); - $event = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_save_post' ); - - $this->assertEquals( true, $event->args[3]['is_gutenberg_meta_box_update'] ); - - unset( $_POST['action'] ); - unset( $_GET['classic-editor'] ); - unset( $_GET['meta_box'] ); - - if ( is_multisite() ) { - update_site_option( 'active_sitewide_plugins', $plugins ); - } else { - update_option( 'active_plugins', $plugins ); - } } public function test_update_post_updates_data() {