|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
add_action( 'init', 'yikes_maybe_activate_blocks' ); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* If Gutenberg is active, activate our blocks. |
|
6
|
|
|
* |
|
7
|
|
|
* We check if Gutenberg is active by looking for the existence of the `gutenberg_init()` function. |
|
8
|
|
|
*/ |
|
9
|
|
|
function yikes_maybe_activate_blocks() { |
|
10
|
|
|
include ABSPATH . WPINC . '/version.php'; |
|
11
|
|
|
if ( function_exists( 'gutenberg_init' ) || isset( $wp_version ) && version_compare( $wp_version, '5.0.0', '>=' ) ) { |
|
|
|
|
|
|
12
|
|
|
require_once YIKES_MC_PATH . 'blocks/api/api.php'; |
|
13
|
|
|
require_once YIKES_MC_PATH . 'blocks/easy-forms-block/easy-forms-block.php'; |
|
14
|
|
|
$yikes_easy_form_block = new YIKES_Easy_Form_Block(); |
|
|
|
|
|
|
15
|
|
|
$yikes_easy_form_blocks_api = new YIKES_Easy_Forms_Blocks_API(); |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class YIKES_Easy_Forms_Blocks. |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class YIKES_Easy_Forms_Blocks { |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
const BLOCK_NAMESPACE = 'yikes-inc-easy-forms/'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Register our hooks. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct() { |
|
30
|
|
|
add_action( 'enqueue_block_editor_assets', array( $this, 'editor_scripts' ) ); |
|
31
|
|
|
add_action( 'init', array( $this, 'register_blocks' ), 11 ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Enqueue our scripts. |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract public function editor_scripts(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Register our Easy Forms block callback. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function register_blocks() { |
|
43
|
|
|
register_block_type( static::BLOCK_NAMESPACE . static::BLOCK, array( |
|
44
|
|
|
'render_callback' => array( $this, 'render_block' ), |
|
45
|
|
|
)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Take the shortcode parameters from the Gutenberg block and render our shortcode. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $attributes Block attributes. |
|
52
|
|
|
* @param string $content Block content. |
|
53
|
|
|
* @return string Block output. |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract public function render_block( $attributes, $content ); |
|
56
|
|
|
} |
|
57
|
|
|
|
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.