| @@ -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. |
| @@ -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 |
| @@ -277,9 +308,9 @@ | ||
| 277 | 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 | |