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

Improve handling of font license errors #359

Merged
merged 5 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions admin/class-manage-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,17 @@ 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 ( ! is_array( $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'] ) : '';
Expand Down
43 changes: 28 additions & 15 deletions src/google-fonts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -156,25 +156,38 @@ function GoogleFonts() {

const getFontCredits = ( selectedFontObj ) => {
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 ];
// eslint-disable-next-line no-console
fontObj.onerror = ( event ) => console.error( event );
fontObj.onload = ( event ) => getFontData( event );
fontObj.src = fontFile;
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 );
}
}
};

Expand Down