From 8d0e414093331640944d22e66e9432eeec342f40 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Thu, 2 Jan 2025 11:16:06 +0800 Subject: [PATCH 1/2] Add referrer support when subscribing to a Kit Form in Contact Form 7 --- composer.json | 2 +- .../class-convertkit-contactform7.php | 25 +++++++++++- .../Helper/Acceptance/ConvertKitAPI.php | 40 +++++++++++++++++++ .../other/ContactForm7FormCest.php | 10 ++++- 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 18926888..ea6c254a 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "project", "license": "GPLv3", "require": { - "convertkit/convertkit-wordpress-libraries": "2.0.5" + "convertkit/convertkit-wordpress-libraries": "dev-add-form-referrer-support" }, "require-dev": { "lucatume/wp-browser": "<3.5", diff --git a/includes/integrations/contactform7/class-convertkit-contactform7.php b/includes/integrations/contactform7/class-convertkit-contactform7.php index bcd7a645..901f4d59 100644 --- a/includes/integrations/contactform7/class-convertkit-contactform7.php +++ b/includes/integrations/contactform7/class-convertkit-contactform7.php @@ -163,7 +163,7 @@ public function handle_wpcf7_submit( $contact_form, $result ) { } // Add subscriber to form. - return $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'] ); + return $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'], $this->get_referrer_url() ); /** * Sequence @@ -199,6 +199,29 @@ public function handle_wpcf7_submit( $contact_form, $result ) { } + /** + * Gets the referrer URL to send to the `form_subscribe` API method. + * + * Falls back to the action's AJAX URL if the Post ID the form was + * embedded in cannot be determined. + * + * @since 2.7.1 + * + * @return string + */ + private function get_referrer_url() { + + // If the request includes the Post ID the form was embedded in, + // return that URL. + if ( array_key_exists( '_wpcf7_container_post', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended + return get_permalink( absint( $_REQUEST['_wpcf7_container_post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended + } + + // Return the AJAX URL. + return home_url( add_query_arg( null, null ) ); + + } + } // Bootstrap. diff --git a/tests/_support/Helper/Acceptance/ConvertKitAPI.php b/tests/_support/Helper/Acceptance/ConvertKitAPI.php index 7ca242bd..7865f994 100644 --- a/tests/_support/Helper/Acceptance/ConvertKitAPI.php +++ b/tests/_support/Helper/Acceptance/ConvertKitAPI.php @@ -68,6 +68,46 @@ public function apiCheckSubscriberExists($I, $emailAddress) return $results['subscribers'][0]['id']; } + /** + * Check the given subscriber ID has been assigned to the given form ID. + * + * @since 2.7.1 + * + * @param AcceptanceTester $I AcceptanceTester. + * @param int $subscriberID Subscriber ID. + * @param int $formID Form ID. + * @param string $referrer Referrer. + */ + public function apiCheckSubscriberHasForm($I, $subscriberID, $formID, $referrer = false) + { + // Run request. + $results = $this->apiRequest( + 'forms/' . $formID . '/subscribers', + 'GET', + [ + // Check all subscriber states. + 'status' => 'all', + ] + ); + + // Iterate through subscribers. + $subscriberHasForm = false; + foreach ($results['subscribers'] as $subscriber) { + if ($subscriber['id'] === $subscriberID) { + $subscriberHasForm = true; + break; + } + } + + // Assert if the subscriber has the form. + $this->assertTrue($subscriberHasForm); + + // If a referrer is specified, assert it matches the subscriber's referrer now. + if ($referrer) { + $I->assertEquals($subscriber['referrer'], $referrer); + } + } + /** * Check the given subscriber ID has been assigned to the given sequence ID. * diff --git a/tests/acceptance/integrations/other/ContactForm7FormCest.php b/tests/acceptance/integrations/other/ContactForm7FormCest.php index a105a01b..2c43d370 100644 --- a/tests/acceptance/integrations/other/ContactForm7FormCest.php +++ b/tests/acceptance/integrations/other/ContactForm7FormCest.php @@ -62,7 +62,15 @@ public function testSettingsContactForm7ToConvertKitFormMapping(AcceptanceTester ); // Confirm that the email address was added to ConvertKit. - $I->apiCheckSubscriberExists($I, $emailAddress); + $subscriberID = $I->apiCheckSubscriberExists($I, $emailAddress); + + // Check that the subscriber has the expected form and referrer value set. + $I->apiCheckSubscriberHasForm( + $I, + $subscriberID, + $_ENV['CONVERTKIT_API_THIRD_PARTY_INTEGRATIONS_FORM_ID'], + $_ENV['TEST_SITE_WP_URL'] . $I->grabFromCurrentUrl() + ); } /** From 64424d064b1f4612659c806d214297c9966cf582 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Thu, 2 Jan 2025 11:39:01 +0800 Subject: [PATCH 2/2] Add referrer support when subscribing to a Kit Form in Forminator --- .../class-convertkit-forminator.php | 32 ++++++++++++++++++- .../integrations/other/ForminatorCest.php | 10 +++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/includes/integrations/forminator/class-convertkit-forminator.php b/includes/integrations/forminator/class-convertkit-forminator.php index c5bcf5a9..696db762 100644 --- a/includes/integrations/forminator/class-convertkit-forminator.php +++ b/includes/integrations/forminator/class-convertkit-forminator.php @@ -161,7 +161,7 @@ public function maybe_subscribe( $entry, $form_id, $form_data_array ) { } // Add subscriber to form. - return $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'] ); + return $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'], $this->get_referrer_url() ); /** * Sequence @@ -197,6 +197,36 @@ public function maybe_subscribe( $entry, $form_id, $form_data_array ) { } + /** + * Gets the referrer URL to send to the `form_subscribe` API method. + * + * Falls back to the action's AJAX URL if the Post ID the form was + * embedded in cannot be determined. + * + * @since 2.7.1 + * + * @return string + */ + private function get_referrer_url() { + + // If the request includes the HTTP referrer, return that URL + // as it will include any UTM parameters. + if ( array_key_exists( '_wp_http_referer', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended + // referrer is a relative path, so use home_url() to return a fully qualified URL. + return esc_url( home_url( $_REQUEST['_wp_http_referer'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended + } + + // If the request includes the current_url, return that URL. + // It won't include any UTM parameters, but is still an accurate URL. + if ( array_key_exists( 'current_url', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended + return esc_url( $_REQUEST['current_url'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended + } + + // Return the AJAX URL. + return home_url( add_query_arg( null, null ) ); + + } + } // Bootstrap. diff --git a/tests/acceptance/integrations/other/ForminatorCest.php b/tests/acceptance/integrations/other/ForminatorCest.php index 85f24350..a146f116 100644 --- a/tests/acceptance/integrations/other/ForminatorCest.php +++ b/tests/acceptance/integrations/other/ForminatorCest.php @@ -62,7 +62,15 @@ public function testSettingsForminatorFormToConvertKitFormMapping(AcceptanceTest ); // Confirm that the email address was added to ConvertKit. - $I->apiCheckSubscriberExists($I, $emailAddress); + $subscriberID = $I->apiCheckSubscriberExists($I, $emailAddress); + + // Check that the subscriber has the expected form and referrer value set. + $I->apiCheckSubscriberHasForm( + $I, + $subscriberID, + $_ENV['CONVERTKIT_API_THIRD_PARTY_INTEGRATIONS_FORM_ID'], + $_ENV['TEST_SITE_WP_URL'] . $I->grabFromCurrentUrl() + ); } /**