Skip to content

Commit

Permalink
move new class in the intended Core directory and use new pattern to …
Browse files Browse the repository at this point in the history
…avoid duplicate declarations
  • Loading branch information
petitphp committed Apr 15, 2024
1 parent 2c71b10 commit 7a383f8
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 133 deletions.

This file was deleted.

47 changes: 26 additions & 21 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,23 @@ function _gutenberg_get_search_result_label_field( $result_object, $field_name,
* @internal
*/
function _gutenberg_register_search_result_additional_fields() {
global $wp_rest_additional_fields;

if ( isset( $wp_rest_additional_fields['search-result']['label'] ) ) {
return;
$search_controller = new WP_REST_Search_Controller( array() );
if ( ! isset( $search_controller->get_item_schema()['property']['label'] ) ) {
register_rest_field(
'search-result',
'label',
array(
'get_callback' => '_gutenberg_get_search_result_label_field',
'update_callback' => null,
'schema' => array(
'description' => __( 'Object human readable subtype.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
)
);
}

register_rest_field(
'search-result',
'label',
array(
'get_callback' => '_gutenberg_get_search_result_label_field',
'update_callback' => null,
'schema' => array(
'description' => __( 'Object human readable subtype.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
)
);
}

add_action( 'rest_api_init', '_gutenberg_register_search_result_additional_fields' );
Expand All @@ -109,8 +106,16 @@ function _gutenberg_register_search_result_additional_fields() {
* @return array
*/
function _gutenberg_register_media_search_handler( $handlers ) {
if ( class_exists( 'WP_REST_Media_Search_Handler_Gutenberg' ) ) {
$handlers[] = new WP_REST_Media_Search_Handler_Gutenberg();
$should_load_media_search_handler = true;
foreach ( $handlers as $handler ) {
if ( $handler instanceof WP_REST_Media_Search_Handler ) {
$should_load_media_search_handler = false;
break;
}
}

if ( $should_load_media_search_handler ) {
$handlers[] = new WP_REST_Media_Search_Handler();
}

return $handlers;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* REST API: WP_REST_Media_Search_Handler class
*
* @package WordPress
* @subpackage REST_API
* @since 6.6.0
*/

if ( ! class_exists( 'WP_REST_Media_Search_Handler' ) ) {
/**
* Core class representing a search handler for attachments in the REST API.
*
* @since 6.6.0
*
* @see WP_REST_Search_Handler
*/
class WP_REST_Media_Search_Handler extends WP_REST_Post_Search_Handler {

/**
* Constructor.
*
* @since 6.6.0
*/
public function __construct() {
parent::__construct();
$this->type = 'media';
$this->subtypes = array();
}

/**
* Searches the object type content for a given search request.
*
* @since 6.6.0
*
* @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 ) {

$query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'paged' => (int) $request['page'],
'posts_per_page' => (int) $request['per_page'],
);

if ( ! empty( $request['search'] ) ) {
$query_args['s'] = $request['search'];

// Filter query clauses to include filenames.
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
}

if ( ! empty( $request['exclude'] ) ) {
$query_args['post__not_in'] = $request['exclude'];
}

if ( ! empty( $request['include'] ) ) {
$query_args['post__in'] = $request['include'];
}

/**
* Filters the query arguments for a REST API search request.
*
* Enables adding extra arguments or setting defaults for a media search request.
*
* @since 6.6.0
*
* @param array $query_args Key value array of query var to query value.
* @param WP_REST_Request $request The request used.
*/
$query_args = apply_filters( 'rest_media_search_query', $query_args, $request );

$query = new WP_Query();
$posts = $query->query( $query_args );
// Querying the whole post object will warm the object cache, avoiding an extra query per result.
$found_ids = wp_list_pluck( $posts, 'ID' );
$total = $query->found_posts;

return array(
self::RESULT_IDS => $found_ids,
self::RESULT_TOTAL => $total,
);
}

/**
* Prepares the search result for a given ID.
*
* @since 6.6.0
*
* @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 ) {
$data = parent::prepare_item( $id, $fields );

if ( isset( $data[ WP_REST_Search_Controller::PROP_SUBTYPE ] ) ) {
unset( $data[ WP_REST_Search_Controller::PROP_SUBTYPE ] );
}

return $data;
}
}
}
3 changes: 1 addition & 2 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/compat/wordpress-6.5/rest-api.php';

// WordPress 6.6 compat.
require_once __DIR__ . '/compat/wordpress-6.6/class-wp-rest-media-search-handler-gutenberg.php';
require_once __DIR__ . '/compat/wordpress-6.6/rest-api.php';
require_once __DIR__ . '/compat/wordpress-6.6/rest-api/search/class-wp-rest-media-search-handler.php';

// Plugin specific code.
require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php';
Expand Down

0 comments on commit 7a383f8

Please sign in to comment.