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