Make WordPress Core

Changeset 58292


Ignore:
Timestamp:
06/03/2024 12:35:47 PM (7 weeks ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Move Posts controller hooked blocks injection logic.

In a similar vein as [58291], this changeset introduces a new insert_hooked_blocks_into_rest_response function and hooks it to the rest_prepare_wp_navigation filter.

This is part of an ongoing effort to move Block Hooks related code out of the Navigation block. Specifically, insert_hooked_blocks_into_rest_response is based on block_core_navigation_insert_hooked_blocks_into_rest_response. Eventually, it will be possible to deprecate the latter.

Follow-up to [58291].

See #60759.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r58291 r58292  
    11371137
    11381138/**
     1139 * Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks.
     1140 *
     1141 * @since 6.6.0
     1142 *
     1143 * @param WP_REST_Response $response The response object.
     1144 * @param WP_Post          $post     Post object.
     1145 * @return WP_REST_Response The response object.
     1146 */
     1147function insert_hooked_blocks_into_rest_response( $response, $post ) {
     1148    if ( ! isset( $response->data['content']['raw'] ) || ! isset( $response->data['content']['rendered'] ) ) {
     1149        return $response;
     1150    }
     1151
     1152    $attributes = array();
     1153    $ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
     1154    if ( ! empty( $ignored_hooked_blocks ) ) {
     1155        $ignored_hooked_blocks  = json_decode( $ignored_hooked_blocks, true );
     1156        $attributes['metadata'] = array(
     1157            'ignoredHookedBlocks' => $ignored_hooked_blocks,
     1158        );
     1159    }
     1160    $content = get_comment_delimited_block_content(
     1161        'core/navigation',
     1162        $attributes,
     1163        $response->data['content']['raw']
     1164    );
     1165
     1166    $content = apply_block_hooks_to_content( $content, $post );
     1167
     1168    // Remove mock Navigation block wrapper.
     1169    $content = remove_serialized_parent_block( $content );
     1170
     1171    $response->data['content']['raw'] = $content;
     1172
     1173    /** This filter is documented in wp-includes/post-template.php */
     1174    $response->data['content']['rendered'] = apply_filters( 'the_content', $content );
     1175
     1176    return $response;
     1177}
     1178
     1179/**
    11391180 * Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
    11401181 *
  • trunk/src/wp-includes/default-filters.php

    r58291 r58292  
    761761add_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' );
    762762
     763// Inject hooked blocks into the wp_navigation post type REST response.
     764add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 );
     765
    763766unset( $filter, $action );
Note: See TracChangeset for help on using the changeset viewer.