getresponse-handler.php
2 years ago
integration-base.php
2 years ago
mailchimp-handler.php
2 years ago
integration-base.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Integrations; |
| 4 | |
| 5 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 6 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 7 | |
| 8 | // If this file is called directly, abort. |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | abstract class Integration_Base { |
| 14 | protected $api_base_url = ''; |
| 15 | protected $api_key = ''; |
| 16 | protected $api_request_args = array(); |
| 17 | |
| 18 | public function __construct( $api_key ) { |
| 19 | $this->api_key = $api_key; |
| 20 | } |
| 21 | |
| 22 | public function success_codes(): array { |
| 23 | return array( 204, 200 ); |
| 24 | } |
| 25 | |
| 26 | abstract public function get_all_data(); |
| 27 | |
| 28 | public function request( $end_point, $request_args = array() ) { |
| 29 | $response = $this->base_request( $end_point, $request_args ); |
| 30 | |
| 31 | if ( ! $response || is_wp_error( $response ) ) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | $data = wp_remote_retrieve_body( $response ); |
| 36 | |
| 37 | if ( ! $data ) { |
| 38 | return array(); |
| 39 | } |
| 40 | |
| 41 | $data = json_decode( $data, true ); |
| 42 | |
| 43 | return $data; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param $end_point |
| 48 | * @param array $request_args |
| 49 | * |
| 50 | * @throws Silence_Exception |
| 51 | */ |
| 52 | public function request_with_code( $end_point, $request_args = array() ) { |
| 53 | $response = $this->base_request( $end_point, $request_args ); |
| 54 | $code = (int) wp_remote_retrieve_response_code( $response ); |
| 55 | |
| 56 | if ( ! in_array( $code, $this->success_codes(), true ) ) { |
| 57 | $message = wp_remote_retrieve_response_message( $response ); |
| 58 | |
| 59 | throw new Silence_Exception( esc_html( $message ) ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public function base_request( $end_point, $request_args = array() ) { |
| 64 | $args = array_merge_recursive( $this->api_request_args, $request_args ); |
| 65 | $url = esc_url_raw( $this->api_base_url . $end_point ); |
| 66 | |
| 67 | return wp_remote_request( $url, $args ); |
| 68 | } |
| 69 | |
| 70 | } |
| 71 |