Make WordPress Core

Changeset 58065


Ignore:
Timestamp:
04/30/2024 09:31:41 AM (6 weeks ago)
Author:
swissspidy
Message:

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.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r57648 r58065  
    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(
     
    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 );
     
    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
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r57603 r58065  
    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
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r57623 r58065  
    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
     
    23622361        );
    23632362        $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
     2378Brisket 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
     2380Shankle 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        );
    23642399    }
    23652400
  • trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php

    r56746 r58065  
    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
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r58062 r58065  
    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.",
     
    20502055                            ],
    20512056                            "default": "view",
     2057                            "required": false
     2058                        },
     2059                        "excerpt_length": {
     2060                            "description": "Override the default excerpt length.",
     2061                            "type": "integer",
    20522062                            "required": false
    20532063                        },
Note: See TracChangeset for help on using the changeset viewer.