Skip to content

Commit

Permalink
Add function to determine duplicate variants and delete.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Sep 12, 2023
1 parent 0d3373a commit f72d258
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
28 changes: 5 additions & 23 deletions lib/experimental/fonts/font-library/class-wp-font-family-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,12 @@ public static function merge_fonts_data( $font1, $font2 ) {

$font_faces_1 = isset( $font1['fontFace'] ) ? $font1['fontFace'] : array();
$font_faces_2 = isset( $font2['fontFace'] ) ? $font2['fontFace'] : array();

$merged_font_faces = array_merge( $font_faces_1, $font_faces_2 );

$unique_faces_map = array();

foreach ( $merged_font_faces as $font_face ) {
$fontStyle = isset( $font_face['fontStyle'] ) ? $font_face['fontStyle'] : '';
$fontWeight = isset( $font_face['fontWeight'] ) ? $font_face['fontWeight'] : '';

// Create a unique key based on fontStyle and fontWeight
$unique_key = $fontStyle . '_' . $fontWeight;

// If this is a new face based on fontStyle and fontWeight, add it to the unique_faces_map
if ( ! isset( $unique_faces_map[ $unique_key ] ) ) {
$unique_faces_map[$unique_key] = $font_face;
} else {
// Overwrite the old src with the new one.
$unique_faces_map[ $unique_key ]['src'] = $font_face['src'];
// TODO: remove the old font face asset.
}
}

$unique_faces = array_values( $unique_faces_map );


$serialized_faces = array_map( 'serialize', $merged_font_faces );
$unique_serialized_faces = array_unique( $serialized_faces );
$unique_faces = array_map( 'unserialize', $unique_serialized_faces );

$merged_font = array_merge( $font1, $font2 );
$merged_font['fontFace'] = $unique_faces;

Expand Down
57 changes: 54 additions & 3 deletions lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,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 +496,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 @@ -491,9 +528,23 @@ private function create_font_post() {
* @return int|WP_Error Post ID if the update was successful, WP_Error otherwise.
*/
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;
$post_font_data = json_decode( $post->post_content, true );
$new_data = WP_Font_Family_Utils::merge_fonts_data( $post_font_data, $this->data );
$intersecting = $this->get_intersecting_font_faces( $post_font_data['fontFace'], $new_data['fontFace'] );

if ( ! 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

0 comments on commit f72d258

Please sign in to comment.