From 484782d6b5d50fe7ca0fcc0be3e0433ef7fd3c4d Mon Sep 17 00:00:00 2001 From: Wil Gerken Date: Wed, 15 Apr 2026 14:16:11 -0700 Subject: [PATCH 1/4] fix(recaptcha): skip script registration on TEC Community Events pages --- includes/class-recaptcha.php | 7 +++ tests/unit-tests/recaptcha.php | 106 +++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 tests/unit-tests/recaptcha.php diff --git a/includes/class-recaptcha.php b/includes/class-recaptcha.php index 2f1329d040..81a4b2ee1c 100644 --- a/includes/class-recaptcha.php +++ b/includes/class-recaptcha.php @@ -115,6 +115,13 @@ public static function get_script_url() { * Register the reCAPTCHA script. */ public static function register_scripts() { + // The Events Calendar Community Events loads its own reCAPTCHA api.js on its + // submission page. Two api.js loads with different site keys break reCAPTCHA, + // and Newspack's client does not protect TEC forms, so bail early rather + // than breaking TEC's own reCAPTCHA. + if ( function_exists( 'tribe_is_community_edit_event_page' ) && tribe_is_community_edit_event_page() ) { + return; + } if ( self::can_use_captcha() ) { \wp_enqueue_style( self::SCRIPT_HANDLE, diff --git a/tests/unit-tests/recaptcha.php b/tests/unit-tests/recaptcha.php new file mode 100644 index 0000000000..457da11393 --- /dev/null +++ b/tests/unit-tests/recaptcha.php @@ -0,0 +1,106 @@ + [ + 'site_key' => 'test_key', + 'site_secret' => 'test_secret', + ], + 'v2_invisible' => [ + 'site_key' => '', + 'site_secret' => '', + ], + ] + ); + } + + /** + * Normal front-end pages should enqueue Google's reCAPTCHA api.js. + */ + public function test_enqueues_api_script_on_normal_page() { + $this->enable_recaptcha(); + $GLOBALS['newspack_test_is_tec_community_page'] = false; + + Recaptcha::register_scripts(); + + $this->assertTrue( + wp_script_is( 'newspack-recaptcha-api', 'registered' ), + 'reCAPTCHA api.js should be registered on a normal page when reCAPTCHA is enabled.' + ); + } + + /** + * The TEC Community Events submission page should bail out and not enqueue api.js. + */ + public function test_skips_api_script_on_tec_community_page() { + $this->enable_recaptcha(); + $GLOBALS['newspack_test_is_tec_community_page'] = true; + + Recaptcha::register_scripts(); + + $this->assertFalse( + wp_script_is( 'newspack-recaptcha-api', 'registered' ), + 'reCAPTCHA api.js should NOT be registered on TEC Community Events submission pages.' + ); + } + + /** + * When reCAPTCHA is disabled, api.js should never be enqueued regardless of page. + */ + public function test_does_not_enqueue_when_recaptcha_disabled() { + delete_option( 'newspack_recaptcha_use_captcha' ); + $GLOBALS['newspack_test_is_tec_community_page'] = false; + + Recaptcha::register_scripts(); + + $this->assertFalse( + wp_script_is( 'newspack-recaptcha-api', 'registered' ), + 'reCAPTCHA api.js should not be registered when reCAPTCHA is disabled.' + ); + } +} From dc8e010cd5a5168b7888166bab213a9fc0a48c6f Mon Sep 17 00:00:00 2001 From: Wil Gerken Date: Wed, 15 Apr 2026 14:38:19 -0700 Subject: [PATCH 2/4] style(recaptcha): prefix globals and trim trailing whitespace --- includes/class-recaptcha.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-recaptcha.php b/includes/class-recaptcha.php index 81a4b2ee1c..91585cbe23 100644 --- a/includes/class-recaptcha.php +++ b/includes/class-recaptcha.php @@ -117,9 +117,9 @@ public static function get_script_url() { public static function register_scripts() { // The Events Calendar Community Events loads its own reCAPTCHA api.js on its // submission page. Two api.js loads with different site keys break reCAPTCHA, - // and Newspack's client does not protect TEC forms, so bail early rather + // and Newspack's client does not protect TEC forms, so bail early rather // than breaking TEC's own reCAPTCHA. - if ( function_exists( 'tribe_is_community_edit_event_page' ) && tribe_is_community_edit_event_page() ) { + if ( \function_exists( 'tribe_is_community_edit_event_page' ) && \tribe_is_community_edit_event_page() ) { return; } if ( self::can_use_captcha() ) { From 2fa69ad12809c9af8bb444a0e0a85a53a8d45470 Mon Sep 17 00:00:00 2001 From: Wil Gerken Date: Wed, 15 Apr 2026 14:57:22 -0700 Subject: [PATCH 3/4] test(recaptcha): extract TEC stub to mocks file for PHPCS --- tests/mocks/tec-community-events-mocks.php | 20 ++++++++++++++++++++ tests/unit-tests/recaptcha.php | 12 +----------- 2 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 tests/mocks/tec-community-events-mocks.php diff --git a/tests/mocks/tec-community-events-mocks.php b/tests/mocks/tec-community-events-mocks.php new file mode 100644 index 0000000000..401882c187 --- /dev/null +++ b/tests/mocks/tec-community-events-mocks.php @@ -0,0 +1,20 @@ + Date: Wed, 15 Apr 2026 16:40:39 -0700 Subject: [PATCH 4/4] test(recaptcha): address Copilot feedback on assertions and cleanup --- tests/unit-tests/recaptcha.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/unit-tests/recaptcha.php b/tests/unit-tests/recaptcha.php index 61a3bccc33..3f4bd22a46 100644 --- a/tests/unit-tests/recaptcha.php +++ b/tests/unit-tests/recaptcha.php @@ -2,7 +2,7 @@ /** * Tests for the Recaptcha class. * - * @package Newspack + * @package Newspack\Tests */ use Newspack\Recaptcha; @@ -25,6 +25,8 @@ public function set_up() { wp_dequeue_script( 'newspack-recaptcha-api' ); wp_deregister_script( 'newspack-recaptcha' ); wp_deregister_script( 'newspack-recaptcha-api' ); + wp_dequeue_style( 'newspack-recaptcha' ); + wp_deregister_style( 'newspack-recaptcha' ); $GLOBALS['newspack_test_is_tec_community_page'] = false; } @@ -59,8 +61,8 @@ public function test_enqueues_api_script_on_normal_page() { Recaptcha::register_scripts(); $this->assertTrue( - wp_script_is( 'newspack-recaptcha-api', 'registered' ), - 'reCAPTCHA api.js should be registered on a normal page when reCAPTCHA is enabled.' + wp_script_is( 'newspack-recaptcha-api', 'enqueued' ), + 'reCAPTCHA api.js should be enqueued on a normal page when reCAPTCHA is enabled.' ); } @@ -74,8 +76,8 @@ public function test_skips_api_script_on_tec_community_page() { Recaptcha::register_scripts(); $this->assertFalse( - wp_script_is( 'newspack-recaptcha-api', 'registered' ), - 'reCAPTCHA api.js should NOT be registered on TEC Community Events submission pages.' + wp_script_is( 'newspack-recaptcha-api', 'enqueued' ), + 'reCAPTCHA api.js should NOT be enqueued on TEC Community Events submission pages.' ); } @@ -89,8 +91,8 @@ public function test_does_not_enqueue_when_recaptcha_disabled() { Recaptcha::register_scripts(); $this->assertFalse( - wp_script_is( 'newspack-recaptcha-api', 'registered' ), - 'reCAPTCHA api.js should not be registered when reCAPTCHA is disabled.' + wp_script_is( 'newspack-recaptcha-api', 'enqueued' ), + 'reCAPTCHA api.js should not be enqueued when reCAPTCHA is disabled.' ); } }