-
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
Try add Post Type Archive in Link UI and Navigation Link #66821
base: trunk
Are you sure you want to change the base?
Conversation
Size Change: +134 B (+0.01%) Total Size: 1.82 MB
ℹ️ View Unchanged
|
* @type string|int|WP_Error $total Numeric string containing the number of post-type archives found, or WP_Error object. | ||
* } | ||
*/ | ||
public function search_items( WP_REST_Request $request ) { |
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.
Need to check possible $request
params for the search handler endpoints and ensure we're handling those correctly.
For example, offset and pagination...etc.
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.
Here are the Search endpoint docs.
We're handling pagination based on this example from post-formats.
* @param int $id Item ID. | ||
* @return array[] Array of link arrays for the given item. | ||
*/ | ||
public function prepare_item_links( $id ) { |
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.
Is it worth linking to the collection for each post archive returned? I have no use case for this so might be unnecessary at this stage,
Obviously we'll need to write some unit tests for the new endpoint. |
Flaky tests detected in 5c63265. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11723564937
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @gwwar, @mgratch, @Odinnh, @wpchannel, @walton-alex, @nathan-schmidt-viget. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Thank you for working on this ❤️ Really looking forward to having this in core 🚀 |
@cr0ybot As the contributor who provided the most popular work-around for the original Issue I wonder if you would consider reviewing this PR? You can use Playground to make this super simple. |
This doesn't really semantically feel like a search handler. We aren't querying the database at all. It seems like this should be using the types endpoint. It's probably worth looking at including non show_in_rest post types at this point. |
@getdave I've tested searching for custom post types with and without archives, with and without front. The first test worked well in that only post types with archives were findable, and the second worked well also until I changed the permalink structure, which broke the link in the navigation block. I realize that the current workaround with a custom link will also break if the permalink structure changes, but the old menu system would keep the link working. @TimothyBJacobs Does it matter if a subclass of |
👋 Thanks for taking a look at this. I did look at a few options before implementing a custom search handler. The
The only way I can see us being able to us the Types endpoint is by:
Also worth noting that we already have the Post Formats endpoint which is conceptually pretty similar to this Post Type Archive endpoint. I'm open to further advice on the REST API here @TimothyBJacobs. What's the best way for a client-side application to:
|
@cr0ybot That's a known issue but one I believe is separate to this PR. |
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.
Please find my review comments below. Thank you!
It would also be helpful to have some PHPUnit tests for the proposed new REST API controller, if possible. This isn’t a blocker, but unit tests can be useful in identifying any remaining issues, if any.
* Associative array containing found IDs and total count for the matching search results. | ||
* | ||
* @type int[] $ids Found post archive IDs. | ||
* @type string|int|WP_Error $total Numeric string containing the number of post-type archives found, or WP_Error object. |
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.
* @type string|int|WP_Error $total Numeric string containing the number of post-type archives found, or WP_Error object. | |
* @type int $total The number of post-type archives found. |
To my knowledge, count()
returns an integer.
// If any of the post types have `has_archive` set to true then add a post-type-archive variation. | ||
$has_archive = array_filter( | ||
$post_types, | ||
function ( $post_type ) { |
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.
This closure can be declared as static (might improve performance).
function ( $post_type ) { | |
static function ( $post_type ) { |
* @return array { | ||
* Associative array containing fields for the post-archive based on the `$fields` parameter. | ||
* | ||
* @type string $id Optional. Post Archive Slug. |
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.
Nitpick.
* @type string $id Optional. Post Archive Slug. | |
* @type id $id Optional. Post Archive Slug. |
@@ -20,6 +20,9 @@ function gutenberg_register_block_editor_settings() { | |||
add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' ); | |||
|
|||
|
|||
|
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.
Nitpick: Are these empty lines needed for some reason?
Thank you @anton-vlasenko 🙇
I agree 💯 I'm going to wait on writing them until I get a confidence check from @TimothyBJacobs that my reasoning for using a new handler is justified. |
What?
Enables searching for Post Type Archives using the Link UI and is specifically aimed at the Navigation block to bring parity with the Classic Menus system.
Closes #31452
Why?
Parity with Classic Menus where you could search for and add links to the Post Type Archives.
This also ensures that when visiting the Archives pages the
current-menu-item
class is added to the menu item.How?
Testing Instructions
Code to register CPTs for testing
+ Add block
and then look for thePost Type Archive Link
block variation. (hint if you can't see it then pressBrowse All
and search. It will come up automatically for subsequent rounds of testing).Testing Instructions for Keyboard
Screenshots or screencast