builders
2 years ago
privacy
2 years ago
class-yit-ajax.php
2 years ago
class-yit-assets.php
2 years ago
class-yit-cpt-unlimited.php
5 years ago
class-yit-gradients.php
2 years ago
class-yit-help-desk.php
2 years ago
class-yit-icons.php
2 years ago
class-yit-metabox.php
2 years ago
class-yit-plugin-common.php
5 years ago
class-yit-plugin-licence.php
2 years ago
class-yit-plugin-panel-woocommerce.php
1 year ago
class-yit-plugin-panel.php
1 year ago
class-yit-plugin-subpanel.php
2 years ago
class-yit-pointers.php
2 years ago
class-yit-theme-licence.php
2 years ago
class-yit-upgrade.php
5 years ago
class-yit-video.php
5 years ago
class-yith-bh-onboarding.php
3 years ago
class-yith-dashboard.php
4 years ago
class-yith-debug.php
2 years ago
class-yith-external-services.php
1 year ago
class-yith-post-type-admin.php
2 years ago
class-yith-system-status.php
1 year ago
class-yith-external-services.php
106 lines
| 1 | <?php |
| 2 | /** |
| 3 | * YITH Plugin External Services Class. |
| 4 | * |
| 5 | * @class YITH_External_Services |
| 6 | * @author YITH <plugins@yithemes.com> |
| 7 | * @package YITH\PluginFramework\Classes |
| 8 | * @since 4.6.2 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 12 | |
| 13 | if ( ! class_exists( 'YITH_External_Services' ) ) { |
| 14 | /** |
| 15 | * Class YITH_External_Services |
| 16 | */ |
| 17 | class YITH_External_Services { |
| 18 | |
| 19 | const API_URL = "https://o37n31x524.execute-api.eu-north-1.amazonaws.com/v1/"; |
| 20 | |
| 21 | /** |
| 22 | * The single instance of the class. |
| 23 | * |
| 24 | * @var self |
| 25 | */ |
| 26 | protected static $instance = null; |
| 27 | |
| 28 | /** |
| 29 | * Get class instance. |
| 30 | * |
| 31 | * @return self |
| 32 | */ |
| 33 | public static function get_instance() { |
| 34 | return ! is_null( static::$instance ) ? static::$instance : static::$instance = new static(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Constructor |
| 39 | */ |
| 40 | protected function __construct() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Do an API request |
| 45 | * |
| 46 | * @param string $endpoint The endpoint of the request. |
| 47 | * @param array $args An array of request arguments. |
| 48 | * |
| 49 | * @return array |
| 50 | * @throws Exception Throws Exception. |
| 51 | * @since 4.6.2 |
| 52 | */ |
| 53 | public function request( $endpoint, $args = array() ) { |
| 54 | $defaults = array( |
| 55 | 'headers' => array( |
| 56 | 'Content-Type' => ' application/json', |
| 57 | ), |
| 58 | 'method' => 'POST', |
| 59 | 'timeout' => 2900, |
| 60 | ); |
| 61 | |
| 62 | // Merge given args with default. |
| 63 | $args = array_merge( $defaults, $args ); |
| 64 | |
| 65 | $url = self::API_URL . $endpoint; |
| 66 | |
| 67 | // Make the request. |
| 68 | $response = wp_remote_request( $url, $args ); |
| 69 | |
| 70 | |
| 71 | if ( is_wp_error( $response ) || ! in_array( absint( $response['response']['code'] ), array( 200, 201, 400, 204 ), true ) ) { |
| 72 | throw new Exception( esc_html_x( 'Error: Unable to process the External Services API Request.' . $response['response']['message'], 'External Services error message', 'yith-plugin-fw' ) ); |
| 73 | } |
| 74 | |
| 75 | return $this->maybe_json_decode( $response['body'] ); |
| 76 | |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Decode a Json string if necessary |
| 81 | * |
| 82 | * @param string $response String to check. |
| 83 | * |
| 84 | * @return string|array |
| 85 | */ |
| 86 | private function maybe_json_decode( $response ) { |
| 87 | json_decode( $response ); |
| 88 | return json_last_error() === JSON_ERROR_NONE ? json_decode( $response, true ) : $response; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * Return the authentication token |
| 94 | * |
| 95 | * @param array $args Args to request a new token. |
| 96 | * |
| 97 | * @return array|string |
| 98 | * @throws Exception |
| 99 | */ |
| 100 | public function auth( $args ) { |
| 101 | return $this->request('auth', $args ); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | YITH_External_Services::get_instance(); |