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

Font Library #50927

Closed
wants to merge 119 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
6288451
adding a mockup of the font library
matiasbenedetto May 9, 2023
2306a48
adding endpoint for google fonts api font list
matiasbenedetto May 10, 2023
54cb297
more UI work for fonts library
matiasbenedetto May 10, 2023
12c3e1a
added computed categories to google fonts api data
matiasbenedetto May 12, 2023
00ca4d7
UI work
matiasbenedetto May 12, 2023
6389e60
installing complete families of google fonts
matiasbenedetto May 12, 2023
9fe9e50
activate and deactivate font families and font variants
matiasbenedetto May 15, 2023
7d5fd85
adding global styles save button
matiasbenedetto May 15, 2023
d0187b1
transforming google fonts api response in to themejson fontFamilies f…
matiasbenedetto May 16, 2023
e076fc5
More UI Work
matiasbenedetto May 16, 2023
f8f736d
More UI Work
matiasbenedetto May 16, 2023
3b7e785
implementing toggleInstallFont and installedFontsOutline
matiasbenedetto May 17, 2023
5f1b248
UI implementation work
matiasbenedetto May 17, 2023
fffbfa0
google fonts search
matiasbenedetto May 17, 2023
052f54c
font variant styles
matiasbenedetto May 17, 2023
e12f9d1
select control for categories
matiasbenedetto May 17, 2023
919927e
discard font library changes when switching tabs in the font library …
matiasbenedetto May 17, 2023
bbf6684
activated fonts
matiasbenedetto May 18, 2023
1c2bbe1
load theme fonts assets
matiasbenedetto May 18, 2023
3eca1d6
UI work for font previews
matiasbenedetto May 18, 2023
ed7de77
fixing logic for select/unselect variants to install
matiasbenedetto May 19, 2023
b3715e3
UI work
matiasbenedetto May 19, 2023
42e300f
saving google fonts working
matiasbenedetto May 22, 2023
1a5c60f
handling font uninstall
matiasbenedetto May 23, 2023
ad40519
toggle activation for system fonts (fonts without font faces)
matiasbenedetto May 23, 2023
92eee6c
adding preview controls to installed fonts tab
matiasbenedetto May 23, 2023
5e22396
fixing toogle activate fonts and font faces
matiasbenedetto May 24, 2023
dfa88c9
fix available fonts logic
matiasbenedetto May 24, 2023
00a3da3
UI work
matiasbenedetto May 24, 2023
b735348
avoid the uninstall button if the font was added by a theme
matiasbenedetto May 24, 2023
1b3f290
adding a flag to the theme installed fonts to avoid being removed
matiasbenedetto May 24, 2023
18912e6
fixing is selected logic for google fonts
matiasbenedetto May 24, 2023
9bd78bb
uninstall fonts api endpoint
matiasbenedetto May 24, 2023
9bc2158
dealing with fonts without name
matiasbenedetto May 24, 2023
2f285cf
avoid mixing custom and theme fonts
matiasbenedetto May 24, 2023
abe450a
avoid mixing custom and theme fonts
matiasbenedetto May 24, 2023
9a8d230
google fonts api downloader and updater
matiasbenedetto May 24, 2023
a4f1bfa
fixinf card styles with DEBUG false
matiasbenedetto May 24, 2023
eee8fc2
removing unused function
matiasbenedetto May 25, 2023
5795db2
remove the font families with no font faces
matiasbenedetto May 25, 2023
a34480e
removing unused utils
matiasbenedetto May 25, 2023
b3b8fc3
removing unused imports
matiasbenedetto May 25, 2023
3c08990
using slug instead of name to find fonts
matiasbenedetto May 25, 2023
2c27265
simplify function
matiasbenedetto May 25, 2023
0a75cd6
moving fonts library backend files to its own folder
matiasbenedetto May 25, 2023
d985ab2
adding phpdocs
matiasbenedetto May 25, 2023
ad3fdb3
JS lint
matiasbenedetto May 25, 2023
7bc0c2d
removing card elevation
matiasbenedetto May 25, 2023
9bc4cd5
adding lib-font library to read font metadata tables
matiasbenedetto May 29, 2023
9e16a04
implementing local fonts upload
matiasbenedetto May 29, 2023
cfb645b
CSS fixes for font previews
matiasbenedetto May 29, 2023
f9998c7
add partial selection state for font cards
matiasbenedetto May 30, 2023
b69e8b3
Merge branch 'trunk' into try/font-library
matiasbenedetto May 31, 2023
2f29246
Updating endpoints permissions checks
matiasbenedetto May 31, 2023
67dbd7b
Adding some error handling on api endpoints
matiasbenedetto May 31, 2023
bf1a9eb
Adding error handling and comments in Fonts library php class methods
matiasbenedetto May 31, 2023
4b92a6c
Add mime type check for font file paths
matiasbenedetto Jun 1, 2023
9e0a8b7
Initialize Fonts Library api from it own filee
matiasbenedetto Jun 1, 2023
ee3890c
Adding prefix to installed font slugs to avoid slug collisions
matiasbenedetto Jun 1, 2023
084e014
Move fonts directory file permission check to permission callback of …
matiasbenedetto Jun 1, 2023
64b5ec7
Avoid duplicated files when selecting local fonts to upload
matiasbenedetto Jun 1, 2023
22b0079
Replacing modal opening from dropdown to 2 buttons with icons
matiasbenedetto Jun 2, 2023
1965048
Fonts modal title change
matiasbenedetto Jun 2, 2023
599f1f9
changing main fonts list
matiasbenedetto Jun 2, 2023
111fa8a
Removing action handler and google and installed font card components
matiasbenedetto Jun 2, 2023
bbd4900
Splitting the installed fonts in theme and user fonts sections
matiasbenedetto Jun 2, 2023
e577b4c
hiding preview options
matiasbenedetto Jun 2, 2023
74e4fc1
Moving user fonts at the top of library modal and removing sub heading
matiasbenedetto Jun 6, 2023
457d0a0
Adding tooltip to panel buttons
matiasbenedetto Jun 6, 2023
e59863a
changing installed fonts tab title to library
matiasbenedetto Jun 6, 2023
313e7d8
moving modal is open state and installed font selected state to the c…
matiasbenedetto Jun 6, 2023
3238f8d
Update save buttons text
matiasbenedetto Jun 6, 2023
41785fd
Update tab texts
matiasbenedetto Jun 6, 2023
eb328ba
Updating tab layout title
matiasbenedetto Jun 6, 2023
caf2af0
Changing upload local fonts button
matiasbenedetto Jun 7, 2023
549c9fd
Select the installed font insted of the font stored in custom global …
matiasbenedetto Jun 7, 2023
8ac93e6
moving where alphabetical orderingis done
matiasbenedetto Jun 7, 2023
63b4926
Using custom and theme fonts from context to make profit of alphabeti…
matiasbenedetto Jun 7, 2023
eccebee
Set fallback values for font familiies definitions
matiasbenedetto Jun 7, 2023
fb7282c
encode font asset url if it is not url encoded
matiasbenedetto Jun 7, 2023
1c9e908
Adding infinite scroll to fonts grid component
matiasbenedetto Jun 7, 2023
6b2a0a4
Adding font families json sanitization using WP_Theme_JSON capabilities
matiasbenedetto Jun 9, 2023
12b4ac1
remove lib-font npm dependency and include it in the source code with…
matiasbenedetto Jun 14, 2023
179a769
use content_url instead of site_url
matiasbenedetto Jun 14, 2023
fcf428c
list all the font families from theme json
matiasbenedetto Jun 14, 2023
9f3f39e
Extend WP_Rest_Controller
matiasbenedetto Jun 14, 2023
a34bec8
Add _Controller to PHP class name
matiasbenedetto Jun 21, 2023
b990eac
comment spell fix
matiasbenedetto Jun 21, 2023
b2d0d93
comment spell fix
matiasbenedetto Jun 21, 2023
5b79d35
minor syntax change
matiasbenedetto Jun 21, 2023
04c2c69
comment spelling fix
matiasbenedetto Jun 21, 2023
3d3b958
comment spelling
matiasbenedetto Jun 21, 2023
2dddc5d
comment spelling
matiasbenedetto Jun 21, 2023
abaaa9e
updating class name
matiasbenedetto Jun 21, 2023
bd3ab6d
Refactoring the use of global settings state
matiasbenedetto Jun 21, 2023
a508e8d
TEMPORAL FIX: Disabling add_missing_fonts_to_theme_json() untils this…
matiasbenedetto Jun 21, 2023
d6fb2b4
public php class method
matiasbenedetto Jun 21, 2023
cd3c27f
Merge branch 'trunk' into try/font-library
matiasbenedetto Jun 22, 2023
4df499b
fix after merging latest changes from trunk
matiasbenedetto Jun 22, 2023
95ced36
Declaring class properties
matiasbenedetto Jun 23, 2023
645626f
minor refactor of delete_font_face_assets and delete_asset functions
matiasbenedetto Jun 23, 2023
ed2a0d1
removing line not needed
matiasbenedetto Jun 23, 2023
dd771f1
Removing not needed variable definition
matiasbenedetto Jun 23, 2023
fc69ef1
improving syntax
matiasbenedetto Jun 23, 2023
1a18413
avoid redundant code line
matiasbenedetto Jun 23, 2023
d8adb54
avoiding duplicated code
matiasbenedetto Jun 29, 2023
962c87b
Refactoring to use one post per font family in the library
matiasbenedetto Jul 12, 2023
7a0027f
Refactoring delete font family
matiasbenedetto Jul 12, 2023
dafa05a
avoid adding number to filename when there is only one asset
matiasbenedetto Jul 12, 2023
2fe4ae8
Refactor install fonts endpoint to understand only formdata
matiasbenedetto Jul 13, 2023
7787782
Renaming PHP file
matiasbenedetto Jul 13, 2023
16b5e7c
Spliting backend code in 2 clasess and refactoring
matiasbenedetto Jul 13, 2023
1aaa217
hide preview tools on local fonts upload
matiasbenedetto Jul 13, 2023
6b76e47
Merge branch 'trunk' into try/font-library
matiasbenedetto Jul 13, 2023
8e470fd
Avoid tracking post id in font family class
matiasbenedetto Jul 17, 2023
141f978
Format php.
jffng Jul 17, 2023
6365c22
Update comment.
jffng Jul 17, 2023
006ebde
Fix array key issue with merge.
jffng Jul 17, 2023
8b10c60
rename custom post type to wp_font_family
matiasbenedetto Jul 28, 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
116 changes: 116 additions & 0 deletions bin/fonts/get-google-fonts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* External dependencies
*/
const fs = require( 'fs' );
const crypto = require( 'crypto' );

