-
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?
Changes from 9 commits
c0fc1c2
35ab880
f6e1372
6750bb6
f337c93
96fa58c
98bd9a8
3e0a338
5c63265
320aecb
d7b3959
b0e7a3a
466a524
9726549
81dac0c
bd4b2b2
8e65e9e
be1ef66
8636df2
29cafb9
7ddfb5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/** | ||
* REST API: WP_REST_Post_Archive_Search_Handler class | ||
* | ||
* @package WordPress | ||
* @subpackage REST_API | ||
* @since 6.6.0 | ||
*/ | ||
|
||
/** | ||
* Core class representing a search handler for Post Archives in the REST API. | ||
* | ||
* @since 5.6.0 | ||
* | ||
* @see WP_REST_Search_Handler | ||
*/ | ||
class WP_REST_Post_Archive_Search_Handler extends WP_REST_Search_Handler { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since 5.6.0 | ||
*/ | ||
public function __construct() { | ||
$this->type = 'post-type-archive'; | ||
} | ||
|
||
/** | ||
* Searches post-type archives for a given search request. | ||
* | ||
* @since 5.6.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 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 ) { | ||
|
||
$search_term = $request['search']; | ||
|
||
$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, 'objects' ); | ||
$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->name, $search_term ) !== false ) { | ||
$found_ids[] = $post_type->name; | ||
} | ||
} | ||
} | ||
|
||
$page = (int) $request['page']; | ||
$per_page = (int) $request['per_page']; | ||
|
||
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 5.6.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 string $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 ] = $post_type->name; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Prepares links for the search result of a given ID. | ||
* | ||
* @since 5.6.0 | ||
* | ||
* @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 commentThe 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, |
||
|
||
$links = array(); | ||
|
||
return $links; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,9 @@ | |
add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' ); | ||
|
||
|
||
|
||
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. Nitpick: Are these empty lines needed for some reason? |
||
|
||
|
||
/** | ||
* Shim for get_sample_permalink() to add support for auto-draft status. | ||
* | ||
|
@@ -92,3 +95,13 @@ | |
return $permalink; | ||
} | ||
add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); | ||
|
||
|
||
|
||
function register_post_archive_rest_search_handler( $handlers ) { | ||
$handlers[] = new WP_REST_Post_Archive_Search_Handler(); | ||
|
||
|
||
return $handlers; | ||
} | ||
add_filter( 'wp_rest_search_handlers', 'register_post_archive_rest_search_handler' ); | ||
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.