Skip to content

Commit

Permalink
Falls back to using remote font urls if uploading is not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder authored and getdave committed Feb 22, 2024
1 parent 3f62466 commit 2e1d6c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ public function create_item( $request ) {
$settings = $request->get_param( 'font_face_settings' );
$file_params = $request->get_file_params();

if ( ! empty( $file_params ) && ! $this->can_upload_fonts() ) {
return new WP_Error(
'rest_cannot_upload_fonts',
__( 'You are not allowed to upload font files.', 'gutenberg' ),
array( 'status' => 403 )
);
}

// Check that the necessary font face properties are unique.
$query = new WP_Query(
array(
Expand Down Expand Up @@ -903,6 +911,18 @@ public function handle_font_file_upload_error( $file, $message ) {
return new WP_Error( $code, $message, array( 'status' => $status ) );
}

/**
* Checks if fonts can be uploaded to the site.
*
* @since 6.5.0
*
* @return bool Whether font assets can be upload.
*/
protected function can_upload_fonts() {
$fonts_dir = wp_get_font_dir()['path'];
return wp_is_file_mod_allowed( 'can_upload_fonts' ) && wp_is_writable( $fonts_dir );
}

/**
* Returns relative path to an uploaded font file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
import { debounce } from '@wordpress/compose';
import { sprintf, __, _x } from '@wordpress/i18n';
import { search, closeSmall } from '@wordpress/icons';
import { store as editorStore } from '@wordpress/editor';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -161,13 +163,13 @@ function FontCollection( { slug } ) {
setFontsToInstall( [] );
};

const handleInstall = async () => {
const handleInstall = async ( shouldUpload = true ) => {
setNotice( null );

const fontFamily = fontsToInstall[ 0 ];

try {
if ( fontFamily?.fontFace ) {
if ( fontFamily?.fontFace && shouldUpload ) {
await Promise.all(
fontFamily.fontFace.map( async ( fontFace ) => {
if ( fontFace.src ) {
Expand Down Expand Up @@ -398,12 +400,17 @@ function PaginationFooter( { page, totalPages, setPage } ) {

function InstallFooter( { handleInstall, isDisabled } ) {
const { isInstalling } = useContext( FontLibraryContext );
const fontUploadEnabled = useSelect(
( select ) =>
select( editorStore ).getEditorSettings().fontUploadEnabled,
[]
);

return (
<Flex justify="flex-end">
<Button
variant="primary"
onClick={ handleInstall }
onClick={ () => handleInstall( fontUploadEnabled ) }
isBusy={ isInstalling }
disabled={ isDisabled || isInstalling }
__experimentalIsFocusable
Expand Down

0 comments on commit 2e1d6c6

Please sign in to comment.