PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.4
3.4.4 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 All 197 releases
← All changes | includes/blocks/class-convertkit-block-form.php +180 -37 2.2.43.4.4 View file →
@@ -26,8 +26,11 @@
26 26
27 27 // Register this as a Gutenberg block in the ConvertKit Plugin.
28 28 add_filter( 'convertkit_blocks', array( $this, 'register' ) );
29 29
30 + // Register this block's MCP abilities.
31 + add_filter( 'convertkit_abilities', array( $this, 'register_abilities' ) );
32 +
30 33 // Enqueue scripts for this Gutenberg Block in the editor view.
31 34 add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts_editor' ) );
32 35
33 36 // Enqueue styles for this Gutenberg Block in the editor view.
@@ -32,8 +35,11 @@
32 35
33 36 // Enqueue styles for this Gutenberg Block in the editor view.
34 37 add_action( 'convertkit_gutenberg_enqueue_styles', array( $this, 'enqueue_styles_editor' ) );
35 38
39 + // Enqueue scripts and styles for this Gutenberg Block in the editor and frontend views.
40 + add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) );
41 +
36 42 }
37 43
38 44 /**
39 45 * Enqueues scripts for this Gutenberg Block in the editor view.
@@ -57,8 +63,19 @@
57 63
58 64 }
59 65
60 66 /**
67 + * Enqueues styles for this Gutenberg Block in the editor and frontend views.
68 + *
69 + * @since 2.3.3
70 + */
71 + public function enqueue_styles() {
72 +
73 + convertkit_enqueue_frontend_css();
74 +
75 + }
76 +
77 + /**
61 78 * Returns this block's programmatic name, excluding the convertkit- prefix.
62 79 *
63 80 * @since 1.9.6
64 81 *
@@ -68,9 +85,11 @@
68 85
69 86 /**
70 87 * This will register as:
71 88 * - a shortcode, with the name [convertkit_form].
89 + * - a shortcode, with the name [kit_form].
72 90 * - a shortcode, with the name [convertkit], for backward compat.
91 + * - a shortcode, with the name [kit].
73 92 * - a Gutenberg block, with the name convertkit/form.
74 93 */
75 94 return 'form';
76 95
@@ -76,8 +95,43 @@
76 95
77 96 }
78 97
79 98 /**
99 + * Returns this block's title.
100 + *
101 + * @since 3.1.1
102 + */
103 + public function get_title() {
104 +
105 + return __( 'Kit Form', 'convertkit' );
106 +
107 + }
108 +
109 + /**
110 + * Returns this block's plural title.
111 + *
112 + * @since 3.4.0
113 + *
114 + * @return string
115 + */
116 + public function get_title_plural() {
117 +
118 + return __( 'Kit Forms', 'convertkit' );
119 +
120 + }
121 +
122 + /**
123 + * Returns this block's icon.
124 + *
125 + * @since 3.1.1
126 + */
127 + public function get_icon() {
128 +
129 + return 'resources/backend/images/block-icon-form.svg';
130 +
131 + }
132 +
133 + /**
80 134 * Returns this block's Title, Icon, Categories, Keywords and properties.
81 135 *
82 136 * @since 1.9.6
83 137 *
@@ -88,14 +142,15 @@
88 142 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
89 143 $settings = new ConvertKit_Settings();
90 144
91 145 return array(
92 - 'title' => __( 'ConvertKit Form', 'convertkit' ),
93 - 'description' => __( 'Displays a ConvertKit Form.', 'convertkit' ),
94 - 'icon' => 'resources/backend/images/block-icon-form.svg',
146 + 'title' => $this->get_title(),
147 + 'description' => __( 'Displays a Kit Form.', 'convertkit' ),
148 + 'icon' => $this->get_icon(),
95 149 'category' => 'convertkit',
96 150 'keywords' => array(
97 151 __( 'ConvertKit', 'convertkit' ),
152 + __( 'Kit', 'convertkit' ),
98 153 __( 'Form', 'convertkit' ),
99 154 ),
100 155
101 156 // Function to call when rendering as a block or a shortcode on the frontend web site.
@@ -103,9 +158,9 @@
103 158
104 159 // Shortcode: TinyMCE / QuickTags Modal Width and Height.
105 160 'modal' => array(
106 161 'width' => 500,
107 - 'height' => 106,
162 + 'height' => 55,
108 163 ),
109 164
110 165 // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals.
111 166 'shortcode_include_closing_tag' => false,
@@ -116,17 +171,19 @@
116 171 // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg.
117 172 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-form.png',
118 173
119 174 // Help descriptions, displayed when no API key / resources exist and this block/shortcode is added.
120 - 'no_api_key' => array(
121 - 'notice' => __( 'No API Key specified.', 'convertkit' ),
122 - 'link' => convertkit_get_setup_wizard_plugin_link(),
123 - 'link_text' => __( 'Click here to add your API Key.', 'convertkit' ),
175 + 'no_access_token' => array(
176 + 'notice' => __( 'Not connected to Kit.', 'convertkit' ),
177 + 'link' => convertkit_get_setup_wizard_plugin_link(),
178 + 'link_text' => __( 'Click here to connect your Kit account.', 'convertkit' ),
179 + 'instruction_text' => __( 'Connect your Kit account at Settings > Kit, and then refresh this page to select a form.', 'convertkit' ),
124 180 ),
125 181 'no_resources' => array(
126 - 'notice' => __( 'No forms exist in ConvertKit.', 'convertkit' ),
127 - 'link' => convertkit_get_new_form_url(),
128 - 'link_text' => __( 'Click here to create your first form.', 'convertkit' ),
182 + 'notice' => __( 'No forms exist in Kit.', 'convertkit' ),
183 + 'link' => convertkit_get_new_form_url(),
184 + 'link_text' => __( 'Click here to create your first form.', 'convertkit' ),
185 + 'instruction_text' => __( 'Add a form to your Kit account, and then refresh this page to select a form.', 'convertkit' ),
129 186 ),
130 187
131 188 // Gutenberg: Help descriptions, displayed when no settings defined for a newly added Block.
132 189 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ),
@@ -148,9 +205,9 @@
148 205 ),
149 206
150 207 // Whether an API Key exists in the Plugin, and are the required resources (forms) available.
151 208 // If no API Key is specified in the Plugin's settings, render the "No API Key" output.
152 - 'has_api_key' => $settings->has_api_key_and_secret(),
209 + 'has_access_token' => $settings->has_access_and_refresh_token(),
153 210 'has_resources' => $convertkit_forms->exist(),
154 211 );
155 212
156 213 }
@@ -168,8 +225,19 @@
168 225 'form' => array(
169 226 'type' => 'string',
170 227 ),
171 228
229 + // get_supports() style, color and typography attributes.
230 + 'align' => array(
231 + 'type' => 'string',
232 + ),
233 + 'style' => array(
234 + 'type' => 'object',
235 + ),
236 + 'backgroundColor' => array(
237 + 'type' => 'string',
238 + ),
239 +
172 240 // Always required for Gutenberg.
173 241 'is_gutenberg_example' => array(
174 242 'type' => 'boolean',
175 243 'default' => false,
@@ -178,8 +246,33 @@
178 246
179 247 }
180 248
181 249 /**
250 + * Returns this block's supported built-in Attributes.
251 + *
252 + * @since 1.9.7.4
253 + *
254 + * @return array Supports
255 + */
256 + public function get_supports() {
257 +
258 + return array(
259 + 'align' => true,
260 + 'className' => true,
261 + 'color' => array(
262 + 'link' => false,
263 + 'background' => true,
264 + 'text' => false,
265 + ),
266 + 'spacing' => array(
267 + 'margin' => true,
268 + 'padding' => true,
269 + ),
270 + );
271 +
272 + }
273 +
274 + /**
182 275 * Returns this block's Fields
183 276 *
184 277 * @since 1.9.6
185 278 *
@@ -186,31 +279,44 @@
186 279 * @return bool|array
187 280 */
188 281 public function get_fields() {
189 282
190 - // Bail if the request is not for the WordPress Administration or frontend editor.
191 - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
192 - return false;
193 - }
194 -
195 - // Get ConvertKit Forms.
283 + // Get ConvertKit Forms. Non-legacy forms populate the sidebar dropdown;
284 + // legacy forms are exposed separately as a fallback so the sidebar can
285 + // keep displaying a previously-saved legacy form as the current
286 + // selection without offering other legacy forms as new choices.
196 287 $forms = array();
288 + $legacy_forms = array();
197 289 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
198 290 if ( $convertkit_forms->exist() ) {
199 291 foreach ( $convertkit_forms->get() as $form ) {
200 - $forms[ absint( $form['id'] ) ] = sanitize_text_field( $form['name'] );
292 + $label = sprintf(
293 + '%s [%s]',
294 + sanitize_text_field( $form['name'] ),
295 + // Legacy forms don't include a `format` key, so define them as inline.
296 + ( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' )
297 + );
298 +
299 + if ( ! empty( $form['format'] ) ) {
300 + $forms[ absint( $form['id'] ) ] = $label;
301 + } else {
302 + $legacy_forms[ absint( $form['id'] ) ] = $label;
303 + }
201 304 }
202 305 }
203 306
204 307 return array(
205 308 'form' => array(
206 - 'label' => __( 'Form', 'convertkit' ),
207 - 'type' => 'select',
208 - 'values' => $forms,
209 - 'data' => array(
309 + 'label' => __( 'Form', 'convertkit' ),
310 + 'type' => 'resource',
311 + 'resource' => 'forms',
312 + 'values' => $forms,
313 + 'legacy_values' => $legacy_forms,
314 + 'data' => array(
210 315 // Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format
211 316 // (modal, slide in, sticky bar) and output a message in the block editor for the preview to explain
212 - // why some formats cannot be previewed.
317 + // why some formats cannot be previewed. Includes legacy forms so the preview code can still find
318 + // them when a saved block references a legacy form.
213 319 'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ),
214 320 ),
215 321 ),
216 322 );
@@ -225,13 +331,8 @@
225 331 * @return bool|array
226 332 */
227 333 public function get_panels() {
228 334
229 - // Bail if the request is not for the WordPress Administration or frontend editor.
230 - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
231 - return false;
232 - }
233 -
234 335 return array(
235 336 'general' => array(
236 337 'label' => __( 'General', 'convertkit' ),
237 338 'fields' => array(
@@ -267,8 +368,19 @@
267 368 * @return string Output
268 369 */
269 370 public function render( $atts ) {
270 371
372 + global $post;
373 +
374 + $post_id = is_a( $post, 'WP_Post' ) ? $post->ID : 0;
375 +
376 + // Check if the Block Visibility Plugin permits displaying this block.
377 + if ( ! $this->is_block_visible( $atts ) ) {
378 + // Block should not be displayed due to Block Visibility Plugin conditions.
379 + // Return a blank string now.
380 + return '';
381 + }
382 +
271 383 // Parse shortcode attributes, defining fallback defaults if required.
272 384 $atts = shortcode_atts(
273 385 $this->get_default_values(),
274 386 $this->sanitize_atts( $atts ),
@@ -297,22 +409,32 @@
297 409 }
298 410
299 411 // Get Form HTML.
300 412 $forms = new ConvertKit_Resource_Forms( 'output_form' );
301 - $form = $forms->get_html( $form_id );
413 + $form = $forms->get_html( $form_id, $post_id );
302 414
303 - // If an error occured, it might be that we're requesting a Form ID that exists in ConvertKit
415 + // If an error occurred, it might be that we're requesting a Form ID that exists in ConvertKit
304 416 // but does not yet exist in the Plugin's Form Resources.
305 417 // If so, refresh the Form Resources and try again.
306 - if ( is_wp_error( $form ) ) {
418 + if ( is_wp_error( $form ) && $form->get_error_data() === 404 ) {
307 419 // Refresh Forms from the API.
308 - $forms->refresh();
420 + $result = $forms->refresh();
309 421
422 + // Bail if an error occurred.
423 + if ( is_wp_error( $result ) ) {
424 + if ( $settings->debug_enabled() ) {
425 + return '<!-- ' . $result->get_error_message() . ' --> <!-- ' . $form->get_error_message() . ' -->';
426 + }
427 +
428 + return '';
429 + }
430 +
431 + // Refresh succeeded.
310 432 // Get Form HTML again.
311 - $form = $forms->get_html( $form_id );
433 + $form = $forms->get_html( $form_id, $post_id );
312 434 }
313 435
314 - // If an error still occured, the shortcode might be from the ConvertKit App for a Legacy Form ID
436 + // If an error still occurred, the shortcode might be from the ConvertKit App for a Legacy Form ID
315 437 // These ConvertKit App shortcodes, for some reason, use a different Form ID than the one presented
316 438 // to us in the API.
317 439 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
318 440 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
@@ -317,12 +439,19 @@
317 439 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
318 440 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
319 441 if ( is_wp_error( $form ) ) {
320 442 // Initialize the API.
321 - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'output_form' );
443 + $api = new ConvertKit_API_V4(
444 + CONVERTKIT_OAUTH_CLIENT_ID,
445 + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
446 + $settings->get_access_token(),
447 + $settings->get_refresh_token(),
448 + $settings->debug_enabled(),
449 + 'output_form'
450 + );
322 451
323 452 // Return Legacy Form HTML from the API, which bypasses any internal Plugin check to see if the Form ID exists.
324 - $form = $api->get_form_html( $form_id );
453 + $form = $api->get_form_html( $form_id, $settings->get_api_key() );
325 454 }
326 455
327 456 // Finally, if we still get an error, there's nothing more we can do. The Form ID isn't valid.
328 457 if ( is_wp_error( $form ) ) {
@@ -330,8 +459,22 @@
330 459 return '<!-- ' . $form->get_error_message() . ' -->';
331 460 }
332 461
333 462 return '';
463 + }
464 +
465 + // Build HTML.
466 + // For the block editor, don't include compiled CSS classes and styles,
467 + // as the block editor will add these to the parent container.
468 + // Otherwise the block will render incorrectly with double padding, double margins etc.
469 + // If there's no Form HTML, it's a non-inline form, so don't render any output.
470 + if ( ! $this->is_block_editor_request() && ! empty( $form ) ) {
471 + $form = sprintf(
472 + '<div class="%s" style="%s">%s</div>',
473 + implode( ' ', map_deep( $this->get_css_classes(), 'sanitize_html_class' ) ),
474 + implode( ';', map_deep( $this->get_css_styles( $atts ), 'esc_attr' ) ),
475 + $form
476 + );
334 477 }
335 478
336 479 /**
337 480 * Filter the block's content immediately before it is output.