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: return null if a font collection is not registered #58735

Merged
merged 10 commits into from
Feb 7, 2024
5 changes: 2 additions & 3 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,13 @@ public function get_font_collections() {
* @since 6.5.0
*
* @param string $slug Font collection slug.
* @return WP_Font_Collection|WP_Error Font collection object,
* or WP_Error object if the font collection doesn't exist.
* @return WP_Font_Collection|null Font collection object, or null if the font collection doesn't exist.
*/
public function get_font_collection( $slug ) {
if ( $this->is_collection_registered( $slug ) ) {
return $this->collections[ $slug ];
}
return new WP_Error( 'font_collection_not_found', __( 'Font collection not found.', 'gutenberg' ) );
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ public function get_item( $request ) {
$slug = $request->get_param( 'slug' );
$collection = WP_Font_Library::get_instance()->get_font_collection( $slug );

// If the collection doesn't exist returns a 404.
if ( is_wp_error( $collection ) ) {
$collection->add_data( array( 'status' => 404 ) );
return $collection;
// @TODO: remove `is_wp_error` check once WP trunk is updated to return null when a collection is not found.
if ( ! $collection || is_wp_error( $collection ) ) {
return new WP_Error( 'rest_font_collection_not_found', __( 'Font collection not found.' ), array( 'status' => 404 ) );
}

return $this->prepare_item_for_response( $collection, $request );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function test_get_item_invalid_slug() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/non-existing-collection' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'font_collection_not_found', $response, 404 );
$this->assertErrorResponse( 'rest_font_collection_not_found', $response, 404 );
}

/**
Expand Down
Loading