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 detailed errors from the fonts api #54864

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
82 changes: 57 additions & 25 deletions lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private function download_asset( $url, $filename ) {
}

/**
* Moves an uploaded font face asset from temp folder to the fonts directory.
* Moves an uploaded font face asset from temp directory to the fonts directory.
*
* This is used when uploading local fonts.
*
Expand All @@ -266,10 +266,14 @@ private function move_font_face_asset( $font_face, $file ) {
// If the filename has no font mime type, don't move the file and
// return the font face definition without src to be ignored later.
if ( ! WP_Font_Family_Utils::has_font_mime_type( $filename ) ) {
return $new_font_face;
$error = new WP_Error(
'font_mime_check_failed',
__( 'The font mime type is not valid.', 'gutenberg' )
);
return array( $new_font_face, $error );
}

// Move the uploaded font asset from the temp folder to the fonts directory.
// Move the uploaded font asset from the temp directory to the fonts directory.
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
Expand All @@ -284,7 +288,12 @@ private function move_font_face_asset( $font_face, $file ) {
$new_font_face['src'] = $handled_file['url'];
}

return $new_font_face;
$error = isset( $handled_file['error'] ) ? new WP_Error(
'font_face_upload_failed',
__( 'The font face assets could not be uploaded.', 'gutenberg' )
) : null;

return array( $new_font_face, $error );
}

/**
Expand Down Expand Up @@ -333,6 +342,7 @@ private function download_font_face_assets( $font_face ) {
$sources = (array) $font_face['downloadFromUrl'];
$new_font_face['src'] = array();
$index = 0;
$error = null;

foreach ( $sources as $src ) {
$suffix = $index++ > 0 ? $index : '';
Expand All @@ -348,15 +358,20 @@ private function download_font_face_assets( $font_face ) {
}
}

if ( count( $new_font_face['src'] ) === 1 ) {
if ( count( $new_font_face['src'] ) > 0 ) {
$new_font_face['src'] = $new_font_face['src'][0];
} else {
$error = new WP_Error(
'font_face_download_failed',
__( 'The font face assets could not be downloaded.', 'gutenberg' )
);
}

// Remove the download url reference from the font face definition
// because it is no longer needed.
unset( $new_font_face['downloadFromUrl'] );

return $new_font_face;
return array( $new_font_face, $error );
}


Expand All @@ -370,13 +385,14 @@ private function download_font_face_assets( $font_face ) {
* @return bool True if the font faces were downloaded or moved successfully, false otherwise.
*/
private function download_or_move_font_faces( $files ) {
$font_face_errors = null;
if ( ! $this->has_font_faces() ) {
return true;
return array( true, $font_face_errors );
}

$new_font_faces = array();
foreach ( $this->data['fontFace'] as $font_face ) {
// If the fonts are not meant to be dowloaded or uploaded
// If the fonts are not meant to be downloaded or uploaded
// (for example to install fonts that use a remote url).
$new_font_face = $font_face;

Expand All @@ -396,18 +412,26 @@ private function download_or_move_font_faces( $files ) {

// If installing google fonts, download the font face assets.
if ( ! empty( $font_face['downloadFromUrl'] ) ) {
$new_font_face = $this->download_font_face_assets( $new_font_face );
list( $new_font_face, $error ) = $this->download_font_face_assets( $new_font_face );
}

// If installing local fonts, move the font face assets from
// the temp folder to the wp fonts directory.
// the temp directory to the wp fonts directory.
if ( ! empty( $font_face['uploadedFile'] ) && ! empty( $files ) ) {
$new_font_face = $this->move_font_face_asset(
list( $new_font_face, $error ) = $this->move_font_face_asset(
$new_font_face,
$files[ $new_font_face['uploadedFile'] ]
);
}

if ( is_wp_error( $error ) ) {
if ( null === $font_face_errors || ! is_wp_error( $font_face_errors ) ) {
$font_face_errors = $error;
} elseif ( ! in_array( $error->get_error_code(), $font_face_errors->get_error_codes(), true ) ) {
$font_face_errors->add( $error->get_error_code(), $error->get_error_message() );
}
}

/*
* If the font face assets were successfully downloaded, add the font face
* to the new font. Font faces with failed downloads are not added to the
Expand All @@ -420,10 +444,16 @@ private function download_or_move_font_faces( $files ) {

if ( ! empty( $new_font_faces ) ) {
$this->data['fontFace'] = $new_font_faces;
return true;
return array( true, $font_face_errors );
}

return false;
/*
* Possible errors:
* - font_face_download_failed: The font face assets could not be downloaded.
* - font_face_upload_failed: The font face assets could not be uploaded.
* - font_mime_check_failed: The font mime type is not valid.
*/
return array( false, $font_face_errors );
}

/**
Expand Down Expand Up @@ -597,23 +627,25 @@ private function create_or_update_font_post() {
public function install( $files = null ) {
add_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) );
add_filter( 'upload_dir', array( 'WP_Font_Library', 'set_upload_dir' ) );
$were_assets_written = $this->download_or_move_font_faces( $files );
list($were_assets_written, $errors) = $this->download_or_move_font_faces( $files );
remove_filter( 'upload_dir', array( 'WP_Font_Library', 'set_upload_dir' ) );
remove_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) );

if ( ! $were_assets_written ) {
return new WP_Error(
'font_face_download_failed',
__( 'The font face assets could not be written.', 'gutenberg' )
);
}

$post_id = $this->create_or_update_font_post();
if ( $were_assets_written ) {
$post_id = $this->create_or_update_font_post();

if ( is_wp_error( $post_id ) ) {
return $post_id;
if ( is_wp_error( $post_id ) ) {
if ( ! is_wp_error( $errors ) ) {
$errors = $post_id;
} else {
$errors->add( $post_id->get_error_code(), $post_id->get_error_message() );
}
}
}

return $this->get_data();
return array(
'data' => $this->get_data(),
'errors' => $errors,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ public function update_font_library_permissions_check() {
$upload_dir = wp_upload_dir()['basedir'];
if ( ! is_writable( $temp_dir ) || ! wp_is_writable( $upload_dir ) ) {
return new WP_Error(
'rest_cannot_write_fonts_folder',
__( 'Error: WordPress does not have permission to write the fonts folder on your server.', 'gutenberg' ),
'rest_cannot_write_fonts',
__( 'Error: WordPress does not have permission to write the fonts directory on your server.', 'gutenberg' ),
array(
'status' => 500,
)
Expand Down Expand Up @@ -373,26 +373,11 @@ public function install_fonts( $request ) {
$files = $request->get_file_params();

// Iterates the fonts data received and creates a new WP_Font_Family object for each one.
$fonts_installed = array();
foreach ( $fonts_to_install as $font_data ) {
$font = new WP_Font_Family( $font_data );
$font->install( $files );
$fonts_installed[] = $font;
}

if ( empty( $fonts_installed ) ) {
return new WP_Error(
'error_installing_fonts',
__( 'Error installing fonts. No font was installed.', 'gutenberg' ),
array( 'status' => 500 )
);
}

$response = array();
foreach ( $fonts_installed as $font ) {
$response[] = $font->get_data();
foreach ( $fonts_to_install as $font_data ) {
$font = new WP_Font_Family( $font_data );
$response[] = $font->install( $files );
}

return new WP_REST_Response( $response );
}
}
Loading