Skip to content

Commit 0ad13ae

Browse files
committed
fix regression introduced in #249 and load ChatrixConfig object so that correct iframe src is deduced
1 parent 3c0fb34 commit 0ad13ae

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/plugin.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,36 @@ function main() {
2020
}
2121

2222
function register_scripts() {
23+
// Common configuration data for both admin and frontend
24+
$json_data = wp_json_encode(
25+
array(
26+
'rootUrl' => root_url() . '/iframe/',
27+
)
28+
);
29+
30+
// Helper function to register and enqueue the configuration script
31+
$register_config_script = function() use ( $json_data ) {
32+
// Enqueue script for global configuration.
33+
wp_register_script( SCRIPT_HANDLE_CONFIG, '', array(), automattic_chatrix_version(), true );
34+
wp_enqueue_script( SCRIPT_HANDLE_CONFIG );
35+
wp_add_inline_script( SCRIPT_HANDLE_CONFIG, 'window.' . CONFIG_VARIABLE . " = $json_data;" );
36+
};
37+
38+
// Admin scripts - needed for Gutenberg editor
2339
add_action(
24-
'wp_enqueue_scripts',
25-
function () {
26-
$json_data = wp_json_encode(
27-
array(
28-
'rootUrl' => root_url() . '/iframe/',
29-
)
30-
);
40+
'admin_enqueue_scripts',
41+
function () use ( $register_config_script ) {
42+
// Register and enqueue configuration script in admin area
43+
$register_config_script();
44+
}
45+
);
3146

32-
// Enqueue script for global configuration.
33-
wp_register_script( SCRIPT_HANDLE_CONFIG, '', array(), automattic_chatrix_version(), true );
34-
wp_enqueue_script( SCRIPT_HANDLE_CONFIG );
35-
wp_add_inline_script( SCRIPT_HANDLE_CONFIG, 'window.' . CONFIG_VARIABLE . " = $json_data;" );
47+
// Frontend scripts
48+
add_action(
49+
'wp_enqueue_scripts',
50+
function () use ( $register_config_script ) {
51+
// Register and enqueue configuration script
52+
$register_config_script();
3653

3754
// Note we don't enqueue the SCRIPT_HANDLE_APP script yet. It will be enqueued whenever SCRIPT_HANDLE_APP
3855
// is specified as a dependency of another script.

0 commit comments

Comments
 (0)