Skip to content

Commit

Permalink
generalizes the category search handler to all terms
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed Jun 18, 2020
1 parent 7415d89 commit 9534391
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
<?php
/**
* REST API: WP_REST_Category_Search_Handler class
* REST API: WP_REST_Terms_Search_Handler class
*
* @package WordPress
* @subpackage REST_API
* @since 5.5.0
*/

/**
* Core class representing a search handler for categories in the REST API.
* Core class representing a search handler for term in the REST API.
*
* @since 5.5.0
*
* @see WP_REST_Search_Handler
*/
class WP_REST_Category_Search_Handler extends WP_REST_Search_Handler {
class WP_REST_Term_Search_Handler extends WP_REST_Search_Handler {

/**
* Constructor.
*
* @since 5.0.0
*/
public function __construct() {
$this->type = 'category';
$this->type = 'term';

// Support all public post types except attachments.
$this->subtypes = array_values(
get_taxonomies(
array(
'public' => true,
),
'names'
)
);
}

/**
Expand All @@ -38,21 +48,26 @@ public function __construct() {
public function search_items( WP_REST_Request $request ) {

if ( ! empty( $request['search'] ) ) {
$category_search = $request['search'];
$term_search = $request['search'];
}

$category_search = apply_filters( 'rest_category_search_query', $category_search, $request );
$term_search = apply_filters( 'rest_term_search_query', $term_search, $request );

$taxonomies = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $taxonomies, true ) ) {
$taxonomies = $this->subtypes;
}

$categories = get_categories(
$terms = get_terms(
array(
'get' => 'all',
'name__like' => $category_search,
'taxonomy' => $taxonomies,
'name__like' => $term_search,
)
);

$found_ids = array();
foreach ( $categories as $category ) {
$found_ids[] = $category->term_id;
foreach ( $terms as $term ) {
$found_ids[] = $term->term_id;
}

return array(
Expand All @@ -71,21 +86,21 @@ public function search_items( WP_REST_Request $request ) {
* @return array Associative array containing all fields for the item.
*/
public function prepare_item( $id, array $fields ) {
$category = get_category( $id );
$term = get_term( $id );

$data = array();

if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_ID ] = (int) $id;
}
if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_TITLE ] = $category->name;
$data[ WP_REST_Search_Controller::PROP_TITLE ] = $term->name;
}
if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_URL ] = get_category_link( $id );
$data[ WP_REST_Search_Controller::PROP_URL ] = get_term_link( $id );
}
if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
$data[ WP_REST_Search_Controller::PROP_TYPE ] = $term->taxonomy;
}

return $data;
Expand Down
4 changes: 2 additions & 2 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( ! class_exists( 'WP_REST_Post_Format_Search_Handler' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-post-format-search-handler.php';
}
if ( ! class_exists( 'WP_REST_Category_Search_Handler' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-category-search-handler.php';
if ( ! class_exists( 'WP_REST_Term_Search_Handler' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-term-search-handler.php';
}
/**
* End: Include for phase 2
Expand Down
8 changes: 4 additions & 4 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,14 @@ function gutenberg_post_format_search_handler( $search_handlers ) {
add_filter( 'wp_rest_search_handlers', 'gutenberg_post_format_search_handler', 10, 5 );

/**
* Registers the category search handler.
* Registers the terms search handler.
*
* @param string $search_handlers Title list of current handlers.
*
* @return array Title updated list of handlers.
*/
function gutenberg_category_search_handler( $search_handlers ) {
$search_handlers[] = new WP_REST_Category_Search_Handler();
function gutenberg_term_search_handler( $search_handlers ) {
$search_handlers[] = new WP_REST_Term_Search_Handler();
return $search_handlers;
}
add_filter( 'wp_rest_search_handlers', 'gutenberg_category_search_handler', 10, 5 );
add_filter( 'wp_rest_search_handlers', 'gutenberg_term_search_handler', 10, 5 );
10 changes: 5 additions & 5 deletions packages/edit-navigation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ import Layout from './components/layout';
* @return {Promise<Object[]>} List of suggestions
*/
const fetchLinkSuggestions = async ( search, { perPage = 20 } = {} ) => {
const posts = await apiFetch( {
const posts = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post',
} ),
} );

const categories = await apiFetch( {
const terms = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'category',
type: 'term',
} ),
} );

const formats = await apiFetch( {
const formats = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post-format',
} ),
} );

return Promise.all( [ posts, categories, formats ] ).then( ( results ) => {
return Promise.all( [ posts, terms, formats ] ).then( ( results ) => {
return map( flatten( results ), ( post ) => ( {
id: post.id,
url: post.url,
Expand Down
10 changes: 5 additions & 5 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,31 @@ import ConvertToGroupButtons from '../convert-to-group-buttons';
* @return {Promise<Object[]>} List of suggestions
*/
const fetchLinkSuggestions = async ( search, { perPage = 20 } = {} ) => {
const posts = await apiFetch( {
const posts = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post',
} ),
} );

const categories = await apiFetch( {
const terms = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'category',
type: 'term',
} ),
} );

const formats = await apiFetch( {
const formats = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post-format',
} ),
} );

return Promise.all( [ posts, categories, formats ] ).then( ( results ) => {
return Promise.all( [ posts, terms, formats ] ).then( ( results ) => {
return map( flatten( results ), ( post ) => ( {
id: post.id,
url: post.url,
Expand Down

0 comments on commit 9534391

Please sign in to comment.