| @@ -13,10 +13,15 @@ | ||
| 13 | 13 | exit; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | - * Enqueue Gutenberg block assets for backend editor. | |
| 17 | + * Enqueue Gutenberg block assets for both frontend + backend. | |
| 18 | 18 | * |
| 19 | + * Assets enqueued: | |
| 20 | + * 1. blocks.style.build.css - Frontend + Backend. | |
| 21 | + * 2. blocks.build.js - Backend. | |
| 22 | + * 3. blocks.editor.build.css - Backend. | |
| 23 | + * | |
| 19 | 24 | * @uses {wp-blocks} for block type registration & related functions. |
| 20 | 25 | * @uses {wp-element} for WP Element abstraction — structure of blocks. |
| 21 | 26 | * @uses {wp-i18n} to internationalize the block's text. |
| 22 | 27 | * @uses {wp-editor} for WP editor styles. |
| @@ -21,72 +26,54 @@ | ||
| 21 | 26 | * @uses {wp-i18n} to internationalize the block's text. |
| 22 | 27 | * @uses {wp-editor} for WP editor styles. |
| 23 | 28 | * @since 1.0.0 |
| 24 | 29 | */ |
| 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. | |
| 30 | +function wordpress_cgb_block_assets() { // phpcs:ignore | |
| 31 | + // Register block styles for both frontend + backend. | |
| 32 | + wp_register_style( | |
| 33 | + 'wordpress-cgb-style-css', // Handle. | |
| 34 | + plugins_url( 'dist/blocks.style.build.css?ver=' . CODOC_PLUGIN_VERSION, dirname( __FILE__ ) ), // Block style CSS. | |
| 35 | + array( 'wp-editor' ), // Dependency to include the CSS after it. | |
| 36 | + null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time. | |
| 37 | + ); | |
| 38 | + | |
| 39 | + // Register block editor script for backend. | |
| 40 | + wp_register_script( | |
| 41 | + 'wordpress-cgb-block-js', // Handle. | |
| 29 | 42 | plugins_url( '/dist/blocks.build.js?ver=' . CODOC_PLUGIN_VERSION, dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack. |
| 30 | 43 | array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), // Dependencies, defined above. |
| 31 | 44 | null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time. |
| 32 | 45 | true // Enqueue the script in the footer. |
| 33 | 46 | ); |
| 34 | - | |
| 35 | - // Pass necessary options to JavaScript | |
| 36 | - global $_CODOC; | |
| 37 | - $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 38 | - | |
| 39 | - $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 40 | - $block_defaults_raw = isset($codoc_settings['block_defaults']) ? $codoc_settings['block_defaults'] : ''; | |
| 41 | - $block_defaults_decoded = json_decode($block_defaults_raw, true); | |
| 42 | - $block_defaults = is_array($block_defaults_decoded) ? $block_defaults_decoded : new stdClass(); | |
| 43 | - wp_localize_script('codoc-block-js', 'OPTIONS', array( | |
| 44 | - 'codoc_url' => $_CODOC->get_codoc_url(), | |
| 45 | - 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 46 | - 'codoc_plugin_version' => CODOC_PLUGIN_VERSION, | |
| 47 | - 'codoc_sdk_path' => defined('CODOC_SDK_PATH') ? CODOC_SDK_PATH : '/sdk/js/sdk.v1.js', | |
| 48 | - 'codoc_account_is_pro' => isset($auth_info['account_is_pro']) ? $auth_info['account_is_pro'] : 0, | |
| 49 | - 'codoc_currency_code' => isset($auth_info['currency_code']) ? $auth_info['currency_code'] : 'yen', | |
| 50 | - 'codoc_currency_decimal_places' => isset($auth_info['currency_decimal_places']) ? $auth_info['currency_decimal_places'] : 0, | |
| 51 | - 'codoc_block_defaults' => $block_defaults, | |
| 52 | - 'codoc_max_lengths' => array( | |
| 53 | - 'title' => CODOC_MAX_TITLE_LENGTH, | |
| 54 | - 'body_free' => CODOC_MAX_BODY_FREE_LENGTH, | |
| 55 | - 'body_paywalled' => CODOC_MAX_BODY_PAYWALLED_LENGTH, | |
| 56 | - 'body' => CODOC_MAX_BODY_LENGTH, | |
| 57 | - 'binded_url' => CODOC_MAX_BINDED_URL_LENGTH, | |
| 58 | - ), | |
| 59 | - )); | |
| 60 | - | |
| 61 | - // Set script translations | |
| 62 | - wp_set_script_translations('codoc-block-js', 'codoc', plugin_dir_path( dirname(__FILE__) ) . 'languages'); | |
| 63 | - | |
| 64 | - // Styles for editor only | |
| 65 | - wp_enqueue_style( | |
| 66 | - 'codoc-block-editor-css', // Handle. | |
| 47 | + | |
| 48 | + // Register block editor styles for backend. | |
| 49 | + wp_register_style( | |
| 50 | + 'wordpress-cgb-block-editor-css', // Handle. | |
| 67 | 51 | plugins_url( 'dist/blocks.editor.build.css?ver=' . CODOC_PLUGIN_VERSION, dirname( __FILE__ ) ), // Block editor CSS. |
| 68 | 52 | array( 'wp-edit-blocks' ), // Dependency to include the CSS after it. |
| 69 | 53 | null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time. |
| 70 | 54 | ); |
| 71 | -} | |
| 72 | 55 | |
| 73 | -// Hook: Editor assets. | |
| 74 | -add_action( 'enqueue_block_editor_assets', 'wordpress_cgb_block_editor_assets' ); | |
| 75 | - | |
| 76 | -/** | |
| 77 | - * Enqueue Gutenberg block assets for frontend. | |
| 78 | - * | |
| 79 | - * @since 1.0.0 | |
| 80 | - */ | |
| 81 | -function wordpress_cgb_block_frontend_assets() { // phpcs:ignore | |
| 82 | - // Styles for frontend | |
| 83 | - wp_enqueue_style( | |
| 84 | - 'codoc-style-css', // Handle. | |
| 85 | - plugins_url( 'dist/blocks.style.build.css?ver=' . CODOC_PLUGIN_VERSION, dirname( __FILE__ ) ), // Block style CSS. | |
| 86 | - array( 'wp-editor' ), // Dependency to include the CSS after it. | |
| 87 | - null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time. | |
| 56 | + /** | |
| 57 | + * Register Gutenberg block on server-side. | |
| 58 | + * | |
| 59 | + * Register the block on server-side to ensure that the block | |
| 60 | + * scripts and styles for both frontend and backend are | |
| 61 | + * enqueued when the editor loads. | |
| 62 | + * | |
| 63 | + * @link https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type#enqueuing-block-scripts | |
| 64 | + * @since 1.16.0 | |
| 65 | + */ | |
| 66 | + register_block_type( | |
| 67 | + 'cgb/block-wordpress', array( | |
| 68 | + // Enqueue blocks.style.build.css on both frontend & backend. | |
| 69 | + 'style' => 'wordpress-cgb-style-css', | |
| 70 | + // Enqueue blocks.build.js in the editor only. | |
| 71 | + 'editor_script' => 'wordpress-cgb-block-js', | |
| 72 | + // Enqueue blocks.editor.build.css in the editor only. | |
| 73 | + 'editor_style' => 'wordpress-cgb-block-editor-css', | |
| 74 | + ) | |
| 88 | 75 | ); |
| 89 | 76 | } |
| 90 | 77 | |
| 91 | -// Hook: Frontend assets. | |
| 92 | -add_action( 'enqueue_block_assets', 'wordpress_cgb_block_frontend_assets' ); | |
| 78 | +// Hook: Block assets. | |
| 79 | +add_action( 'init', 'wordpress_cgb_block_assets' ); | |