← All changes
|
includes/blocks/class-convertkit-block-form-builder.php
+67
-1
3.4.2
→
3.4.4
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. |
| @@ -800,8 +839,35 @@ | ||
| 800 | 839 | $subscribed_message = $parser->html->createElement( 'div' ); |
| 801 | 840 | $subscribed_message->setAttribute( 'class', 'convertkit-form-builder-subscribed-message' ); |
| 802 | 841 | $subscribed_message->appendChild( $parser->html->createTextNode( $atts['text_if_subscribed'] ) ); |
| 803 | 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 | + } | |
| 804 | 870 | } |
| 805 | 871 | |
| 806 | 872 | // Add hidden fields. |
| 807 | 873 | $fields = array( |