const API_URL = 'https://www.googleapis.com/webfonts/v1/webfonts?key=';
const API_KEY = process.env.GOOGLE_FONTS_API_KEY;
const GOOGLE_FONTS_FILE_PATH =
'../../lib/experimental/fonts-library/google-fonts.json';

function getCategories( fonts ) {
const categories = new Set();
fonts.forEach( ( font ) => {
categories.add( font.category );
} );
// Returs an array of categories
return [ ...categories ];
}

function calculateHash( somestring ) {
return crypto
.createHash( 'md5' )
.update( somestring )
.digest( 'hex' )
.toString();
}

// Google Fonts API categories mappping to fallback system fonts
const GOOGLE_FONT_FALLBACKS = {
display: 'system-ui',
'sans-serif': 'sans-serif',
serif: 'serif',
handwriting: 'cursive',
monospace: 'monospace',
};

function getStyleFromGoogleVariant( variant ) {
return variant.includes( 'italic' ) ? 'italic' : 'normal';
}

function getWeightFromGoogleVariant( variant ) {
return variant === 'regular' || variant === 'italic'
? '400'
: variant.replace( 'italic', '' );
}

function getFallbackForGoogleFont( googleFontCategory ) {
return GOOGLE_FONT_FALLBACKS[ googleFontCategory ] || 'system-ui';
}

