diff --git a/.wp-env.json b/.wp-env.json index 2542c32..3a8a4f9 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,5 +1,5 @@ { - "core": "WordPress/WordPress#6.7.1", + "core": "WordPress/WordPress#6.8", "phpVersion": "7.4", "plugins": [ "." ] } diff --git a/src/Block/block.php b/src/Block/block.php index 9faee48..259f9db 100644 --- a/src/Block/block.php +++ b/src/Block/block.php @@ -6,15 +6,15 @@ function register() { $block_path = dirname( plugin_dir_path( __FILE__ ), 2 ) . '/build/block'; $block_json_path = "$block_path/block.json"; - register_site_status_test( $block_json_path ); - - if ( ! file_exists( $block_json_path ) ) { - return; - } - add_action( 'init', function () use ( $block_json_path ) { + register_site_status_test( $block_json_path ); + + if ( ! file_exists( $block_json_path ) ) { + return; + } + $metadata = parse_block_json( $block_json_path ); register_block_type( $block_json_path, diff --git a/src/plugin.php b/src/plugin.php index 986baa3..50c0f26 100644 --- a/src/plugin.php +++ b/src/plugin.php @@ -20,19 +20,36 @@ function main() { } function register_scripts() { + // Common configuration data for both admin and frontend. + $json_data = wp_json_encode( + array( + 'rootUrl' => root_url() . '/iframe/', + ) + ); + + // Helper function to register and enqueue the configuration script. + $register_config_script = function() use ( $json_data ) { + // Enqueue script for global configuration. + wp_register_script( SCRIPT_HANDLE_CONFIG, '', array(), automattic_chatrix_version(), true ); + wp_enqueue_script( SCRIPT_HANDLE_CONFIG ); + wp_add_inline_script( SCRIPT_HANDLE_CONFIG, 'window.' . CONFIG_VARIABLE . " = $json_data;" ); + }; + + // Admin scripts - needed for Gutenberg editor. add_action( - 'wp_enqueue_scripts', - function () { - $json_data = wp_json_encode( - array( - 'rootUrl' => root_url() . '/iframe/', - ) - ); + 'admin_enqueue_scripts', + function () use ( $register_config_script ) { + // Register and enqueue configuration script in admin area. + $register_config_script(); + } + ); - // Enqueue script for global configuration. - wp_register_script( SCRIPT_HANDLE_CONFIG, '', array(), automattic_chatrix_version(), true ); - wp_enqueue_script( SCRIPT_HANDLE_CONFIG ); - wp_add_inline_script( SCRIPT_HANDLE_CONFIG, 'window.' . CONFIG_VARIABLE . " = $json_data;" ); + // Frontend scripts. + add_action( + 'wp_enqueue_scripts', + function () use ( $register_config_script ) { + // Register and enqueue configuration script. + $register_config_script(); // Note we don't enqueue the SCRIPT_HANDLE_APP script yet. It will be enqueued whenever SCRIPT_HANDLE_APP // is specified as a dependency of another script.