module.php
124 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 | const SPAM_EXCEPTION = 'honeypot'; |
| 29 | public function __construct() { |
| 30 | add_filter( 'jet-form-builder/security/spam-statuses', array( $this, 'add_spam_statuses' ) ); |
| 31 | } |
| 32 | public function add_spam_statuses( $statuses ) { |
| 33 | $statuses[] = self::SPAM_EXCEPTION; |
| 34 | return $statuses; |
| 35 | } |
| 36 | |
| 37 | public function init_hooks() { |
| 38 | add_filter( |
| 39 | 'jet-form-builder/after-start-form', |
| 40 | array( $this, 'on_render_form' ) |
| 41 | ); |
| 42 | add_filter( |
| 43 | 'jet-form-builder/request-handler/request', |
| 44 | array( $this, 'handle_request' ) |
| 45 | ); |
| 46 | add_filter( |
| 47 | 'jet-form-builder/message-types', |
| 48 | array( $this, 'handle_global_messages' ) |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | public function remove_hooks() { |
| 53 | remove_filter( |
| 54 | 'jet-form-builder/after-start-form', |
| 55 | array( $this, 'on_render_form' ) |
| 56 | ); |
| 57 | remove_filter( |
| 58 | 'jet-form-builder/request-handler/request', |
| 59 | array( $this, 'handle_request' ) |
| 60 | ); |
| 61 | remove_filter( |
| 62 | 'jet-form-builder/message-types', |
| 63 | array( $this, 'handle_global_messages' ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | public function on_render_form( string $content ): string { |
| 68 | $args = jet_form_builder()->post_type->get_args(); |
| 69 | |
| 70 | if ( empty( $args['use_honeypot'] ) ) { |
| 71 | return $content; |
| 72 | } |
| 73 | |
| 74 | $field = Live_Form::force_render_field( |
| 75 | 'text-field', |
| 76 | array( |
| 77 | 'field_type' => 'email', |
| 78 | 'name' => self::FIELD, |
| 79 | 'autocomplete' => 'nope', |
| 80 | ) |
| 81 | ); |
| 82 | |
| 83 | $content .= sprintf( |
| 84 | '<div style="transform: scale(0); position: absolute;">%s</div>', |
| 85 | $field |
| 86 | ); |
| 87 | |
| 88 | return $content; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param array $request |
| 93 | * |
| 94 | * @return array |
| 95 | * @throws Spam_Exception |
| 96 | */ |
| 97 | public function handle_request( array $request ): array { |
| 98 | $args = jet_form_builder()->post_type->get_args(); |
| 99 | |
| 100 | if ( empty( $args['use_honeypot'] ) ) { |
| 101 | return $request; |
| 102 | } |
| 103 | |
| 104 | if ( ! empty( $request[ self::FIELD ] ) ) { |
| 105 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 106 | throw new Spam_Exception( self::SPAM_EXCEPTION ); |
| 107 | } |
| 108 | |
| 109 | unset( $request[ self::FIELD ] ); |
| 110 | |
| 111 | return $request; |
| 112 | } |
| 113 | |
| 114 | public function handle_global_messages( array $types ): array { |
| 115 | $types[ self::SPAM_EXCEPTION ] = array( |
| 116 | 'label' => __( 'Honeypot validation failed', 'jet-form-builder' ), |
| 117 | 'value' => __( 'You are not allowed to fill in the honeypot field', 'jet-form-builder' ), |
| 118 | ); |
| 119 | |
| 120 | return $types; |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |