Script Loader: Guard compression_test() with manage_options check - #12748
Script Loader: Guard compression_test() with manage_options check#12748irozum wants to merge 2 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Pull request overview
This PR updates the Script Loader compression test so it only outputs its inline <script> block for users who can actually complete the related AJAX flow (i.e., users with the manage_options capability), aligning runtime behavior with the existing docblock and the capability check already enforced by the wp_ajax_wp_compression_test() handler.
Changes:
- Add an early
current_user_can( 'manage_options' )guard incompression_test()to prevent output for non-privileged wp-admin users. - Add PHPUnit coverage to verify
compression_test()outputs nothing for a user withoutmanage_options, and still outputs the test script for a user with it.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/wp-admin/includes/template.php |
Adds a manage_options capability guard to prevent emitting the compression-test inline script for non-admin-capable users. |
tests/phpunit/tests/admin/includesTemplate.php |
Adds tests asserting compression_test() output is empty for an Editor and contains the test script for an Administrator. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
@irozum Maybe you should add your modification at the root: wordpress-develop/src/wp-admin/admin-footer.php Lines 107 to 112 in d7de266 |
| ob_start(); | ||
| compression_test(); | ||
| $output = ob_get_clean(); |
There was a problem hiding this comment.
| ob_start(); | |
| compression_test(); | |
| $output = ob_get_clean(); | |
| $output = get_echo( 'compression_test' ); |
| ob_start(); | ||
| compression_test(); | ||
| $output = ob_get_clean(); |
There was a problem hiding this comment.
| ob_start(); | |
| compression_test(); | |
| $output = ob_get_clean(); | |
| $output = get_echo( 'compression_test' ); |
compression_test()is called fromwp-admin/admin-footer.phpfor every user who can access wp-admin, whenever thecan_compress_scriptsoption is unset — not only administrators. It outputs a<script>block that fires AJAX requests to thewp-compression-testaction, but that AJAX handler (wp_ajax_wp_compression_test()) requires themanage_optionscapability, so non-admin users (e.g. Editors) get-1responses and can never actually resolve the test. This also contradicts the function's own docblock, which states it "Has no effect when the current user is not an administrator."This PR adds a
current_user_can( 'manage_options' )check at the top ofcompression_test()so it returns early — and outputs nothing — for users who can't act on the result, matching the documented behavior. Administrators are unaffected.Trac ticket: https://core.trac.wordpress.org/ticket/65750
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.