[wp-trac] [WordPress Trac] #40510: REST API: Post Revisions: Adding support for pagination

WordPress Trac noreply at wordpress.org
Sun Apr 1 20:48:57 UTC 2018


#40510: REST API: Post Revisions: Adding support for pagination
----------------------------------------+-----------------------
 Reporter:  benoitperson                |       Owner:  flixos90
     Type:  enhancement                 |      Status:  assigned
 Priority:  normal                      |   Milestone:  5.0
Component:  REST API                    |     Version:  4.7
 Severity:  normal                      |  Resolution:
 Keywords:  has-patch needs-unit-tests  |     Focuses:  rest-api
----------------------------------------+-----------------------

Comment (by birgire):

 This seems to be in a good progress.

 I played around with the {{{offset}}} and {{{page}}} parameters in
 [attachment:40510.2.diff] and noticed that for out-of-bound values, we
 loose the information for {{{X-WP-Total}}} and {{{X-WP-TotalPages}}} as
 they only show {{{0}}}.

 Here's an example for a post with two revisions and {{{per_page}}} as
 {{{1}}}:

 {{{
 page=1:
     [X-WP-Total] => 2
     [X-WP-TotalPages] => 2
     [Link] =>
 <http://wp.localhost/index.php?rest_route=%2Fwp%2Fv2%2Fposts%2F3%2Frevisions&per_page=1&page=2>;
 rel="next"

 page=2:
     [X-WP-Total] => 2
     [X-WP-TotalPages] => 2
     [Link] =>
 <http://wp.localhost/index.php?rest_route=%2Fwp%2Fv2%2Fposts%2F3%2Frevisions&per_page=1&page=1>;
 rel="prev"

 page=3:
     [X-WP-Total] => 0
     [X-WP-TotalPages] => 0
     [Link] =>
 <http://wp.localhost/index.php?rest_route=%2Fwp%2Fv2%2Fposts%2F3%2Frevisions&per_page=1&page=0>;
 rel="prev"

 }}}

 Here we see what happens with the out-of-bound value {{{3}}} for
 {{{page}}}.


 The reason is that the {{{found_posts}}} and {{{max_num_pages}}}
 properties of {{{WP_Query}}} are not calculated for empty results set. I'm
 not sure why that has to happen, because it's useful information.

 We can see that here:

 https://core.trac.wordpress.org/browser/tags/4.9.4/src/wp-includes/class-
 wp-query.php#L2828

 as there is no {{{$this->set_found_posts( $q, $limits );}}} call, but even
 if it were, there's an empty posts check within that method too.


 The post rest controller handles this, but only for the {{{page}}}
 parameter.


 Then I started to look at the corresponding tests and
 [attachment:40510.3.diff] is a suggestion that:

 - Handles out-of-bound values for {{{offset}}} and {{{page}}}.
 - Introduces tests for the revision query parameters:
  - offset
  - page
  - per_page
  - search

 The tests for the post rest controller where really helpful, but I tried
 to break them up into single test methods.

 The patch also adds another revision and the total revision count to the
 current fixtures, to make it more usable by the tests. I was hesitated to
 do this, but I ended up doing this as it seems cleaner way ;-)

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40510#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list