Skip to content

Commit 16214ba

Browse files
authored
Merge pull request #156 from ryanwelcher/fix/allowed-controls-undefined
Fix TypeError crash when window.aql.allowedControls is undefined
2 parents 8e40311 + 90388a7 commit 16214ba

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

includes/enqueues.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function () {
4040
// Add inline script.
4141
wp_add_inline_script(
4242
'advanced-query-loop',
43-
'aql.allowedControls = "' . implode( ',', Query_Params_Generator::get_allowed_controls() ) . '";'
43+
'window.aql = window.aql || {}; window.aql.allowedControls = "' . implode( ',', Query_Params_Generator::get_allowed_controls() ) . '";'
4444
);
4545
}
4646

src/variations/controls.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ const isAdvancedQueryLoop = ( props ) => {
4545
return namespace && namespace === AQL;
4646
};
4747

48+
/**
49+
* Fallback list of all control identifiers, used when window.aql.allowedControls
50+
* is not available (e.g. due to script loading order issues from caching plugins).
51+
*/
52+
const ALL_CONTROLS = [
53+
'additional_post_types',
54+
'taxonomy_query_builder',
55+
'post_meta_query',
56+
'post_order',
57+
'exclude_current_post',
58+
'include_posts',
59+
'child_items_only',
60+
'date_query_dynamic_range',
61+
'date_query_relationship',
62+
'pagination',
63+
'exclude_posts',
64+
'enable_caching',
65+
];
66+
4867
/**
4968
* Custom controls
5069
*
@@ -71,9 +90,10 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => {
7190

7291
// If the is the correct variation, add the custom controls.
7392
if ( isAdvancedQueryLoop( props ) ) {
74-
const { allowedControls } = window?.aql;
93+
const { allowedControls } = window?.aql ?? {};
7594
const { attributes } = props;
76-
const allowedControlsArray = allowedControls.split( ',' );
95+
const allowedControlsArray =
96+
allowedControls?.split( ',' ) ?? ALL_CONTROLS;
7797
const propsWithControls = {
7898
...props,
7999
allowedControls: allowedControlsArray,

0 commit comments

Comments
 (0)