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-builder.php +87 -10 3.3.7 → 3.4.5 View file →
@@ -24,8 +24,28 @@
24 24 */
25 25 public $subscriber_id = false;
26 26
27 27 /**
28 + * Holds the WP_Error object if the form submission failed,
29 + * to display on screen as a notice.
30 + *
31 + * @since 3.4.4
32 + *
33 + * @var bool|WP_Error
34 + */
35 + public $error = false;
36 +
37 + /**
38 + * Holds the number of times this block has been rendered on the Post,
39 + * to ensure error notice IDs are unique.
40 + *
41 + * @since 3.4.4
42 + *
43 + * @var int
44 + */
45 + public $render_count = 0;
46 +
47 + /**
28 48 * Constructor
29 49 *
30 50 * @since 3.0.0
31 51 */
@@ -80,9 +100,11 @@
80 100 // Check spam protection.
81 101 $spam_protection = new ConvertKit_Spam_Protection();
82 102
83 103 // Bail if spam protection failed.
84 - if ( is_wp_error( $spam_protection->verify( 'convertkit_form_builder' ) ) ) {
104 + $spam_protection_result = $spam_protection->verify( 'convertkit_form_builder' );
105 + if ( is_wp_error( $spam_protection_result ) ) {
106 + $this->error = $spam_protection_result;
85 107 return;
86 108 }
87 109
88 110 // Sanitize form data.
@@ -87,8 +109,18 @@
87 109
88 110 // Sanitize form data.
89 111 $form_data = map_deep( wp_unslash( $_REQUEST['convertkit'] ), 'sanitize_text_field' );
90 112
113 + // Bail if the email address is invalid. The entry isn't stored, as an invalid
114 + // email address is of no use to the creator.
115 + if ( ! is_email( $form_data['email'] ) ) {
116 + $this->error = new WP_Error(
117 + 'convertkit_block_form_builder_invalid_email',
118 + __( 'Please enter a valid email address.', 'convertkit' )
119 + );
120 + return;
121 + }
122 +
91 123 // Build custom fields, if any were specified.
92 124 $custom_fields = array();
93 125 if ( array_key_exists( 'custom_fields', $form_data ) ) {
94 126 $custom_fields = $form_data['custom_fields'];
@@ -120,8 +152,13 @@
120 152 'api_error' => __( 'Plugin Access Token not configured', 'convertkit' ),
121 153 )
122 154 );
123 155 }
156 +
157 + $this->error = new WP_Error(
158 + 'convertkit_block_form_builder_no_access_token',
159 + __( 'Sorry, we were unable to subscribe you. Please try again later.', 'convertkit' )
160 + );
124 161 return;
125 162 }
126 163
127 164 // Initialize the API.
@@ -164,8 +201,10 @@
164 201 'api_error' => $result->get_error_message(),
165 202 )
166 203 );
167 204 }
205 +
206 + $this->error = $result;
168 207 return;
169 208 }
170 209
171 210 // Store entry.
@@ -516,19 +555,29 @@
516 555 * @return bool|array
517 556 */
518 557 public function get_fields() {
519 558
520 - // Get Kit Forms.
521 - $forms = new ConvertKit_Resource_Forms( 'block_form_builder' );
522 - $forms_options = array();
559 + // Get Kit Forms. Non-legacy forms populate the sidebar dropdown;
560 + // legacy forms are exposed separately as a fallback so the sidebar can
561 + // keep displaying a previously-saved legacy form as the current
562 + // selection without offering other legacy forms as new choices.
563 + $forms = new ConvertKit_Resource_Forms( 'block_form_builder' );
564 + $forms_options = array();
565 + $forms_legacy_options = array();
523 566 if ( $forms->exist() ) {
524 567 foreach ( $forms->get() as $form ) {
525 - // Legacy forms don't include a `format` key, so define them as inline.
526 - $forms_options[ $form['id'] ] = sprintf(
568 + $label = sprintf(
527 569 '%s [%s]',
528 570 sanitize_text_field( $form['name'] ),
571 + // Legacy forms don't include a `format` key, so define them as inline.
529 572 ( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' )
530 573 );
574 +
575 + if ( ! empty( $form['format'] ) ) {
576 + $forms_options[ $form['id'] ] = $label;
577 + } else {
578 + $forms_legacy_options[ $form['id'] ] = $label;
579 + }
531 580 }
532 581 }
533 582
534 583 // Get Kit Tags.
@@ -574,12 +623,13 @@
574 623 'value' => 0,
575 624 ),
576 625 ),
577 626 'form_id' => array(
578 - 'label' => __( 'Form', 'convertkit' ),
579 - 'type' => 'select',
580 - 'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ),
581 - 'values' => $forms_options,
627 + 'label' => __( 'Form', 'convertkit' ),
628 + 'type' => 'select',
629 + 'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ),
630 + 'values' => $forms_options,
631 + 'legacy_values' => $forms_legacy_options,
582 632 ),
583 633 'tag_id' => array(
584 634 'label' => __( 'Tag', 'convertkit' ),
585 635 'type' => 'select',
@@ -789,8 +839,35 @@
789 839 $subscribed_message = $parser->html->createElement( 'div' );
790 840 $subscribed_message->setAttribute( 'class', 'convertkit-form-builder-subscribed-message' );
791 841 $subscribed_message->appendChild( $parser->html->createTextNode( $atts['text_if_subscribed'] ) );
792 842 $form->insertBefore( $subscribed_message, $form->firstChild ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
843 + }
844 +
845 + // Add error notice if the submission failed.
846 + if ( is_wp_error( $this->error ) ) {
847 + ++$this->render_count;
848 + $error_id = 'convertkit-form-builder-error-' . $this->render_count;
849 +
850 + $error_notice = $parser->html->createElement( 'div' );
851 + $error_notice->setAttribute( 'id', $error_id );
852 + $error_notice->setAttribute( 'class', 'convertkit-form-builder-notice convertkit-form-builder-notice-error' );
853 + $error_notice->setAttribute( 'role', 'alert' );
854 + $error_notice->setAttribute( 'tabindex', '-1' );
855 + $error_notice->appendChild( $parser->html->createTextNode( $this->error->get_error_message() ) );
856 + $form->insertBefore( $error_notice, $form->firstChild ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
857 +
858 + // Focus the email field if it caused the error, so screen readers and
859 + // browsers move to it. Otherwise focus the notice, as the error isn't
860 + // specific to a field.
861 + // Query within the form, as it's not yet appended to the document.
862 + $email_field = $parser->xpath->query( './/input[@name="convertkit[email]"]', $form )->item( 0 );
863 + if ( $email_field && $this->error->get_error_code() === 'convertkit_block_form_builder_invalid_email' ) {
864 + $email_field->setAttribute( 'aria-invalid', 'true' ); // @phpstan-ignore-line
865 + $email_field->setAttribute( 'aria-describedby', $error_id ); // @phpstan-ignore-line
866 + $email_field->setAttribute( 'autofocus', 'autofocus' ); // @phpstan-ignore-line
867 + } else {
868 + $error_notice->setAttribute( 'autofocus', 'autofocus' );
869 + }
793 870 }
794 871
795 872 // Add hidden fields.
796 873 $fields = array(