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