Skip to content

Commit

Permalink
Merge pull request #69 from ConvertKit/use-account-id-as-provider-key
Browse files Browse the repository at this point in the history
Use Kit Account ID as WPForms Provider Key
  • Loading branch information
n7studios authored Nov 12, 2024
2 parents bec865c + 3795579 commit b2e0dd7
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 46 additions & 2 deletions includes/class-integrate-convertkit-wpforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<a href="%s" class="%s-reconnect">%s</a>',
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( '</a></span></li>', '</a></span>' . $reconnect_button . '</li>', $html );

// Output UI.
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

}

/**
* Runs update/upgrade routines between Plugin versions.
*
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/_support/Helper/Acceptance/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand Down
30 changes: 23 additions & 7 deletions tests/_support/Helper/Acceptance/WPForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
[
Expand All @@ -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',
[
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
57 changes: 55 additions & 2 deletions tests/acceptance/forms/FormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 <select> option name.
* @param bool|array $tags Values to use for tags.
* @param bool|array $customFields Custom field key / value pairs.
* @return int Form ID
*/
private function _wpFormsSetupFormOnly(AcceptanceTester $I, $optionName, $tags = false, $customFields = false)
{
// Define connection with valid API credentials.
$this->accountID = $I->setupWPFormsIntegration($I);
Expand All @@ -539,8 +593,7 @@ private function _wpFormsSetupForm(AcceptanceTester $I, $optionName, $tags = fal
// Check that the resources are cached with the correct key.
$I->seeCachedResourcesInDatabase($I, $this->accountID);

// Create a Page with the WPForms shortcode as its content.
return $I->createPageWithWPFormsShortcode($I, $wpFormsID);
return $wpFormsID;
}

/**
Expand Down
19 changes: 17 additions & 2 deletions tests/acceptance/general/IntegrationsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function _before(AcceptanceTester $I)
}

/**
* Test that adding a ConvertKit account to the ConvertKit integration sections
* works with valid API credentials.
* Test that adding a Kit account to the Kit integration sections
* works when connecting, reconnecting and disconnecting.
*
* @since 1.5.0
*
Expand Down Expand Up @@ -72,6 +72,21 @@ public function testAddIntegrationWithValidCredentials(AcceptanceTester $I)
// This sanity checks that we didn't accidentally save the API Key to the API Secret field as we did in 1.5.7 and lower.
$I->assertTrue($I->checkWPFormsIntegrationExists($I, $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN']));

// Confirm that the connection can be reconnected.
$I->seeElementInDOM('a.convertkit-reconnect');
$reconnectURL = $I->grabAttributeFrom('a.convertkit-reconnect', 'href');
$I->assertStringContainsString(
'https://app.kit.com/oauth/authorize?client_id=' . $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'] . '&response_type=code&redirect_uri=' . urlencode( $_ENV['KIT_OAUTH_REDIRECT_URI'] ),
$reconnectURL
);
$I->assertStringContainsString(
'&state=' . $I->apiEncodeState(
$_ENV['TEST_SITE_WP_URL'] . '/wp-admin/admin.php?page=wpforms-settings&view=integrations',
$_ENV['CONVERTKIT_OAUTH_CLIENT_ID']
),
$reconnectURL
);

// Confirm that the connection can be disconnected.
$I->click('Disconnect');

Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/recommendations/RecommendationsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testCreatorNetworkRecommendationsOptionWhenInvalidCredentials(Ac
public function testCreatorNetworkRecommendationsOptionWhenDisabledOnConvertKitAccount(AcceptanceTester $I)
{
// Setup Plugin with API Key and Secret for ConvertKit Account that does not have the Creator Network enabled.
$accountID = $I->setupWPFormsIntegration($I, $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA']);
$accountID = $I->setupWPFormsIntegration($I, $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], $_ENV['CONVERTKIT_API_ACCOUNT_ID_NO_DATA']);

// Create Form.
$wpFormsID = $I->createWPFormsForm($I);
Expand Down

0 comments on commit b2e0dd7

Please sign in to comment.