Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions includes/class-convertkit-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,9 @@ public function output_global_non_inline_form() {
return;
}

// Determine if the Non-inline Form Limit per Session setting is enabled.
$limit_per_session = $this->settings->non_inline_form_limit_per_session();

// Get form.
$convertkit_forms = new ConvertKit_Resource_Forms();

Expand All @@ -828,13 +831,13 @@ public function output_global_non_inline_form() {
// Add the form to the scripts array so it is included in the output.
add_filter(
'convertkit_output_scripts_footer',
function ( $scripts ) use ( $form ) {
function ( $scripts ) use ( $form, $limit_per_session ) {

$scripts[] = array(
'async' => true,
'data-uid' => $form['uid'],
'src' => $form['embed_js'],
'data-kit-limit-per-session' => true,
'data-kit-limit-per-session' => $limit_per_session ? '1' : '0',
);

return $scripts;
Expand Down
2 changes: 1 addition & 1 deletion includes/class-convertkit-resource-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ function ( $scripts ) use ( $id, $post_id, $settings ) {
'async' => true,
'data-uid' => $this->resources[ $id ]['uid'],
'src' => $this->resources[ $id ]['embed_js'],
'data-kit-limit-per-session' => $settings->non_inline_form_limit_per_session(),
'data-kit-limit-per-session' => $settings->non_inline_form_limit_per_session() ? '1' : '0',
);

// If debugging is enabled, add the post ID to the script.
Expand Down
4 changes: 2 additions & 2 deletions resources/frontend/js/convertkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ document.addEventListener('DOMContentLoaded', function () {

// Set a cookie if any scripts with data-kit-limit-per-session attribute exist.
if (
document.querySelectorAll('script[data-kit-limit-per-session]').length >
0
document.querySelectorAll('script[data-kit-limit-per-session="1"]')
.length > 0
) {
document.cookie = 'ck_non_inline_form_displayed=1; path=/';
if (convertkit.debug) {
Expand Down
51 changes: 51 additions & 0 deletions tests/EndToEnd/forms/general/NonInlineFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,57 @@ public function testNonInlineFormLimitPerSession(EndToEndTester $I)
$I->seeCookie('ck_non_inline_form_displayed');
}

/**
* Test that the non-inline form limit per session setting does not set a cookie
* when disabled.
*
* @since 3.0.9
*
* @param EndToEndTester $I Tester.
*/
public function testNonInlineFormLimitPerSessionDoesNotSetCookieWhenDisabled(EndToEndTester $I)
{
// Setup Plugin with a non-inline Default Form for Site Wide.
$I->setupKitPlugin(
$I,
[
'non_inline_form' => array(
$_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'],
),
]
);

// Create a Page in the database.
$I->havePostInDatabase(
[
'post_title' => 'Kit: Non Inline Form: Limit Per Session Disabled',
'post_name' => 'kit-non-inline-form-limit-per-session-disabled',
'post_type' => 'page',
'post_status' => 'publish',
]
);

// View the home page.
$I->amOnPage('/');

// Confirm that one Kit Form is output in the DOM.
// This confirms that there is only one script on the page for this form, which renders the form.
$I->seeNumberOfElementsInDOM('form[data-sv-form="' . $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'] . '"]', 1);

// Confirm no cookie is set.
$I->dontSeeCookie('ck_non_inline_form_displayed');

// View Page.
$I->amOnPage('/kit-non-inline-form-limit-per-session-disabled');

// Confirm that one Kit Form is output in the DOM.
// This confirms that there is only one script on the page for this form, which renders the form.
$I->seeNumberOfElementsInDOM('form[data-sv-form="' . $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'] . '"]', 1);

// Confirm no cookie is set.
$I->dontSeeCookie('ck_non_inline_form_displayed');
}

/**
* Test that the defined default non-inline form displays site wide
* when stored as a string in the Plugin settings from older
Expand Down