| <?php |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller { |
|
|
| |
| |
| |
| |
| |
| public function __construct() { |
| $this->namespace = 'wp/v2'; |
| $this->rest_base = 'pattern-directory'; |
| } |
|
|
| |
| |
| |
| |
| |
| public function register_routes() { |
| register_rest_route( |
| $this->namespace, |
| '/' . $this->rest_base . '/patterns', |
| array( |
| array( |
| 'methods' => WP_REST_Server::READABLE, |
| 'callback' => array( $this, 'get_items' ), |
| 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 'args' => $this->get_collection_params(), |
| ), |
| 'schema' => array( $this, 'get_public_item_schema' ), |
| ) |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function get_items_permissions_check( $request ) { |
| if ( current_user_can( 'edit_posts' ) ) { |
| return true; |
| } |
|
|
| foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { |
| if ( current_user_can( $post_type->cap->edit_posts ) ) { |
| return true; |
| } |
| } |
|
|
| return new WP_Error( |
| 'rest_pattern_directory_cannot_view', |
| __( 'Sorry, you are not allowed to browse the local block pattern directory.' ), |
| array( 'status' => rest_authorization_required_code() ) |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function get_items( $request ) { |
| |
| |
| |
| |
| |
| require ABSPATH . WPINC . '/version.php'; |
|
|
| $valid_query_args = array( |
| 'offset' => true, |
| 'order' => true, |
| 'orderby' => true, |
| 'page' => true, |
| 'per_page' => true, |
| 'search' => true, |
| 'slug' => true, |
| ); |
| $query_args = array_intersect_key( $request->get_params(), $valid_query_args ); |
|
|
| $query_args['locale'] = get_user_locale(); |
| $query_args['wp-version'] = $wp_version; |
| $query_args['pattern-categories'] = isset( $request['category'] ) ? $request['category'] : false; |
| $query_args['pattern-keywords'] = isset( $request['keyword'] ) ? $request['keyword'] : false; |
|
|
| $query_args = array_filter( $query_args ); |
|
|
| $transient_key = $this->get_transient_key( $query_args ); |
|
|
| |
| |
| |
| |
| $raw_patterns = get_site_transient( $transient_key ); |
|
|
| if ( ! $raw_patterns ) { |
| $api_url = 'http://api.wordpress.org/patterns/1.0/?' . build_query( $query_args ); |
| if ( wp_http_supports( array( 'ssl' ) ) ) { |
| $api_url = set_url_scheme( $api_url, 'https' ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| $cache_ttl = 5; |
| $wporg_response = wp_remote_get( $api_url ); |
| $raw_patterns = json_decode( wp_remote_retrieve_body( $wporg_response ) ); |
|
|
| if ( is_wp_error( $wporg_response ) ) { |
| $raw_patterns = $wporg_response; |
|
|
| } elseif ( ! is_array( $raw_patterns ) ) { |
| |
| $raw_patterns = new WP_Error( |
| 'pattern_api_failed', |
| sprintf( |
| |
| __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ), |
| __( 'https://wordpress.org/support/forums/' ) |
| ), |
| array( |
| 'response' => wp_remote_retrieve_body( $wporg_response ), |
| ) |
| ); |
|
|
| } else { |
| |
| $cache_ttl = HOUR_IN_SECONDS; |
| } |
|
|
| set_site_transient( $transient_key, $raw_patterns, $cache_ttl ); |
| } |
|
|
| if ( is_wp_error( $raw_patterns ) ) { |
| $raw_patterns->add_data( array( 'status' => 500 ) ); |
|
|
| return $raw_patterns; |
| } |
|
|
| $response = array(); |
|
|
| if ( $raw_patterns ) { |
| foreach ( $raw_patterns as $pattern ) { |
| $response[] = $this->prepare_response_for_collection( |
| $this->prepare_item_for_response( $pattern, $request ) |
| ); |
| } |
| } |
|
|
| return new WP_REST_Response( $response ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function prepare_item_for_response( $item, $request ) { |
| |
| $raw_pattern = $item; |
|
|
| $prepared_pattern = array( |
| 'id' => absint( $raw_pattern->id ), |
| 'title' => sanitize_text_field( $raw_pattern->title->rendered ), |
| 'content' => wp_kses_post( $raw_pattern->pattern_content ), |
| 'categories' => array_map( 'sanitize_title', $raw_pattern->category_slugs ), |
| 'keywords' => array_map( 'sanitize_text_field', explode( ',', $raw_pattern->meta->wpop_keywords ) ), |
| 'description' => sanitize_text_field( $raw_pattern->meta->wpop_description ), |
| 'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ), |
| 'block_types' => array_map( 'sanitize_text_field', $raw_pattern->meta->wpop_block_types ), |
| ); |
|
|
| $prepared_pattern = $this->add_additional_fields_to_object( $prepared_pattern, $request ); |
|
|
| $response = new WP_REST_Response( $prepared_pattern ); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| return apply_filters( 'rest_prepare_block_pattern', $response, $raw_pattern, $request ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function get_item_schema() { |
| if ( $this->schema ) { |
| return $this->add_additional_fields_schema( $this->schema ); |
| } |
|
|
| $this->schema = array( |
| '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 'title' => 'pattern-directory-item', |
| 'type' => 'object', |
| 'properties' => array( |
| 'id' => array( |
| 'description' => __( 'The pattern ID.' ), |
| 'type' => 'integer', |
| 'minimum' => 1, |
| 'context' => array( 'view', 'edit', 'embed' ), |
| ), |
|
|
| 'title' => array( |
| 'description' => __( 'The pattern title, in human readable format.' ), |
| 'type' => 'string', |
| 'minLength' => 1, |
| 'context' => array( 'view', 'edit', 'embed' ), |
| ), |
|
|
| 'content' => array( |
| 'description' => __( 'The pattern content.' ), |
| 'type' => 'string', |
| 'minLength' => 1, |
| 'context' => array( 'view', 'edit', 'embed' ), |
| ), |
|
|
| 'categories' => array( |
| 'description' => __( "The pattern's category slugs." ), |
| 'type' => 'array', |
| 'uniqueItems' => true, |
| 'items' => array( 'type' => 'string' ), |
| 'context' => array( 'view', 'edit', 'embed' ), |
| ), |
|
|
| 'keywords' => array( |
| 'description' => __( "The pattern's keywords." ), |
| 'type' => 'array', |
| 'uniqueItems' => true, |
| 'items' => array( 'type' => 'string' ), |
| 'context' => array( 'view', 'edit', 'embed' ), |
| ), |
|
|
| 'description' => array( |
| 'description' => __( 'A description of the pattern.' ), |
| 'type' => 'string', |
| 'minLength' => 1, |
| 'context' => array( 'view', 'edit', 'embed' ), |
| ), |
|
|
| 'viewport_width' => array( |
| 'description' => __( 'The preferred width of the viewport when previewing a pattern, in pixels.' ), |
| 'type' => 'integer', |
| 'context' => array( 'view', 'edit', 'embed' ), |
| ), |
|
|
| 'block_types' => array( |
| 'description' => __( 'The block types which can use this pattern.' ), |
| 'type' => 'array', |
| 'uniqueItems' => true, |
| 'items' => array( 'type' => 'string' ), |
| 'context' => array( 'view', 'embed' ), |
| ), |
| ), |
| ); |
|
|
| return $this->add_additional_fields_schema( $this->schema ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function get_collection_params() { |
| $query_params = parent::get_collection_params(); |
|
|
| $query_params['per_page']['default'] = 100; |
| $query_params['search']['minLength'] = 1; |
| $query_params['context']['default'] = 'view'; |
|
|
| $query_params['category'] = array( |
| 'description' => __( 'Limit results to those matching a category ID.' ), |
| 'type' => 'integer', |
| 'minimum' => 1, |
| ); |
|
|
| $query_params['keyword'] = array( |
| 'description' => __( 'Limit results to those matching a keyword ID.' ), |
| 'type' => 'integer', |
| 'minimum' => 1, |
| ); |
|
|
| $query_params['slug'] = array( |
| 'description' => __( 'Limit results to those matching a pattern (slug).' ), |
| 'type' => 'array', |
| ); |
|
|
| $query_params['offset'] = array( |
| 'description' => __( 'Offset the result set by a specific number of items.' ), |
| 'type' => 'integer', |
| ); |
|
|
| $query_params['order'] = array( |
| 'description' => __( 'Order sort attribute ascending or descending.' ), |
| 'type' => 'string', |
| 'default' => 'desc', |
| 'enum' => array( 'asc', 'desc' ), |
| ); |
|
|
| $query_params['orderby'] = array( |
| 'description' => __( 'Sort collection by post attribute.' ), |
| 'type' => 'string', |
| 'default' => 'date', |
| 'enum' => array( |
| 'author', |
| 'date', |
| 'id', |
| 'include', |
| 'modified', |
| 'parent', |
| 'relevance', |
| 'slug', |
| 'include_slugs', |
| 'title', |
| 'favorite_count', |
| ), |
| ); |
|
|
| |
| |
| |
| |
| |
| |
| |
| return apply_filters( 'rest_pattern_directory_collection_params', $query_params ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected function get_transient_key( $query_args ) { |
|
|
| if ( isset( $query_args['slug'] ) ) { |
| |
| $query_args['slug'] = wp_parse_list( $query_args['slug'] ); |
|
|
| |
| if ( empty( $query_args['slug'] ) ) { |
| unset( $query_args['slug'] ); |
| } else { |
| |
| sort( $query_args['slug'] ); |
| } |
| } |
|
|
| return 'wp_remote_block_patterns_' . md5( serialize( $query_args ) ); |
| } |
| } |
|
|