| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* This class handles a post request being send to a given endpoint. |
| 10 |
*/ |
| 11 |
class WPSEO_Remote_Request { |
| 12 |
|
| 13 |
/** |
| 14 |
* Holds the post method. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
public const METHOD_POST = 'post'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Holds the get method. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
public const METHOD_GET = 'get'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Holds the endpoint to send the request to. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $endpoint = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* Holds the arguments to use in this request. |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
protected $args = [ |
| 40 |
'blocking' => false, |
| 41 |
'timeout' => 2, |
| 42 |
]; |
| 43 |
|
| 44 |
/** |
| 45 |
* Holds the response error. |
| 46 |
* |
| 47 |
* @var WP_Error|null |
| 48 |
*/ |
| 49 |
protected $response_error; |
| 50 |
|
| 51 |
/** |
| 52 |
* Holds the response body. |
| 53 |
* |
| 54 |
* @var mixed |
| 55 |
*/ |
| 56 |
protected $response_body; |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the endpoint and arguments. |
| 60 |
* |
| 61 |
* @param string $endpoint The endpoint to send the request to. |
| 62 |
* @param array $args The arguments to use in this request. |
| 63 |
*/ |
| 64 |
public function __construct( $endpoint, array $args = [] ) { |
| 65 |
$this->endpoint = $endpoint; |
| 66 |
$this->args = wp_parse_args( $this->args, $args ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Sets the request body. |
| 71 |
* |
| 72 |
* @param mixed $body The body to set. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function set_body( $body ) { |
| 77 |
$this->args['body'] = $body; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Sends the data to the given endpoint. |
| 82 |
* |
| 83 |
* @param string $method The type of request to send. |
| 84 |
* |
| 85 |
* @return bool True when sending data has been successful. |
| 86 |
*/ |
| 87 |
public function send( $method = self::METHOD_POST ) { |
| 88 |
switch ( $method ) { |
| 89 |
case self::METHOD_POST: |
| 90 |
$response = $this->post(); |
| 91 |
break; |
| 92 |
case self::METHOD_GET: |
| 93 |
$response = $this->get(); |
| 94 |
break; |
| 95 |
default: |
| 96 |
/* translators: %1$s expands to the request method */ |
| 97 |
$response = new WP_Error( 1, sprintf( __( 'Request method %1$s is not valid.', 'wordpress-seo' ), $method ) ); |
| 98 |
break; |
| 99 |
} |
| 100 |
|
| 101 |
return $this->process_response( $response ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns the value of the response error. |
| 106 |
* |
| 107 |
* @return WP_Error|null The response error. |
| 108 |
*/ |
| 109 |
public function get_response_error() { |
| 110 |
return $this->response_error; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns the response body. |
| 115 |
* |
| 116 |
* @return mixed The response body. |
| 117 |
*/ |
| 118 |
public function get_response_body() { |
| 119 |
return $this->response_body; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Processes the given response. |
| 124 |
* |
| 125 |
* @param mixed $response The response to process. |
| 126 |
* |
| 127 |
* @return bool True when response is valid. |
| 128 |
*/ |
| 129 |
protected function process_response( $response ) { |
| 130 |
if ( $response instanceof WP_Error ) { |
| 131 |
$this->response_error = $response; |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
$this->response_body = wp_remote_retrieve_body( $response ); |
| 137 |
|
| 138 |
return ( wp_remote_retrieve_response_code( $response ) === 200 ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Performs a post request to the specified endpoint with set arguments. |
| 143 |
* |
| 144 |
* @return WP_Error|array The response or WP_Error on failure. |
| 145 |
*/ |
| 146 |
protected function post() { |
| 147 |
return wp_remote_post( $this->endpoint, $this->args ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Performs a post request to the specified endpoint with set arguments. |
| 152 |
* |
| 153 |
* @return WP_Error|array The response or WP_Error on failure. |
| 154 |
*/ |
| 155 |
protected function get() { |
| 156 |
return wp_remote_get( $this->endpoint, $this->args ); |
| 157 |
} |
| 158 |
} |
| 159 |
|