Make WordPress Core


Ignore:
Timestamp:
11/09/2022 12:28:33 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.
Fixes #56815.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/editor.php

    r54162 r54769  
    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.