Changeset 58065
- Timestamp:
- 04/30/2024 09:31:41 AM (5 months ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r57648 r58065 98 98 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 99 99 ); 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 } 100 106 if ( isset( $schema['properties']['password'] ) ) { 101 107 $get_item_args['password'] = array( … … 1873 1879 1874 1880 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 1875 1894 /** This filter is documented in wp-includes/post-template.php */ 1876 1895 $excerpt = apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ); … … 1884 1903 'protected' => (bool) $post->post_password, 1885 1904 ); 1905 1906 if ( isset( $override_excerpt_length ) ) { 1907 remove_filter( 1908 'excerpt_length', 1909 $override_excerpt_length, 1910 20 1911 ); 1912 } 1886 1913 } 1887 1914 -
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r57603 r58065 260 260 $data = $response->get_data(); 261 261 $keys = array_keys( $data['endpoints'][0]['args'] ); 262 sort( $keys ); 263 $this->assertSame( array( 'context', 'id' ), $keys ); 262 $this->assertEqualSets( array( 'context', 'id' ), $keys ); 264 263 } 265 264 -
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r57623 r58065 223 223 $data = $response->get_data(); 224 224 $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 ); 227 226 } 228 227 … … 2362 2361 ); 2363 2362 $this->assertSame( 0, $filter_count ); 2363 } 2364 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 ); 2364 2399 } 2365 2400 -
trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
r56746 r58065 157 157 $data = $response->get_data(); 158 158 $keys = array_keys( $data['endpoints'][0]['args'] ); 159 sort( $keys ); 160 $this->assertSame( array( 'context', 'id' ), $keys ); 159 $this->assertEqualSets( array( 'context', 'id' ), $keys ); 161 160 } 162 161 -
trunk/tests/qunit/fixtures/wp-api-generated.js
r58062 r58065 887 887 "required": false 888 888 }, 889 "excerpt_length": { 890 "description": "Override the default excerpt length.", 891 "type": "integer", 892 "required": false 893 }, 889 894 "password": { 890 895 "description": "The password for the post if it is password protected.", … … 2050 2055 ], 2051 2056 "default": "view", 2057 "required": false 2058 }, 2059 "excerpt_length": { 2060 "description": "Override the default excerpt length.", 2061 "type": "integer", 2052 2062 "required": false 2053 2063 },
Note: See TracChangeset
for help on using the changeset viewer.