| @@ -31,9 +31,9 @@ | ||
| 31 | 31 | /** |
| 32 | 32 | * Setting the development mode. |
| 33 | 33 | */ |
| 34 | 34 | public function __construct() { |
| 35 | - $this->dev_mode = defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV; | |
| 35 | + $this->dev_mode = Helper::is_dev_api(); | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * Determining the endpoint URL based on the mode. |
| @@ -39,10 +39,10 @@ | ||
| 39 | 39 | * Determining the endpoint URL based on the mode. |
| 40 | 40 | * |
| 41 | 41 | * @return string |
| 42 | 42 | */ |
| 43 | - public function url() { | |
| 44 | - if ( $this->dev_mode ) { | |
| 43 | + public function url($is_live_api = false) { | |
| 44 | + if ( !$is_live_api && Helper::is_dev_api() ) { | |
| 45 | 45 | $this->url = 'https://app.templately.dev/api/plugin'; |
| 46 | 46 | } |
| 47 | 47 | return $this->url; |
| 48 | 48 | } |
| @@ -137,12 +137,14 @@ | ||
| 137 | 137 | if ( empty( $query ) ) { |
| 138 | 138 | $query = $this->query; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | + $is_live_api = null; | |
| 141 | 142 | $headers = [ |
| 142 | - 'Content-Type' => 'application/json', | |
| 143 | - 'x-templately-ip' => Helper::get_ip(), | |
| 144 | - 'x-templately-url' => home_url( '/' ) | |
| 143 | + 'Content-Type' => 'application/json', | |
| 144 | + 'x-templately-ip' => Helper::get_ip(), | |
| 145 | + 'x-templately-url' => home_url( '/' ), | |
| 146 | + 'x-templately-version' => TEMPLATELY_VERSION, | |
| 145 | 147 | ]; |
| 146 | 148 | |
| 147 | 149 | if ( ! empty( $args['headers'] ) ) { |
| 148 | 150 | $headers = wp_parse_args( $args['headers'], $headers ); |
| @@ -148,8 +150,13 @@ | ||
| 148 | 150 | $headers = wp_parse_args( $args['headers'], $headers ); |
| 149 | 151 | unset( $args['headers'] ); |
| 150 | 152 | } |
| 151 | 153 | |
| 154 | + if ( ! empty( $args['is_live_api'] ) ) { | |
| 155 | + $is_live_api = $args['is_live_api']; | |
| 156 | + unset( $args['is_live_api'] ); | |
| 157 | + } | |
| 158 | + | |
| 152 | 159 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 153 | 160 | Helper::log( 'URL: ' . $this->url() ); |
| 154 | 161 | Helper::log( 'QUERY: ' . $query ); |
| 155 | 162 | } |
| @@ -165,9 +172,9 @@ | ||
| 165 | 172 | $retryCount = 0; |
| 166 | 173 | $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3; |
| 167 | 174 | $args = wp_parse_args( $args, $_default_args ); |
| 168 | 175 | do { |
| 169 | - $response = wp_remote_post( $this->url(), $args ); | |
| 176 | + $response = wp_remote_post( $this->url($is_live_api), $args ); | |
| 170 | 177 | $retryCount++; |
| 171 | 178 | } while ( is_wp_error( $response ) && $retryCount < $maxRetries ); |
| 172 | 179 | |
| 173 | 180 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| @@ -180,9 +187,9 @@ | ||
| 180 | 187 | return $this->maybeErrors( $response, $args ); |
| 181 | 188 | } |
| 182 | 189 | |
| 183 | 190 | /** |
| 184 | - * Formating the self::post() response | |
| 191 | + * Formatting the self::post() response | |
| 185 | 192 | * |
| 186 | 193 | * @param mixed $response |
| 187 | 194 | * @param array $args |
| 188 | 195 | * @return mixed |
| @@ -191,13 +198,16 @@ | ||
| 191 | 198 | if ( $response instanceof WP_Error ) { |
| 192 | 199 | return $response; // Return WP_Error, if it is an error. |
| 193 | 200 | } |
| 194 | 201 | |
| 202 | + // Check for verification header before processing response body | |
| 203 | + Helper::check_verification_header( $response ); | |
| 204 | + | |
| 195 | 205 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 196 | 206 | $response_message = wp_remote_retrieve_response_message( $response ); |
| 197 | 207 | |
| 198 | 208 | /** |
| 199 | - * Retrive Data from Response Body. | |
| 209 | + * Retrieve Data from Response Body. | |
| 200 | 210 | */ |
| 201 | 211 | $response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 202 | 212 | /** |
| 203 | 213 | * If the graphql hit with any error. |