Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync: send gutenberg info when syncing posts #10130

Merged
merged 3 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion sync/class.jetpack-sync-module-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/php/sync/test_class.jetpack-sync-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down