Skip to content

Commit

Permalink
Extend Types API to include archive link and update client side code
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Dec 10, 2024
1 parent 7ddfb5a commit 7106e9f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 144 deletions.

This file was deleted.

1 change: 0 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ function gutenberg_is_experiment_enabled( $name ) {
// WordPress 6.8 compat.
require __DIR__ . '/compat/wordpress-6.8/block-comments.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-post-archive-search-handler.php';

// Plugin specific code.
require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php';
Expand Down
28 changes: 21 additions & 7 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,25 @@ function gutenberg_register_edit_site_export_controller_endpoints() {
add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' );


/**
* Registers REST search handler for Post Type Archives.
*/
function gutenberg_register_post_archive_rest_search_handler( $handlers ) {
$handlers[] = new Gutenberg_REST_Post_Archive_Search_Handler();
return $handlers;


function gutenberg_register_archive_url_field() {
register_rest_field( 'type', 'archive_url', array(
'get_callback' => function( $object ) {
if ( isset( $object['has_archive'] ) && $object['has_archive'] ) {
$post_type = $object['slug'];
$archive_url = get_post_type_archive_link( $post_type );
return $archive_url ? $archive_url : '';
}
return '';
},
'update_callback' => null,
'schema' => array(
'description' => __( 'URL to the post type archive page' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
) );
}
add_filter( 'wp_rest_search_handlers', 'gutenberg_register_post_archive_rest_search_handler' );

add_action( 'rest_api_init', 'gutenberg_register_archive_url_field' );
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ type MediaAPIResult = {
type: string;
};

type PostTypesAPIResult = {
slug: string;
name: string;
archive_url: string;
};

export type SearchResult = {
/**
* Post or term id.
Expand Down Expand Up @@ -242,26 +248,27 @@ export default async function fetchLinkSuggestions(

if ( ! type || type === 'post-type-archive' ) {
queries.push(
apiFetch< SearchAPIResult[] >( {
path: addQueryArgs( '/wp/v2/search', {
search,
apiFetch< PostTypesAPIResult[] >( {
path: addQueryArgs( '/wp/v2/types', {
page,
per_page: perPage,
type: 'post-type-archive',
} ),
} )
.then( ( results ) => {
return results.map( ( result ) => {
return {
id: result.id,
url: result.url,
title:
decodeEntities( result.title || '' ) ||
__( '(no title)' ),
type: `${ result.type }`,
kind: 'post-type-archive',
};
} );
const resultValues = Object.values( results );
return resultValues
.filter( ( result ) => !! result.archive_url ) // Filter out results with falsy archive_url, including empty strings
.map( ( result, index ) => {
return {
id: index + 1, // avoid results being filtered due to falsy id
url: result.archive_url,
title:
decodeEntities( result.name || '' ) ||
__( '(no title)' ),
type: 'post-type', // Assuming 'post-type' is a constant value
kind: 'post-type-archive',
};
} );
} )
.catch( () => [] ) // Fail by returning no results.
);
Expand Down

0 comments on commit 7106e9f

Please sign in to comment.