Repository navigation
REST API: Add author filtering to autosave collections. #7555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamsilverstein
wants to merge
26
commits into
WordPress:trunk
Choose a base branch
from
adamsilverstein:ticket/62057
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
8b77257
Remove autosaves preload, reducing memory footprint
adamsilverstein 9c3ff0e
Merge branch 'trunk' into ticket/62057
adamsilverstein 1ab05ae
Try: add per_page support for autosaves
adamsilverstein 76731ee
Merge branch 'trunk' into ticket/62057
adamsilverstein 2851b9c
add test for per_page parameter
adamsilverstein 8a7992c
Add a test for the per_page parameter
adamsilverstein 5064081
Add tests for autosaves per_page parameter
adamsilverstein 64b9a54
phpcbf
adamsilverstein beb3859
autosaves route test: add per_page to expected parameters
adamsilverstein a1c0cbe
preload a single autosave
adamsilverstein 267d51d
phpcbf
adamsilverstein 0fa5288
merge trunk
adamsilverstein e06ecb1
remove autosave preload entirely
adamsilverstein 58511dc
Merge trunk into ticket/62057
adamsilverstein 120edbc
REST API: Allow the autosaves collection to be scoped by author.
adamsilverstein cc03ae0
Merge remote-tracking branch 'origin/trunk' into ticket/62057
adamsilverstein 6108638
Tests: Regenerate the REST API client fixtures for the autosaves auth…
adamsilverstein 4725ee0
Merge trunk into ticket/62057
adamsilverstein 916d1dd
Merge branch 'trunk' into ticket/62057
adamsilverstein d163ca5
Editor: Restore the unscoped autosaves preload path.
adamsilverstein fbb7534
REST API: Fetch author scoped autosaves in a single query.
adamsilverstein d6ba582
REST API: Require positive author IDs for autosaves.
adamsilverstein dc7a922
Tests: Cover invalid, repeated and template author autosave queries.
adamsilverstein 68c48b0
Tests: Update the REST API client fixture for the author minimum.
adamsilverstein 5f31cca
Tests: Give author scoped autosave tests explicit dates.
adamsilverstein 84953d3
REST API: Simplify author scoped autosave lookup.
adamsilverstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -307,6 +307,7 @@ public function get_item( $request ) { | |||
| * Contains the user's autosave, for empty if it doesn't exist. | ||||
| * | ||||
| * @since 5.0.0 | ||||
| * @since 7.2.0 Added support for the `author` parameter. | ||||
| * | ||||
| * @param WP_REST_Request $request Full details about the request. | ||||
| * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||||
|
|
@@ -321,8 +322,44 @@ public function get_items( $request ) { | |||
| // Return early as this handler doesn't add any response headers. | ||||
| return new WP_REST_Response( array() ); | ||||
| } | ||||
|
|
||||
| $response = array(); | ||||
| $parent_id = $parent->ID; | ||||
|
|
||||
| if ( ! empty( $request['author'] ) ) { | ||||
| $autosaves = get_posts( | ||||
| array( | ||||
| 'post_type' => 'revision', | ||||
| 'post_status' => 'inherit', | ||||
| 'post_parent' => $parent_id, | ||||
| 'name' => $parent_id . '-autosave-v1', | ||||
| 'author__in' => $request['author'], | ||||
| 'orderby' => array( | ||||
| 'date' => 'DESC', | ||||
| 'ID' => 'DESC', | ||||
| ), | ||||
| 'posts_per_page' => -1, | ||||
| 'suppress_filters' => false, | ||||
| ) | ||||
| ); | ||||
|
|
||||
| // Each author has at most one autosave; keep only the newest if not. | ||||
| $seen_authors = array(); | ||||
|
|
||||
| foreach ( $autosaves as $autosave ) { | ||||
| if ( in_array( $autosave->post_author, $seen_authors, true ) ) { | ||||
| continue; | ||||
| } | ||||
|
|
||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Micronit: This blank line separates two bits of PHP code which are part of the same “paragraph”.
Suggested change
|
||||
| $seen_authors[] = $autosave->post_author; | ||||
|
|
||||
| $data = $this->prepare_item_for_response( $autosave, $request ); | ||||
| $response[] = $this->prepare_response_for_collection( $data ); | ||||
| } | ||||
|
|
||||
| return rest_ensure_response( $response ); | ||||
| } | ||||
|
|
||||
| $revisions = wp_get_post_revisions( $parent_id, array( 'check_enabled' => false ) ); | ||||
|
|
||||
| foreach ( $revisions as $revision ) { | ||||
|
|
@@ -513,6 +550,15 @@ public function prepare_item_for_response( $item, $request ) { | |||
| public function get_collection_params() { | ||||
| return array( | ||||
| 'context' => $this->get_context_param( array( 'default' => 'view' ) ), | ||||
| 'author' => array( | ||||
| 'description' => __( 'Limit result set to autosaves assigned to specific authors.' ), | ||||
| 'type' => 'array', | ||||
| 'items' => array( | ||||
| 'type' => 'integer', | ||||
| 'minimum' => 1, | ||||
| ), | ||||
| 'default' => array(), | ||||
| ), | ||||
| ); | ||||
| } | ||||
| } | ||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.