'application/json' ), bool $blocking = true, int $timeout = 10, bool $sslverify = true ) { if ( defined( 'PRODIGY_DEBUG_MODE' ) && PRODIGY_DEBUG_MODE ) { $sslverify = false; } $access_token = Prodigy_Cookies::get_cookie( Prodigy_User::ACCESS_COOKIE_NAME ); if ( ! empty( $access_token ) ) { $headers['x-Authorization-Customer'] = $access_token; } $this->request = compact( 'headers', 'blocking', 'timeout', 'sslverify' ); } /** * @param string $api_url * @param array $params * @param string $headers * * @return array|WP_Error */ public function post_remote_content( string $api_url, array $params = array(), string $headers = '' ) { $this->request['body'] = wp_json_encode( $params ); $this->request['method'] = 'POST'; $request = ! empty( $headers ) ? $headers : $this->request; $request['headers']['x-Authorization'] = get_option( Prodigy_Admin_Setup_Wizard::OPTION_STORE_KEY ); $start = microtime( true ); $response = wp_remote_post( $api_url, $request ); Prodigy_Prod_Logger::api_response( 'POST', $api_url, $response, (int) round( ( microtime( true ) - $start ) * 1000 ) ); return $response; } /** * @param string $api_url * @param array $params * @param string $headers * * @return array|WP_Error */ public function patch_remote_content( string $api_url, array $params = array(), string $headers = '' ) { $this->request['method'] = 'PATCH'; $this->request['body'] = wp_json_encode( $params ); $request = ! empty( $headers ) ? $headers : $this->request; $request['headers']['x-Authorization'] = get_option( Prodigy_Admin_Setup_Wizard::OPTION_STORE_KEY ); $start = microtime( true ); $response = wp_remote_request( $api_url, $request ); Prodigy_Prod_Logger::api_response( 'PATCH', $api_url, $response, (int) round( ( microtime( true ) - $start ) * 1000 ) ); return $response; } /** * @param string $api_url * @param array $headers * * @return array|WP_Error */ public function delete_remote_content( string $api_url, array $headers = array() ) { $this->request['method'] = 'DELETE'; if ( is_array( $headers ) ) { $this->request['headers'] += $headers; } $payload = array( 'wp_user_id' => get_current_user_id(), 'ts' => time(), 'request_id' => wp_generate_uuid4(), ); if ( defined( 'PRODIGY_DEBUG_MODE' ) && PRODIGY_DEBUG_MODE ) { $payload['trace_string'] = wp_json_encode( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 8 ) ); } $this->request['body'] = wp_json_encode( $payload ); $this->request['headers']['x-Authorization'] = get_option( Prodigy_Admin_Setup_Wizard::OPTION_STORE_KEY ); $start = microtime( true ); $response = wp_remote_request( $api_url, $this->request ); Prodigy_Prod_Logger::api_response( 'DELETE', $api_url, $response, (int) round( ( microtime( true ) - $start ) * 1000 ) ); return $response; } /** * @param string $api_url * @param array $params * @param string $headers * * @return array|\WP_Error */ public function put_remote_content( string $api_url, array $params = array(), string $headers = '' ) { $this->request['method'] = 'PUT'; $this->request['body'] = wp_json_encode( $params ); $request = ! empty( $headers ) ? $headers : $this->request; $request['headers']['x-Authorization'] = get_option( Prodigy_Admin_Setup_Wizard::OPTION_STORE_KEY ); $start = microtime( true ); $response = wp_remote_request( $api_url, $request ); Prodigy_Prod_Logger::api_response( 'PUT', $api_url, $response, (int) round( ( microtime( true ) - $start ) * 1000 ) ); return $response; } /** * @param string $api_url * @param string|null $headers * * @return array|WP_Error */ public function get_remote_content( string $api_url, ?string $headers = null ) { $request = $headers ?? array( 'headers' => $this->request['headers'] ); $request['headers']['x-Authorization'] = get_option( Prodigy_Admin_Setup_Wizard::OPTION_STORE_KEY ); $start = microtime( true ); $response = wp_remote_get( $api_url, $request ); Prodigy_Prod_Logger::api_response( 'GET', $api_url, $response, (int) round( ( microtime( true ) - $start ) * 1000 ) ); return $response; } }