| @@ -1,10 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Templately\Utils; |
| 3 | 3 | |
| 4 | -use Templately\API\Login; | |
| 5 | 4 | use WP_Error; |
| 6 | 5 | |
| 6 | +use function wp_remote_retrieve_response_code; | |
| 7 | +use function wp_remote_retrieve_response_message; | |
| 8 | +use function wp_remote_retrieve_body; | |
| 9 | + | |
| 7 | 10 | class Http extends Base { |
| 8 | 11 | /** |
| 9 | 12 | * API Endpoint |
| 10 | 13 | * @var string |
| @@ -30,10 +33,10 @@ | ||
| 30 | 33 | |
| 31 | 34 | /** |
| 32 | 35 | * Setting the development mode. |
| 33 | 36 | */ |
| 34 | - public function __construct() { | |
| 35 | - $this->dev_mode = Helper::is_dev_api(); | |
| 37 | + public function __construct(){ | |
| 38 | + $this->dev_mode = defined('TEMPLATELY_DEV') && TEMPLATELY_DEV; | |
| 36 | 39 | } |
| 37 | 40 | |
| 38 | 41 | /** |
| 39 | 42 | * Determining the endpoint URL based on the mode. |
| @@ -40,87 +43,46 @@ | ||
| 40 | 43 | * |
| 41 | 44 | * @return string |
| 42 | 45 | */ |
| 43 | 46 | public function url() { |
| 44 | - if ( Helper::is_dev_api() ) { | |
| 47 | + if( $this->dev_mode ) { | |
| 45 | 48 | $this->url = 'https://app.templately.dev/api/plugin'; |
| 46 | 49 | } |
| 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 | - | |
| 56 | 50 | return $this->url; |
| 57 | 51 | } |
| 58 | 52 | |
| 59 | 53 | /** |
| 60 | - * Generate Google OAuth authentication URL | |
| 61 | - * | |
| 62 | - * @param string $redirect_to Optional redirect path after authentication | |
| 63 | - * @return string The Google auth URL with query parameters | |
| 64 | - */ | |
| 65 | -public function google_auth_url($redirect_to = '', $current_url = '') { | |
| 66 | - $base_url = $this->url(); | |
| 67 | - // Replace /api/plugin with /api/auth/plugin/google | |
| 68 | - $auth_url = str_replace('/api/plugin', '/api/auth/plugin/google', $base_url); | |
| 69 | - | |
| 70 | - // Get the referer to return to the exact same page we initiated login from securely | |
| 71 | - if ( ! empty( $current_url ) ) { | |
| 72 | - $referer = esc_url_raw( $current_url ); | |
| 73 | - } else { | |
| 74 | - $referer = isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; | |
| 54 | + * Preparing query arguments. | |
| 55 | + * Unknown. | |
| 56 | + * | |
| 57 | + * @return string | |
| 58 | + */ | |
| 59 | + public static function prepare( $query, ...$args ) { | |
| 60 | + return sprintf( $query, ...$args ); | |
| 75 | 61 | } |
| 76 | 62 | |
| 77 | - $return_url = wp_validate_redirect( $referer, '' ); | |
| 78 | - | |
| 79 | - if ( empty( $return_url ) ) { | |
| 80 | - $return_url = admin_url( 'admin.php?page=templately' ); | |
| 81 | - } | |
| 82 | - | |
| 83 | - $return_params = [ | |
| 84 | - 'templately_google_login' => '1', | |
| 85 | - ]; | |
| 86 | - | |
| 87 | - // Add redirect-to parameter if provided | |
| 88 | - if (!empty($redirect_to)) { | |
| 89 | - $return_params['redirect-to'] = $redirect_to; | |
| 90 | - } | |
| 91 | - | |
| 92 | - $site_url_with_params = add_query_arg($return_params, $return_url); | |
| 93 | - | |
| 94 | - $query_params = [ | |
| 95 | - 'site_url' => urlencode($site_url_with_params), | |
| 96 | - 'site_ip' => Helper::get_ip(), | |
| 97 | - 'state' => wp_generate_password(32, false) // Add unique random state for cache busting | |
| 98 | - ]; | |
| 99 | - | |
| 100 | - return add_query_arg($query_params, $auth_url); | |
| 101 | -} | |
| 102 | - | |
| 103 | -/** | |
| 63 | + /** | |
| 64 | + * Preparing query arguments | |
| 65 | + * | |
| 104 | 66 | * @param array $args |
| 105 | 67 | * @return string |
| 106 | 68 | */ |
| 107 | 69 | protected function prepareArgs( $args ) { |
| 108 | 70 | $prepareArgs = ""; |
| 109 | - foreach ( $args as $key => $value ) { | |
| 110 | - switch ( true ) { | |
| 71 | + foreach( $args as $key => $value ) { | |
| 72 | + switch( true ) { | |
| 111 | 73 | case is_int( $value ): |
| 112 | 74 | case is_bool( $value ): |
| 113 | - case is_string( $value ) && ( $value === 'true' || $value === 'false' ): | |
| 75 | + case is_string( $value ) && ($value === 'true' || $value === 'false'): | |
| 114 | 76 | $prepareArgs .= "$key:" . $value . ","; |
| 115 | 77 | break; |
| 116 | 78 | default: |
| 117 | - $prepareArgs .= "$key:" . '"' . Helper::esc_json_string( $value ) . '"' . ","; | |
| 79 | + $prepareArgs .= "$key:" . '"' . $value . '"' . ","; | |
| 118 | 80 | break; |
| 119 | 81 | } |
| 120 | 82 | } |
| 121 | 83 | |
| 122 | - return rtrim( $prepareArgs, ',' ); | |
| 84 | + return rtrim($prepareArgs, ','); | |
| 123 | 85 | } |
| 124 | 86 | |
| 125 | 87 | /** |
| 126 | 88 | * Preparing query for the endpoint. |
| @@ -132,21 +94,21 @@ | ||
| 132 | 94 | * @return Http |
| 133 | 95 | */ |
| 134 | 96 | public function query( $query_name, $params, $funcArgs = [], ...$args ) { |
| 135 | 97 | $query = '{'; |
| 136 | - $query .= $query_name; | |
| 137 | - if ( is_array( $funcArgs ) && ! empty( $funcArgs ) ) { | |
| 138 | - $query .= "(" . $this->prepareArgs( $funcArgs ) . ")"; | |
| 139 | - } | |
| 140 | - if ( ! empty( $params ) ) { | |
| 141 | - $query .= "{"; | |
| 142 | - $query .= $params; | |
| 143 | - $query .= "}"; | |
| 144 | - } | |
| 98 | + $query .= $query_name; | |
| 99 | + if( is_array( $funcArgs ) && ! empty( $funcArgs ) ) { | |
| 100 | + $query .= "(". $this->prepareArgs( $funcArgs ) .")"; | |
| 101 | + } | |
| 102 | + if( ! empty( $params ) ) { | |
| 103 | + $query .= "{"; | |
| 104 | + $query .= $params; | |
| 105 | + $query .= "}"; | |
| 106 | + } | |
| 145 | 107 | $query .= '}'; |
| 146 | 108 | |
| 147 | 109 | $this->endpoint = $query_name; |
| 148 | - $this->query = ! empty( $args ) ? sprintf( $query, ...$args ) : $query; | |
| 110 | + $this->query = ! empty( $args ) ? sprintf($query, ...$args) : $query; | |
| 149 | 111 | return $this; |
| 150 | 112 | } |
| 151 | 113 | |
| 152 | 114 | /** |
| @@ -162,9 +124,9 @@ | ||
| 162 | 124 | $this->query( $mutate, $params, $funcArgs, ...$args ); |
| 163 | 125 | $mutation = 'mutation'; |
| 164 | 126 | $mutation .= $this->query; |
| 165 | 127 | $this->endpoint = $mutate; |
| 166 | - $this->query = ! empty( $args ) ? sprintf( $mutation, ...$args ) : $mutation; | |
| 128 | + $this->query = ! empty( $args ) ? sprintf($mutation, ...$args) : $mutation; | |
| 167 | 129 | return $this; |
| 168 | 130 | } |
| 169 | 131 | |
| 170 | 132 | /** |
| @@ -173,58 +135,47 @@ | ||
| 173 | 135 | * @param string $query |
| 174 | 136 | * @param array $args |
| 175 | 137 | * @return mixed |
| 176 | 138 | */ |
| 177 | - public function post( $args = [] ) { | |
| 178 | - if ( empty( $query ) ) { | |
| 139 | + public function post( $query = '', $args = array() ){ | |
| 140 | + if( empty( $query ) ) { | |
| 179 | 141 | $query = $this->query; |
| 180 | 142 | } |
| 181 | 143 | |
| 182 | 144 | $headers = [ |
| 183 | - 'Content-Type' => 'application/json', | |
| 184 | - 'x-templately-ip' => Helper::get_ip(), | |
| 185 | - 'x-templately-url' => home_url( '/' ), | |
| 186 | - 'x-templately-version' => TEMPLATELY_VERSION, | |
| 145 | + 'Content-Type' => 'application/json', | |
| 187 | 146 | ]; |
| 188 | 147 | |
| 189 | - if ( ! empty( $args['headers'] ) ) { | |
| 148 | + if( ! empty( $args['headers'] ) ) { | |
| 190 | 149 | $headers = wp_parse_args( $args['headers'], $headers ); |
| 191 | 150 | unset( $args['headers'] ); |
| 192 | 151 | } |
| 193 | 152 | |
| 194 | - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 195 | - Helper::log( 'URL: ' . $this->url() ); | |
| 196 | - Helper::log( 'QUERY: ' . $query ); | |
| 153 | + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 154 | + Helper::log('URL: ' . $this->url()); | |
| 155 | + Helper::log('QUERY: ' . $query); | |
| 197 | 156 | } |
| 198 | 157 | |
| 199 | - $_default_args = [ | |
| 200 | - 'timeout' => $this->dev_mode ? 120 : 30, | |
| 158 | + $response = wp_remote_post( $this->url(), [ | |
| 159 | + 'timeout' => $this->dev_mode ? 40 : 15, | |
| 201 | 160 | 'headers' => $headers, |
| 202 | - 'body' => wp_json_encode( [ | |
| 161 | + 'body' => wp_json_encode([ | |
| 203 | 162 | 'query' => $query |
| 204 | - ] ) | |
| 205 | - ]; | |
| 163 | + ]) | |
| 164 | + ]); | |
| 206 | 165 | |
| 207 | - $retryCount = 0; | |
| 208 | - $maxRetries = defined('TEMPLATELY_HTTP_RETRY') ? TEMPLATELY_HTTP_RETRY : 3; | |
| 209 | - $args = wp_parse_args( $args, $_default_args ); | |
| 210 | - do { | |
| 211 | - $response = wp_remote_post( $this->url(), $args ); | |
| 212 | - $retryCount++; | |
| 213 | - } while ( is_wp_error( $response ) && $retryCount < $maxRetries ); | |
| 214 | 166 | |
| 215 | - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 216 | - Helper::log( 'Retry Count: ' . $retryCount ); | |
| 217 | - // Helper::log( 'RAW RESPONSE: ' ); | |
| 218 | - // Helper::log( $response ); | |
| 219 | - // Helper::log( 'END RAW RESPONSE' ); | |
| 220 | - } | |
| 167 | + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 168 | + Helper::log('RAW RESPONSE: '); | |
| 169 | + Helper::log( $response ); | |
| 170 | + Helper::log('END RAW RESPONSE'); | |
| 171 | + } | |
| 221 | 172 | |
| 222 | 173 | return $this->maybeErrors( $response, $args ); |
| 223 | 174 | } |
| 224 | 175 | |
| 225 | 176 | /** |
| 226 | - * Formatting the self::post() response | |
| 177 | + * Formating the self::post() response | |
| 227 | 178 | * |
| 228 | 179 | * @param mixed $response |
| 229 | 180 | * @param array $args |
| 230 | 181 | * @return mixed |
| @@ -229,100 +180,55 @@ | ||
| 229 | 180 | * @param array $args |
| 230 | 181 | * @return mixed |
| 231 | 182 | */ |
| 232 | 183 | private function maybeErrors( &$response, $args = [] ) { |
| 233 | - if ( $response instanceof WP_Error ) { | |
| 184 | + if( $response instanceof WP_Error ) { | |
| 234 | 185 | return $response; // Return WP_Error, if it is an error. |
| 235 | 186 | } |
| 236 | 187 | |
| 237 | - // Check for verification header before processing response body | |
| 238 | - Helper::check_verification_header( $response ); | |
| 239 | - Helper::check_site_disconnection( $response ); | |
| 188 | + $response_code = wp_remote_retrieve_response_code($response); | |
| 189 | + $response_message = wp_remote_retrieve_response_message($response); | |
| 240 | 190 | |
| 241 | - $response_code = wp_remote_retrieve_response_code( $response ); | |
| 242 | - $response_message = wp_remote_retrieve_response_message( $response ); | |
| 243 | - | |
| 244 | 191 | /** |
| 245 | - * Retrieve Data from Response Body. | |
| 192 | + * Retrive Data from Response Body. | |
| 246 | 193 | */ |
| 247 | 194 | $response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 248 | 195 | /** |
| 249 | 196 | * If the graphql hit with any error. |
| 250 | 197 | */ |
| 251 | - if ( ! empty( $response['errors'] ) ) { | |
| 252 | - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 253 | - Helper::log( 'ERROR: ' ); | |
| 198 | + if( ! empty( $response['errors'] ) ) { | |
| 199 | + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 200 | + Helper::log('ERROR: '); | |
| 254 | 201 | Helper::log( $response['errors'] ); |
| 255 | - Helper::log( 'END ERROR' ); | |
| 202 | + Helper::log('END ERROR'); | |
| 256 | 203 | } |
| 257 | - if ( is_array( $response['errors'] ) ) { | |
| 204 | + if( is_array( $response['errors'] ) ) { | |
| 258 | 205 | $wp_error = new WP_Error; |
| 259 | - array_walk( $response['errors'], function ( $error ) use ( &$wp_error ) { | |
| 260 | - if ( isset( $error['message'] ) ) { | |
| 261 | - if ( $error['message'] === 'validation' ) { | |
| 262 | - array_walk( $error['extensions'], function ( $_error, $_error_key ) use ( &$wp_error ) { | |
| 263 | - if ( $_error_key == 'validation' ) { | |
| 264 | - array_walk( $_error, function ( $v_error, $key ) use ( &$wp_error ) { | |
| 265 | - $wp_error->add( "{$key}_error", $v_error[0] ); | |
| 266 | - } ); | |
| 267 | - } | |
| 268 | - } ); | |
| 269 | - } else { | |
| 270 | - $error_data = []; | |
| 271 | - if(!empty($error["extensions"]["statusText"])) { | |
| 272 | - $error_data["statusText"] = $error["extensions"]["statusText"]; | |
| 273 | - } | |
| 274 | - if ( isset( $error['debugMessage'] ) ) { | |
| 275 | - $wp_error->add( 'templately_graphql_error', $error['debugMessage'] ); | |
| 276 | - } else { | |
| 277 | - $wp_error->add( 'templately_graphql_error', $error['message'], $error_data ); | |
| 278 | - } | |
| 279 | - } | |
| 206 | + array_walk( $response['errors'], function( $error ) use( &$wp_error ) { | |
| 207 | + if( isset( $error['message'] ) ) { | |
| 208 | + if( $error['message'] === 'validation' ) { | |
| 209 | + array_walk( $error['extensions'], function( $_error, $_error_key ) use( &$wp_error ) { | |
| 210 | + if( $_error_key == 'validation' ) { | |
| 211 | + array_walk( $_error, function( $v_error, $key ) use( &$wp_error ) { | |
| 212 | + $wp_error->add( "{$key}_error", ucwords($v_error[0]) ); | |
| 213 | + }); | |
| 214 | + } | |
| 215 | + }); | |
| 216 | + } else { | |
| 217 | + $wp_error->add( 'templately_graphql_error', $error['message'] ); | |
| 218 | + } | |
| 280 | 219 | } |
| 281 | - } ); | |
| 282 | - | |
| 283 | - if( $wp_error->get_error_code() === 'templately_graphql_error' ) { | |
| 284 | - if( $wp_error->get_error_message() == 'Unauthorized' ) { | |
| 285 | - $global_user = Login::get_instance()->delete(); | |
| 286 | - | |
| 287 | - return [ | |
| 288 | - 'redirect' => true, | |
| 289 | - 'url' => 'sign-in', | |
| 290 | - 'user' => $global_user, | |
| 291 | - ]; | |
| 292 | - } | |
| 293 | - } | |
| 294 | - | |
| 220 | + }); | |
| 295 | 221 | return $wp_error; |
| 296 | 222 | } |
| 297 | - } elseif ( ! empty( $response['status'] ) && $response['status'] === 'error' ) { | |
| 298 | - | |
| 299 | - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 300 | - Helper::log( 'ERROR: ' ); | |
| 301 | - Helper::log( $response ); | |
| 302 | - Helper::log( 'END ERROR' ); | |
| 303 | - } | |
| 304 | - | |
| 305 | - $error_data = []; | |
| 306 | - if ( ! empty( $response['statusText'] ) ) { | |
| 307 | - $error_data['statusText'] = $response['statusText']; | |
| 308 | - } | |
| 309 | - | |
| 310 | - $error_message = ! empty( $response['message'] ) ? $response['message'] : __( 'Unknown error occurred', 'templately' ); | |
| 311 | - | |
| 312 | - return new WP_Error( 'templately_api_error', $error_message, $error_data ); | |
| 313 | 223 | } |
| 314 | 224 | |
| 315 | - $_response = isset( $response['data'][$this->endpoint] ) ? $response['data'][$this->endpoint] : []; | |
| 316 | - // {"data":{"connectWithApiKey":{"status":"error","message":"Invalid API key.","user":null}}} | |
| 317 | - if ( ! empty( $response['status'] ) && $response['status'] === 'error' ) { | |
| 225 | + $_response = ! empty( $response['data'][ $this->endpoint ] ) ? $response['data'][ $this->endpoint ] : []; | |
| 318 | 226 | |
| 319 | - } | |
| 320 | - | |
| 321 | - if ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 322 | - Helper::log( 'RESPONSE: ' ); | |
| 227 | + if( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ) { | |
| 228 | + Helper::log('RESPONSE: '); | |
| 323 | 229 | Helper::log( $_response ); |
| 324 | - Helper::log( 'END RESPONSE' ); | |
| 230 | + Helper::log('END RESPONSE'); | |
| 325 | 231 | } |
| 326 | 232 | |
| 327 | 233 | return $_response; |
| 328 | 234 | } |