| @@ -12,8 +12,20 @@ | ||
| 12 | 12 | */ |
| 13 | 13 | class FrmAntiSpam extends FrmValidate { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | + * Track when the token filters have been added so they are only added once. | |
| 17 | + * The callback checks the Anti-Spam setting of the form being rendered, so | |
| 18 | + * a single callback covers every form on the page. Adding one callback for | |
| 19 | + * each form would print duplicate data-token attributes. | |
| 20 | + * | |
| 21 | + * @since 6.34 | |
| 22 | + * | |
| 23 | + * @var bool | |
| 24 | + */ | |
| 25 | + private static $filters_added = false; | |
| 26 | + | |
| 27 | + /** | |
| 16 | 28 | * @return string |
| 17 | 29 | */ |
| 18 | 30 | protected function get_option_key() { |
| 19 | 31 | return 'antispam'; |
| @@ -39,10 +51,16 @@ | ||
| 39 | 51 | * |
| 40 | 52 | * @return void |
| 41 | 53 | */ |
| 42 | 54 | public function init() { |
| 43 | - add_filter( 'frm_form_attributes', array( $this, 'add_token_to_form' ), 10, 1 ); | |
| 44 | - add_filter( 'frm_form_div_attributes', array( $this, 'add_token_to_form' ), 10, 1 ); | |
| 55 | + if ( self::$filters_added ) { | |
| 56 | + return; | |
| 57 | + } | |
| 58 | + | |
| 59 | + self::$filters_added = true; | |
| 60 | + | |
| 61 | + add_filter( 'frm_form_attributes', array( $this, 'add_token_to_form' ), 10, 2 ); | |
| 62 | + add_filter( 'frm_form_div_attributes', array( $this, 'add_token_to_form' ), 10, 2 ); | |
| 45 | 63 | } |
| 46 | 64 | |
| 47 | 65 | /** |
| 48 | 66 | * Return a valid token. |
| @@ -100,9 +118,9 @@ | ||
| 100 | 118 | private function get_valid_tokens() { |
| 101 | 119 | $current_date = time(); |
| 102 | 120 | |
| 103 | 121 | // Create our array of times to check before today. A user with a longer |
| 104 | - // cache time can extend this. A user with a shorter cache time can remove times. | |
| 122 | + // Cache time can extend this. A user with a shorter cache time can remove times. | |
| 105 | 123 | $valid_token_times_before = apply_filters( |
| 106 | 124 | 'frm_form_token_check_before_today', |
| 107 | 125 | array( |
| 108 | 126 | // Two days ago. |
| @@ -107,9 +125,9 @@ | ||
| 107 | 125 | array( |
| 108 | 126 | // Two days ago. |
| 109 | 127 | 2 * DAY_IN_SECONDS, |
| 110 | 128 | // One day ago. |
| 111 | - 1 * DAY_IN_SECONDS, | |
| 129 | + DAY_IN_SECONDS, | |
| 112 | 130 | ) |
| 113 | 131 | ); |
| 114 | 132 | |
| 115 | 133 | // Mostly to catch edge cases like the form page loading and submitting on two different days. |
| @@ -159,18 +177,31 @@ | ||
| 159 | 177 | return in_array( $token, $this->get_valid_tokens(), true ); |
| 160 | 178 | } |
| 161 | 179 | |
| 162 | 180 | /** |
| 163 | - * Add the token field to the form. | |
| 181 | + * Add the token field to the form if the form has Anti-Spam enabled. | |
| 164 | 182 | * |
| 165 | 183 | * @since 4.11 |
| 184 | + * @since 6.34 The $form param was added, and forms without Anti-Spam enabled are now skipped. | |
| 166 | 185 | * |
| 167 | - * @param string $attributes | |
| 186 | + * @param string $attributes | |
| 187 | + * @param object|null $form The form being rendered. | |
| 168 | 188 | * |
| 169 | 189 | * @return string |
| 170 | 190 | */ |
| 171 | - public function add_token_to_form( $attributes ) { | |
| 172 | - return $attributes . ( ' data-token="' . esc_attr( $this->get() ) . '"' ); | |
| 191 | + public function add_token_to_form( $attributes, $form = null ) { | |
| 192 | + $antispam = $this; | |
| 193 | + | |
| 194 | + if ( $form ) { | |
| 195 | + $antispam = new self( (int) $form->id ); | |
| 196 | + $antispam->form = $form; | |
| 197 | + } | |
| 198 | + | |
| 199 | + if ( ! $antispam->run_antispam() ) { | |
| 200 | + return $attributes; | |
| 201 | + } | |
| 202 | + | |
| 203 | + return $attributes . ( ' data-token="' . esc_attr( $antispam->get() ) . '"' ); | |
| 173 | 204 | } |
| 174 | 205 | |
| 175 | 206 | /** |
| 176 | 207 | * @param int $form_id |
| @@ -208,9 +239,9 @@ | ||
| 208 | 239 | |
| 209 | 240 | // If the antispam setting is enabled and we don't have a token, bail. |
| 210 | 241 | if ( ! $token ) { |
| 211 | 242 | if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) { |
| 212 | - // add an exception for the entries page. | |
| 243 | + // Add an exception for the entries page. | |
| 213 | 244 | return true; |
| 214 | 245 | } |
| 215 | 246 | |
| 216 | 247 | return $this->process_antispam_filter( $this->get_missing_token_message() ); |
| @@ -273,13 +304,13 @@ | ||
| 273 | 304 | } |
| 274 | 305 | |
| 275 | 306 | // If the user is an admin, return text with a link to support. |
| 276 | 307 | // We add a space here to separate the sentences, but outside of the localized |
| 277 | - // text to avoid it being removed. | |
| 308 | + // Text to avoid it being removed. | |
| 278 | 309 | return ' ' . sprintf( |
| 279 | 310 | // translators: %1$s start link, %2$s end link. |
| 280 | 311 | esc_html__( 'Please check out our %1$stroubleshooting guide%2$s for details on resolving this issue.', 'formidable' ), |
| 281 | - '<a href="https://formidableforms.com/knowledgebase/add-spam-protection/">', | |
| 312 | + '<a href="https://formidableforms.com/knowledgebase/add-spam-protection/" target="_blank" rel="noopener">', | |
| 282 | 313 | '</a>' |
| 283 | 314 | ); |
| 284 | 315 | } |
| 285 | 316 | |
| @@ -314,12 +345,14 @@ | ||
| 314 | 345 | /** |
| 315 | 346 | * @return void |
| 316 | 347 | */ |
| 317 | 348 | private static function clear_wp_super_cache() { |
| 318 | - if ( function_exists( 'wp_cache_clean_cache' ) ) { | |
| 319 | - global $file_prefix; | |
| 320 | - wp_cache_clean_cache( $file_prefix, true ); | |
| 349 | + if ( ! function_exists( 'wp_cache_clean_cache' ) ) { | |
| 350 | + return; | |
| 321 | 351 | } |
| 352 | + | |
| 353 | + global $file_prefix; | |
| 354 | + wp_cache_clean_cache( $file_prefix, true ); | |
| 322 | 355 | } |
| 323 | 356 | |
| 324 | 357 | /** |
| 325 | 358 | * @return void |