Skip to content

Commit b8bf297

Browse files
committed
Editor: Improve frontend performance for get_default_block_editor_settings().
The `wp_max_upload_size()` function can be expensive to call, especially for large sites or multisites. For the frontend usage of `get_default_block_editor_settings()` knowing the allowed upload size is typically unnecessary. This changeset adds a condition so that `wp_max_upload_size()` is only called if the current user can actually `upload_files`. It keeps the data present when it is actually needed while avoiding the execution overhead when it is not needed. Props janthiel, Clorith, flixos90, spacedmonkey. Fixes #56815. git-svn-id: https://develop.svn.wordpress.org/trunk@54769 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fba9680 commit b8bf297

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/wp-includes/block-editor.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ function get_allowed_block_types( $block_editor_context ) {
153153
*/
154154
function get_default_block_editor_settings() {
155155
// Media settings.
156-
$max_upload_size = wp_max_upload_size();
157-
if ( ! $max_upload_size ) {
158-
$max_upload_size = 0;
156+
157+
// wp_max_upload_size() can be expensive, so only call it when relevant for the current user.
158+
$max_upload_size = 0;
159+
if ( current_user_can( 'upload_files' ) ) {
160+
$max_upload_size = wp_max_upload_size();
161+
if ( ! $max_upload_size ) {
162+
$max_upload_size = 0;
163+
}
159164
}
160165

161166
/** This filter is documented in wp-admin/includes/media.php */

tests/phpunit/tests/blocks/editor.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,31 @@ public function test_get_default_block_editor_settings() {
301301
$this->assertTrue( $settings['__unstableGalleryWithImageBlocks'] );
302302
}
303303

304+
/**
305+
* @ticket 56815
306+
*/
307+
public function test_get_default_block_editor_settings_max_upload_file_size() {
308+
// Force the return value of wp_max_upload_size() to be 500.
309+
add_filter(
310+
'upload_size_limit',
311+
function() {
312+
return 500;
313+
}
314+
);
315+
316+
// Expect 0 when user is not allowed to upload (as wp_max_upload_size() should not be called).
317+
$settings = get_default_block_editor_settings();
318+
$this->assertSame( 0, $settings['maxUploadFileSize'] );
319+
320+
// Set up an administrator, as they can upload files.
321+
$administrator = self::factory()->user->create( array( 'role' => 'administrator' ) );
322+
wp_set_current_user( $administrator );
323+
324+
// Expect the above 500 as the user is now allowed to upload.
325+
$settings = get_default_block_editor_settings();
326+
$this->assertSame( 500, $settings['maxUploadFileSize'] );
327+
}
328+
304329
/**
305330
* @ticket 53397
306331
*/

0 commit comments

Comments
 (0)