| @@ -4,11 +4,9 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * Contains methods for executing requests and processing responses. |
| 6 | 6 | * Uses the WINP\JsonMapper\Mapper to convert the response to a convenient object. |
| 7 | 7 | * |
| 8 | - * @author Webcraftic <wordpress.webraftic@gmail.com> | |
| 9 | - * @copyright (c) 11.12.2018, Webcraftic | |
| 10 | - * @version 1.0 | |
| 8 | + * @package Woody_Code_Snippets | |
| 11 | 9 | */ |
| 12 | 10 | |
| 13 | 11 | // Exit if accessed directly |
| 14 | 12 | use WpOrg\Requests\Requests; |
| @@ -16,12 +14,16 @@ | ||
| 16 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | 15 | exit; |
| 18 | 16 | } |
| 19 | 17 | |
| 18 | +/** | |
| 19 | + * WINP_Request class | |
| 20 | + */ | |
| 20 | 21 | class WINP_Request { |
| 21 | - //В новых версиях Вуди начиная с 2.2.10 будет обращаться к новой версии API библиотеки сниппетов | |
| 22 | - // это делается для обратной совместимости, чтобы старые версии продолжили работать со старым API | |
| 23 | - //const WINP_REQUEST_URL = 'http://185.75.88.217/v2/woody/'; //тестовая после переноса на другой сервер | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Base request URL. | |
| 25 | + */ | |
| 24 | 26 | const WINP_REQUEST_URL = 'https://api.woodysnippet.com/v2/woody/'; |
| 25 | 27 | |
| 26 | 28 | /** |
| 27 | 29 | * WINP_REQUEST constructor. |
| @@ -26,16 +28,12 @@ | ||
| 26 | 28 | /** |
| 27 | 29 | * WINP_REQUEST constructor. |
| 28 | 30 | */ |
| 29 | 31 | public function __construct() { |
| 30 | - require_once WINP_PLUGIN_DIR . '/includes/jsonmapper/class-json-mapper.php'; | |
| 31 | - require_once WINP_PLUGIN_DIR . '/includes/jsonmapper/exceptions/class-exception.php'; | |
| 32 | - | |
| 33 | - /*add_filter( 'http_request_args', function ( $parsed_args, $url ) { | |
| 34 | - $parsed_args['sslverify'] = false; | |
| 35 | - | |
| 36 | - return $parsed_args; | |
| 37 | - }, 10, 2 );*/ | |
| 32 | + // Load GPL-compatible DTO classes. | |
| 33 | + require_once WINP_PLUGIN_DIR . '/admin/includes/dto/class-dto-base.php'; | |
| 34 | + require_once WINP_PLUGIN_DIR . '/admin/includes/dto/class-type.php'; | |
| 35 | + require_once WINP_PLUGIN_DIR . '/admin/includes/dto/class-snippet.php'; | |
| 38 | 36 | } |
| 39 | 37 | |
| 40 | 38 | /** |
| 41 | 39 | * Get license key |
| @@ -42,9 +40,9 @@ | ||
| 42 | 40 | * |
| 43 | 41 | * @return string |
| 44 | 42 | */ |
| 45 | 43 | private function get_key() { |
| 46 | - return WINP_Plugin::app()->premium->get_license()->get_key(); | |
| 44 | + return WINP_Plugin::app()->premium->get_key(); | |
| 47 | 45 | } |
| 48 | 46 | |
| 49 | 47 | /** |
| 50 | 48 | * Get license plugin_id |
| @@ -72,8 +70,9 @@ | ||
| 72 | 70 | private function get_headers() { |
| 73 | 71 | return [ |
| 74 | 72 | 'Authorization' => 'Bearer ' . $this->get_token(), |
| 75 | 73 | 'PluginId' => $this->get_plugin_id(), |
| 74 | + 'Version' => 'v2', // That tells server that the user is using SDK, not Freemius. | |
| 76 | 75 | ]; |
| 77 | 76 | } |
| 78 | 77 | |
| 79 | 78 | /** |
| @@ -81,9 +80,9 @@ | ||
| 81 | 80 | * |
| 82 | 81 | * @return bool |
| 83 | 82 | */ |
| 84 | 83 | public function is_key() { |
| 85 | - return WINP_Plugin::app()->premium->is_activate() && $this->get_key(); | |
| 84 | + return WINP_Plugin::app()->premium->is_active() && $this->get_key(); | |
| 86 | 85 | } |
| 87 | 86 | |
| 88 | 87 | /** |
| 89 | 88 | * Make POST request with authorization headers and return response |
| @@ -88,9 +87,9 @@ | ||
| 88 | 87 | /** |
| 89 | 88 | * Make POST request with authorization headers and return response |
| 90 | 89 | * |
| 91 | 90 | * @param string $point |
| 92 | - * @param array $args | |
| 91 | + * @param array $args | |
| 93 | 92 | * |
| 94 | 93 | * @return array|bool|WP_Error |
| 95 | 94 | */ |
| 96 | 95 | public function post( $point, $args = [] ) { |
| @@ -106,18 +105,24 @@ | ||
| 106 | 105 | /** |
| 107 | 106 | * Make GET request with authorization headers and return response |
| 108 | 107 | * |
| 109 | 108 | * @param string $point |
| 110 | - * @param array $args | |
| 109 | + * @param array $args | |
| 111 | 110 | * |
| 112 | 111 | * @return array|bool|WP_Error |
| 113 | 112 | */ |
| 114 | 113 | public function get( $point, $args = [] ) { |
| 115 | - if ( ! $this->is_key() ) { | |
| 114 | + // Allow common endpoints without authentication. | |
| 115 | + $is_common_endpoint = strpos( $point, 'common' ) === 0; | |
| 116 | + | |
| 117 | + if ( ! $is_common_endpoint && ! $this->is_key() ) { | |
| 116 | 118 | return false; |
| 117 | 119 | } |
| 118 | 120 | |
| 119 | - $args['headers'] = $this->get_headers(); | |
| 121 | + // Add headers if user has valid license (even for common endpoints). | |
| 122 | + if ( $this->is_key() ) { | |
| 123 | + $args['headers'] = $this->get_headers(); | |
| 124 | + } | |
| 120 | 125 | |
| 121 | 126 | return wp_remote_get( self::WINP_REQUEST_URL . $point, $args ); |
| 122 | 127 | } |
| 123 | 128 | |
| @@ -124,9 +129,9 @@ | ||
| 124 | 129 | /** |
| 125 | 130 | * Make PUT request with authorization headers and return response |
| 126 | 131 | * |
| 127 | 132 | * @param string $point |
| 128 | - * @param array $args | |
| 133 | + * @param array $args | |
| 129 | 134 | * |
| 130 | 135 | * @return array|bool|WP_Error |
| 131 | 136 | */ |
| 132 | 137 | public function put( $point, $args = [] ) { |
| @@ -213,38 +218,36 @@ | ||
| 213 | 218 | |
| 214 | 219 | /** |
| 215 | 220 | * Get mapped object by name |
| 216 | 221 | * |
| 217 | - * @param $json | |
| 218 | - * @param $object_name | |
| 222 | + * @param array<string, mixed>|bool|WP_Error $json Response array from wp_remote_* functions. | |
| 223 | + * @param string $object_name Class name (e.g., 'WINP_DTO_Snippet'). | |
| 219 | 224 | * |
| 220 | - * @return bool|mixed | |
| 225 | + * @return object|bool Object instance or false on failure. | |
| 226 | + * @throws Exception If mapping fails. | |
| 221 | 227 | */ |
| 222 | 228 | public function map_object( $json, $object_name ) { |
| 223 | 229 | if ( ! $this->check_response( $json ) ) { |
| 224 | - error_log( 'Snippet api [map_object]: ' . $this->get_response_error( $json ) ); | |
| 230 | + return false; | |
| 231 | + } | |
| 225 | 232 | |
| 233 | + if ( is_wp_error( $json ) || ! isset( $json['body'] ) ) { | |
| 226 | 234 | return false; |
| 227 | 235 | } |
| 228 | 236 | |
| 229 | - $body = json_decode( $json['body'] ); | |
| 237 | + $body = json_decode( $json['body'], true ); | |
| 230 | 238 | |
| 231 | 239 | if ( ! $this->check_body( $body ) ) { |
| 232 | - error_log( 'Snippet api [map_objects]: Wrong body' ); | |
| 233 | - | |
| 234 | 240 | return false; |
| 235 | 241 | } |
| 236 | 242 | |
| 237 | - $mapper = new WINP\JsonMapper\Mapper(); | |
| 238 | - | |
| 239 | - $mapper->bExceptionOnUndefinedProperty = true; | |
| 240 | - $mapper->bExceptionOnMissingData = true; | |
| 241 | - | |
| 242 | 243 | try { |
| 243 | - return $mapper->map( $body, new $object_name() ); | |
| 244 | - } catch ( WINP\JsonMapper\Exception $exception ) { | |
| 245 | - error_log( 'Snippet api [map_object]: ' . $exception->getMessage() ); | |
| 244 | + if ( ! method_exists( $object_name, 'from_array' ) ) { | |
| 245 | + throw new Exception( "Class {$object_name} does not have a from_array method" ); | |
| 246 | + } | |
| 246 | 247 | |
| 248 | + return $object_name::from_array( $body ); | |
| 249 | + } catch ( Exception $exception ) { | |
| 247 | 250 | return false; |
| 248 | 251 | } |
| 249 | 252 | } |
| 250 | 253 | |
| @@ -250,39 +253,36 @@ | ||
| 250 | 253 | |
| 251 | 254 | /** |
| 252 | 255 | * Get mapped objects by name |
| 253 | 256 | * |
| 254 | - * @param $json | |
| 255 | - * @param $object_name | |
| 257 | + * @param array<string, mixed>|bool|WP_Error $json Response array from wp_remote_* functions. | |
| 258 | + * @param string $object_name Class name (e.g., 'WINP_DTO_Snippet'). | |
| 256 | 259 | * |
| 257 | - * @return bool|mixed | |
| 260 | + * @return array<object>|bool Array of object instances or false on failure. | |
| 261 | + * @throws Exception If mapping fails. | |
| 258 | 262 | */ |
| 259 | 263 | public function map_objects( $json, $object_name ) { |
| 260 | 264 | if ( ! $this->check_response( $json ) ) { |
| 261 | - error_log( 'Snippet api [map_objects]: ' . $this->get_response_error( $json ) ); | |
| 265 | + return false; | |
| 266 | + } | |
| 262 | 267 | |
| 268 | + if ( is_wp_error( $json ) || ! isset( $json['body'] ) ) { | |
| 263 | 269 | return false; |
| 264 | 270 | } |
| 265 | 271 | |
| 266 | - $body = json_decode( $json['body'] ); | |
| 272 | + $body = json_decode( $json['body'], true ); | |
| 267 | 273 | |
| 268 | - if ( ! $this->check_body( $body ) ) { | |
| 269 | - error_log( 'Snippet api [map_objects]: Wrong body' ); | |
| 270 | - | |
| 274 | + if ( ! is_array( $body ) ) { | |
| 271 | 275 | return false; |
| 272 | 276 | } |
| 273 | 277 | |
| 274 | - $mapper = new WINP\JsonMapper\Mapper(); | |
| 275 | - | |
| 276 | - $mapper->bExceptionOnUndefinedProperty = true; | |
| 277 | - $mapper->bExceptionOnMissingData = true; | |
| 278 | - | |
| 279 | 278 | try { |
| 280 | - return $mapper->mapArray( $body, [], $object_name ); | |
| 281 | - } catch ( WINP\JsonMapper\Exception $exception ) { | |
| 282 | - error_log( 'Snippet api [map_objects]: ' . $exception->getMessage() ); | |
| 279 | + if ( ! method_exists( $object_name, 'array_from_json' ) ) { | |
| 280 | + throw new Exception( "Class {$object_name} does not have an array_from_json method" ); | |
| 281 | + } | |
| 283 | 282 | |
| 283 | + return $object_name::array_from_json( $body ); | |
| 284 | + } catch ( Exception $exception ) { | |
| 284 | 285 | return false; |
| 285 | 286 | } |
| 286 | 287 | } |
| 287 | - | |
| 288 | 288 | } |