PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.4.8
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.4.8
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 2.7.1 All 27 releases
insert-php / admin / includes / class.request.php

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

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