-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix/55278 standardize font library rest api #55454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
da8196d
Refactored the consumption of the API to use singular font family data
pbking 7ce8011
Refactored unit tests to pass with code refactor.
pbking 1b11df3
Renamed test with .spec
pbking eae4e9e
Refactored test to use toThrow instead of a getError function
pbking bb8e518
Refactor Font Library API to transfer SINGULAR items
pbking 23c0745
update font collection tests
pbking 47579c7
update font collection tests
pbking d0a4757
Fixed font family tests
pbking 1e0b21c
Moved Font Collection tests to wpFontCollection folder
pbking d27f570
Added register font collection test
pbking 5f4c8b5
Adding Font Collections Controller Tests (WIP)
pbking 8215568
Refactored Font Library Controller tests to Font Family Controller Tests
pbking cfc98f1
Fixed failing font collection tests (when ran in a suite) due to repe…
pbking 71e3f40
Fixed failing font family deletion test
pbking 0a60636
fixed linting errors
pbking 329ee7d
Refactored font collections get_items to use rest_ensure_response
pbking 9af3ae0
Renamed permission callback per request
pbking 40db889
Refactored api parameters to be snake_case instead of camelCase
pbking ff6d56a
Renamed test classes to align with standards
pbking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
283 changes: 283 additions & 0 deletions
283
lib/experimental/fonts/font-library/class-wp-rest-font-collections-controller.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,283 @@ | ||
| <?php | ||
| /** | ||
| * Rest Font Collection Controller. | ||
| * | ||
| * This file contains the class for the REST API Font Collection Controller. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage Font Collection | ||
| * @since 6.4.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Font Collection Controller class. | ||
| * | ||
| * @since 6.4.0 | ||
| */ | ||
| class WP_REST_Font_Collections_Controller extends WP_REST_Controller { | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @since 6.4.0 | ||
| */ | ||
| public function __construct() { | ||
| $this->rest_base = 'font-collections'; | ||
| $this->namespace = 'wp/v2'; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the routes for the objects of the controller. | ||
| * | ||
| * @since 6.4.0 | ||
| */ | ||
| public function register_routes() { | ||
| register_rest_route( | ||
| $this->namespace, | ||
| '/' . $this->rest_base, | ||
| array( | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_items' ), | ||
| 'permission_callback' => array( $this, 'update_font_library_permissions_check' ), | ||
| ), | ||
| 'schema' => array( $this, 'get_items_schema' ), | ||
| ) | ||
| ); | ||
|
|
||
| register_rest_route( | ||
| $this->namespace, | ||
| '/' . $this->rest_base . '/(?P<id>[\/\w-]+)', | ||
| array( | ||
| 'args' => array( | ||
| 'id' => array( | ||
| 'description' => __( 'Unique identifier for the post.' ), | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| ), | ||
| ), | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_item' ), | ||
| 'permission_callback' => array( $this, 'update_font_library_permissions_check' ), | ||
| ), | ||
| 'schema' => array( $this, 'get_item_schema' ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Gets a font collection. | ||
| * | ||
| * @since 6.4.0 | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||
| */ | ||
| public function get_item( $request ) { | ||
| $id = $request['id']; | ||
| $collection = WP_Font_Library::get_font_collection( $id ); | ||
| // If the collection doesn't exist returns a 404. | ||
| if ( is_wp_error( $collection ) ) { | ||
| $collection->add_data( array( 'status' => 404 ) ); | ||
| return $collection; | ||
| } | ||
| $collection_with_data = $collection->get_data(); | ||
| // If there was an error getting the collection data, return the error. | ||
| if ( is_wp_error( $collection_with_data ) ) { | ||
| $collection_with_data->add_data( array( 'status' => 500 ) ); | ||
| return $collection_with_data; | ||
| } | ||
|
|
||
| return rest_ensure_response( $collection_with_data ); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the font collections available. | ||
| * | ||
| * @since 6.4.0 | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return WP_REST_Response Response object. | ||
| */ | ||
| public function get_items( $request ) { | ||
| $collections = array(); | ||
| foreach ( WP_Font_Library::get_font_collections() as $collection ) { | ||
| $collections[] = $collection->get_config(); | ||
| } | ||
| return rest_ensure_response( $collections ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the user has permissions to update the Font Library. | ||
| * | ||
| * @since 6.4.0 | ||
| * | ||
| * @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise. | ||
| */ | ||
| public function update_font_library_permissions_check() { | ||
| if ( ! current_user_can( 'edit_theme_options' ) ) { | ||
| return new WP_Error( | ||
| 'rest_cannot_update_font_library', | ||
pbking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| __( 'Sorry, you are not allowed to update the Font Library on this site.' ), | ||
pbking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| array( | ||
| 'status' => rest_authorization_required_code(), | ||
| ) | ||
| ); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the schema for the font collections item, conforming to JSON Schema. | ||
| * | ||
| * @since 6.4.0 | ||
| * | ||
| * @return array Item schema data. | ||
| */ | ||
| public function get_items_schema() { | ||
| if ( $this->schema ) { | ||
| return $this->add_additional_fields_schema( $this->schema ); | ||
| } | ||
|
|
||
| $schema = array( | ||
| '$schema' => 'http://json-schema.org/draft-04/schema#', | ||
| 'title' => 'font-collections', | ||
| 'type' => 'array', | ||
| 'items' => array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'id' => array( | ||
| 'description' => __( 'Unique identifier for the font collection.' ), | ||
| 'type' => 'string', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| 'readonly' => true, | ||
| ), | ||
| 'name' => array( | ||
| 'description' => __( 'Name of the font collection.' ), | ||
| 'type' => 'string', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
| 'description' => array( | ||
| 'description' => __( 'Description of the font collection.' ), | ||
| 'type' => 'string', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
| 'src' => array( | ||
| 'description' => __( 'Source to the list of font families.' ), | ||
| 'type' => 'string', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| $this->schema = $schema; | ||
| return $this->add_additional_fields_schema( $this->schema ); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the item's schema, conforming to JSON Schema. | ||
| * | ||
| * @since 6.4.0 | ||
| * | ||
| * @return array Item schema data. | ||
| */ | ||
| public function get_item_schema() { | ||
| if ( $this->schema ) { | ||
| return $this->add_additional_fields_schema( $this->schema ); | ||
| } | ||
|
|
||
| $schema = array( | ||
| '$schema' => 'http://json-schema.org/draft-04/schema#', | ||
| 'title' => 'font-collection', | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'id' => array( | ||
| 'description' => __( 'Unique identifier for the font collection.' ), | ||
| 'type' => 'string', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| 'readonly' => true, | ||
| ), | ||
| 'name' => array( | ||
| 'description' => __( 'Name of the font collection.' ), | ||
| 'type' => 'string', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
| 'description' => array( | ||
| 'description' => __( 'Description of the font collection.' ), | ||
| 'type' => 'string', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
| 'data' => array( | ||
| 'description' => __( 'Data of the font collection.' ), | ||
| 'type' => 'object', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| 'properties' => array( | ||
| 'font_families' => array( | ||
| 'description' => __( 'List of font families.' ), | ||
| 'type' => 'array', | ||
| 'items' => array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'name' => array( | ||
| 'description' => __( 'Name of the font family.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| 'font_family' => array( | ||
| 'description' => __( 'Font family string.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| 'slug' => array( | ||
| 'description' => __( 'Slug of the font family.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| 'category' => array( | ||
| 'description' => __( 'Category of the font family.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| 'font_face' => array( | ||
| 'description' => __( 'Font face details.' ), | ||
| 'type' => 'array', | ||
| 'items' => array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'download_from_url' => array( | ||
| 'description' => __( 'URL to download the font.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| 'font_weight' => array( | ||
| 'description' => __( 'Font weight.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| 'fonts_style' => array( | ||
| 'description' => __( 'Font style.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| 'font_family' => array( | ||
| 'description' => __( 'Font family string.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| 'preview' => array( | ||
| 'description' => __( 'URL for font preview.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| 'preview' => array( | ||
| 'description' => __( 'URL for font family preview.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| $this->schema = $schema; | ||
| return $this->add_additional_fields_schema( $this->schema ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.