*/ final class Api { /** * The base uri of the api. * * @since 2.0.0 * @access protected * @var array $base_uris list of base uri. */ private $base_uris = array(); /** * Initialize the class and set its properties. * * @since 2.0.0 */ public function __construct() { $this->base_uris = array( 'api' => ACTIRISE_URL_API, 'flashbid' => ACTIRISE_URL_FLASHBID, ); } /** * Get the list of ad networks. * * @since 2.0.0 * @param string $key_uri The key of the base uri. * @param string $uri The uri. * @param array $args The arguments. * @return mixed|array|WP_Error The list of ad networks. */ public function get( $key_uri, $uri, $args = array() ) { $base_uri = $this->get_base_uri( $key_uri ); if ( $base_uri === '' ) { return new WP_Error( 500, 'Invalid base uri for' . $key_uri ); } return $this->request( 'GET', $base_uri, $uri, $args ); } /** * Post to Actirise API * * @since 2.0.0 * @param string $key_uri The key of the base uri. * @param string $uri The uri. * @param array $args The arguments. * @return mixed|array|WP_Error The response. */ public function post( $key_uri, $uri, $args = array() ) { $base_uri = $this->get_base_uri( $key_uri ); if ( $base_uri === '' ) { return new WP_Error( 500, 'Invalid base uri for' . $key_uri ); } return $this->request( 'POST', $base_uri, $uri, $args ); } /** * Delete to Actirise API * * @since 2.0.0 * @param string $key_uri The key of the base uri. * @param string $uri The uri. * @param array $args The arguments. * @return mixed|array|WP_Error The response. */ public function delete( $key_uri, $uri, $args = array() ) { $base_uri = $this->get_base_uri( $key_uri ); if ( $base_uri === '' ) { return new WP_Error( 500, 'Invalid base uri for' . $key_uri ); } return $this->request( 'DELETE', $base_uri, $uri, $args ); } /** * Get base url in array * * @since 2.0.0 * @param string $key The key of the base uri. * @return string The base uri. */ private function get_base_uri( $key ) { return isset( $this->base_uris[ $key ] ) ? $this->base_uris[ $key ] : ''; } /** * Request to Actirise API * * @since 2.0.0 * @param string $type The type of request. * @param string $base_uri The base uri. * @param string $uri The uri. * @param array $args The arguments. * @return mixed|array|WP_Error The response. */ private function request( $type, $base_uri, $uri, $args ) { $url = $base_uri . $uri; if ( 'GET' === $type ) { $url = add_query_arg( $args, $url ); } $options_request = array( 'method' => $type, 'timeout' => 10, 'sslverify' => true, ); if ( 'POST' === $type || 'DELETE' === $type ) { $options_request['body'] = wp_json_encode( $args ); } $response = wp_remote_request( $url, $options_request ); if ( is_wp_error( $response ) ) { return $response; } $response_code = wp_remote_retrieve_response_code( $response ); if ( $response_code !== 200 ) { return new WP_Error( $response_code, 'EAC' ); } $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) ) { return new WP_Error( 500, 'Empty body' ); } $data = json_decode( $body, true ); if ( is_null( $data ) ) { return new WP_Error( 500, 'Invalid json' ); } return $data; } }