PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.81
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.81
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_API_Core.php

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

755 lines 32.2 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\Connection\WPDADB;
6 use WPDataAccess\Plugin_Table_Models\WPDA_Media_Model;
7 use WPDataAccess\Plugin_Table_Models\WPDA_Table_Settings_Model;
8 use WPDataAccess\WPDA;
9 abstract class WPDA_API_Core {
10 public abstract function register_rest_routes();
11
12 private static $user_roles = null;
13
14 private static $user_login = null;
15
16 private $params;
17
18 public function __construct() {
19 $this->params = array(
20 'dbs' => array(
21 'required' => true,
22 'type' => 'string',
23 'description' => __( 'Local database name or remote connection string', 'wp-data-access' ),
24 'sanitize_callback' => function ( $param ) {
25 return $this->sanitize_db_identifier( $param );
26 },
27 'validate_callback' => function ( $param ) {
28 return $this->validate_db_identifier( $param );
29 },
30 ),
31 'tbl' => array(
32 'required' => true,
33 'type' => 'string',
34 'description' => __( 'Table or view name', 'wp-data-access' ),
35 'sanitize_callback' => function ( $param ) {
36 return $this->sanitize_db_identifier( $param );
37 },
38 'validate_callback' => function ( $param ) {
39 return $this->validate_db_identifier( $param );
40 },
41 ),
42 'client_side' => array(
43 'required' => false,
44 'type' => 'boolean',
45 'description' => __( 'Server side processing', 'wp-data-access' ),
46 'sanitize_callback' => 'sanitize_text_field',
47 'validate_callback' => 'rest_validate_request_arg',
48 ),
49 'app_id' => array(
50 'required' => true,
51 'type' => 'integer',
52 'description' => __( 'App ID', 'wp-data-access' ),
53 'sanitize_callback' => 'absint',
54 'validate_callback' => 'rest_validate_request_arg',
55 ),
56 'cnt_id' => array(
57 'required' => true,
58 'type' => 'integer',
59 'description' => __( 'Container ID', 'wp-data-access' ),
60 'sanitize_callback' => 'absint',
61 'validate_callback' => 'rest_validate_request_arg',
62 ),
63 'app_name' => array(
64 'required' => true,
65 'type' => 'string',
66 'description' => __( 'App name', 'wp-data-access' ),
67 'sanitize_callback' => 'sanitize_text_field',
68 'validate_callback' => 'rest_validate_request_arg',
69 ),
70 'app_title' => array(
71 'required' => true,
72 'type' => 'string',
73 'description' => __( 'App title', 'wp-data-access' ),
74 'sanitize_callback' => 'sanitize_text_field',
75 'validate_callback' => 'rest_validate_request_arg',
76 ),
77 'app_type' => array(
78 'required' => true,
79 'type' => 'integer',
80 'description' => __( 'App type', 'wp-data-access' ),
81 'sanitize_callback' => 'absint',
82 'validate_callback' => 'rest_validate_request_arg',
83 ),
84 'app_settings' => array(
85 'required' => true,
86 'type' => 'string',
87 'description' => __( 'App settings', 'wp-data-access' ),
88 'sanitize_callback' => 'sanitize_text_field',
89 'validate_callback' => 'rest_validate_request_arg',
90 ),
91 'app_add_to_menu' => array(
92 'required' => true,
93 'type' => 'integer',
94 'description' => __( 'Add app to dashboard menu', 'wp-data-access' ),
95 'sanitize_callback' => 'absint',
96 'validate_callback' => 'rest_validate_request_arg',
97 ),
98 'app_cls' => array(
99 'required' => true,
100 'type' => 'array',
101 'description' => __( 'App columns', 'wp-data-access' ),
102 'sanitize_callback' => function ( $param ) {
103 return $this->sanitize_columns( $param );
104 },
105 'validate_callback' => function ( $param ) {
106 return $this->validate_columns( $param );
107 },
108 ),
109 'join_tab' => array(
110 'required' => false,
111 'type' => 'boolean',
112 'description' => __( 'Use join table', 'wp-data-access' ),
113 'sanitize_callback' => 'sanitize_text_field',
114 'validate_callback' => 'rest_validate_request_arg',
115 ),
116 'rel_tab' => array(
117 'required' => false,
118 'type' => 'boolean',
119 'description' => __( 'Use relation table', 'wp-data-access' ),
120 'sanitize_callback' => 'sanitize_text_field',
121 'validate_callback' => 'rest_validate_request_arg',
122 ),
123 'md' => array(
124 'required' => false,
125 'type' => 'mixed',
126 'description' => __( 'Master detail join conditions', 'wp-data-access' ),
127 'sanitize_callback' => function ( $param ) {
128 $columns = array();
129 foreach ( rest_sanitize_object( $param ) as $column_name => $value ) {
130 $columns[$this->sanitize_db_identifier( $column_name )] = sanitize_text_field( wp_unslash( $value ) );
131 }
132 return $columns;
133 },
134 'validate_callback' => function ( $param ) {
135 return is_array( $param );
136 },
137 ),
138 'cascade' => array(
139 'required' => false,
140 'type' => 'boolean',
141 'description' => __( 'Use search arguments if true', 'wp-data-access' ),
142 'sanitize_callback' => 'sanitize_text_field',
143 'validate_callback' => 'rest_validate_request_arg',
144 ),
145 'app_apps' => array(
146 'required' => false,
147 'type' => 'array',
148 'description' => __( 'Array of app IDs', 'wp-data-access' ),
149 'sanitize_callback' => function ( $param ) {
150 $apps = array();
151 foreach ( $param as $value ) {
152 if ( is_numeric( $value ) ) {
153 $apps[] = $value;
154 }
155 }
156 return $apps;
157 },
158 'validate_callback' => function ( $param ) {
159 return is_array( $param );
160 },
161 ),
162 'app_query' => array(
163 'required' => false,
164 'type' => 'string',
165 'description' => __( 'Custom query', 'wp-data-access' ),
166 'sanitize_callback' => function ( $param ) {
167 return html_entity_decode( wp_unslash( $param ), ENT_QUOTES );
168 // Preserve SQL operators
169 },
170 ),
171 'col' => array(
172 'required' => true,
173 'type' => 'string',
174 'description' => __( 'Column name', 'wp-data-access' ),
175 'sanitize_callback' => function ( $param ) {
176 return $this->sanitize_db_identifier( $param );
177 },
178 'validate_callback' => function ( $param ) {
179 return $this->validate_db_identifier( $param );
180 },
181 ),
182 'cols' => array(
183 'required' => false,
184 'type' => 'mixed',
185 'description' => __( 'Table or view columns', 'wp-data-access' ),
186 'sanitize_callback' => function ( $param ) {
187 $columns = array();
188 foreach ( rest_sanitize_object( $param ) as $column_name => $queryable ) {
189 $columns[$this->sanitize_db_identifier( $column_name )] = $queryable === true;
190 }
191 return $columns;
192 },
193 'validate_callback' => function ( $param ) {
194 return is_array( $param );
195 },
196 ),
197 'page_index' => array(
198 'required' => false,
199 'type' => 'integer',
200 'description' => __( 'Page number', 'wp-data-access' ),
201 'default' => 0,
202 'minimum' => 0,
203 'sanitize_callback' => 'absint',
204 'validate_callback' => 'rest_validate_request_arg',
205 ),
206 'page_size' => array(
207 'required' => false,
208 'type' => 'integer',
209 'description' => __( 'Rows per page (0=all)', 'wp-data-access' ),
210 'default' => 10,
211 'minimum' => 1,
212 'sanitize_callback' => 'absint',
213 'validate_callback' => 'rest_validate_request_arg',
214 ),
215 'search' => array(
216 'required' => false,
217 'type' => 'string',
218 'description' => __( 'Global search filter', 'wp-data-access' ),
219 'sanitize_callback' => 'sanitize_text_field',
220 'validate_callback' => 'rest_validate_request_arg',
221 ),
222 'search_columns' => array(
223 'required' => false,
224 'type' => 'mixed',
225 'description' => __( 'Column search filters', 'wp-data-access' ),
226 'sanitize_callback' => function ( $param ) {
227 $search = array();
228 foreach ( rest_sanitize_array( $param ) as $value ) {
229 if ( isset( $value['id'], $value['value'] ) ) {
230 $search[] = array(
231 'id' => $this->sanitize_db_identifier( $value['id'] ),
232 'value' => ( is_array( $value['value'] ) ? map_deep( $value['value'], 'sanitize_text_field' ) : sanitize_text_field( $value['value'] ) ),
233 );
234 }
235 }
236 return $search;
237 },
238 'validate_callback' => function ( $param ) {
239 return is_array( $param );
240 },
241 ),
242 'search_column_fns' => array(
243 'required' => false,
244 'description' => __( 'Column search filter modes', 'wp-data-access' ),
245 'sanitize_callback' => function ( $param ) {
246 $search_modes = array();
247 foreach ( $param as $key => $value ) {
248 if ( in_array( $value, WPDA_Table::WPDA_SEARCH_MODES ) ) {
249 // Accepting only valid modes
250 $search_modes[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
251 }
252 }
253 return $search_modes;
254 },
255 'validate_callback' => function ( $param ) {
256 return is_array( $param );
257 },
258 ),
259 'search_column_lov' => array(
260 'required' => false,
261 'type' => 'mixed',
262 'description' => __( 'Search columns for lov support', 'wp-data-access' ),
263 'sanitize_callback' => function ( $param ) {
264 $lovs = array();
265 foreach ( $param as $value ) {
266 $lovs[] = $this->sanitize_db_identifier( $value );
267 }
268 return $lovs;
269 },
270 'validate_callback' => function ( $param ) {
271 return is_array( $param );
272 },
273 ),
274 'search_data_types' => array(
275 'required' => false,
276 'type' => 'mixed',
277 'description' => __( 'Search columns for lov support', 'wp-data-access' ),
278 'sanitize_callback' => function ( $param ) {
279 $date_types = array();
280 foreach ( $param as $key => $value ) {
281 $date_types[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
282 }
283 return $date_types;
284 },
285 'validate_callback' => function ( $param ) {
286 return is_array( $param );
287 },
288 ),
289 'search_custom' => array(
290 'required' => false,
291 'description' => __( 'Custom search filters auto generated from http parameter requirements in default where', 'wp-data-access' ),
292 'sanitize_callback' => function ( $param ) {
293 $search_custom = array();
294 foreach ( $param as $key => $value ) {
295 if ( is_array( $value ) ) {
296 foreach ( $value as $column_name => $column_value ) {
297 $search_custom[$key][$this->sanitize_db_identifier( $column_name )] = sanitize_text_field( $column_value );
298 }
299 }
300 }
301 return $search_custom;
302 },
303 'validate_callback' => function ( $param ) {
304 return is_array( $param );
305 },
306 ),
307 'search_params' => array(
308 'required' => false,
309 'description' => __( 'Shortcode parameters', 'wp-data-access' ),
310 'sanitize_callback' => function ( $param ) {
311 $search_custom = array();
312 foreach ( $param as $key => $value ) {
313 $search_custom[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
314 }
315 return $search_custom;
316 },
317 'validate_callback' => function ( $param ) {
318 return is_array( $param );
319 },
320 ),
321 'sorting' => array(
322 'required' => false,
323 'description' => __( 'Order by (array of { id and desc })', 'wp-data-access' ),
324 'sanitize_callback' => function ( $param ) {
325 $order_by = array();
326 foreach ( rest_sanitize_object( $param ) as $value ) {
327 if ( isset( $value['id'], $value['desc'] ) ) {
328 $order_by[] = array(
329 'id' => $this->sanitize_db_identifier( $value['id'] ),
330 'desc' => sanitize_text_field( $value['desc'] ),
331 );
332 }
333 }
334 return $order_by;
335 },
336 'validate_callback' => function ( $param ) {
337 if ( !is_array( $param ) ) {
338 return false;
339 }
340 foreach ( $param as $value ) {
341 if ( !isset( $value['id'], $value['desc'] ) ) {
342 return false;
343 }
344 }
345 return true;
346 },
347 ),
348 'row_count' => array(
349 'required' => false,
350 'type' => 'integer',
351 'description' => __( 'Row count', 'wp-data-access' ),
352 'minimum' => 0,
353 'sanitize_callback' => 'absint',
354 'validate_callback' => 'rest_validate_request_arg',
355 ),
356 'row_count_estimate' => array(
357 'required' => false,
358 'type' => 'boolean',
359 'description' => __( 'Calculate row count estimate', 'wp-data-access' ),
360 'sanitize_callback' => 'sanitize_text_field',
361 'validate_callback' => 'rest_validate_request_arg',
362 ),
363 'key' => array(
364 'required' => true,
365 'type' => 'mixed',
366 'description' => __( 'Primary key', 'wp-data-access' ),
367 'sanitize_callback' => function ( $param ) {
368 $primary_keys = array();
369 foreach ( $param as $key => $value ) {
370 $primary_keys[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
371 }
372 return $primary_keys;
373 },
374 'validate_callback' => function ( $param ) {
375 return is_array( $param );
376 },
377 ),
378 'val' => array(
379 'required' => true,
380 'type' => 'mixed',
381 'description' => __( 'Column values', 'wp-data-access' ),
382 'validate_callback' => function ( $param ) {
383 return is_array( $param );
384 },
385 ),
386 'typ' => array(
387 'required' => true,
388 'type' => 'integer',
389 'description' => __( 'Type = 0, view = 1', 'wp-data-access' ),
390 'minimum' => 0,
391 'maximum' => 1,
392 'sanitize_callback' => 'absint',
393 'validate_callback' => 'rest_validate_request_arg',
394 ),
395 'media' => array(
396 'required' => false,
397 'type' => 'mixed',
398 'description' => __( 'Media columns', 'wp-data-access' ),
399 'sanitize_callback' => function ( $param ) {
400 $media = array();
401 foreach ( $param as $key => $value ) {
402 $media[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
403 }
404 return $media;
405 },
406 'validate_callback' => function ( $param ) {
407 return is_array( $param );
408 },
409 ),
410 'copy_key' => array(
411 'required' => true,
412 'type' => 'string',
413 'description' => __( 'Internal copy action identifier', 'wp-data-access' ),
414 'sanitize_callback' => 'sanitize_text_field',
415 'validate_callback' => 'rest_validate_request_arg',
416 ),
417 'access' => array(
418 'required' => true,
419 'type' => 'string',
420 'description' => __( 'Access (user | global) ', 'wp-data-access' ),
421 'sanitize_callback' => function ( $param ) {
422 return ( 'global' === strtolower( $param ) ? 'global' : 'user' );
423 },
424 'validate_callback' => function ( $param ) {
425 return 'global' === strtolower( $param ) || 'user' === strtolower( $param );
426 },
427 ),
428 'query' => array(
429 'required' => true,
430 'type' => 'string',
431 'description' => __( 'SQL query', 'wp-data-access' ),
432 'sanitize_callback' => function ( $param ) {
433 return html_entity_decode( wp_unslash( $param ), ENT_QUOTES );
434 // Preserve SQL operators
435 },
436 ),
437 'name' => array(
438 'required' => true,
439 'type' => 'string',
440 'description' => __( 'Query name', 'wp-data-access' ),
441 'sanitize_callback' => 'sanitize_text_field',
442 'validate_callback' => 'rest_validate_request_arg',
443 ),
444 'vqb' => array(
445 'required' => false,
446 'type' => 'mixed',
447 'description' => __( 'Visual Query Builder', 'wp-data-access' ),
448 'sanitize_callback' => function ( $param ) {
449 return rest_sanitize_object( $param );
450 },
451 'validate_callback' => 'rest_validate_request_arg',
452 ),
453 'params' => array(
454 'required' => false,
455 'type' => 'array',
456 'description' => __( 'Cron job parameters', 'wp-data-access' ),
457 'sanitize_callback' => function ( $param ) {
458 $params = array();
459 foreach ( $param as $key => $value ) {
460 if ( 'params' === $key || 'notify' === $key ) {
461 // Sanitize custom parameters
462 $custom_params = array();
463 foreach ( $value as $param_key => $param_value ) {
464 $custom_params[sanitize_text_field( $param_key )] = sanitize_text_field( $param_value );
465 }
466 $params[sanitize_text_field( $key )] = $custom_params;
467 } else {
468 $params[sanitize_text_field( $key )] = sanitize_text_field( $value );
469 }
470 }
471 return $params;
472 },
473 'validate_callback' => function ( $param ) {
474 return is_array( $param );
475 },
476 ),
477 );
478 }
479
480 protected function get_param( $key, $description = null ) {
481 if ( isset( $this->params[$key] ) ) {
482 $param = $this->params[$key];
483 if ( null !== $description ) {
484 $param['description'] = $description;
485 }
486 return $param;
487 } else {
488 // Force REST API error
489 return false;
490 }
491 }
492
493 protected function get_user_roles() {
494 if ( null === WPDA_API_Core::$user_roles ) {
495 WPDA_API_Core::$user_roles = WPDA::get_current_user_roles();
496 if ( false === WPDA_API_Core::$user_roles ) {
497 WPDA_API_Core::$user_roles = array();
498 }
499 }
500 return WPDA_API_Core::$user_roles;
501 }
502
503 protected function get_user_login() {
504 if ( null === WPDA_API_Core::$user_login ) {
505 WPDA_API_Core::$user_login = WPDA::get_current_user_login();
506 }
507 return WPDA_API_Core::$user_login;
508 }
509
510 protected function current_user_can_access() {
511 return WPDA::current_user_is_admin();
512 }
513
514 protected function unauthorized() {
515 return new \WP_Error('error', __( 'Unauthorized', 'wp-data-access' ), array(
516 'status' => 401,
517 ));
518 }
519
520 protected function current_user_token_valid( $request ) {
521 return wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' );
522 }
523
524 protected function invalid_nonce() {
525 return new \WP_Error('rest_cookie_invalid_nonce', 'Cookie check failed', array(
526 'status' => 403,
527 ));
528 }
529
530 protected function bad_request() {
531 return new \WP_Error('error', __( 'Bad request', 'wp-data-access' ), array(
532 'status' => 400,
533 ));
534 }
535
536 protected function invalid_app_settings() {
537 return new \WP_Error('error', __( 'Invalid app settings - contact support', 'wp-data-access' ), array(
538 'status' => 403,
539 ));
540 }
541
542 protected function current_user_can_remote() {
543 return false;
544 }
545
546 public static function sanitize_db_identifier( $param ) {
547 if ( !is_string( $param ) ) {
548 return null;
549 }
550 // Preserve starting and trailing spaces
551 $spaces_before = strlen( $param ) - strlen( ltrim( $param ) );
552 $spaces_after = strlen( $param ) - strlen( rtrim( $param ) );
553 return str_repeat( ' ', $spaces_before ) . WPDA::remove_backticks( sanitize_text_field( $param ) ) . str_repeat( ' ', $spaces_after );
554 }
555
556 public static function validate_db_identifier( $param ) {
557 return !empty( WPDA::remove_backticks( $param ) );
558 }
559
560 protected function sanitize_columns( $param ) {
561 $sanitized_param = array();
562 foreach ( $param as $p ) {
563 $sanitized_param[] = array(
564 'columnName' => $this->sanitize_db_identifier( $p['columnName'] ),
565 'isSelected' => $p['isSelected'],
566 );
567 }
568 return $sanitized_param;
569 }
570
571 protected function validate_columns( $param ) {
572 if ( !is_array( $param ) ) {
573 return false;
574 }
575 foreach ( $param as $p ) {
576 if ( !isset( $p['columnName'], $p['isSelected'] ) || !$this->validate_db_identifier( $p['columnName'] ) || 'boolean' !== gettype( $p['isSelected'] ) ) {
577 return false;
578 }
579 }
580 return true;
581 }
582
583 protected function get_wp_roles() {
584 $roles = array();
585 global $wp_roles;
586 foreach ( $wp_roles->roles as $role => $role_object ) {
587 if ( isset( $role_object['name'] ) ) {
588 $roles[$role] = $role_object['name'];
589 }
590 }
591 return $roles;
592 }
593
594 protected function get_wp_users() {
595 $users = array();
596 foreach ( get_users() as $user ) {
597 if ( isset( $user->user_login, $user->display_name ) ) {
598 $users[$user->user_login] = $user->display_name;
599 }
600 }
601 return $users;
602 }
603
604 protected function get_env() {
605 $env = array(
606 'ip' => ( isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '' ),
607 'id' => WPDA::get_current_user_id(),
608 'user' => WPDA::get_current_user_login(),
609 'roles' => WPDA::get_current_user_roles(),
610 'login' => 'anonymous' !== WPDA::get_current_user_login(),
611 );
612 return $env;
613 }
614
615 protected function get_table_info( $dbs, $tbl, $default_where = '' ) {
616 $wpdadb = WPDADB::get_db_connection( $dbs );
617 if ( $wpdadb === null ) {
618 return array(
619 'type' => null,
620 'engine' => null,
621 'count' => null,
622 );
623 }
624 $query = $wpdadb->prepare( "\n\t\t\t\t\tselect table_type,\n\t\t\t\t\t engine,\n\t\t\t\t\t table_rows\n\t\t\t\t\t from information_schema.tables\n\t\t\t\t\t where table_schema = %s\n\t\t\t\t\t and table_name = %s\n\t\t\t\t", array($wpdadb->dbname, $tbl) );
625 $resultset = $wpdadb->get_results( $query, 'ARRAY_N' );
626 if ( count( $resultset ) === 1 ) {
627 if ( null !== $resultset[0][2] ) {
628 return array(
629 'type' => $resultset[0][0],
630 'engine' => $resultset[0][1],
631 'count' => ( '' === $default_where ? ( $resultset[0][2] == 0 ? null : $resultset[0][2] ) : null ),
632 );
633 } else {
634 $count = $this->get_row_count_estimate( $dbs, $tbl );
635 return array(
636 'type' => $resultset[0][0],
637 'engine' => $resultset[0][1],
638 'count' => ( $count === 0 ? null : $count ),
639 );
640 }
641 } else {
642 return array(
643 'type' => null,
644 'engine' => null,
645 'count' => null,
646 );
647 }
648 }
649
650 protected function get_row_count_estimate( $dbs, $tbl ) {
651 $wpdadb = WPDADB::get_db_connection( $dbs );
652 if ( null === $wpdadb ) {
653 return -1;
654 }
655 $explain = $wpdadb->get_results( 'explain select count(*) from `' . str_replace( '`', '', $tbl ) . '`', 'ARRAY_A' );
656 if ( isset( $explain[0]['rows'] ) ) {
657 return $explain[0]['rows'];
658 } else {
659 // This should never happen
660 return -1;
661 }
662 }
663
664 protected function get_media(
665 $dbs,
666 $tbl,
667 $columns,
668 $prefix = ''
669 ) {
670 $media = array();
671 $wp_media = array();
672 foreach ( $columns as $column ) {
673 $media_type = WPDA_Media_Model::get_column_media( $tbl, $column['column_name'], $dbs );
674 $column_name = $prefix . $column['column_name'];
675 switch ( $media_type ) {
676 case 'ImageURL':
677 $media[$column_name] = $media_type;
678 break;
679 case 'Hyperlink':
680 // Get table settings.
681 $table_settings_db = WPDA_Table_Settings_Model::query( $tbl, $dbs );
682 if ( isset( $table_settings_db[0]['wpda_table_settings'] ) ) {
683 $table_settings = json_decode( $table_settings_db[0]['wpda_table_settings'], true );
684 } else {
685 $table_settings = null;
686 }
687 // Check hyperlink format.
688 if ( isset( $table_settings['table_settings']['hyperlink_definition'] ) && 'text' === $table_settings['table_settings']['hyperlink_definition'] ) {
689 $media[$column_name] = 'HyperlinkURL';
690 } else {
691 $media[$column_name] = 'HyperlinkObject';
692 }
693 break;
694 default:
695 if ( false !== $media_type ) {
696 // Handle WordPress Media Library integration
697 $media[$column_name] = "WP-{$media_type}";
698 }
699 }
700 $wp_media[$column_name] = $media_type;
701 }
702 return [
703 'media' => $media,
704 'wp_media' => $wp_media,
705 ];
706 }
707
708 /**
709 * Write standard JSON response.
710 *
711 * @param string $message Response text message.
712 * @param mixed $data Response data.
713 * @param mixed $context Context data.
714 * @param mixed $meta Meta data.
715 * @return \WP_REST_Response
716 */
717 protected static function WPDA_Rest_Response(
718 $message = '',
719 $data = null,
720 $context = null,
721 $meta = null
722 ) {
723 // Prepare response.
724 $response = new \WP_REST_Response(array(
725 'code' => 'ok',
726 'message' => $message,
727 'data' => $data,
728 'context' => $context,
729 'meta' => $meta,
730 ), 200);
731 // Disable caching.
732 $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0' );
733 $response->header( 'Pragma', 'no-cache' );
734 $response->header( 'Expires', '0' );
735 return $response;
736 }
737
738 protected static function WPDA_Rest_Response_Info( $message = '' ) {
739 // Prepare response.
740 $response = new \WP_REST_Response(array(
741 'code' => 'info',
742 'message' => $message,
743 'data' => null,
744 'context' => null,
745 'meta' => null,
746 ), 200);
747 // Disable caching.
748 $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0' );
749 $response->header( 'Pragma', 'no-cache' );
750 $response->header( 'Expires', '0' );
751 return $response;
752 }
753
754 }
755