diff --git a/.env.dist.testing b/.env.dist.testing
index d9287dc..478f863 100644
--- a/.env.dist.testing
+++ b/.env.dist.testing
@@ -17,6 +17,8 @@ TEST_TABLE_PREFIX=wp_
TEST_SITE_WP_URL=http://127.0.0.1
TEST_SITE_WP_DOMAIN=127.0.0.1
TEST_SITE_ADMIN_EMAIL=wordpress@convertkit.local
+CONVERTKIT_API_ACCOUNT_ID=841415
+CONVERTKIT_API_ACCOUNT_ID_NO_DATA=753065
CONVERTKIT_API_FORM_NAME="WPForms Form"
CONVERTKIT_API_FORM_ID="3753745"
CONVERTKIT_API_LEGACY_FORM_NAME="Legacy Form"
diff --git a/.env.example b/.env.example
index 19a4f6a..432cfda 100644
--- a/.env.example
+++ b/.env.example
@@ -21,6 +21,8 @@ CONVERTKIT_API_KEY_NO_DATA=
CONVERTKIT_API_SECRET_NO_DATA=
CONVERTKIT_API_KEY=
CONVERTKIT_API_SECRET=
+CONVERTKIT_API_ACCOUNT_ID=841415
+CONVERTKIT_API_ACCOUNT_ID_NO_DATA=753065
CONVERTKIT_API_FORM_NAME="WPForms Form"
CONVERTKIT_API_FORM_ID="3753745"
CONVERTKIT_API_LEGACY_FORM_NAME="Legacy Form"
diff --git a/includes/class-integrate-convertkit-wpforms.php b/includes/class-integrate-convertkit-wpforms.php
index efe30e4..ecb869f 100644
--- a/includes/class-integrate-convertkit-wpforms.php
+++ b/includes/class-integrate-convertkit-wpforms.php
@@ -71,6 +71,51 @@ public function init() {
}
+ /**
+ * Add provider to the Settings Integrations tab.
+ *
+ * @since 1.0.0
+ *
+ * @param array $active Array of active connections.
+ * @param array $settings Array of all connections settings.
+ */
+ public function integrations_tab_options( $active, $settings ) {
+
+ // If no Kit accounts connected to WPForms, don't modify the UI.
+ if ( ! array_key_exists( $this->slug, $settings ) ) {
+ parent::integrations_tab_options( $active, $settings );
+ return;
+ }
+ if ( ! count( $settings[ $this->slug ] ) ) {
+ parent::integrations_tab_options( $active, $settings );
+ return;
+ }
+
+ // Initialize API to generate OAuth URL.
+ $api = new Integrate_ConvertKit_WPForms_API(
+ INTEGRATE_CONVERTKIT_WPFORMS_OAUTH_CLIENT_ID,
+ INTEGRATE_CONVERTKIT_WPFORMS_OAUTH_REDIRECT_URI
+ );
+
+ // Fetch UI.
+ ob_start();
+ parent::integrations_tab_options( $active, $settings );
+ $html = ob_get_clean();
+
+ // Inject Reconnect button for each Kit account.
+ $reconnect_button = sprintf(
+ '%s',
+ esc_url( $api->get_oauth_url( admin_url( 'admin.php?page=wpforms-settings&view=integrations' ) ) ),
+ esc_attr( $this->slug ),
+ esc_html__( 'Reconnect', 'integrate-convertkit-wpforms' )
+ );
+ $html = str_replace( '', '' . $reconnect_button . '', $html );
+
+ // Output UI.
+ echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
+
+ }
+
/**
* Runs update/upgrade routines between Plugin versions.
*
@@ -717,7 +762,6 @@ public function maybe_get_and_store_access_token() {
}
// Update the provider's settings and return its unique ID.
- $id = uniqid();
wpforms_update_providers_options(
$this->slug,
array(
@@ -727,7 +771,7 @@ public function maybe_get_and_store_access_token() {
'label' => $account['account']['name'],
'date' => time(),
),
- $id
+ 'kit-' . $account['account']['id']
);
// Reload the integrations screen, which will now show the connection.
diff --git a/tests/_support/Helper/Acceptance/Plugin.php b/tests/_support/Helper/Acceptance/Plugin.php
index 8b4aa31..8176103 100644
--- a/tests/_support/Helper/Acceptance/Plugin.php
+++ b/tests/_support/Helper/Acceptance/Plugin.php
@@ -19,7 +19,7 @@ class Plugin extends \Codeception\Module
*/
public function activateConvertKitPlugin($I)
{
- $I->activateThirdPartyPlugin($I, 'kit-formerly-convertkit-for-wpforms');
+ $I->activateThirdPartyPlugin($I, 'integrate-convertkit-wpforms');
}
/**
@@ -32,7 +32,7 @@ public function activateConvertKitPlugin($I)
*/
public function deactivateConvertKitPlugin($I)
{
- $I->deactivateThirdPartyPlugin($I, 'kit-formerly-convertkit-for-wpforms');
+ $I->deactivateThirdPartyPlugin($I, 'integrate-convertkit-wpforms');
}
/**
diff --git a/tests/_support/Helper/Acceptance/WPForms.php b/tests/_support/Helper/Acceptance/WPForms.php
index 4a50c86..67a0b5d 100644
--- a/tests/_support/Helper/Acceptance/WPForms.php
+++ b/tests/_support/Helper/Acceptance/WPForms.php
@@ -17,11 +17,13 @@ class WPForms extends \Codeception\Module
* @param AcceptanceTester $I AcceptanceTester.
* @param bool|string $accessToken Access Token (if not specified, CONVERTKIT_OAUTH_ACCESS_TOKEN is used).
* @param bool|string $refreshToken Refresh Token (if not specified, CONVERTKIT_OAUTH_REFRESH_TOKEN is used).
+ * @param string $accountID Kit Account ID.
+ * @return string Account ID in WPForms.
*/
- public function setupWPFormsIntegration($I, $accessToken = false, $refreshToken = false)
+ public function setupWPFormsIntegration($I, $accessToken = false, $refreshToken = false, $accountID = false)
{
- // Define a random account ID key for this test.
- $accountID = rand(63000, 64000) . 'bdcceea3'; // phpcs:ignore WordPress.WP.AlternativeFunctions
+ $accountID = 'kit-' . ( $accountID ? $accountID : $_ENV['CONVERTKIT_API_ACCOUNT_ID'] );
+
$I->haveOptionInDatabase(
'wpforms_providers',
[
@@ -47,11 +49,13 @@ public function setupWPFormsIntegration($I, $accessToken = false, $refreshToken
* @param AcceptanceTester $I AcceptanceTester.
* @param bool|string $apiKey API Key (if not specified, CONVERTKIT_API_KEY is used).
* @param bool|string $apiSecret API Secret (if not specified, CONVERTKIT_API_SECRET is used).
+ * @param string $accountID Kit Account ID.
+ * @return string Account ID in WPForms.
*/
- public function setupWPFormsIntegrationWithAPIKeyAndSecret($I, $apiKey = false, $apiSecret = false)
+ public function setupWPFormsIntegrationWithAPIKeyAndSecret($I, $apiKey = false, $apiSecret = false, $accountID = false)
{
- // Define a random account ID key for this test.
- $accountID = rand(63000, 64000) . 'bdcceea3'; // phpcs:ignore WordPress.WP.AlternativeFunctions
+ $accountID = 'kit-' . ( $accountID ? $accountID : $_ENV['CONVERTKIT_API_ACCOUNT_ID'] );
+
$I->haveOptionInDatabase(
'wpforms_providers',
[
@@ -91,6 +95,18 @@ public function checkWPFormsIntegrationExists($I, $accessToken, $refreshToken)
return false;
}
+ /**
+ * Helper method to delete the ConvertKit integration in WPForms.
+ *
+ * @since 1.7.8
+ *
+ * @param AcceptanceTester $I AcceptanceTester.
+ */
+ public function deleteWPFormsIntegration($I)
+ {
+ $I->dontHaveOptionInDatabase('wpforms_providers');
+ }
+
/**
* Creates a WPForms Form with ConvertKit Settings, as if it were created
* in 1.4.1 or older.
@@ -368,7 +384,7 @@ public function configureConvertKitSettingsOnForm($I, $wpFormID, $formName, $nam
$connectionID = $I->grabAttributeFrom('.wpforms-provider-connections .wpforms-provider-connection', 'data-connection_id');
// Specify field values.
- $I->waitForElementVisible('div[data-connection_id="' . $connectionID . '"] .wpforms-provider-fields');
+ $I->waitForElementVisible('div[data-connection_id="' . $connectionID . '"] .wpforms-provider-fields', 30);
if ($formName) {
// Select Form.
diff --git a/tests/acceptance/forms/FormCest.php b/tests/acceptance/forms/FormCest.php
index 98f7bd9..0bc18a9 100644
--- a/tests/acceptance/forms/FormCest.php
+++ b/tests/acceptance/forms/FormCest.php
@@ -61,6 +61,39 @@ public function testCreateFormToConvertKitFormMapping(AcceptanceTester $I)
$I->apiCheckSubscriberExists($I, $emailAddress, 'First');
}
+ /**
+ * Tests that the connection configured when editing a WPForms Form at Marketing > Kit is retained when:
+ * - Connects to Kit at Settings > Integrations,
+ * - Configures the connection at WPForms Form > Marketing > Kit,
+ * - Disconnects from Kit at Settings > Integrations,
+ * - Connects (again) to the same Kit at Settings > Integrations
+ * - Observe the connection at WPForms Form > Marketing > Kit is retained.
+ *
+ * @since 1.7.8
+ *
+ * @param AcceptanceTester $I Tester.
+ */
+ public function testConnectionRetainedWhenAccountReconnected(AcceptanceTester $I)
+ {
+ // Setup WPForms Form and configuration for this test.
+ $formID = $this->_wpFormsSetupFormOnly(
+ $I,
+ $_ENV['CONVERTKIT_API_FORM_NAME']
+ );
+
+ // Disconnect from Kit at Settings > Integrations.
+ $I->deleteWPFormsIntegration($I);
+
+ // Connect to Kit at Settings > Integrations.
+ $I->setupWPFormsIntegration($I);
+
+ // Edit the WPForms Form, confirming the connection still exists.
+ $I->amOnAdminPage('admin.php?page=wpforms-builder&view=providers&form_id=' . $formID);
+ $I->waitForElementVisible('div[data-provider="convertkit"]');
+ $I->see('Select Account');
+ $I->see('Kit Form');
+ }
+
/**
* Test that the Plugin works when:
* - Creating a WPForms Form,
@@ -515,6 +548,27 @@ public function testCreateFormWithCustomField(AcceptanceTester $I)
* @return int Page ID
*/
private function _wpFormsSetupForm(AcceptanceTester $I, $optionName, $tags = false, $customFields = false)
+ {
+ // Create Form.
+ $wpFormsID = $this->_wpFormsSetupFormOnly($I, $optionName, $tags, $customFields);
+
+ // Create a Page with the WPForms shortcode as its content.
+ return $I->createPageWithWPFormsShortcode($I, $wpFormsID);
+ }
+
+ /**
+ * Maps the given resource name to the created WPForms Form,
+ * embeds the shortcode on a new Page, returning the Form ID.
+ *
+ * @since 1.7.8
+ *
+ * @param AcceptanceTester $I Tester.
+ * @param string $optionName