@@ -550,20 +550,31 @@ public function get_plugin_settings_instance() {
550550 * @return bool
551551 */
552552 private function initialize_api () {
553+ static $ result ;
554+
555+ if ( is_bool ( $ result ) ) {
556+ return $ result ;
557+ }
558+
559+ $ result = false ;
553560 $ site_key = $ this ->plugin_settings ->get_recaptcha_key ( 'site_key_v3 ' );
554561 $ secret_key = $ this ->plugin_settings ->get_recaptcha_key ( 'secret_key_v3 ' );
555562
556563 if ( ! ( $ site_key && $ secret_key ) ) {
557- $ this ->log_debug ( __METHOD__ . '(): missing v3 key configuration. Please check the add-on settings. ' );
564+ $ this ->log_debug ( __METHOD__ . '(): Missing v3 key configuration. Please check the add-on settings. ' );
565+
558566 return false ;
559567 }
560568
561569 if ( '1 ' !== $ this ->get_plugin_setting ( 'recaptcha_keys_status_v3 ' ) ) {
562- $ this ->log_debug ( __METHOD__ . '(): could not initialize reCAPTCHA v3 because site and/or secret key is invalid. ' );
570+ $ this ->log_debug ( __METHOD__ . '(): Could not initialize reCAPTCHA v3 because site and/or secret key is invalid. ' );
571+
563572 return false ;
564573 }
565574
566- $ this ->log_debug ( __METHOD__ . '(): Initializing API. ' );
575+ $ result = true ;
576+ $ this ->log_debug ( __METHOD__ . '(): API Initialized. ' );
577+
567578 return true ;
568579 }
569580
@@ -763,25 +774,39 @@ public function add_recaptcha_meta_box( $data ) {
763774 */
764775 public function check_for_spam_entry ( $ is_spam , $ form , $ entry ) {
765776
766- if ( $ is_spam || $ this ->is_disabled_by_form_setting ( $ form ) || ! $ this ->initialize_api () || $ this ->is_preview () ) {
777+ if ( $ is_spam ) {
778+ $ this ->log_debug ( __METHOD__ . '(): Skipping, entry has already been identified as spam by another anti-spam solution. ' );
767779 return $ is_spam ;
768780 }
769781
770- $ is_spam = ( float ) $ this ->get_score_from_entry ( $ entry ) <= $ this -> get_spam_score_threshold ( );
782+ $ is_spam = $ this ->is_spam_submission ( $ form , $ entry );
771783 $ this ->log_debug ( __METHOD__ . '(): Is submission considered spam? ' . ( $ is_spam ? 'Yes. ' : 'No. ' ) );
772784
773785 return $ is_spam ;
774786 }
775787
776- public function is_spam_submission ( $ form ) {
777- if ( $ this ->is_disabled_by_form_setting ( $ form ) || $ this ->is_preview () || ! $ this ->initialize_api () ) {
788+ /**
789+ * Determines if the submission is spam by comparing its score with the threshold.
790+ *
791+ * @since 1.4
792+ * @since 1.5 Added the optional $entry param.
793+ *
794+ * @param array $form The form being processed.
795+ * @param array $entry The entry being processed.
796+ *
797+ * @return bool
798+ */
799+ public function is_spam_submission ( $ form , $ entry = array () ) {
800+ if ( $ this ->should_skip_validation ( $ form ) ) {
801+ $ this ->log_debug ( __METHOD__ . '(): Score check skipped. ' );
802+
778803 return false ;
779804 }
780805
781- $ score = $ this ->token_verifier ->get_score ();
782- $ threashold = $ this ->get_spam_score_threshold ();
806+ $ score = empty ( $ entry ) ? $ this ->token_verifier ->get_score () : $ this -> get_score_from_entry ( $ entry );
807+ $ threshold = $ this ->get_spam_score_threshold ();
783808
784- return (float ) $ score <= (float ) $ threashold ;
809+ return (float ) $ score <= (float ) $ threshold ;
785810 }
786811 /**
787812 * Get the Recaptcha score from the entry details.
@@ -812,6 +837,12 @@ private function get_score_from_entry( $entry ) {
812837 * @return float
813838 */
814839 private function get_spam_score_threshold () {
840+ static $ value ;
841+
842+ if ( ! empty ( $ value ) ) {
843+ return $ value ;
844+ }
845+
815846 $ value = (float ) $ this ->get_plugin_setting ( 'score_threshold_v3 ' );
816847 if ( empty ( $ value ) ) {
817848 $ value = 0.5 ;
@@ -846,8 +877,8 @@ private function is_disabled_by_form_setting( $form ) {
846877 public function validate_submission ( $ submission_data ) {
847878 $ this ->log_debug ( __METHOD__ . '(): Validating form (# ' . rgars ( $ submission_data , 'form/id ' ) . ') submission. ' );
848879
849- if ( $ this ->should_skip_validation ( $ submission_data ) ) {
850- $ this ->log_debug ( __METHOD__ . '(): Validation skipped. reCAPTCHA v3 is misconfigured, disabled, or the form was submitted in preview mode. ' );
880+ if ( $ this ->should_skip_validation ( rgar ( $ submission_data, ' form ' ) ) ) {
881+ $ this ->log_debug ( __METHOD__ . '(): Validation skipped. ' );
851882
852883 return $ submission_data ;
853884 }
@@ -864,24 +895,55 @@ public function validate_submission( $submission_data ) {
864895 * we want to skip the reCaptcha validation so it isn't triggered twice, as this will make it always fail.
865896 *
866897 * @since 1.4
898+ * @since 1.5 Changed param to $form array.
867899 *
868- * @param array $submission_data The submitted form data .
900+ * @param array $form The form being processed .
869901 *
870902 * @return bool
871903 */
872- public function should_skip_validation ( $ submission_data ) {
904+ public function should_skip_validation ( $ form ) {
905+ static $ result = array ();
906+
907+ $ form_id = rgar ( $ form , 'id ' );
908+ if ( isset ( $ result [ $ form_id ] ) ) {
909+ return $ result [ $ form_id ];
910+ }
911+
912+ $ result [ $ form_id ] = true ;
913+
914+ if ( $ this ->is_preview () ) {
915+ $ this ->log_debug ( __METHOD__ . '(): Yes! Form preview page. ' );
916+
917+ return true ;
918+ }
919+
920+ if ( ! $ this ->initialize_api () ) {
921+ $ this ->log_debug ( __METHOD__ . '(): Yes! API not initialized. ' );
922+
923+ return true ;
924+ }
925+
926+ if ( $ this ->is_disabled_by_form_setting ( $ form ) ) {
927+ $ this ->log_debug ( __METHOD__ . '(): Yes! Disabled by form setting. ' );
928+
929+ return true ;
930+ }
931+
932+ if ( defined ( 'REST_REQUEST ' ) && REST_REQUEST && ! isset ( $ _POST [ $ this ->field ->get_input_name ( $ form_id ) ] ) ) {
933+ $ this ->log_debug ( __METHOD__ . '(): Yes! REST request without input. ' );
873934
874- $ is_recaptcha_configured = $ this ->initialize_api () && ! $ this ->is_disabled_by_form_setting ( rgar ( $ submission_data , 'form ' ) );
875- // Skip validation if reCaptcha is not configured or if the form is in preview mode.
876- if ( ! $ is_recaptcha_configured || $ this ->is_preview () ) {
877935 return true ;
878936 }
879937
880938 // For older versions of Stripe, skip the first validation attempt and only validate on the second attempt. Newer versions of Stripe will validate twice without a problem.
881939 if ( $ this ->is_stripe_validation () && version_compare ( gf_stripe ()->get_version (), '5.4.3 ' , '< ' ) ) {
940+ $ this ->log_debug ( __METHOD__ . '(): Yes! Older Stripe validation. ' );
941+
882942 return true ;
883943 }
884944
945+ $ result [ $ form_id ] = false ;
946+
885947 return false ;
886948 }
887949
0 commit comments