Skip to content

Commit

Permalink
Merge pull request #758 from Kit/add-form-referrer-support
Browse files Browse the repository at this point in the history
Form Subscribe: Add `referrer` support
  • Loading branch information
n7studios authored Jan 3, 2025
2 parents cffc515 + 0fa02f3 commit e436e9f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 31 additions & 1 deletion includes/integrations/forminator/class-convertkit-forminator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions tests/_support/Helper/Acceptance/ConvertKitAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 9 additions & 1 deletion tests/acceptance/integrations/other/ContactForm7FormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion tests/acceptance/integrations/other/ForminatorCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

/**
Expand Down

0 comments on commit e436e9f

Please sign in to comment.