| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | /** |
| 9 | 9 | * Track the printed selectors so we do not print the same CSS twice. |
| 10 | 10 | * |
| 11 | 11 | * @since 6.22 |
| 12 | + * | |
| 12 | 13 | * @var array |
| 13 | 14 | */ |
| 14 | 15 | private static $printed_honeypot_selectors = array(); |
| 15 | 16 | |
| @@ -28,11 +29,13 @@ | ||
| 28 | 29 | protected function get_option_key() { |
| 29 | 30 | return 'honeypot'; |
| 30 | 31 | } |
| 31 | 32 | |
| 33 | + /** | |
| 34 | + * @return bool | |
| 35 | + */ | |
| 32 | 36 | private static function is_enabled() { |
| 33 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 34 | - return $frm_settings->honeypot; | |
| 37 | + return (bool) FrmAppHelper::get_settings()->honeypot; | |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | /** |
| 38 | 41 | * @return bool |
| @@ -38,9 +41,9 @@ | ||
| 38 | 41 | * @return bool |
| 39 | 42 | */ |
| 40 | 43 | public function validate() { |
| 41 | 44 | if ( ! $this->is_option_on() || ! $this->check_honeypot_filter() ) { |
| 42 | - // never flag as honeypot spam if disabled. | |
| 45 | + // Never flag as honeypot spam if disabled. | |
| 43 | 46 | return true; |
| 44 | 47 | } |
| 45 | 48 | return ! $this->is_honeypot_spam(); |
| 46 | 49 | } |
| @@ -49,11 +52,12 @@ | ||
| 49 | 52 | * @return bool |
| 50 | 53 | */ |
| 51 | 54 | private function is_honeypot_spam() { |
| 52 | 55 | $is_honeypot_spam = $this->is_legacy_honeypot_spam(); |
| 56 | + | |
| 53 | 57 | if ( ! $is_honeypot_spam ) { |
| 58 | + $field_id = $this->get_honeypot_field_id(); | |
| 54 | 59 | |
| 55 | - $field_id = $this->get_honeypot_field_id(); | |
| 56 | 60 | if ( ! $field_id ) { |
| 57 | 61 | return false; |
| 58 | 62 | } |
| 59 | 63 | |
| @@ -98,8 +102,9 @@ | ||
| 98 | 102 | * @return void |
| 99 | 103 | */ |
| 100 | 104 | public static function maybe_render_field( $form_id ) { |
| 101 | 105 | $honeypot = new self( $form_id ); |
| 106 | + | |
| 102 | 107 | if ( ! $honeypot->should_render_field() ) { |
| 103 | 108 | return; |
| 104 | 109 | } |
| 105 | 110 | |
| @@ -114,10 +119,9 @@ | ||
| 114 | 119 | |
| 115 | 120 | global $frm_vars; |
| 116 | 121 | $offset = isset( $frm_vars['honeypot_selectors'] ) ? count( $frm_vars['honeypot_selectors'] ) + 1 : 1; |
| 117 | 122 | $honeypot_field_id = $max_field_id ? $max_field_id + $offset : $offset; |
| 118 | - | |
| 119 | - $class = class_exists( 'FrmProFormState' ) ? 'FrmProFormState' : 'FrmFormState'; | |
| 123 | + $class = class_exists( 'FrmProFormState' ) ? 'FrmProFormState' : 'FrmFormState'; | |
| 120 | 124 | $class::set_initial_value( 'honeypot_field_id', $honeypot_field_id ); |
| 121 | 125 | |
| 122 | 126 | $honeypot->render_field( $honeypot_field_id ); |
| 123 | 127 | self::maybe_print_honeypot_css(); |
| @@ -126,8 +130,10 @@ | ||
| 126 | 130 | /** |
| 127 | 131 | * Maybe print honeypot JS. |
| 128 | 132 | * |
| 129 | 133 | * @since 6.21 |
| 134 | + * | |
| 135 | + * @return void | |
| 130 | 136 | */ |
| 131 | 137 | public static function maybe_print_honeypot_js() { |
| 132 | 138 | if ( FrmAppHelper::is_admin() || ! self::is_enabled() ) { |
| 133 | 139 | return; |
| @@ -133,8 +139,9 @@ | ||
| 133 | 139 | return; |
| 134 | 140 | } |
| 135 | 141 | |
| 136 | 142 | $css = self::get_honeypot_field_css(); |
| 143 | + | |
| 137 | 144 | if ( ! $css ) { |
| 138 | 145 | return; |
| 139 | 146 | } |
| 140 | 147 | |
| @@ -158,8 +165,10 @@ | ||
| 158 | 165 | /** |
| 159 | 166 | * Maybe print honeypot CSS in case JS doesn't run. |
| 160 | 167 | * |
| 161 | 168 | * @since 6.21 |
| 169 | + * | |
| 170 | + * @return void | |
| 162 | 171 | */ |
| 163 | 172 | public static function maybe_print_honeypot_css() { |
| 164 | 173 | // Print the CSS if form is loaded by API. |
| 165 | 174 | if ( ! FrmFormsHelper::form_is_loaded_by_api() ) { |
| @@ -166,8 +175,9 @@ | ||
| 166 | 175 | return; |
| 167 | 176 | } |
| 168 | 177 | |
| 169 | 178 | $css = self::get_honeypot_field_css(); |
| 179 | + | |
| 170 | 180 | if ( $css ) { |
| 171 | 181 | echo '<style>' . esc_html( $css ) . '</style>'; |
| 172 | 182 | } |
| 173 | 183 | } |
| @@ -178,15 +188,18 @@ | ||
| 178 | 188 | * @return string |
| 179 | 189 | */ |
| 180 | 190 | private static function get_honeypot_field_css() { |
| 181 | 191 | global $frm_vars; |
| 192 | + | |
| 182 | 193 | if ( empty( $frm_vars['honeypot_selectors'] ) ) { |
| 183 | 194 | return ''; |
| 184 | 195 | } |
| 185 | 196 | |
| 186 | 197 | $selectors = $frm_vars['honeypot_selectors']; |
| 198 | + | |
| 187 | 199 | if ( self::$printed_honeypot_selectors ) { |
| 188 | 200 | $selectors = array_diff( $selectors, self::$printed_honeypot_selectors ); |
| 201 | + | |
| 189 | 202 | if ( ! $selectors ) { |
| 190 | 203 | return ''; |
| 191 | 204 | } |
| 192 | 205 | } |
| @@ -205,8 +218,9 @@ | ||
| 205 | 218 | } |
| 206 | 219 | |
| 207 | 220 | /** |
| 208 | 221 | * @param int $honeypot_field_id |
| 222 | + * | |
| 209 | 223 | * @return void |
| 210 | 224 | */ |
| 211 | 225 | public function render_field( $honeypot_field_id = 0 ) { |
| 212 | 226 | if ( ! $honeypot_field_id ) { |
| @@ -224,8 +238,9 @@ | ||
| 224 | 238 | ); |
| 225 | 239 | |
| 226 | 240 | $container_id = 'frm_field_' . $field_id . '_container'; |
| 227 | 241 | $this->track_html_id( $container_id ); |
| 242 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 228 | 243 | ?> |
| 229 | 244 | <div id="<?php echo esc_attr( $container_id ); ?>"> |
| 230 | 245 | <label for="<?php echo esc_attr( $input_attrs['id'] ); ?>" <?php FrmFormsHelper::maybe_hide_inline(); ?>> |
| 231 | 246 | <?php esc_html_e( 'If you are human, leave this field blank.', 'formidable' ); ?> |
| @@ -232,12 +247,19 @@ | ||
| 232 | 247 | </label> |
| 233 | 248 | <input <?php FrmAppHelper::array_to_html_params( $input_attrs, true ); ?> <?php FrmFormsHelper::maybe_hide_inline(); ?> /> |
| 234 | 249 | </div> |
| 235 | 250 | <?php |
| 251 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 236 | 252 | } |
| 237 | 253 | |
| 254 | + /** | |
| 255 | + * @param string $html_id | |
| 256 | + * | |
| 257 | + * @return void | |
| 258 | + */ | |
| 238 | 259 | private function track_html_id( $html_id ) { |
| 239 | 260 | global $frm_vars; |
| 261 | + | |
| 240 | 262 | if ( ! isset( $frm_vars['honeypot_selectors'] ) ) { |
| 241 | 263 | $frm_vars['honeypot_selectors'] = array(); |
| 242 | 264 | } |
| 243 | 265 | |
| @@ -243,14 +265,19 @@ | ||
| 243 | 265 | |
| 244 | 266 | $frm_vars['honeypot_selectors'][] = '#' . $html_id; |
| 245 | 267 | } |
| 246 | 268 | |
| 269 | + /** | |
| 270 | + * @return int | |
| 271 | + */ | |
| 247 | 272 | private function get_honeypot_field_id() { |
| 248 | - $class = class_exists( 'FrmProFormState' ) ? 'FrmProFormState' : 'FrmFormState'; | |
| 249 | - $honeypot_field_id = $class::get_from_request( 'honeypot_field_id', 0 ); | |
| 250 | - return $honeypot_field_id; | |
| 273 | + $class = class_exists( 'FrmProFormState' ) ? 'FrmProFormState' : 'FrmFormState'; | |
| 274 | + return $class::get_from_request( 'honeypot_field_id', 0 ); | |
| 251 | 275 | } |
| 252 | 276 | |
| 277 | + /** | |
| 278 | + * @return string | |
| 279 | + */ | |
| 253 | 280 | private function get_honeypot_field_key() { |
| 254 | 281 | return FrmAppHelper::generate_new_key( 5 ); |
| 255 | 282 | } |
| 256 | 283 | |
| @@ -283,8 +310,9 @@ | ||
| 283 | 310 | * @return string The generated class name. |
| 284 | 311 | */ |
| 285 | 312 | public static function generate_class_name() { |
| 286 | 313 | $class_name = self::get_honeypot_class_name(); |
| 314 | + | |
| 287 | 315 | if ( 'frm_verify' !== $class_name ) { |
| 288 | 316 | // Re-use the option. |
| 289 | 317 | // We can't generate a new class too often or the field may not be hidden. |
| 290 | 318 | return $class_name; |
| @@ -300,11 +328,13 @@ | ||
| 300 | 328 | * @return string The current class name to use the for Honeypot field. |
| 301 | 329 | */ |
| 302 | 330 | private static function get_honeypot_class_name() { |
| 303 | 331 | $option = get_option( 'frm_honeypot_class' ); |
| 332 | + | |
| 304 | 333 | if ( ! is_string( $option ) ) { |
| 305 | 334 | // For backward compatibility use the old class name. |
| 306 | 335 | return 'frm_verify'; |
| 307 | 336 | } |
| 337 | + | |
| 308 | 338 | return $option; |
| 309 | 339 | } |
| 310 | 340 | } |