Skip to content
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

REST API: allow overriding excerpt length #5932

Closed
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
30 changes: 28 additions & 2 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,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 );
}

/**
Expand Down Expand Up @@ -2345,6 +2344,33 @@ public function test_prepare_item_skips_content_filter_if_not_needed() {
$this->assertSame( 0, $filter_count );
}

public function test_prepare_item_override_excerpt_length() {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
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 );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

// 43 words plus the ellipsis added via the 'excerpt_more' filter.
$this->assertCount( 44, explode( ' ', $data['excerpt']['rendered'] ) );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}

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

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ 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 );
$keys = array_keys( $data['endpoints'][0]['args'] );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
$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
Loading