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

Automatically add font license info for Google fonts #298

Merged
merged 26 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ab21e9e
Add font license function for Google fonts
mikachan Mar 31, 2023
9e29028
Install php-font-lib
mikachan Apr 1, 2023
6015bc2
Use php-font-lib to check for license info
mikachan Apr 1, 2023
b5eaaa5
Use custom format for OFL
mikachan Apr 2, 2023
121fc68
Rename function to manage_font_license
mikachan Apr 4, 2023
a4397de
Squashed merge commit with trunk
mikachan Apr 5, 2023
8d05847
Try to fix fallback-fonts-list file
mikachan Apr 5, 2023
92cd07c
Revert "Try to fix fallback-fonts-list file"
mikachan Apr 5, 2023
a96c5f8
Revert "Squashed merge commit with trunk"
mikachan Apr 5, 2023
6b7028c
Replace php-font-lib with a later release
mikachan Apr 5, 2023
329cc48
Rename font name variable
mikachan Apr 5, 2023
27cb94b
Use stripos rather than strpos
mikachan Apr 5, 2023
d2347ad
Refactor remove functionality
mikachan Apr 5, 2023
e1669b7
Use lib-font to get font credits
mikachan Apr 19, 2023
d24aece
Use font-credits data passed from client-side
mikachan Apr 19, 2023
686f375
Remove php-font-lib
mikachan Apr 19, 2023
531fecb
Add ending tag to credit info
mikachan Apr 19, 2023
0433552
Use array_key_exists consistently
mikachan Apr 19, 2023
2b4348f
Merge branch 'trunk' into add/handle-google-font-credits
mikachan Apr 27, 2023
3c7df1c
Use get() function to get name records
mikachan Apr 27, 2023
f1bec0c
Trim whitespace from all credit fields
mikachan Apr 27, 2023
740ca8a
Prevent license info from being over 200 chars
mikachan Apr 27, 2023
168cf9a
Refactor end of credits note
mikachan Apr 27, 2023
43d7ad9
Split license info at first new line
mikachan Apr 27, 2023
9894b33
Add new line above credits
mikachan May 4, 2023
5945aaa
Move delete functionality to prepare_font_families_for_database
mikachan May 4, 2023
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
89 changes: 89 additions & 0 deletions admin/class-manage-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ protected function prepare_font_families_for_database( $font_families ) {
$new_font_faces = array();
foreach ( $font_family['fontFace'] as $font_face ) {
$updated_font_face = $font_face;
// Remove font license from readme.txt if font family is removed
if ( isset( $font_family['shouldBeRemoved'] ) ) {
$this->manage_font_license( $font_face['fontFamily'], 'remove' );
}
if ( ! isset( $font_face['shouldBeRemoved'] ) && ! isset( $font_family['shouldBeRemoved'] ) ) {
$new_font_faces[] = $updated_font_face;
} else {
Expand Down Expand Up @@ -178,6 +182,10 @@ function save_local_fonts_to_theme() {
$new_font_faces = array( $uploaded_font_face );

$this->add_or_update_theme_font_faces( $_POST['font-name'], $font_slug, $new_font_faces );

// Add font license to readme.txt
$this->manage_font_license( $_POST['font-name'], $file_name );

return add_action( 'admin_notices', array( 'Font_Form_Messages', 'admin_notice_embed_font_success' ) );
}
return add_action( 'admin_notices', array( 'Font_Form_Messages', 'admin_notice_embed_font_file_error' ) );
Expand Down Expand Up @@ -229,8 +237,11 @@ function save_google_fonts_to_theme() {
);
}
}

$this->add_or_update_theme_font_faces( $google_font_name, $font_slug, $new_font_faces );

// Add font license to readme.txt
$this->manage_font_license( $font_family['family'], $file_name );
}

add_action( 'admin_notices', array( 'Font_Form_Messages', 'admin_notice_embed_font_success' ) );
Expand Down Expand Up @@ -296,7 +307,85 @@ function ( $font_family ) use ( $font_slug ) {
get_stylesheet_directory() . '/theme.json',
$theme_json_string
);
}