function getFontFamilyFromGoogleFont( font ) {
return {
name: font.family,
fontFamily: `${ font.family }, ${ getFallbackForGoogleFont(
font.category
) }`,
slug: 'wp-font-lib-' + font.family.replace( /\s+/g, '-' ).toLowerCase(),
category: font.category,
fontFace: font.variants.map( ( variant ) => ( {
src: font.files?.[ variant ],
fontWeight: getWeightFromGoogleVariant( variant ),
fontStyle: getStyleFromGoogleVariant( variant ),
fontFamily: font.family,
} ) ),
};
}

async function updateFiles() {
let newApiData;
let newData;
let response;

try {
newApiData = await fetch( `${ API_URL }${ API_KEY }` );
response = await newApiData.json();
const fontFamilies = response.items.map( getFontFamilyFromGoogleFont );
const categories = getCategories( response.items );
newData = { fontFamilies, categories };
} catch ( error ) {
// TODO: show in UI and remove console statement
// eslint-disable-next-line
console.error( '❎ Error fetching the Google Fonts API:', error );
process.exit( 1 );
}

if ( response.items ) {
const newDataString = JSON.stringify( newData, null, 2 );

const oldFileData = fs.readFileSync( GOOGLE_FONTS_FILE_PATH, 'utf8' );
const oldData = JSON.parse( oldFileData );
const oldDataString = JSON.stringify( oldData, null, 2 );

if (
calculateHash( newDataString ) !== calculateHash( oldDataString )
) {
fs.writeFileSync( GOOGLE_FONTS_FILE_PATH, newDataString );
// TODO: show in UI and remove console statement
// eslint-disable-next-line
console.info( '✅ Google Fonts JSON file updated' );
} else {
// TODO: show in UI and remove console statement
// eslint-disable-next-line
console.info( 'ℹ️ Google Fonts JSON file is up to date' );
}
} else {
// TODO: show in UI and remove console statement
// eslint-disable-next-line
console.error(
'❎ No new data to check. Check the Google Fonts API key.'
);
process.exit( 1 );
}
}

updateFiles();
2 changes: 1 addition & 1 deletion lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public static function get_theme_data( $deprecated = array(), $options = array()

// BEGIN OF EXPERIMENTAL CODE. Not to backport to core.
if ( ! class_exists( 'WP_Font_Face' ) && class_exists( 'WP_Fonts_Resolver' ) ) {
static::$theme = WP_Fonts_Resolver::add_missing_fonts_to_theme_json( static::$theme );
// static::$theme = WP_Fonts_Resolver::add_missing_fonts_to_theme_json( static::$theme );
}
// END OF EXPERIMENTAL CODE.

Expand Down
Loading
Loading