Skip to content

Customize: Use nowdocs for WP_Customize_Manager inline scripts - #16325

Draft
shail-mehta wants to merge 2 commits into
WordPress:trunkfrom
shail-mehta:add/66856-customize-manager-inline-js
Draft

shail-mehta wants to merge 2 commits into
WordPress:trunkfrom
shail-mehta:add/66856-customize-manager-inline-js

Conversation

@shail-mehta

@shail-mehta shail-mehta commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

What

Trac ticket: #66856

Converts the four WP_Customize_Manager inline scripts that used an ob_start() template buffer with wp_remove_surrounding_empty_script_tags() to wp_print_inline_script_tag() / wp_get_inline_script_tag() with <<<'JS' nowdocs:

  • WP_Customize_Manager::wp_die()
  • WP_Customize_Manager::remove_frameless_preview_messenger_channel()
  • WP_Customize_Manager::customize_preview_settings()
  • WP_Customize_Manager::customize_pane_settings()

Why

#66856 lists these four methods as remaining ob_start() + wp_remove_surrounding_empty_script_tags() blocks that should use nowdocs and direct string construction.

How

  • wp_die(): the messenger error script is a <<<'JS' nowdoc arrow function receiving $settings through wp_json_encode( ..., JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ). The ob_start() buffer for wp_enqueue_scripts() and wp_print_scripts() output is kept.
  • remove_frameless_preview_messenger_channel(): the static script is passed directly to wp_print_inline_script_tag() as a <<<'JS' nowdoc.
  • customize_preview_settings(): static setup and closing lines are <<<'JS' nowdocs. Only the per-setting v[...] lines are built with sprintf().
  • customize_pane_settings(): static initialization and the (function ( s ){, (function ( c ){, and closing lines are <<<'JS' nowdocs. Per-setting and per-control lines are still built one at a time to avoid peak memory spikes.
  • \n//# sourceURL= is preserved on all four inline scripts.

Testing Instructions

  1. Run php -l src/wp-includes/class-wp-customize-manager.php and confirm there are no syntax errors.
  2. Open Appearance → Customize (wp-admin/customize.php), confirm the Customizer pane and preview iframe load normally, and verify changes preview and publish with no console errors.
  3. Open a frontend URL with ?customize_messenger_channel=preview-0 outside an iframe, and confirm the parameter is removed via location.replace().
  4. View page source on the Customizer and confirm the _wpCustomizeSettings and sourceURL inline scripts are printed correctly.

Use of AI Tools

  • Cursor

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the Customizer inline-script generation refactor.

No open issues 🎉

What changed in this PR

Refactors inline-script generation in WP_Customize_Manager to use nowdocs and direct string construction, addressing the four methods identified in Trac #66856.

Changes:

  • Replaces buffered <script> blocks and surrounding-tag removal with directly constructed JavaScript.
  • Retains one-by-one setting and control serialization to limit peak memory usage.
  • Preserves inline-script helpers and sourceURL annotations.
File Description
src/​wp-includes/​class-wp-customize-manager.php Updates script construction for error messaging, frameless-preview redirects, and preview/pane settings.

Review effort: Balanced

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the inline-script generation refactor.

No open issues 🎉

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants