| @@ -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,12 +39,21 @@ | ||
| 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 | + | |
| 48 | + /** | |
| 49 | + * Filter the API endpoint URL | |
| 50 | + * | |
| 51 | + * @since 3.5.0 | |
| 52 | + * @param string $url The endpoint URL | |
| 53 | + */ | |
| 54 | + $this->url = apply_filters('templately_dev_api_endpoint_url', $this->url); | |
| 55 | + | |
| 47 | 56 | return $this->url; |
| 48 | 57 | } |
| 49 | 58 | |
| 50 | 59 | /** |
| @@ -137,12 +146,14 @@ | ||
| 137 | 146 | if ( empty( $query ) ) { |
| 138 | 147 | $query = $this->query; |
| 139 | 148 | } |
| 140 | 149 | |
| 150 | + $is_live_api = null; | |
| 141 | 151 | $headers = [ |
| 142 | - 'Content-Type' => 'application/json', | |
| 143 | - 'x-templately-ip' => Helper::get_ip(), | |
| 144 | - 'x-templately-url' => home_url( '/' ) | |
| 152 | + 'Content-Type' => 'application/json', | |
| 153 | + 'x-templately-ip' => Helper::get_ip(), | |
| 154 | + 'x-templately-url' => home_url( '/' ), | |
| 155 | + 'x-templately-version' => TEMPLATELY_VERSION, | |
| 145 | 156 | ]; |
| 146 | 157 | |
| 147 | 158 | if ( ! empty( $args['headers'] ) ) { |
| 148 | 159 | $headers = wp_parse_args( $args['headers'], $headers ); |
| @@ -148,8 +159,13 @@ | ||
| 148 | 159 | $headers = wp_parse_args( $args['headers'], $headers ); |
| 149 | 160 | unset( $args['headers'] ); |
| 150 | 161 | } |
| 151 | 162 | |
| 163 | + if ( ! empty( $args['is_live_api'] ) ) { | |
| 164 | + $is_live_api = $args['is_live_api']; | |
| 165 | + unset( $args['is_live_api'] ); | |
| 166 | + } | |
| 167 | + | |
| 152 | 168 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 153 | 169 | Helper::log( 'URL: ' . $this->url() ); |
| 154 | 170 | Helper::log( 'QUERY: ' . $query ); |
| 155 | 171 | } |
| @@ -165,17 +181,17 @@ | ||
| 165 | 181 | $retryCount = 0; |
| 166 | 182 | $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3; |
| 167 | 183 | $args = wp_parse_args( $args, $_default_args ); |
| 168 | 184 | do { |
| 169 | - $response = wp_remote_post( $this->url(), $args ); | |
| 185 | + $response = wp_remote_post( $this->url($is_live_api), $args ); | |
| 170 | 186 | $retryCount++; |
| 171 | 187 | } while ( is_wp_error( $response ) && $retryCount < $maxRetries ); |
| 172 | 188 | |
| 173 | 189 | if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { |
| 174 | 190 | Helper::log( 'Retry Count: ' . $retryCount ); |
| 175 | - Helper::log( 'RAW RESPONSE: ' ); | |
| 176 | - Helper::log( $response ); | |
| 177 | - Helper::log( 'END RAW RESPONSE' ); | |
| 191 | + // Helper::log( 'RAW RESPONSE: ' ); | |
| 192 | + // Helper::log( $response ); | |
| 193 | + // Helper::log( 'END RAW RESPONSE' ); | |
| 178 | 194 | } |
| 179 | 195 | |
| 180 | 196 | return $this->maybeErrors( $response, $args ); |
| 181 | 197 | } |
| @@ -180,9 +196,9 @@ | ||
| 180 | 196 | return $this->maybeErrors( $response, $args ); |
| 181 | 197 | } |
| 182 | 198 | |
| 183 | 199 | /** |
| 184 | - * Formating the self::post() response | |
| 200 | + * Formatting the self::post() response | |
| 185 | 201 | * |
| 186 | 202 | * @param mixed $response |
| 187 | 203 | * @param array $args |
| 188 | 204 | * @return mixed |
| @@ -191,13 +207,16 @@ | ||
| 191 | 207 | if ( $response instanceof WP_Error ) { |
| 192 | 208 | return $response; // Return WP_Error, if it is an error. |
| 193 | 209 | } |
| 194 | 210 | |
| 211 | + // Check for verification header before processing response body | |
| 212 | + Helper::check_verification_header( $response ); | |
| 213 | + | |
| 195 | 214 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 196 | 215 | $response_message = wp_remote_retrieve_response_message( $response ); |
| 197 | 216 | |
| 198 | 217 | /** |
| 199 | - * Retrive Data from Response Body. | |
| 218 | + * Retrieve Data from Response Body. | |
| 200 | 219 | */ |
| 201 | 220 | $response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 202 | 221 | /** |
| 203 | 222 | * If the graphql hit with any error. |
| @@ -220,12 +239,16 @@ | ||
| 220 | 239 | } ); |
| 221 | 240 | } |
| 222 | 241 | } ); |
| 223 | 242 | } else { |
| 243 | + $error_data = []; | |
| 244 | + if(!empty($error["extensions"]["statusText"])) { | |
| 245 | + $error_data["statusText"] = $error["extensions"]["statusText"]; | |
| 246 | + } | |
| 224 | 247 | if ( isset( $error['debugMessage'] ) ) { |
| 225 | 248 | $wp_error->add( 'templately_graphql_error', $error['debugMessage'] ); |
| 226 | 249 | } else { |
| 227 | - $wp_error->add( 'templately_graphql_error', $error['message'] ); | |
| 250 | + $wp_error->add( 'templately_graphql_error', $error['message'], $error_data ); | |
| 228 | 251 | } |
| 229 | 252 | } |
| 230 | 253 | } |
| 231 | 254 | } ); |