Make WordPress Core

Changeset 50024


Ignore:
Timestamp:
01/26/2021 06:26:13 PM (6 years ago)
Author:
johnbillion
Message:

REST API: Introduce modified_before and modified_after query parameters for the posts endpoints.

These parameters work just the same as before and after except they operate on the post modified date instead of the post published date.

Props claytoncollie, TimothyBlynJacobs, hellofromTonya

Fixes #50617

Location:
trunk
Files:
5 edited

Legend:

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

    r49942 r50024  
    217217                $args['date_query'] = array();
    218218
    219                 // Set before into date query. Date query must be specified as an array of an array.
    220219                if ( isset( $registered['before'], $request['before'] ) ) {
    221                         $args['date_query'][0]['before'] = $request['before'];
    222                 }
    223 
    224                 // Set after into date query. Date query must be specified as an array of an array.
     220                        $args['date_query'][] = array(
     221                                'before' => $request['before'],
     222                                'column' => 'post_date',
     223                        );
     224                }
     225
     226                if ( isset( $registered['modified_before'], $request['modified_before'] ) ) {
     227                        $args['date_query'][] = array(
     228                                'before' => $request['modified_before'],
     229                                'column' => 'post_modified',
     230                        );
     231                }
     232
    225233                if ( isset( $registered['after'], $request['after'] ) ) {
    226                         $args['date_query'][0]['after'] = $request['after'];
     234                        $args['date_query'][] = array(
     235                                'after'  => $request['after'],
     236                                'column' => 'post_date',
     237                        );
     238                }
     239
     240                if ( isset( $registered['modified_after'], $request['modified_after'] ) ) {
     241                        $args['date_query'][] = array(
     242                                'after'  => $request['modified_after'],
     243                                'column' => 'post_modified',
     244                        );
    227245                }
    228246
     
    26292647         *
    26302648         * @since 4.7.0
     2649         * @since 5.4.0 The `tax_relation` query parameter was added.
     2650         * @since 5.7.0 The `modified_after` and `modified_before` query parameters were added.
    26312651         *
    26322652         * @return array Collection parameters.
     
    26392659                $query_params['after'] = array(
    26402660                        'description' => __( 'Limit response to posts published after a given ISO8601 compliant date.' ),
     2661                        'type'        => 'string',
     2662                        'format'      => 'date-time',
     2663                );
     2664
     2665                $query_params['modified_after'] = array(
     2666                        'description' => __( 'Limit response to posts modified after a given ISO8601 compliant date.' ),
    26412667                        'type'        => 'string',
    26422668                        'format'      => 'date-time',
     
    26642690                $query_params['before'] = array(
    26652691                        'description' => __( 'Limit response to posts published before a given ISO8601 compliant date.' ),
     2692                        'type'        => 'string',
     2693                        'format'      => 'date-time',
     2694                );
     2695
     2696                $query_params['modified_before'] = array(
     2697                        'description' => __( 'Limit response to posts modified before a given ISO8601 compliant date.' ),
    26662698                        'type'        => 'string',
    26672699                        'format'      => 'date-time',
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r49603 r50024  
    199199                                'media_type',
    200200                                'mime_type',
     201                                'modified_after',
     202                                'modified_before',
    201203                                'offset',
    202204                                'order',
     
    561563                $request->set_param( 'after', '2016-01-15T00:00:00Z' );
    562564                $request->set_param( 'before', '2016-01-17T00:00:00Z' );
     565                $response = rest_get_server()->dispatch( $request );
     566                $data     = $response->get_data();
     567                $this->assertCount( 1, $data );
     568                $this->assertSame( $id2, $data[0]['id'] );
     569        }
     570
     571        /**
     572         * @ticket 50617
     573         */
     574        public function test_get_items_invalid_modified_date() {
     575                $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     576                $request->set_param( 'modified_after', rand_str() );
     577                $request->set_param( 'modified_before', rand_str() );
     578                $response = rest_get_server()->dispatch( $request );
     579                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     580        }
     581
     582        /**
     583         * @ticket 50617
     584         */
     585        public function test_get_items_valid_modified_date() {
     586                $id1 = $this->factory->attachment->create_object(
     587                        $this->test_file,
     588                        0,
     589                        array(
     590                                'post_date'      => '2016-01-01 00:00:00',
     591                                'post_mime_type' => 'image/jpeg',
     592                                'post_excerpt'   => 'A sample caption',
     593                        )
     594                );
     595                $id2 = $this->factory->attachment->create_object(
     596                        $this->test_file,
     597                        0,
     598                        array(
     599                                'post_date'      => '2016-01-02 00:00:00',
     600                                'post_mime_type' => 'image/jpeg',
     601                                'post_excerpt'   => 'A sample caption',
     602                        )
     603                );
     604                $id3 = $this->factory->attachment->create_object(
     605                        $this->test_file,
     606                        0,
     607                        array(
     608                                'post_date'      => '2016-01-03 00:00:00',
     609                                'post_mime_type' => 'image/jpeg',
     610                                'post_excerpt'   => 'A sample caption',
     611                        )
     612                );
     613                $this->update_post_modified( $id1, '2016-01-15 00:00:00' );
     614                $this->update_post_modified( $id2, '2016-01-16 00:00:00' );
     615                $this->update_post_modified( $id3, '2016-01-17 00:00:00' );
     616                $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     617                $request->set_param( 'modified_after', '2016-01-15T00:00:00Z' );
     618                $request->set_param( 'modified_before', '2016-01-17T00:00:00Z' );
    563619                $response = rest_get_server()->dispatch( $request );
    564620                $data     = $response->get_data();
  • trunk/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r49603 r50024  
    7777                                'include',
    7878                                'menu_order',
     79                                'modified_after',
     80                                'modified_before',
    7981                                'offset',
    8082                                'order',
     
    356358                $request->set_param( 'after', '2016-01-15T00:00:00Z' );
    357359                $request->set_param( 'before', '2016-01-17T00:00:00Z' );
     360                $response = rest_get_server()->dispatch( $request );
     361                $data     = $response->get_data();
     362                $this->assertCount( 1, $data );
     363                $this->assertSame( $post2, $data[0]['id'] );
     364        }
     365
     366        /**
     367         * @ticket 50617
     368         */
     369        public function test_get_items_invalid_modified_date() {
     370                $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
     371                $request->set_param( 'modified_after', rand_str() );
     372                $request->set_param( 'modified_before', rand_str() );
     373                $response = rest_get_server()->dispatch( $request );
     374                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     375        }
     376
     377        /**
     378         * @ticket 50617
     379         */
     380        public function test_get_items_valid_modified_date() {
     381                $post1 = $this->factory->post->create(
     382                        array(
     383                                'post_date' => '2016-01-01 00:00:00',
     384                                'post_type' => 'page',
     385                        )
     386                );
     387                $post2 = $this->factory->post->create(
     388                        array(
     389                                'post_date' => '2016-01-02 00:00:00',
     390                                'post_type' => 'page',
     391                        )
     392                );
     393                $post3 = $this->factory->post->create(
     394                        array(
     395                                'post_date' => '2016-01-03 00:00:00',
     396                                'post_type' => 'page',
     397                        )
     398                );
     399                $this->update_post_modified( $post1, '2016-01-15 00:00:00' );
     400                $this->update_post_modified( $post2, '2016-01-16 00:00:00' );
     401                $this->update_post_modified( $post3, '2016-01-17 00:00:00' );
     402                $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
     403                $request->set_param( 'modified_after', '2016-01-15T00:00:00Z' );
     404                $request->set_param( 'modified_before', '2016-01-17T00:00:00Z' );
    358405                $response = rest_get_server()->dispatch( $request );
    359406                $data     = $response->get_data();
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r49603 r50024  
    182182                                'exclude',
    183183                                'include',
     184                                'modified_after',
     185                                'modified_before',
    184186                                'offset',
    185187                                'order',
     
    14851487                $request->set_param( 'after', '2016-01-15T00:00:00Z' );
    14861488                $request->set_param( 'before', '2016-01-17T00:00:00Z' );
     1489                $response = rest_get_server()->dispatch( $request );
     1490                $data     = $response->get_data();
     1491                $this->assertCount( 1, $data );
     1492                $this->assertSame( $post2, $data[0]['id'] );
     1493        }
     1494
     1495        /**
     1496         * @ticket 50617
     1497         */
     1498        public function test_get_items_invalid_modified_date() {
     1499                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1500                $request->set_param( 'modified_after', rand_str() );
     1501                $request->set_param( 'modified_before', rand_str() );
     1502                $response = rest_get_server()->dispatch( $request );
     1503                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1504        }
     1505
     1506        /**
     1507         * @ticket 50617
     1508         */
     1509        public function test_get_items_valid_modified_date() {
     1510                $post1 = $this->factory->post->create( array( 'post_date' => '2016-01-01 00:00:00' ) );
     1511                $post2 = $this->factory->post->create( array( 'post_date' => '2016-01-02 00:00:00' ) );
     1512                $post3 = $this->factory->post->create( array( 'post_date' => '2016-01-03 00:00:00' ) );
     1513                $this->update_post_modified( $post1, '2016-01-15 00:00:00' );
     1514                $this->update_post_modified( $post2, '2016-01-16 00:00:00' );
     1515                $this->update_post_modified( $post3, '2016-01-17 00:00:00' );
     1516                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1517                $request->set_param( 'modified_after', '2016-01-15T00:00:00Z' );
     1518                $request->set_param( 'modified_before', '2016-01-17T00:00:00Z' );
    14871519                $response = rest_get_server()->dispatch( $request );
    14881520                $data     = $response->get_data();
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r49925 r50024  
    299299                            "required": false
    300300                        },
     301                        "modified_after": {
     302                            "description": "Limit response to posts modified after a given ISO8601 compliant date.",
     303                            "type": "string",
     304                            "format": "date-time",
     305                            "required": false
     306                        },
    301307                        "author": {
    302308                            "description": "Limit result set to posts assigned to specific authors.",
     
    319325                        "before": {
    320326                            "description": "Limit response to posts published before a given ISO8601 compliant date.",
     327                            "type": "string",
     328                            "format": "date-time",
     329                            "required": false
     330                        },
     331                        "modified_before": {
     332                            "description": "Limit response to posts modified before a given ISO8601 compliant date.",
    321333                            "type": "string",
    322334                            "format": "date-time",
     
    14771489                            "required": false
    14781490                        },
     1491                        "modified_after": {
     1492                            "description": "Limit response to posts modified after a given ISO8601 compliant date.",
     1493                            "type": "string",
     1494                            "format": "date-time",
     1495                            "required": false
     1496                        },
    14791497                        "author": {
    14801498                            "description": "Limit result set to posts assigned to specific authors.",
     
    14971515                        "before": {
    14981516                            "description": "Limit response to posts published before a given ISO8601 compliant date.",
     1517                            "type": "string",
     1518                            "format": "date-time",
     1519                            "required": false
     1520                        },
     1521                        "modified_before": {
     1522                            "description": "Limit response to posts modified before a given ISO8601 compliant date.",
    14991523                            "type": "string",
    15001524                            "format": "date-time",
     
    25402564                            "required": false
    25412565                        },
     2566                        "modified_after": {
     2567                            "description": "Limit response to posts modified after a given ISO8601 compliant date.",
     2568                            "type": "string",
     2569                            "format": "date-time",
     2570                            "required": false
     2571                        },
    25422572                        "author": {
    25432573                            "description": "Limit result set to posts assigned to specific authors.",
     
    25602590                        "before": {
    25612591                            "description": "Limit response to posts published before a given ISO8601 compliant date.",
     2592                            "type": "string",
     2593                            "format": "date-time",
     2594                            "required": false
     2595                        },
     2596                        "modified_before": {
     2597                            "description": "Limit response to posts modified before a given ISO8601 compliant date.",
    25622598                            "type": "string",
    25632599                            "format": "date-time",
     
    31893225                            "required": false
    31903226                        },
     3227                        "modified_after": {
     3228                            "description": "Limit response to posts modified after a given ISO8601 compliant date.",
     3229                            "type": "string",
     3230                            "format": "date-time",
     3231                            "required": false
     3232                        },
    31913233                        "before": {
    31923234                            "description": "Limit response to posts published before a given ISO8601 compliant date.",
     3235                            "type": "string",
     3236                            "format": "date-time",
     3237                            "required": false
     3238                        },
     3239                        "modified_before": {
     3240                            "description": "Limit response to posts modified before a given ISO8601 compliant date.",
    31933241                            "type": "string",
    31943242                            "format": "date-time",
Note: See TracChangeset for help on using the changeset viewer.