[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 $is_head_request = $request->is_method( 'HEAD' ); 93 94 $items = array(); 95 foreach ( $collections_page as $collection ) { 96 $item = $this->prepare_item_for_response( $collection, $request ); 97 98 // If there's an error loading a collection, skip it and continue loading valid collections. 99 if ( is_wp_error( $item ) ) { 100 continue; 101 } 102 103 /* 104 * Skip preparing the response body for HEAD requests. 105 * Cannot exit earlier due to backward compatibility reasons, 106 * as validation occurs in the prepare_item_for_response method. 107 */ 108 if ( $is_head_request ) { 109 continue; 110 } 111 112 $item = $this->prepare_response_for_collection( $item ); 113 $items[] = $item; 114 } 115 116 $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $items ); 117 118 $response->header( 'X-WP-Total', (int) $total_items ); 119 $response->header( 'X-WP-TotalPages', $max_pages ); 120 121 $request_params = $request->get_query_params(); 122 $collection_url = rest_url( $this->namespace . '/' . $this->rest_base ); 123 $base = add_query_arg( urlencode_deep( $request_params ), $collection_url ); 124 125 if ( $page > 1 ) { 126 $prev_page = $page - 1; 127 128 if ( $prev_page > $max_pages ) { 129 $prev_page = $max_pages; 130 } 131 132 $prev_link = add_query_arg( 'page', $prev_page, $base ); 133 $response->link_header( 'prev', $prev_link ); 134 } 135 if ( $max_pages > $page ) { 136 $next_page = $page + 1; 137 $next_link = add_query_arg( 'page', $next_page, $base ); 138 139 $response->link_header( 'next', $next_link ); 140 } 141 142 return $response; 143 } 144 145 /** 146 * Gets a font collection. 147 * 148 * @since 6.5.0 149 * 150 * @param WP_REST_Request $request Full details about the request. 151 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 152 */ 153 public function get_item( $request ) { 154 $slug = $request->get_param( 'slug' ); 155 $collection = WP_Font_Library::get_instance()->get_font_collection( $slug ); 156 157 if ( ! $collection ) { 158 return new WP_Error( 'rest_font_collection_not_found', __( 'Font collection not found.' ), array( 'status' => 404 ) ); 159 } 160 161 return $this->prepare_item_for_response( $collection, $request ); 162 } 163 164 /** 165 * Prepare a single collection output for response. 166 * 167 * @since 6.5.0 168 * 169 * @param WP_Font_Collection $item Font collection object. 170 * @param WP_REST_Request $request Request object. 171 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 172 */ 173 public function prepare_item_for_response( $item, $request ) { 174 $fields = $this->get_fields_for_response( $request ); 175 $data = array(); 176 177 if ( rest_is_field_included( 'slug', $fields ) ) { 178 $data['slug'] = $item->slug; 179 } 180 181 // If any data fields are requested, get the collection data. 182 $data_fields = array( 'name', 'description', 'font_families', 'categories' ); 183 if ( ! empty( array_intersect( $fields, $data_fields ) ) ) { 184 $collection_data = $item->get_data(); 185 if ( is_wp_error( $collection_data ) ) { 186 $collection_data->add_data( array( 'status' => 500 ) ); 187 return $collection_data; 188 } 189 190 /** 191 * Don't prepare the response body for HEAD requests. 192 * Can't exit at the beginning of the method due to the potential need to return a WP_Error object. 193 */ 194 if ( $request->is_method( 'HEAD' ) ) { 195 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */ 196 return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request ); 197 } 198 199 foreach ( $data_fields as $field ) { 200 if ( rest_is_field_included( $field, $fields ) ) { 201 $data[ $field ] = $collection_data[ $field ]; 202 } 203 } 204 } 205 206 /** 207 * Don't prepare the response body for HEAD requests. 208 * Can't exit at the beginning of the method due to the potential need to return a WP_Error object. 209 */ 210 if ( $request->is_method( 'HEAD' ) ) { 211 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */ 212 return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request ); 213 } 214 215 $response = rest_ensure_response( $data ); 216 217 if ( rest_is_field_included( '_links', $fields ) ) { 218 $links = $this->prepare_links( $item ); 219 $response->add_links( $links ); 220 } 221 222 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 223 $response->data = $this->add_additional_fields_to_object( $response->data, $request ); 224 $response->data = $this->filter_response_by_context( $response->data, $context ); 225 226 /** 227 * Filters the font collection data for a REST API response. 228 * 229 * @since 6.5.0 230 * 231 * @param WP_REST_Response $response The response object. 232 * @param WP_Font_Collection $item The font collection object. 233 * @param WP_REST_Request $request Request used to generate the response. 234 */ 235 return apply_filters( 'rest_prepare_font_collection', $response, $item, $request ); 236 } 237 238 /** 239 * Retrieves the font collection's schema, conforming to JSON Schema. 240 * 241 * @since 6.5.0 242 * 243 * @return array Item schema data. 244 */ 245 public function get_item_schema() { 246 if ( $this->schema ) { 247 return $this->add_additional_fields_schema( $this->schema ); 248 } 249 250 $schema = array( 251 '$schema' => 'http://json-schema.org/draft-04/schema#', 252 'title' => 'font-collection', 253 'type' => 'object', 254 'properties' => array( 255 'slug' => array( 256 'description' => __( 'Unique identifier for the font collection.' ), 257 'type' => 'string', 258 'context' => array( 'view', 'edit', 'embed' ), 259 'readonly' => true, 260 ), 261 'name' => array( 262 'description' => __( 'The name for the font collection.' ), 263 'type' => 'string', 264 'context' => array( 'view', 'edit', 'embed' ), 265 ), 266 'description' => array( 267 'description' => __( 'The description for the font collection.' ), 268 'type' => 'string', 269 'context' => array( 'view', 'edit', 'embed' ), 270 ), 271 'font_families' => array( 272 'description' => __( 'The font families for the font collection.' ), 273 'type' => 'array', 274 'context' => array( 'view', 'edit', 'embed' ), 275 ), 276 'categories' => array( 277 'description' => __( 'The categories for the font collection.' ), 278 'type' => 'array', 279 'context' => array( 'view', 'edit', 'embed' ), 280 ), 281 ), 282 ); 283 284 $this->schema = $schema; 285 286 return $this->add_additional_fields_schema( $this->schema ); 287 } 288 289 /** 290 * Prepares links for the request. 291 * 292 * @since 6.5.0 293 * 294 * @param WP_Font_Collection $collection Font collection data 295 * @return array Links for the given font collection. 296 */ 297 protected function prepare_links( $collection ) { 298 return array( 299 'self' => array( 300 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $collection->slug ) ), 301 ), 302 'collection' => array( 303 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), 304 ), 305 ); 306 } 307 308 /** 309 * Retrieves the search params for the font collections. 310 * 311 * @since 6.5.0 312 * 313 * @return array Collection parameters. 314 */ 315 public function get_collection_params() { 316 $query_params = parent::get_collection_params(); 317 318 $query_params['context'] = $this->get_context_param( array( 'default' => 'view' ) ); 319 320 unset( $query_params['search'] ); 321 322 /** 323 * Filters REST API collection parameters for the font collections controller. 324 * 325 * @since 6.5.0 326 * 327 * @param array $query_params JSON Schema-formatted collection parameters. 328 */ 329 return apply_filters( 'rest_font_collections_collection_params', $query_params ); 330 } 331 332 /** 333 * Checks whether the user has permissions to use the Fonts Collections. 334 * 335 * @since 6.5.0 336 * 337 * @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise. 338 */ 339 public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable 340 if ( current_user_can( 'edit_theme_options' ) ) { 341 return true; 342 } 343 344 return new WP_Error( 345 'rest_cannot_read', 346 __( 'Sorry, you are not allowed to access font collections.' ), 347 array( 348 'status' => rest_authorization_required_code(), 349 ) 350 ); 351 } 352 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Apr 3 08:20:01 2025 | Cross-referenced by PHPXref |