forked from Automattic/jetpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.jetpack-gutenberg.php
232 lines (209 loc) · 6.03 KB
/
class.jetpack-gutenberg.php
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Handle the registration and use of all blocks available in Jetpack for the block editor, aka Gutenberg.
*
* @package Jetpack
*/
/**
* Helper function to register a Jetpack Gutenberg block
*
* @param string $type Slug of the block. Will be prefixed with jetpack/.
* @param array $args Arguments that are passed into the register_block_type.
*
* @see register_block_type
*
* @since 6.7.0
*
* @return void
*/
function jetpack_register_block( $type, $args = array() ) {
$type = sanitize_title_with_dashes( $type );
Jetpack_Gutenberg::add_block( $type, $args );
}
/**
* General Gutenberg editor specific functionality
*/
class Jetpack_Gutenberg {
/**
* Array of blocks we will be registering.
*
* @var array $blocks Array of blocks we will be registering.
*/
public static $blocks = array();
/**
* Add a block to the list of blocks to be registered.
*
* @param string $type Slug of the block.
* @param array $args Arguments that are passed into the register_block_type.
*/
public static function add_block( $type, $args ) {
self::$blocks[ $type ] = $args;
}
/**
* Register all Jetpack blocks available.
*
* @return void|WP_Block_Type|false The registered block type on success, or false on failure.
*/
public static function load_blocks() {
if ( ! self::is_gutenberg_available() ) {
return;
}
if ( ! self::should_load_blocks() ) {
return;
}
foreach ( self::$blocks as $type => $args ) {
register_block_type(
'jetpack/' . $type,
$args
);
}
}
/**
* Check if Gutenberg editor is available
*
* @since 6.7.0
*
* @return bool
*/
public static function is_gutenberg_available() {
return function_exists( 'register_block_type' );
}
/**
* Check whether conditions indicate Gutenberg blocks should be loaded
*
* Loading blocks is enabled by default and may be disabled via filter:
* add_filter( 'jetpack_gutenberg', '__return_false' );
*
* @since 6.7.0
*
* @return bool
*/
public static function should_load_blocks() {
if ( ! Jetpack::is_active() ) {
return false;
}
/**
* Filter to disable Gutenberg blocks
*
* @since 6.5.0
*
* @param bool true Whether to load Gutenberg blocks
*/
return (bool) apply_filters( 'jetpack_gutenberg', true );
}
/**
* Only enqueue block assets when needed.
*
* @param string $type slug of the block.
* @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued.
*
* @return void
*/
public static function load_assets_as_required( $type, $script_dependencies = array() ) {
$type = sanitize_title_with_dashes( $type );
// Enqueue styles.
$style_relative_path = '_inc/blocks/' . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
if ( self::block_has_asset( $style_relative_path ) ) {
$style_version = self::get_asset_version( $style_relative_path );
$view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
}
// Enqueue script.
$script_relative_path = '_inc/blocks/' . $type . '/view.js';
if ( self::block_has_asset( $script_relative_path ) ) {
$script_version = self::get_asset_version( $script_relative_path );
$view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
}
}
/**
* Check if an asset exists for a block.
*
* @param string $file Path of the file we are looking for.
*
* @return bool $block_has_asset Does the file exist.
*/
public static function block_has_asset( $file ) {
return file_exists( JETPACK__PLUGIN_DIR . $file );
}
/**
* Get the version number to use when loading the file. Allows us to bypass cache when developing.
*
* @param string $file Path of the file we are looking for.
*
* @return string $script_version Version number.
*/
public static function get_asset_version( $file ) {
return Jetpack::is_development_version() && self::block_has_asset( $file )
? filemtime( JETPACK__PLUGIN_DIR . $file )
: JETPACK__VERSION;
}
/**
* Load Gutenberg editor assets
*
* @since 6.7.0
*
* @return void
*/
public static function enqueue_block_editor_assets() {
if ( ! self::should_load_blocks() ) {
return;
}
$rtl = is_rtl() ? '.rtl' : '';
$beta = defined( 'JETPACK_BETA_BLOCKS' ) && JETPACK_BETA_BLOCKS ? '-beta' : '';
$editor_script = plugins_url( "_inc/blocks/editor{$beta}.js", JETPACK__PLUGIN_FILE );
$editor_style = plugins_url( "_inc/blocks/editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE );
$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' )
? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' )
: JETPACK__VERSION;
wp_enqueue_script(
'jetpack-blocks-editor',
$editor_script,
array(
'lodash',
'wp-api-fetch',
'wp-blocks',
'wp-components',
'wp-compose',
'wp-data',
'wp-date',
'wp-edit-post',
'wp-editor',
'wp-element',
'wp-hooks',
'wp-i18n',
'wp-keycodes',
'wp-plugins',
'wp-token-list',
'wp-url',
),
$version,
false
);
wp_localize_script(
'jetpack-blocks-editor',
'Jetpack_Block_Assets_Base_Url',
plugins_url( '_inc/blocks/', JETPACK__PLUGIN_FILE )
);
wp_localize_script(
'jetpack-blocks-editor',
'Jetpack_Initial_State',
array(
'getModules' => array(
'markdown' => array(
'name' => 'markdown',
'activated' => Jetpack::is_module_active( 'markdown' ),
'override' => Jetpack_Modules_Overrides::instance()->get_module_override( 'markdown' )
),
'related-posts' => array(
'name' => 'related-posts',
'activated' => Jetpack::is_module_active( 'related-posts' ),
'override' => Jetpack_Modules_Overrides::instance()->get_module_override( 'related-posts' )
)
)
)
);
Jetpack::setup_wp_i18n_locale_data();
wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version );
}
}