From efbcdc5ae730aa7e1fee1a4cbe76c5eb900ab618 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Wed, 26 May 2021 13:24:24 -0400 Subject: [PATCH] Block Patterns API: Update for core compatibility (#32240) --- lib/block-patterns.php | 6 +++++- lib/class-wp-rest-pattern-directory-controller.php | 14 +++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/block-patterns.php b/lib/block-patterns.php index 4ea77fc70cde96..3756d6db20a1fb 100644 --- a/lib/block-patterns.php +++ b/lib/block-patterns.php @@ -203,9 +203,13 @@ function remove_core_patterns() { * Import patterns from wordpress.org/patterns. */ function load_remote_patterns() { + // This is the core function that provides the same feature. + if ( function_exists( '_load_remote_block_patterns' ) ) { + return; + } $patterns = get_transient( 'gutenberg_remote_block_patterns' ); if ( ! $patterns ) { - $request = new WP_REST_Request( 'GET', '/__experimental/pattern-directory/patterns' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); $core_keyword_id = 11; // 11 is the ID for "core". $request->set_param( 'keyword', $core_keyword_id ); $response = rest_do_request( $request ); diff --git a/lib/class-wp-rest-pattern-directory-controller.php b/lib/class-wp-rest-pattern-directory-controller.php index c42c4e8422a11b..3e8bb20f457944 100644 --- a/lib/class-wp-rest-pattern-directory-controller.php +++ b/lib/class-wp-rest-pattern-directory-controller.php @@ -20,11 +20,12 @@ * @see WP_REST_Controller */ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller { + /** * Constructs the controller. */ public function __construct() { - $this->namespace = '__experimental'; + $this->namespace = 'wp/v2'; $this->rest_base = 'pattern-directory'; } @@ -56,7 +57,7 @@ public function register_routes() { * * @return WP_Error|bool True if the request has permission, WP_Error object otherwise. */ - public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Method must match signature of parent class. + public function get_items_permissions_check( $request ) { if ( current_user_can( 'edit_posts' ) ) { return true; } @@ -158,7 +159,7 @@ public function get_items( $request ) { * * @return WP_REST_Response */ - public function prepare_item_for_response( $raw_pattern, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Method must match signature of parent class. + public function prepare_item_for_response( $raw_pattern, $request ) { $prepared_pattern = array( 'id' => absint( $raw_pattern->id ), 'title' => sanitize_text_field( $raw_pattern->title->rendered ), @@ -167,7 +168,6 @@ public function prepare_item_for_response( $raw_pattern, $request ) { // phpcs:i 'keywords' => array_map( 'sanitize_title', $raw_pattern->keyword_slugs ), 'description' => sanitize_text_field( $raw_pattern->meta->wpop_description ), 'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ), - ); $prepared_pattern = $this->add_additional_fields_to_object( $prepared_pattern, $request ); @@ -183,7 +183,7 @@ public function prepare_item_for_response( $raw_pattern, $request ) { // phpcs:i * @param object $raw_pattern The unprepared pattern. * @param WP_REST_Request $request The request object. */ - return apply_filters( 'rest_prepare_application_password', $response, $raw_pattern, $request ); + return apply_filters( 'rest_prepare_block_pattern', $response, $raw_pattern, $request ); } /** @@ -261,7 +261,7 @@ public function get_item_schema() { /** * Retrieves the search params for the patterns collection. * - * @since 5.5.0 + * @since 5.8.0 * * @return array Collection parameters. */ @@ -290,7 +290,7 @@ public function get_collection_params() { /** * Filter collection parameters for the pattern directory controller. * - * @since 5.5.0 + * @since 5.8.0 * * @param array $query_params JSON Schema-formatted collection parameters. */