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 +193 -30 2.2.23.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 *
@@ -84,15 +138,19 @@
84 138 * @return array
85 139 */
86 140 public function get_overview() {
87 141
142 + $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
143 + $settings = new ConvertKit_Settings();
144 +
88 145 return array(
89 - 'title' => __( 'ConvertKit Form', 'convertkit' ),
90 - 'description' => __( 'Displays a ConvertKit Form.', 'convertkit' ),
91 - 'icon' => 'resources/backend/images/block-icon-form.png',
146 + 'title' => $this->get_title(),
147 + 'description' => __( 'Displays a Kit Form.', 'convertkit' ),
148 + 'icon' => $this->get_icon(),
92 149 'category' => 'convertkit',
93 150 'keywords' => array(
94 151 __( 'ConvertKit', 'convertkit' ),
152 + __( 'Kit', 'convertkit' ),
95 153 __( 'Form', 'convertkit' ),
96 154 ),
97 155
98 156 // Function to call when rendering as a block or a shortcode on the frontend web site.
@@ -100,9 +158,9 @@
100 158
101 159 // Shortcode: TinyMCE / QuickTags Modal Width and Height.
102 160 'modal' => array(
103 161 'width' => 500,
104 - 'height' => 100,
162 + 'height' => 55,
105 163 ),
106 164
107 165 // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals.
108 166 'shortcode_include_closing_tag' => false,
@@ -112,9 +170,23 @@
112 170
113 171 // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg.
114 172 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-form.png',
115 173
116 - // Gutenberg: Help description, displayed when no settings defined for a newly added Block.
174 + // Help descriptions, displayed when no API key / resources exist and this block/shortcode is added.
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' ),
180 + ),
181 + 'no_resources' => array(
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' ),
186 + ),
187 +
188 + // Gutenberg: Help descriptions, displayed when no settings defined for a newly added Block.
117 189 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ),
118 190
119 191 // Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor.
120 192 // If not defined, render_callback above will be used.
@@ -130,8 +202,13 @@
130 202
131 203 /* translators: Form name in ConvertKit */
132 204 'gutenberg_form_sticky_bar' => __( 'Sticky bar form "%s" selected. View on the frontend site to see the sticky bar form.', 'convertkit' ),
133 205 ),
206 +
207 + // Whether an API Key exists in the Plugin, and are the required resources (forms) available.
208 + // If no API Key is specified in the Plugin's settings, render the "No API Key" output.
209 + 'has_access_token' => $settings->has_access_and_refresh_token(),
210 + 'has_resources' => $convertkit_forms->exist(),
134 211 );
135 212
136 213 }
137 214
@@ -148,8 +225,19 @@
148 225 'form' => array(
149 226 'type' => 'string',
150 227 ),
151 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 +
152 240 // Always required for Gutenberg.
153 241 'is_gutenberg_example' => array(
154 242 'type' => 'boolean',
155 243 'default' => false,
@@ -158,8 +246,33 @@
158 246
159 247 }
160 248
161 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 + /**
162 275 * Returns this block's Fields
163 276 *
164 277 * @since 1.9.6
165 278 *
@@ -166,31 +279,44 @@
166 279 * @return bool|array
167 280 */
168 281 public function get_fields() {
169 282
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.
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.
176 287 $forms = array();
288 + $legacy_forms = array();
177 289 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
178 290 if ( $convertkit_forms->exist() ) {
179 291 foreach ( $convertkit_forms->get() as $form ) {
180 - $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 + }
181 304 }
182 305 }
183 306
184 307 return array(
185 308 'form' => array(
186 - 'label' => __( 'Form', 'convertkit' ),
187 - 'type' => 'select',
188 - 'values' => $forms,
189 - 'data' => array(
309 + 'label' => __( 'Form', 'convertkit' ),
310 + 'type' => 'resource',
311 + 'resource' => 'forms',
312 + 'values' => $forms,
313 + 'legacy_values' => $legacy_forms,
314 + 'data' => array(
190 315 // Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format
191 316 // (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.
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.
193 319 'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ),
194 320 ),
195 321 ),
196 322 );
@@ -205,13 +331,8 @@
205 331 * @return bool|array
206 332 */
207 333 public function get_panels() {
208 334
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 335 return array(
215 336 'general' => array(
216 337 'label' => __( 'General', 'convertkit' ),
217 338 'fields' => array(
@@ -247,8 +368,19 @@
247 368 * @return string Output
248 369 */
249 370 public function render( $atts ) {
250 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 +
251 383 // Parse shortcode attributes, defining fallback defaults if required.
252 384 $atts = shortcode_atts(
253 385 $this->get_default_values(),
254 386 $this->sanitize_atts( $atts ),
@@ -277,22 +409,32 @@
277 409 }
278 410
279 411 // Get Form HTML.
280 412 $forms = new ConvertKit_Resource_Forms( 'output_form' );
281 - $form = $forms->get_html( $form_id );
413 + $form = $forms->get_html( $form_id, $post_id );
282 414
283 - // 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
284 416 // but does not yet exist in the Plugin's Form Resources.
285 417 // If so, refresh the Form Resources and try again.
286 - if ( is_wp_error( $form ) ) {
418 + if ( is_wp_error( $form ) && $form->get_error_data() === 404 ) {
287 419 // Refresh Forms from the API.
288 - $forms->refresh();
420 + $result = $forms->refresh();
289 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.
290 432 // Get Form HTML again.
291 - $form = $forms->get_html( $form_id );
433 + $form = $forms->get_html( $form_id, $post_id );
292 434 }
293 435
294 - // 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
295 437 // These ConvertKit App shortcodes, for some reason, use a different Form ID than the one presented
296 438 // to us in the API.
297 439 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
298 440 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
@@ -297,12 +439,19 @@
297 439 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
298 440 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
299 441 if ( is_wp_error( $form ) ) {
300 442 // Initialize the API.
301 - $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 + );
302 451
303 452 // 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 );
453 + $form = $api->get_form_html( $form_id, $settings->get_api_key() );
305 454 }
306 455
307 456 // Finally, if we still get an error, there's nothing more we can do. The Form ID isn't valid.
308 457 if ( is_wp_error( $form ) ) {
@@ -310,8 +459,22 @@
310 459 return '<!-- ' . $form->get_error_message() . ' -->';
311 460 }
312 461
313 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 + );
314 477 }
315 478
316 479 /**
317 480 * Filter the block's content immediately before it is output.