function manage_font_license( $font_name, $file_name ) {
if ( ! $font_name ) {
return;
}

// Build end of credits note
$end_credits_note = '-- End of ' . $font_name . ' Font credits --';

// Get theme readme.txt
$readme_file = get_stylesheet_directory() . '/readme.txt';
$readme_file_contents = file_get_contents( $readme_file );

if ( ! $readme_file ) {
return;
}

// If file_name exists, then add font license to readme.txt
if ( 'remove' !== $file_name && is_string( $file_name ) && ! empty( $_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 );

// 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'] ) : '';
$license_url = array_key_exists( 'licenseURL', $font_credits ) ? "\n" . 'License URL: ' . trim( $font_credits['licenseURL'] ) : '';
$font_source = array_key_exists( 'source', $font_credits ) ? "\n" . 'Source: ' . trim( $font_credits['source'] ) : '';

// Handle longer, multi-line license info content
if ( is_string( $license_info ) ) {
// Split license info at first new line
$license_info = "\n" . strtok( $license_info, "\n" );

// Prevent license info from being over 200 characters
if ( strlen( $license_info ) > 200 ) {
$license_info = substr( $license_info, 0, strrpos( substr( $license_info, 0, 200 ), ' ' ) ) . '...';
}
}

// Build the font credits string
$font_credits = "

{$font_name} Font
{$copyright} {$license_info} {$license_url} {$font_source}
{$end_credits_note}
";

// Add font credits to the end of readme.txt
file_put_contents(
$readme_file,
$font_credits,
FILE_APPEND
);
}
}

// If file_name is set to 'remove', then remove font license from readme.txt
if ( 'remove' === $file_name ) {
// Check if font credits are in readme.txt
if ( false !== stripos( $readme_file_contents, $font_name ) ) {
// Calculate the start and end positions of the font credits
$font_name_strlength = strlen( $font_name . ' Font' ) + 1;
$end_credits_strlength = strlen( $end_credits_note ) + 1;
$font_start = stripos( $readme_file_contents, "\n" . $font_name . ' Font' ) + $font_name_strlength;
$font_end = stripos( $readme_file_contents, $end_credits_note, $font_start );

// Check if the start and end positions are valid
if ( false === $font_start || false === $font_end ) {
return;
}

// Remove the font credits from readme.txt
$removed_font_credits = substr_replace( $readme_file_contents, '', $font_start - $font_name_strlength, $font_end + $end_credits_strlength - $font_start + $font_name_strlength );
file_put_contents( $readme_file, $removed_font_credits );
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
"test": "phpunit",
"test:watch": "phpunit-watcher watch < /dev/tty"
}
}
}
23 changes: 12 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@
See https://github.com/WordPress/WordPress-Coding-Standards/issues/2035
-->
<ini name="error_reporting" value="E_ALL &#38; ~E_DEPRECATED" />
</ruleset>
</ruleset>
34 changes: 33 additions & 1 deletion src/google-fonts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useState, useEffect } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
import { SelectControl, Spinner, Button } from '@wordpress/components';
import { Font } from 'lib-font';

import FontsSidebar from '../fonts-sidebar';
import FontVariant from './font-variant';
Expand Down Expand Up @@ -35,6 +36,7 @@ const EMPTY_SELECTION_DATA = {};
function GoogleFonts() {
const [ googleFontsData, setGoogleFontsData ] = useState( {} );
const [ selectedFont, setSelectedFont ] = useState( null );
const [ selectedFontCredits, setSelectedFontCredits ] = useState( null );
const [ selectionData, setSelectionData ] =
useState( EMPTY_SELECTION_DATA );

Expand Down Expand Up @@ -152,8 +154,33 @@ function GoogleFonts() {
return select( coreDataStore ).getCurrentTheme();
}, null );

const getFontCredits = ( selectedFontObj ) => {
const fontObj = new Font( selectedFontObj.family );

// 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 );

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 );
}
};

const handleSelectChange = ( value ) => {
setSelectedFont( googleFontsData.items[ value ] );
getFontCredits( googleFontsData.items[ value ] );
};

let selectedFontFamilyId = '';
Expand Down Expand Up @@ -334,6 +361,11 @@ function GoogleFonts() {
Object.values( selectionData )
) }
/>
<input
type="hidden"
name="font-credits"
value={ JSON.stringify( selectedFontCredits ) }
/>
<Button
variant="primary"
type="submit"
Expand All @@ -342,7 +374,7 @@ function GoogleFonts() {
}
>
{ __(
'Add google fonts to your theme',
'Add Google fonts to your theme',
'create-block-theme'
) }
</Button>
Expand Down