@@ -20,19 +20,36 @@ function main() {
2020}
2121
2222function 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