PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.5
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.5
3.4.5 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 All 198 releases
← All changes | includes/blocks/class-convertkit-block-form.php +195 -50 2.2.3 → 3.4.5 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,54 +142,59 @@
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.png',
95 - 'category' => 'convertkit',
96 - 'keywords' => array(
146 + 'title' => $this->get_title(),
147 + 'description' => __( 'Displays a Kit Form.', 'convertkit' ),
148 + 'icon' => $this->get_icon(),
149 + 'category' => 'convertkit',
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.
102 - 'render_callback' => array( $this, 'render' ),
157 + 'render_callback' => array( $this, 'render' ),
103 158
104 159 // Shortcode: TinyMCE / QuickTags Modal Width and Height.
105 - 'modal' => array(
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 - 'shortcode_include_closing_tag' => false,
166 + 'shortcode_include_closing_tag' => false,
112 167
113 168 // Gutenberg: Block Icon in Editor.
114 - 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-form.svg' ),
169 + 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-form.svg' ),
115 170
116 171 // 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',
172 + 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-form.png',
118 173
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' ),
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' ),
124 180 ),
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' ),
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' ),
129 186 ),
130 - 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ),
131 187
188 + // Gutenberg: Help descriptions, displayed when no settings defined for a newly added Block.
189 + 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ),
190 +
132 191 // Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor.
133 192 // If not defined, render_callback above will be used.
134 - 'gutenberg_preview_render_callback' => 'convertKitGutenbergFormBlockRenderPreview',
193 + 'gutenberg_preview_render_callback' => 'convertKitGutenbergFormBlockRenderPreview',
135 194
136 195 // General: Any other strings for use in JS that need to support translation / i18n.
137 - 'i18n' => array(
196 + 'i18n' => array(
138 197 /* translators: Form name in ConvertKit */
139 198 'gutenberg_form_modal' => __( 'Modal form "%s" selected. View on the frontend site to see the modal form.', 'convertkit' ),
140 199
141 200 /* translators: Form name in ConvertKit */
@@ -146,10 +205,10 @@
146 205 ),
147 206
148 207 // Whether an API Key exists in the Plugin, and are the required resources (forms) available.
149 208 // 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(),
209 + 'has_access_token' => $settings->has_access_and_refresh_token(),
210 + 'has_resources' => $convertkit_forms->exist(),
152 211 );
153 212
154 213 }
155 214
@@ -166,8 +225,19 @@
166 225 'form' => array(
167 226 'type' => 'string',
168 227 ),
169 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 +
170 240 // Always required for Gutenberg.
171 241 'is_gutenberg_example' => array(
172 242 'type' => 'boolean',
173 243 'default' => false,
@@ -176,8 +246,33 @@
176 246
177 247 }
178 248
179 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 + /**
180 275 * Returns this block's Fields
181 276 *
182 277 * @since 1.9.6
183 278 *
@@ -184,31 +279,44 @@
184 279 * @return bool|array
185 280 */
186 281 public function get_fields() {
187 282
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.
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.
194 287 $forms = array();
288 + $legacy_forms = array();
195 289 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
196 290 if ( $convertkit_forms->exist() ) {
197 291 foreach ( $convertkit_forms->get() as $form ) {
198 - $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 + }
199 304 }
200 305 }
201 306
202 307 return array(
203 308 'form' => array(
204 - 'label' => __( 'Form', 'convertkit' ),
205 - 'type' => 'select',
206 - 'values' => $forms,
207 - 'data' => array(
309 + 'label' => __( 'Form', 'convertkit' ),
310 + 'type' => 'resource',
311 + 'resource' => 'forms',
312 + 'values' => $forms,
313 + 'legacy_values' => $legacy_forms,
314 + 'data' => array(
208 315 // Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format
209 316 // (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.
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.
211 319 'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ),
212 320 ),
213 321 ),
214 322 );
@@ -223,13 +331,8 @@
223 331 * @return bool|array
224 332 */
225 333 public function get_panels() {
226 334
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 335 return array(
233 336 'general' => array(
234 337 'label' => __( 'General', 'convertkit' ),
235 338 'fields' => array(
@@ -265,8 +368,19 @@
265 368 * @return string Output
266 369 */
267 370 public function render( $atts ) {
268 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 +
269 383 // Parse shortcode attributes, defining fallback defaults if required.
270 384 $atts = shortcode_atts(
271 385 $this->get_default_values(),
272 386 $this->sanitize_atts( $atts ),
@@ -295,22 +409,32 @@
295 409 }
296 410
297 411 // Get Form HTML.
298 412 $forms = new ConvertKit_Resource_Forms( 'output_form' );
299 - $form = $forms->get_html( $form_id );
413 + $form = $forms->get_html( $form_id, $post_id );
300 414
301 - // 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
302 416 // but does not yet exist in the Plugin's Form Resources.
303 417 // If so, refresh the Form Resources and try again.
304 - if ( is_wp_error( $form ) ) {
418 + if ( is_wp_error( $form ) && $form->get_error_data() === 404 ) {
305 419 // Refresh Forms from the API.
306 - $forms->refresh();
420 + $result = $forms->refresh();
307 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.
308 432 // Get Form HTML again.
309 - $form = $forms->get_html( $form_id );
433 + $form = $forms->get_html( $form_id, $post_id );
310 434 }
311 435
312 - // 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
313 437 // These ConvertKit App shortcodes, for some reason, use a different Form ID than the one presented
314 438 // to us in the API.
315 439 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
316 440 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
@@ -315,12 +439,19 @@
315 439 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
316 440 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
317 441 if ( is_wp_error( $form ) ) {
318 442 // Initialize the API.
319 - $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 + );
320 451
321 452 // 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 );
453 + $form = $api->get_form_html( $form_id, $settings->get_api_key() );
323 454 }
324 455
325 456 // Finally, if we still get an error, there's nothing more we can do. The Form ID isn't valid.
326 457 if ( is_wp_error( $form ) ) {
@@ -328,8 +459,22 @@
328 459 return '<!-- ' . $form->get_error_message() . ' -->';
329 460 }
330 461
331 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 + );
332 477 }
333 478
334 479 /**
335 480 * Filter the block's content immediately before it is output.