PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.82
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.82
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataAccess / API / WPDA_QB.php

WPDA_QB.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.82, at WPDataAccess/API/WPDA_QB.php

428 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\API;
4
5 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists;
6 use WPDataAccess\Query_Builder\WPDA_Query_Builder;
7 use WPDataAccess\Query_Builder\WPDA_Query_Builder_Scheduler;
8 use WPDataAccess\Utilities\WPDA_Mail;
9 use WPDataAccess\WPDA;
10 class WPDA_QB extends WPDA_API_Core {
11 const QUERY_BUILDER_AUTO_COMPLETE = 'wpda_query_builder_auto_complete';
12
13 public function register_rest_routes() {
14 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/open', array(
15 'methods' => array('POST'),
16 'callback' => array($this, 'open'),
17 'permission_callback' => '__return_true',
18 'args' => array(
19 'access' => $this->get_param( 'access' ),
20 ),
21 ) );
22 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/run', array(
23 'methods' => array('POST'),
24 'callback' => array($this, 'run'),
25 'permission_callback' => '__return_true',
26 'args' => array(
27 'dbs' => $this->get_param( 'dbs' ),
28 'query' => $this->get_param( 'query' ),
29 'limit' => array(
30 'required' => false,
31 'type' => 'string',
32 'description' => __( 'Limit query output', 'wp-data-access' ),
33 'sanitize_callback' => 'sanitize_text_field',
34 'validate_callback' => 'rest_validate_request_arg',
35 ),
36 'protect' => array(
37 'required' => false,
38 'type' => 'boolean',
39 'description' => __( 'Protect WordPress tables', 'wp-data-access' ),
40 'sanitize_callback' => 'sanitize_text_field',
41 'validate_callback' => 'rest_validate_request_arg',
42 ),
43 'params' => $this->get_param( 'params' ),
44 ),
45 ) );
46 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/save', array(
47 'methods' => array('POST'),
48 'callback' => array($this, 'save'),
49 'permission_callback' => '__return_true',
50 'args' => array(
51 'access' => $this->get_param( 'access' ),
52 'dbs' => $this->get_param( 'dbs' ),
53 'name' => $this->get_param( 'name' ),
54 'query' => $this->get_param( 'query' ),
55 'is_visual' => array(
56 'required' => false,
57 'type' => 'boolean',
58 'description' => __( 'Is Visual Query?', 'wp-data-access' ),
59 'sanitize_callback' => 'sanitize_text_field',
60 'validate_callback' => 'rest_validate_request_arg',
61 ),
62 'vqb' => $this->get_param( 'vqb' ),
63 'old_name' => array(
64 'required' => false,
65 'type' => 'string',
66 'description' => __( 'Old query name', 'wp-data-access' ),
67 'sanitize_callback' => 'sanitize_text_field',
68 'validate_callback' => 'rest_validate_request_arg',
69 ),
70 'insert' => array(
71 'required' => false,
72 'type' => 'boolean',
73 'description' => __( 'Perform insert', 'wp-data-access' ),
74 'sanitize_callback' => 'sanitize_text_field',
75 'validate_callback' => 'rest_validate_request_arg',
76 ),
77 'params' => $this->get_param( 'params' ),
78 ),
79 ) );
80 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/delete', array(
81 'methods' => array('POST'),
82 'callback' => array($this, 'delete'),
83 'permission_callback' => '__return_true',
84 'args' => array(
85 'access' => $this->get_param( 'access' ),
86 'name' => $this->get_param( 'name' ),
87 ),
88 ) );
89 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/copy', array(
90 'methods' => array('POST'),
91 'callback' => array($this, 'copy'),
92 'permission_callback' => '__return_true',
93 'args' => array(
94 'access_from' => $this->get_param( 'access' ),
95 'from' => $this->get_param( 'name' ),
96 'access_to' => $this->get_param( 'access' ),
97 'to' => $this->get_param( 'name' ),
98 ),
99 ) );
100 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/hints', array(
101 'methods' => array('POST'),
102 'callback' => array($this, 'hints'),
103 'permission_callback' => '__return_true',
104 'args' => array(
105 'dbs' => $this->get_param( 'dbs' ),
106 ),
107 ) );
108 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/ac', array(
109 'methods' => array('POST'),
110 'callback' => array($this, 'ac'),
111 'permission_callback' => '__return_true',
112 'args' => array(
113 'enable' => array(
114 'required' => false,
115 'type' => 'boolean',
116 'description' => __( 'Get|set auto complete', 'wp-data-access' ),
117 'sanitize_callback' => 'sanitize_text_field',
118 'validate_callback' => 'rest_validate_request_arg',
119 ),
120 ),
121 ) );
122 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/schedules', array(
123 'methods' => array('POST'),
124 'callback' => array($this, 'cron_schedules'),
125 'permission_callback' => '__return_true',
126 'args' => array(),
127 ) );
128 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/add', array(
129 'methods' => array('POST'),
130 'callback' => array($this, 'cron_add'),
131 'permission_callback' => '__return_true',
132 'args' => array(
133 'access' => $this->get_param( 'access' ),
134 'name' => $this->get_param( 'name' ),
135 'params' => $this->get_param( 'params' ),
136 'start' => array(
137 'required' => true,
138 'type' => 'integer',
139 'description' => __( 'Start date/time', 'wp-data-access' ),
140 'sanitize_callback' => 'sanitize_text_field',
141 'validate_callback' => 'rest_validate_request_arg',
142 ),
143 'interval' => array(
144 'required' => true,
145 'type' => 'string',
146 'description' => __( 'Recurrence', 'wp-data-access' ),
147 'sanitize_callback' => 'sanitize_text_field',
148 'validate_callback' => 'rest_validate_request_arg',
149 ),
150 ),
151 ) );
152 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/delete', array(
153 'methods' => array('POST'),
154 'callback' => array($this, 'cron_delete'),
155 'permission_callback' => '__return_true',
156 'args' => array(
157 'access' => $this->get_param( 'access' ),
158 'name' => $this->get_param( 'name' ),
159 'params' => $this->get_param( 'params' ),
160 ),
161 ) );
162 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/vqb/get', array(
163 'methods' => array('POST'),
164 'callback' => array($this, 'vqb_get'),
165 'permission_callback' => '__return_true',
166 'args' => array(
167 'access' => $this->get_param( 'access' ),
168 'name' => $this->get_param( 'name' ),
169 ),
170 ) );
171 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/vqb/tbl', array(
172 'methods' => array('POST'),
173 'callback' => array($this, 'vqb_tbl'),
174 'permission_callback' => '__return_true',
175 'args' => array(
176 'dbs' => $this->get_param( 'dbs' ),
177 'tbl' => $this->get_param( 'tbl' ),
178 ),
179 ) );
180 }
181
182 public function vqb_tbl( $request ) {
183 if ( !$this->current_user_can_access() ) {
184 return $this->unauthorized();
185 }
186 if ( !$this->current_user_token_valid( $request ) ) {
187 return $this->invalid_nonce();
188 }
189 $dbs = $request->get_param( 'dbs' );
190 $tbl = $request->get_param( 'tbl' );
191 $cols = WPDA_Dictionary_Lists::get_table_widget_info( $dbs, $tbl );
192 return $this->WPDA_Rest_Response( '', $cols );
193 }
194
195 public function vqb_get( $request ) {
196 if ( !$this->current_user_can_access() ) {
197 return $this->unauthorized();
198 }
199 if ( !$this->current_user_token_valid( $request ) ) {
200 return $this->invalid_nonce();
201 }
202 $access = $request->get_param( 'access' );
203 $name = $request->get_param( 'name' );
204 $qb = new WPDA_Query_Builder();
205 if ( 'user' === $access ) {
206 $query = $qb->get_visual_query( $name );
207 } else {
208 $query = maybe_unserialize( $qb->get_visual_query_global( $name ) );
209 }
210 return $this->WPDA_Rest_Response( '', $query );
211 }
212
213 public function open( $request ) {
214 if ( !$this->current_user_can_access() ) {
215 return $this->unauthorized();
216 }
217 if ( !$this->current_user_token_valid( $request ) ) {
218 return $this->invalid_nonce();
219 }
220 $access = $request->get_param( 'access' );
221 $qb = new WPDA_Query_Builder();
222 if ( 'user' === $access ) {
223 $queries = $qb->get_query_list();
224 } else {
225 $queries = $qb->get_query_list_global();
226 }
227 if ( is_array( $queries ) ) {
228 uksort( $queries, 'strnatcasecmp' );
229 }
230 return $this->WPDA_Rest_Response( '', $queries );
231 }
232
233 public function run( $request ) {
234 if ( !$this->current_user_can_access() ) {
235 return $this->unauthorized();
236 }
237 if ( !$this->current_user_token_valid( $request ) ) {
238 return $this->invalid_nonce();
239 }
240 $dbs = $request->get_param( 'dbs' );
241 $query = $request->get_param( 'query' );
242 $limit = $request->get_param( 'limit' );
243 $protect = $request->get_param( 'protect' );
244 $params = $request->get_param( 'params' );
245 $qb = new WPDA_Query_Builder();
246 return $this->WPDA_Rest_Response( '', $qb->execute_query(
247 $dbs,
248 $query,
249 $limit,
250 $protect,
251 $params
252 ) );
253 }
254
255 public function save( $request ) {
256 if ( !$this->current_user_can_access() ) {
257 return $this->unauthorized();
258 }
259 if ( !$this->current_user_token_valid( $request ) ) {
260 return $this->invalid_nonce();
261 }
262 $access = $request->get_param( 'access' );
263 $dbs = $request->get_param( 'dbs' );
264 $name = $request->get_param( 'name' );
265 $query = $request->get_param( 'query' );
266 $is_visual = '1' === $request->get_param( 'is_visual' );
267 $vqb = $request->get_param( 'vqb' );
268 $old_name = $request->get_param( 'old_name' ) ?? '';
269 $insert = '1' === $request->get_param( 'insert' );
270 $params = $request->get_param( 'params' );
271 $qb = new WPDA_Query_Builder();
272 $saved = array();
273 if ( 'user' === $access ) {
274 if ( $insert ) {
275 // Check if query name is already used
276 $saved = $qb->get_query( $name );
277 }
278 if ( 0 === count( $saved ) ) {
279 $qb->update_query(
280 $dbs,
281 $name,
282 $query,
283 $old_name,
284 $is_visual,
285 $vqb,
286 $params
287 );
288 }
289 } else {
290 if ( $insert ) {
291 // Check if query name is already used
292 $saved = $qb->get_query_global( $name );
293 }
294 if ( 0 === count( $saved ) ) {
295 $qb->update_query_global(
296 $dbs,
297 $name,
298 $query,
299 $old_name,
300 $is_visual,
301 $vqb,
302 $params
303 );
304 }
305 }
306 if ( 0 === count( $saved ) ) {
307 return $this->WPDA_Rest_Response( '' );
308 } else {
309 return new \WP_Error('error', 'Query name already exists', array(
310 'status' => 403,
311 ));
312 }
313 }
314
315 public function delete( $request ) {
316 if ( !$this->current_user_can_access() ) {
317 return $this->unauthorized();
318 }
319 if ( !$this->current_user_token_valid( $request ) ) {
320 return $this->invalid_nonce();
321 }
322 $access = $request->get_param( 'access' );
323 $name = $request->get_param( 'name' );
324 $qb = new WPDA_Query_Builder();
325 if ( 'user' === $access ) {
326 $qb->delete_query( $name );
327 } else {
328 $qb->delete_query_global( $name );
329 }
330 return $this->WPDA_Rest_Response( '' );
331 }
332
333 public function copy( $request ) {
334 if ( !$this->current_user_can_access() ) {
335 return $this->unauthorized();
336 }
337 if ( !$this->current_user_token_valid( $request ) ) {
338 return $this->invalid_nonce();
339 }
340 $access_from = $request->get_param( 'access_from' );
341 $from_name = $request->get_param( 'from' );
342 $access_to = $request->get_param( 'access_to' );
343 $to_name = $request->get_param( 'to' );
344 $qb = new WPDA_Query_Builder();
345 $source = ( 'user' === $access_from ? $qb->get_query( $from_name ) : $qb->get_query_global( $from_name ) );
346 $visual = ( 'user' === $access_from ? $qb->get_visual_query( $from_name ) : maybe_unserialize( $qb->get_visual_query_global( $from_name ) ) );
347 if ( 0 === count( $source ) ) {
348 return new \WP_Error('error', 'Source query not found', array(
349 'status' => 403,
350 ));
351 }
352 if ( 'user' === $access_to ) {
353 $saved = $qb->get_query( $to_name );
354 if ( 0 === count( $saved ) ) {
355 $qb->add_query( $to_name, $source );
356 if ( !empty( $visual ) ) {
357 $qb->upd_visual_query( $to_name, $visual );
358 }
359 }
360 } else {
361 $saved = $qb->get_query_global( $to_name );
362 if ( 0 === count( $saved ) ) {
363 $qb->add_query_global( $to_name, $source );
364 if ( !empty( $visual ) ) {
365 $qb->upd_visual_query_global( $to_name, $visual );
366 }
367 }
368 }
369 if ( 0 === count( $saved ) ) {
370 return $this->WPDA_Rest_Response( '' );
371 } else {
372 return new \WP_Error('error', 'Query name already exists', array(
373 'status' => 403,
374 ));
375 }
376 }
377
378 public function hints( $request ) {
379 if ( !$this->current_user_can_access() ) {
380 return $this->unauthorized();
381 }
382 if ( !$this->current_user_token_valid( $request ) ) {
383 return $this->invalid_nonce();
384 }
385 $dbs = $request->get_param( 'dbs' );
386 $qb = new WPDA_Query_Builder();
387 return $this->WPDA_Rest_Response( '', $qb->get_hints( $dbs ) );
388 }
389
390 public function ac( $request ) {
391 if ( !$this->current_user_can_access() ) {
392 return $this->unauthorized();
393 }
394 if ( !$this->current_user_token_valid( $request ) ) {
395 return $this->invalid_nonce();
396 }
397 $enable = $request->get_param( 'enable' );
398 if ( null === $enable ) {
399 // Return current value
400 return $this->WPDA_Rest_Response( get_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, true ) );
401 } elseif ( '1' == $enable ) {
402 // Enable auto complete
403 update_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, true );
404 return $this->WPDA_Rest_Response( '' );
405 } else {
406 // Enable auto complete
407 update_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, false );
408 return $this->WPDA_Rest_Response( '' );
409 }
410 }
411
412 public function cron_schedules( $request ) {
413 // This feature is implemented in the premium version
414 return $this->unauthorized();
415 }
416
417 public function cron_add( $request ) {
418 // This feature is implemented in the premium version
419 return $this->unauthorized();
420 }
421
422 public function cron_delete( $request ) {
423 // This feature is implemented in the premium version
424 return $this->unauthorized();
425 }
426
427 }
428