← All changes
|
includes/blocks/class-convertkit-block-form-builder.php
+67
-36
3.3.1
→
3.4.3
View file →
| @@ -76,17 +76,13 @@ | ||
| 76 | 76 | if ( ! array_key_exists( 'post_id', $_REQUEST['convertkit'] ) ) { |
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - // Check reCAPTCHA. | |
| 81 | - $recaptcha = new ConvertKit_Recaptcha(); | |
| 82 | - $recaptcha_response = $recaptcha->verify_recaptcha( | |
| 83 | - ( isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : '' ), | |
| 84 | - 'convertkit_form_builder' | |
| 85 | - ); | |
| 80 | + // Check spam protection. | |
| 81 | + $spam_protection = new ConvertKit_Spam_Protection(); | |
| 86 | 82 | |
| 87 | - // Bail if reCAPTCHA failed. | |
| 88 | - if ( is_wp_error( $recaptcha_response ) ) { | |
| 83 | + // Bail if spam protection failed. | |
| 84 | + if ( is_wp_error( $spam_protection->verify( 'convertkit_form_builder' ) ) ) { | |
| 89 | 85 | return; |
| 90 | 86 | } |
| 91 | 87 | |
| 92 | 88 | // Sanitize form data. |
| @@ -137,17 +133,22 @@ | ||
| 137 | 133 | $settings->debug_enabled(), |
| 138 | 134 | 'block_form_builder' |
| 139 | 135 | ); |
| 140 | 136 | |
| 137 | + // Determine the subscriber state. | |
| 138 | + // If a Form is specified, mark the subscriber as inactive, so the form's double optin is honored. | |
| 139 | + // If a Tag or Sequence is specified, mark the subscriber as active, as there's no double optin for tags or sequences. | |
| 140 | + $subscriber_state = $form_id !== false ? 'inactive' : 'active'; | |
| 141 | + | |
| 141 | 142 | // Create subscriber. |
| 142 | 143 | $result = $api->create_subscriber( |
| 143 | 144 | sanitize_email( $form_data['email'] ), |
| 144 | 145 | array_key_exists( 'first_name', $form_data ) ? $form_data['first_name'] : '', |
| 145 | - 'active', | |
| 146 | + $subscriber_state, | |
| 146 | 147 | $custom_fields |
| 147 | 148 | ); |
| 148 | 149 | |
| 149 | - // Bail if an error occured. | |
| 150 | + // Bail if an error occurred. | |
| 150 | 151 | if ( is_wp_error( $result ) ) { |
| 151 | 152 | // Store entry and return. |
| 152 | 153 | if ( $form_data['store_entries'] ) { |
| 153 | 154 | $entries->upsert( |
| @@ -188,13 +189,22 @@ | ||
| 188 | 189 | $subscriber->set( $result['subscriber']['id'] ); |
| 189 | 190 | |
| 190 | 191 | // If a form was specified, add the subscriber to the form. |
| 191 | 192 | if ( $form_id ) { |
| 192 | - $result = $api->add_subscriber_to_form( | |
| 193 | - $form_id, | |
| 194 | - $result['subscriber']['id'], | |
| 195 | - get_permalink( absint( $form_data['post_id'] ) ) | |
| 196 | - ); | |
| 193 | + // For Legacy Forms, a different endpoint is used. | |
| 194 | + $forms = new ConvertKit_Resource_Forms(); | |
| 195 | + if ( $forms->is_legacy( $form_id ) ) { | |
| 196 | + $result = $api->add_subscriber_to_legacy_form( | |
| 197 | + $form_id, | |
| 198 | + $result['subscriber']['id'] | |
| 199 | + ); | |
| 200 | + } else { | |
| 201 | + $result = $api->add_subscriber_to_form( | |
| 202 | + $form_id, | |
| 203 | + $result['subscriber']['id'], | |
| 204 | + get_permalink( absint( $form_data['post_id'] ) ) | |
| 205 | + ); | |
| 206 | + } | |
| 197 | 207 | |
| 198 | 208 | if ( $form_data['store_entries'] ) { |
| 199 | 209 | $entries->upsert( |
| 200 | 210 | array( |
| @@ -386,10 +396,20 @@ | ||
| 386 | 396 | ), |
| 387 | 397 | ), |
| 388 | 398 | ), |
| 389 | 399 | |
| 400 | + // Help descriptions, displayed when no Access Token / resources exist and this block/shortcode is added. | |
| 401 | + 'no_access_token' => array( | |
| 402 | + 'notice' => __( 'Not connected to Kit.', 'convertkit' ), | |
| 403 | + 'link' => convertkit_get_setup_wizard_plugin_link(), | |
| 404 | + 'link_text' => __( 'Click here to connect your Kit account.', 'convertkit' ), | |
| 405 | + 'instruction_text' => __( 'Connect your Kit account at Settings > Kit, and then refresh this page to configure this block.', 'convertkit' ), | |
| 406 | + ), | |
| 407 | + | |
| 390 | 408 | 'has_access_token' => $settings->has_access_and_refresh_token(), |
| 391 | - 'has_resources' => $convertkit_forms->exist(), | |
| 409 | + | |
| 410 | + // This block works without resources, so we don't need to check if resources exist. | |
| 411 | + 'has_resources' => true, | |
| 392 | 412 | ); |
| 393 | 413 | |
| 394 | 414 | } |
| 395 | 415 | |
| @@ -496,19 +516,29 @@ | ||
| 496 | 516 | * @return bool|array |
| 497 | 517 | */ |
| 498 | 518 | public function get_fields() { |
| 499 | 519 | |
| 500 | - // Get Kit Forms. | |
| 501 | - $forms = new ConvertKit_Resource_Forms( 'block_form_builder' ); | |
| 502 | - $forms_options = array(); | |
| 520 | + // Get Kit Forms. Non-legacy forms populate the sidebar dropdown; | |
| 521 | + // legacy forms are exposed separately as a fallback so the sidebar can | |
| 522 | + // keep displaying a previously-saved legacy form as the current | |
| 523 | + // selection without offering other legacy forms as new choices. | |
| 524 | + $forms = new ConvertKit_Resource_Forms( 'block_form_builder' ); | |
| 525 | + $forms_options = array(); | |
| 526 | + $forms_legacy_options = array(); | |
| 503 | 527 | if ( $forms->exist() ) { |
| 504 | 528 | foreach ( $forms->get() as $form ) { |
| 505 | - // Legacy forms don't include a `format` key, so define them as inline. | |
| 506 | - $forms_options[ $form['id'] ] = sprintf( | |
| 529 | + $label = sprintf( | |
| 507 | 530 | '%s [%s]', |
| 508 | 531 | sanitize_text_field( $form['name'] ), |
| 532 | + // Legacy forms don't include a `format` key, so define them as inline. | |
| 509 | 533 | ( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) |
| 510 | 534 | ); |
| 535 | + | |
| 536 | + if ( ! empty( $form['format'] ) ) { | |
| 537 | + $forms_options[ $form['id'] ] = $label; | |
| 538 | + } else { | |
| 539 | + $forms_legacy_options[ $form['id'] ] = $label; | |
| 540 | + } | |
| 511 | 541 | } |
| 512 | 542 | } |
| 513 | 543 | |
| 514 | 544 | // Get Kit Tags. |
| @@ -554,12 +584,13 @@ | ||
| 554 | 584 | 'value' => 0, |
| 555 | 585 | ), |
| 556 | 586 | ), |
| 557 | 587 | 'form_id' => array( |
| 558 | - 'label' => __( 'Form', 'convertkit' ), | |
| 559 | - 'type' => 'select', | |
| 560 | - 'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ), | |
| 561 | - 'values' => $forms_options, | |
| 588 | + 'label' => __( 'Form', 'convertkit' ), | |
| 589 | + 'type' => 'select', | |
| 590 | + 'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ), | |
| 591 | + 'values' => $forms_options, | |
| 592 | + 'legacy_values' => $forms_legacy_options, | |
| 562 | 593 | ), |
| 563 | 594 | 'tag_id' => array( |
| 564 | 595 | 'label' => __( 'Tag', 'convertkit' ), |
| 565 | 596 | 'type' => 'select', |
| @@ -706,25 +737,25 @@ | ||
| 706 | 737 | '<button type="submit"$1>$2</button>', |
| 707 | 738 | $block_content |
| 708 | 739 | ); |
| 709 | 740 | |
| 710 | - // Return the button if reCAPTCHA does not need to be used. | |
| 711 | - $settings = new ConvertKit_Settings(); | |
| 712 | - if ( ! $settings->has_recaptcha_site_and_secret_keys() ) { | |
| 741 | + // Return the button if no spam protection provider is active. | |
| 742 | + $spam_protection = new ConvertKit_Spam_Protection(); | |
| 743 | + $provider = $spam_protection->get_active_provider(); | |
| 744 | + if ( ! $provider ) { | |
| 713 | 745 | return $block_content; |
| 714 | 746 | } |
| 715 | 747 | |
| 716 | - // Enqueue reCAPTCHA JS. | |
| 717 | - $recaptcha = new ConvertKit_Recaptcha(); | |
| 718 | - $recaptcha->enqueue_scripts(); | |
| 748 | + // Enqueue the spam protection provider's JS. | |
| 749 | + $provider->enqueue_scripts(); | |
| 719 | 750 | |
| 720 | - // Add reCAPTCHA attributes to button. | |
| 751 | + // Parse the button's DOM. | |
| 721 | 752 | $parser = new ConvertKit_HTML_Parser( $block_content ); |
| 722 | 753 | $button = $parser->xpath->query( '//button' )->item( 0 ); |
| 723 | - $button->setAttribute( 'data-sitekey', esc_attr( $settings->recaptcha_site_key() ) ); // @phpstan-ignore-line | |
| 724 | - $button->setAttribute( 'data-callback', 'convertKitRecaptchaFormSubmit' ); // @phpstan-ignore-line | |
| 725 | - $button->setAttribute( 'data-action', 'convertkit_form_builder' ); // @phpstan-ignore-line | |
| 726 | - $button->setAttribute( 'class', trim( $button->getAttribute( 'class' ) . ' g-recaptcha' ) ); // @phpstan-ignore-line | |
| 754 | + | |
| 755 | + // Attach the spam protection provider's attributes/elements to the form/button as necessary. | |
| 756 | + // $button is narrowed from DOMNode to DOMElement by the //button xpath expression above. | |
| 757 | + $provider->attach_to_form_button_dom( $parser, $button, 'convertkit_form_builder' ); // @phpstan-ignore-line | |
| 727 | 758 | |
| 728 | 759 | // Return button HTML. |
| 729 | 760 | return $parser->get_body_html(); |
| 730 | 761 | |