函数文档

insert_hooked_blocks_into_rest_response()

💡 云策文档标注

概述

insert_hooked_blocks_into_rest_response() 函数用于在 WordPress REST API 的 Posts 端点响应中,通过钩子机制添加第一个和最后一个内部块。它处理内容原始数据和渲染内容,确保 Block Hooks 算法正确应用。

关键要点

  • 函数钩入 REST API 响应,针对 Posts 端点,添加内部块。
  • 参数包括 $response(WP_REST_Response 对象)和 $post(WP_Post 对象),返回 WP_REST_Response 对象。
  • 使用 apply_block_hooks_to_content_from_post_object() 函数处理原始内容,并管理 the_content 过滤器以避免重复调用。
  • 支持从 WordPress 6.6.0 版本引入,并在 6.8.0 版本扩展支持非 wp_navigation 文章类型。

代码示例

function insert_hooked_blocks_into_rest_response( $response, $post ) {
    if ( empty( $response->data['content']['raw'] ) ) {
        return $response;
    }

    $response->data['content']['raw'] = apply_block_hooks_to_content_from_post_object(
        $response->data['content']['raw'],
        $post,
        'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
    );

    // If the rendered content was previously empty, we leave it like that.
    if ( empty( $response->data['content']['rendered'] ) ) {
        return $response;
    }

    // `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_from_post_object' );
    if ( false !== $priority ) {
        remove_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
    }

    /** This filter is documented in wp-includes/post-template.php */
    $response->data['content']['rendered'] = apply_filters(
        'the_content',
        $response->data['content']['raw']
    );

    // Restore the filter if it was set initially.
    if ( false !== $priority ) {
        add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
    }

    return $response;
}

注意事项

  • 函数仅在响应中的原始内容非空时处理,否则直接返回响应。
  • 通过 has_filter()、remove_filter() 和 add_filter() 管理 the_content 过滤器,防止 apply_block_hooks_to_content_from_post_object() 被重复调用。
  • 相关函数包括 apply_block_hooks_to_content_from_post_object()、has_filter()、remove_filter()、apply_filters() 和 add_filter(),用于支持 Block Hooks 算法和过滤器操作。

📄 原文内容

Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.

Parameters

$responseWP_REST_Responserequired
The response object.
$postWP_Postrequired
Post object.

Return

WP_REST_Response The response object.

Source

function insert_hooked_blocks_into_rest_response( $response, $post ) {
	if ( empty( $response->data['content']['raw'] ) ) {
		return $response;
	}

	$response->data['content']['raw'] = apply_block_hooks_to_content_from_post_object(
		$response->data['content']['raw'],
		$post,
		'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
	);

	// If the rendered content was previously empty, we leave it like that.
	if ( empty( $response->data['content']['rendered'] ) ) {
		return $response;
	}

	// `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_from_post_object' );
	if ( false !== $priority ) {
		remove_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
	}

	/** This filter is documented in wp-includes/post-template.php */
	$response->data['content']['rendered'] = apply_filters(
		'the_content',
		$response->data['content']['raw']
	);

	// Restore the filter if it was set initially.
	if ( false !== $priority ) {
		add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
	}

	return $response;
}

Hooks

apply_filters( ‘the_content’, string $content )

Filters the post content.

Changelog

Version Description
6.8.0 Support non-wp_navigation post types.
6.6.0 Introduced.