PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.1.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.1.8
3.4.2 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 All 195 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.1.8, at includes/class-convertkit-gutenberg.php

311 lines 8.5 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 REST API routes.
36 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
37
38 }
39
40 /**
41 * Register REST API routes.
42 *
43 * @since 3.1.0
44 */
45 public function register_routes() {
46
47 // Register route to refresh resources andreturn all blocks registered by the Plugin,
48 // when the user clicks the refresh button in the Gutenberg editor.
49 register_rest_route(
50 'kit/v1',
51 '/blocks',
52 array(
53 'methods' => WP_REST_Server::READABLE,
54
55 // Return blocks.
56 'callback' => function () {
57 // Refresh Forms.
58 $forms = new ConvertKit_Resource_Forms( 'block_edit' );
59 $result = $forms->refresh();
60 if ( is_wp_error( $result ) ) {
61 // Return blocks without refreshing other resources.
62 return rest_ensure_response( convertkit_get_blocks() );
63 }
64
65 // Refresh Posts.
66 $posts = new ConvertKit_Resource_Posts( 'block_edit' );
67 $result = $posts->refresh();
68 if ( is_wp_error( $result ) ) {
69 // Return blocks without refreshing other resources.
70 return rest_ensure_response( convertkit_get_blocks() );
71 }
72
73 // Refresh Products.
74 $products = new ConvertKit_Resource_Products( 'block_edit' );
75 $result = $products->refresh();
76 if ( is_wp_error( $result ) ) {
77 // Return blocks without refreshing other resources.
78 return rest_ensure_response( convertkit_get_blocks() );
79 }
80
81 // Return blocks, which will now include the refreshed resources.
82 return rest_ensure_response( convertkit_get_blocks() );
83 },
84
85 // Only refresh resources for users who can edit posts.
86 'permission_callback' => function () {
87 return current_user_can( 'edit_posts' );
88 },
89 )
90 );
91
92 }
93
94 /**
95 * Registers the ConvertKit Block Category.
96 *
97 * @since 1.9.6
98 *
99 * @param array $categories Block Categories.
100 * @param WP_Post $post WordPress Post.
101 * @return array Block Categories
102 */
103 public function add_block_categories( $categories, $post ) {
104
105 // Define block categories.
106 $categories = array_merge(
107 $categories,
108 array(
109 array(
110 'slug' => 'convertkit',
111 'title' => 'Kit',
112 ),
113 )
114 );
115
116 /**
117 * Adds block categories to the default Gutenberg Block Categories
118 *
119 * @since 1.9.6
120 *
121 * @param array $categories Block Categories
122 * @param WP_Post $post WordPress Post
123 */
124 $categories = apply_filters( 'convertkit_admin_gutenberg_add_block_categories', $categories, $post );
125
126 // Return filtered results.
127 return $categories;
128
129 }
130
131 /**
132 * Registers Blocks, so that they can be used in the Gutenberg Editor
133 *
134 * @since 1.9.6
135 */
136 public function add_blocks() {
137
138 // Bail if Gutenberg isn't available.
139 if ( ! function_exists( 'register_block_type' ) ) {
140 return;
141 }
142
143 // Get blocks.
144 $blocks = convertkit_get_blocks();
145
146 // Bail if no blocks are available.
147 if ( ! count( $blocks ) ) {
148 return;
149 }
150
151 // Get registered blocks.
152 $registered_blocks = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() );
153
154 // Iterate through blocks, registering them.
155 foreach ( $blocks as $block => $properties ) {
156
157 // Skip if this block has already been registered.
158 if ( in_array( 'convertkit/' . $block, $registered_blocks, true ) ) {
159 continue;
160 }
161
162 // Register block.
163 register_block_type(
164 CONVERTKIT_PLUGIN_PATH . '/includes/blocks/' . $block,
165 array(
166 'attributes' => $properties['attributes'],
167 'editor_script' => 'convertkit-gutenberg',
168 'render_callback' => array(
169 $properties['render_callback'][0],
170 $properties['render_callback'][1],
171 ),
172 )
173 );
174
175 }
176
177 // Enqueue block scripts and styles in the editor view.
178 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
179 add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) );
180
181 // Enqueue block scripts and styles in the editor and frontend views.
182 add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) );
183 add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) );
184
185 }
186
187 /**
188 * Enqueues scripts for Gutenberg blocks in the editor view.
189 *
190 * @since 1.9.6
191 */
192 public function enqueue_scripts() {
193
194 // Bail if request isn't for the Admin or a Frontend Editor.
195 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
196 return;
197 }
198
199 // Get settings.
200 $settings = new ConvertKit_Settings();
201
202 // Get blocks and block toolbar buttons.
203 $blocks = convertkit_get_blocks();
204 $block_formatters = convertkit_get_block_formatters();
205 $pre_publish_actions = convertkit_get_pre_publish_actions();
206
207 // Enqueue Gutenberg Javascript, and set the blocks data.
208 wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
209 wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks );
210 if ( count( $pre_publish_actions ) ) {
211 wp_localize_script( 'convertkit-gutenberg', 'convertkit_pre_publish_actions', $pre_publish_actions );
212 }
213 wp_localize_script(
214 'convertkit-gutenberg',
215 'convertkit_gutenberg',
216 array(
217 'ajaxurl' => rest_url( 'kit/v1/blocks' ),
218 'get_blocks_nonce' => wp_create_nonce( 'wp_rest' ),
219 )
220 );
221
222 // Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data.
223 wp_enqueue_script( 'convertkit-gutenberg-block-formatters', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-formatters.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
224 wp_localize_script( 'convertkit-gutenberg-block-formatters', 'convertkit_block_formatters', $block_formatters );
225
226 /**
227 * Enqueue any additional scripts for Gutenberg blocks that have been registered.
228 *
229 * @since 1.9.6.5
230 *
231 * @param array $blocks ConvertKit Blocks.
232 * @param array $block_formatters ConvertKit Block Formatters.
233 */
234 do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks, $block_formatters );
235
236 }
237
238 /**
239 * Enqueues styles for Gutenberg blocks in the editor view.
240 *
241 * Use wp_enqueue_style() hooked to the enqueue_block_assets hook for frontend styles:
242 * https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/
243 *
244 * @since 1.9.6.9
245 */
246 public function enqueue_styles() {
247
248 // Bail if request isn't for the Admin.
249 if ( ! is_admin() ) {
250 return;
251 }
252
253 /**
254 * Enqueue styles for Gutenberg blocks that have been registered.
255 *
256 * @since 1.9.6.9
257 */
258 do_action( 'convertkit_gutenberg_enqueue_styles' );
259
260 }
261
262 /**
263 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
264 * if the Plugin's Disable JavaScript option is not enabled.
265 *
266 * @since 1.9.7.6
267 */
268 public function enqueue_scripts_editor_and_frontend() {
269
270 // Don't load scripts if the Disable JS option is on.
271 $settings = new ConvertKit_Settings();
272 if ( $settings->scripts_disabled() ) {
273 return;
274 }
275
276 /**
277 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
278 * if the Plugin's Disable JavaScript option is not enabled.
279 *
280 * @since 1.9.7.6
281 */
282 do_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend' );
283
284 }
285
286 /**
287 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
288 * if the Plugin's Disable CSS option is not enabled.
289 *
290 * @since 1.9.7.6
291 */
292 public function enqueue_styles_editor_and_frontend() {
293
294 // Don't load styles if the Disable CSS option is on.
295 $settings = new ConvertKit_Settings();
296 if ( $settings->css_disabled() ) {
297 return;
298 }
299
300 /**
301 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
302 * if the Plugin's Disable CSS option is not enabled.
303 *
304 * @since 1.9.7.6
305 */
306 do_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend' );
307
308 }
309
310 }
311