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 +193 -50 2.2.33.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 *
@@ -88,54 +140,59 @@
88 140 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
89 141 $settings = new ConvertKit_Settings();
90 142
91 143 return array(
92 - 'title' => __( 'ConvertKit Form', 'convertkit' ),
93 - 'description' => __( 'Displays a ConvertKit Form.', 'convertkit' ),
94 - 'icon' => 'resources/backend/images/block-icon-form.png',
95 - 'category' => 'convertkit',
96 - 'keywords' => array(
144 + 'title' => $this->get_title(),
145 + 'description' => __( 'Displays a Kit Form.', 'convertkit' ),
146 + 'icon' => $this->get_icon(),
147 + 'category' => 'convertkit',
148 + 'keywords' => array(
97 149 __( 'ConvertKit', 'convertkit' ),
150 + __( 'Kit', 'convertkit' ),
98 151 __( 'Form', 'convertkit' ),
99 152 ),
100 153
101 154 // Function to call when rendering as a block or a shortcode on the frontend web site.
102 - 'render_callback' => array( $this, 'render' ),
155 + 'render_callback' => array( $this, 'render' ),
103 156
104 157 // Shortcode: TinyMCE / QuickTags Modal Width and Height.
105 - 'modal' => array(
158 + 'modal' => array(
106 159 'width' => 500,
107 - 'height' => 106,
160 + 'height' => 55,
108 161 ),
109 162
110 163 // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals.
111 - 'shortcode_include_closing_tag' => false,
164 + 'shortcode_include_closing_tag' => false,
112 165
113 166 // Gutenberg: Block Icon in Editor.
114 - 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-form.svg' ),
167 + 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-form.svg' ),
115 168
116 169 // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg.
117 - 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-form.png',
170 + 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-form.png',
118 171
119 - // Gutenberg: Help descriptions, displayed when no settings defined for a newly added Block, or API keys / forms don't exist.
120 - 'gutenberg_help_description_no_api_key' => array(
121 - 'notice' => __( 'No API Key specified.', 'convertkit' ),
122 - 'link' => convertkit_get_settings_link(),
123 - 'link_text' => __( 'Click here to add your API Key.', 'convertkit' ),
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' ),
124 178 ),
125 - 'gutenberg_help_description_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' ),
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' ),
129 184 ),
130 - 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ),
131 185
186 + // Gutenberg: Help descriptions, displayed when no settings defined for a newly added Block.
187 + 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ),
188 +
132 189 // Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor.
133 190 // If not defined, render_callback above will be used.
134 - 'gutenberg_preview_render_callback' => 'convertKitGutenbergFormBlockRenderPreview',
191 + 'gutenberg_preview_render_callback' => 'convertKitGutenbergFormBlockRenderPreview',
135 192
136 193 // General: Any other strings for use in JS that need to support translation / i18n.
137 - 'i18n' => array(
194 + 'i18n' => array(
138 195 /* translators: Form name in ConvertKit */
139 196 'gutenberg_form_modal' => __( 'Modal form "%s" selected. View on the frontend site to see the modal form.', 'convertkit' ),
140 197
141 198 /* translators: Form name in ConvertKit */
@@ -146,10 +203,10 @@
146 203 ),
147 204
148 205 // Whether an API Key exists in the Plugin, and are the required resources (forms) available.
149 206 // If no API Key is specified in the Plugin's settings, render the "No API Key" output.
150 - 'has_api_key' => $settings->has_api_key_and_secret(),
151 - 'has_resources' => $convertkit_forms->exist(),
207 + 'has_access_token' => $settings->has_access_and_refresh_token(),
208 + 'has_resources' => $convertkit_forms->exist(),
152 209 );
153 210
154 211 }
155 212
@@ -166,8 +223,19 @@
166 223 'form' => array(
167 224 'type' => 'string',
168 225 ),
169 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 +
170 238 // Always required for Gutenberg.
171 239 'is_gutenberg_example' => array(
172 240 'type' => 'boolean',
173 241 'default' => false,
@@ -176,8 +244,33 @@
176 244
177 245 }
178 246
179 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 + /**
180 273 * Returns this block's Fields
181 274 *
182 275 * @since 1.9.6
183 276 *
@@ -184,31 +277,44 @@
184 277 * @return bool|array
185 278 */
186 279 public function get_fields() {
187 280
188 - // Bail if the request is not for the WordPress Administration or frontend editor.
189 - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
190 - return false;
191 - }
192 -
193 - // 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.
194 285 $forms = array();
286 + $legacy_forms = array();
195 287 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
196 288 if ( $convertkit_forms->exist() ) {
197 289 foreach ( $convertkit_forms->get() as $form ) {
198 - $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 + }
199 302 }
200 303 }
201 304
202 305 return array(
203 306 'form' => array(
204 - 'label' => __( 'Form', 'convertkit' ),
205 - 'type' => 'select',
206 - 'values' => $forms,
207 - 'data' => array(
307 + 'label' => __( 'Form', 'convertkit' ),
308 + 'type' => 'resource',
309 + 'resource' => 'forms',
310 + 'values' => $forms,
311 + 'legacy_values' => $legacy_forms,
312 + 'data' => array(
208 313 // Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format
209 314 // (modal, slide in, sticky bar) and output a message in the block editor for the preview to explain
210 - // 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.
211 317 'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ),
212 318 ),
213 319 ),
214 320 );
@@ -223,13 +329,8 @@
223 329 * @return bool|array
224 330 */
225 331 public function get_panels() {
226 332
227 - // Bail if the request is not for the WordPress Administration or frontend editor.
228 - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
229 - return false;
230 - }
231 -
232 333 return array(
233 334 'general' => array(
234 335 'label' => __( 'General', 'convertkit' ),
235 336 'fields' => array(
@@ -265,8 +366,19 @@
265 366 * @return string Output
266 367 */
267 368 public function render( $atts ) {
268 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 +
269 381 // Parse shortcode attributes, defining fallback defaults if required.
270 382 $atts = shortcode_atts(
271 383 $this->get_default_values(),
272 384 $this->sanitize_atts( $atts ),
@@ -295,22 +407,32 @@
295 407 }
296 408
297 409 // Get Form HTML.
298 410 $forms = new ConvertKit_Resource_Forms( 'output_form' );
299 - $form = $forms->get_html( $form_id );
411 + $form = $forms->get_html( $form_id, $post_id );
300 412
301 - // 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
302 414 // but does not yet exist in the Plugin's Form Resources.
303 415 // If so, refresh the Form Resources and try again.
304 - if ( is_wp_error( $form ) ) {
416 + if ( is_wp_error( $form ) && $form->get_error_data() === 404 ) {
305 417 // Refresh Forms from the API.
306 - $forms->refresh();
418 + $result = $forms->refresh();
307 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.
308 430 // Get Form HTML again.
309 - $form = $forms->get_html( $form_id );
431 + $form = $forms->get_html( $form_id, $post_id );
310 432 }
311 433
312 - // 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
313 435 // These ConvertKit App shortcodes, for some reason, use a different Form ID than the one presented
314 436 // to us in the API.
315 437 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
316 438 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
@@ -315,12 +437,19 @@
315 437 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
316 438 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
317 439 if ( is_wp_error( $form ) ) {
318 440 // Initialize the API.
319 - $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 + );
320 449
321 450 // Return Legacy Form HTML from the API, which bypasses any internal Plugin check to see if the Form ID exists.
322 - $form = $api->get_form_html( $form_id );
451 + $form = $api->get_form_html( $form_id, $settings->get_api_key() );
323 452 }
324 453
325 454 // Finally, if we still get an error, there's nothing more we can do. The Form ID isn't valid.
326 455 if ( is_wp_error( $form ) ) {
@@ -328,8 +457,22 @@
328 457 return '<!-- ' . $form->get_error_message() . ' -->';
329 458 }
330 459
331 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 + );
332 475 }
333 476
334 477 /**
335 478 * Filter the block's content immediately before it is output.