Make WordPress Core

Changeset 54772


Ignore:
Timestamp:
11/09/2022 01:06:46 AM (4 years ago)
Author:
flixos90
Message:

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.
Merges [54769] to the 6.1 branch.
Fixes #56815.

Location:
branches/6.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

  • branches/6.1/src/wp-includes/block-editor.php

    r54291 r54772  
    154154function 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
  • branches/6.1/tests/phpunit/tests/blocks/editor.php

    r54162 r54772  
    300300                $this->assertIsInt( $settings['maxUploadFileSize'] );
    301301                $this->assertTrue( $settings['__unstableGalleryWithImageBlocks'] );
     302        }
     303
     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'] );
    302327        }
    303328
Note: See TracChangeset for help on using the changeset viewer.