Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ public function register_routes() {
$get_item_args = array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
);
if ( isset( $schema['properties']['excerpt'] ) ) {
$get_item_args['excerpt_length'] = array(
'description' => __( 'Override the default excerpt length.' ),
'type' => 'integer',
);
}
if ( isset( $schema['properties']['password'] ) ) {
$get_item_args['password'] = array(
'description' => __( 'The password for the post if it is password protected.' ),
Expand Down Expand Up @@ -1872,6 +1878,19 @@ public function prepare_item_for_response( $item, $request ) {
}

if ( rest_is_field_included( 'excerpt', $fields ) ) {
if ( isset( $request['excerpt_length'] ) ) {
$excerpt_length = $request['excerpt_length'];
$override_excerpt_length = static function () use ( $excerpt_length ) {
return $excerpt_length;
};

add_filter(
'excerpt_length',
$override_excerpt_length,
20
);
}

/** This filter is documented in wp-includes/post-template.php */
$excerpt = apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );

Expand All @@ -1883,6 +1902,14 @@ public function prepare_item_for_response( $item, $request ) {
'rendered' => post_password_required( $post ) ? '' : $excerpt,
'protected' => (bool) $post->post_password,
);

if ( isset( $override_excerpt_length ) ) {
remove_filter(
'excerpt_length',
$override_excerpt_length,
20
);
}
}

if ( $has_password_filter ) {
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/tests/rest-api/rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ public function test_registered_get_item_params() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$keys = array_keys( $data['endpoints'][0]['args'] );
sort( $keys );
$this->assertSame( array( 'context', 'id' ), $keys );
$this->assertEqualSets( array( 'context', 'id' ), $keys );
}

/**
Expand Down
39 changes: 37 additions & 2 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ public function test_registered_get_item_params() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$keys = array_keys( $data['endpoints'][0]['args'] );
sort( $keys );
$this->assertSame( array( 'context', 'id', 'password' ), $keys );
$this->assertEqualSets( array( 'context', 'id', 'password', 'excerpt_length' ), $keys );
}

public function test_registered_get_items_embed() {
Expand Down Expand Up @@ -2363,6 +2362,42 @@ public function test_prepare_item_skips_content_filter_if_not_needed() {
$this->assertSame( 0, $filter_count );
}

/**
* @ticket 59043
*
* @covers WP_REST_Posts_Controller::prepare_item_for_response
*/
public function test_prepare_item_override_excerpt_length() {
wp_set_current_user( self::$editor_id );

$post_id = self::factory()->post->create(
array(
'post_excerpt' => '',
'post_content' => 'Bacon ipsum dolor amet porchetta capicola sirloin prosciutto brisket shankle jerky. Ham hock filet mignon boudin ground round, prosciutto alcatra spare ribs meatball turducken pork beef ribs ham beef. Bacon pastrami short loin, venison tri-tip ham short ribs doner swine. Tenderloin pig tongue pork jowl doner. Pork loin rump t-bone, beef strip steak flank drumstick tri-tip short loin capicola jowl. Cow filet mignon hamburger doner rump. Short loin jowl drumstick, tongue tail beef ribs pancetta flank brisket landjaeger chuck venison frankfurter turkey.

Brisket shank rump, tongue beef ribs swine fatback turducken capicola meatball picanha chicken cupim meatloaf turkey. Bacon biltong shoulder tail frankfurter boudin cupim turkey drumstick. Porchetta pig shoulder, jerky flank pork tail meatball hamburger. Doner ham hock ribeye tail jerky swine. Leberkas ribeye pancetta, tenderloin capicola doner turducken chicken venison ground round boudin pork chop. Tail pork loin pig spare ribs, biltong ribeye brisket pork chop cupim. Short loin leberkas spare ribs jowl landjaeger tongue kevin flank bacon prosciutto.

Shankle pork chop prosciutto ribeye ham hock pastrami. T-bone shank brisket bacon pork chop. Cupim hamburger pork loin short loin. Boudin ball tip cupim ground round ham shoulder. Sausage rump cow tongue bresaola pork pancetta biltong tail chicken turkey hamburger. Kevin flank pork loin salami biltong. Alcatra landjaeger pastrami andouille kielbasa ham tenderloin drumstick sausage turducken tongue corned beef.',
)
);

$endpoint = new WP_REST_Posts_Controller( 'post' );
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'excerpt' );
$request->set_param( 'excerpt_length', 43 );
$response = $endpoint->prepare_item_for_response( get_post( $post_id ), $request );
$data = $response->get_data();
$this->assertArrayHasKey( 'excerpt', $data, 'Response must contain an "excerpt" key.' );

// 43 words plus the ellipsis added via the 'excerpt_more' filter.
$this->assertCount(
44,
explode( ' ', $data['excerpt']['rendered'] ),
'Incorrect word count in the excerpt. Expected the excerpt to contain 44 words (43 words plus an ellipsis), but a different word count was found.'
);
}

public function test_create_item() {
wp_set_current_user( self::$editor_id );

Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ public function test_registered_get_item_params() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$keys = array_keys( $data['endpoints'][0]['args'] );
sort( $keys );
$this->assertSame( array( 'context', 'id' ), $keys );
$this->assertEqualSets( array( 'context', 'id' ), $keys );
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,11 @@ mockedApiResponse.Schema = {
"default": "view",
"required": false
},
"excerpt_length": {
"description": "Override the default excerpt length.",
"type": "integer",
"required": false
},
"password": {
"description": "The password for the post if it is password protected.",
"type": "string",
Expand Down Expand Up @@ -2051,6 +2056,11 @@ mockedApiResponse.Schema = {
"default": "view",
"required": false
},
"excerpt_length": {
"description": "Override the default excerpt length.",
"type": "integer",
"required": false
},
"password": {
"description": "The password for the post if it is password protected.",
"type": "string",
Expand Down