PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.0.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.0.2
3.4.3 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 All 196 releases
← All changes | includes/class-convertkit-gutenberg.php +11 -199 3.4.22.0.2 View file →
@@ -31,71 +31,11 @@
31 31
32 32 // Register Gutenberg Blocks.
33 33 add_action( 'init', array( $this, 'add_blocks' ) );
34 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 35 }
42 36
43 37 /**
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 38 * Registers the ConvertKit Block Category.
99 39 *
100 40 * @since 1.9.6
101 41 *
@@ -110,9 +50,9 @@
110 50 $categories,
111 51 array(
112 52 array(
113 53 'slug' => 'convertkit',
114 - 'title' => 'Kit',
54 + 'title' => 'ConvertKit',
115 55 ),
116 56 )
117 57 );
118 58
@@ -146,15 +86,12 @@
146 86 // Get blocks.
147 87 $blocks = convertkit_get_blocks();
148 88
149 89 // Bail if no blocks are available.
150 - if ( ! count( $blocks ) ) {
90 + if ( ! is_array( $blocks ) || ! count( $blocks ) ) {
151 91 return;
152 92 }
153 93
154 - // Determine the block API version to use for registering blocks.
155 - $block_api_version = $this->get_block_api_version();
156 -
157 94 // Get registered blocks.
158 95 $registered_blocks = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() );
159 96
160 97 // Iterate through blocks, registering them.
@@ -160,15 +97,15 @@
160 97 // Iterate through blocks, registering them.
161 98 foreach ( $blocks as $block => $properties ) {
162 99
163 100 // Skip if this block has already been registered.
164 - if ( in_array( 'convertkit/' . $block, $registered_blocks, true ) ) {
101 + if ( is_array( $registered_blocks ) && in_array( 'convertkit/' . $block, $registered_blocks, true ) ) {
165 102 continue;
166 103 }
167 104
168 105 // Register block.
169 106 register_block_type(
170 - CONVERTKIT_PLUGIN_PATH . '/includes/blocks/v' . (string) $block_api_version . '/' . $block,
107 + 'convertkit/' . $block,
171 108 array(
172 109 'attributes' => $properties['attributes'],
173 110 'editor_script' => 'convertkit-gutenberg',
174 111 'render_callback' => array(
@@ -176,14 +113,13 @@
176 113 $properties['render_callback'][1],
177 114 ),
178 115 )
179 116 );
180 -
181 117 }
182 118
183 119 // Enqueue block scripts and styles in the editor view.
184 120 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
185 - add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) );
121 + add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_styles' ) );
186 122
187 123 // Enqueue block scripts and styles in the editor and frontend views.
188 124 add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) );
189 125 add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) );
@@ -190,101 +126,8 @@
190 126
191 127 }
192 128
193 129 /**
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 130 * Enqueues scripts for Gutenberg blocks in the editor view.
288 131 *
289 132 * @since 1.9.6
290 133 */
@@ -297,54 +140,23 @@
297 140
298 141 // Get settings.
299 142 $settings = new ConvertKit_Settings();
300 143
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();
144 + // Get blocks.
145 + $blocks = convertkit_get_blocks();
306 146
307 147 // Enqueue Gutenberg Javascript, and set the blocks data.
308 148 wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
309 149 wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks );
310 150
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 151 /**
339 152 * Enqueue any additional scripts for Gutenberg blocks that have been registered.
340 153 *
341 154 * @since 1.9.6.5
342 155 *
343 - * @param array $blocks ConvertKit Blocks.
344 - * @param array $block_formatters ConvertKit Block Formatters.
156 + * @param array $blocks ConvertKit Blocks.
345 157 */
346 - do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks, $block_formatters );
158 + do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks );
347 159
348 160 }
349 161
350 162 /**
@@ -356,10 +168,10 @@
356 168 * @since 1.9.6.9
357 169 */
358 170 public function enqueue_styles() {
359 171
360 - // Bail if request isn't for the Admin.
361 - if ( ! is_admin() ) {
172 + // Bail if request isn't for the Admin or a Frontend Editor.
173 + if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
362 174 return;
363 175 }
364 176
365 177 /**