Skip to content

Commit

Permalink
Font Library: load collection JSON data from a URL in the collection …
Browse files Browse the repository at this point in the history
…config (#54067)

* Adding Font Collection class

* php formatting and linting

* Adding tests for collections routes

* Adding tests for WP_Font_Collection constructor

* adding tests for WP_Font_Collection get_data()

* adding tests for WP_Font_Library register_font_collection()

* get font collection tests

* adding 'wp_' prefix to the 'register_font_collection' filter name

* fix callback name

* making class property private

* registering filter from font-library.php file

* removing superfluous comment

* moving files to according changes in trunk

* config without a json file should fail

* fix property name in tests

* name fix

Co-authored-by: Tonya Mork <[email protected]>

* comment update

Co-authored-by: Tonya Mork <[email protected]>

* Adds WP_Error to return type

* Improves contructor error handling.

* Ensures each required param is of the right data type.
* Improves each param check error to include expected data type.

* FontCollection::get_data(): Improves error handling

* Rechecking if "data_json_file" exists in the $config property shouldn't be necessary,
  as it's checked in the constructor.

* If the file does not exist, bail out immediately as there's nothing more to do.

* Adds a check and WP_Error for file_get_contents():
file_get_contents() returns false on failure.
If there's nothing in the file, an empty string is returned.
This change checks for both of these conditions and returns a WP_Error if either happens.

* Internationalizes WP_Error error message for consistency in Core.

* Removes empty space for wpcs

* adding filter in a simpler way

Co-authored-by: Tonya Mork <[email protected]>

* micro-optimization

Co-authored-by: Tonya Mork <[email protected]>

* reuse WP_Error response instead of creating a new one

Co-authored-by: Tonya Mork <[email protected]>

* Eliminates try/catch

* Revert "Eliminates try/catch" commit

This reverts commit 0e6c026.

* Remove wp_register_font_collection and replace it by a global function, remove try catch and raise the error if needed

* adding function comment and guard agains re-declaration

* php format

* fixing docblock coments

* removing param comment

* re-adding parameter comment removed by mistake

* format php

* updating WP_Font_Collection __construct tests

* updating WP_Font_Collection get_data tests

* updating tests

* php format

* array check

Co-authored-by: Tonya Mork <[email protected]>

* Documents config array structure

* adding tests for /fonts/collections endpoint

* adding /fonts/collections/<id> endpoint test

* format

* format

* add test for missing collection

* removing not needed variables

* Add more tests and split the test for WP_REST_Font_Library_Controller

* lint

* try to create dir for tests in CI

* renane config property to src and handle URLs apart from file paths

* Add the ability to load collection JSON data from a URL

* remove unwanted comment

* fix missing merge

* fix merge with trunk

* removing test file after merge with trunk

* removing duplicated test files after merge with trunk

* decode data to make availanle in the client as json

* update google fonts collection source

* format php'

* removing the default font collection (google fonts) json file from repo

* fixing logic to get data from url

* add tests to try the font collection fetching from url

* update test

* format comments

* replacing existing url in test with a mock url

* early return

Co-authored-by: Anton Vlasenko <[email protected]>

* remove not needed filter

Co-authored-by: Anton Vlasenko <[email protected]>

* rewording error message

Co-authored-by: Anton Vlasenko <[email protected]>

---------

Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Anton Vlasenko <[email protected]>
  • Loading branch information
3 people committed Sep 22, 2023
1 parent 0c0d013 commit 8a82a37
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 55,785 deletions.
35 changes: 26 additions & 9 deletions lib/experimental/fonts/font-library/class-wp-font-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function __construct( $config ) {
throw new Exception( 'Font Collection config name is required as a non-empty string.' );
}

if ( empty( $config['data_json_file'] ) || ! is_string( $config['data_json_file'] ) ) {
throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' );
if ( empty( $config['src'] ) || ! is_string( $config['src'] ) ) {
throw new Exception( 'Font Collection config "src" option is required as a non-empty string.' );
}

$this->config = $config;
Expand All @@ -78,18 +78,35 @@ public function get_config() {
* else an instance of WP_Error on failure.
*/
public function get_data() {
if ( ! file_exists( $this->config['data_json_file'] ) ) {
return new WP_Error( 'font_collection_file_error', __( 'Font Collection data JSON file does not exist.', 'gutenberg' ) );
}
// If the src is a URL, fetch the data from the URL.
if ( false !== strpos( $this->config['src'], 'http' ) && false !== strpos( $this->config['src'], ':https://' ) ) {
if ( ! wp_http_validate_url( $this->config['src'] ) ) {
return new WP_Error( 'font_collection_read_error', __( 'Invalid URL for Font Collection data.', 'gutenberg' ) );
}

$response = wp_remote_get( $this->config['src'] );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error( 'font_collection_read_error', __( 'Error fetching the Font Collection data from a URL.', 'gutenberg' ) );
}

$data = wp_json_file_decode( $this->config['data_json_file'], array( 'associative' => true ) );
if ( empty( $data ) ) {
return new WP_Error( 'font_collection_read_error', __( 'Error reading the Font Collection data JSON file contents.', 'gutenberg' ) );
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $data ) ) {
return new WP_Error( 'font_collection_read_error', __( 'Error decoding the Font Collection data from the REST response JSON.', 'gutenberg' ) );
}
// If the src is a file path, read the data from the file.
} else {
if ( ! file_exists( $this->config['src'] ) ) {
return new WP_Error( 'font_collection_read_error', __( 'Font Collection data JSON file does not exist.', 'gutenberg' ) );
}
$data = wp_json_file_decode( $this->config['src'], array( 'associative' => true ) );
if ( empty( $data ) ) {
return new WP_Error( 'font_collection_read_error', __( 'Error reading the Font Collection data JSON file contents.', 'gutenberg' ) );
}
}

$collection_data = $this->get_config();
$collection_data['data'] = $data;
unset( $collection_data['data_json_file'] );
unset( $collection_data['src'] );
return $collection_data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,18 @@ public function register_routes() {
public function get_font_collection( $request ) {
$id = $request->get_param( '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;
}

return new WP_REST_Response( $collection->get_data() );
$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 new WP_REST_Response( $collection_with_data );
}

/**
Expand Down
Loading

1 comment on commit 8a82a37

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8a82a37.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6276909498
📝 Reported issues:

Please sign in to comment.