| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woody Request class |
| 4 |
* |
| 5 |
* Contains methods for executing requests and processing responses. |
| 6 |
* Uses the WINP\JsonMapper\Mapper to convert the response to a convenient object. |
| 7 |
* |
| 8 |
* @author Webcraftic <wordpress.webraftic@gmail.com> |
| 9 |
* @copyright (c) 11.12.2018, Webcraftic |
| 10 |
* @version 1.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly |
| 14 |
use WpOrg\Requests\Requests; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
class WINP_Request { |
| 21 |
//В новы� |
| 22 |
версия� |
| 23 |
Вуди начиная с 2.2.10 будет обращаться к новой версии API библиотеки сниппетов |
| 24 |
// это делается для обратной совместимости, чтобы старые версии продолжили работать со старым API |
| 25 |
//const WINP_REQUEST_URL = 'http://185.75.88.217/v2/woody/'; //тестовая после переноса на другой сервер |
| 26 |
const WINP_REQUEST_URL = 'https://api.woodysnippet.com/v2/woody/'; |
| 27 |
|
| 28 |
/** |
| 29 |
* WINP_REQUEST constructor. |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
require_once WINP_PLUGIN_DIR . '/includes/jsonmapper/class-json-mapper.php'; |
| 33 |
require_once WINP_PLUGIN_DIR . '/includes/jsonmapper/exceptions/class-exception.php'; |
| 34 |
|
| 35 |
/*add_filter( 'http_request_args', function ( $parsed_args, $url ) { |
| 36 |
$parsed_args['sslverify'] = false; |
| 37 |
|
| 38 |
return $parsed_args; |
| 39 |
}, 10, 2 );*/ |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get license key |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
private function get_key() { |
| 48 |
return WINP_Plugin::app()->premium->get_license()->get_key(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get license plugin_id |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
private function get_plugin_id() { |
| 57 |
return WINP_Plugin::app()->premium->get_setting( 'plugin_id' ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get base64 token string |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
private function get_token() { |
| 66 |
return base64_encode( $this->get_key() ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get headers |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
private function get_headers() { |
| 75 |
return [ |
| 76 |
'Authorization' => 'Bearer ' . $this->get_token(), |
| 77 |
'PluginId' => $this->get_plugin_id(), |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Check is key data available |
| 83 |
* |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
public function is_key() { |
| 87 |
return WINP_Plugin::app()->premium->is_activate() && $this->get_key(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Make POST request with authorization headers and return response |
| 92 |
* |
| 93 |
* @param string $point |
| 94 |
* @param array $args |
| 95 |
* |
| 96 |
* @return array|bool|WP_Error |
| 97 |
*/ |
| 98 |
public function post( $point, $args = [] ) { |
| 99 |
if ( ! $this->is_key() ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
$args['headers'] = $this->get_headers(); |
| 104 |
|
| 105 |
return wp_remote_post( self::WINP_REQUEST_URL . $point, $args ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Make GET request with authorization headers and return response |
| 110 |
* |
| 111 |
* @param string $point |
| 112 |
* @param array $args |
| 113 |
* |
| 114 |
* @return array|bool|WP_Error |
| 115 |
*/ |
| 116 |
public function get( $point, $args = [] ) { |
| 117 |
if ( ! $this->is_key() ) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
$args['headers'] = $this->get_headers(); |
| 122 |
|
| 123 |
return wp_remote_get( self::WINP_REQUEST_URL . $point, $args ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Make PUT request with authorization headers and return response |
| 128 |
* |
| 129 |
* @param string $point |
| 130 |
* @param array $args |
| 131 |
* |
| 132 |
* @return array|bool|WP_Error |
| 133 |
*/ |
| 134 |
public function put( $point, $args = [] ) { |
| 135 |
if ( ! $this->is_key() ) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
$args['method'] = Requests::PUT; |
| 140 |
$args['headers'] = $this->get_headers(); |
| 141 |
|
| 142 |
return wp_remote_request( self::WINP_REQUEST_URL . $point, $args ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Check response |
| 147 |
* |
| 148 |
* @param $response |
| 149 |
* |
| 150 |
* @return bool |
| 151 |
*/ |
| 152 |
public function check_response( $response ) { |
| 153 |
if ( empty( $response ) || $response instanceof WP_Error ) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
if ( ! isset( $response['body'] ) || empty( $response['body'] ) ) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
if ( 200 != $response['response']['code'] && 201 != $response['response']['code'] ) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Check body |
| 170 |
* |
| 171 |
* @param $body |
| 172 |
* |
| 173 |
* @return bool |
| 174 |
*/ |
| 175 |
public function check_body( $body ) { |
| 176 |
if ( empty( $body ) ) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
if ( ! is_array( $body ) && ! is_object( $body ) ) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get response text error |
| 189 |
* |
| 190 |
* @param $response |
| 191 |
* |
| 192 |
* @return string |
| 193 |
*/ |
| 194 |
public function get_response_error( $response ) { |
| 195 |
if ( empty( $response ) ) { |
| 196 |
return 'Empty response'; |
| 197 |
} |
| 198 |
|
| 199 |
if ( $response instanceof WP_Error ) { |
| 200 |
return $response->get_error_message(); |
| 201 |
} |
| 202 |
|
| 203 |
if ( is_array( $response ) ) { |
| 204 |
if ( ! isset( $response['body'] ) || empty( $response['body'] ) ) { |
| 205 |
return 'Empty body'; |
| 206 |
} |
| 207 |
|
| 208 |
if ( 200 != $response['response']['code'] && 201 != $response['response']['code'] ) { |
| 209 |
return $response['response']['message'] . ' [Code: ' . $response['response']['code'] . ']'; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return 'Unknown error'; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get mapped object by name |
| 218 |
* |
| 219 |
* @param $json |
| 220 |
* @param $object_name |
| 221 |
* |
| 222 |
* @return bool|mixed |
| 223 |
*/ |
| 224 |
public function map_object( $json, $object_name ) { |
| 225 |
if ( ! $this->check_response( $json ) ) { |
| 226 |
error_log( 'Snippet api [map_object]: ' . $this->get_response_error( $json ) ); |
| 227 |
|
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
$body = json_decode( $json['body'] ); |
| 232 |
|
| 233 |
if ( ! $this->check_body( $body ) ) { |
| 234 |
error_log( 'Snippet api [map_objects]: Wrong body' ); |
| 235 |
|
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
$mapper = new WINP\JsonMapper\Mapper(); |
| 240 |
|
| 241 |
$mapper->bExceptionOnUndefinedProperty = true; |
| 242 |
$mapper->bExceptionOnMissingData = true; |
| 243 |
|
| 244 |
try { |
| 245 |
return $mapper->map( $body, new $object_name() ); |
| 246 |
} catch ( WINP\JsonMapper\Exception $exception ) { |
| 247 |
error_log( 'Snippet api [map_object]: ' . $exception->getMessage() ); |
| 248 |
|
| 249 |
return false; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Get mapped objects by name |
| 255 |
* |
| 256 |
* @param $json |
| 257 |
* @param $object_name |
| 258 |
* |
| 259 |
* @return bool|mixed |
| 260 |
*/ |
| 261 |
public function map_objects( $json, $object_name ) { |
| 262 |
if ( ! $this->check_response( $json ) ) { |
| 263 |
error_log( 'Snippet api [map_objects]: ' . $this->get_response_error( $json ) ); |
| 264 |
|
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
$body = json_decode( $json['body'] ); |
| 269 |
|
| 270 |
if ( ! $this->check_body( $body ) ) { |
| 271 |
error_log( 'Snippet api [map_objects]: Wrong body' ); |
| 272 |
|
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
$mapper = new WINP\JsonMapper\Mapper(); |
| 277 |
|
| 278 |
$mapper->bExceptionOnUndefinedProperty = true; |
| 279 |
$mapper->bExceptionOnMissingData = true; |
| 280 |
|
| 281 |
try { |
| 282 |
return $mapper->mapArray( $body, [], $object_name ); |
| 283 |
} catch ( WINP\JsonMapper\Exception $exception ) { |
| 284 |
error_log( 'Snippet api [map_objects]: ' . $exception->getMessage() ); |
| 285 |
|
| 286 |
return false; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
} |
| 291 |
|