-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Block Hooks: Apply to Post Content (on frontend and in editor) #7898
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
Changes from all commits
0711c61
b40dd94
20a5470
90abff4
0b844b3
838a75e
6793b55
2958c64
74b39fa
007d7a4
1d98a27
c6c8481
f29b288
a5cb598
886d375
89b835d
e14db01
7635d23
2b7d36a
a7693b0
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 |
|---|---|---|
|
|
@@ -912,7 +912,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke | |
| * @param string $relative_position The relative position of the hooked blocks. | ||
| * Can be one of 'before', 'after', 'first_child', or 'last_child'. | ||
| * @param string $anchor_block_type The anchor block type. | ||
| * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, | ||
| * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, | ||
| * or pattern that the anchor block belongs to. | ||
| */ | ||
| $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); | ||
|
|
@@ -935,7 +935,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke | |
| * @param string $hooked_block_type The hooked block type name. | ||
| * @param string $relative_position The relative position of the hooked block. | ||
| * @param array $parsed_anchor_block The anchor block, in parsed block array format. | ||
| * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, | ||
| * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, | ||
| * or pattern that the anchor block belongs to. | ||
| */ | ||
| $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); | ||
|
|
@@ -951,7 +951,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke | |
| * @param string $hooked_block_type The hooked block type name. | ||
| * @param string $relative_position The relative position of the hooked block. | ||
| * @param array $parsed_anchor_block The anchor block, in parsed block array format. | ||
| * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, | ||
| * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, | ||
| * or pattern that the anchor block belongs to. | ||
| */ | ||
| $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); | ||
|
|
@@ -1039,17 +1039,25 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po | |
| * | ||
| * @since 6.6.0 | ||
| * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered. | ||
| * @since 6.8.0 Have the `$context` parameter default to `null`, in which case `get_post()` will be called to use the current post as context. | ||
| * @access private | ||
| * | ||
| * @param string $content Serialized content. | ||
| * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, | ||
| * or pattern that the blocks belong to. | ||
| * @param callable $callback A function that will be called for each block to generate | ||
| * the markup for a given list of blocks that are hooked to it. | ||
| * Default: 'insert_hooked_blocks'. | ||
| * @param string $content Serialized content. | ||
| * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, post object, or pattern | ||
| * that the blocks belong to. If set to `null`, `get_post()` | ||
| * will be called to use the current post as context. | ||
| * Default: `null`. | ||
| * @param callable $callback A function that will be called for each block to generate | ||
| * the markup for a given list of blocks that are hooked to it. | ||
| * Default: 'insert_hooked_blocks'. | ||
| * @return string The serialized markup. | ||
| */ | ||
| function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) { | ||
| function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) { | ||
| // Default to the current post if no context is provided. | ||
| if ( null === $context ) { | ||
| $context = get_post(); | ||
| } | ||
|
|
||
| $hooked_blocks = get_hooked_blocks(); | ||
|
|
||
| $before_block_visitor = '_inject_theme_attribute_in_template_part_block'; | ||
|
|
@@ -1165,36 +1173,37 @@ function extract_serialized_parent_block( $serialized_block ) { | |
| } | ||
|
|
||
| /** | ||
| * Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content. | ||
| * Currently only supports `wp_navigation` post types. | ||
| * Updates the wp_postmeta with the list of ignored hooked blocks | ||
| * where the inner blocks are stored as post content. | ||
| * | ||
| * @since 6.6.0 | ||
| * @since 6.8.0 Support non-`wp_navigation` post types. | ||
| * @access private | ||
| * | ||
| * @param stdClass $post Post object. | ||
| * @return stdClass The updated post object. | ||
| */ | ||
| function update_ignored_hooked_blocks_postmeta( $post ) { | ||
| /* | ||
| * In this scenario the user has likely tried to create a navigation via the REST API. | ||
| * In this scenario the user has likely tried to create a new post object via the REST API. | ||
| * In which case we won't have a post ID to work with and store meta against. | ||
| */ | ||
| if ( empty( $post->ID ) ) { | ||
| return $post; | ||
| } | ||
|
|
||
| /* | ||
| * Skip meta generation when consumers intentionally update specific Navigation fields | ||
| * Skip meta generation when consumers intentionally update specific fields | ||
| * and omit the content update. | ||
| */ | ||
| if ( ! isset( $post->post_content ) ) { | ||
| return $post; | ||
| } | ||
|
|
||
| /* | ||
| * Skip meta generation when the post content is not a navigation block. | ||
| * Skip meta generation if post type is not set. | ||
| */ | ||
| if ( ! isset( $post->post_type ) || 'wp_navigation' !== $post->post_type ) { | ||
| if ( ! isset( $post->post_type ) ) { | ||
| return $post; | ||
| } | ||
|
|
||
|
|
@@ -1208,8 +1217,14 @@ function update_ignored_hooked_blocks_postmeta( $post ) { | |
| ); | ||
| } | ||
|
|
||
| if ( 'wp_navigation' === $post->post_type ) { | ||
| $wrapper_block_type = 'core/navigation'; | ||
| } else { | ||
| $wrapper_block_type = 'core/post-content'; | ||
| } | ||
|
|
||
| $markup = get_comment_delimited_block_content( | ||
| 'core/navigation', | ||
| $wrapper_block_type, | ||
| $attributes, | ||
| $post->post_content | ||
| ); | ||
|
|
@@ -1266,16 +1281,17 @@ function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_a | |
| } | ||
|
|
||
| /** | ||
| * Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks. | ||
| * Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks. | ||
| * | ||
| * @since 6.6.0 | ||
| * @since 6.8.0 Support non-`wp_navigation` post types. | ||
| * | ||
| * @param WP_REST_Response $response The response object. | ||
| * @param WP_Post $post Post object. | ||
| * @return WP_REST_Response The response object. | ||
| */ | ||
| function insert_hooked_blocks_into_rest_response( $response, $post ) { | ||
| if ( ! isset( $response->data['content']['raw'] ) || ! isset( $response->data['content']['rendered'] ) ) { | ||
| if ( empty( $response->data['content']['raw'] ) || empty( $response->data['content']['rendered'] ) ) { | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return $response; | ||
| } | ||
|
|
||
|
|
@@ -1287,22 +1303,44 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) { | |
| 'ignoredHookedBlocks' => $ignored_hooked_blocks, | ||
| ); | ||
| } | ||
|
|
||
| if ( 'wp_navigation' === $post->post_type ) { | ||
|
Member
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. What about synced patterns
Contributor
Author
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. Ah, good point. I guess the A filter might make sense here 👍 Something to map post types to block types. (I wonder if we already have a function or data structure to do that somewhere 🤔 )
Contributor
Author
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've finally tested this with a synced pattern, and it's currently not inserting hooked blocks. Simply adding
Member
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. That's fair. It was also discussed in https://github.com/WordPress/wordpress-develop/pull/7898/files#r1867470796 to add support for every post type. The challenge with Patterns is that they are handled out of the box if they come from files bundled from the theme or when registered with PHP. The last missing puzzle would be a synced version of the pattern.
Contributor
Author
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. Update: Support for synced patterns was implemented in WordPress/gutenberg#68058 and #8015, respectively. I've filed a Trac ticket for other CPTs (and filterable post type -- block type mappings): https://core.trac.wordpress.org/ticket/62715 |
||
| $wrapper_block_type = 'core/navigation'; | ||
| } else { | ||
| $wrapper_block_type = 'core/post-content'; | ||
| } | ||
|
|
||
| $content = get_comment_delimited_block_content( | ||
| 'core/navigation', | ||
| $wrapper_block_type, | ||
| $attributes, | ||
| $response->data['content']['raw'] | ||
| ); | ||
|
|
||
| $content = apply_block_hooks_to_content( $content, $post ); | ||
| $content = apply_block_hooks_to_content( | ||
| $content, | ||
| $post, | ||
| 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' | ||
| ); | ||
|
Comment on lines
+1319
to
+1323
Contributor
Author
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. We're now inserting hooked blocks and setting This change also affects the Navigation block. We need to make sure that any hooked blocks inside of the Navigation block -- both as first/last child of
Contributor
Author
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. To test if the Navigation block still works, we can follow the testing instructions from WordPress/gutenberg#59021.
Contributor
Author
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. Turns out there was an unrelated issue with this. I've filed a fix: #7941
Contributor
Author
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. |
||
|
|
||
| // Remove mock Navigation block wrapper. | ||
| // Remove mock block wrapper. | ||
| $content = remove_serialized_parent_block( $content ); | ||
|
|
||
| $response->data['content']['raw'] = $content; | ||
|
|
||
| // `apply_block_hooks_to_content` is called above. Ensure it is not called again as a filter. | ||
| $priority = has_filter( 'the_content', 'apply_block_hooks_to_content' ); | ||
| if ( false !== $priority ) { | ||
| remove_filter( 'the_content', 'apply_block_hooks_to_content', $priority ); | ||
| } | ||
|
|
||
| /** This filter is documented in wp-includes/post-template.php */ | ||
| $response->data['content']['rendered'] = apply_filters( 'the_content', $content ); | ||
|
|
||
| // Restore the filter if it was set initially. | ||
| if ( false !== $priority ) { | ||
| add_filter( 'the_content', 'apply_block_hooks_to_content', $priority ); | ||
| } | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
|
|
@@ -1320,7 +1358,7 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) { | |
| * @access private | ||
| * | ||
| * @param array $hooked_blocks An array of blocks hooked to another given block. | ||
| * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, | ||
| * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object, | ||
| * or pattern that the blocks belong to. | ||
| * @param callable $callback A function that will be called for each block to generate | ||
| * the markup for a given list of blocks that are hooked to it. | ||
|
|
@@ -1377,7 +1415,7 @@ function make_before_block_visitor( $hooked_blocks, $context, $callback = 'inser | |
| * @access private | ||
| * | ||
| * @param array $hooked_blocks An array of blocks hooked to another block. | ||
| * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, | ||
| * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object, | ||
| * or pattern that the blocks belong to. | ||
| * @param callable $callback A function that will be called for each block to generate | ||
| * the markup for a given list of blocks that are hooked to it. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,6 +192,7 @@ | |
| add_filter( 'the_title', 'convert_chars' ); | ||
| add_filter( 'the_title', 'trim' ); | ||
|
|
||
| add_filter( 'the_content', 'apply_block_hooks_to_content', 8 ); // BEFORE do_blocks(). | ||
|
Contributor
Author
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. Quoting #7889 (comment):
|
||
| add_filter( 'the_content', 'do_blocks', 9 ); | ||
| add_filter( 'the_content', 'wptexturize' ); | ||
| add_filter( 'the_content', 'convert_smilies', 20 ); | ||
|
|
@@ -760,9 +761,13 @@ | |
| add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' ); | ||
|
|
||
| // Update ignoredHookedBlocks postmeta for wp_navigation post type. | ||
| add_filter( 'rest_pre_insert_page', 'update_ignored_hooked_blocks_postmeta' ); | ||
| add_filter( 'rest_pre_insert_post', 'update_ignored_hooked_blocks_postmeta' ); | ||
| add_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' ); | ||
|
|
||
| // Inject hooked blocks into the wp_navigation post type REST response. | ||
| // Inject hooked blocks into the Posts endpoint REST response for some given post types. | ||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| add_filter( 'rest_prepare_page', 'insert_hooked_blocks_into_rest_response', 10, 2 ); | ||
| add_filter( 'rest_prepare_post', 'insert_hooked_blocks_into_rest_response', 10, 2 ); | ||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+769
to
+770
Contributor
Author
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. This follows the precedent of I wonder if we should run
Member
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. It would be worthwhile to explore supporting other types than
Contributor
Author
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've filed https://core.trac.wordpress.org/ticket/62715 to keep track of this. |
||
| add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 ); | ||
|
|
||
| unset( $filter, $action ); | ||
Uh oh!
There was an error while loading. Please reload this page.