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