Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#55857 closed enhancement (fixed)

Usage of wp_get_post_revisions in WP_REST_Posts_Controller

Reported by: furi3r's profile furi3r Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.1 Priority: normal
Severity: normal Version:
Component: REST API Keywords: has-patch has-unit-tests add-to-field-guide
Focuses: rest-api, performance Cc:

Description

I've come across this line of code:
https://github.com/WordPress/wordpress-develop/blob/6.0/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L2020

We could optimize that query since wp_get_post_revisions accepts arguments, and we are going to use only the last revision, we could use LIMIT, instead of querying all of them.


Change History (22)

#1 @TimothyBlynJacobs
2 years ago

We do also need the revision count.

This ticket was mentioned in PR #2836 on WordPress/wordpress-develop by manuelRod.


2 years ago
#2

  • Keywords has-patch has-unit-tests added

Trac ticket: #55857

#3 @furi3r
2 years ago

@TimothyBlynJacobs I've working on it, you can take a look to the PR.

#4 @peterwilsoncc
2 years ago

There's a few places aside from the rest api that use wp_get_post_revisions() to do a count that could make use of this code.

#5 @furi3r
2 years ago

@peterwilsoncc I would go first for the rest API usage, since this is introducing a new core function, and the scope here will be more limited. Then we can follow up in another trac for other core occurrences (mainly revision.php)

manuelRod commented on PR #2836:


2 years ago
#6

@spacedmonkey @peterwilsoncc I've added the new round of revisions.

#7 @spacedmonkey
2 years ago

  • Owner set to spacedmonkey
  • Resolution set to fixed
  • Status changed from new to closed

In 53759:

REST API: Use wp_get_lastest_revision_id_and_total_count function in WP_REST_Posts_Controller class.

Add new function called wp_get_lastest_revision_id_and_total_count, that performs an optimized query to get the last revision and total and use it in WP_REST_Posts_Controller class.

Props Spacedmonkey, timothyblynjacobs, furi3r, peterwilsoncc.
Fixes #55857.

spacedmonkey commented on PR #2836:


2 years ago
#8

Committed.

#9 follow-up: @JustinSainton
2 years ago

Maybe a silly note, but this reads odd to me as wp_get_lastest_revision_id_and_total_count instead of wp_get_latest_revision_id_and_total_count, especially when the docs use the word latest and not lastest (which I'm not certain is a word...but I'm also not certain it's not 🤣)

#10 in reply to: ↑ 9 ; follow-up: @SergeyBiryukov
2 years ago

Replying to JustinSainton:

Maybe a silly note, but this reads odd to me as wp_get_lastest_revision_id_and_total_count instead of wp_get_latest_revision_id_and_total_count, especially when the docs use the word latest and not lastest

Good point. Looking at the tests, they mention "wp_get_last_revision_id_and_total_count()", and the variable name is $last_revision (here and in a few other places in core), so I think this was supposed to be "last".

Last edited 2 years ago by SergeyBiryukov (previous) (diff)

#11 @SergeyBiryukov
2 years ago

In 53769:

Revisions: Correct the function name for retrieving the last revision ID and total count.

Includes:

  • Renaming the function to wp_get_last_revision_id_and_total_count().
  • Making the default value for $post consistent with wp_get_post_revisions().
  • Making WP_Error codes more specific and using them in test assertions.
  • Adjusting the function description per the documentation standards.

Follow-up to [53759].

Props JustinSainton, SergeyBiryukov.
See #55857.

#12 @SergeyBiryukov
2 years ago

  • Milestone changed from Awaiting Review to 6.1

#14 in reply to: ↑ 10 @SergeyBiryukov
2 years ago

Replying to SergeyBiryukov:

Looking at the tests, they mention "wp_get_last_revision_id_and_total_count()", and the variable name is $last_revision (here and in a few other places in core), so I think this was supposed to be "last".

Looks like I missed this comment making an argument in favor of "latest" instead of "last". If anyone feels strongly about that, feel free to reopen, but in that case I would also suggest updating the "last revision" wording in other places.

#15 @peterwilsoncc
2 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

My feelings are just strong enough to reopen the ticket. What is the first and last revision seems a little ambiguous to me.

What got me to reopen was that Core refers to latest posts, latest comments, etc fairly consistently based on a basic search for _last_ vs _latest_.

I create a follow up PR.

Last edited 2 years ago by peterwilsoncc (previous) (diff)

SergeyBiryukov commented on PR #3020:


2 years ago
#17

Looks good to me 👍

I have added a commit with some more instances. Though not directly related to the new function, it aims for more consistency in core, always referring to the latest revision as "latest" instead of "last". This can be committed separately if preferred.

#18 @SergeyBiryukov
2 years ago

In 53778:

Revisions: Rename the function for retrieving the latest revision ID and total count.

The new name is wp_get_latest_revision_id_and_total_count().

This aims to reduce ambiguity about what exactly is the "first" or "last" revision, and bring more consistency with similar wording elsewhere in core, e.g. latest posts, latest comments, etc.

Follow-up to [53759], [53769].

Props peterwilsoncc.
See #55857.

#19 @SergeyBiryukov
2 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 53779:

Revisions: Update the "last revision" wording to "latest revision" in various files.

This aims to reduce ambiguity about what exactly is the "first" or "last" revision, and bring more consistency with similar wording elsewhere in core, e.g. latest posts, latest comments, etc.

This affects:

  • wp_save_post_revision()
  • wp_prepare_revisions_for_js()
  • WP_Customize_Manager::filter_revision_post_has_changed()

Follow-up to [53759], [53769], [53778].

Props peterwilsoncc.
Fixes #55857.

SergeyBiryukov commented on PR #3020:


2 years ago
#20

Merged in r53778 and r53779.

#21 @SergeyBiryukov
2 years ago

In 53841:

Revisions: Use latest_id as the array key for the latest revision ID.

This updates wp_get_latest_revision_id_and_total_count() and its usage to be a bit more descriptive and a bit less repetitive, e.g. $revisions['latest_id'] instead of $revision['revision'].

Includes updating the @return tag to explain when the function returns a WP_Error.

Follow-up to [53759], [53769], [53778], [53779].

See #55857.

#22 @milana_cap
2 years ago

  • Keywords add-to-field-guide added
Note: See TracTickets for help on using tickets.