diff --git a/includes/class-recaptcha.php b/includes/class-recaptcha.php index 2f1329d040..91585cbe23 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/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 @@ + [ + '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', 'enqueued' ), + 'reCAPTCHA api.js should be enqueued 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', 'enqueued' ), + 'reCAPTCHA api.js should NOT be enqueued 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', 'enqueued' ), + 'reCAPTCHA api.js should not be enqueued when reCAPTCHA is disabled.' + ); + } +}