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

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