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