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 13 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
84 changes: 84 additions & 0 deletions admin/class-manage-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ function delete_font_asset( $font_face ) {
}

if ( file_exists( $font_asset_path ) ) {
// Remove font license from readme.txt
$this->manage_font_license( $font_face['fontFamily'], 'remove' );
return unlink( $font_asset_path );
}

Expand Down Expand Up @@ -178,6 +180,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 +235,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 +305,82 @@ 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;
}

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

if ( ! $readme_file ) {
return;
}

// Format font name for font source link
$google_formatted_font_name = str_replace( ' ', '+', $font_name );

// If file_name exists, then add font license to readme.txt
if ( 'remove' !== $file_name && is_string( $file_name ) ) {
// Check that the font is not already credited in readme.txt
if ( false === stripos( $readme_file_contents, $font_name ) ) {
// Require php-font-lib
require_once( __DIR__ . '/../includes/FontLib/Autoloader.php' );

// Build font source URL
$font_source = 'https://www.google.com/fonts/specimen/' . $google_formatted_font_name;
mikachan marked this conversation as resolved.
Show resolved Hide resolved

// Get license info from font file
$font = \FontLib\Font::load( get_stylesheet_directory() . '/assets/fonts/' . $file_name );
$font->parse();
$license_info = $font->getNameTableString( 13 ) ? $font->getNameTableString( 13 ) : '';

if ( stripos( $font->getNameTableString( 13 ), 'SIL Open Font License' ) ) {
mikachan marked this conversation as resolved.
Show resolved Hide resolved
// If license is SIL Open Font License, use custom format
$license_info = 'Licensed under the SIL Open Font License, Version 1.1';
}

$license_url = $font->getNameTableString( 14 ) ? "({$font->getNameTableString(14)})" : '';
mikachan marked this conversation as resolved.
Show resolved Hide resolved

// Build the font credits string
$font_credits = "
{$font_name} Font
{$license_info} {$license_url}
Source: {$font_source}
";

// 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;
$google_font_name_strlength = strlen( '/' . $google_formatted_font_name ) + 1;
$font_start = stripos( $readme_file_contents, $font_name . ' Font' ) + $font_name_strlength;
$font_end = stripos( $readme_file_contents, '/' . $google_formatted_font_name, $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 + $google_font_name_strlength - $font_start + $font_name_strlength );
file_put_contents( $readme_file, $removed_font_credits );
}
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@
"sirbrillig/phpcs-variable-analysis": "^2.8"
},
"require": {
"composer/installers": "~1.0"
"composer/installers": "~1.0",
"phenx/php-font-lib": "^0.5.4"
},
"scripts": {
"format": "phpcbf --standard=phpcs.xml.dist --report-summary --report-source",
"lint": "phpcs --standard=phpcs.xml.dist",
"test": "phpunit",
"test:watch": "phpunit-watcher watch < /dev/tty"
"test:watch": "phpunit-watcher watch < /dev/tty",
"post-autoload-dump": [
"cp -r vendor/phenx/php-font-lib/src/FontLib includes"
mikachan marked this conversation as resolved.
Show resolved Hide resolved
]
}
}
}
69 changes: 57 additions & 12 deletions composer.lock

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

Loading