: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* API request class: Request
* Handles all the internal stuff to form and process a proper API request.
* @package Smush\Core\Api
namespace Smush\Core\Api;
if ( ! defined( 'WPINC' ) ) {
private $headers = array();
private $post_args = array();
private $get_args = array();
* @param Abstract_API $service API service.
* @throws Exception Init exception.
public function __construct( $service ) {
if ( ! $service instanceof Abstract_API ) {
throw new Exception( __( 'Invalid API service.', 'wp-smushit' ), 404 );
$this->service = $service;
* Get the current site URL.
* The network_site_url() of the WP installation. (Or network_home_url if not passing an API key).
public function get_this_site() {
if ( defined( 'WP_SMUSH_API_DOMAIN' ) && WP_SMUSH_API_DOMAIN ) {
return WP_SMUSH_API_DOMAIN;
return network_site_url();
* @param int $timeout Request timeout (seconds).
public function set_timeout( $timeout ) {
$this->timeout = $timeout;
* Add a new request argument for POST requests.
* @param string $name Argument name.
* @param string $value Argument value.
public function add_post_argument( $name, $value ) {
$this->post_args[ $name ] = $value;
* Add a new request argument for GET requests.
* @param string $name Argument name.
* @param string $value Argument value.
public function add_get_argument( $name, $value ) {
$this->get_args[ $name ] = $value;
* Add a new request argument for GET requests.
* @param string $name Argument name.
* @param string $value Argument value.
public function add_header_argument( $name, $value ) {
$this->headers[ $name ] = $value;
* @param string $path Endpoint route.
* @param array $data Data array.
public function post( $path, $data = array() ) {
$result = $this->request( $path, $data );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
* @param string $path Endpoint route.
* @param array $data Data array.
public function get( $path, $data = array() ) {
$result = $this->request( $path, $data, 'get' );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
* @param string $path Endpoint route.
* @param array $data Data array.
public function head( $path, $data = array() ) {
$result = $this->request( $path, $data, 'head' );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
* @param string $path Endpoint route.
* @param array $data Data array.
public function patch( $path, $data = array() ) {
$result = $this->request( $path, $data, 'patch' );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
* Make a DELETE API call.
* @param string $path Endpoint route.
* @param array $data Data array.
public function delete( $path, $data = array() ) {
$result = $this->request( $path, $data, 'delete' );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
* Get API endpoint URL for request.
* @param string $path Endpoint path.
private function get_api_url( $path = '' ) {
$base = defined( 'WPMUDEV_CUSTOM_API_SERVER' ) && WPMUDEV_CUSTOM_API_SERVER
? WPMUDEV_CUSTOM_API_SERVER
: 'https://wpmudev.com/';
$url = "$base/api/{$this->service->name}/{$this->service->version}/";
$url = trailingslashit( $url . $path );
* Add authorization header.
private function sign_request() {
if ( ! empty( $this->service->api_key ) ) {
$this->add_header_argument( 'Authorization', 'Basic ' . $this->service->api_key );
* @param string $path API endpoint route.
* @param array $data Data array.
* @param string $method API method.
private function request( $path, $data = array(), $method = 'post' ) {
$url = $this->get_api_url( $path );
$url = add_query_arg( $this->get_args, $url );
if ( 'post' !== $method && 'patch' !== $method && 'delete' !== $method ) {
$url = add_query_arg( $data, $url );
'user-agent' => WP_SMUSH_UA,
'headers' => $this->headers,
'method' => strtoupper( $method ),
'timeout' => $this->timeout,
if ( ! $args['timeout'] || 2 === $args['timeout'] ) {
$args['blocking'] = false;
switch ( strtolower( $method ) ) {
if ( is_array( $data ) ) {
$args['body'] = array_merge( $data, $this->post_args );
$response = wp_remote_post( $url, $args );
$response = wp_remote_head( $url, $args );
$response = wp_remote_get( $url, $args );
$response = wp_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
Helper::logger()->api()->error( sprintf( 'Error [%s->%s]: %s', $method, $path, $response->get_error_message() ) );