From 69e93d6aee810fc2ca6d00bf7b8660962efd7cd6 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 9 May 2023 16:29:55 +0100 Subject: [PATCH 1/5] Return early if font_credits is falsy --- admin/class-manage-fonts.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/admin/class-manage-fonts.php b/admin/class-manage-fonts.php index 42ed3159..4288a75c 100644 --- a/admin/class-manage-fonts.php +++ b/admin/class-manage-fonts.php @@ -332,6 +332,10 @@ function manage_font_license( $font_name, $file_name ) { // Get font credits from font file metadata $font_credits = json_decode( stripslashes( $_POST['font-credits'] ), true ); + if ( ! $font_credits ) { + return; + } + // Assign font credits to variables $copyright = array_key_exists( 'copyright', $font_credits ) ? trim( $font_credits['copyright'] ) : ''; $license_info = array_key_exists( 'license', $font_credits ) ? "\n" . trim( $font_credits['license'] ) : ''; From 769ed0a9cbfcfae0934aadd81a10d7e7c61021af Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 9 May 2023 17:08:02 +0100 Subject: [PATCH 2/5] Set empty object as default value for font-credits --- src/google-fonts/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/google-fonts/index.js b/src/google-fonts/index.js index 7f147210..d7e67ac7 100644 --- a/src/google-fonts/index.js +++ b/src/google-fonts/index.js @@ -36,7 +36,7 @@ const EMPTY_SELECTION_DATA = {}; function GoogleFonts() { const [ googleFontsData, setGoogleFontsData ] = useState( {} ); const [ selectedFont, setSelectedFont ] = useState( null ); - const [ selectedFontCredits, setSelectedFontCredits ] = useState( null ); + const [ selectedFontCredits, setSelectedFontCredits ] = useState( {} ); const [ selectionData, setSelectionData ] = useState( EMPTY_SELECTION_DATA ); From 2e7865add6f218d40b7740cc6730903011722fed Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 9 May 2023 17:08:45 +0100 Subject: [PATCH 3/5] Handle font loading error --- src/google-fonts/index.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/google-fonts/index.js b/src/google-fonts/index.js index d7e67ac7..a10cfbfc 100644 --- a/src/google-fonts/index.js +++ b/src/google-fonts/index.js @@ -156,25 +156,32 @@ function GoogleFonts() { const getFontCredits = ( selectedFontObj ) => { const fontObj = new Font( selectedFontObj.family ); + let fontError = false; // Load font file fontObj.src = Object.values( selectedFontObj.files )[ 0 ]; - // eslint-disable-next-line no-console - fontObj.onerror = ( event ) => console.error( event ); - fontObj.onload = ( event ) => getFontData( event ); + fontObj.onerror = ( event ) => { + // eslint-disable-next-line no-console + console.error( event ); + fontError = true; + }; - function getFontData( event ) { - const font = event.detail.font; - const nameTable = font.opentype.tables.name; + if ( ! fontError ) { + fontObj.onload = ( event ) => getFontData( event ); - const fontCredits = { - copyright: nameTable.get( 0 ), - source: nameTable.get( 11 ), - license: nameTable.get( 13 ), - licenseURL: nameTable.get( 14 ), - }; + function getFontData( event ) { + const font = event.detail.font; + const nameTable = font.opentype.tables.name; + + const fontCredits = { + copyright: nameTable.get( 0 ), + source: nameTable.get( 11 ), + license: nameTable.get( 13 ), + licenseURL: nameTable.get( 14 ), + }; - setSelectedFontCredits( fontCredits ); + setSelectedFontCredits( fontCredits ); + } } }; From 62b87837b5c70c12cb54349f2e3c5ca973bd9297 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 9 May 2023 17:19:36 +0100 Subject: [PATCH 4/5] Check if font-credits is set and is an array --- admin/class-manage-fonts.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/admin/class-manage-fonts.php b/admin/class-manage-fonts.php index 4288a75c..163d6761 100644 --- a/admin/class-manage-fonts.php +++ b/admin/class-manage-fonts.php @@ -325,14 +325,14 @@ function manage_font_license( $font_name, $file_name ) { return; } - // If file_name exists, then add font license to readme.txt - if ( 'remove' !== $file_name && is_string( $file_name ) && ! empty( $_POST['font-credits'] ) ) { + // If file_name and font-credits exist, then add font license to readme.txt + if ( 'remove' !== $file_name && is_string( $file_name ) && ! empty( $_POST['font-credits'] ) && isset( $_POST['font-credits'] ) ) { // Check that the font is not already credited in readme.txt if ( false === stripos( $readme_file_contents, $font_name ) ) { // Get font credits from font file metadata $font_credits = json_decode( stripslashes( $_POST['font-credits'] ), true ); - if ( ! $font_credits ) { + if ( ! is_array( $font_credits ) ) { return; } From fab487f36bd17421a331b422df8ad85545e2b33a Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 10 May 2023 10:25:21 +0100 Subject: [PATCH 5/5] Ensure all Google fonts are served over https --- src/google-fonts/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/google-fonts/index.js b/src/google-fonts/index.js index a10cfbfc..7e9c8421 100644 --- a/src/google-fonts/index.js +++ b/src/google-fonts/index.js @@ -158,8 +158,14 @@ function GoogleFonts() { const fontObj = new Font( selectedFontObj.family ); let fontError = false; + // Force font file to be https + let fontFile = Object.values( selectedFontObj.files )[ 0 ]; + if ( fontFile.includes( 'http://' ) ) { + fontFile = fontFile.replace( 'http://', 'https://' ); + } + // Load font file - fontObj.src = Object.values( selectedFontObj.files )[ 0 ]; + fontObj.src = fontFile; fontObj.onerror = ( event ) => { // eslint-disable-next-line no-console console.error( event );