| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blocks Initializer |
| 4 |
* |
| 5 |
* Enqueue CSS/JS of all the blocks. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @package CGB |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Enqueue Gutenberg block assets for backend editor. |
| 18 |
* |
| 19 |
* @uses {wp-blocks} for block type registration & related functions. |
| 20 |
* @uses {wp-element} for WP Element abstraction — structure of blocks. |
| 21 |
* @uses {wp-i18n} to internationalize the block's text. |
| 22 |
* @uses {wp-editor} for WP editor styles. |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
function wordpress_cgb_block_editor_assets() { // phpcs:ignore |
| 26 |
// Scripts and styles for editor only |
| 27 |
wp_enqueue_script( |
| 28 |
'codoc-block-js', // Handle. |
| 29 |
plugins_url( '/dist/blocks.build.js?ver=' . CODOC_PLUGIN_VERSION, dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack. |
| 30 |
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), // Dependencies, defined above. |
| 31 |
null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time. |
| 32 |
true // Enqueue the script in the footer. |
| 33 |
); |
| 34 |
|
| 35 |
// Pass necessary options to JavaScript |
| 36 |
global $_CODOC; |
| 37 |
$auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); |
| 38 |
|
| 39 |
wp_localize_script('codoc-block-js', 'OPTIONS', array( |
| 40 |
'codoc_url' => $_CODOC->get_codoc_url(), |
| 41 |
'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), |
| 42 |
'codoc_plugin_version' => CODOC_PLUGIN_VERSION, |
| 43 |
'codoc_sdk_path' => defined('CODOC_SDK_PATH') ? CODOC_SDK_PATH : '/sdk/js/sdk.v1.js', |
| 44 |
'codoc_account_is_pro' => isset($auth_info['account_is_pro']) ? $auth_info['account_is_pro'] : 0, |
| 45 |
'codoc_currency_code' => isset($auth_info['currency_code']) ? $auth_info['currency_code'] : 'yen', |
| 46 |
'codoc_currency_decimal_places' => isset($auth_info['currency_decimal_places']) ? $auth_info['currency_decimal_places'] : 0, |
| 47 |
)); |
| 48 |
|
| 49 |
// Set script translations |
| 50 |
wp_set_script_translations('codoc-block-js', 'codoc', plugin_dir_path( dirname(__FILE__) ) . 'languages'); |
| 51 |
|
| 52 |
// Styles for editor only |
| 53 |
wp_enqueue_style( |
| 54 |
'codoc-block-editor-css', // Handle. |
| 55 |
plugins_url( 'dist/blocks.editor.build.css?ver=' . CODOC_PLUGIN_VERSION, dirname( __FILE__ ) ), // Block editor CSS. |
| 56 |
array( 'wp-edit-blocks' ), // Dependency to include the CSS after it. |
| 57 |
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time. |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
// Hook: Editor assets. |
| 62 |
add_action( 'enqueue_block_editor_assets', 'wordpress_cgb_block_editor_assets' ); |
| 63 |
|
| 64 |
/** |
| 65 |
* Enqueue Gutenberg block assets for frontend. |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
*/ |
| 69 |
function wordpress_cgb_block_frontend_assets() { // phpcs:ignore |
| 70 |
// Styles for frontend |
| 71 |
wp_enqueue_style( |
| 72 |
'codoc-style-css', // Handle. |
| 73 |
plugins_url( 'dist/blocks.style.build.css?ver=' . CODOC_PLUGIN_VERSION, dirname( __FILE__ ) ), // Block style CSS. |
| 74 |
array( 'wp-editor' ), // Dependency to include the CSS after it. |
| 75 |
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time. |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
// Hook: Frontend assets. |
| 80 |
add_action( 'enqueue_block_assets', 'wordpress_cgb_block_frontend_assets' ); |
| 81 |
|