Skip to content

Commit

Permalink
Merge branch 'trunk' into fix/font-face/backup-font-family-name-check
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Sep 20, 2023
2 parents 60a5a50 + 1873738 commit 014b4e3
Show file tree
Hide file tree
Showing 290 changed files with 59,009 additions and 665 deletions.
471 changes: 471 additions & 0 deletions changelog.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Printing since 1440. This is the development plugin for the block editor, site editor, and other future WordPress core functionality.
* Requires at least: 6.2
* Requires PHP: 7.0
* Version: 16.6.0
* Version: 16.7.0-rc.1
* Author: Gutenberg Team
* Text Domain: gutenberg
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function get_data() {
return new WP_Error( 'font_collection_file_error', __( 'Font Collection data JSON file does not exist.', 'gutenberg' ) );
}

$data = file_get_contents( $this->config['data_json_file'] );
$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' ) );
}
Expand Down
60 changes: 58 additions & 2 deletions lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private function get_upload_overrides( $filename ) {
'test_form' => false,
// Seems mime type for files that are not images cannot be tested.
// See wp_check_filetype_and_ext().
'test_type' => false,
'test_type' => true,
'mimes' => WP_Font_Library::ALLOWED_FONT_MIME_TYPES,
'unique_filename_callback' => static function () use ( $filename ) {
// Keep the original filename.
return $filename;
Expand Down Expand Up @@ -379,6 +380,20 @@ private function download_or_move_font_faces( $files ) {
// (for example to install fonts that use a remote url).
$new_font_face = $font_face;

$font_face_is_repeated = false;

// If the font face has the same fontStyle and fontWeight as an existing, continue.
foreach ( $new_font_faces as $font_to_compare ) {
if ( $new_font_face['fontStyle'] === $font_to_compare['fontStyle'] &&
$new_font_face['fontWeight'] === $font_to_compare['fontWeight'] ) {
$font_face_is_repeated = true;
}
}

if ( $font_face_is_repeated ) {
continue;
}

// 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 );
Expand Down Expand Up @@ -482,6 +497,29 @@ private function create_font_post() {
return $post_id;
}

/**
* Gets the font faces that are in both the existing and incoming font families.
*
* @since 6.4.0
*
* @param array $existing The existing font faces.
* @param array $incoming The incoming font faces.
* @return array The font faces that are in both the existing and incoming font families.
*/
private function get_intersecting_font_faces( $existing, $incoming ) {
$intersecting = array();
foreach ( $existing as $existing_face ) {
foreach ( $incoming as $incoming_face ) {
if ( $incoming_face['fontStyle'] === $existing_face['fontStyle'] &&
$incoming_face['fontWeight'] === $existing_face['fontWeight'] &&
$incoming_face['src'] !== $existing_face['src'] ) {
$intersecting[] = $existing_face;
}
}
}
return $intersecting;
}

/**
* Updates a post for a font family.
*
Expand All @@ -493,7 +531,23 @@ private function create_font_post() {
private function update_font_post( $post ) {
$post_font_data = json_decode( $post->post_content, true );
$new_data = WP_Font_Family_Utils::merge_fonts_data( $post_font_data, $this->data );
$this->data = $new_data;
if ( isset( $post_font_data['fontFace'] ) && ! empty( $post_font_data['fontFace'] ) ) {
$intersecting = $this->get_intersecting_font_faces( $post_font_data['fontFace'], $new_data['fontFace'] );
}

if ( isset( $intersecting ) && ! empty( $intersecting ) ) {
$serialized_font_faces = array_map( 'serialize', $new_data['fontFace'] );
$serialized_intersecting = array_map( 'serialize', $intersecting );

$diff = array_diff( $serialized_font_faces, $serialized_intersecting );

$new_data['fontFace'] = array_values( array_map( 'unserialize', $diff ) );

foreach ( $intersecting as $intersect ) {
$this->delete_font_face_assets( $intersect );
}
}
$this->data = $new_data;

$post = array(
'ID' => $post->ID,
Expand Down Expand Up @@ -541,9 +595,11 @@ private function create_or_update_font_post() {
* @return array|WP_Error An array of font family data on success, WP_Error otherwise.
*/
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 );
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(
Expand Down
20 changes: 17 additions & 3 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
*/
class WP_Font_Library {

const PHP_7_TTF_MIME_TYPE = PHP_VERSION_ID >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf';

const ALLOWED_FONT_MIME_TYPES = array(
'otf' => 'font/otf',
'ttf' => 'font/ttf',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'ttf' => PHP_VERSION_ID >= 70400 ? 'font/sfnt' : self::PHP_7_TTF_MIME_TYPE,
'woff' => PHP_VERSION_ID >= 80100 ? 'font/woff' : 'application/font-woff',
'woff2' => PHP_VERSION_ID >= 80100 ? 'font/woff2' : 'application/font-woff2',
);

/**
Expand Down Expand Up @@ -118,4 +120,16 @@ public static function set_upload_dir( $defaults ) {

return $defaults;
}

/**
* Sets the allowed mime types for fonts.
*
* @since 6.4.0
*
* @param array $mime_types List of allowed mime types.
* @return array Modified upload directory.
*/
public static function set_allowed_mime_types( $mime_types ) {
return array_merge( $mime_types, self::ALLOWED_FONT_MIME_TYPES );
}
}
Loading

0 comments on commit 014b4e3

Please sign in to comment.