Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Adds meta cap for upload_fonts and gates REST API font uploads
  • Loading branch information
creativecoder committed Feb 22, 2024
commit 9e11bd92d4403cbf8fe8c9b2230ef8b3f2803c25
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 ) && ! current_user_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
29 changes: 29 additions & 0 deletions lib/compat/wordpress-6.5/fonts/fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, files modifications must be allowed, the fonts directory must be
* writable, and the user must have the 'edit_theme_options' capability.
*
* @since 5.6.0
*
* @param bool[] $allcaps An array of all the user's capabilities.
* @return bool[] Filtered array of the user's capabilities.
*/
function gutenberg_maybe_grant_upload_font_cap( $allcaps, $caps ) {
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'] )
) {
$allcaps['upload_fonts'] = true;
}

return $allcaps;
}
add_filter( 'user_has_cap', 'gutenberg_maybe_grant_upload_font_cap', 10, 2 );

/**
* Initializes REST routes.
*
Expand Down