Skip to content

Commit

Permalink
Merge pull request #683 from ConvertKit/contact-form-7-subscribe-option
Browse files Browse the repository at this point in the history
Contact Form 7: Add `Subscribe` option
  • Loading branch information
n7studios authored Jul 19, 2024
2 parents dc61f01 + 87d1712 commit 5ff3206
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 58 deletions.
27 changes: 27 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,30 @@ function convertkit_get_file_contents( $local_file ) {
return $contents;

}

/**
* Returns a dropdown field commonly used for settings, comprising of:
* - Do not subscribe
* - Subscribe
* - Subscribe to Form
*
* @since 2.5.2
*
* @param string $name Field name.
* @param string $value Field value.
* @param string $id Field ID attribute.
* @param string $css_class Field CSS class(es).
*/
function convertkit_get_subscription_dropdown_field( $name, $value, $id, $css_class = '' ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter

// Load resource classes.
$forms = new ConvertKit_Resource_Forms( 'contact_form_7' );

ob_start();
include CONVERTKIT_PLUGIN_PATH . '/views/backend/subscription-dropdown-field.php';
$output = trim( ob_get_clean() );

// Return output.
return $output;

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public function print_section_info() {
);
?>
</p>
<p>
<?php echo esc_html_e( 'Each Contact Form 7 Form has the following ConvertKit options:', 'convertkit' ); ?>
<br />
<code><?php echo esc_html_e( 'Do not subscribe', 'convertkit' ); ?></code>: <?php esc_html_e( 'Do not subscribe the email address to ConvertKit', 'convertkit' ); ?>
<br />
<code><?php echo esc_html_e( 'Subscribe', 'convertkit' ); ?></code>: <?php esc_html_e( 'Subscribes the email address to ConvertKit', 'convertkit' ); ?>
<br />
<code><?php echo esc_html_e( 'Form', 'convertkit' ); ?></code>: <?php esc_html_e( 'Susbcribes the email address to ConvertKit, and adds the subscriber to the ConvertKit Form', 'convertkit' ); ?>
</p>
<?php

}
Expand Down Expand Up @@ -97,20 +106,6 @@ public function render() {

do_settings_sections( $this->settings_key );

// Get Forms.
$forms = new ConvertKit_Resource_Forms( 'contact_form_7' );
$creator_network_recommendations = new ConvertKit_Resource_Creator_Network_Recommendations( 'contact_form_7' );

// Bail with an error if no ConvertKit Forms exist.
if ( ! $forms->exist() ) {
$this->output_error( __( 'No Forms exist on ConvertKit.', 'convertkit' ) );
$this->render_container_end();
return;
}

// Get Creator Network Recommendations script.
$creator_network_recommendations_enabled = $creator_network_recommendations->enabled();

// Get Contact Form 7 Forms.
$cf7_forms = $this->get_cf7_forms();

Expand All @@ -121,10 +116,14 @@ public function render() {
return;
}

// Get Creator Network Recommendations script.
$creator_network_recommendations = new ConvertKit_Resource_Creator_Network_Recommendations( 'contact_form_7' );
$creator_network_recommendations_enabled = $creator_network_recommendations->enabled();

// Setup WP_List_Table.
$table = new Multi_Value_Field_Table();
$table->add_column( 'title', __( 'Contact Form 7 Form', 'convertkit' ), true );
$table->add_column( 'form', __( 'ConvertKit Form', 'convertkit' ), false );
$table->add_column( 'form', __( 'ConvertKit', 'convertkit' ), false );
$table->add_column( 'email', __( 'Contact Form 7 Email Field', 'convertkit' ), false );
$table->add_column( 'name', __( 'Contact Form 7 Name Field', 'convertkit' ), false );
$table->add_column( 'creator_network_recommendations', __( 'Enable Creator Network Recommendations', 'convertkit' ), false );
Expand All @@ -134,14 +133,11 @@ public function render() {
// Build row.
$table_row = array(
'title' => $cf7_form['name'],
'form' => $forms->get_select_field_all(
'form' => convertkit_get_subscription_dropdown_field(
'_wp_convertkit_integration_contactform7_settings[' . $cf7_form['id'] . ']',
(string) $this->settings->get_convertkit_subscribe_setting_by_cf7_form_id( $cf7_form['id'] ),
'_wp_convertkit_integration_contactform7_settings_' . $cf7_form['id'],
false,
(string) $this->settings->get_convertkit_form_id_by_cf7_form_id( $cf7_form['id'] ),
array(
'' => __( 'None', 'convertkit' ),
)
'widefat'
),
'email' => 'your-email',
'name' => 'your-name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public function has_settings() {
}

/**
* Returns the ConvertKit Form ID that is mapped against the given Contact Form 7 Form ID.
* Returns the ConvertKit subscription setting for the given Contact Form 7 Form ID.
*
* @since 1.9.6
*
* @param int $cf7_form_id Contact Form 7 Form ID.
* @return bool|int
* @return bool|string|int
*/
public function get_convertkit_form_id_by_cf7_form_id( $cf7_form_id ) {
public function get_convertkit_subscribe_setting_by_cf7_form_id( $cf7_form_id ) {

// Bail if no settings exist.
if ( ! $this->has_settings() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public function handle_wpcf7_submit( $contact_form, $result ) {
}

// Get ConvertKit Form ID mapped to this Contact Form 7 Form.
$contact_form_7_settings = new ConvertKit_ContactForm7_Settings();
$convertkit_form_id = $contact_form_7_settings->get_convertkit_form_id_by_cf7_form_id( $contact_form->id() );
$contact_form_7_settings = new ConvertKit_ContactForm7_Settings();
$convertkit_subscribe_setting = $contact_form_7_settings->get_convertkit_subscribe_setting_by_cf7_form_id( $contact_form->id() );

// If no ConvertKit Form is mapped to this Contact Form 7 Form, bail.
if ( ! $convertkit_form_id ) {
// If no ConvertKit subscribe setting is defined, bail.
if ( ! $convertkit_subscribe_setting ) {
return;
}

Expand Down Expand Up @@ -129,18 +129,26 @@ public function handle_wpcf7_submit( $contact_form, $result ) {
'contact_form_7'
);

// If the setting is 'Subscribe', just create the subscriber without assigning to a resource.
if ( $convertkit_subscribe_setting === 'subscribe' ) {
return $api->create_subscriber(
$email,
$first_name
);
}

// For Legacy Forms, a different endpoint is used.
$forms = new ConvertKit_Resource_Forms();
if ( $forms->is_legacy( $convertkit_form_id ) ) {
if ( $forms->is_legacy( $convertkit_subscribe_setting ) ) {
return $api->legacy_form_subscribe(
$convertkit_form_id,
(int) $convertkit_subscribe_setting,
$email,
$first_name
);
}

return $api->form_subscribe(
$convertkit_form_id,
(int) $convertkit_subscribe_setting,
$email,
$first_name
);
Expand Down
6 changes: 3 additions & 3 deletions tests/_support/Helper/Acceptance/ConvertKitAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function apiCheckSubscriberExists($I, $emailAddress)
$I->assertGreaterThan(0, $results['pagination']['total_count']);
$I->assertEquals($emailAddress, $results['subscribers'][0]['email_address']);
}

/**
* Check the given subscriber ID has been assigned to the given tag ID.
*
Expand Down Expand Up @@ -114,12 +113,13 @@ public function apiCheckSubscriberDoesNotExist($I, $emailAddress)
'subscribers',
'GET',
[
'email_address' => $emailAddress,
'email_address' => $emailAddress,
'include_total_count' => true,
]
);

// Check no subscribers are returned by this request.
$I->assertEquals(0, $results['total_subscribers']);
$I->assertEquals(0, $results['pagination']['total_count']);
}

/**
Expand Down
174 changes: 150 additions & 24 deletions tests/acceptance/integrations/other/ContactForm7FormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,6 @@ public function testSettingsContactForm7WhenNoCredentials(AcceptanceTester $I)
$I->dontSeeElementInDOM('table.wp-list-table');
}

/**
* Tests that no Contact Form 7 settings display and a 'No Forms exist on ConvertKit'
* notification displays when no Forms exist.
*
* @since 2.2.7
*
* @param AcceptanceTester $I Tester.
*/
public function testSettingsContactForm7WhenNoForms(AcceptanceTester $I)
{
// Setup Plugin.
$I->setupConvertKitPluginCredentialsNoData($I);
$I->setupConvertKitPluginResourcesNoData($I);

// Load Contact Form 7 Plugin Settings.
$I->amOnAdminPage('options-general.php?page=_wp_convertkit_settings&tab=contactform7');

// Confirm notice is displayed.
$I->see('No Forms exist on ConvertKit.');

// Confirm no settings table is displayed.
$I->dontSeeElementInDOM('table.wp-list-table');
}

/**
* Test that saving a Contact Form 7 to ConvertKit Form Mapping works.
*
Expand Down Expand Up @@ -210,6 +186,156 @@ function($I) {
$I->apiCheckSubscriberExists($I, $emailAddress);
}

/**
* Test that setting a Contact Form 7 Form to the '(Do not subscribe)' option works.
*
* @since 2.5.2
*
* @param AcceptanceTester $I Tester.
*/
public function testSettingsContactForm7DoNotSubscribeOption(AcceptanceTester $I)
{
// Setup ConvertKit Plugin.
$I->setupConvertKitPlugin($I);
$I->setupConvertKitPluginResources($I);

// Create Contact Form 7 Form.
$contactForm7ID = $this->_createContactForm7Form($I);

// Load Contact Form 7 Plugin Settings.
$I->amOnAdminPage('options-general.php?page=_wp_convertkit_settings&tab=contactform7');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Check that a Form Mapping option is displayed.
$I->seeElementInDOM('#_wp_convertkit_integration_contactform7_settings_' . $contactForm7ID);

// Set Contact Form 7 setting to subscribe.
$I->selectOption('#_wp_convertkit_integration_contactform7_settings_' . $contactForm7ID, '(Do not subscribe)');

$I->click('Save Changes');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Check the value of the Form field matches the input provided.
$I->seeOptionIsSelected('#_wp_convertkit_integration_contactform7_settings_' . $contactForm7ID, '(Do not subscribe)');

// Create Page with Contact Form 7 Shortcode.
$I->havePageInDatabase(
[
'post_title' => 'ConvertKit: Contact Form 7: Do Not Subscribe',
'post_name' => 'convertkit-contact-form-7-do-not-subscribe',
'post_content' => 'Form:
[contact-form-7 id="' . $contactForm7ID . '"]',
]
);

// Load the Page on the frontend site.
$I->amOnPage('/convertkit-contact-form-7-do-not-subscribe');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Define email address for this test.
$emailAddress = $I->generateEmailAddress();

// Complete Name and Email.
$I->fillField('input[name=your-name]', 'ConvertKit Name');
$I->fillField('input[name=your-email]', $emailAddress);
$I->fillField('input[name=your-subject]', 'ConvertKit Subject');

// Submit Form.
$I->click('Submit');

// Confirm the form submitted without errors.
$I->performOn(
'form.sent',
function($I) {
$I->see('Thank you for your message. It has been sent.');
}
);

// Confirm that the email address was not added to ConvertKit.
$I->apiCheckSubscriberDoesNotExist($I, $emailAddress);
}

/**
* Test that setting a Contact Form 7 Form to the 'Subscribe' option works.
*
* @since 2.5.2
*
* @param AcceptanceTester $I Tester.
*/
public function testSettingsContactForm7SubscribeOption(AcceptanceTester $I)
{
// Setup ConvertKit Plugin.
$I->setupConvertKitPlugin($I);
$I->setupConvertKitPluginResources($I);

// Create Contact Form 7 Form.
$contactForm7ID = $this->_createContactForm7Form($I);

// Load Contact Form 7 Plugin Settings.
$I->amOnAdminPage('options-general.php?page=_wp_convertkit_settings&tab=contactform7');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Check that a Form Mapping option is displayed.
$I->seeElementInDOM('#_wp_convertkit_integration_contactform7_settings_' . $contactForm7ID);

// Set Contact Form 7 setting to subscribe.
$I->selectOption('#_wp_convertkit_integration_contactform7_settings_' . $contactForm7ID, 'Subscribe');

$I->click('Save Changes');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Check the value of the Form field matches the input provided.
$I->seeOptionIsSelected('#_wp_convertkit_integration_contactform7_settings_' . $contactForm7ID, 'Subscribe');

// Create Page with Contact Form 7 Shortcode.
$I->havePageInDatabase(
[
'post_title' => 'ConvertKit: Contact Form 7: Subscribe',
'post_name' => 'convertkit-contact-form-7-subscribe',
'post_content' => 'Form:
[contact-form-7 id="' . $contactForm7ID . '"]',
]
);

// Load the Page on the frontend site.
$I->amOnPage('/convertkit-contact-form-7-subscribe');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Define email address for this test.
$emailAddress = $I->generateEmailAddress();

// Complete Name and Email.
$I->fillField('input[name=your-name]', 'ConvertKit Name');
$I->fillField('input[name=your-email]', $emailAddress);
$I->fillField('input[name=your-subject]', 'ConvertKit Subject');

// Submit Form.
$I->click('Submit');

// Confirm the form submitted without errors.
$I->performOn(
'form.sent',
function($I) {
$I->see('Thank you for your message. It has been sent.');
}
);

// Confirm that the email address was added to ConvertKit.
$I->apiCheckSubscriberExists($I, $emailAddress);
}

/**
* Tests that the 'Enable Creator Network Recommendations' option on a Form's settings
* is not displayed when invalid credentials are specified at WPForms > Settings > Integrations > ConvertKit.
Expand Down
Loading

0 comments on commit 5ff3206

Please sign in to comment.