[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-font-collections-controller.php (source)

   1  <?php
   2  /**
   3   * Rest Font Collections Controller.
   4   *
   5   * This file contains the class for the REST API Font Collections Controller.
   6   *
   7   * @package    WordPress
   8   * @subpackage REST_API
   9   * @since      6.5.0
  10   */
  11  
  12  /**
  13   * Font Library Controller class.
  14   *
  15   * @since 6.5.0
  16   */
  17  class WP_REST_Font_Collections_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 6.5.0
  23       */
  24  	public function __construct() {
  25          $this->rest_base = 'font-collections';
  26          $this->namespace = 'wp/v2';
  27      }
  28  
  29      /**
  30       * Registers the routes for the objects of the controller.
  31       *
  32       * @since 6.5.0
  33       */
  34  	public function register_routes() {
  35          register_rest_route(
  36              $this->namespace,
  37              '/' . $this->rest_base,
  38              array(
  39                  array(
  40                      'methods'             => WP_REST_Server::READABLE,
  41                      'callback'            => array( $this, 'get_items' ),
  42                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  43                      'args'                => $this->get_collection_params(),
  44  
  45                  ),
  46                  'schema' => array( $this, 'get_public_item_schema' ),
  47              )
  48          );
  49  
  50          register_rest_route(
  51              $this->namespace,
  52              '/' . $this->rest_base . '/(?P<slug>[\/\w-]+)',
  53              array(
  54                  array(
  55                      'methods'             => WP_REST_Server::READABLE,
  56                      'callback'            => array( $this, 'get_item' ),
  57                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  58                      'args'                => array(
  59                          'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  60                      ),
  61                  ),
  62                  'schema' => array( $this, 'get_public_item_schema' ),
  63              )
  64          );
  65      }
  66  
  67      /**
  68       * Gets the font collections available.
  69       *
  70       * @since 6.5.0
  71       *
  72       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  73       */
  74  	public function get_items( $request ) {
  75          $collections_all = WP_Font_Library::get_instance()->get_font_collections();
  76  
  77          $page        = $request['page'];
  78          $per_page    = $request['per_page'];
  79          $total_items = count( $collections_all );
  80          $max_pages   = (int) ceil( $total_items / $per_page );
  81  
  82          if ( $page > $max_pages && $total_items > 0 ) {
  83              return new WP_Error(
  84                  'rest_post_invalid_page_number',
  85                  __( 'The page number requested is larger than the number of pages available.' ),
  86                  array( 'status' => 400 )
  87              );
  88          }
  89  
  90          $collections_page = array_slice( $collections_all, ( $page - 1 ) * $per_page, $per_page );
  91  
  92          $items = array();
  93          foreach ( $collections_page as $collection ) {
  94              $item = $this->prepare_item_for_response( $collection, $request );
  95  
  96              // If there's an error loading a collection, skip it and continue loading valid collections.
  97              if ( is_wp_error( $item ) ) {
  98                  continue;
  99              }
 100              $item    = $this->prepare_response_for_collection( $item );
 101              $items[] = $item;
 102          }
 103  
 104          $response = rest_ensure_response( $items );
 105  
 106          $response->header( 'X-WP-Total', (int) $total_items );
 107          $response->header( 'X-WP-TotalPages', $max_pages );
 108  
 109          $request_params = $request->get_query_params();
 110          $collection_url = rest_url( $this->namespace . '/' . $this->rest_base );
 111          $base           = add_query_arg( urlencode_deep( $request_params ), $collection_url );
 112  
 113          if ( $page > 1 ) {
 114              $prev_page = $page - 1;
 115  
 116              if ( $prev_page > $max_pages ) {
 117                  $prev_page = $max_pages;
 118              }
 119  
 120              $prev_link = add_query_arg( 'page', $prev_page, $base );
 121              $response->link_header( 'prev', $prev_link );
 122          }
 123          if ( $max_pages > $page ) {
 124              $next_page = $page + 1;
 125              $next_link = add_query_arg( 'page', $next_page, $base );
 126  
 127              $response->link_header( 'next', $next_link );
 128          }
 129  
 130          return $response;
 131      }
 132  
 133      /**
 134       * Gets a font collection.
 135       *
 136       * @since 6.5.0
 137       *
 138       * @param WP_REST_Request $request Full details about the request.
 139       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 140       */
 141  	public function get_item( $request ) {
 142          $slug       = $request->get_param( 'slug' );
 143          $collection = WP_Font_Library::get_instance()->get_font_collection( $slug );
 144  
 145          if ( ! $collection ) {
 146              return new WP_Error( 'rest_font_collection_not_found', __( 'Font collection not found.' ), array( 'status' => 404 ) );
 147          }
 148  
 149          return $this->prepare_item_for_response( $collection, $request );
 150      }
 151  
 152      /**
 153      * Prepare a single collection output for response.
 154      *
 155      * @since 6.5.0
 156      *
 157      * @param WP_Font_Collection $item    Font collection object.
 158      * @param WP_REST_Request    $request Request object.
 159      * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 160      */
 161  	public function prepare_item_for_response( $item, $request ) {
 162          $fields = $this->get_fields_for_response( $request );
 163          $data   = array();
 164  
 165          if ( rest_is_field_included( 'slug', $fields ) ) {
 166              $data['slug'] = $item->slug;
 167          }
 168  
 169          // If any data fields are requested, get the collection data.
 170          $data_fields = array( 'name', 'description', 'font_families', 'categories' );
 171          if ( ! empty( array_intersect( $fields, $data_fields ) ) ) {
 172              $collection_data = $item->get_data();
 173              if ( is_wp_error( $collection_data ) ) {
 174                  $collection_data->add_data( array( 'status' => 500 ) );
 175                  return $collection_data;
 176              }
 177  
 178              foreach ( $data_fields as $field ) {
 179                  if ( rest_is_field_included( $field, $fields ) ) {
 180                      $data[ $field ] = $collection_data[ $field ];
 181                  }
 182              }
 183          }
 184  
 185          $response = rest_ensure_response( $data );
 186  
 187          if ( rest_is_field_included( '_links', $fields ) ) {
 188              $links = $this->prepare_links( $item );
 189              $response->add_links( $links );
 190          }
 191  
 192          $context        = ! empty( $request['context'] ) ? $request['context'] : 'view';
 193          $response->data = $this->add_additional_fields_to_object( $response->data, $request );
 194          $response->data = $this->filter_response_by_context( $response->data, $context );
 195  
 196          /**
 197           * Filters the font collection data for a REST API response.
 198           *
 199           * @since 6.5.0
 200           *
 201           * @param WP_REST_Response   $response The response object.
 202           * @param WP_Font_Collection $item     The font collection object.
 203           * @param WP_REST_Request    $request  Request used to generate the response.
 204           */
 205          return apply_filters( 'rest_prepare_font_collection', $response, $item, $request );
 206      }
 207  
 208      /**
 209       * Retrieves the font collection's schema, conforming to JSON Schema.
 210       *
 211       * @since 6.5.0
 212       *
 213       * @return array Item schema data.
 214       */
 215  	public function get_item_schema() {
 216          if ( $this->schema ) {
 217              return $this->add_additional_fields_schema( $this->schema );
 218          }
 219  
 220          $schema = array(
 221              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 222              'title'      => 'font-collection',
 223              'type'       => 'object',
 224              'properties' => array(
 225                  'slug'          => array(
 226                      'description' => __( 'Unique identifier for the font collection.' ),
 227                      'type'        => 'string',
 228                      'context'     => array( 'view', 'edit', 'embed' ),
 229                      'readonly'    => true,
 230                  ),
 231                  'name'          => array(
 232                      'description' => __( 'The name for the font collection.' ),
 233                      'type'        => 'string',
 234                      'context'     => array( 'view', 'edit', 'embed' ),
 235                  ),
 236                  'description'   => array(
 237                      'description' => __( 'The description for the font collection.' ),
 238                      'type'        => 'string',
 239                      'context'     => array( 'view', 'edit', 'embed' ),
 240                  ),
 241                  'font_families' => array(
 242                      'description' => __( 'The font families for the font collection.' ),
 243                      'type'        => 'array',
 244                      'context'     => array( 'view', 'edit', 'embed' ),
 245                  ),
 246                  'categories'    => array(
 247                      'description' => __( 'The categories for the font collection.' ),
 248                      'type'        => 'array',
 249                      'context'     => array( 'view', 'edit', 'embed' ),
 250                  ),
 251              ),
 252          );
 253  
 254          $this->schema = $schema;
 255  
 256          return $this->add_additional_fields_schema( $this->schema );
 257      }
 258  
 259      /**
 260       * Prepares links for the request.
 261       *
 262       * @since 6.5.0
 263       *
 264       * @param WP_Font_Collection $collection Font collection data
 265       * @return array Links for the given font collection.
 266       */
 267  	protected function prepare_links( $collection ) {
 268          return array(
 269              'self'       => array(
 270                  'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $collection->slug ) ),
 271              ),
 272              'collection' => array(
 273                  'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
 274              ),
 275          );
 276      }
 277  
 278      /**
 279       * Retrieves the search params for the font collections.
 280       *
 281       * @since 6.5.0
 282       *
 283       * @return array Collection parameters.
 284       */
 285  	public function get_collection_params() {
 286          $query_params = parent::get_collection_params();
 287  
 288          $query_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
 289  
 290          unset( $query_params['search'] );
 291  
 292          /**
 293           * Filters REST API collection parameters for the font collections controller.
 294           *
 295           * @since 6.5.0
 296           *
 297           * @param array $query_params JSON Schema-formatted collection parameters.
 298           */
 299          return apply_filters( 'rest_font_collections_collection_params', $query_params );
 300      }
 301  
 302      /**
 303       * Checks whether the user has permissions to use the Fonts Collections.
 304       *
 305       * @since 6.5.0
 306       *
 307       * @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise.
 308       */
 309  	public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
 310          if ( current_user_can( 'edit_theme_options' ) ) {
 311              return true;
 312          }
 313  
 314          return new WP_Error(
 315              'rest_cannot_read',
 316              __( 'Sorry, you are not allowed to access font collections.' ),
 317              array(
 318                  'status' => rest_authorization_required_code(),
 319              )
 320          );
 321      }
 322  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref