From 39975b1a956f8c4a05c88aa5010d698dcc3940b5 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 17:09:43 +1200 Subject: [PATCH 1/6] Rename sync_status top wp_sync_status and move to top level field of wp_block post type --- src/wp-includes/post.php | 4 ++-- .../rest-api/endpoints/class-wp-rest-blocks-controller.php | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index d6ab4db603a89..db685ec67c635 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8040,14 +8040,14 @@ function use_block_editor_for_post_type( $post_type ) { /** * Registers any additional post meta fields. * - * @since 6.3.0 Adds sync_status meta field to the wp_block post type so an unsynced option can be added. + * @since 6.3.0 Adds wp_sync_status meta field to the wp_block post type so an unsynced option can be added. * * @link https://github.com/WordPress/gutenberg/pull/51144 */ function wp_create_initial_post_meta() { register_post_meta( 'wp_block', - 'sync_status', + 'wp_sync_status', array( 'sanitize_callback' => 'sanitize_text_field', 'single' => true, diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php index cd2e63d7c5b22..3133f2e1328ac 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php @@ -40,6 +40,7 @@ public function check_read_permission( $post ) { * Filters a response based on the context defined in the schema. * * @since 5.0.0 + * @since 6.3 Adds the `wp_sync_status` postmeta property to the toplevel of response. * * @param array $data Response data to filter. * @param string $context Context defined in the schema. @@ -56,6 +57,10 @@ public function filter_response_by_context( $data, $context ) { unset( $data['title']['rendered'] ); unset( $data['content']['rendered'] ); + // Add the core wp_sync_status meta as top level property to the response. + $wp_sync_status = $data['meta']['wp_sync_status']; + $data['wp_sync_status'] = $wp_sync_status; + unset( $data['meta']['wp_sync_status'] ); return $data; } From 4a8245f8c18597e0ab5e49179af9cf7109e81f77 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 17:29:50 +1200 Subject: [PATCH 2/6] Changes from review --- src/wp-includes/post.php | 2 +- .../rest-api/endpoints/class-wp-rest-blocks-controller.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index db685ec67c635..f713412f5ed35 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8040,7 +8040,7 @@ function use_block_editor_for_post_type( $post_type ) { /** * Registers any additional post meta fields. * - * @since 6.3.0 Adds wp_sync_status meta field to the wp_block post type so an unsynced option can be added. + * @since 6.3.0 Adds `wp_sync_status` meta field to the wp_block post type so an unsynced option can be added. * * @link https://github.com/WordPress/gutenberg/pull/51144 */ diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php index 3133f2e1328ac..c971d81da9bcb 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php @@ -58,8 +58,7 @@ public function filter_response_by_context( $data, $context ) { unset( $data['content']['rendered'] ); // Add the core wp_sync_status meta as top level property to the response. - $wp_sync_status = $data['meta']['wp_sync_status']; - $data['wp_sync_status'] = $wp_sync_status; + $data['wp_sync_status'] = $data['meta']['wp_sync_status']; unset( $data['meta']['wp_sync_status'] ); return $data; } From cbb645037e43a850c52bbe68bd71d79cbbd42866 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 13:57:25 +1200 Subject: [PATCH 3/6] Switch to wp_pattern_sync_status --- src/wp-includes/post.php | 4 ++-- .../endpoints/class-wp-rest-blocks-controller.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index f713412f5ed35..a2963d00da00b 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8040,14 +8040,14 @@ function use_block_editor_for_post_type( $post_type ) { /** * Registers any additional post meta fields. * - * @since 6.3.0 Adds `wp_sync_status` meta field to the wp_block post type so an unsynced option can be added. + * @since 6.3.0 Adds `wp_pattern_sync_status` meta field to the wp_block post type so an unsynced option can be added. * * @link https://github.com/WordPress/gutenberg/pull/51144 */ function wp_create_initial_post_meta() { register_post_meta( 'wp_block', - 'wp_sync_status', + 'wp_pattern_sync_status', array( 'sanitize_callback' => 'sanitize_text_field', 'single' => true, diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php index c971d81da9bcb..8a5713fae7543 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php @@ -40,7 +40,7 @@ public function check_read_permission( $post ) { * Filters a response based on the context defined in the schema. * * @since 5.0.0 - * @since 6.3 Adds the `wp_sync_status` postmeta property to the toplevel of response. + * @since 6.3 Adds the `wp_pattern_sync_status` postmeta property to the toplevel of response. * * @param array $data Response data to filter. * @param string $context Context defined in the schema. @@ -57,9 +57,9 @@ public function filter_response_by_context( $data, $context ) { unset( $data['title']['rendered'] ); unset( $data['content']['rendered'] ); - // Add the core wp_sync_status meta as top level property to the response. - $data['wp_sync_status'] = $data['meta']['wp_sync_status']; - unset( $data['meta']['wp_sync_status'] ); + // Add the core wp_pattern_sync_status meta as top level property to the response. + $data['wp_pattern_sync_status'] = $data['meta']['wp_pattern_sync_status']; + unset( $data['meta']['wp_pattern_sync_status'] ); return $data; } From 24cc9ca6fbb7ce7ca78d4d26e1bfee88be78c2b0 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 15:38:30 +1200 Subject: [PATCH 4/6] Check that wp_pattern_sync_status is set before assigning --- .../rest-api/endpoints/class-wp-rest-blocks-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php index 8a5713fae7543..efd2fe9df6e55 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php @@ -58,7 +58,7 @@ public function filter_response_by_context( $data, $context ) { unset( $data['content']['rendered'] ); // Add the core wp_pattern_sync_status meta as top level property to the response. - $data['wp_pattern_sync_status'] = $data['meta']['wp_pattern_sync_status']; + $data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : ''; unset( $data['meta']['wp_pattern_sync_status'] ); return $data; } From bf2ccc2dd6189f7161ae4bb2f07397a790ba3359 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 5 Jul 2023 11:09:06 +1200 Subject: [PATCH 5/6] Add unit test --- .../tests/rest-api/rest-blocks-controller.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-blocks-controller.php b/tests/phpunit/tests/rest-api/rest-blocks-controller.php index 48208d2175ba3..597aefe635bf4 100644 --- a/tests/phpunit/tests/rest-api/rest-blocks-controller.php +++ b/tests/phpunit/tests/rest-api/rest-blocks-controller.php @@ -220,4 +220,39 @@ public function test_content() { $data['content'] ); } + + /** + * Check that the `wp_pattern_sync_status` postmeta is moved from meta array to top + * level of response. + * + * @ticket 58677 + */ + public function test_wp_patterns_sync_status_post_meta() { + register_post_meta( + 'wp_block', + 'wp_pattern_sync_status', + array( + 'single' => true, + 'type' => 'string', + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'string', + 'properties' => array( + 'sync_status' => array( + 'type' => 'string', + ), + ), + ), + ), + ) + ); + wp_set_current_user( self::$user_ids['author'] ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/blocks/' . self::$post_id ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertArrayHasKey( 'wp_pattern_sync_status', $data ); + $this->assertArrayNotHasKey( 'wp_pattern_sync_status', $data['meta'] ); + } } From ce0491b5fcd3215be67eafee45c7d5948133b37f Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 5 Jul 2023 11:15:00 +1200 Subject: [PATCH 6/6] Linting --- .../rest-api/endpoints/class-wp-rest-blocks-controller.php | 2 +- tests/phpunit/tests/rest-api/rest-blocks-controller.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php index efd2fe9df6e55..6028f42b37f4a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php @@ -40,7 +40,7 @@ public function check_read_permission( $post ) { * Filters a response based on the context defined in the schema. * * @since 5.0.0 - * @since 6.3 Adds the `wp_pattern_sync_status` postmeta property to the toplevel of response. + * @since 6.3 Adds the `wp_pattern_sync_status` postmeta property to the top level of response. * * @param array $data Response data to filter. * @param string $context Context defined in the schema. diff --git a/tests/phpunit/tests/rest-api/rest-blocks-controller.php b/tests/phpunit/tests/rest-api/rest-blocks-controller.php index 597aefe635bf4..1cdd3474dd174 100644 --- a/tests/phpunit/tests/rest-api/rest-blocks-controller.php +++ b/tests/phpunit/tests/rest-api/rest-blocks-controller.php @@ -232,9 +232,9 @@ public function test_wp_patterns_sync_status_post_meta() { 'wp_block', 'wp_pattern_sync_status', array( - 'single' => true, - 'type' => 'string', - 'show_in_rest' => array( + 'single' => true, + 'type' => 'string', + 'show_in_rest' => array( 'schema' => array( 'type' => 'string', 'properties' => array(