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 both frontend + backend. |
18
|
|
|
* |
19
|
|
|
* `wp-blocks`: includes block type registration and related functions. |
20
|
|
|
* |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
function my_block_cgb_block_assets() { |
24
|
|
|
// Styles. |
25
|
|
|
wp_enqueue_style( 'my_block-cgb-style-css', |
26
|
|
|
plugins_url( 'dist/blocks.style.build.css', dirname( __FILE__ ) ), |
27
|
|
|
array( 'wp-blocks' ) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Hook: Frontend assets. |
32
|
|
|
add_action( 'enqueue_block_assets', 'my_block_cgb_block_assets' ); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Enqueue Gutenberg block assets for backend editor. |
36
|
|
|
* |
37
|
|
|
* `wp-blocks`: includes block type registration and related functions. |
38
|
|
|
* `wp-element`: includes the WordPress Element abstraction for describing the structure of your blocks. |
39
|
|
|
* `wp-i18n`: To internationalize the block's text. |
40
|
|
|
* |
41
|
|
|
* @since 1.0.0 |
42
|
|
|
*/ |
43
|
|
|
function my_block_cgb_editor_assets() { |
44
|
|
|
// Scripts. |
45
|
|
|
wp_enqueue_script( |
46
|
|
|
'my_block-cgb-block-js', // Handle. |
47
|
|
|
plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack. |
48
|
|
|
array( 'wp-blocks', 'wp-i18n', 'wp-element' ), // Dependencies, defined above. |
49
|
|
|
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time. |
|
|
|
|
50
|
|
|
true // Enqueue the script in the footer. |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
// Styles. |
54
|
|
|
wp_enqueue_style( |
55
|
|
|
'my_block-cgb-block-editor-css', // Handle. |
56
|
|
|
plugins_url( 'dist/blocks.editor.build.css', dirname( __FILE__ ) ), // Block editor CSS. |
57
|
|
|
array( 'wp-edit-blocks' ) // Dependency to include the CSS after it. |
58
|
|
|
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: filemtime — Gets file modification time. |
|
|
|
|
59
|
|
|
); |
60
|
|
|
} // End function my_block_cgb_editor_assets(). |
61
|
|
|
|
62
|
|
|
// Hook: Editor assets. |
63
|
|
|
add_action( 'enqueue_block_editor_assets', 'my_block_cgb_editor_assets' ); |
64
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.