Skip to content

Commit

Permalink
return detailed errors from the fonts api
Browse files Browse the repository at this point in the history
  • Loading branch information
madhusudhand committed Sep 27, 2023
1 parent b813d3d commit bea0a15
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
56 changes: 41 additions & 15 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;
$has_errors = false;

foreach ( $sources as $src ) {
$suffix = $index++ > 0 ? $index : '';
Expand All @@ -345,6 +355,8 @@ private function download_font_face_assets( $font_face ) {
$new_src = $this->download_asset( $src, $filename );
if ( $new_src ) {
$new_font_face['src'][] = $new_src;
} else {
$has_errors = true;
}
}

Expand All @@ -356,7 +368,12 @@ private function download_font_face_assets( $font_face ) {
// because it is no longer needed.
unset( $new_font_face['downloadFromUrl'] );

return $new_font_face;
$error = $has_errors ? new WP_Error(
'font_face_download_failed',
__( 'The font face assets could not be downloaded.', 'gutenberg' )
) : null;

return array( $new_font_face, $error );
}


Expand All @@ -370,13 +387,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 ) {
$errors = array();
if ( ! $this->has_font_faces() ) {
return true;
return array( true, $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 +414,22 @@ 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 ) ) {
$errors[] = $error;
}

/*
* 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 +442,10 @@ private function download_or_move_font_faces( $files ) {

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

return false;
return array( false, $errors );
}

/**
Expand Down Expand Up @@ -597,17 +619,21 @@ 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',
$errors[] = new WP_Error(
'font_face_upload_failed',
__( 'The font face assets could not be written.', 'gutenberg' )
);
}

if ( ! empty( $errors ) ) {
return $errors;
}

$post_id = $this->create_or_update_font_post();

if ( is_wp_error( $post_id ) ) {
Expand Down
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 @@ -375,8 +375,8 @@ public function install_fonts( $request ) {
// 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 );
$font = new WP_Font_Family( $font_data );
$errors = $font->install( $files );
$fonts_installed[] = $font;
}

Expand All @@ -388,6 +388,14 @@ public function install_fonts( $request ) {
);
}

if ( ! empty( $errors ) ) {
return new WP_Error(
'error_installing_fonts',
__( 'Error installing fonts.', 'gutenberg' ),
array( 'status' => 500 )
);
}

$response = array();
foreach ( $fonts_installed as $font ) {
$response[] = $font->get_data();
Expand Down

0 comments on commit bea0a15

Please sign in to comment.