Created
June 12, 2020 14:51
-
-
Save goaround/3a91ed216453bb19b9da69a9643468fa to your computer and use it in GitHub Desktop.
WordPress Plugin: Taxonomy WP REST Search for the WordPress Block Editor (Gutenberg)
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
<?php | |
/** | |
* Plugin Name: Taxonomy WP REST Search | |
* Plugin URI: | |
* Description: Taxonomy WP REST Search for the WordPress Block Editor (Gutenberg) | |
* Author: Johannes Kinast <johannes@travel-dealz.de> | |
* Author URI: https://go-around.de | |
* Version: 1.0.0 | |
*/ | |
namespace Taxonomy_WP_REST_Search; | |
use WP_REST_Search_Handler; | |
use WP_REST_Request; | |
use WP_REST_Search_Controller; | |
use WP_Term_Query; | |
function wp_rest_search_handlers( $handlers ) { | |
// Add Taxonomy Handler | |
$handlers[] = new WP_REST_Taxonomy_Search_Handler(); | |
return $handlers; | |
} | |
add_filter( 'wp_rest_search_handlers', __NAMESPACE__ . '\wp_rest_search_handlers' ); | |
function change_search_type( $result, $server, $request ) { | |
if ( '/wp/v2/search' !== $request->get_route() ) { | |
return $result; | |
} | |
$supported = [ | |
'tax' => 'taxonomy', | |
'ter' => 'taxonomy', | |
]; | |
$matches = []; | |
preg_match( '/^-(\w{2,3}) (.*)/', $request->get_param( 'search' ), $matches ); | |
if ( 3 === count( $matches ) && isset( $supported[$matches[1]] ) ) { | |
$request->set_param( 'type', $supported[$matches[1]] ); | |
$request->set_param( 'search', $matches[2] ); | |
} | |
return $result; | |
} | |
add_filter( 'rest_pre_dispatch', __NAMESPACE__ . '\change_search_type', 10, 3 ); | |
class WP_REST_Taxonomy_Search_Handler extends WP_REST_Search_Handler { | |
public function __construct() { | |
$this->type = 'taxonomy'; | |
$this->subtypes = get_taxonomies( [ | |
'show_ui' => true, | |
'public' => true, | |
] ); | |
} | |
/** | |
* Searches the object type content for a given search request. | |
* | |
* | |
* @param WP_REST_Request $request Full REST request. | |
* @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing | |
* an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the | |
* total count for the matching search results. | |
*/ | |
public function search_items( WP_REST_Request $request ) { | |
// Get the taxonomy types to search for the current request. | |
$taxonomy_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ]; | |
if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $taxonomy_types, true ) ) { | |
$taxonomy_types = $this->subtypes; | |
} | |
$offset = ( $request['page'] - 1 ) * $request['per_page']; | |
$query_args = array( | |
'taxonomy' => $taxonomy_types, | |
// Replace paged with offset | |
//'paged' => (int) $request['page'], | |
'number' => (int) $request['per_page'], | |
'offset' => (int) $offset, | |
'fields' => 'ids', | |
'hide_empty' => false, | |
); | |
if ( ! empty( $request['search'] ) ) { | |
$query_args['search'] = $request['search']; | |
} | |
$query = new WP_Term_Query(); | |
$found_ids = $query->query( $query_args ); | |
$total = $offset + count( $found_ids ); | |
if ( count( $found_ids ) === (int) $request['per_page'] ) { | |
$total = $total + 1; | |
} | |
return [ | |
self::RESULT_IDS => $found_ids, | |
self::RESULT_TOTAL => $total, | |
]; | |
} | |
/** | |
* Prepares the search result for a given ID. | |
* | |
* | |
* @param int $id Item ID. | |
* @param array $fields Fields to include for the item. | |
* @return array Associative array containing all fields for the item. | |
*/ | |
public function prepare_item( $id, array $fields ) { | |
$term = get_term( $id ); | |
$data = []; | |
if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) { | |
$data[ WP_REST_Search_Controller::PROP_ID ] = (int) $term->term_id; | |
} | |
if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) { | |
$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_term_link( $term ); | |
} | |
if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) { | |
$data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type; | |
} | |
if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) { | |
$data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = get_taxonomy( $term->taxonomy )->labels->singular_name; | |
} | |
return $data; | |
} | |
/** | |
* Prepares links for the search result of a given ID. | |
* | |
* | |
* @param int $id Item ID. | |
* @return array Links for the given item. | |
*/ | |
public function prepare_item_links( $id ) { | |
$links = []; | |
return $links; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment