From 0100fede0103cfb2b9743965bf21ed6f1e203d14 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 13:23:39 +1200 Subject: [PATCH 01/14] Switch the sync_status to top level field on rest return instead of a meta field --- lib/compat/wordpress-6.3/blocks.php | 1 + ...class-gutenberg-rest-blocks-controller.php | 47 +++++++++++++++++++ lib/load.php | 1 + packages/block-editor/src/store/selectors.js | 10 ++-- .../components/page-patterns/use-patterns.js | 2 +- .../use-pattern-details.js | 2 +- .../src/components/post-sync-status/index.js | 6 +-- 7 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php diff --git a/lib/compat/wordpress-6.3/blocks.php b/lib/compat/wordpress-6.3/blocks.php index b338d0a2467096..6d432ff7b2e942 100644 --- a/lib/compat/wordpress-6.3/blocks.php +++ b/lib/compat/wordpress-6.3/blocks.php @@ -60,6 +60,7 @@ function gutenberg_rename_reusable_block_cpt_to_pattern( $args, $post_type ) { $args['labels']['item_reverted_to_draft'] = __( 'Pattern reverted to draft.' ); $args['labels']['item_scheduled'] = __( 'Pattern scheduled.' ); $args['labels']['item_updated'] = __( 'Pattern updated.' ); + $args['rest_controller_class'] = 'Gutenberg_REST_Blocks_Controller'; } return $args; diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php new file mode 100644 index 00000000000000..0464dbb8a5def7 --- /dev/null +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -0,0 +1,47 @@ + // Filter to either fully synced patterns (sync_status === 'fully'), // or old school reusable blocks (sync_status === ''). - reusableBlock.meta?.sync_status === 'fully' || - reusableBlock.meta?.sync_status === '' || - ! reusableBlock.meta?.sync_status + reusableBlock.sync_status === 'fully' || + reusableBlock.sync_status === '' || + ! reusableBlock.sync_status ) .map( buildReusableBlockInserterItem ) : []; @@ -2312,9 +2312,7 @@ function getUnsyncedPatterns( state ) { state?.settings?.__experimentalReusableBlocks ?? EMPTY_ARRAY; return reusableBlocks - .filter( - ( reusableBlock ) => reusableBlock.meta?.sync_status === 'unsynced' - ) + .filter( ( reusableBlock ) => reusableBlock.sync_status === 'unsynced' ) .map( ( reusableBlock ) => { return { name: `core/block/${ reusableBlock.id }`, diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js index a8d76b58cb45d5..d60421e3e59918 100644 --- a/packages/edit-site/src/components/page-patterns/use-patterns.js +++ b/packages/edit-site/src/components/page-patterns/use-patterns.js @@ -154,7 +154,7 @@ const reusableBlockToPattern = ( reusableBlock ) => ( { categories: reusableBlock.wp_pattern, id: reusableBlock.id, name: reusableBlock.slug, - syncStatus: reusableBlock.meta?.sync_status || SYNC_TYPES.full, + syncStatus: reusableBlock.sync_status || SYNC_TYPES.full, title: reusableBlock.title.raw, type: reusableBlock.type, reusableBlock, diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js b/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js index dfc367ea0b97dc..00cd449b82d900 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js @@ -95,7 +95,7 @@ export default function usePatternDetails( postType, postId ) { details.push( { label: __( 'Syncing' ), value: - record.meta?.sync_status === 'unsynced' + record.sync_status === 'unsynced' ? __( 'Not synced' ) : __( 'Fully synced' ), } ); diff --git a/packages/editor/src/components/post-sync-status/index.js b/packages/editor/src/components/post-sync-status/index.js index c384fd234c7a34..7b5f7c1df8a35b 100644 --- a/packages/editor/src/components/post-sync-status/index.js +++ b/packages/editor/src/components/post-sync-status/index.js @@ -11,17 +11,17 @@ import { PanelRow } from '@wordpress/components'; import { store as editorStore } from '../../store'; export default function PostSyncStatus() { - const { meta, postType } = useSelect( ( select ) => { + const { syncStatus, postType } = useSelect( ( select ) => { const { getEditedPostAttribute } = select( editorStore ); return { - meta: getEditedPostAttribute( 'meta' ), + syncStatus: getEditedPostAttribute( 'sync_status' ), postType: getEditedPostAttribute( 'type' ), }; }, [] ); if ( postType !== 'wp_block' ) { return null; } - const syncStatus = meta?.sync_status; + const isFullySynced = ! syncStatus; return ( From 9a6b6008add6aca0e62e687ea8af02a50e8ac8a6 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 13:48:24 +1200 Subject: [PATCH 02/14] Add comment --- .../wordpress-6.3/class-gutenberg-rest-blocks-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 0464dbb8a5def7..0dd3d4b4acda71 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -39,6 +39,7 @@ public function filter_response_by_context( $data, $context ) { unset( $data['title']['rendered'] ); unset( $data['content']['rendered'] ); + // Add the core sync_status meta as top level property to the response. $sync_status = $data['meta']['sync_status']; $data['sync_status'] = $sync_status ; unset( $data['meta']['sync_status'] ); From 06c0f179e8acc20faa99b421ac700b050d2d54eb Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 13:50:50 +1200 Subject: [PATCH 03/14] Fix tab indents --- .../class-gutenberg-rest-blocks-controller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 0dd3d4b4acda71..222a4c07b1b727 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -40,9 +40,9 @@ public function filter_response_by_context( $data, $context ) { unset( $data['content']['rendered'] ); // Add the core sync_status meta as top level property to the response. - $sync_status = $data['meta']['sync_status']; - $data['sync_status'] = $sync_status ; - unset( $data['meta']['sync_status'] ); - return $data; + $sync_status = $data['meta']['sync_status']; + $data['sync_status'] = $sync_status ; + unset( $data['meta']['sync_status'] ); + return $data; } } From 1c1e2beb4d5114407502d590e7e545fcd14f70b8 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 13:53:39 +1200 Subject: [PATCH 04/14] This time indents are correct --- .../class-gutenberg-rest-blocks-controller.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 222a4c07b1b727..7c6b1f966637b6 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -23,7 +23,7 @@ class Gutenberg_REST_Blocks_Controller extends WP_REST_Blocks_Controller { * * @since 5.0.0 * @since 6.3 Adds the `sync_status` property to the response. - * + * * @param array $data Response data to filter. * @param string $context Context defined in the schema. * @return array Filtered response. @@ -39,10 +39,10 @@ public function filter_response_by_context( $data, $context ) { unset( $data['title']['rendered'] ); unset( $data['content']['rendered'] ); - // Add the core sync_status meta as top level property to the response. - $sync_status = $data['meta']['sync_status']; - $data['sync_status'] = $sync_status ; - unset( $data['meta']['sync_status'] ); - return $data; + // Add the core sync_status meta as top level property to the response. + $sync_status = $data['meta']['sync_status']; + $data['sync_status'] = $sync_status ; + unset( $data['meta']['sync_status'] ); + return $data; } } From c7366fb7cb07b2aac5b95a6f53893e0da465ef9d Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 14:10:00 +1200 Subject: [PATCH 05/14] Stop lying to me VSCode linter! --- .../class-gutenberg-rest-blocks-controller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 7c6b1f966637b6..c2cd22fd23a232 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -17,7 +17,7 @@ * @see WP_REST_Posts_Controller * @see WP_REST_Controller */ -class Gutenberg_REST_Blocks_Controller extends WP_REST_Blocks_Controller { +class Gutenberg_REST_Blocks_Controller extends WP_REST_Blocks_Controller { /** * Filters a response based on the context defined in the schema. * @@ -38,10 +38,10 @@ public function filter_response_by_context( $data, $context ) { */ unset( $data['title']['rendered'] ); unset( $data['content']['rendered'] ); - + // Add the core sync_status meta as top level property to the response. - $sync_status = $data['meta']['sync_status']; - $data['sync_status'] = $sync_status ; + $sync_status = $data['meta']['sync_status']; + $data['sync_status'] = $sync_status; unset( $data['meta']['sync_status'] ); return $data; } From 0ba1a18369c3495beeb949ba4ac12fb94e8e674c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 16:53:47 +1200 Subject: [PATCH 06/14] Rename sync_status to wp_sync_status --- lib/compat/wordpress-6.3/blocks.php | 8 ++++---- .../class-gutenberg-rest-blocks-controller.php | 8 ++++---- packages/block-editor/src/store/selectors.js | 14 ++++++++------ .../src/components/create-pattern-modal/index.js | 2 +- .../src/components/page-patterns/use-patterns.js | 2 +- .../use-pattern-details.js | 2 +- .../src/components/post-sync-status/index.js | 2 +- packages/reusable-blocks/src/store/actions.js | 2 +- 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/compat/wordpress-6.3/blocks.php b/lib/compat/wordpress-6.3/blocks.php index 6d432ff7b2e942..b3523d06c37d62 100644 --- a/lib/compat/wordpress-6.3/blocks.php +++ b/lib/compat/wordpress-6.3/blocks.php @@ -90,7 +90,7 @@ function gutenberg_add_custom_fields_to_wp_block( $args, $post_type ) { add_filter( 'register_post_type_args', 'gutenberg_add_custom_fields_to_wp_block', 10, 2 ); /** - * Adds sync_status meta fields to the wp_block post type so an unsynced option can be added. + * Adds wp_sync_status meta fields to the wp_block post type so an unsynced option can be added. * * Note: This should be removed when the minimum required WP version is >= 6.3. * @@ -102,7 +102,7 @@ function gutenberg_wp_block_register_post_meta() { $post_type = 'wp_block'; register_post_meta( $post_type, - 'sync_status', + 'wp_sync_status', array( 'auth_callback' => function() { return current_user_can( 'edit_posts' ); @@ -114,7 +114,7 @@ function gutenberg_wp_block_register_post_meta() { 'schema' => array( 'type' => 'string', 'properties' => array( - 'sync_status' => array( + 'wp_sync_status' => array( 'type' => 'string', ), ), @@ -124,7 +124,7 @@ function gutenberg_wp_block_register_post_meta() { ); } /** - * Sanitizes the array of wp_block post meta sync_status string. + * Sanitizes the array of wp_block post meta wp_sync_status string. * * Note: This should be removed when the minimum required WP version is >= 6.3. * diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index c2cd22fd23a232..3c0d6c6ff30188 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -22,7 +22,7 @@ class Gutenberg_REST_Blocks_Controller extends WP_REST_Blocks_Controller { * Filters a response based on the context defined in the schema. * * @since 5.0.0 - * @since 6.3 Adds the `sync_status` property to the response. + * @since 6.3 Adds the `wp_sync_status` property to the response. * * @param array $data Response data to filter. * @param string $context Context defined in the schema. @@ -40,9 +40,9 @@ public function filter_response_by_context( $data, $context ) { unset( $data['content']['rendered'] ); // Add the core sync_status meta as top level property to the response. - $sync_status = $data['meta']['sync_status']; - $data['sync_status'] = $sync_status; - unset( $data['meta']['sync_status'] ); + $wp_sync_status = $data['meta']['wp_sync_status']; + $data['wp_sync_status'] = $wp_sync_status; + unset( $data['meta']['wp_sync_status'] ); return $data; } } diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index bf09317b5e077d..96e119774dc8c1 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2034,11 +2034,11 @@ export const getInserterItems = createSelector( ? getReusableBlocks( state ) .filter( ( reusableBlock ) => - // Filter to either fully synced patterns (sync_status === 'fully'), - // or old school reusable blocks (sync_status === ''). - reusableBlock.sync_status === 'fully' || - reusableBlock.sync_status === '' || - ! reusableBlock.sync_status + // Filter to either fully synced patterns (wp_sync_status === 'fully'), + // or old school reusable blocks (wp_sync_status === ''). + reusableBlock.wp_sync_status === 'fully' || + reusableBlock.wp_sync_status === '' || + ! reusableBlock.wp_sync_status ) .map( buildReusableBlockInserterItem ) : []; @@ -2312,7 +2312,9 @@ function getUnsyncedPatterns( state ) { state?.settings?.__experimentalReusableBlocks ?? EMPTY_ARRAY; return reusableBlocks - .filter( ( reusableBlock ) => reusableBlock.sync_status === 'unsynced' ) + .filter( + ( reusableBlock ) => reusableBlock.wp_sync_status === 'unsynced' + ) .map( ( reusableBlock ) => { return { name: `core/block/${ reusableBlock.id }`, diff --git a/packages/edit-site/src/components/create-pattern-modal/index.js b/packages/edit-site/src/components/create-pattern-modal/index.js index 7906cb2352c7b7..09bbee5b9cebbf 100644 --- a/packages/edit-site/src/components/create-pattern-modal/index.js +++ b/packages/edit-site/src/components/create-pattern-modal/index.js @@ -56,7 +56,7 @@ export default function CreatePatternModal( { status: 'publish', meta: syncType === SYNC_TYPES.unsynced - ? { sync_status: syncType } + ? { wp_sync_status: syncType } : undefined, }, { throwOnError: true } diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js index d60421e3e59918..24a7013c9b87c3 100644 --- a/packages/edit-site/src/components/page-patterns/use-patterns.js +++ b/packages/edit-site/src/components/page-patterns/use-patterns.js @@ -154,7 +154,7 @@ const reusableBlockToPattern = ( reusableBlock ) => ( { categories: reusableBlock.wp_pattern, id: reusableBlock.id, name: reusableBlock.slug, - syncStatus: reusableBlock.sync_status || SYNC_TYPES.full, + syncStatus: reusableBlock.wp_sync_status || SYNC_TYPES.full, title: reusableBlock.title.raw, type: reusableBlock.type, reusableBlock, diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js b/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js index 00cd449b82d900..d5a2541f2f142e 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js @@ -95,7 +95,7 @@ export default function usePatternDetails( postType, postId ) { details.push( { label: __( 'Syncing' ), value: - record.sync_status === 'unsynced' + record.wp_sync_status === 'unsynced' ? __( 'Not synced' ) : __( 'Fully synced' ), } ); diff --git a/packages/editor/src/components/post-sync-status/index.js b/packages/editor/src/components/post-sync-status/index.js index 7b5f7c1df8a35b..7cd9b635eb7841 100644 --- a/packages/editor/src/components/post-sync-status/index.js +++ b/packages/editor/src/components/post-sync-status/index.js @@ -14,7 +14,7 @@ export default function PostSyncStatus() { const { syncStatus, postType } = useSelect( ( select ) => { const { getEditedPostAttribute } = select( editorStore ); return { - syncStatus: getEditedPostAttribute( 'sync_status' ), + syncStatus: getEditedPostAttribute( 'wp_sync_status' ), postType: getEditedPostAttribute( 'type' ), }; }, [] ); diff --git a/packages/reusable-blocks/src/store/actions.js b/packages/reusable-blocks/src/store/actions.js index aae706adfab36a..047deea4b4b000 100644 --- a/packages/reusable-blocks/src/store/actions.js +++ b/packages/reusable-blocks/src/store/actions.js @@ -52,7 +52,7 @@ export const __experimentalConvertBlocksToReusable = const meta = syncType === 'unsynced' ? { - sync_status: syncType, + wp_sync_status: syncType, } : undefined; From 41ed77be374fc0cef860e369439ffab529950bd2 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 17:03:09 +1200 Subject: [PATCH 07/14] Align equals --- .../wordpress-6.3/class-gutenberg-rest-blocks-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 3c0d6c6ff30188..14940e6f314ebe 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -40,7 +40,7 @@ public function filter_response_by_context( $data, $context ) { unset( $data['content']['rendered'] ); // Add the core sync_status meta as top level property to the response. - $wp_sync_status = $data['meta']['wp_sync_status']; + $wp_sync_status = $data['meta']['wp_sync_status']; $data['wp_sync_status'] = $wp_sync_status; unset( $data['meta']['wp_sync_status'] ); return $data; From 06bbdfe856ddd85262c06a5c6bb10101a3197171 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 30 Jun 2023 17:33:47 +1200 Subject: [PATCH 08/14] remove redundant var declaration --- .../wordpress-6.3/class-gutenberg-rest-blocks-controller.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 14940e6f314ebe..2a5f1a2729b1ae 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -40,8 +40,7 @@ public function filter_response_by_context( $data, $context ) { unset( $data['content']['rendered'] ); // Add the core 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 70ac0f378e1a19cfcda44f8b106171b27ee8e883 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 3 Jul 2023 19:14:18 +1200 Subject: [PATCH 09/14] Rename to 'wp_pattern_sync_status' --- lib/compat/wordpress-6.3/blocks.php | 26 +++---------------- ...class-gutenberg-rest-blocks-controller.php | 6 ++--- packages/block-editor/src/store/selectors.js | 13 +++++----- .../components/create-pattern-modal/index.js | 2 +- .../components/page-patterns/use-patterns.js | 2 +- .../use-pattern-details.js | 2 +- .../src/components/post-sync-status/index.js | 2 +- packages/reusable-blocks/src/store/actions.js | 2 +- 8 files changed, 19 insertions(+), 36 deletions(-) diff --git a/lib/compat/wordpress-6.3/blocks.php b/lib/compat/wordpress-6.3/blocks.php index b3523d06c37d62..d0c03b629247fc 100644 --- a/lib/compat/wordpress-6.3/blocks.php +++ b/lib/compat/wordpress-6.3/blocks.php @@ -102,39 +102,21 @@ function gutenberg_wp_block_register_post_meta() { $post_type = 'wp_block'; register_post_meta( $post_type, - 'wp_sync_status', + 'wp_pattern_sync_status', array( 'auth_callback' => function() { return current_user_can( 'edit_posts' ); }, - 'sanitize_callback' => 'gutenberg_wp_block_sanitize_post_meta', + 'sanitize_callback' => 'sanitize_text_field', 'single' => true, 'type' => 'string', 'show_in_rest' => array( 'schema' => array( - 'type' => 'string', - 'properties' => array( - 'wp_sync_status' => array( - 'type' => 'string', - ), - ), + 'type' => 'string', + 'enum' => array( 'partial', 'unsynced' ), ), ), ) ); } -/** - * Sanitizes the array of wp_block post meta wp_sync_status string. - * - * Note: This should be removed when the minimum required WP version is >= 6.3. - * - * @see https://github.com/WordPress/gutenberg/pull/51144 - * - * @param array $meta_value String to sanitize. - * - * @return array Sanitized string. - */ -function gutenberg_wp_block_sanitize_post_meta( $meta_value ) { - return sanitize_text_field( $meta_value ); -} add_action( 'init', 'gutenberg_wp_block_register_post_meta' ); diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 2a5f1a2729b1ae..12bc30d1fbd89f 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -22,7 +22,7 @@ class Gutenberg_REST_Blocks_Controller extends WP_REST_Blocks_Controller { * Filters a response based on the context defined in the schema. * * @since 5.0.0 - * @since 6.3 Adds the `wp_sync_status` property to the response. + * @since 6.3 Adds the `wp_pattern_sync_status` property to the response. * * @param array $data Response data to filter. * @param string $context Context defined in the schema. @@ -40,8 +40,8 @@ public function filter_response_by_context( $data, $context ) { unset( $data['content']['rendered'] ); // Add the core 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'] ); + $data['wp_pattern_sync_status'] = $data['meta']['wp_pattern_sync_status']; + unset( $data['meta']['wp_pattern_sync_status'] ); return $data; } } diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 96e119774dc8c1..2603a68a7120c7 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2034,11 +2034,11 @@ export const getInserterItems = createSelector( ? getReusableBlocks( state ) .filter( ( reusableBlock ) => - // Filter to either fully synced patterns (wp_sync_status === 'fully'), - // or old school reusable blocks (wp_sync_status === ''). - reusableBlock.wp_sync_status === 'fully' || - reusableBlock.wp_sync_status === '' || - ! reusableBlock.wp_sync_status + // Filter to either fully synced patterns (wp_pattern_sync_status === 'fully'), + // or old school reusable blocks (wp_pattern_sync_status === ''). + reusableBlock.wp_pattern_sync_status === 'fully' || + reusableBlock.wp_pattern_sync_status === '' || + ! reusableBlock.wp_pattern_sync_status ) .map( buildReusableBlockInserterItem ) : []; @@ -2313,7 +2313,8 @@ function getUnsyncedPatterns( state ) { return reusableBlocks .filter( - ( reusableBlock ) => reusableBlock.wp_sync_status === 'unsynced' + ( reusableBlock ) => + reusableBlock.wp_pattern_sync_status === 'unsynced' ) .map( ( reusableBlock ) => { return { diff --git a/packages/edit-site/src/components/create-pattern-modal/index.js b/packages/edit-site/src/components/create-pattern-modal/index.js index 09bbee5b9cebbf..46d734b86fdd19 100644 --- a/packages/edit-site/src/components/create-pattern-modal/index.js +++ b/packages/edit-site/src/components/create-pattern-modal/index.js @@ -56,7 +56,7 @@ export default function CreatePatternModal( { status: 'publish', meta: syncType === SYNC_TYPES.unsynced - ? { wp_sync_status: syncType } + ? { wp_pattern_sync_status: syncType } : undefined, }, { throwOnError: true } diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js index 24a7013c9b87c3..cef7b4721193f4 100644 --- a/packages/edit-site/src/components/page-patterns/use-patterns.js +++ b/packages/edit-site/src/components/page-patterns/use-patterns.js @@ -154,7 +154,7 @@ const reusableBlockToPattern = ( reusableBlock ) => ( { categories: reusableBlock.wp_pattern, id: reusableBlock.id, name: reusableBlock.slug, - syncStatus: reusableBlock.wp_sync_status || SYNC_TYPES.full, + syncStatus: reusableBlock.wp_pattern_sync_status || SYNC_TYPES.full, title: reusableBlock.title.raw, type: reusableBlock.type, reusableBlock, diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js b/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js index d5a2541f2f142e..9853e2e6de23bc 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js @@ -95,7 +95,7 @@ export default function usePatternDetails( postType, postId ) { details.push( { label: __( 'Syncing' ), value: - record.wp_sync_status === 'unsynced' + record.wp_pattern_sync_status === 'unsynced' ? __( 'Not synced' ) : __( 'Fully synced' ), } ); diff --git a/packages/editor/src/components/post-sync-status/index.js b/packages/editor/src/components/post-sync-status/index.js index 7cd9b635eb7841..22de1396d06790 100644 --- a/packages/editor/src/components/post-sync-status/index.js +++ b/packages/editor/src/components/post-sync-status/index.js @@ -14,7 +14,7 @@ export default function PostSyncStatus() { const { syncStatus, postType } = useSelect( ( select ) => { const { getEditedPostAttribute } = select( editorStore ); return { - syncStatus: getEditedPostAttribute( 'wp_sync_status' ), + syncStatus: getEditedPostAttribute( 'wp_pattern_sync_status' ), postType: getEditedPostAttribute( 'type' ), }; }, [] ); diff --git a/packages/reusable-blocks/src/store/actions.js b/packages/reusable-blocks/src/store/actions.js index 047deea4b4b000..17a2e83d5e776a 100644 --- a/packages/reusable-blocks/src/store/actions.js +++ b/packages/reusable-blocks/src/store/actions.js @@ -52,7 +52,7 @@ export const __experimentalConvertBlocksToReusable = const meta = syncType === 'unsynced' ? { - wp_sync_status: syncType, + wp_pattern_sync_status: syncType, } : undefined; From 492c4eeaa858ac03505b3e472623b45b7974c835 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 13:51:37 +1200 Subject: [PATCH 10/14] Add filter to map existing sync_status values --- lib/blocks.php | 28 +++++++++++++++++++ lib/compat/wordpress-6.3/blocks.php | 2 +- ...class-gutenberg-rest-blocks-controller.php | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 8185567db1b804..e98f711b5c85a5 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -372,3 +372,31 @@ function gutenberg_register_legacy_social_link_blocks() { } add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); + +/** + * Migrate the legacy `sync_status` meta key (added 16.1) to the new `wp_pattern_sync_status` meta key (16.1.1). + * + * This filter is INTENTIONALLY left out of core as the meta key was fist introduced to core in 6.3 as `wp_pattern_sync_status`. + * see https://github.com/WordPress/gutenberg/pull/52232 + * + * @param mixed $value The value to return, either a single metadata value or an array of values depending on the value of $single. + * @param int $object_id ID of the object metadata is for. + * @param string $meta_key Metadata key. + * @param bool $single Whether to return only the first value of the specified $meta_key. + */ +function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $single ) { + if ( 'wp_pattern_sync_status' !== $meta_key ) { + return $value; + } + + $sync_status = get_post_meta( $object_id, 'sync_status', $single ); + + if ( $single && 'unsynced' === $sync_status ) { + return $sync_status; + } elseif ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) { + return $sync_status; + } + + return $value; +} +add_filter( 'default_post_metadata', 'gutenberg_legacy_wp_block_post_meta', 10, 4 ); diff --git a/lib/compat/wordpress-6.3/blocks.php b/lib/compat/wordpress-6.3/blocks.php index d0c03b629247fc..ccc68786dc6adb 100644 --- a/lib/compat/wordpress-6.3/blocks.php +++ b/lib/compat/wordpress-6.3/blocks.php @@ -90,7 +90,7 @@ function gutenberg_add_custom_fields_to_wp_block( $args, $post_type ) { add_filter( 'register_post_type_args', 'gutenberg_add_custom_fields_to_wp_block', 10, 2 ); /** - * Adds wp_sync_status meta fields to the wp_block post type so an unsynced option can be added. + * Adds wp_pattern_sync_status meta fields to the wp_block post type so an unsynced option can be added. * * Note: This should be removed when the minimum required WP version is >= 6.3. * diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 12bc30d1fbd89f..6f452e7fd3909b 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -39,7 +39,7 @@ public function filter_response_by_context( $data, $context ) { unset( $data['title']['rendered'] ); unset( $data['content']['rendered'] ); - // Add the core sync_status meta as top level property to the response. + // 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 1414c6f0752d939f8bfd84d3afdd9e5c9be0dd37 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 13:53:58 +1200 Subject: [PATCH 11/14] Remove check for `fully` status --- packages/block-editor/src/store/selectors.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 2603a68a7120c7..b551422d80d6ed 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2034,9 +2034,6 @@ export const getInserterItems = createSelector( ? getReusableBlocks( state ) .filter( ( reusableBlock ) => - // Filter to either fully synced patterns (wp_pattern_sync_status === 'fully'), - // or old school reusable blocks (wp_pattern_sync_status === ''). - reusableBlock.wp_pattern_sync_status === 'fully' || reusableBlock.wp_pattern_sync_status === '' || ! reusableBlock.wp_pattern_sync_status ) From c57b682d15b601bb015161b574000099db0c3d6e Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 14:58:44 +1200 Subject: [PATCH 12/14] Add check for fully back --- packages/block-editor/src/store/selectors.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index b551422d80d6ed..65130faf51dd93 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2034,8 +2034,13 @@ export const getInserterItems = createSelector( ? getReusableBlocks( state ) .filter( ( reusableBlock ) => - reusableBlock.wp_pattern_sync_status === '' || - ! reusableBlock.wp_pattern_sync_status + // Reusable blocks that are fully synced should have no sync status set + // for backwards compat between patterns and old reusable blocks, but + // some in release 16.1 may have had sync status inadvertantly set to + // 'fully' if created in the site editor. + reusableBlock.meta?.sync_status === 'fully' || + reusableBlock.meta?.sync_status === '' || + ! reusableBlock.meta?.sync_status ) .map( buildReusableBlockInserterItem ) : []; From 31df533cb521411444fa6f0bf69126359bb35b46 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 15:10:42 +1200 Subject: [PATCH 13/14] Check meta field is still set before assigning --- .../wordpress-6.3/class-gutenberg-rest-blocks-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php index 6f452e7fd3909b..08108e1638334c 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-blocks-controller.php @@ -40,7 +40,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 49c2606d9d09634040794520f64ba9d34a72cd0c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 15:14:44 +1200 Subject: [PATCH 14/14] Fix filter params --- packages/block-editor/src/store/selectors.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 65130faf51dd93..4cca99535a8e50 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2038,9 +2038,9 @@ export const getInserterItems = createSelector( // for backwards compat between patterns and old reusable blocks, but // some in release 16.1 may have had sync status inadvertantly set to // 'fully' if created in the site editor. - reusableBlock.meta?.sync_status === 'fully' || - reusableBlock.meta?.sync_status === '' || - ! reusableBlock.meta?.sync_status + reusableBlock.wp_pattern_sync_status === 'fully' || + reusableBlock.wp_pattern_sync_status === '' || + ! reusableBlock.wp_pattern_sync_status ) .map( buildReusableBlockInserterItem ) : [];