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