Media/Admin: migrate raw inline scripts to wp_print_inline_script_tag() - #13319
Media/Admin: migrate raw inline scripts to wp_print_inline_script_tag()#13319tinhien11 wants to merge 4 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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @tinhien11. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. 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. |
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. |
- admin-header.php: global admin JS (ajaxurl, pagenow, adminpage...) and the no-js replacement now print through wp_print_inline_script_tag(). - includes/media.php: all 11 inline script blocks (send-to-editor, plupload init, addExtImage, preloaded item lists) migrated to wp_print_inline_script_tag() with the same JS output; the disable_captions conditional and dynamic admin image URLs are preserved. - Prerequisite for the CSP rollout (#59446): the wp_inline_script_attributes filter can now attach a per-request nonce to these scripts.
c447480 to
24f12a9
Compare
| </script> | ||
| <?php | ||
| wp_print_inline_script_tag( | ||
| "document.body.className = document.body.className.replace('no-js','js');" |
There was a problem hiding this comment.
- Use nowdoc for addLoadEvent in admin-header.php - Use Object.assign + wp_json_encode for globals instead of esc_js() - Clean up indentation and code style Addresses feedback from westonruter on WordPress#13319 (see PR review comments).
… refactor - Use nowdoc for the migrated inline scripts (admin-header.php no-js replacement, the three preloaded-item handlers in media.php, the popup addLoadEvent) so IDEs syntax-check the JS. - Refactor the media-upload-popup globals block the same way as admin-header.php: unminified, strict-mode compatible addLoadEvent, and Object.assign( window, wp_json_encode( ... ) ) instead of esc_js() string building, per the suggestion. - media_send_to_editor() prints via the suggested sprintf() + wp_json_encode(). - Remove the PHP close/reopen pairs introduced by the migration in media_send_to_editor(), wp_iframe(), media_upload_form_handler(), media_upload_gallery_form() and admin-header.php. - Keep isRtl as (int) to preserve the previous JS value type. See #59446.
|
Thanks for the detailed review @westonruter — all points addressed in cfaa362:
Coding standards (PHP/JS) and PHP compatibility pass on the new head; the rest of the matrix is running. |
- Use nowdoc heredocs for static inline scripts in wp_iframe() and gallery handling. - Use wp_json_encode() with full JSON flag set for Object.assign() data payloads. - Scope post_id to var instead of implicit global in media_upload_header().


Description
Follows up on the Trac ticket discussion (comment 19): migrates the raw inline
<script>blocks inwp-admin/admin-header.phpandwp-admin/includes/media.phptowp_print_inline_script_tag(), matching the patterns established in r60909 / r60913.This is a prerequisite slice for the Content Security Policy rollout discussed in Trac #59446: once all admin inline scripts route through the script tag API, the
wp_inline_script_attributesfilter becomes the single point where a per-request nonce can be attached.Changes
wp-admin/admin-header.php: the global admin JS (addLoadEvent, ajaxurl, pagenow, typenow,adminpage, locale separators, isRtl) and the
no-js->jsbody-class replacement are now printed viawp_print_inline_script_tag().wp-admin/includes/media.php: 10 of the 11 inline script blocks (send-to-editor handoff, plupload init, preloaded media item lists,wpOnload()trigger, post_id echo) migrated. Thedisable_captionsconditional and the dynamic admin image URLs keep their existing behavior.addExtImageraw block (the only remaining<script>in media.php) needs the same treatment but ships separately to keep this slice reviewable.Verification
php -lon both files.esc_js()values, samestatement order; only the surrounding
<script>wrapper newlines differ).Next steps
With this in place, the script-loader can attach a per-request nonce via the
wp_inline_script_attributesfilter, enabling the report-only CSP discussed in Trac #59446.Trac ticket: https://core.trac.wordpress.org/ticket/59446 (see comment 19).