-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclass-convertkit-gutenberg.php
More file actions
412 lines (339 loc) · 11.5 KB
/
class-convertkit-gutenberg.php
File metadata and controls
412 lines (339 loc) · 11.5 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* ConvertKit Gutenberg class.
*
* @package ConvertKit
* @author ConvertKit
*/
/**
* Registers blocks defined in the `convertkit_blocks` filter in Gutenberg.
*
* @package ConvertKit
* @author ConvertKit
*/
class ConvertKit_Gutenberg {
/**
* Constructor
*
* @since 1.9.6
*/
public function __construct() {
// Register Gutenberg Block Categories and Blocks.
if ( get_bloginfo( 'version' ) >= 5.8 ) {
// Filter changed in 5.8.
add_filter( 'block_categories_all', array( $this, 'add_block_categories' ), 10, 2 );
} else {
add_filter( 'block_categories', array( $this, 'add_block_categories' ), 10, 2 );
}
// Register Gutenberg Blocks.
add_action( 'init', array( $this, 'add_blocks' ) );
// Register Gutenberg Plugin Sidebars.
add_action( 'init', array( $this, 'add_plugin_sidebars' ) );
// Register REST API routes.
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
/**
* Register REST API routes.
*
* @since 3.1.0
*/
public function register_routes() {
// Register route to refresh resources andreturn all blocks registered by the Plugin,
// when the user clicks the refresh button in the Gutenberg editor.
register_rest_route(
'kit/v1',
'/blocks',
array(
'methods' => WP_REST_Server::READABLE,
// Return blocks.
'callback' => function () {
// Refresh Forms.
$forms = new ConvertKit_Resource_Forms( 'block_edit' );
$result = $forms->refresh();
if ( is_wp_error( $result ) ) {
// Return blocks without refreshing other resources.
return rest_ensure_response( convertkit_get_blocks() );
}
// Refresh Posts.
$posts = new ConvertKit_Resource_Posts( 'block_edit' );
$result = $posts->refresh();
if ( is_wp_error( $result ) ) {
// Return blocks without refreshing other resources.
return rest_ensure_response( convertkit_get_blocks() );
}
// Refresh Products.
$products = new ConvertKit_Resource_Products( 'block_edit' );
$result = $products->refresh();
if ( is_wp_error( $result ) ) {
// Return blocks without refreshing other resources.
return rest_ensure_response( convertkit_get_blocks() );
}
// Return blocks, which will now include the refreshed resources.
return rest_ensure_response( convertkit_get_blocks() );
},
// Only refresh resources for users who can edit posts.
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
)
);
}
/**
* Registers the ConvertKit Block Category.
*
* @since 1.9.6
*
* @param array $categories Block Categories.
* @param WP_Post $post WordPress Post.
* @return array Block Categories
*/
public function add_block_categories( $categories, $post ) {
// Define block categories.
$categories = array_merge(
$categories,
array(
array(
'slug' => 'convertkit',
'title' => 'Kit',
),
)
);
/**
* Adds block categories to the default Gutenberg Block Categories
*
* @since 1.9.6
*
* @param array $categories Block Categories
* @param WP_Post $post WordPress Post
*/
$categories = apply_filters( 'convertkit_admin_gutenberg_add_block_categories', $categories, $post );
// Return filtered results.
return $categories;
}
/**
* Registers Blocks, so that they can be used in the Gutenberg Editor
*
* @since 1.9.6
*/
public function add_blocks() {
// Bail if Gutenberg isn't available.
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
// Get blocks.
$blocks = convertkit_get_blocks();
// Bail if no blocks are available.
if ( ! count( $blocks ) ) {
return;
}
// Determine the block API version to use for registering blocks.
$block_api_version = $this->get_block_api_version();
// Get registered blocks.
$registered_blocks = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() );
// Iterate through blocks, registering them.
foreach ( $blocks as $block => $properties ) {
// Skip if this block has already been registered.
if ( in_array( 'convertkit/' . $block, $registered_blocks, true ) ) {
continue;
}
// Register block.
register_block_type(
CONVERTKIT_PLUGIN_PATH . '/includes/blocks/v' . (string) $block_api_version . '/' . $block,
array(
'attributes' => $properties['attributes'],
'editor_script' => 'convertkit-gutenberg',
'render_callback' => array(
$properties['render_callback'][0],
$properties['render_callback'][1],
),
)
);
}
// Enqueue block scripts and styles in the editor view.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) );
// Enqueue block scripts and styles in the editor and frontend views.
add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) );
add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) );
}
/**
* Registers post meta for any registered plugin sidebars using register_post_meta(),
* so data is saved when using the Gutenberg editor.
*
* @since 3.3.0
*/
public function add_plugin_sidebars() {
// Get plugin sidebars.
$plugin_sidebars = convertkit_get_plugin_sidebars();
// Bail if no plugin sidebars are available.
if ( ! count( $plugin_sidebars ) ) {
return;
}
foreach ( $plugin_sidebars as $plugin_sidebar ) {
register_post_meta(
'',
$plugin_sidebar['meta_key'],
array(
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
'properties' => $plugin_sidebar['attributes'],
),
),
'single' => true,
'type' => 'object',
'default' => $plugin_sidebar['default_values'],
'sanitize_callback' => function ( $value ) use ( $plugin_sidebar ) {
// If the value is not an array, return the default values.
if ( ! is_array( $value ) ) {
return $plugin_sidebar['default_values'];
}
// Iterate through the attributes and sanitize the value.
foreach ( $plugin_sidebar['attributes'] as $key => $attribute ) {
$value[ $key ] = sanitize_text_field( $value[ $key ] ?? $value['default'] );
}
// Return the sanitized value.
return $value;
},
'auth_callback' => function () use ( $plugin_sidebar ) {
return current_user_can( $plugin_sidebar['minimum_capability'] );
},
)
);
}
}
/**
* Determines the block API version to use for registering blocks.
*
* @since 3.2.0
*
* @return int Block API version.
*/
public function get_block_api_version() {
// Determine the block API version to use for registering blocks.
// WordPress supports Version 3 from WordPress 6.3:
// https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/.
$block_api_version = ( version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ? 3 : 2 );
/**
* Determine the block API version to use for registering blocks.
*
* @since 3.1.4
*
* @param int $block_api_version Block API version.
* @return int Block API version.
*/
$block_api_version = apply_filters( 'convertkit_gutenberg_block_api_version', $block_api_version );
return absint( $block_api_version );
}
/**
* Enqueues scripts for Gutenberg blocks in the editor view.
*
* @since 1.9.6
*/
public function enqueue_scripts() {
// Bail if request isn't for the Admin or a Frontend Editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
return;
}
// Get settings.
$settings = new ConvertKit_Settings();
// Get blocks and block toolbar buttons.
$blocks = convertkit_get_blocks();
$block_formatters = convertkit_get_block_formatters();
$pre_publish_actions = convertkit_get_pre_publish_actions();
$plugin_sidebars = convertkit_get_plugin_sidebars();
// Enqueue Gutenberg Javascript, and set the blocks data.
wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks );
// If pre-publish actions are available, set the data.
if ( count( $pre_publish_actions ) ) {
wp_localize_script( 'convertkit-gutenberg', 'convertkit_pre_publish_actions', $pre_publish_actions );
}
// If plugin sidebars are available, set the data.
if ( count( $plugin_sidebars ) ) {
wp_localize_script( 'convertkit-gutenberg', 'convertkit_plugin_sidebars', $plugin_sidebars );
}
// Set the Gutenberg data.
wp_localize_script(
'convertkit-gutenberg',
'convertkit_gutenberg',
array(
'ajaxurl' => rest_url( 'kit/v1/blocks' ),
'block_api_version' => $this->get_block_api_version(),
'get_blocks_nonce' => wp_create_nonce( 'wp_rest' ),
)
);
// Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data.
wp_enqueue_script( 'convertkit-gutenberg-block-formatters', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-formatters.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
wp_localize_script( 'convertkit-gutenberg-block-formatters', 'convertkit_block_formatters', $block_formatters );
/**
* Enqueue any additional scripts for Gutenberg blocks that have been registered.
*
* @since 1.9.6.5
*
* @param array $blocks ConvertKit Blocks.
* @param array $block_formatters ConvertKit Block Formatters.
*/
do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks, $block_formatters );
}
/**
* Enqueues styles for Gutenberg blocks in the editor view.
*
* Use wp_enqueue_style() hooked to the enqueue_block_assets hook for frontend styles:
* https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/
*
* @since 1.9.6.9
*/
public function enqueue_styles() {
// Bail if request isn't for the Admin.
if ( ! is_admin() ) {
return;
}
/**
* Enqueue styles for Gutenberg blocks that have been registered.
*
* @since 1.9.6.9
*/
do_action( 'convertkit_gutenberg_enqueue_styles' );
}
/**
* Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
* if the Plugin's Disable JavaScript option is not enabled.
*
* @since 1.9.7.6
*/
public function enqueue_scripts_editor_and_frontend() {
// Don't load scripts if the Disable JS option is on.
$settings = new ConvertKit_Settings();
if ( $settings->scripts_disabled() ) {
return;
}
/**
* Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
* if the Plugin's Disable JavaScript option is not enabled.
*
* @since 1.9.7.6
*/
do_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend' );
}
/**
* Enqueues styles for Gutenberg blocks in both the editor and frontend view,
* if the Plugin's Disable CSS option is not enabled.
*
* @since 1.9.7.6
*/
public function enqueue_styles_editor_and_frontend() {
// Don't load styles if the Disable CSS option is on.
$settings = new ConvertKit_Settings();
if ( $settings->css_disabled() ) {
return;
}
/**
* Enqueues styles for Gutenberg blocks in both the editor and frontend view,
* if the Plugin's Disable CSS option is not enabled.
*
* @since 1.9.7.6
*/
do_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend' );
}
}