| @@ -12,20 +12,8 @@ | ||
| 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 | - /** | |
| 28 | 16 | * @return string |
| 29 | 17 | */ |
| 30 | 18 | protected function get_option_key() { |
| 31 | 19 | return 'antispam'; |
| @@ -32,14 +20,11 @@ | ||
| 32 | 20 | } |
| 33 | 21 | |
| 34 | 22 | /** |
| 35 | 23 | * @param int $form_id |
| 36 | - * | |
| 37 | - * @return void | |
| 38 | 24 | */ |
| 39 | 25 | public static function maybe_init( $form_id ) { |
| 40 | 26 | $antispam = new self( $form_id ); |
| 41 | - | |
| 42 | 27 | if ( $antispam->run_antispam() ) { |
| 43 | 28 | $antispam->init(); |
| 44 | 29 | } |
| 45 | 30 | } |
| @@ -47,20 +32,12 @@ | ||
| 47 | 32 | /** |
| 48 | 33 | * Initialise the actions for the Anti-spam. |
| 49 | 34 | * |
| 50 | 35 | * @since 4.11 |
| 51 | - * | |
| 52 | - * @return void | |
| 53 | 36 | */ |
| 54 | 37 | public function init() { |
| 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 ); | |
| 38 | + add_filter( 'frm_form_attributes', array( $this, 'add_token_to_form' ), 10, 1 ); | |
| 39 | + add_filter( 'frm_form_div_attributes', array( $this, 'add_token_to_form' ), 10, 1 ); | |
| 63 | 40 | } |
| 64 | 41 | |
| 65 | 42 | /** |
| 66 | 43 | * Return a valid token. |
| @@ -73,9 +50,13 @@ | ||
| 73 | 50 | */ |
| 74 | 51 | private function get( $current = true ) { |
| 75 | 52 | // If $current was not passed, or it is true, we use the current timestamp. |
| 76 | 53 | // If $current was passed in as a string, we'll use that passed in timestamp. |
| 77 | - $time = $current === true ? time() : $current; | |
| 54 | + if ( $current !== true ) { | |
| 55 | + $time = $current; | |
| 56 | + } else { | |
| 57 | + $time = time(); | |
| 58 | + } | |
| 78 | 59 | |
| 79 | 60 | // Format the timestamp to be less exact, as we want to deal in days. |
| 80 | 61 | // June 19th, 2020 would get formatted as: 1906202017125. |
| 81 | 62 | // Day of the month, month number, year, day number of the year, week number of the year. |
| @@ -81,14 +62,13 @@ | ||
| 81 | 62 | // Day of the month, month number, year, day number of the year, week number of the year. |
| 82 | 63 | $token_date = gmdate( 'dmYzW', $time ); |
| 83 | 64 | |
| 84 | 65 | // Combine our token date and our token salt, and md5 it. |
| 85 | - return md5( $token_date . $this->get_antispam_secret_key() ); | |
| 66 | + $form_token_string = md5( $token_date . $this->get_antispam_secret_key() ); | |
| 67 | + | |
| 68 | + return $form_token_string; | |
| 86 | 69 | } |
| 87 | 70 | |
| 88 | - /** | |
| 89 | - * @return string | |
| 90 | - */ | |
| 91 | 71 | private function get_antispam_secret_key() { |
| 92 | 72 | $secret_key = get_option( 'frm_antispam_secret_key' ); |
| 93 | 73 | |
| 94 | 74 | // If we already have the secret, send it back. |
| @@ -118,16 +98,14 @@ | ||
| 118 | 98 | private function get_valid_tokens() { |
| 119 | 99 | $current_date = time(); |
| 120 | 100 | |
| 121 | 101 | // Create our array of times to check before today. A user with a longer |
| 122 | - // Cache time can extend this. A user with a shorter cache time can remove times. | |
| 102 | + // cache time can extend this. A user with a shorter cache time can remove times. | |
| 123 | 103 | $valid_token_times_before = apply_filters( |
| 124 | 104 | 'frm_form_token_check_before_today', |
| 125 | 105 | array( |
| 126 | - // Two days ago. | |
| 127 | - 2 * DAY_IN_SECONDS, | |
| 128 | - // One day ago. | |
| 129 | - DAY_IN_SECONDS, | |
| 106 | + ( 2 * DAY_IN_SECONDS ), // Two days ago. | |
| 107 | + ( 1 * DAY_IN_SECONDS ), // One day ago. | |
| 130 | 108 | ) |
| 131 | 109 | ); |
| 132 | 110 | |
| 133 | 111 | // Mostly to catch edge cases like the form page loading and submitting on two different days. |
| @@ -134,10 +112,9 @@ | ||
| 134 | 112 | // This probably won't be filtered by users too much, but they could extend it. |
| 135 | 113 | $valid_token_times_after = apply_filters( |
| 136 | 114 | 'frm_form_token_check_after_today', |
| 137 | 115 | array( |
| 138 | - // Add in 45 minutes past today to catch some midnight edge cases. | |
| 139 | - 45 * MINUTE_IN_SECONDS, | |
| 116 | + ( 45 * MINUTE_IN_SECONDS ), // Add in 45 minutes past today to catch some midnight edge cases. | |
| 140 | 117 | ) |
| 141 | 118 | ); |
| 142 | 119 | |
| 143 | 120 | // Built up our valid tokens. |
| @@ -177,41 +154,26 @@ | ||
| 177 | 154 | return in_array( $token, $this->get_valid_tokens(), true ); |
| 178 | 155 | } |
| 179 | 156 | |
| 180 | 157 | /** |
| 181 | - * Add the token field to the form if the form has Anti-Spam enabled. | |
| 158 | + * Add the token field to the form. | |
| 182 | 159 | * |
| 183 | 160 | * @since 4.11 |
| 184 | - * @since 6.34 The $form param was added, and forms without Anti-Spam enabled are now skipped. | |
| 185 | 161 | * |
| 186 | - * @param string $attributes | |
| 187 | - * @param object|null $form The form being rendered. | |
| 162 | + * @param string $attributes | |
| 188 | 163 | * |
| 189 | 164 | * @return string |
| 190 | 165 | */ |
| 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() ) . '"' ); | |
| 166 | + public function add_token_to_form( $attributes ) { | |
| 167 | + $attributes .= ' data-token="' . esc_attr( $this->get() ) . '"'; | |
| 168 | + return $attributes; | |
| 204 | 169 | } |
| 205 | 170 | |
| 206 | 171 | /** |
| 207 | 172 | * @param int $form_id |
| 208 | - * | |
| 209 | - * @return void | |
| 210 | 173 | */ |
| 211 | 174 | public static function maybe_echo_token( $form_id ) { |
| 212 | 175 | $antispam = new self( $form_id ); |
| 213 | - | |
| 214 | 176 | if ( $antispam->run_antispam() ) { |
| 215 | 177 | echo 'data-token="' . esc_attr( $antispam->get() ) . '"'; |
| 216 | 178 | } |
| 217 | 179 | } |
| @@ -239,12 +201,11 @@ | ||
| 239 | 201 | |
| 240 | 202 | // If the antispam setting is enabled and we don't have a token, bail. |
| 241 | 203 | if ( ! $token ) { |
| 242 | 204 | if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) { |
| 243 | - // Add an exception for the entries page. | |
| 205 | + // add an exception for the entries page. | |
| 244 | 206 | return true; |
| 245 | 207 | } |
| 246 | - | |
| 247 | 208 | return $this->process_antispam_filter( $this->get_missing_token_message() ); |
| 248 | 209 | } |
| 249 | 210 | |
| 250 | 211 | // Verify the token. |
| @@ -255,8 +216,24 @@ | ||
| 255 | 216 | return $this->process_antispam_filter( true ); |
| 256 | 217 | } |
| 257 | 218 | |
| 258 | 219 | /** |
| 220 | + * @return bool True if saving a draft. | |
| 221 | + */ | |
| 222 | + private function is_saving_a_draft() { | |
| 223 | + global $frm_vars; | |
| 224 | + if ( empty( $frm_vars['form_params'] ) ) { | |
| 225 | + return false; | |
| 226 | + } | |
| 227 | + $form_params = $frm_vars['form_params']; | |
| 228 | + if ( ! isset( $form_params[ $this->form_id ] ) ) { | |
| 229 | + return false; | |
| 230 | + } | |
| 231 | + $this_form_params = $form_params[ $this->form_id ]; | |
| 232 | + return ! empty( $this_form_params['action'] ) && 'update' === $this_form_params['action']; | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 259 | 236 | * Helper to run our filter on all the responses for the antispam checks. |
| 260 | 237 | * |
| 261 | 238 | * @since 4.11 |
| 262 | 239 | * |
| @@ -303,14 +280,14 @@ | ||
| 303 | 280 | return ''; |
| 304 | 281 | } |
| 305 | 282 | |
| 306 | 283 | // If the user is an admin, return text with a link to support. |
| 307 | - // We add a space here to separate the sentences, but outside of the localized | |
| 308 | - // Text to avoid it being removed. | |
| 284 | + // We add a space here to seperate the sentences, but outside of the localized | |
| 285 | + // text to avoid it being removed. | |
| 309 | 286 | return ' ' . sprintf( |
| 310 | 287 | // translators: %1$s start link, %2$s end link. |
| 311 | 288 | esc_html__( 'Please check out our %1$stroubleshooting guide%2$s for details on resolving this issue.', 'formidable' ), |
| 312 | - '<a href="https://formidableforms.com/knowledgebase/add-spam-protection/" target="_blank" rel="noopener">', | |
| 289 | + '<a href="https://formidableforms.com/knowledgebase/add-spam-protection/">', | |
| 313 | 290 | '</a>' |
| 314 | 291 | ); |
| 315 | 292 | } |
| 316 | 293 | |
| @@ -315,10 +292,8 @@ | ||
| 315 | 292 | } |
| 316 | 293 | |
| 317 | 294 | /** |
| 318 | 295 | * Clear third party cache plugins to avoid data-tokens missing or appearing when the antispam setting is changed. |
| 319 | - * | |
| 320 | - * @return void | |
| 321 | 296 | */ |
| 322 | 297 | public static function clear_caches() { |
| 323 | 298 | self::clear_w3_total_cache(); |
| 324 | 299 | self::clear_wp_fastest_cache(); |
| @@ -325,11 +300,8 @@ | ||
| 325 | 300 | self::clear_wp_super_cache(); |
| 326 | 301 | self::clear_wp_optimize(); |
| 327 | 302 | } |
| 328 | 303 | |
| 329 | - /** | |
| 330 | - * @return void | |
| 331 | - */ | |
| 332 | 304 | private static function clear_w3_total_cache() { |
| 333 | 305 | if ( is_callable( 'w3tc_flush_all' ) ) { |
| 334 | 306 | w3tc_flush_all(); |
| 335 | 307 | } |
| @@ -334,30 +306,19 @@ | ||
| 334 | 306 | w3tc_flush_all(); |
| 335 | 307 | } |
| 336 | 308 | } |
| 337 | 309 | |
| 338 | - /** | |
| 339 | - * @return void | |
| 340 | - */ | |
| 341 | 310 | private static function clear_wp_fastest_cache() { |
| 342 | 311 | do_action( 'wpfc_clear_all_cache' ); |
| 343 | 312 | } |
| 344 | 313 | |
| 345 | - /** | |
| 346 | - * @return void | |
| 347 | - */ | |
| 348 | 314 | private static function clear_wp_super_cache() { |
| 349 | - if ( ! function_exists( 'wp_cache_clean_cache' ) ) { | |
| 350 | - return; | |
| 315 | + if ( function_exists( 'wp_cache_clean_cache' ) ) { | |
| 316 | + global $file_prefix; | |
| 317 | + wp_cache_clean_cache( $file_prefix, true ); | |
| 351 | 318 | } |
| 352 | - | |
| 353 | - global $file_prefix; | |
| 354 | - wp_cache_clean_cache( $file_prefix, true ); | |
| 355 | 319 | } |
| 356 | 320 | |
| 357 | - /** | |
| 358 | - * @return void | |
| 359 | - */ | |
| 360 | 321 | private static function clear_wp_optimize() { |
| 361 | 322 | if ( class_exists( 'WP_Optimize' ) ) { |
| 362 | 323 | WP_Optimize()->get_page_cache()->purge(); |
| 363 | 324 | } |