1
|
|
|
<?php |
2
|
|
|
/** Blocks package. |
3
|
|
|
* |
4
|
|
|
* @since 9.0.0 |
5
|
|
|
* |
6
|
|
|
* This package lifts elements from Jetpack's Jetpack_Gutenberg class. |
7
|
|
|
* It is now an standalone package reusable outside Jetpack. |
8
|
|
|
* |
9
|
|
|
* @package automattic/jetpack-blocks |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Automattic\Jetpack; |
13
|
|
|
|
14
|
|
|
use Jetpack_Gutenberg; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Register and manage blocks within a plugin. Used to manage block registration, enqueues, and more. |
18
|
|
|
* |
19
|
|
|
* @since 9.0.0 |
20
|
|
|
*/ |
21
|
|
|
class Blocks { |
22
|
|
|
/** |
23
|
|
|
* Wrapper function to safely register a Gutenberg block type |
24
|
|
|
* |
25
|
|
|
* @see register_block_type |
26
|
|
|
* @see Automattic\Jetpack\Blocks::is_gutenberg_version_available |
27
|
|
|
* |
28
|
|
|
* @since 9.0.0 |
29
|
|
|
* |
30
|
|
|
* @param string $slug Slug of the block. |
31
|
|
|
* @param array $args { |
32
|
|
|
* Arguments that are passed into register_block_type. |
33
|
|
|
* See register_block_type for full list of arguments. |
34
|
|
|
* Can also include 2 extra arguments not currently supported by register_block_type. |
35
|
|
|
* |
36
|
|
|
* @type array $version_requirements Array containing required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
37
|
|
|
* @type bool $plan_check Should we check for a specific plan before registering the block. |
38
|
|
|
* } |
39
|
|
|
* |
40
|
|
|
* @return WP_Block_Type|false The registered block type on success, or false on failure. |
41
|
|
|
*/ |
42
|
|
|
public static function jetpack_register_block( $slug, $args = array() ) { |
43
|
|
|
if ( 0 !== strpos( $slug, 'jetpack/' ) && ! strpos( $slug, '/' ) ) { |
44
|
|
|
_doing_it_wrong( 'jetpack_register_block', 'Prefix the block with jetpack/ ', 'Jetpack 9.0.0' ); |
45
|
|
|
$slug = 'jetpack/' . $slug; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ( |
49
|
|
|
isset( $args['version_requirements'] ) |
50
|
|
|
&& ! self::is_gutenberg_version_available( $args['version_requirements'], $slug ) |
51
|
|
|
) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Checking whether block is registered to ensure it isn't registered twice. |
56
|
|
|
if ( self::is_registered( $slug ) ) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$feature_name = self::remove_extension_prefix( $slug ); |
61
|
|
|
|
62
|
|
|
// This is only useful in Jetpack. |
63
|
|
|
if ( ! self::is_standalone_block() ) { |
64
|
|
|
// If the block is dynamic, and a Jetpack block, wrap the render_callback to check availability. |
65
|
|
|
if ( ! empty( $args['plan_check'] ) ) { |
66
|
|
|
if ( isset( $args['render_callback'] ) ) { |
67
|
|
|
$args['render_callback'] = Jetpack_Gutenberg::get_render_callback_with_availability_check( $feature_name, $args['render_callback'] ); |
68
|
|
|
} |
69
|
|
|
$method_name = 'set_availability_for_plan'; |
70
|
|
|
} else { |
71
|
|
|
$method_name = 'set_extension_available'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
add_action( |
75
|
|
|
'jetpack_register_gutenberg_extensions', |
76
|
|
|
function () use ( $feature_name, $method_name ) { |
77
|
|
|
call_user_func( array( 'Jetpack_Gutenberg', $method_name ), $feature_name ); |
78
|
|
|
} |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
// Ensure editor styles are registered so that the site editor knows about the |
82
|
|
|
// editor style dependency when copying styles to the editor iframe. |
83
|
|
|
if ( ! isset( $args['editor_style'] ) ) { |
84
|
|
|
$args['editor_style'] = 'jetpack-blocks-editor'; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return register_block_type( $slug, $args ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if an extension/block is already registered |
93
|
|
|
* |
94
|
|
|
* @since 9.0.0 |
95
|
|
|
* |
96
|
|
|
* @param string $slug Name of extension/block to check. |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public static function is_registered( $slug ) { |
101
|
|
|
return \WP_Block_Type_Registry::get_instance()->is_registered( $slug ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Remove the 'jetpack/' or jetpack-' prefix from an extension name |
106
|
|
|
* |
107
|
|
|
* @since 9.0.0 |
108
|
|
|
* |
109
|
|
|
* @param string $extension_name The extension name. |
110
|
|
|
* |
111
|
|
|
* @return string The unprefixed extension name. |
112
|
|
|
*/ |
113
|
|
|
public static function remove_extension_prefix( $extension_name ) { |
114
|
|
View Code Duplication |
if ( 0 === strpos( $extension_name, 'jetpack/' ) || 0 === strpos( $extension_name, 'jetpack-' ) ) { |
115
|
|
|
return substr( $extension_name, strlen( 'jetpack/' ) ); |
116
|
|
|
} |
117
|
|
|
return $extension_name; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
122
|
|
|
* php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
123
|
|
|
* optionally fall back to that. |
124
|
|
|
* |
125
|
|
|
* @since 9.0.0 |
126
|
|
|
* |
127
|
|
|
* @param array $version_requirements { |
128
|
|
|
* An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
129
|
|
|
* |
130
|
|
|
* @type string $gutenberg Gutenberg version. |
131
|
|
|
* @type string $wp Optional. WordPress version. |
132
|
|
|
* } |
133
|
|
|
* @param string $slug The slug of the block or plugin that has the Gutenberg version requirement. |
134
|
|
|
* |
135
|
|
|
* @return boolean True if the version of Gutenberg required by the block or plugin is available. |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public static function is_gutenberg_version_available( $version_requirements, $slug ) { |
138
|
|
|
global $wp_version; |
139
|
|
|
|
140
|
|
|
// Bail if we don't at least have the Gutenberg version requirement, the WP version is optional. |
141
|
|
|
if ( empty( $version_requirements['gutenberg'] ) ) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// If running a local dev build of Gutenberg plugin GUTENBERG_DEVELOPMENT_MODE is set so assume correct version. |
146
|
|
|
if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE ) { |
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$version_available = false; |
151
|
|
|
|
152
|
|
|
// If running a production build of the Gutenberg plugin then GUTENBERG_VERSION is set, otherwise if WP version |
153
|
|
|
// with required version of Gutenberg is known check that. |
154
|
|
|
if ( defined( 'GUTENBERG_VERSION' ) ) { |
155
|
|
|
$version_available = version_compare( GUTENBERG_VERSION, $version_requirements['gutenberg'], '>=' ); |
156
|
|
|
} elseif ( ! empty( $version_requirements['wp'] ) ) { |
157
|
|
|
$version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ( |
161
|
|
|
! $version_available |
162
|
|
|
&& ! self::is_standalone_block() // This is only useful in Jetpack. |
163
|
|
|
) { |
164
|
|
|
Jetpack_Gutenberg::set_extension_unavailable( |
165
|
|
|
$slug, |
166
|
|
|
'incorrect_gutenberg_version', |
167
|
|
|
array( |
168
|
|
|
'required_feature' => $slug, |
169
|
|
|
'required_version' => $version_requirements, |
170
|
|
|
'current_version' => array( |
171
|
|
|
'wp' => $wp_version, |
172
|
|
|
'gutenberg' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null, |
173
|
|
|
), |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $version_available; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get CSS classes for a block. |
183
|
|
|
* |
184
|
|
|
* @since 9.0.0 |
185
|
|
|
* |
186
|
|
|
* @param string $slug Block slug. |
187
|
|
|
* @param array $attr Block attributes. |
188
|
|
|
* @param array $extra Potential extra classes you may want to provide. |
189
|
|
|
* |
190
|
|
|
* @return string $classes List of CSS classes for a block. |
191
|
|
|
*/ |
192
|
|
|
public static function classes( $slug, $attr, $extra = array() ) { |
193
|
|
|
if ( empty( $slug ) ) { |
194
|
|
|
return ''; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Basic block name class. |
198
|
|
|
$classes = array( |
199
|
|
|
'wp-block-jetpack-' . $slug, |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
// Add alignment if provided. |
203
|
|
|
if ( |
204
|
|
|
! empty( $attr['align'] ) |
205
|
|
|
&& in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true ) |
206
|
|
|
) { |
207
|
|
|
$classes[] = 'align' . $attr['align']; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// Add custom classes if provided in the block editor. |
211
|
|
|
if ( ! empty( $attr['className'] ) ) { |
212
|
|
|
$classes[] = $attr['className']; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Add any extra classes. |
216
|
|
|
if ( is_array( $extra ) && ! empty( $extra ) ) { |
217
|
|
|
$classes = array_merge( $classes, array_filter( $extra ) ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return implode( ' ', $classes ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Does the page return AMP content. |
225
|
|
|
* |
226
|
|
|
* @since 9.0.0 |
227
|
|
|
* |
228
|
|
|
* @return bool $is_amp_request Are we on an AMP view. |
229
|
|
|
*/ |
230
|
|
|
public static function is_amp_request() { |
231
|
|
|
$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ); |
232
|
|
|
|
233
|
|
|
/** This filter is documented in 3rd-party/class.jetpack-amp-support.php */ |
234
|
|
|
return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Check whether or the block being registered is a standalone block, |
239
|
|
|
* running in a context outside of the Jetpack plugin. |
240
|
|
|
* |
241
|
|
|
* @since 9.6.0 |
242
|
|
|
* |
243
|
|
|
* @return bool |
244
|
|
|
*/ |
245
|
|
|
public static function is_standalone_block() { |
246
|
|
|
$is_standalone_block = ! class_exists( Jetpack_Gutenberg::class ); |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Returns true if the block is not being registered within a Jetpack plugin context. |
250
|
|
|
* |
251
|
|
|
* @since 9.6.0 |
252
|
|
|
* |
253
|
|
|
* @param boolean $is_standalone_block Is the block running standalone versus as part of the Jetpack plugin. |
254
|
|
|
*/ |
255
|
|
|
return apply_filters( 'jetpack_is_standalone_block', $is_standalone_block ); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|