1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Handle the registration and use of all blocks available in Jetpack for the block editor, aka Gutenberg. |
4
|
|
|
* |
5
|
|
|
* @package Jetpack |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Helper function to register a Jetpack Gutenberg block |
10
|
|
|
* |
11
|
|
|
* @param string $type Slug of the block. Will be prefixed with jetpack/. |
12
|
|
|
* @param array $args Arguments that are passed into the register_block_type. |
13
|
|
|
* |
14
|
|
|
* @see register_block_type |
15
|
|
|
* |
16
|
|
|
* @since 6.7.0 |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
function jetpack_register_block( $type, $args = array() ) { |
21
|
|
|
$type = sanitize_title_with_dashes( $type ); |
22
|
|
|
Jetpack_Gutenberg::add_block( $type, $args ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* General Gutenberg editor specific functionality |
27
|
|
|
*/ |
28
|
|
|
class Jetpack_Gutenberg { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Array of blocks we will be registering. |
32
|
|
|
* |
33
|
|
|
* @var array $blocks Array of blocks we will be registering. |
34
|
|
|
*/ |
35
|
|
|
public static $blocks = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add a block to the list of blocks to be registered. |
39
|
|
|
* |
40
|
|
|
* @param string $type Slug of the block. |
41
|
|
|
* @param array $args Arguments that are passed into the register_block_type. |
42
|
|
|
*/ |
43
|
|
|
public static function add_block( $type, $args ) { |
44
|
|
|
self::$blocks[ $type ] = $args; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Register all Jetpack blocks available. |
49
|
|
|
* |
50
|
|
|
* @return void|WP_Block_Type|false The registered block type on success, or false on failure. |
51
|
|
|
*/ |
52
|
|
|
public static function load_blocks() { |
53
|
|
|
if ( ! self::is_gutenberg_available() ) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ( ! self::should_load_blocks() ) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ( self::$blocks as $type => $args ) { |
62
|
|
|
register_block_type( |
63
|
|
|
'jetpack/' . $type, |
64
|
|
|
$args |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check if Gutenberg editor is available |
71
|
|
|
* |
72
|
|
|
* @since 6.7.0 |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public static function is_gutenberg_available() { |
77
|
|
|
return function_exists( 'register_block_type' ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check whether conditions indicate Gutenberg blocks should be loaded |
82
|
|
|
* |
83
|
|
|
* Loading blocks is enabled by default and may be disabled via filter: |
84
|
|
|
* add_filter( 'jetpack_gutenberg', '__return_false' ); |
85
|
|
|
* |
86
|
|
|
* @since 6.7.0 |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public static function should_load_blocks() { |
91
|
|
|
if ( ! Jetpack::is_active() ) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Filter to disable Gutenberg blocks |
97
|
|
|
* |
98
|
|
|
* @since 6.5.0 |
99
|
|
|
* |
100
|
|
|
* @param bool true Whether to load Gutenberg blocks |
101
|
|
|
*/ |
102
|
|
|
return (bool) apply_filters( 'jetpack_gutenberg', true ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Only enqueue block assets when needed. |
107
|
|
|
* |
108
|
|
|
* @param string $type slug of the block. |
109
|
|
|
* @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
114
|
|
|
$type = sanitize_title_with_dashes( $type ); |
115
|
|
|
// Enqueue styles. |
116
|
|
|
$style_relative_path = '_inc/blocks/' . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css'; |
117
|
|
View Code Duplication |
if ( self::block_has_asset( $style_relative_path ) ) { |
118
|
|
|
$style_version = self::get_asset_version( $style_relative_path ); |
119
|
|
|
$view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); |
120
|
|
|
wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Enqueue script. |
124
|
|
|
$script_relative_path = '_inc/blocks/' . $type . '/view.js'; |
125
|
|
View Code Duplication |
if ( self::block_has_asset( $script_relative_path ) ) { |
126
|
|
|
$script_version = self::get_asset_version( $script_relative_path ); |
127
|
|
|
$view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
128
|
|
|
wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false ); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check if an asset exists for a block. |
134
|
|
|
* |
135
|
|
|
* @param string $file Path of the file we are looking for. |
136
|
|
|
* |
137
|
|
|
* @return bool $block_has_asset Does the file exist. |
138
|
|
|
*/ |
139
|
|
|
public static function block_has_asset( $file ) { |
140
|
|
|
return file_exists( JETPACK__PLUGIN_DIR . $file ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the version number to use when loading the file. Allows us to bypass cache when developing. |
145
|
|
|
* |
146
|
|
|
* @param string $file Path of the file we are looking for. |
147
|
|
|
* |
148
|
|
|
* @return string $script_version Version number. |
149
|
|
|
*/ |
150
|
|
|
public static function get_asset_version( $file ) { |
151
|
|
|
return Jetpack::is_development_version() && self::block_has_asset( $file ) |
152
|
|
|
? filemtime( JETPACK__PLUGIN_DIR . $file ) |
153
|
|
|
: JETPACK__VERSION; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Load Gutenberg editor assets |
158
|
|
|
* |
159
|
|
|
* @since 6.7.0 |
160
|
|
|
* |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
public static function enqueue_block_editor_assets() { |
164
|
|
|
if ( ! self::should_load_blocks() ) { |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$rtl = is_rtl() ? '.rtl' : ''; |
169
|
|
|
$beta = defined( 'JETPACK_BETA_BLOCKS' ) && JETPACK_BETA_BLOCKS ? '-beta' : ''; |
170
|
|
|
|
171
|
|
|
$editor_script = plugins_url( "_inc/blocks/editor{$beta}.js", JETPACK__PLUGIN_FILE ); |
172
|
|
|
$editor_style = plugins_url( "_inc/blocks/editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE ); |
173
|
|
|
|
174
|
|
|
$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' ) |
175
|
|
|
? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' ) |
176
|
|
|
: JETPACK__VERSION; |
177
|
|
|
|
178
|
|
|
wp_enqueue_script( |
179
|
|
|
'jetpack-blocks-editor', |
180
|
|
|
$editor_script, |
181
|
|
|
array( |
182
|
|
|
'lodash', |
183
|
|
|
'wp-api-fetch', |
184
|
|
|
'wp-blocks', |
185
|
|
|
'wp-components', |
186
|
|
|
'wp-compose', |
187
|
|
|
'wp-data', |
188
|
|
|
'wp-date', |
189
|
|
|
'wp-edit-post', |
190
|
|
|
'wp-editor', |
191
|
|
|
'wp-element', |
192
|
|
|
'wp-hooks', |
193
|
|
|
'wp-i18n', |
194
|
|
|
'wp-keycodes', |
195
|
|
|
'wp-plugins', |
196
|
|
|
'wp-token-list', |
197
|
|
|
'wp-url', |
198
|
|
|
), |
199
|
|
|
$version, |
200
|
|
|
false |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
wp_localize_script( |
204
|
|
|
'jetpack-blocks-editor', |
205
|
|
|
'Jetpack_Block_Assets_Base_Url', |
206
|
|
|
plugins_url( '_inc/blocks/', JETPACK__PLUGIN_FILE ) |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
$jp_react_page = new Jetpack_React_Page(); |
210
|
|
|
wp_localize_script( |
211
|
|
|
'jetpack-blocks-editor', |
212
|
|
|
'Jetpack_Initial_State', |
213
|
|
|
$jp_react_page->get_initial_state() |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
Jetpack::setup_wp_i18n_locale_data(); |
217
|
|
|
|
218
|
|
|
wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version ); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|