PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.3.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.3.4
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-gutenberg.php

class-convertkit-gutenberg.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.3.4, at includes/class-convertkit-gutenberg.php

423 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Gutenberg class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers blocks defined in the `convertkit_blocks` filter in Gutenberg.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Gutenberg {
16
17 /**
18 * Constructor
19 *
20 * @since 1.9.6
21 */
22 public function __construct() {
23
24 // Register Gutenberg Block Categories and Blocks.
25 if ( get_bloginfo( 'version' ) >= 5.8 ) {
26 // Filter changed in 5.8.
27 add_filter( 'block_categories_all', array( $this, 'add_block_categories' ), 10, 2 );
28 } else {
29 add_filter( 'block_categories', array( $this, 'add_block_categories' ), 10, 2 );
30 }
31
32 // Register Gutenberg Blocks.
33 add_action( 'init', array( $this, 'add_blocks' ) );
34
35 // Register Gutenberg Plugin Sidebars.
36 add_action( 'init', array( $this, 'add_plugin_sidebars' ) );
37
38 // Register REST API routes.
39 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
40
41 }
42
43 /**
44 * Register REST API routes.
45 *
46 * @since 3.1.0
47 */
48 public function register_routes() {
49
50 // Register route to refresh resources andreturn all blocks registered by the Plugin,
51 // when the user clicks the refresh button in the Gutenberg editor.
52 register_rest_route(
53 'kit/v1',
54 '/blocks',
55 array(
56 'methods' => WP_REST_Server::READABLE,
57
58 // Return blocks.
59 'callback' => function () {
60 // Refresh Forms.
61 $forms = new ConvertKit_Resource_Forms( 'block_edit' );
62 $result = $forms->refresh();
63 if ( is_wp_error( $result ) ) {
64 // Return blocks without refreshing other resources.
65 return rest_ensure_response( convertkit_get_blocks() );
66 }
67
68 // Refresh Posts.
69 $posts = new ConvertKit_Resource_Posts( 'block_edit' );
70 $result = $posts->refresh();
71 if ( is_wp_error( $result ) ) {
72 // Return blocks without refreshing other resources.
73 return rest_ensure_response( convertkit_get_blocks() );
74 }
75
76 // Refresh Products.
77 $products = new ConvertKit_Resource_Products( 'block_edit' );
78 $result = $products->refresh();
79 if ( is_wp_error( $result ) ) {
80 // Return blocks without refreshing other resources.
81 return rest_ensure_response( convertkit_get_blocks() );
82 }
83
84 // Return blocks, which will now include the refreshed resources.
85 return rest_ensure_response( convertkit_get_blocks() );
86 },
87
88 // Only refresh resources for users who can edit posts.
89 'permission_callback' => function () {
90 return current_user_can( 'edit_posts' );
91 },
92 )
93 );
94
95 }
96
97 /**
98 * Registers the ConvertKit Block Category.
99 *
100 * @since 1.9.6
101 *
102 * @param array $categories Block Categories.
103 * @param WP_Post $post WordPress Post.
104 * @return array Block Categories
105 */
106 public function add_block_categories( $categories, $post ) {
107
108 // Define block categories.
109 $categories = array_merge(
110 $categories,
111 array(
112 array(
113 'slug' => 'convertkit',
114 'title' => 'Kit',
115 ),
116 )
117 );
118
119 /**
120 * Adds block categories to the default Gutenberg Block Categories
121 *
122 * @since 1.9.6
123 *
124 * @param array $categories Block Categories
125 * @param WP_Post $post WordPress Post
126 */
127 $categories = apply_filters( 'convertkit_admin_gutenberg_add_block_categories', $categories, $post );
128
129 // Return filtered results.
130 return $categories;
131
132 }
133
134 /**
135 * Registers Blocks, so that they can be used in the Gutenberg Editor
136 *
137 * @since 1.9.6
138 */
139 public function add_blocks() {
140
141 // Bail if Gutenberg isn't available.
142 if ( ! function_exists( 'register_block_type' ) ) {
143 return;
144 }
145
146 // Get blocks.
147 $blocks = convertkit_get_blocks();
148
149 // Bail if no blocks are available.
150 if ( ! count( $blocks ) ) {
151 return;
152 }
153
154 // Determine the block API version to use for registering blocks.
155 $block_api_version = $this->get_block_api_version();
156
157 // Get registered blocks.
158 $registered_blocks = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() );
159
160 // Iterate through blocks, registering them.
161 foreach ( $blocks as $block => $properties ) {
162
163 // Skip if this block has already been registered.
164 if ( in_array( 'convertkit/' . $block, $registered_blocks, true ) ) {
165 continue;
166 }
167
168 // Register block.
169 register_block_type(
170 CONVERTKIT_PLUGIN_PATH . '/includes/blocks/v' . (string) $block_api_version . '/' . $block,
171 array(
172 'attributes' => $properties['attributes'],
173 'editor_script' => 'convertkit-gutenberg',
174 'render_callback' => array(
175 $properties['render_callback'][0],
176 $properties['render_callback'][1],
177 ),
178 )
179 );
180
181 }
182
183 // Enqueue block scripts and styles in the editor view.
184 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
185 add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) );
186
187 // Enqueue block scripts and styles in the editor and frontend views.
188 add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) );
189 add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) );
190
191 }
192
193 /**
194 * Registers post meta for any registered plugin sidebars using register_post_meta(),
195 * so data is saved when using the Gutenberg editor.
196 *
197 * @since 3.3.0
198 */
199 public function add_plugin_sidebars() {
200
201 // Get plugin sidebars.
202 $plugin_sidebars = convertkit_get_plugin_sidebars();
203
204 // Bail if no plugin sidebars are available.
205 if ( ! count( $plugin_sidebars ) ) {
206 return;
207 }
208
209 foreach ( $plugin_sidebars as $plugin_sidebar ) {
210 register_post_meta(
211 '',
212 $plugin_sidebar['meta_key'],
213 array(
214 'show_in_rest' => array(
215 'schema' => array(
216 'type' => 'object',
217 'properties' => $plugin_sidebar['attributes'],
218 ),
219 ),
220 'single' => true,
221 'type' => 'object',
222 'default' => $plugin_sidebar['default_values'],
223 'sanitize_callback' => function ( $meta ) use ( $plugin_sidebar ) {
224
225 // If the value is not an array, return the default values.
226 if ( ! is_array( $meta ) ) {
227 return $plugin_sidebar['default_values'];
228 }
229
230 // Iterate through the attributes and sanitize the meta.
231 foreach ( $plugin_sidebar['attributes'] as $key => $attribute ) {
232 $meta[ $key ] = sanitize_text_field( $meta[ $key ] ?? $attribute['default'] );
233 }
234
235 // If a Form or Landing Page was specified, request a review.
236 // This can safely be called multiple times, as the review request
237 // class will ensure once a review request is dismissed by the user,
238 // it is never displayed again.
239 if ( $meta['form'] || $meta['landing_page'] ) {
240 WP_ConvertKit()->get_class( 'review_request' )->request_review();
241 }
242
243 // Return the sanitized meta.
244 return $meta;
245
246 },
247 'auth_callback' => function () use ( $plugin_sidebar ) {
248
249 return current_user_can( $plugin_sidebar['minimum_capability'] );
250
251 },
252 )
253 );
254 }
255
256 }
257
258 /**
259 * Determines the block API version to use for registering blocks.
260 *
261 * @since 3.2.0
262 *
263 * @return int Block API version.
264 */
265 public function get_block_api_version() {
266
267 // Determine the block API version to use for registering blocks.
268 // WordPress supports Version 3 from WordPress 6.3:
269 // https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/.
270 $block_api_version = ( version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ? 3 : 2 );
271
272 /**
273 * Determine the block API version to use for registering blocks.
274 *
275 * @since 3.1.4
276 *
277 * @param int $block_api_version Block API version.
278 * @return int Block API version.
279 */
280 $block_api_version = apply_filters( 'convertkit_gutenberg_block_api_version', $block_api_version );
281
282 return absint( $block_api_version );
283
284 }
285
286 /**
287 * Enqueues scripts for Gutenberg blocks in the editor view.
288 *
289 * @since 1.9.6
290 */
291 public function enqueue_scripts() {
292
293 // Bail if request isn't for the Admin or a Frontend Editor.
294 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
295 return;
296 }
297
298 // Get settings.
299 $settings = new ConvertKit_Settings();
300
301 // Get blocks and block toolbar buttons.
302 $blocks = convertkit_get_blocks();
303 $block_formatters = convertkit_get_block_formatters();
304 $pre_publish_actions = convertkit_get_pre_publish_actions();
305 $plugin_sidebars = convertkit_get_plugin_sidebars();
306
307 // Enqueue Gutenberg Javascript, and set the blocks data.
308 wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
309 wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks );
310
311 // If pre-publish actions are available, set the data.
312 if ( count( $pre_publish_actions ) ) {
313 wp_localize_script( 'convertkit-gutenberg', 'convertkit_pre_publish_actions', $pre_publish_actions );
314 }
315
316 // If plugin sidebars are available, set the data.
317 if ( count( $plugin_sidebars ) ) {
318 wp_localize_script( 'convertkit-gutenberg', 'convertkit_plugin_sidebars', $plugin_sidebars );
319 }
320
321 // Set the Gutenberg data.
322 wp_localize_script(
323 'convertkit-gutenberg',
324 'convertkit_gutenberg',
325 array(
326 'ajaxurl' => rest_url( 'kit/v1/blocks' ),
327 'block_api_version' => $this->get_block_api_version(),
328 'get_blocks_nonce' => wp_create_nonce( 'wp_rest' ),
329 'refresh_resources_url' => rest_url( 'kit/v1/resources/refresh/' ),
330 'refresh_resources_nonce' => wp_create_nonce( 'wp_rest' ),
331 )
332 );
333
334 // Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data.
335 wp_enqueue_script( 'convertkit-gutenberg-block-formatters', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-formatters.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
336 wp_localize_script( 'convertkit-gutenberg-block-formatters', 'convertkit_block_formatters', $block_formatters );
337
338 /**
339 * Enqueue any additional scripts for Gutenberg blocks that have been registered.
340 *
341 * @since 1.9.6.5
342 *
343 * @param array $blocks ConvertKit Blocks.
344 * @param array $block_formatters ConvertKit Block Formatters.
345 */
346 do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks, $block_formatters );
347
348 }
349
350 /**
351 * Enqueues styles for Gutenberg blocks in the editor view.
352 *
353 * Use wp_enqueue_style() hooked to the enqueue_block_assets hook for frontend styles:
354 * https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/
355 *
356 * @since 1.9.6.9
357 */
358 public function enqueue_styles() {
359
360 // Bail if request isn't for the Admin.
361 if ( ! is_admin() ) {
362 return;
363 }
364
365 /**
366 * Enqueue styles for Gutenberg blocks that have been registered.
367 *
368 * @since 1.9.6.9
369 */
370 do_action( 'convertkit_gutenberg_enqueue_styles' );
371
372 }
373
374 /**
375 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
376 * if the Plugin's Disable JavaScript option is not enabled.
377 *
378 * @since 1.9.7.6
379 */
380 public function enqueue_scripts_editor_and_frontend() {
381
382 // Don't load scripts if the Disable JS option is on.
383 $settings = new ConvertKit_Settings();
384 if ( $settings->scripts_disabled() ) {
385 return;
386 }
387
388 /**
389 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
390 * if the Plugin's Disable JavaScript option is not enabled.
391 *
392 * @since 1.9.7.6
393 */
394 do_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend' );
395
396 }
397
398 /**
399 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
400 * if the Plugin's Disable CSS option is not enabled.
401 *
402 * @since 1.9.7.6
403 */
404 public function enqueue_styles_editor_and_frontend() {
405
406 // Don't load styles if the Disable CSS option is on.
407 $settings = new ConvertKit_Settings();
408 if ( $settings->css_disabled() ) {
409 return;
410 }
411
412 /**
413 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
414 * if the Plugin's Disable CSS option is not enabled.
415 *
416 * @since 1.9.7.6
417 */
418 do_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend' );
419
420 }
421
422 }
423