PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.3.10
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.3.10
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
insert-php / admin / includes / class.request.php

class.request.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.3.10, at admin/includes/class.request.php

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