Skip to content

Commit f9e5c6f

Browse files
committed
REST API: allow overriding excerpt length.
This can be used by the excerpt block in the editor to change the excerpt length without filtering `excerpt_length` in a conflicting way. This enhancement still needs a corresponding change on the Gutenberg side. Props swissspidy, antonvlasenko, mukesh27, azaozz, andraganescu, timothyblynjacobs. Fixes #59043. git-svn-id: https://develop.svn.wordpress.org/trunk@58065 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b284298 commit f9e5c6f

5 files changed

Lines changed: 76 additions & 6 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public function register_routes() {
9797
$get_item_args = array(
9898
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
9999
);
100+
if ( isset( $schema['properties']['excerpt'] ) ) {
101+
$get_item_args['excerpt_length'] = array(
102+
'description' => __( 'Override the default excerpt length.' ),
103+
'type' => 'integer',
104+
);
105+
}
100106
if ( isset( $schema['properties']['password'] ) ) {
101107
$get_item_args['password'] = array(
102108
'description' => __( 'The password for the post if it is password protected.' ),
@@ -1872,6 +1878,19 @@ public function prepare_item_for_response( $item, $request ) {
18721878
}
18731879

18741880
if ( rest_is_field_included( 'excerpt', $fields ) ) {
1881+
if ( isset( $request['excerpt_length'] ) ) {
1882+
$excerpt_length = $request['excerpt_length'];
1883+
$override_excerpt_length = static function () use ( $excerpt_length ) {
1884+
return $excerpt_length;
1885+
};
1886+
1887+
add_filter(
1888+
'excerpt_length',
1889+
$override_excerpt_length,
1890+
20
1891+
);
1892+
}
1893+
18751894
/** This filter is documented in wp-includes/post-template.php */
18761895
$excerpt = apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
18771896

@@ -1883,6 +1902,14 @@ public function prepare_item_for_response( $item, $request ) {
18831902
'rendered' => post_password_required( $post ) ? '' : $excerpt,
18841903
'protected' => (bool) $post->post_password,
18851904
);
1905+
1906+
if ( isset( $override_excerpt_length ) ) {
1907+
remove_filter(
1908+
'excerpt_length',
1909+
$override_excerpt_length,
1910+
20
1911+
);
1912+
}
18861913
}
18871914

18881915
if ( $has_password_filter ) {

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ public function test_registered_get_item_params() {
259259
$response = rest_get_server()->dispatch( $request );
260260
$data = $response->get_data();
261261
$keys = array_keys( $data['endpoints'][0]['args'] );
262-
sort( $keys );
263-
$this->assertSame( array( 'context', 'id' ), $keys );
262+
$this->assertEqualSets( array( 'context', 'id' ), $keys );
264263
}
265264

266265
/**

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ public function test_registered_get_item_params() {
222222
$response = rest_get_server()->dispatch( $request );
223223
$data = $response->get_data();
224224
$keys = array_keys( $data['endpoints'][0]['args'] );
225-
sort( $keys );
226-
$this->assertSame( array( 'context', 'id', 'password' ), $keys );
225+
$this->assertEqualSets( array( 'context', 'id', 'password', 'excerpt_length' ), $keys );
227226
}
228227

229228
public function test_registered_get_items_embed() {
@@ -2363,6 +2362,42 @@ public function test_prepare_item_skips_content_filter_if_not_needed() {
23632362
$this->assertSame( 0, $filter_count );
23642363
}
23652364

2365+
/**
2366+
* @ticket 59043
2367+
*
2368+
* @covers WP_REST_Posts_Controller::prepare_item_for_response
2369+
*/
2370+
public function test_prepare_item_override_excerpt_length() {
2371+
wp_set_current_user( self::$editor_id );
2372+
2373+
$post_id = self::factory()->post->create(
2374+
array(
2375+
'post_excerpt' => '',
2376+
'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.
2377+
2378+
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.
2379+
2380+
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.',
2381+
)
2382+
);
2383+
2384+
$endpoint = new WP_REST_Posts_Controller( 'post' );
2385+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
2386+
$request->set_param( 'context', 'edit' );
2387+
$request->set_param( '_fields', 'excerpt' );
2388+
$request->set_param( 'excerpt_length', 43 );
2389+
$response = $endpoint->prepare_item_for_response( get_post( $post_id ), $request );
2390+
$data = $response->get_data();
2391+
$this->assertArrayHasKey( 'excerpt', $data, 'Response must contain an "excerpt" key.' );
2392+
2393+
// 43 words plus the ellipsis added via the 'excerpt_more' filter.
2394+
$this->assertCount(
2395+
44,
2396+
explode( ' ', $data['excerpt']['rendered'] ),
2397+
'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.'
2398+
);
2399+
}
2400+
23662401
public function test_create_item() {
23672402
wp_set_current_user( self::$editor_id );
23682403

tests/phpunit/tests/rest-api/wpRestMenuItemsController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ public function test_registered_get_item_params() {
156156
$response = rest_get_server()->dispatch( $request );
157157
$data = $response->get_data();
158158
$keys = array_keys( $data['endpoints'][0]['args'] );
159-
sort( $keys );
160-
$this->assertSame( array( 'context', 'id' ), $keys );
159+
$this->assertEqualSets( array( 'context', 'id' ), $keys );
161160
}
162161

163162
/**

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,11 @@ mockedApiResponse.Schema = {
886886
"default": "view",
887887
"required": false
888888
},
889+
"excerpt_length": {
890+
"description": "Override the default excerpt length.",
891+
"type": "integer",
892+
"required": false
893+
},
889894
"password": {
890895
"description": "The password for the post if it is password protected.",
891896
"type": "string",
@@ -2051,6 +2056,11 @@ mockedApiResponse.Schema = {
20512056
"default": "view",
20522057
"required": false
20532058
},
2059+
"excerpt_length": {
2060+
"description": "Override the default excerpt length.",
2061+
"type": "integer",
2062+
"required": false
2063+
},
20542064
"password": {
20552065
"description": "The password for the post if it is password protected.",
20562066
"type": "string",

0 commit comments

Comments
 (0)