Skip to content

Commit

Permalink
feat: prepare embedable post links in search results
Browse files Browse the repository at this point in the history
  • Loading branch information
lucymtc committed Jan 19, 2024
1 parent 18d0938 commit a593a75
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions wp/headless-wp/includes/classes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function register() {

$gutenberg = new Gutenberg();
$gutenberg->register();

$search = new Search\Search();
$search->register();
}

/**
Expand Down
68 changes: 68 additions & 0 deletions wp/headless-wp/includes/classes/Search/PostSearchHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Search Class
*
* @package HeadlessWP
*/

namespace HeadlessWP\Search;

/**
* Class PostSearchHandler
*/
class PostSearchHandler extends \WP_REST_Post_Search_Handler {

/**
* Overrides the prepare_item_links method for the search result to include author and taxonomies embedded data.
*
* @param int $id Item ID.
* @return array Links for the given item.
*/
public function prepare_item_links( $id ) {

$post = get_post( $id );
$links = parent::prepare_item_links( $id );

// Add author link
if ( isset( $post->post_author ) ) {
$links['author'] = array(
array(
'href' => rest_url( 'wp/v2/users/' . $post->post_author ),
'embeddable' => true,
),
);
}

// Add terms link (wp:term)
$taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
foreach ( $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post->ID, $taxonomy->name );

if ( ! empty( $terms ) ) {
$links['wp:term'][] = array(
'href' => add_query_arg( 'post', $post->ID, rest_url( 'wp/v2/' . $taxonomy->rest_base ) ),
'taxonomy' => $taxonomy->rest_base,
'embeddable' => true,
);
}
}

// Add wp:featuredmedia link
$featured_media_id = get_post_thumbnail_id( $post->ID );
if ( ! empty( $featured_media_id ) ) {
$links['wp:featuredmedia'] = array(
array(
'href' => rest_url( 'wp/v2/media/' . $featured_media_id ),
'embeddable' => true,
),
);
}

// Reduce the search response size.
if ( ! empty( $links['self'] ) ) {
$links['self']['embeddable'] = false;
}

return apply_filters( 'tenup_headless_wp_rest_search_item_embedable_links', $links, $post );
}
}
39 changes: 39 additions & 0 deletions wp/headless-wp/includes/classes/Search/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Search Class
*
* @package HeadlessWP
*/

namespace HeadlessWP\Search;

/**
* Search Class
*/
class Search {

/**
* Set up any hooks
*/
public function register() {
add_filter( 'wp_rest_search_handlers', array( $this, 'override_rest_search_handlers' ), 10, 1 );
}

/**
* Override \WP_REST_Post_Search_Handler to include posts _embed data in search results.
*
* @param array $search_handlers List of search handlers to use in WP_REST_Search_Controller.
* @return array
*/
public function override_rest_search_handlers( $search_handlers ) {

foreach ( $search_handlers as &$handler ) {
if ( $handler instanceof \WP_REST_Post_Search_Handler ) {
$handler = new PostSearchHandler();
break;
}
}

return $search_handlers;
}
}

0 comments on commit a593a75

Please sign in to comment.