-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Implement WP_REST_Search_Controller
class
#6489
Conversation
…port for individual search results.
/** | ||
* Gets the post statuses allowed for search. | ||
* | ||
* @since 1.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since 2.9.0
/** | ||
* Gets the post types allowed for search. | ||
* | ||
* @since 1.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since 2.9.0
Note that the test failure comes from not including the 'gutenberg' text domain in translation strings. Since we're gonna move this over to core once it's done and not merge it into the plugin, I think we can ignore this? |
Let's go ahead and include them for now. They're easy enough to remove after the fact. |
// Retrieve the list of registered collection query parameters. | ||
$registered = $this->get_collection_params(); | ||
$query_args = array( | ||
'post_status' => array_keys( $this->get_allowed_post_stati() ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wp_link_query
uses post_status=>publish
. Is there a reason we're opening things up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any post status that identifies as its content as public should be searched. That is publish
only in most cases, but plugins may add to that.
I also included inherit
to be able to search for attachments. If that should not be possible, I'd be okay with removing that status.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should focus on only supporting post_status=publish
for now.
Support for post_status=inherit
opens up a can of permissions-related worms. Custom post statuses should be handled in conjunction with #3144
'$schema' => 'http://json-schema.org/draft-04/schema#', | ||
'title' => 'search', | ||
'type' => 'object', | ||
'properties' => array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should drop: author
, content
, excerpt
, date
, date_gmt
, guid
, modified
, modified_gmt
, and status
for a couple of reasons:
- They aren't absolutely necessary for link search, which is the immediate problem we need to solve.
- They'll significantly reduce complexity for the controller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree as this is supposed to be merged into core. A REST controller in core should be available to cover common use-cases beyond Gutenberg. If this was a first test for Gutenberg internally, I'd be okay with using only the few fields needed for that very case, but for core, the typical properties expected should be available.
Furthermore, they don't really add complexity, but just increase the response. I'll implement the new _fields
filtering so that those fields are not even generated when they're not needed. Gutenberg can then make a request like wp/v2/search?search=mytext&_fields=id,title,type,link
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be okay with using only the few fields needed for that very case, but for core, the typical properties expected should be available.
Can you explain how you define "typical properties", and where, say, modified_gmt
fits in this category?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd define as typical properties those that exist on every post regardless of post type, i.e. things that are part of the wp_posts
database table. In that regard I'd also like to align the format with that from the other post type-specific controllers.
With the changes included in the latest commit, only item data for the fields requested is generated. This means that a core version later than https://core.trac.wordpress.org/changeset/43087 is required to run this PR. |
311413e provides tests for the whole controller functionality. d7b1762 introduces a I think this is now in a solid state to be considered for core merge. For Gutenberg and the link modal specifically, a request |
Remaining test errors are only occurring because Gutenberg tests against the latest WordPress version, not against trunk. In core, all of this would work (again, since it requires https://core.trac.wordpress.org/changeset/43087). |
After talking to @danielbachhuber, it became clear that there are essentially two possible approaches for implementing a search endpoint:
For Gutenberg itself, a simple endpoint that could be just like the one implemented here without the unneeded schema properties and collection params would be sufficient. The good thing is that that version will allow to later shape it into either of the two directions. However, for core, we should decide whether we move forward with the first or the second approach. |
@danielbachhuber I made all changes you suggest, except changing the post statuses because I don't think it makes sense to get rid of them (see #6489 (comment)) - flagging this here since GH no longer shows it because the lines you commented have changed. The gist is: Both custom post statuses and |
With the latest commit, I changed the |
@felixarntz Looking pretty good. It doesn't look like it searches against If it is necessary, we'll need unit test coverage for it, including permissions checks. |
I decided to exclude attachments from the search because they aren't currently searched in the link modal. We could change that if that is preferred, but regardless of that decision I think the controller should support attachments as this is going to be merged into WordPress core, and search is not only used in Gutenberg. I can add tests for it too. |
Where else is search used in WordPress core? |
The main search functionality (search form in frontend) could use this for example. Plugins can use this. By making this only tailored to Gutenberg's very use-case, we force developers to unnecessarily rewrite a lot of code for only simple changes. Especially since this is nothing complicated and has been solved before, I don't think it's worth keeping it out. |
Does it?
Can plugins write their own code to modify the behavior of this controller?
Can you share the code that's necessary to rewrite?
This is good read on the topic. Notably, features should be added based on genuine need, not hypothetical. Based on the information presented, support for searching attachments doesn't appear necessary for our current use-case. |
Not yet obviously, but it would be a good use-case for this controller.
They cannot filter these things, but they can use the controller by making a request to it. If a plugin wants to search content including attachments, this controller would be worthless to them without it.
The WordPress search is a valid use-case for this controller, and without attachments it would be incomplete. What is the point of introducing a REST API controller that searches posts, but doesn't actually work for all posts? In addition, even a post of another post type can have a status of |
Plugins can't filter the behavior of this controller to adapt it to their needs?
"without attachments it would be incomplete" seems to communicate a hypothetical future, not the current reality. To my knowledge, and correct me if I'm wrong, WordPress frontend search doesn't include attachments by default. Am I wrong?
It seems to solve our immediate needs for link search just fine.
Can you point me to a real-world example?
This statement seems to cherry-pick a quote and ignore the main point of the post. |
It's currently not possible to filter things like the supported collection request parameters and how content is searched. Let me know if this should be added.
I read the post and understand what it says, we apparently interpret it differently. Anyway, the recent bits of the discussion seem to not move this forward. I think we need a third opinion on the topic of flexibility vs pragmatism here. |
Sure, filters would be a great addition.
Sounds good. cc @pento |
This was specifically requested in #4598 |
@felixarntz Are you alright with removing |
I talked with @rmccue for a while at WCEU, and we came up with a plan for this:
|
👍 Works for me.
One nice thing about landing in Gutenberg early is that we can iterate on the endpoint based on real-world usage, and then commit to core once we think it's finalized. I'd prefer to follow this approach; however, if you prefer to work on the endpoint design via Trac patches that sounds reasonable to me. |
I'm inclined to agree with @danielbachhuber, let's get this into Gutenberg first, iterating on Trac is slow and annoying. Once we have Gutenberg's usage locked down, we'll be in a good place to look at expanding it to more data types. |
Sounds good to me. Will work on the new version either this weekend at WC Sevilla Contributor Day, or early next week. I'm inclined to create a new PR though and close this one, only referencing it from there. Is that good with you? |
Hey, @felixarntz: have you had a chance to look at this? |
@pento I will tomorrow! |
Just chiming in to note that @felixarntz summarised the conversation well. The key point for the Agreed with landing in Gutenberg first for iteration speed as well. |
@danielbachhuber @pento @rmccue I opened #7894 for the new implementation of the global search controller. Let's continue there. |
Were any filters introduced here? I can't seem to locate any in core. |
Description
This PR aims at introducing a
WP_REST_Search_Controller
class to allow searching across multiple post types via the REST API. This will allow powering the link modal and other related ideas where results must not be restricted to a single post type.While this PR should be implemented and evaluated here, the final implementation will then be merged back into core, with the
gutenberg/v1
namespace changed towp/v2
. See https://core.trac.wordpress.org/ticket/39965 for the related ticket.This addresses #2084.
How has this been tested?
For now I've only performed a few API calls to test things work as expected. I plan on writing unit tests later this week.
Types of changes
WP_REST_Search_Controller
is introduced, and its routes are registered.WP_REST_Posts_Controller
.public
andshow_in_rest
set to true are searched. The post types to search can be further restricted with atype
request parameter.public
set to true, plusinherit
are searched.WP_REST_Posts_Controller
that already has a lot of the necessary functionality implemented. It's still too different though to extend it. We may wanna consider outsourcing some common functionality, but let's keep it simple for now.Checklist: