From dc148440f85ca08e3dbf4d5cf25bf83d111ce7de Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 19 Apr 2022 11:30:43 -0500 Subject: [PATCH] Fixes undefined index notice in _gutenberg_is_webfont_equal(). Adds a guard clause to check if the attribute exists in each webfont before the comparison happens. If no, it bails out, returning false, as there's nothing to compare. Some girl scouting: * Renames the function's properties to be more readable. * Adds `array` type declaration as only arrays are accepted. * A wee bit of alignment for consistency in Core. --- lib/experimental/webfonts-utils.php | 41 +++++++++++++++++------------ 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/experimental/webfonts-utils.php b/lib/experimental/webfonts-utils.php index 495f06d0e0fac..2320062d097c9 100644 --- a/lib/experimental/webfonts-utils.php +++ b/lib/experimental/webfonts-utils.php @@ -50,27 +50,34 @@ function _gutenberg_resolve_font_face_uri( $font_face ) { } /** - * Compares two webfonts. + * Compares if the two given webfonts are the equal. * - * @param array $a The first webfont. - * @param array $b The second webfont. - * @param boolean $is_camel_case If the font attributes are in camel case or kebab case. Defaults to camel case. - * - * @return boolean True if they're equal, false otherwise. + * @param array $webfont1 The first webfont. + * @param array $webfont2 The second webfont. + * @param boolean $is_camel_case True if the font attributes are in camel case; else false for kebab case. + * Defaults to camel case. + * @return boolean True if the webfonts are equal, false otherwise. */ -function _gutenberg_is_webfont_equal( $a, $b, $is_camel_case = true ) { - $equality_attrs = $is_camel_case ? array( - 'fontFamily', - 'fontStyle', - 'fontWeight', - ) : array( - 'font-family', - 'font-style', - 'font-weight', - ); +function _gutenberg_is_webfont_equal( array $webfont1, array $webfont2, $is_camel_case = true ) { + $equality_attrs = $is_camel_case + ? array( + 'fontFamily', + 'fontStyle', + 'fontWeight', + ) + : array( + 'font-family', + 'font-style', + 'font-weight', + ); foreach ( $equality_attrs as $attr ) { - if ( $a[ $attr ] !== $b[ $attr ] ) { + // Bail out if the attribute does not exist. + if ( ! isset( $webfont1[ $attr ] ) || ! isset( $webfont2[ $attr ] ) ) { + return false; + } + + if ( $webfont1[ $attr ] !== $webfont2[ $attr ] ) { return false; } }