[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-pattern-directory-controller.php (source)

   1  <?php
   2  /**
   3   * Block Pattern Directory REST API: WP_REST_Pattern_Directory_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 5.8.0
   8   */
   9  
  10  /**
  11   * Controller which provides REST endpoint for block patterns.
  12   *
  13   * This simply proxies the endpoint at http://api.wordpress.org/patterns/1.0/. That isn't necessary for
  14   * functionality, but is desired for privacy. It prevents api.wordpress.org from knowing the user's IP address.
  15   *
  16   * @since 5.8.0
  17   *
  18   * @see WP_REST_Controller
  19   */
  20  class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller {
  21  
  22      /**
  23       * Constructs the controller.
  24       *
  25       * @since 5.8.0
  26       */
  27  	public function __construct() {
  28          $this->namespace     = 'wp/v2';
  29              $this->rest_base = 'pattern-directory';
  30      }
  31  
  32      /**
  33       * Registers the necessary REST API routes.
  34       *
  35       * @since 5.8.0
  36       */
  37  	public function register_routes() {
  38          register_rest_route(
  39              $this->namespace,
  40              '/' . $this->rest_base . '/patterns',
  41              array(
  42                  array(
  43                      'methods'             => WP_REST_Server::READABLE,
  44                      'callback'            => array( $this, 'get_items' ),
  45                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  46                      'args'                => $this->get_collection_params(),
  47                  ),
  48                  'schema' => array( $this, 'get_public_item_schema' ),
  49              )
  50          );
  51      }
  52  
  53      /**
  54       * Checks whether a given request has permission to view the local pattern directory.
  55       *
  56       * @since 5.8.0
  57       *
  58       * @param WP_REST_Request $request Full details about the request.
  59       * @return true|WP_Error True if the request has permission, WP_Error object otherwise.
  60       */
  61  	public function get_items_permissions_check( $request ) {
  62          if ( current_user_can( 'edit_posts' ) ) {
  63              return true;
  64          }
  65  
  66          foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  67              if ( current_user_can( $post_type->cap->edit_posts ) ) {
  68                  return true;
  69              }
  70          }
  71  
  72          return new WP_Error(
  73              'rest_pattern_directory_cannot_view',
  74              __( 'Sorry, you are not allowed to browse the local block pattern directory.' ),
  75              array( 'status' => rest_authorization_required_code() )
  76          );
  77      }
  78  
  79      /**
  80       * Search and retrieve block patterns metadata
  81       *
  82       * @since 5.8.0
  83       *
  84       * @param WP_REST_Request $request Full details about the request.
  85       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  86       */
  87  	public function get_items( $request ) {
  88          /*
  89           * Include an unmodified `$wp_version`, so the API can craft a response that's tailored to
  90           * it. Some plugins modify the version in a misguided attempt to improve security by
  91           * obscuring the version, which can cause invalid requests.
  92           */
  93          require  ABSPATH . WPINC . '/version.php';
  94  
  95          $query_args = array(
  96              'locale'     => get_user_locale(),
  97              'wp-version' => $wp_version,
  98          );
  99  
 100          $category_id = $request['category'];
 101          $keyword_id  = $request['keyword'];
 102          $search_term = $request['search'];
 103  
 104          if ( $category_id ) {
 105              $query_args['pattern-categories'] = $category_id;
 106          }
 107  
 108          if ( $keyword_id ) {
 109              $query_args['pattern-keywords'] = $keyword_id;
 110          }
 111  
 112          if ( $search_term ) {
 113              $query_args['search'] = $search_term;
 114          }
 115  
 116          /*
 117           * Include a hash of the query args, so that different requests are stored in
 118           * separate caches.
 119           *
 120           * MD5 is chosen for its speed, low-collision rate, universal availability, and to stay
 121           * under the character limit for `_site_transient_timeout_{...}` keys.
 122           *
 123           * @link https://stackoverflow.com/questions/3665247/fastest-hash-for-non-cryptographic-uses
 124           */
 125          $transient_key = 'wp_remote_block_patterns_' . md5( implode( '-', $query_args ) );
 126  
 127          /*
 128           * Use network-wide transient to improve performance. The locale is the only site
 129           * configuration that affects the response, and it's included in the transient key.
 130           */
 131          $raw_patterns = get_site_transient( $transient_key );
 132  
 133          if ( ! $raw_patterns ) {
 134              $api_url = add_query_arg(
 135                  array_map( 'rawurlencode', $query_args ),
 136                  'http://api.wordpress.org/patterns/1.0/'
 137              );
 138  
 139              if ( wp_http_supports( array( 'ssl' ) ) ) {
 140                  $api_url = set_url_scheme( $api_url, 'https' );
 141              }
 142  
 143              /*
 144               * Default to a short TTL, to mitigate cache stampedes on high-traffic sites.
 145               * This assumes that most errors will be short-lived, e.g., packet loss that causes the
 146               * first request to fail, but a follow-up one will succeed. The value should be high
 147               * enough to avoid stampedes, but low enough to not interfere with users manually
 148               * re-trying a failed request.
 149               */
 150              $cache_ttl      = 5;
 151              $wporg_response = wp_remote_get( $api_url );
 152              $raw_patterns   = json_decode( wp_remote_retrieve_body( $wporg_response ) );
 153  
 154              if ( is_wp_error( $wporg_response ) ) {
 155                  $raw_patterns = $wporg_response;
 156  
 157              } elseif ( ! is_array( $raw_patterns ) ) {
 158                  // HTTP request succeeded, but response data is invalid.
 159                  $raw_patterns = new WP_Error(
 160                      'pattern_api_failed',
 161                      sprintf(
 162                      /* translators: %s: Support forums URL. */
 163                          __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 164                          __( 'https://wordpress.org/support/forums/' )
 165                      ),
 166                      array(
 167                          'response' => wp_remote_retrieve_body( $wporg_response ),
 168                      )
 169                  );
 170  
 171              } else {
 172                  // Response has valid data.
 173                  $cache_ttl = HOUR_IN_SECONDS;
 174              }
 175  
 176              set_site_transient( $transient_key, $raw_patterns, $cache_ttl );
 177          }
 178  
 179          if ( is_wp_error( $raw_patterns ) ) {
 180              $raw_patterns->add_data( array( 'status' => 500 ) );
 181  
 182              return $raw_patterns;
 183          }
 184  
 185          $response = array();
 186  
 187          if ( $raw_patterns ) {
 188              foreach ( $raw_patterns as $pattern ) {
 189                  $response[] = $this->prepare_response_for_collection(
 190                      $this->prepare_item_for_response( $pattern, $request )
 191                  );
 192              }
 193          }
 194  
 195          return new WP_REST_Response( $response );
 196      }
 197  
 198      /**
 199       * Prepare a raw pattern before it's output in an API response.
 200       *
 201       * @since 5.8.0
 202       * @since 5.9.0 Renamed `$raw_pattern` to `$item` to match parent class for PHP 8 named parameter support.
 203       *
 204       * @param object          $item    Raw pattern from api.wordpress.org, before any changes.
 205       * @param WP_REST_Request $request Request object.
 206       * @return WP_REST_Response
 207       */
 208  	public function prepare_item_for_response( $item, $request ) {
 209          // Restores the more descriptive, specific name for use within this method.
 210          $raw_pattern      = $item;
 211          $prepared_pattern = array(
 212              'id'             => absint( $raw_pattern->id ),
 213              'title'          => sanitize_text_field( $raw_pattern->title->rendered ),
 214              'content'        => wp_kses_post( $raw_pattern->pattern_content ),
 215              'categories'     => array_map( 'sanitize_title', $raw_pattern->category_slugs ),
 216              'keywords'       => array_map( 'sanitize_title', $raw_pattern->keyword_slugs ),
 217              'description'    => sanitize_text_field( $raw_pattern->meta->wpop_description ),
 218              'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ),
 219          );
 220  
 221          $prepared_pattern = $this->add_additional_fields_to_object( $prepared_pattern, $request );
 222  
 223          $response = new WP_REST_Response( $prepared_pattern );
 224  
 225          /**
 226           * Filters the REST API response for a pattern.
 227           *
 228           * @since 5.8.0
 229           *
 230           * @param WP_REST_Response $response    The response object.
 231           * @param object           $raw_pattern The unprepared pattern.
 232           * @param WP_REST_Request  $request     The request object.
 233           */
 234          return apply_filters( 'rest_prepare_block_pattern', $response, $raw_pattern, $request );
 235      }
 236  
 237      /**
 238       * Retrieves the pattern's schema, conforming to JSON Schema.
 239       *
 240       * @since 5.8.0
 241       *
 242       * @return array Item schema data.
 243       */
 244  	public function get_item_schema() {
 245          if ( $this->schema ) {
 246              return $this->add_additional_fields_schema( $this->schema );
 247          }
 248  
 249          $this->schema = array(
 250              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 251              'title'      => 'pattern-directory-item',
 252              'type'       => 'object',
 253              'properties' => array(
 254                  'id'             => array(
 255                      'description' => __( 'The pattern ID.' ),
 256                      'type'        => 'integer',
 257                      'minimum'     => 1,
 258                      'context'     => array( 'view', 'embed' ),
 259                  ),
 260  
 261                  'title'          => array(
 262                      'description' => __( 'The pattern title, in human readable format.' ),
 263                      'type'        => 'string',
 264                      'minLength'   => 1,
 265                      'context'     => array( 'view', 'embed' ),
 266                  ),
 267  
 268                  'content'        => array(
 269                      'description' => __( 'The pattern content.' ),
 270                      'type'        => 'string',
 271                      'minLength'   => 1,
 272                      'context'     => array( 'view', 'embed' ),
 273                  ),
 274  
 275                  'categories'     => array(
 276                      'description' => __( "The pattern's category slugs." ),
 277                      'type'        => 'array',
 278                      'uniqueItems' => true,
 279                      'items'       => array( 'type' => 'string' ),
 280                      'context'     => array( 'view', 'embed' ),
 281                  ),
 282  
 283                  'keywords'       => array(
 284                      'description' => __( "The pattern's keyword slugs." ),
 285                      'type'        => 'array',
 286                      'uniqueItems' => true,
 287                      'items'       => array( 'type' => 'string' ),
 288                      'context'     => array( 'view', 'embed' ),
 289                  ),
 290  
 291                  'description'    => array(
 292                      'description' => __( 'A description of the pattern.' ),
 293                      'type'        => 'string',
 294                      'minLength'   => 1,
 295                      'context'     => array( 'view', 'embed' ),
 296                  ),
 297  
 298                  'viewport_width' => array(
 299                      'description' => __( 'The preferred width of the viewport when previewing a pattern, in pixels.' ),
 300                      'type'        => 'integer',
 301                      'context'     => array( 'view', 'embed' ),
 302                  ),
 303              ),
 304          );
 305  
 306          return $this->add_additional_fields_schema( $this->schema );
 307      }
 308  
 309      /**
 310       * Retrieves the search params for the patterns collection.
 311       *
 312       * @since 5.8.0
 313       *
 314       * @return array Collection parameters.
 315       */
 316  	public function get_collection_params() {
 317          $query_params = parent::get_collection_params();
 318  
 319          // Pagination is not supported.
 320          unset( $query_params['page'] );
 321          unset( $query_params['per_page'] );
 322  
 323          $query_params['search']['minLength'] = 1;
 324          $query_params['context']['default']  = 'view';
 325  
 326          $query_params['category'] = array(
 327              'description' => __( 'Limit results to those matching a category ID.' ),
 328              'type'        => 'integer',
 329              'minimum'     => 1,
 330          );
 331  
 332          $query_params['keyword'] = array(
 333              'description' => __( 'Limit results to those matching a keyword ID.' ),
 334              'type'        => 'integer',
 335              'minimum'     => 1,
 336          );
 337  
 338          /**
 339           * Filter collection parameters for the pattern directory controller.
 340           *
 341           * @since 5.8.0
 342           *
 343           * @param array $query_params JSON Schema-formatted collection parameters.
 344           */
 345          return apply_filters( 'rest_pattern_directory_collection_params', $query_params );
 346      }
 347  }


Generated: Mon Sep 20 01:00:04 2021 Cross-referenced by PHPXref 0.7.1