Blocks.php
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace AIOSEO\Plugin\Common\Utils;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Block helpers.
*
* @since 4.1.1
*/
class Blocks {
/**
* Class constructor.
*
* @since 4.1.1
*/
public function __construct() {
add_action( 'init', [ $this, 'init' ] );
}
/**
* Initializes our blocks.
*
* @since 4.1.1
*
* @return void
*/
public function init() {
add_action( 'enqueue_block_editor_assets', [ $this, 'registerBlockEditorAssets' ] );
}
/**
* Registers the block type with WordPress.
*
* @since 4.2.1
*
* @param string $slug Block type name including namespace.
* @param array $args Array of block type arguments with additional 'wp_min_version' arg.
*
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
public function registerBlock( $slug = '', $args = [] ) {
global $wp_version;
if ( ! strpos( $slug, '/' ) ) {
$slug = 'aioseo/' . $slug;
}
if ( ! $this->isBlockEditorActive() ) {
return;
}
// Check if the block requires a minimum WP version.
if ( ! empty( $args['wp_min_version'] ) && version_compare( $wp_version, $args['wp_min_version'], '>' ) ) {
return false;
}
// Checking whether block is registered to ensure it isn't registered twice.
if ( $this->isRegistered( $slug ) ) {
return false;
}
$defaults = [
'render_callback' => null,
'editor_script' => aioseo()->core->assets->jsHandle( 'src/vue/standalone/blocks/main.js' ),
'editor_style' => aioseo()->core->assets->cssHandle( 'src/vue/assets/scss/blocks-editor.scss' ),
'attributes' => null,
'supports' => null
];
$args = wp_parse_args( $args, $defaults );
return register_block_type( $slug, $args );
}
/**
* Register Gutenberg editor assets
*
* @since 4.2.1
*
* @return void
*/
public function registerBlockEditorAssets() {
aioseo()->core->assets->loadCss( 'src/vue/standalone/blocks/main.js', [], false );
$dependencies = [
'wp-blocks',
'wp-components',
'wp-element',
'wp-i18n',
'wp-data',
'wp-url',
'wp-polyfill',
aioseo()->core->assets->jsHandle( 'src/vue/standalone/post-settings/main.js' )
];
global $wp_version;
if ( version_compare( $wp_version, '5.2', '>=' ) ) {
$dependencies[] = 'wp-block-editor';
}
aioseo()->core->assets->registerJs( 'src/vue/standalone/blocks/main.js', $dependencies );
aioseo()->core->assets->registerCss( 'src/vue/assets/scss/blocks-editor.scss' );
}
/**
* Check if a block is already registered.
*
* @since 4.2.1
*
* @param string $slug Name of block to check.
*
* @return bool
*/
public function isRegistered( $slug ) {
return \WP_Block_Type_Registry::get_instance()->is_registered( $slug );
}
/**
* Helper function to determine if we're rendering the block inside Gutenberg.
*
* @since 4.1.1
*
* @return bool In gutenberg.
*/
public function isGBEditor() {
return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore HM.Security.NonceVerification.Recommended
}
/**
* Helper function to determine if we can register blocks.
*
* @since 4.1.1
*
* @return bool Can register block.
*/
public function isBlockEditorActive() {
return function_exists( 'register_block_type' );
}
}