From 1cf6009edebeb170b5aefd3ac0867f377d49734c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 3 Jul 2023 17:11:21 +1200 Subject: [PATCH 1/8] Rename sync_status to pattern_post_sync_status --- lib/compat/wordpress-6.3/blocks.php | 8 ++++---- packages/block-editor/src/store/selectors.js | 10 ++++++---- .../src/components/create-pattern-modal/index.js | 2 +- .../src/components/page-library/use-patterns.js | 2 +- .../editor/src/components/post-sync-status/index.js | 2 +- packages/reusable-blocks/src/store/actions.js | 2 +- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/compat/wordpress-6.3/blocks.php b/lib/compat/wordpress-6.3/blocks.php index b338d0a2467096..a3a46f3f545f45 100644 --- a/lib/compat/wordpress-6.3/blocks.php +++ b/lib/compat/wordpress-6.3/blocks.php @@ -89,7 +89,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_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. * @@ -101,7 +101,7 @@ function gutenberg_wp_block_register_post_meta() { $post_type = 'wp_block'; register_post_meta( $post_type, - 'sync_status', + 'wp_pattern_sync_status', array( 'auth_callback' => function() { return current_user_can( 'edit_posts' ); @@ -113,7 +113,7 @@ function gutenberg_wp_block_register_post_meta() { 'schema' => array( 'type' => 'string', 'properties' => array( - 'sync_status' => array( + 'wp_pattern_sync_status' => array( 'type' => 'string', ), ), @@ -123,7 +123,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_pattern_sync_status string. * * Note: This should be removed when the minimum required WP version is >= 6.3. * diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 1ab3606ba68963..616e81135d6821 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2045,11 +2045,13 @@ export const getInserterItems = createSelector( ? getReusableBlocks( state ) .filter( ( reusableBlock ) => - syncStatus === reusableBlock.meta?.sync_status || + syncStatus === + reusableBlock.meta?.wp_pattern_sync_status || ( ! syncStatus && - ( reusableBlock.meta?.sync_status === '' || - reusableBlock.meta?.sync_status === - 'fully' ) ) // Only reusable blocks added via site editor in release 16.1 will have sync_status of 'fully'. + ( reusableBlock.meta?.wp_pattern_sync_status === + '' || + reusableBlock.meta + ?.wp_pattern_sync_status === 'fully' ) ) // Only reusable blocks added via site editor in release 16.1 will have wp_pattern_sync_status of 'fully'. ) .map( buildReusableBlockInserterItem ) : []; 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 5180ad5d870684..4be54b12ab030b 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_pattern_sync_status: syncType } : undefined, }, { throwOnError: true } diff --git a/packages/edit-site/src/components/page-library/use-patterns.js b/packages/edit-site/src/components/page-library/use-patterns.js index 6f3b7407e917dd..507417021688a7 100644 --- a/packages/edit-site/src/components/page-library/use-patterns.js +++ b/packages/edit-site/src/components/page-library/use-patterns.js @@ -145,7 +145,7 @@ const reusableBlockToPattern = ( reusableBlock ) => ( { categories: reusableBlock.wp_pattern, id: reusableBlock.id, name: reusableBlock.slug, - syncStatus: reusableBlock.meta?.sync_status || SYNC_TYPES.full, + syncStatus: reusableBlock.meta?.wp_pattern_sync_status || SYNC_TYPES.full, title: reusableBlock.title.raw, type: reusableBlock.type, reusableBlock, diff --git a/packages/editor/src/components/post-sync-status/index.js b/packages/editor/src/components/post-sync-status/index.js index c384fd234c7a34..a3a1896f1093ee 100644 --- a/packages/editor/src/components/post-sync-status/index.js +++ b/packages/editor/src/components/post-sync-status/index.js @@ -21,7 +21,7 @@ export default function PostSyncStatus() { if ( postType !== 'wp_block' ) { return null; } - const syncStatus = meta?.sync_status; + const syncStatus = meta?.wp_pattern_sync_status; const isFullySynced = ! syncStatus; return ( diff --git a/packages/reusable-blocks/src/store/actions.js b/packages/reusable-blocks/src/store/actions.js index aae706adfab36a..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' ? { - sync_status: syncType, + wp_pattern_sync_status: syncType, } : undefined; From eedd80d5bcf745b5efbeabac673726ea7b74ede0 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Mon, 3 Jul 2023 17:42:37 +0800 Subject: [PATCH 2/8] Migrate sync_status to wp_pattern_sync_status --- lib/compat/wordpress-6.3/blocks.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/compat/wordpress-6.3/blocks.php b/lib/compat/wordpress-6.3/blocks.php index a3a46f3f545f45..b4f9f8d4b23eed 100644 --- a/lib/compat/wordpress-6.3/blocks.php +++ b/lib/compat/wordpress-6.3/blocks.php @@ -137,3 +137,31 @@ function gutenberg_wp_block_sanitize_post_meta( $meta_value ) { return sanitize_text_field( $meta_value ); } add_action( 'init', 'gutenberg_wp_block_register_post_meta' ); + +/** + * Migrate the legacy `sync_status` meta key to the new `wp_pattern_sync_status` meta key. + * + * Note: This should be removed when the minimum required WP version is >= 6.3. + * + * @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. + * + * @see https://github.com/WordPress/gutenberg/pull/52232 + */ +function gutenberg_wp_block_migrate_post_meta( $value, $object_id, $meta_key, $single ) { + if ( 'wp_pattern_sync_status' !== $meta_key || null !== $value ) { + return $value; + } + + $sync_status = get_post_meta( $object_id, $meta_key, $single ); + + if ( $sync_status ) { + return $value; + } + + // Return legacy meta key. + return get_post_meta( $object_id, 'sync_status', $single ); +} +add_filter( 'get_post_metadata', 'gutenberg_wp_block_migrate_post_meta', 10, 4 ); From 93cddbe18f8d7da74d4c1ce60edda659486584c4 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 10:28:42 +1200 Subject: [PATCH 3/8] Switch to using default_post_metadata filter and move it to the permanent blocks.php file --- lib/blocks.php | 27 +++++++++++++++++++++++++++ lib/compat/wordpress-6.3/blocks.php | 28 ---------------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 8185567db1b804..c810b60ff368b1 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -372,3 +372,30 @@ function gutenberg_register_legacy_social_link_blocks() { } add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); + +/** + * Migrate the legacy `sync_status` meta key to the new `wp_pattern_sync_status` meta key. + * + * @since 16.1.1 + * + * @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. + * + * @see https://github.com/WordPress/gutenberg/pull/52232 + */ +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 ( isset( $sync_status[0] ) && $sync_status[0] === 'unsynced' ) { + 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 b4f9f8d4b23eed..a3a46f3f545f45 100644 --- a/lib/compat/wordpress-6.3/blocks.php +++ b/lib/compat/wordpress-6.3/blocks.php @@ -137,31 +137,3 @@ function gutenberg_wp_block_sanitize_post_meta( $meta_value ) { return sanitize_text_field( $meta_value ); } add_action( 'init', 'gutenberg_wp_block_register_post_meta' ); - -/** - * Migrate the legacy `sync_status` meta key to the new `wp_pattern_sync_status` meta key. - * - * Note: This should be removed when the minimum required WP version is >= 6.3. - * - * @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. - * - * @see https://github.com/WordPress/gutenberg/pull/52232 - */ -function gutenberg_wp_block_migrate_post_meta( $value, $object_id, $meta_key, $single ) { - if ( 'wp_pattern_sync_status' !== $meta_key || null !== $value ) { - return $value; - } - - $sync_status = get_post_meta( $object_id, $meta_key, $single ); - - if ( $sync_status ) { - return $value; - } - - // Return legacy meta key. - return get_post_meta( $object_id, 'sync_status', $single ); -} -add_filter( 'get_post_metadata', 'gutenberg_wp_block_migrate_post_meta', 10, 4 ); From afc7105af2531fdd4d6ee75416fb57b3e9c04b92 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 10:34:16 +1200 Subject: [PATCH 4/8] Switch to yoda condition --- lib/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/blocks.php b/lib/blocks.php index c810b60ff368b1..c757a19880c55d 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -392,7 +392,7 @@ function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $si $sync_status = get_post_meta( $object_id, 'sync_status', $single ); - if ( isset( $sync_status[0] ) && $sync_status[0] === 'unsynced' ) { + if ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) { return $sync_status; } From 61248af9625bd6ff611c01cdc90f4ea319a3a246 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 11:34:53 +1200 Subject: [PATCH 5/8] Add note about filter not needed in core --- lib/blocks.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/blocks.php b/lib/blocks.php index c757a19880c55d..a46ed4e4f993a3 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -376,6 +376,8 @@ function gutenberg_register_legacy_social_link_blocks() { /** * Migrate the legacy `sync_status` meta key to the new `wp_pattern_sync_status` meta key. * + * 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`. + * * @since 16.1.1 * * @param mixed $value The value to return, either a single metadata value or an array of values depending on the value of $single. From 61d443866b94ebe412ea50e972885507909d1c90 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 13:37:04 +1200 Subject: [PATCH 6/8] Changes from review --- lib/blocks.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index a46ed4e4f993a3..0de129071da88e 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -377,15 +377,14 @@ function gutenberg_register_legacy_social_link_blocks() { * Migrate the legacy `sync_status` meta key to the new `wp_pattern_sync_status` meta key. * * 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`. - * * @since 16.1.1 + * @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. * - * @see https://github.com/WordPress/gutenberg/pull/52232 */ function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $single ) { if ( 'wp_pattern_sync_status' !== $meta_key ) { @@ -394,7 +393,9 @@ function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $si $sync_status = get_post_meta( $object_id, 'sync_status', $single ); - if ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) { + if ( $single && 'unsynced' === $sync_status ) { + return $sync_status; + } elseif ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) { return $sync_status; } From 07cd1a254a68c237c1816f535dc19530b5032848 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 13:40:49 +1200 Subject: [PATCH 7/8] Keep the linting gods happy --- lib/blocks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/blocks.php b/lib/blocks.php index 0de129071da88e..7af2f5b2284b0a 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -377,6 +377,7 @@ function gutenberg_register_legacy_social_link_blocks() { * Migrate the legacy `sync_status` meta key to the new `wp_pattern_sync_status` meta key. * * 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`. + * * @since 16.1.1 * @see https://github.com/WordPress/gutenberg/pull/52232 * From 520ee0f1493ff102317d1b1cf9877163af71f70c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Jul 2023 13:47:11 +1200 Subject: [PATCH 8/8] VS Code your linting skills are terrible! --- lib/blocks.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 7af2f5b2284b0a..e98f711b5c85a5 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -374,18 +374,15 @@ function gutenberg_register_legacy_social_link_blocks() { add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); /** - * Migrate the legacy `sync_status` meta key to the new `wp_pattern_sync_status` meta key. + * 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`. - * - * @since 16.1.1 - * @see https://github.com/WordPress/gutenberg/pull/52232 + * 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 ) {