Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,15 @@ public function prepare_item_for_response( $item, $request ) {
}

$fields = $this->get_fields_for_response( $request );
$schema = $this->get_item_schema();

/*
* Some controllers, such as the synced patterns controller, remove the
* `rendered` properties from the schema, since a block only renders in the
* context of a post or page. Skip the rendering passes for those fields.
*/
$can_render_title = isset( $schema['properties']['title']['properties']['rendered'] );
$can_render_content = isset( $schema['properties']['content']['properties']['rendered'] );

// Base fields for every post.
$data = array();
Expand Down Expand Up @@ -1975,7 +1984,7 @@ public function prepare_item_for_response( $item, $request ) {
if ( rest_is_field_included( 'title.raw', $fields ) ) {
$data['title']['raw'] = $post->post_title;
}
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
if ( $can_render_title && rest_is_field_included( 'title.rendered', $fields ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );

Expand All @@ -2001,7 +2010,7 @@ public function prepare_item_for_response( $item, $request ) {
if ( rest_is_field_included( 'content.raw', $fields ) ) {
$data['content']['raw'] = $post->post_content;
}
if ( rest_is_field_included( 'content.rendered', $fields ) ) {
if ( $can_render_content && rest_is_field_included( 'content.rendered', $fields ) ) {
/** This filter is documented in wp-includes/post-template.php */
$data['content']['rendered'] = post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content );
}
Expand Down
49 changes: 49 additions & 0 deletions tests/phpunit/tests/rest-api/rest-blocks-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,55 @@
);
}

/**
* Check that the rendering callbacks for the stripped `rendered` fields do not run.
*
* `title.rendered` and `content.rendered` are removed from the pattern schema
* and response, so the `the_title` and `the_content` passes should not run at all.
*
* @ticket 66644
*/
public function test_rendered_fields_are_not_rendered() {
wp_set_current_user( self::$user_ids['author'] );

$post_id = wp_insert_post(
array(
'post_type' => 'wp_block',
'post_status' => 'publish',
'post_title' => 'Pattern with excerpt',
'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
'post_excerpt' => 'Pattern excerpt',
)
);

$the_content_calls = 0;
$the_title_calls = 0;

$count_the_content = static function ( $content ) use ( &$the_content_calls ) {
$the_content_calls++;

Check warning on line 249 in tests/phpunit/tests/rest-api/rest-blocks-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Stand-alone post-increment statement found. Use pre-increment instead: ++$the_content_calls.
return $content;
};
$count_the_title = static function ( $title ) use ( &$the_title_calls ) {

Check warning on line 252 in tests/phpunit/tests/rest-api/rest-blocks-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$the_title_calls++;

Check warning on line 253 in tests/phpunit/tests/rest-api/rest-blocks-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Stand-alone post-increment statement found. Use pre-increment instead: ++$the_title_calls.
return $title;
};

add_filter( 'the_content', $count_the_content );
add_filter( 'the_title', $count_the_title );

$request = new WP_REST_Request( 'GET', '/wp/v2/blocks/' . $post_id );
$response = rest_get_server()->dispatch( $request );

remove_filter( 'the_content', $count_the_content );
remove_filter( 'the_title', $count_the_title );

wp_delete_post( $post_id );

$this->assertSame( 200, $response->get_status() );
$this->assertSame( 0, $the_content_calls );
$this->assertSame( 0, $the_title_calls );
}

/**
* Check that the `wp_pattern_sync_status` postmeta is moved from meta array to top
* level of response.
Expand Down
Loading