|
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
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function load_assets_as_required( $type ) { |
|
113
|
|
|
$type = sanitize_title_with_dashes( $type ); |
|
114
|
|
|
// Enqueue styles. |
|
115
|
|
|
$style_relative_path = '_inc/blocks/' . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css'; |
|
116
|
|
View Code Duplication |
if ( self::block_has_asset( $style_relative_path ) ) { |
|
117
|
|
|
$style_version = self::get_asset_version( $style_relative_path ); |
|
118
|
|
|
$view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); |
|
119
|
|
|
wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// Enqueue script. |
|
123
|
|
|
$script_relative_path = '_inc/blocks/' . $type . '/view.js'; |
|
124
|
|
View Code Duplication |
if ( self::block_has_asset( $script_relative_path ) ) { |
|
125
|
|
|
$script_version = self::get_asset_version( $script_relative_path ); |
|
126
|
|
|
$view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
|
127
|
|
|
wp_enqueue_script( 'jetpack-block-' . $type, $view_script, array(), $script_version, false ); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Check if an asset exists for a block. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $file Path of the file we are looking for. |
|
135
|
|
|
* |
|
136
|
|
|
* @return bool $block_has_asset Does the file exist. |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function block_has_asset( $file ) { |
|
139
|
|
|
return file_exists( JETPACK__PLUGIN_DIR . $file ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the version number to use when loading the file. Allows us to bypass cache when developing. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $file Path of the file we are looking for. |
|
146
|
|
|
* |
|
147
|
|
|
* @return string $script_version Version number. |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function get_asset_version( $file ) { |
|
150
|
|
|
return Jetpack::is_development_version() && self::block_has_asset( $file ) |
|
151
|
|
|
? filemtime( JETPACK__PLUGIN_DIR . $file ) |
|
152
|
|
|
: JETPACK__VERSION; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Load Gutenberg editor assets |
|
157
|
|
|
* |
|
158
|
|
|
* @since 6.7.0 |
|
159
|
|
|
* |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function enqueue_block_editor_assets() { |
|
163
|
|
|
if ( ! self::should_load_blocks() ) { |
|
164
|
|
|
return; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$rtl = is_rtl() ? '.rtl' : ''; |
|
168
|
|
|
$beta = defined( 'JETPACK_BETA_BLOCKS' ) && JETPACK_BETA_BLOCKS ? '-beta' : ''; |
|
169
|
|
|
|
|
170
|
|
|
$editor_script = plugins_url( "_inc/blocks/editor{$beta}.js", JETPACK__PLUGIN_FILE ); |
|
171
|
|
|
$editor_style = plugins_url( "_inc/blocks/editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE ); |
|
172
|
|
|
|
|
173
|
|
|
$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' ) |
|
174
|
|
|
? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' ) |
|
175
|
|
|
: JETPACK__VERSION; |
|
176
|
|
|
|
|
177
|
|
|
wp_enqueue_script( |
|
178
|
|
|
'jetpack-blocks-editor', |
|
179
|
|
|
$editor_script, |
|
180
|
|
|
array( |
|
181
|
|
|
'lodash', |
|
182
|
|
|
'wp-api-fetch', |
|
183
|
|
|
'wp-blocks', |
|
184
|
|
|
'wp-components', |
|
185
|
|
|
'wp-compose', |
|
186
|
|
|
'wp-data', |
|
187
|
|
|
'wp-date', |
|
188
|
|
|
'wp-edit-post', |
|
189
|
|
|
'wp-editor', |
|
190
|
|
|
'wp-element', |
|
191
|
|
|
'wp-hooks', |
|
192
|
|
|
'wp-i18n', |
|
193
|
|
|
'wp-keycodes', |
|
194
|
|
|
'wp-plugins', |
|
195
|
|
|
'wp-token-list', |
|
196
|
|
|
'wp-url', |
|
197
|
|
|
), |
|
198
|
|
|
$version, |
|
199
|
|
|
false |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
wp_localize_script( |
|
203
|
|
|
'jetpack-blocks-editor', |
|
204
|
|
|
'Jetpack_Block_Assets_Base_Url', |
|
205
|
|
|
plugins_url( '_inc/blocks/', JETPACK__PLUGIN_FILE ) |
|
206
|
|
|
); |
|
207
|
|
|
|
|
208
|
|
|
$jp_react_page = new Jetpack_React_Page(); |
|
209
|
|
|
wp_localize_script( |
|
210
|
|
|
'jetpack-blocks-editor', |
|
211
|
|
|
'Jetpack_Initial_State', |
|
212
|
|
|
$jp_react_page->get_initial_state() |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
Jetpack::setup_wp_i18n_locale_data(); |
|
216
|
|
|
|
|
217
|
|
|
wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array( 'social-logos' ), $version ); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|