Skip to content
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

Font Library: load collection JSON data from a URL in the collection config #54067

Merged
merged 71 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
79d6af7
Adding Font Collection class
matiasbenedetto Aug 18, 2023
fd1c967
php formatting and linting
matiasbenedetto Aug 18, 2023
0cfac6c
Adding tests for collections routes
matiasbenedetto Aug 18, 2023
6bb2f8b
Adding tests for WP_Font_Collection constructor
matiasbenedetto Aug 18, 2023
4a67d0b
adding tests for WP_Font_Collection get_data()
matiasbenedetto Aug 18, 2023
b26cb63
adding tests for WP_Font_Library register_font_collection()
matiasbenedetto Aug 18, 2023
0cfddb2
get font collection tests
matiasbenedetto Aug 21, 2023
7ba6f61
adding 'wp_' prefix to the 'register_font_collection' filter name
matiasbenedetto Aug 21, 2023
454f9a5
fix callback name
matiasbenedetto Aug 21, 2023
932064f
making class property private
matiasbenedetto Aug 21, 2023
b184398
registering filter from font-library.php file
matiasbenedetto Aug 21, 2023
6a2dfa0
Merge branch 'trunk' into try/font-collections
matiasbenedetto Aug 22, 2023
36ab188
removing superfluous comment
matiasbenedetto Aug 22, 2023
ac938c9
moving files to according changes in trunk
matiasbenedetto Aug 22, 2023
a9c7b6a
config without a json file should fail
matiasbenedetto Aug 22, 2023
b2deb14
fix property name in tests
matiasbenedetto Aug 22, 2023
4765538
name fix
matiasbenedetto Aug 23, 2023
3fb8b0a
comment update
matiasbenedetto Aug 23, 2023
3fadd9d
Adds WP_Error to return type
hellofromtonya Aug 23, 2023
bdaacf8
Improves contructor error handling.
hellofromtonya Aug 23, 2023
85fe753
FontCollection::get_data(): Improves error handling
hellofromtonya Aug 23, 2023
4ae9af6
Removes empty space for wpcs
hellofromtonya Aug 23, 2023
de053da
adding filter in a simpler way
matiasbenedetto Aug 23, 2023
21dfbe3
micro-optimization
matiasbenedetto Aug 23, 2023
fb913af
reuse WP_Error response instead of creating a new one
matiasbenedetto Aug 23, 2023
0e6c026
Eliminates try/catch
hellofromtonya Aug 23, 2023
f79212d
Revert "Eliminates try/catch" commit
hellofromtonya Aug 23, 2023
4e952e3
Remove wp_register_font_collection and replace it by a global functio…
matiasbenedetto Aug 24, 2023
196459a
adding function comment and guard agains re-declaration
matiasbenedetto Aug 24, 2023
a76d943
php format
matiasbenedetto Aug 24, 2023
c15b5ae
fixing docblock coments
matiasbenedetto Aug 24, 2023
85261b3
removing param comment
matiasbenedetto Aug 24, 2023
ad52829
re-adding parameter comment removed by mistake
matiasbenedetto Aug 24, 2023
7161ba1
format php
matiasbenedetto Aug 24, 2023
8402b91
updating WP_Font_Collection __construct tests
matiasbenedetto Aug 24, 2023
e0947c9
updating WP_Font_Collection get_data tests
matiasbenedetto Aug 24, 2023
98d52ad
updating tests
matiasbenedetto Aug 24, 2023
a6ed145
php format
matiasbenedetto Aug 24, 2023
f8dc1a0
array check
matiasbenedetto Aug 24, 2023
4cb5c77
Documents config array structure
hellofromtonya Aug 24, 2023
4b7e5e3
adding tests for /fonts/collections endpoint
matiasbenedetto Aug 24, 2023
56607c9
adding /fonts/collections/<id> endpoint test
matiasbenedetto Aug 24, 2023
c928ebe
format
matiasbenedetto Aug 24, 2023
aeb42cf
format
matiasbenedetto Aug 24, 2023
0a98de6
add test for missing collection
matiasbenedetto Aug 24, 2023
46f9fde
removing not needed variables
matiasbenedetto Aug 24, 2023
785262b
Add more tests and split the test for WP_REST_Font_Library_Controller
matiasbenedetto Aug 25, 2023
de12fe7
lint
matiasbenedetto Aug 25, 2023
dc560b8
try to create dir for tests in CI
matiasbenedetto Aug 25, 2023
60997e9
Merge branch 'trunk' into try/font-collections
matiasbenedetto Aug 25, 2023
f87f538
renane config property to src and handle URLs apart from file paths
matiasbenedetto Aug 29, 2023
f55f7a7
Add the ability to load collection JSON data from a URL
matiasbenedetto Aug 30, 2023
0df9a0e
remove unwanted comment
matiasbenedetto Aug 30, 2023
7cc1dd3
merge with trunk
matiasbenedetto Sep 5, 2023
31a6315
fix missing merge
matiasbenedetto Sep 5, 2023
7d8e6e3
fix merge with trunk
matiasbenedetto Sep 5, 2023
4eadf1a
removing test file after merge with trunk
matiasbenedetto Sep 5, 2023
08f2e48
removing duplicated test files after merge with trunk
matiasbenedetto Sep 5, 2023
ebb5b7b
Merge branch 'trunk' into try/font-collections-url
matiasbenedetto Sep 20, 2023
61568e0
decode data to make availanle in the client as json
matiasbenedetto Sep 20, 2023
678aaef
update google fonts collection source
matiasbenedetto Sep 20, 2023
a097eca
format php'
matiasbenedetto Sep 20, 2023
0022826
removing the default font collection (google fonts) json file from repo
matiasbenedetto Sep 20, 2023
bdd756f
fixing logic to get data from url
matiasbenedetto Sep 20, 2023
d51a3ff
add tests to try the font collection fetching from url
matiasbenedetto Sep 20, 2023
44acc0c
update test
matiasbenedetto Sep 20, 2023
08fc24b
format comments
matiasbenedetto Sep 20, 2023
a047eb7
replacing existing url in test with a mock url
matiasbenedetto Sep 20, 2023
241f2a6
early return
matiasbenedetto Sep 21, 2023
baa8114
remove not needed filter
matiasbenedetto Sep 21, 2023
e4d7565
rewording error message
matiasbenedetto Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'], '://' ) ) {
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
Loading