-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 via new Post Type Archive Search Handler #66821
Closed
Closed
Try add Post Type Archive in Link UI and Navigation Link via new Post Type Archive Search Handler #66821
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c0fc1c2
Initial effort
getdave 35ab880
Temp debugging
getdave f6e1372
Basic initial REST API
getdave 6750bb6
Avoid unused code
getdave f337c93
Include post type in results
getdave 96fa58c
Parse search results
getdave 98bd9a8
Include correct default results for Post Type Archive variation
getdave 3e0a338
Remove redundan type
getdave 5c63265
Add fallback icon for custom post types
getdave 320aecb
Move handler and init code to 6.8 directories
getdave d7b3959
Update doc blocks
getdave b0e7a3a
Use Gutenberg prefix
getdave 466a524
Correct class name prefix
getdave 9726549
Fix linting
getdave 81dac0c
More linting
getdave bd4b2b2
Update types and fix tests
getdave 8e65e9e
Avoid querying for full post type objects where not needed
getdave be1ef66
More linting
getdave 8636df2
Another lint
getdave 29cafb9
Ignore unused variable sniff to satifsy interace of extended class
getdave 7ddfb5a
Apply suggestions from code review
getdave 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
121 changes: 121 additions & 0 deletions
121
lib/compat/wordpress-6.8/class-gutenberg-rest-post-archive-search-handler.php
This file contains 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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
/** | ||
* REST API: WP_REST_Post_Archive_Search_Handler class | ||
* | ||
* @package WordPress | ||
* @subpackage REST_API | ||
* @since 6.8.0 | ||
*/ | ||
|
||
/** | ||
* Core class representing a search handler for Post Archives in the REST API. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @see WP_REST_Search_Handler | ||
*/ | ||
class Gutenberg_REST_Post_Archive_Search_Handler extends WP_REST_Search_Handler { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since 6.8.0 | ||
*/ | ||
public function __construct() { | ||
$this->type = 'post-type-archive'; | ||
} | ||
|
||
/** | ||
* Searches post-type archives for a given search request. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @param WP_REST_Request $request Full REST request. | ||
* @return array { | ||
* Associative array containing found IDs and total count for the matching search results. | ||
* | ||
* @type int[] $ids Found post archive IDs. | ||
* @type int $total The number of post-type archives found. | ||
* } | ||
*/ | ||
public function search_items( WP_REST_Request $request ) { | ||
|
||
$search_term = $request['search']; | ||
$page = (int) $request['page']; | ||
$per_page = (int) $request['per_page']; | ||
|
||
$args = array( | ||
'public' => true, | ||
'has_archive' => true, // ensure only posts with archive are considered. | ||
'show_in_rest' => true, | ||
'_builtin' => false, | ||
); | ||
|
||
$post_types = get_post_types( $args ); | ||
$found_ids = array(); | ||
|
||
if ( ! empty( $post_types ) ) { | ||
foreach ( $post_types as $post_type ) { | ||
// Check if the search term matches the post type name. | ||
if ( empty( $search_term ) || stripos( $post_type, $search_term ) !== false ) { | ||
$found_ids[] = $post_type; | ||
} | ||
} | ||
} | ||
|
||
return array( | ||
self::RESULT_IDS => array_slice( $found_ids, ( $page - 1 ) * $per_page, $per_page ), | ||
self::RESULT_TOTAL => count( $found_ids ), | ||
); | ||
} | ||
|
||
/** | ||
* Prepares the search result for a given post archive ID. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @param int $id Term ID. | ||
* @param array $fields Fields to include for the post archive. | ||
* @return array { | ||
* Associative array containing fields for the post-archive based on the `$fields` parameter. | ||
* | ||
* @type id $id Optional. Post Archive Slug. | ||
* @type string $title Optional. Post Archive name. | ||
* @type string $url Optional. Post Archive permalink URL. | ||
* } | ||
*/ | ||
public function prepare_item( $id, array $fields ) { | ||
|
||
$post_type = get_post_type_object( $id ); | ||
|
||
$data = array(); | ||
|
||
if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) { | ||
$data[ WP_REST_Search_Controller::PROP_ID ] = $id; | ||
} | ||
if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) { | ||
$data[ WP_REST_Search_Controller::PROP_TITLE ] = $post_type->labels->archives; | ||
} | ||
if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) { | ||
$data[ WP_REST_Search_Controller::PROP_URL ] = get_post_type_archive_link( $id ); | ||
} | ||
|
||
if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) { | ||
$data[ WP_REST_Search_Controller::PROP_TYPE ] = sprintf( '%s-%s', $post_type->name, __( 'archive' ) ); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Prepares links for the search result of a given ID. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @param int $id Item ID. | ||
* @return array[] Array of link arrays for the given item. | ||
*/ | ||
public function prepare_item_links( $id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
return array(); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -371,6 +371,7 @@ function block_core_navigation_link_filter_variations( $variations, $block_type | |||||
* @return array | ||||||
*/ | ||||||
function block_core_navigation_link_build_variations() { | ||||||
|
||||||
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); | ||||||
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); | ||||||
|
||||||
|
@@ -392,6 +393,27 @@ function block_core_navigation_link_build_variations() { | |||||
$variations[] = $variation; | ||||||
} | ||||||
} | ||||||
|
||||||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. This closure can be declared as static (might improve performance).
Suggested change
|
||||||
return $post_type->has_archive; | ||||||
} | ||||||
); | ||||||
|
||||||
if ( $has_archive ) { | ||||||
$variation = array( | ||||||
'name' => 'post-type-archive', | ||||||
'title' => __( 'Post Type Archive Link' ), | ||||||
'description' => __( 'A link to a post type archive' ), | ||||||
'attributes' => array( | ||||||
'type' => 'all', | ||||||
'kind' => 'post-type-archive', | ||||||
), | ||||||
); | ||||||
$variations[] = $variation; | ||||||
} | ||||||
} | ||||||
if ( $taxonomies ) { | ||||||
foreach ( $taxonomies as $taxonomy ) { | ||||||
|
This file contains 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
This file contains 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
This file contains 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
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.
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?