/** * Helper function to query the marketplace API via wp_remote_request. * * @param string The url to access. * @param string The method of the request. * @param array The headers sent during the request. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return object The results of the wp_remote_request request. */ protected function remote_request($url = '', $args = array(), $allow_cache = true, $expiration = 300) { if (empty($url)) { return false; } $defaults = array('headers' => array(), 'method' => 'GET', 'body' => '', 'sslverify' => true); $this->count++; $args = wp_parse_args($args, $defaults); $args['headers']['Stream-Site-API-Key'] = $this->api_key; $args['headers']['Accept-Version'] = $this->api_version; $args['headers']['Content-Type'] = 'application/json'; if (WP_Stream::is_development_mode()) { $args['blocking'] = true; } add_filter('http_api_transports', array(__CLASS__, 'http_api_transport_priority'), 10, 3); $transient = 'wp_stream_' . md5($url); if ('GET' === $args['method'] && $allow_cache) { if (false === ($request = get_transient($transient))) { $request = wp_remote_request($url, $args); set_transient($transient, $request, $expiration); } } else { $request = wp_remote_request($url, $args); } remove_filter('http_api_transports', array(__CLASS__, 'http_api_transport_priority'), 10); // Return early if the request is non blocking if (isset($args['blocking']) && false === $args['blocking']) { return true; } if (!is_wp_error($request)) { $data = apply_filters('wp_stream_api_request_data', json_decode($request['body']), $url, $args); // Loose comparison needed if (200 == $request['response']['code'] || 201 == $request['response']['code']) { return $data; } else { // Disconnect if unauthorized or no longer exists, loose comparison needed if (403 == $request['response']['code'] || 410 == $request['response']['code']) { WP_Stream_Admin::remove_api_authentication(); } $this->errors['errors']['http_code'] = $request['response']['code']; } if (isset($data->error)) { $this->errors['errors']['api_error'] = $data->error; } } else { $this->errors['errors']['remote_request_error'] = $request->get_error_message(); WP_Stream::notice(sprintf('<strong>%s</strong> %s.', __('Stream API Error.', 'stream'), $this->errors['errors']['remote_request_error'])); } if (!empty($this->errors)) { delete_transient($transient); } return false; }
/** * Helper function to query the marketplace API via wp_remote_request. * * @param string The url to access. * @param string The method of the request. * @param array The headers sent during the request. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return object The results of the wp_remote_request request. */ protected function remote_request($url = '', $args = array(), $allow_cache = true, $expiration = 300) { if (empty($url) || empty(get_option('api_key'))) { return false; } // error_log("url: ".json_encode($url)); $defaults = array('headers' => array(), 'method' => 'GET', 'body' => '', 'sslverify' => true); $args = wp_parse_args($args, $defaults); $args['headers']['Stream-Site-API-Key'] = get_option('api_key'); $args['headers']['Content-Type'] = 'application/json'; add_filter('http_api_transports', array(__CLASS__, 'http_api_transport_priority'), 10, 3); $transient = 'wp_stream_' . md5($url); // error_log("args:".json_encode($args)); // error_log("url:".$url); if ('GET' === $args['method'] && $allow_cache) { if (false === ($request = get_transient($transient))) { $request = wp_remote_request($url, $args); set_transient($transient, $request, $expiration); } } else { $request = wp_remote_request($url, $args); } remove_filter('http_api_transports', array(__CLASS__, 'http_api_transport_priority'), 10); // Return early if the request is non blocking if (isset($args['blocking']) && false === $args['blocking']) { return true; } if (!is_wp_error($request)) { /** * Filter the request data of the API response. * * Does not fire on non-blocking requests. * * @since 2.0.0 * * @param string $url * @param array $args * * @return array */ $data = apply_filters('wp_stream_api_request_data', json_decode($request['body']), $url, $args); // Loose comparison needed if (200 == $request['response']['code'] || 201 == $request['response']['code']) { return $data; } else { // Disconnect if unauthorized or no longer exists, loose comparison needed if (403 == $request['response']['code'] || 410 == $request['response']['code']) { WP_Stream_Admin::remove_api_authentication(); } $this->errors['errors']['http_code'] = $request['response']['code']; } if (isset($data->error)) { $this->errors['errors']['api_error'] = $data->error; } } else { $this->errors['errors']['remote_request_error'] = $request->get_error_message(); wp_stream_get_instance()->admin->notice(sprintf('<strong>%s</strong> %s.', __('Stream API Error.', 'stream'), $this->errors['errors']['remote_request_error'])); } if (!empty($this->errors)) { delete_transient($transient); } return false; }