← All changes
|
includes/blocks/class-convertkit-block-form-builder.php
+47
-30
3.3.2
→
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. |
| @@ -150,9 +146,9 @@ | ||
| 150 | 146 | $subscriber_state, |
| 151 | 147 | $custom_fields |
| 152 | 148 | ); |
| 153 | 149 | |
| 154 | - // Bail if an error occured. | |
| 150 | + // Bail if an error occurred. | |
| 155 | 151 | if ( is_wp_error( $result ) ) { |
| 156 | 152 | // Store entry and return. |
| 157 | 153 | if ( $form_data['store_entries'] ) { |
| 158 | 154 | $entries->upsert( |
| @@ -400,10 +396,20 @@ | ||
| 400 | 396 | ), |
| 401 | 397 | ), |
| 402 | 398 | ), |
| 403 | 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 | + | |
| 404 | 408 | 'has_access_token' => $settings->has_access_and_refresh_token(), |
| 405 | - '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, | |
| 406 | 412 | ); |
| 407 | 413 | |
| 408 | 414 | } |
| 409 | 415 | |
| @@ -510,19 +516,29 @@ | ||
| 510 | 516 | * @return bool|array |
| 511 | 517 | */ |
| 512 | 518 | public function get_fields() { |
| 513 | 519 | |
| 514 | - // Get Kit Forms. | |
| 515 | - $forms = new ConvertKit_Resource_Forms( 'block_form_builder' ); | |
| 516 | - $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(); | |
| 517 | 527 | if ( $forms->exist() ) { |
| 518 | 528 | foreach ( $forms->get() as $form ) { |
| 519 | - // Legacy forms don't include a `format` key, so define them as inline. | |
| 520 | - $forms_options[ $form['id'] ] = sprintf( | |
| 529 | + $label = sprintf( | |
| 521 | 530 | '%s [%s]', |
| 522 | 531 | sanitize_text_field( $form['name'] ), |
| 532 | + // Legacy forms don't include a `format` key, so define them as inline. | |
| 523 | 533 | ( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) |
| 524 | 534 | ); |
| 535 | + | |
| 536 | + if ( ! empty( $form['format'] ) ) { | |
| 537 | + $forms_options[ $form['id'] ] = $label; | |
| 538 | + } else { | |
| 539 | + $forms_legacy_options[ $form['id'] ] = $label; | |
| 540 | + } | |
| 525 | 541 | } |
| 526 | 542 | } |
| 527 | 543 | |
| 528 | 544 | // Get Kit Tags. |
| @@ -568,12 +584,13 @@ | ||
| 568 | 584 | 'value' => 0, |
| 569 | 585 | ), |
| 570 | 586 | ), |
| 571 | 587 | 'form_id' => array( |
| 572 | - 'label' => __( 'Form', 'convertkit' ), | |
| 573 | - 'type' => 'select', | |
| 574 | - 'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ), | |
| 575 | - '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, | |
| 576 | 593 | ), |
| 577 | 594 | 'tag_id' => array( |
| 578 | 595 | 'label' => __( 'Tag', 'convertkit' ), |
| 579 | 596 | 'type' => 'select', |
| @@ -720,25 +737,25 @@ | ||
| 720 | 737 | '<button type="submit"$1>$2</button>', |
| 721 | 738 | $block_content |
| 722 | 739 | ); |
| 723 | 740 | |
| 724 | - // Return the button if reCAPTCHA does not need to be used. | |
| 725 | - $settings = new ConvertKit_Settings(); | |
| 726 | - 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 ) { | |
| 727 | 745 | return $block_content; |
| 728 | 746 | } |
| 729 | 747 | |
| 730 | - // Enqueue reCAPTCHA JS. | |
| 731 | - $recaptcha = new ConvertKit_Recaptcha(); | |
| 732 | - $recaptcha->enqueue_scripts(); | |
| 748 | + // Enqueue the spam protection provider's JS. | |
| 749 | + $provider->enqueue_scripts(); | |
| 733 | 750 | |
| 734 | - // Add reCAPTCHA attributes to button. | |
| 751 | + // Parse the button's DOM. | |
| 735 | 752 | $parser = new ConvertKit_HTML_Parser( $block_content ); |
| 736 | 753 | $button = $parser->xpath->query( '//button' )->item( 0 ); |
| 737 | - $button->setAttribute( 'data-sitekey', esc_attr( $settings->recaptcha_site_key() ) ); // @phpstan-ignore-line | |
| 738 | - $button->setAttribute( 'data-callback', 'convertKitRecaptchaFormSubmit' ); // @phpstan-ignore-line | |
| 739 | - $button->setAttribute( 'data-action', 'convertkit_form_builder' ); // @phpstan-ignore-line | |
| 740 | - $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 | |
| 741 | 758 | |
| 742 | 759 | // Return button HTML. |
| 743 | 760 | return $parser->get_body_html(); |
| 744 | 761 | |