module.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Security\Honeypot; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Request_Exception; |
| 7 | use Jet_Form_Builder\Live_Form; |
| 8 | use JFB_Components\Module\Base_Module_It; |
| 9 | use JFB_Modules\Security\Exceptions\Spam_Exception; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | class Module implements Base_Module_It { |
| 17 | |
| 18 | const FIELD = '_jfb_email_hp_'; |
| 19 | |
| 20 | public function rep_item_id() { |
| 21 | return 'honeypot'; |
| 22 | } |
| 23 | |
| 24 | public function condition(): bool { |
| 25 | return true; |
| 26 | } |
| 27 | |
| 28 | public function init_hooks() { |
| 29 | add_filter( |
| 30 | 'jet-form-builder/after-start-form', |
| 31 | array( $this, 'on_render_form' ) |
| 32 | ); |
| 33 | add_filter( |
| 34 | 'jet-form-builder/request-handler/request', |
| 35 | array( $this, 'handle_request' ) |
| 36 | ); |
| 37 | add_filter( |
| 38 | 'jet-form-builder/message-types', |
| 39 | array( $this, 'handle_global_messages' ) |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | public function remove_hooks() { |
| 44 | remove_filter( |
| 45 | 'jet-form-builder/after-start-form', |
| 46 | array( $this, 'on_render_form' ) |
| 47 | ); |
| 48 | remove_filter( |
| 49 | 'jet-form-builder/request-handler/request', |
| 50 | array( $this, 'handle_request' ) |
| 51 | ); |
| 52 | remove_filter( |
| 53 | 'jet-form-builder/message-types', |
| 54 | array( $this, 'handle_global_messages' ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | public function on_render_form( string $content ): string { |
| 59 | $args = jet_form_builder()->post_type->get_args(); |
| 60 | |
| 61 | if ( empty( $args['use_honeypot'] ) ) { |
| 62 | return $content; |
| 63 | } |
| 64 | |
| 65 | $field = Live_Form::force_render_field( |
| 66 | 'text-field', |
| 67 | array( |
| 68 | 'field_type' => 'email', |
| 69 | 'name' => self::FIELD, |
| 70 | 'autocomplete' => 'nope', |
| 71 | ) |
| 72 | ); |
| 73 | |
| 74 | $content .= sprintf( |
| 75 | '<div style="transform: scale(0); position: absolute;">%s</div>', |
| 76 | $field |
| 77 | ); |
| 78 | |
| 79 | return $content; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param array $request |
| 84 | * |
| 85 | * @return array |
| 86 | * @throws Spam_Exception |
| 87 | */ |
| 88 | public function handle_request( array $request ): array { |
| 89 | $args = jet_form_builder()->post_type->get_args(); |
| 90 | |
| 91 | if ( empty( $args['use_honeypot'] ) ) { |
| 92 | return $request; |
| 93 | } |
| 94 | |
| 95 | if ( ! empty( $request[ self::FIELD ] ) ) { |
| 96 | throw new Spam_Exception( 'honeypot' ); |
| 97 | } |
| 98 | |
| 99 | unset( $request[ self::FIELD ] ); |
| 100 | |
| 101 | return $request; |
| 102 | } |
| 103 | |
| 104 | public function handle_global_messages( array $types ): array { |
| 105 | $types['honeypot'] = array( |
| 106 | 'label' => __( 'Honeypot validation failed', 'jet-form-builder' ), |
| 107 | 'value' => __( 'You are not allowed to fill in the honeypot field', 'jet-form-builder' ), |
| 108 | ); |
| 109 | |
| 110 | return $types; |
| 111 | } |
| 112 | |
| 113 | } |
| 114 |