PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.7
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.7
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
← All changes | admin/includes/class.request.php +61 -60 2.4.42.7.7 View file →
@@ -4,31 +4,36 @@
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
12 +use WpOrg\Requests\Requests;
13 +
14 14 if ( ! defined( 'ABSPATH' ) ) {
15 15 exit;
16 16 }
17 17
18 +/**
19 + * WINP_Request class
20 + */
18 21 class WINP_Request {
19 - //В новых версиях Вуди начиная с 2.2.10 будет обращаться к новой версии API библиотеки сниппетов
20 - // это делается для обратной совместимости, чтобы старые версии продолжили работать со старым API
21 - //const WINP_REQUEST_URL = 'http://185.75.88.217/v2/woody/'; //тестовая после переноса на другой сервер
22 - const WINP_REQUEST_URL = 'http://api.woodysnippet.com/v2/woody/';
23 - // Старое API http://142.93.91.206/v1/woody/
24 22
25 23 /**
24 + * Base request URL.
25 + */
26 + const WINP_REQUEST_URL = 'https://api.woodysnippet.com/v2/woody/';
27 +
28 + /**
26 29 * WINP_REQUEST constructor.
27 30 */
28 31 public function __construct() {
29 - require_once WINP_PLUGIN_DIR . '/includes/jsonmapper/class-json-mapper.php';
30 - require_once WINP_PLUGIN_DIR . '/includes/jsonmapper/exceptions/class-exception.php';
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';
31 36 }
32 37
33 38 /**
34 39 * Get license key
@@ -35,9 +40,9 @@
35 40 *
36 41 * @return string
37 42 */
38 43 private function get_key() {
39 - return WINP_Plugin::app()->premium->get_license()->get_key();
44 + return WINP_Plugin::app()->premium->get_key();
40 45 }
41 46
42 47 /**
43 48 * Get license plugin_id
@@ -62,12 +67,13 @@
62 67 *
63 68 * @return array
64 69 */
65 70 private function get_headers() {
66 - return array(
71 + return [
67 72 'Authorization' => 'Bearer ' . $this->get_token(),
68 73 'PluginId' => $this->get_plugin_id(),
69 - );
74 + 'Version' => 'v2', // That tells server that the user is using SDK, not Freemius.
75 + ];
70 76 }
71 77
72 78 /**
73 79 * Check is key data available
@@ -74,9 +80,9 @@
74 80 *
75 81 * @return bool
76 82 */
77 83 public function is_key() {
78 - return WINP_Plugin::app()->premium->is_activate() && $this->get_key();
84 + return WINP_Plugin::app()->premium->is_active() && $this->get_key();
79 85 }
80 86
81 87 /**
82 88 * Make POST request with authorization headers and return response
@@ -81,13 +87,13 @@
81 87 /**
82 88 * Make POST request with authorization headers and return response
83 89 *
84 90 * @param string $point
85 - * @param array $args
91 + * @param array $args
86 92 *
87 93 * @return array|bool|WP_Error
88 94 */
89 - public function post( $point, $args = array() ) {
95 + public function post( $point, $args = [] ) {
90 96 if ( ! $this->is_key() ) {
91 97 return false;
92 98 }
93 99
@@ -99,18 +105,24 @@
99 105 /**
100 106 * Make GET request with authorization headers and return response
101 107 *
102 108 * @param string $point
103 - * @param array $args
109 + * @param array $args
104 110 *
105 111 * @return array|bool|WP_Error
106 112 */
107 - public function get( $point, $args = array() ) {
108 - if ( ! $this->is_key() ) {
113 + public function get( $point, $args = [] ) {
114 + // Allow common endpoints without authentication.
115 + $is_common_endpoint = strpos( $point, 'common' ) === 0;
116 +
117 + if ( ! $is_common_endpoint && ! $this->is_key() ) {
109 118 return false;
110 119 }
111 120
112 - $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 + }
113 125
114 126 return wp_remote_get( self::WINP_REQUEST_URL . $point, $args );
115 127 }
116 128
@@ -117,13 +129,13 @@
117 129 /**
118 130 * Make PUT request with authorization headers and return response
119 131 *
120 132 * @param string $point
121 - * @param array $args
133 + * @param array $args
122 134 *
123 135 * @return array|bool|WP_Error
124 136 */
125 - public function put( $point, $args = array() ) {
137 + public function put( $point, $args = [] ) {
126 138 if ( ! $this->is_key() ) {
127 139 return false;
128 140 }
129 141
@@ -206,38 +218,36 @@
206 218
207 219 /**
208 220 * Get mapped object by name
209 221 *
210 - * @param $json
211 - * @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').
212 224 *
213 - * @return bool|mixed
225 + * @return object|bool Object instance or false on failure.
226 + * @throws Exception If mapping fails.
214 227 */
215 228 public function map_object( $json, $object_name ) {
216 229 if ( ! $this->check_response( $json ) ) {
217 - error_log( 'Snippet api [map_object]: ' . $this->get_response_error( $json ) );
230 + return false;
231 + }
218 232
233 + if ( is_wp_error( $json ) || ! isset( $json['body'] ) ) {
219 234 return false;
220 235 }
221 236
222 - $body = json_decode( $json['body'] );
237 + $body = json_decode( $json['body'], true );
223 238
224 239 if ( ! $this->check_body( $body ) ) {
225 - error_log( 'Snippet api [map_objects]: Wrong body' );
226 -
227 240 return false;
228 241 }
229 242
230 - $mapper = new WINP\JsonMapper\Mapper();
231 -
232 - $mapper->bExceptionOnUndefinedProperty = true;
233 - $mapper->bExceptionOnMissingData = true;
234 -
235 243 try {
236 - return $mapper->map( $body, new $object_name() );
237 - } catch ( WINP\JsonMapper\Exception $exception ) {
238 - 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 + }
239 247
248 + return $object_name::from_array( $body );
249 + } catch ( Exception $exception ) {
240 250 return false;
241 251 }
242 252 }
243 253
@@ -243,45 +253,36 @@
243 253
244 254 /**
245 255 * Get mapped objects by name
246 256 *
247 - * @param $json
248 - * @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').
249 259 *
250 - * @return bool|mixed
260 + * @return array<object>|bool Array of object instances or false on failure.
261 + * @throws Exception If mapping fails.
251 262 */
252 263 public function map_objects( $json, $object_name ) {
253 264 if ( ! $this->check_response( $json ) ) {
254 - error_log(
255 - 'Snippet api [map_objects]: ' . $this->get_response_error( $json )
256 - );
265 + return false;
266 + }
257 267
268 + if ( is_wp_error( $json ) || ! isset( $json['body'] ) ) {
258 269 return false;
259 270 }
260 271
261 - $body = json_decode( $json['body'] );
272 + $body = json_decode( $json['body'], true );
262 273
263 - if ( ! $this->check_body( $body ) ) {
264 - error_log( 'Snippet api [map_objects]: Wrong body' );
265 -
274 + if ( ! is_array( $body ) ) {
266 275 return false;
267 276 }
268 277
269 - $mapper = new WINP\JsonMapper\Mapper();
270 -
271 - $mapper->bExceptionOnUndefinedProperty = true;
272 - $mapper->bExceptionOnMissingData = true;
273 -
274 278 try {
275 - return $mapper->mapArray(
276 - $body,
277 - array(),
278 - $object_name
279 - );
280 - } catch ( WINP\JsonMapper\Exception $exception ) {
281 - 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 + }
282 282
283 + return $object_name::array_from_json( $body );
284 + } catch ( Exception $exception ) {
283 285 return false;
284 286 }
285 287 }
286 -
287 288 }