From 2eda5cb688f0e45eb328956862433a7c50a44ba0 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 7 Nov 2025 09:20:06 +0800 Subject: [PATCH 1/3] Fix: Limit Non-Inline Forms per Session --- includes/class-convertkit-output.php | 7 +++++-- includes/class-convertkit-resource-forms.php | 2 +- resources/frontend/js/convertkit.js | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/includes/class-convertkit-output.php b/includes/class-convertkit-output.php index 65063ffb2..8167df8ef 100644 --- a/includes/class-convertkit-output.php +++ b/includes/class-convertkit-output.php @@ -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(); @@ -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; diff --git a/includes/class-convertkit-resource-forms.php b/includes/class-convertkit-resource-forms.php index 3bb1146d0..c8806c395 100644 --- a/includes/class-convertkit-resource-forms.php +++ b/includes/class-convertkit-resource-forms.php @@ -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. diff --git a/resources/frontend/js/convertkit.js b/resources/frontend/js/convertkit.js index ba9de2c46..2349a1eaf 100644 --- a/resources/frontend/js/convertkit.js +++ b/resources/frontend/js/convertkit.js @@ -212,7 +212,7 @@ 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 > + document.querySelectorAll('script[data-kit-limit-per-session="1"]').length > 0 ) { document.cookie = 'ck_non_inline_form_displayed=1; path=/'; From cf798313b7e782845ac55a62ac4d4e46c98e7c82 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 7 Nov 2025 10:17:29 +0800 Subject: [PATCH 2/3] Added test --- .../forms/general/NonInlineFormCest.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/EndToEnd/forms/general/NonInlineFormCest.php b/tests/EndToEnd/forms/general/NonInlineFormCest.php index 4f0e86f4b..5185b6cf5 100644 --- a/tests/EndToEnd/forms/general/NonInlineFormCest.php +++ b/tests/EndToEnd/forms/general/NonInlineFormCest.php @@ -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 From c65cd41961c1a5e5db645ae8fc0b55919639cfa5 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 7 Nov 2025 11:13:45 +0800 Subject: [PATCH 3/3] Lint JS --- resources/frontend/js/convertkit.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/frontend/js/convertkit.js b/resources/frontend/js/convertkit.js index 2349a1eaf..e2a4f5a9f 100644 --- a/resources/frontend/js/convertkit.js +++ b/resources/frontend/js/convertkit.js @@ -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="1"]').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) {