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

Patterns: rename sync_status postmeta to pattern_post_sync_status #52232

Merged
merged 8 commits into from
Jul 4, 2023
Merged
28 changes: 28 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, for posts already created with 'sync_status', they are assigned the new metadata key in gutenberg_wp_block_register_post_meta.

Copy link
Contributor Author

@glendaviesnz glendaviesnz Jul 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a bit weird how metakeys works - they are assigned whether they were explicitly set or not, so if sync_status was actually assigned previously then its value is mapped to 'wp_pattern_sync_status' in gutenberg_wp_block_register_post_meta - does that make sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, thanks for the explainer!

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 );
8 changes: 4 additions & 4 deletions lib/compat/wordpress-6.3/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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' );
Expand All @@ -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',
),
),
Expand All @@ -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.
*
Expand Down
10 changes: 6 additions & 4 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
: [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-sync-status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion packages/reusable-blocks/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const __experimentalConvertBlocksToReusable =
const meta =
syncType === 'unsynced'
? {
sync_status: syncType,
wp_pattern_sync_status: syncType,
}
: undefined;

Expand Down