Skip to content

Commit d72aa21

Browse files
Patterns: rename wp_block sync_status postmeta to wp_pattern_sync_status (#52232)
--------- Co-authored-by: Kai Hao <kai@kaihao.dev>
1 parent 4e6bf8d commit d72aa21

File tree

7 files changed

+42
-12
lines changed

7 files changed

+42
-12
lines changed

lib/blocks.php

+28
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,31 @@ function gutenberg_register_legacy_social_link_blocks() {
372372
}
373373

374374
add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
375+
376+
/**
377+
* Migrate the legacy `sync_status` meta key (added 16.1) to the new `wp_pattern_sync_status` meta key (16.1.1).
378+
*
379+
* 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`.
380+
* see https://github.com/WordPress/gutenberg/pull/52232
381+
*
382+
* @param mixed $value The value to return, either a single metadata value or an array of values depending on the value of $single.
383+
* @param int $object_id ID of the object metadata is for.
384+
* @param string $meta_key Metadata key.
385+
* @param bool $single Whether to return only the first value of the specified $meta_key.
386+
*/
387+
function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $single ) {
388+
if ( 'wp_pattern_sync_status' !== $meta_key ) {
389+
return $value;
390+
}
391+
392+
$sync_status = get_post_meta( $object_id, 'sync_status', $single );
393+
394+
if ( $single && 'unsynced' === $sync_status ) {
395+
return $sync_status;
396+
} elseif ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) {
397+
return $sync_status;
398+
}
399+
400+
return $value;
401+
}
402+
add_filter( 'default_post_metadata', 'gutenberg_legacy_wp_block_post_meta', 10, 4 );

lib/compat/wordpress-6.3/blocks.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function gutenberg_add_custom_fields_to_wp_block( $args, $post_type ) {
8989
add_filter( 'register_post_type_args', 'gutenberg_add_custom_fields_to_wp_block', 10, 2 );
9090

9191
/**
92-
* Adds sync_status meta fields to the wp_block post type so an unsynced option can be added.
92+
* Adds wp_pattern_sync_status meta fields to the wp_block post type so an unsynced option can be added.
9393
*
9494
* Note: This should be removed when the minimum required WP version is >= 6.3.
9595
*
@@ -101,7 +101,7 @@ function gutenberg_wp_block_register_post_meta() {
101101
$post_type = 'wp_block';
102102
register_post_meta(
103103
$post_type,
104-
'sync_status',
104+
'wp_pattern_sync_status',
105105
array(
106106
'auth_callback' => function() {
107107
return current_user_can( 'edit_posts' );
@@ -113,7 +113,7 @@ function gutenberg_wp_block_register_post_meta() {
113113
'schema' => array(
114114
'type' => 'string',
115115
'properties' => array(
116-
'sync_status' => array(
116+
'wp_pattern_sync_status' => array(
117117
'type' => 'string',
118118
),
119119
),
@@ -123,7 +123,7 @@ function gutenberg_wp_block_register_post_meta() {
123123
);
124124
}
125125
/**
126-
* Sanitizes the array of wp_block post meta sync_status string.
126+
* Sanitizes the array of wp_block post meta wp_pattern_sync_status string.
127127
*
128128
* Note: This should be removed when the minimum required WP version is >= 6.3.
129129
*

packages/block-editor/src/store/selectors.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2045,11 +2045,13 @@ export const getInserterItems = createSelector(
20452045
? getReusableBlocks( state )
20462046
.filter(
20472047
( reusableBlock ) =>
2048-
syncStatus === reusableBlock.meta?.sync_status ||
2048+
syncStatus ===
2049+
reusableBlock.meta?.wp_pattern_sync_status ||
20492050
( ! syncStatus &&
2050-
( reusableBlock.meta?.sync_status === '' ||
2051-
reusableBlock.meta?.sync_status ===
2052-
'fully' ) ) // Only reusable blocks added via site editor in release 16.1 will have sync_status of 'fully'.
2051+
( reusableBlock.meta?.wp_pattern_sync_status ===
2052+
'' ||
2053+
reusableBlock.meta
2054+
?.wp_pattern_sync_status === 'fully' ) ) // Only reusable blocks added via site editor in release 16.1 will have wp_pattern_sync_status of 'fully'.
20532055
)
20542056
.map( buildReusableBlockInserterItem )
20552057
: [];

packages/edit-site/src/components/create-pattern-modal/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default function CreatePatternModal( {
5656
status: 'publish',
5757
meta:
5858
syncType === SYNC_TYPES.unsynced
59-
? { sync_status: syncType }
59+
? { wp_pattern_sync_status: syncType }
6060
: undefined,
6161
},
6262
{ throwOnError: true }

packages/edit-site/src/components/page-library/use-patterns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const reusableBlockToPattern = ( reusableBlock ) => ( {
145145
categories: reusableBlock.wp_pattern,
146146
id: reusableBlock.id,
147147
name: reusableBlock.slug,
148-
syncStatus: reusableBlock.meta?.sync_status || SYNC_TYPES.full,
148+
syncStatus: reusableBlock.meta?.wp_pattern_sync_status || SYNC_TYPES.full,
149149
title: reusableBlock.title.raw,
150150
type: reusableBlock.type,
151151
reusableBlock,

packages/editor/src/components/post-sync-status/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function PostSyncStatus() {
2121
if ( postType !== 'wp_block' ) {
2222
return null;
2323
}
24-
const syncStatus = meta?.sync_status;
24+
const syncStatus = meta?.wp_pattern_sync_status;
2525
const isFullySynced = ! syncStatus;
2626

2727
return (

packages/reusable-blocks/src/store/actions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const __experimentalConvertBlocksToReusable =
5252
const meta =
5353
syncType === 'unsynced'
5454
? {
55-
sync_status: syncType,
55+
wp_pattern_sync_status: syncType,
5656
}
5757
: undefined;
5858

0 commit comments

Comments
 (0)