PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.76
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.76
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.76, at WPDataAccess/API/WPDA_QB.php

441 lines 17.7 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 WPDA::wpda_log_wp_error( $vqb );
269 $old_name = $request->get_param( 'old_name' ) ?? '';
270 $insert = '1' === $request->get_param( 'insert' );
271 $params = $request->get_param( 'params' );
272 $qb = new WPDA_Query_Builder();
273 $saved = array();
274 if ( 'user' === $access ) {
275 if ( $insert ) {
276 // Check if query name is already used
277 $saved = $qb->get_query( $name );
278 }
279 if ( 0 === count( $saved ) ) {
280 $qb->update_query(
281 $dbs,
282 $name,
283 $query,
284 $old_name,
285 $is_visual,
286 $vqb,
287 $params
288 );
289 }
290 } else {
291 if ( $insert ) {
292 // Check if query name is already used
293 $saved = $qb->get_query_global( $name );
294 }
295 if ( 0 === count( $saved ) ) {
296 $qb->update_query_global(
297 $dbs,
298 $name,
299 $query,
300 $old_name,
301 $is_visual,
302 $vqb,
303 $params
304 );
305 }
306 }
307 if ( 0 === count( $saved ) ) {
308 return $this->WPDA_Rest_Response( '' );
309 } else {
310 return new \WP_Error('error', 'Query name already exists', array(
311 'status' => 403,
312 ));
313 }
314 }
315
316 public function delete( $request ) {
317 if ( !$this->current_user_can_access() ) {
318 return $this->unauthorized();
319 }
320 if ( !$this->current_user_token_valid( $request ) ) {
321 return $this->invalid_nonce();
322 }
323 $access = $request->get_param( 'access' );
324 $name = $request->get_param( 'name' );
325 $qb = new WPDA_Query_Builder();
326 if ( 'user' === $access ) {
327 $qb->delete_query( $name );
328 } else {
329 $qb->delete_query_global( $name );
330 }
331 return $this->WPDA_Rest_Response( '' );
332 }
333
334 public function copy( $request ) {
335 if ( !$this->current_user_can_access() ) {
336 return $this->unauthorized();
337 }
338 if ( !$this->current_user_token_valid( $request ) ) {
339 return $this->invalid_nonce();
340 }
341 $access_from = $request->get_param( 'access_from' );
342 $from_name = $request->get_param( 'from' );
343 $access_to = $request->get_param( 'access_to' );
344 $to_name = $request->get_param( 'to' );
345 $qb = new WPDA_Query_Builder();
346 $source = ( 'user' === $access_from ? $qb->get_query( $from_name ) : $qb->get_query_global( $from_name ) );
347 $visual = ( 'user' === $access_from ? $qb->get_visual_query( $from_name ) : maybe_unserialize( $qb->get_visual_query_global( $from_name ) ) );
348 if ( 0 === count( $source ) ) {
349 return new \WP_Error('error', 'Source query not found', array(
350 'status' => 403,
351 ));
352 }
353 if ( 'user' === $access_to ) {
354 $saved = $qb->get_query( $to_name );
355 if ( 0 === count( $saved ) ) {
356 $qb->add_query( $to_name, $source );
357 if ( !empty( $visual ) ) {
358 $qb->upd_visual_query( $to_name, $visual );
359 }
360 }
361 } else {
362 $saved = $qb->get_query_global( $to_name );
363 if ( 0 === count( $saved ) ) {
364 $qb->add_query_global( $to_name, $source );
365 if ( !empty( $visual ) ) {
366 $qb->upd_visual_query_global( $to_name, $visual );
367 }
368 }
369 }
370 if ( 0 === count( $saved ) ) {
371 return $this->WPDA_Rest_Response( '' );
372 } else {
373 return new \WP_Error('error', 'Query name already exists', array(
374 'status' => 403,
375 ));
376 }
377 }
378
379 public function hints( $request ) {
380 if ( !$this->current_user_can_access() ) {
381 return $this->unauthorized();
382 }
383 if ( !$this->current_user_token_valid( $request ) ) {
384 return $this->invalid_nonce();
385 }
386 $dbs = $request->get_param( 'dbs' );
387 $qb = new WPDA_Query_Builder();
388 return $this->WPDA_Rest_Response( '', $qb->get_hints( $dbs ) );
389 }
390
391 public function ac( $request ) {
392 if ( !$this->current_user_can_access() ) {
393 return $this->unauthorized();
394 }
395 if ( !$this->current_user_token_valid( $request ) ) {
396 return $this->invalid_nonce();
397 }
398 $enable = $request->get_param( 'enable' );
399 if ( null === $enable ) {
400 // Return current value
401 return $this->WPDA_Rest_Response( get_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, true ) );
402 } elseif ( '1' == $enable ) {
403 // Enable auto complete
404 update_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, true );
405 return $this->WPDA_Rest_Response( '' );
406 } else {
407 // Enable auto complete
408 update_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, false );
409 return $this->WPDA_Rest_Response( '' );
410 }
411 }
412
413 public function cron_schedules( $request ) {
414 if ( !$this->current_user_can_access() ) {
415 return $this->unauthorized();
416 }
417 if ( !$this->current_user_token_valid( $request ) ) {
418 return $this->invalid_nonce();
419 }
420 }
421
422 public function cron_add( $request ) {
423 if ( !$this->current_user_can_access() ) {
424 return $this->unauthorized();
425 }
426 if ( !$this->current_user_token_valid( $request ) ) {
427 return $this->invalid_nonce();
428 }
429 }
430
431 public function cron_delete( $request ) {
432 if ( !$this->current_user_can_access() ) {
433 return $this->unauthorized();
434 }
435 if ( !$this->current_user_token_valid( $request ) ) {
436 return $this->invalid_nonce();
437 }
438 }
439
440 }
441