-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: font family and font uploads capability checks #59294
Changes from 4 commits
9e11bd9
7846623
314cad6
efa01ec
dc57cd4
3da8387
84b54ed
5fe3776
9867c3e
0800644
ed93188
047662a
7d44a12
090c7a0
b1fb5db
1dc3153
8b1663f
9925ddd
1edf1ac
742d091
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,35 @@ function gutenberg_create_initial_post_types() { | |
); | ||
} | ||
|
||
/** | ||
* Filters the user capabilities to grant the 'upload_fonts' capability as necessary. | ||
* | ||
* To grant the 'upload_fonts' capability, file modifications must be allowed, the fonts directory must be | ||
* writable, and the user must have the 'edit_theme_options' capability. | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @param bool[] $allcaps An array of all the user's capabilities. | ||
peterwilsoncc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @return bool[] Filtered array of the user's capabilities. | ||
*/ | ||
function gutenberg_maybe_grant_upload_font_cap( $allcaps, $caps ) { | ||
peterwilsoncc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( ! in_array( 'upload_fonts', $caps, true ) ) { | ||
return $allcaps; | ||
} | ||
|
||
$fonts_dir = wp_get_font_dir()['path']; | ||
if ( | ||
wp_is_file_mod_allowed( 'can_upload_fonts' ) && | ||
wp_is_writable( $fonts_dir ) && | ||
! empty( $allcaps['edit_theme_options'] ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Developers can modify the cap required for editing the font face/families (similar to the example in #59104 (comment) ) so I am not sure this is required.
However, I'm not entirely sure it is not required, either, so will require a logic check on this. |
||
) { | ||
$allcaps['upload_fonts'] = true; | ||
} | ||
|
||
return $allcaps; | ||
} | ||
add_filter( 'user_has_cap', 'gutenberg_maybe_grant_upload_font_cap', 10, 2 ); | ||
|
||
/** | ||
* Initializes REST routes. | ||
* | ||
|
@@ -303,6 +332,21 @@ function _wp_before_delete_font_face( $post_id, $post ) { | |
add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 ); | ||
} | ||
|
||
/** | ||
* Filters the block editor settings to enable or disable font uploads according to user capability. | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @param array $settings Block editor settings. | ||
* @return array Block editor settings. | ||
*/ | ||
function gutenberg_font_uploads_settings( $settings ) { | ||
$settings['fontUploadsEnabled'] = current_user_can( 'upload_fonts' ); | ||
|
||
return $settings; | ||
} | ||
add_filter( 'block_editor_settings_all', 'gutenberg_font_uploads_settings' ); | ||
|
||
// @core-merge: Do not merge this back compat function, it is for supporting a legacy font family format only in Gutenberg. | ||
/** | ||
* Convert legacy font family posts to the new format. | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,9 @@ import { | |||||
Modal, | ||||||
privateApis as componentsPrivateApis, | ||||||
} from '@wordpress/components'; | ||||||
import { store as coreStore } from '@wordpress/core-data'; | ||||||
import { useSelect } from '@wordpress/data'; | ||||||
import { store as editorStore } from '@wordpress/editor'; | ||||||
import { useContext } from '@wordpress/element'; | ||||||
|
||||||
/** | ||||||
|
@@ -19,16 +22,15 @@ import { unlock } from '../../../lock-unlock'; | |||||
|
||||||
const { Tabs } = unlock( componentsPrivateApis ); | ||||||
|
||||||
const DEFAULT_TABS = [ | ||||||
{ | ||||||
id: 'installed-fonts', | ||||||
title: __( 'Library' ), | ||||||
}, | ||||||
{ | ||||||
id: 'upload-fonts', | ||||||
title: __( 'Upload' ), | ||||||
}, | ||||||
]; | ||||||
const DEFAULT_TAB = { | ||||||
id: 'installed-fonts', | ||||||
title: __( 'Library' ), | ||||||
}; | ||||||
|
||||||
const UPLOAD_TAB = { | ||||||
id: 'upload-fonts', | ||||||
title: __( 'Upload' ), | ||||||
}; | ||||||
|
||||||
const tabsFromCollections = ( collections ) => | ||||||
collections.map( ( { slug, name } ) => ( { | ||||||
|
@@ -44,11 +46,23 @@ function FontLibraryModal( { | |||||
defaultTabId = 'installed-fonts', | ||||||
} ) { | ||||||
const { collections, setNotice } = useContext( FontLibraryContext ); | ||||||
const canUserCreate = useSelect( ( select ) => { | ||||||
const { canUser } = select( coreStore ); | ||||||
return canUser( 'create', 'font-families' ); | ||||||
}, [] ); | ||||||
const fontUploadsEnabled = useSelect( ( select ) => { | ||||||
const { getEditorSettings } = select( editorStore ); | ||||||
return getEditorSettings().fontUploadsEnabled; | ||||||
}, [] ); | ||||||
|
||||||
const tabs = [ DEFAULT_TAB ]; | ||||||
|
||||||
const tabs = [ | ||||||
...DEFAULT_TABS, | ||||||
...tabsFromCollections( collections || [] ), | ||||||
]; | ||||||
if ( canUserCreate && fontUploadsEnabled ) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should check only
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @matiasbenedetto Are you able to expand on this a little. I'm not seeing the ability to add System fonts when running Gutenberg. With the recent changes I can see the Google Fonts tabs but without the ability to upload fonts I can go through the process to install only to hit an inactive install button once I try to make use of them. It's a confusing experience. |
||||||
tabs.push( UPLOAD_TAB ); | ||||||
// In the future, font faces can be configured to use a remote url rather than a file upload as the font src property. | ||||||
// In that case, collections tabs should still be shown when font uploads are disabled. | ||||||
peterwilsoncc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
tabs.push( ...tabsFromCollections( collections || [] ) ); | ||||||
} | ||||||
|
||||||
// Reset notice when new tab is selected. | ||||||
const onSelect = () => { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { SETTINGS_DEFAULTS } from '@wordpress/block-editor'; | |
* @property {boolean} richEditingEnabled Whether rich editing is enabled or not | ||
* @property {boolean} codeEditingEnabled Whether code editing is enabled or not | ||
* @property {boolean} fontLibraryEnabled Whether the font library is enabled or not. | ||
* @property {boolean} fontUploadsEnabled Whether uploading fonts in the font library is enabled or not. | ||
* @property {boolean} enableCustomFields Whether the WordPress custom fields are enabled or not. | ||
* true = the user has opted to show the Custom Fields panel at the bottom of the editor. | ||
* false = the user has opted to hide the Custom Fields panel at the bottom of the editor. | ||
|
@@ -28,6 +29,7 @@ export const EDITOR_SETTINGS_DEFAULTS = { | |
richEditingEnabled: true, | ||
codeEditingEnabled: true, | ||
fontLibraryEnabled: true, | ||
fontUploadsEnabled: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably safer to assume false as the default if the filter is removed. That will avoid the UI showing only to display an error if the caps check fails. |
||
enableCustomFields: undefined, | ||
defaultRenderingMode: 'post-only', | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
delete_post
meta capability will need to account for deleting fonts with files attached. This is to account for the file deletion hooks inwp_delete_post()
.I'm not in love with the performance aspect of this code when checking the font family post type but can't think of more performant approach.