diff --git a/lib/compat/wordpress-6.5/fonts/fonts.php b/lib/compat/wordpress-6.5/fonts/fonts.php index 1f29645a9c2b90..bb43a933b6b4a5 100644 --- a/lib/compat/wordpress-6.5/fonts/fonts.php +++ b/lib/compat/wordpress-6.5/fonts/fonts.php @@ -32,14 +32,14 @@ function gutenberg_create_initial_post_types() { 'capabilities' => array( 'read' => 'edit_theme_options', 'read_private_posts' => 'edit_theme_options', - 'create_posts' => 'edit_theme_options', + 'create_posts' => 'install_font_faces', 'publish_posts' => 'edit_theme_options', 'edit_posts' => 'edit_theme_options', 'edit_others_posts' => 'edit_theme_options', 'edit_published_posts' => 'edit_theme_options', - 'delete_posts' => 'edit_theme_options', - 'delete_others_posts' => 'edit_theme_options', - 'delete_published_posts' => 'edit_theme_options', + 'delete_posts' => 'delete_font_faces', + 'delete_others_posts' => 'delete_font_faces', + 'delete_published_posts' => 'delete_font_faces', ), 'map_meta_cap' => true, 'query_var' => false, @@ -64,14 +64,14 @@ function gutenberg_create_initial_post_types() { 'capabilities' => array( 'read' => 'edit_theme_options', 'read_private_posts' => 'edit_theme_options', - 'create_posts' => 'edit_theme_options', + 'create_posts' => 'install_font_faces', 'publish_posts' => 'edit_theme_options', 'edit_posts' => 'edit_theme_options', 'edit_others_posts' => 'edit_theme_options', 'edit_published_posts' => 'edit_theme_options', - 'delete_posts' => 'edit_theme_options', - 'delete_others_posts' => 'edit_theme_options', - 'delete_published_posts' => 'edit_theme_options', + 'delete_posts' => 'delete_font_faces', + 'delete_others_posts' => 'delete_font_faces', + 'delete_published_posts' => 'delete_font_faces', ), 'map_meta_cap' => true, 'query_var' => false, @@ -84,6 +84,32 @@ function gutenberg_create_initial_post_types() { ); } +/** + * Filters the user capabilities to grant the font family capabilities as necessary. + * + * Files must be modifiable to grant these capabilities and the user must als + * have the `edit_theme_options` capability. + * + * These are created as faux primitive capabilities to allow for the use + * if the delete_post meta capability. + * + * @param bool[] $allcaps An array of all the user's capabilities. + * @return bool[] Filtered array of the user's capabilities. + */ +function gutenberg_maybe_grant_font_family_caps( $allcaps ) { + if ( ! wp_is_file_mod_allowed( 'can_modify_font_faces' ) ) { + return $allcaps; + } + + if ( ! empty( $allcaps['edit_theme_options'] ) ) { + $allcaps['install_font_faces'] = true; + $allcaps['delete_font_faces'] = true; + } + + return $allcaps; +} +add_filter( 'user_has_cap', 'gutenberg_maybe_grant_font_family_caps' ); + /** * Initializes REST routes. * diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/index.js b/packages/edit-site/src/components/global-styles/font-library-modal/index.js index 71966449eb616d..5a81b7f39fada5 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/index.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/index.js @@ -6,6 +6,8 @@ import { Modal, privateApis as componentsPrivateApis, } from '@wordpress/components'; +import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; import { useContext } from '@wordpress/element'; /** @@ -24,6 +26,9 @@ const DEFAULT_TABS = [ id: 'installed-fonts', title: __( 'Library' ), }, +]; + +const UPLOAD_TABS = [ { id: 'upload-fonts', title: __( 'Upload' ), @@ -45,9 +50,15 @@ function FontLibraryModal( { } ) { const { collections, setNotice } = useContext( FontLibraryContext ); + const canUserCreate = useSelect( ( select ) => { + const { canUser } = select( coreStore ); + return canUser( 'create', 'font-families' ); + } ); + const tabs = [ ...DEFAULT_TABS, - ...tabsFromCollections( collections || [] ), + ...( canUserCreate ? UPLOAD_TABS : [] ), + ...( canUserCreate ? tabsFromCollections( collections || [] ) : [] ), ]; // Reset notice when new tab is selected. diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js index 99c99e44a43afc..48e27e777fce9a 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js @@ -12,6 +12,8 @@ import { Spinner, FlexItem, } from '@wordpress/components'; +import { store as coreStore } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; /** * Internal dependencies @@ -100,6 +102,7 @@ function InstalledFonts() { handleBack={ !! libraryFontSelected && handleUnselectFont } footer={ @@ -170,14 +173,30 @@ function InstalledFonts() { ); } -function Footer( { shouldDisplayDeleteButton, handleUninstallClick } ) { +function Footer( { + shouldDisplayDeleteButton, + handleUninstallClick, + libraryFontSelected, +} ) { const { saveFontFamilies, fontFamiliesHasChanges, isInstalling } = useContext( FontLibraryContext ); + + const customFontFamilyId = + libraryFontSelected?.source === 'custom' && libraryFontSelected?.id; + + const canUserDelete = useSelect( ( select ) => { + const { canUser } = select( coreStore ); + return ( + customFontFamilyId && + canUser( 'delete', 'font-families', customFontFamilyId ) + ); + } ); + return ( { isInstalling && } - { shouldDisplayDeleteButton && ( + { canUserDelete && shouldDisplayDeleteButton && ( { + const { canUser } = select( coreStore ); + return canUser( 'create', 'font-families' ); + } ); + const handleDropZone = ( files ) => { handleFilesUpload( files ); }; @@ -171,41 +178,43 @@ function UploadFonts() { }; return ( - - - - { isUploading && ( - - - - - - ) } - { ! isUploading && ( - `.${ ext }` - ).join( ',' ) } - multiple={ true } - onChange={ onFilesUpload } - render={ ( { openFileDialog } ) => ( - - { __( 'Upload font' ) } - - ) } - /> - ) } - - - { __( - 'Uploaded fonts appear in your library and can be used in your theme. Supported formats: .tff, .otf, .woff, and .woff2.' + canUserCreate && ( + + + + { isUploading && ( + + + + + ) } - - - + { ! isUploading && ( + `.${ ext }` + ).join( ',' ) } + multiple={ true } + onChange={ onFilesUpload } + render={ ( { openFileDialog } ) => ( + + { __( 'Upload font' ) } + + ) } + /> + ) } + + + { __( + 'Uploaded fonts appear in your library and can be used in your theme. Supported formats: .tff, .otf, .woff, and .woff2.' + ) } + + + + ) ); }