verify-token-action.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Captcha\Friendly_Captcha; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 7 | use JFB_Modules\Gateways\Actions_Abstract\Action_Application_Raw_Body_It; |
| 8 | use JFB_Modules\Gateways\Base_Gateway_Action; |
| 9 | |
| 10 | class Verify_Token_Action extends Base_Gateway_Action implements |
| 11 | Action_Application_Raw_Body_It { |
| 12 | |
| 13 | private $secret; |
| 14 | private $solution; |
| 15 | private $site_key; |
| 16 | |
| 17 | public function action_endpoint() { |
| 18 | return 'siteverify'; |
| 19 | } |
| 20 | |
| 21 | public function base_url(): string { |
| 22 | return 'https://api.friendlycaptcha.com/api/v1/'; |
| 23 | } |
| 24 | |
| 25 | public function send_request() { |
| 26 | $response = parent::send_request(); |
| 27 | |
| 28 | if ( ! empty( $response['success'] ) ) { |
| 29 | return $response; |
| 30 | } |
| 31 | |
| 32 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 33 | throw new Gateway_Exception( Module::SPAM_EXCEPTION, $response, $this->get_request_args() ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @throws Gateway_Exception |
| 38 | */ |
| 39 | public function before_make_request() { |
| 40 | if ( $this->solution && $this->secret && $this->site_key ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 45 | throw new Gateway_Exception( Module::SPAM_EXCEPTION, 'Empty solution. Spammer detected' ); |
| 46 | } |
| 47 | |
| 48 | public function set_secret( string $secret ): Verify_Token_Action { |
| 49 | $this->secret = $secret; |
| 50 | |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | public function set_solution( string $solution ): Verify_Token_Action { |
| 55 | $this->solution = $solution; |
| 56 | |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | public function set_site_key( string $key ): Verify_Token_Action { |
| 61 | $this->site_key = $key; |
| 62 | |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | public function action_body() { |
| 68 | return array( |
| 69 | 'secret' => $this->secret, |
| 70 | 'solution' => $this->solution, |
| 71 | 'sitekey' => $this->site_key, |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 |