Skip to content
Closed
42 changes: 34 additions & 8 deletions lib/compat/wordpress-6.5/fonts/fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Files must be modifiable to grant these capabilities and the user must als
* Files must be modifiable to grant these capabilities and the user must also

* have the `edit_theme_options` capability.
*
* These are created as faux primitive capabilities to allow for the use
* if the delete_post meta capability.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* if the delete_post meta capability.
* of 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -24,6 +26,9 @@ const DEFAULT_TABS = [
id: 'installed-fonts',
title: __( 'Library' ),
},
];

const UPLOAD_TABS = [
{
id: 'upload-fonts',
title: __( 'Upload' ),
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,6 +102,7 @@ function InstalledFonts() {
handleBack={ !! libraryFontSelected && handleUnselectFont }
footer={
<Footer
libraryFontSelected={ libraryFontSelected }
shouldDisplayDeleteButton={ shouldDisplayDeleteButton }
handleUninstallClick={ handleUninstallClick }
/>
Expand Down Expand Up @@ -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 (
<HStack justify="flex-end">
{ isInstalling && <ProgressBar /> }
<div>
{ shouldDisplayDeleteButton && (
{ canUserDelete && shouldDisplayDeleteButton && (
<Button
isDestructive
variant="tertiary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
FlexItem,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useContext, useState } from '@wordpress/element';

/**
Expand All @@ -31,6 +33,11 @@ function UploadFonts() {
const { installFont, notice, setNotice } = useContext( FontLibraryContext );
const [ isUploading, setIsUploading ] = useState( false );

const canUserCreate = useSelect( ( select ) => {
const { canUser } = select( coreStore );
return canUser( 'create', 'font-families' );
} );

const handleDropZone = ( files ) => {
handleFilesUpload( files );
};
Expand Down Expand Up @@ -171,41 +178,43 @@ function UploadFonts() {
};

return (
<TabPanelLayout notice={ notice }>
<DropZone onFilesDrop={ handleDropZone } />
<VStack className="font-library-modal__local-fonts">
{ isUploading && (
<FlexItem>
<div className="font-library-modal__upload-area">
<ProgressBar />
</div>
</FlexItem>
) }
{ ! isUploading && (
<FormFileUpload
accept={ ALLOWED_FILE_EXTENSIONS.map(
( ext ) => `.${ ext }`
).join( ',' ) }
multiple={ true }
onChange={ onFilesUpload }
render={ ( { openFileDialog } ) => (
<Button
className="font-library-modal__upload-area"
onClick={ openFileDialog }
>
{ __( 'Upload font' ) }
</Button>
) }
/>
) }
<Spacer margin={ 2 } />
<Text className="font-library-modal__upload-area__text">
{ __(
'Uploaded fonts appear in your library and can be used in your theme. Supported formats: .tff, .otf, .woff, and .woff2.'
canUserCreate && (
<TabPanelLayout notice={ notice }>
<DropZone onFilesDrop={ handleDropZone } />
<VStack className="font-library-modal__local-fonts">
{ isUploading && (
<FlexItem>
<div className="font-library-modal__upload-area">
<ProgressBar />
</div>
</FlexItem>
) }
</Text>
</VStack>
</TabPanelLayout>
{ ! isUploading && (
<FormFileUpload
accept={ ALLOWED_FILE_EXTENSIONS.map(
( ext ) => `.${ ext }`
).join( ',' ) }
multiple={ true }
onChange={ onFilesUpload }
render={ ( { openFileDialog } ) => (
<Button
className="font-library-modal__upload-area"
onClick={ openFileDialog }
>
{ __( 'Upload font' ) }
</Button>
) }
/>
) }
<Spacer margin={ 2 } />
<Text className="font-library-modal__upload-area__text">
{ __(
'Uploaded fonts appear in your library and can be used in your theme. Supported formats: .tff, .otf, .woff, and .woff2.'
) }
</Text>
</VStack>
</TabPanelLayout>
)
);
}

Expand Down