-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prepare embedable post links in search results
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
wp/headless-wp/includes/classes/Search/PostSearchHandler.php
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
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 ); | ||
} | ||
} |
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
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; | ||
} | ||
} |