diff --git a/lib/compat/wordpress-6.6/class-wp-rest-media-search-handler-gutenberg.php b/lib/compat/wordpress-6.6/class-wp-rest-media-search-handler-gutenberg.php deleted file mode 100644 index ae4bd552b28278..00000000000000 --- a/lib/compat/wordpress-6.6/class-wp-rest-media-search-handler-gutenberg.php +++ /dev/null @@ -1,110 +0,0 @@ -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; - } -} diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index a157ec8272201e..ee1e00433074fe 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -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' ); @@ -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; diff --git a/lib/compat/wordpress-6.6/rest-api/search/class-wp-rest-media-search-handler.php b/lib/compat/wordpress-6.6/rest-api/search/class-wp-rest-media-search-handler.php new file mode 100644 index 00000000000000..d263cf7a832bbf --- /dev/null +++ b/lib/compat/wordpress-6.6/rest-api/search/class-wp-rest-media-search-handler.php @@ -0,0 +1,108 @@ +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; + } + } +} diff --git a/lib/load.php b/lib/load.php index 8370607bf7f1c4..3666bcb1d558f5 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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';