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

Make sure theme families are array before attempting to merge them #56050

Closed
wants to merge 3 commits into from

Conversation

vcanales
Copy link
Member

Follow up to #55981

Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/experimental/fonts-api/class-wp-fonts-resolver.php

@vcanales vcanales marked this pull request as ready for review November 10, 2023 19:57
@vcanales vcanales added [Type] Bug An existing feature does not function as intended [Feature] Typography Font and typography-related issues and PRs labels Nov 10, 2023
@vcanales vcanales changed the title Make sure theme families are array before merging Make sure theme families are array before attempting to merge them Nov 10, 2023
@@ -214,7 +214,7 @@ private static function get_settings() {

// Merge the variation settings with the global settings.
$settings['typography']['fontFamilies']['theme'] = array_merge(
$settings['typography']['fontFamilies']['theme'],
$settings['typography']['fontFamilies']['theme'] ?? array(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excuse me, but I don't understand why this change is necessary.
If $settings['typography']['fontFamilies']['theme'] is null, it will be set to an array here (around line 205):

if ( ! isset( $settings['typography']['fontFamilies']['theme'] ) || ! is_array( $settings['typography']['fontFamilies']['theme'] ) ) {
    $settings['typography']['fontFamilies']['theme'] = array();
}

Copy link
Member Author

@vcanales vcanales Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My best guess is that it's needed because the line you mention depends on us being within the editor:

If I understand that logic correctly, the second time we go through this condition, $set_theme_structure could be false, hence $settings['typography']['fontFamilies']['theme'] never getting initialized as an array. Another solution would be to move that initialization out of the conditional on $set_theme_structure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$settings['typography']['fontFamilies']['theme'] never getting initialized as an array.

Yes, $set_theme_structure is false on the second iteration of the foreach loop.

However, I don't understand how this affects $settings['typography']['fontFamilies']['theme'].
$settings['typography']['fontFamilies']['theme'] is set to an array during the first iteration of the loop and remains an array in subsequent iterations. array_merge() always returns an array value.

Copy link
Member Author

@vcanales vcanales Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$settings['typography']['fontFamilies'] = array_unique( $settings['typography']['fontFamilies'] );

After array_unique on line 222, $settings['typography']['fontFamilies']['theme'] is going null, and it doesn't get reset on the next iteration. It's probably going null because array_unique is getting rid of a duplicate key that contains the the theme array, because the function isn't really comparing the value? I don't think array_unique works well with multidimensional arrays.

Copy link
Contributor

@arthur791004 arthur791004 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the main issue is the $settings['typography']['fontFamilies']['theme'] becomes null after array_unique as it removes the key with the duplicate value.

Hence, I proposed #56067 to

  1. Set the flag of array_unique to SORT_REGULAR. The default flag is SORT_STRING and it means that the elements are converted to a string to do the comparison. However, the value after the conversion becomes Array and it leads to $settings['typography']['fontFamilies']['theme'] being null if the $settings['typography']['fontFamilies'] contains multiple keys.
    $settings['typography']['fontFamilies'] = array(
      'default' => array(),
      'theme' => array(),
    );
    
    print_r( (string) $settings['typography']['fontFamilies']['default'] ); // Array
    print_r( (string) $settings['typography']['fontFamilies']['theme'] ); // Array
    
  2. Reset the $settings['typography']['fontFamilies']['theme'] after the array_unique call to avoid it becoming null in the next iteration. WDYT?

Copy link
Contributor

@anton-vlasenko anton-vlasenko Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vcanales

After array_unique on line 222, $settings['typography']['fontFamilies']['theme'] is going null,

I'm not able to reproduce this.

Can you provide the values of $settings and $variations at which this issue occurs? Please see this code snippet: https://3v4l.org/InOuS#v7.3.0

After array_unique is applied on line 222, $settings['typography']['fontFamilies']['theme'] becomes null and does not reset in the subsequent iteration.

Can you provide an example of such behavior? Specifically, what exact array should I pass to array_unique() in order to make $settings['typography']['fontFamilies']['theme'] a null value?

@vcanales
Copy link
Member Author

Closing in favor of #56067, which seems like a more robust solution.

@vcanales vcanales closed this Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Typography Font and typography-related issues and PRs [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants