-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Synced Patterns: Apply Block Hooks #68058
Changes from all commits
d32b5ad
9acac6d
1a46d06
46dfb41
256ebad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/8015 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/68058 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,7 +170,7 @@ function gutenberg_apply_block_hooks_to_post_content( $content ) { | |
* @return WP_REST_Response The response object. | ||
*/ | ||
function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) { | ||
if ( empty( $response->data['content']['raw'] ) || empty( $response->data['content']['rendered'] ) ) { | ||
if ( empty( $response->data['content']['raw'] ) ) { | ||
return $response; | ||
} | ||
|
||
|
@@ -185,6 +185,8 @@ function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) { | |
|
||
if ( 'wp_navigation' === $post->post_type ) { | ||
$wrapper_block_type = 'core/navigation'; | ||
} elseif ( 'wp_block' === $post->post_type ) { | ||
$wrapper_block_type = 'core/block'; | ||
} else { | ||
$wrapper_block_type = 'core/post-content'; | ||
} | ||
|
@@ -206,6 +208,11 @@ function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) { | |
|
||
$response->data['content']['raw'] = $content; | ||
|
||
// If the rendered content was previously empty, we leave it like that. | ||
if ( empty( $response->data['content']['rendered'] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes perfect sense together with other changes 👍🏻 |
||
return $response; | ||
} | ||
|
||
// No need to inject hooked blocks twice. | ||
$priority = has_filter( 'the_content', 'apply_block_hooks_to_content' ); | ||
if ( false !== $priority ) { | ||
|
@@ -224,6 +231,7 @@ function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) { | |
} | ||
add_filter( 'rest_prepare_page', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 ); | ||
add_filter( 'rest_prepare_post', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 ); | ||
add_filter( 'rest_prepare_wp_block', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 ); | ||
|
||
/** | ||
* Updates the wp_postmeta with the list of ignored hooked blocks | ||
|
@@ -272,6 +280,8 @@ function gutenberg_update_ignored_hooked_blocks_postmeta( $post ) { | |
|
||
if ( 'wp_navigation' === $post->post_type ) { | ||
$wrapper_block_type = 'core/navigation'; | ||
} elseif ( 'wp_block' === $post->post_type ) { | ||
$wrapper_block_type = 'core/block'; | ||
} else { | ||
$wrapper_block_type = 'core/post-content'; | ||
} | ||
|
@@ -311,3 +321,4 @@ function gutenberg_update_ignored_hooked_blocks_postmeta( $post ) { | |
} | ||
add_filter( 'rest_pre_insert_page', 'gutenberg_update_ignored_hooked_blocks_postmeta' ); | ||
add_filter( 'rest_pre_insert_post', 'gutenberg_update_ignored_hooked_blocks_postmeta' ); | ||
add_filter( 'rest_pre_insert_wp_block', 'gutenberg_update_ignored_hooked_blocks_postmeta' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,26 @@ function render_block_core_block( $attributes ) { | |
add_filter( 'render_block_context', $filter_block_context, 1 ); | ||
} | ||
|
||
$ignored_hooked_blocks = get_post_meta( $attributes['ref'], '_wp_ignored_hooked_blocks', true ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find a better place, so I will ask here. How feasible would it be to move the logic to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah, I just had a similar thought yesterday! I'll need to check if we can really absorb this logic into
Anyway, I'll file a ticket for this and will look into it in January 😊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I missed the nuance of the templates. Anyway, it could still be context-based, so the additional logic would only run if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR (WIP): #68926 |
||
if ( ! empty( $ignored_hooked_blocks ) ) { | ||
$ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true ); | ||
$attributes['metadata'] = array( | ||
'ignoredHookedBlocks' => $ignored_hooked_blocks, | ||
); | ||
} | ||
|
||
// Wrap in "Block" block so the Block Hooks algorithm can insert blocks | ||
// that are hooked as first or last child of `core/block`. | ||
$content = get_comment_delimited_block_content( | ||
'core/block', | ||
$attributes, | ||
$content | ||
); | ||
// Apply Block Hooks. | ||
$content = apply_block_hooks_to_content( $content, $reusable_block ); | ||
// Remove block wrapper. | ||
$content = remove_serialized_parent_block( $content ); | ||
|
||
$content = do_blocks( $content ); | ||
unset( $seen_refs[ $attributes['ref'] ] ); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out that synced patterns don't have rendered content set, so I had to loosen the criterion here.