Skip to content

Fix TypeError crash when window.aql.allowedControls is undefined#156

Merged
ryanwelcher merged 1 commit intotrunkfrom
fix/allowed-controls-undefined
Feb 25, 2026
Merged

Fix TypeError crash when window.aql.allowedControls is undefined#156
ryanwelcher merged 1 commit intotrunkfrom
fix/allowed-controls-undefined

Conversation

@ryanwelcher
Copy link
Owner

@ryanwelcher ryanwelcher commented Feb 23, 2026

Summary

Fixes a TypeError: Cannot read properties of undefined (reading 'split') crash reported in #144 and #138.

  • Root cause: window.aql.allowedControls is undefined when controls.js tries to call .split(',') on it. Most likely triggered by caching/optimization plugins (e.g. WP Rocket, W3 Total Cache) that reorder script loading, causing the PHP inline script to execute before window.aql is initialized by the main bundle.
  • PHP inline script now safely initializes window.aql before setting allowedControls, preventing a ReferenceError if the inline script runs out of order
  • JS now uses optional chaining with an ALL_CONTROLS fallback so controls still render when allowedControls is unavailable, rather than crashing or silently hiding all controls

Changes

  • includes/enqueues.php — Changed aql.allowedControls = "..." to window.aql = window.aql || {}; window.aql.allowedControls = "..." so window.aql is safely initialized regardless of script load order
  • src/variations/controls.js — Added ?? {} to the window?.aql destructure and ?? ALL_CONTROLS fallback on .split() so the editor never crashes even if the inline script fails to run

Test plan

  • Install a script optimization plugin (e.g. WP Rocket or Autoptimize) and confirm no TypeError in the block editor console when using an AQL block
  • Confirm all AQL controls still appear normally without any optimization plugin active
  • Confirm the aql_allowed_controls PHP filter still correctly restricts visible controls

Closes #144
Closes #138

Guards against script loading order issues (e.g. from caching/optimization
plugins) that can cause the inline script setting allowedControls to run
before or after window.aql is initialized.

- PHP: Initialize window.aql safely before setting allowedControls
- JS: Use optional chaining with ALL_CONTROLS fallback so controls still
  render when allowedControls is unavailable instead of crashing

Fixes #144, #138
@ryanwelcher ryanwelcher merged commit 16214ba into trunk Feb 25, 2026
7 checks passed
@ryanwelcher ryanwelcher deleted the fix/allowed-controls-undefined branch February 25, 2026 15:47
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.

TypeError: Cannot read properties of undefined (reading 'split') Allowed controls - TypeError: Cannot read properties of undefined (reading 'split')

1 participant