Skip to content

Commit

Permalink
Block Patterns API: Update for core compatibility (#32240)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed May 26, 2021
1 parent f9ef1cb commit efbcdc5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
14 changes: 7 additions & 7 deletions lib/class-wp-rest-pattern-directory-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 ),
Expand All @@ -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 );
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit efbcdc5

Please sign in to comment.