PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.83
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.83
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.83, at WPDataAccess/API/WPDA_API_Core.php

757 lines 32.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\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 if ( is_array( $param ) ) {
460 foreach ( $param as $key => $value ) {
461 if ( 'params' === $key || 'notify' === $key ) {
462 // Sanitize custom parameters
463 $custom_params = array();
464 foreach ( $value as $param_key => $param_value ) {
465 $custom_params[sanitize_text_field( $param_key )] = sanitize_text_field( $param_value );
466 }
467 $params[sanitize_text_field( $key )] = $custom_params;
468 } else {
469 $params[sanitize_text_field( $key )] = sanitize_text_field( $value );
470 }
471 }
472 }
473 return $params;
474 },
475 'validate_callback' => function ( $param ) {
476 return is_array( $param );
477 },
478 ),
479 );
480 }
481
482 protected function get_param( $key, $description = null ) {
483 if ( isset( $this->params[$key] ) ) {
484 $param = $this->params[$key];
485 if ( null !== $description ) {
486 $param['description'] = $description;
487 }
488 return $param;
489 } else {
490 // Force REST API error
491 return false;
492 }
493 }
494
495 protected function get_user_roles() {
496 if ( null === WPDA_API_Core::$user_roles ) {
497 WPDA_API_Core::$user_roles = WPDA::get_current_user_roles();
498 if ( false === WPDA_API_Core::$user_roles ) {
499 WPDA_API_Core::$user_roles = array();
500 }
501 }
502 return WPDA_API_Core::$user_roles;
503 }
504
505 protected function get_user_login() {
506 if ( null === WPDA_API_Core::$user_login ) {
507 WPDA_API_Core::$user_login = WPDA::get_current_user_login();
508 }
509 return WPDA_API_Core::$user_login;
510 }
511
512 protected function current_user_can_access() {
513 return WPDA::current_user_is_admin();
514 }
515
516 protected function unauthorized() {
517 return new \WP_Error('error', __( 'Unauthorized', 'wp-data-access' ), array(
518 'status' => 401,
519 ));
520 }
521
522 protected function current_user_token_valid( $request ) {
523 return wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' );
524 }
525
526 protected function invalid_nonce() {
527 return new \WP_Error('rest_cookie_invalid_nonce', 'Cookie check failed', array(
528 'status' => 403,
529 ));
530 }
531
532 protected function bad_request() {
533 return new \WP_Error('error', __( 'Bad request', 'wp-data-access' ), array(
534 'status' => 400,
535 ));
536 }
537
538 protected function invalid_app_settings() {
539 return new \WP_Error('error', __( 'Invalid app settings - contact support', 'wp-data-access' ), array(
540 'status' => 403,
541 ));
542 }
543
544 protected function current_user_can_remote() {
545 return false;
546 }
547
548 public static function sanitize_db_identifier( $param ) {
549 if ( !is_string( $param ) ) {
550 return null;
551 }
552 // Preserve starting and trailing spaces
553 $spaces_before = strlen( $param ) - strlen( ltrim( $param ) );
554 $spaces_after = strlen( $param ) - strlen( rtrim( $param ) );
555 return str_repeat( ' ', $spaces_before ) . WPDA::remove_backticks( sanitize_text_field( $param ) ) . str_repeat( ' ', $spaces_after );
556 }
557
558 public static function validate_db_identifier( $param ) {
559 return !empty( WPDA::remove_backticks( $param ) );
560 }
561
562 protected function sanitize_columns( $param ) {
563 $sanitized_param = array();
564 foreach ( $param as $p ) {
565 $sanitized_param[] = array(
566 'columnName' => $this->sanitize_db_identifier( $p['columnName'] ),
567 'isSelected' => $p['isSelected'],
568 );
569 }
570 return $sanitized_param;
571 }
572
573 protected function validate_columns( $param ) {
574 if ( !is_array( $param ) ) {
575 return false;
576 }
577 foreach ( $param as $p ) {
578 if ( !isset( $p['columnName'], $p['isSelected'] ) || !$this->validate_db_identifier( $p['columnName'] ) || 'boolean' !== gettype( $p['isSelected'] ) ) {
579 return false;
580 }
581 }
582 return true;
583 }
584
585 protected function get_wp_roles() {
586 $roles = array();
587 global $wp_roles;
588 foreach ( $wp_roles->roles as $role => $role_object ) {
589 if ( isset( $role_object['name'] ) ) {
590 $roles[$role] = $role_object['name'];
591 }
592 }
593 return $roles;
594 }
595
596 protected function get_wp_users() {
597 $users = array();
598 foreach ( get_users() as $user ) {
599 if ( isset( $user->user_login, $user->display_name ) ) {
600 $users[$user->user_login] = $user->display_name;
601 }
602 }
603 return $users;
604 }
605
606 protected function get_env() {
607 $env = array(
608 'ip' => ( isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '' ),
609 'id' => WPDA::get_current_user_id(),
610 'user' => WPDA::get_current_user_login(),
611 'roles' => WPDA::get_current_user_roles(),
612 'login' => 'anonymous' !== WPDA::get_current_user_login(),
613 );
614 return $env;
615 }
616
617 protected function get_table_info( $dbs, $tbl, $default_where = '' ) {
618 $wpdadb = WPDADB::get_db_connection( $dbs );
619 if ( $wpdadb === null ) {
620 return array(
621 'type' => null,
622 'engine' => null,
623 'count' => null,
624 );
625 }
626 $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) );
627 $resultset = $wpdadb->get_results( $query, 'ARRAY_N' );
628 if ( count( $resultset ) === 1 ) {
629 if ( null !== $resultset[0][2] ) {
630 return array(
631 'type' => $resultset[0][0],
632 'engine' => $resultset[0][1],
633 'count' => ( '' === $default_where ? ( $resultset[0][2] == 0 ? null : $resultset[0][2] ) : null ),
634 );
635 } else {
636 $count = $this->get_row_count_estimate( $dbs, $tbl );
637 return array(
638 'type' => $resultset[0][0],
639 'engine' => $resultset[0][1],
640 'count' => ( $count === 0 ? null : $count ),
641 );
642 }
643 } else {
644 return array(
645 'type' => null,
646 'engine' => null,
647 'count' => null,
648 );
649 }
650 }
651
652 protected function get_row_count_estimate( $dbs, $tbl ) {
653 $wpdadb = WPDADB::get_db_connection( $dbs );
654 if ( null === $wpdadb ) {
655 return -1;
656 }
657 $explain = $wpdadb->get_results( 'explain select count(*) from `' . str_replace( '`', '', $tbl ) . '`', 'ARRAY_A' );
658 if ( isset( $explain[0]['rows'] ) ) {
659 return $explain[0]['rows'];
660 } else {
661 // This should never happen
662 return -1;
663 }
664 }
665
666 protected function get_media(
667 $dbs,
668 $tbl,
669 $columns,
670 $prefix = ''
671 ) {
672 $media = array();
673 $wp_media = array();
674 foreach ( $columns as $column ) {
675 $media_type = WPDA_Media_Model::get_column_media( $tbl, $column['column_name'], $dbs );
676 $column_name = $prefix . $column['column_name'];
677 switch ( $media_type ) {
678 case 'ImageURL':
679 $media[$column_name] = $media_type;
680 break;
681 case 'Hyperlink':
682 // Get table settings.
683 $table_settings_db = WPDA_Table_Settings_Model::query( $tbl, $dbs );
684 if ( isset( $table_settings_db[0]['wpda_table_settings'] ) ) {
685 $table_settings = json_decode( $table_settings_db[0]['wpda_table_settings'], true );
686 } else {
687 $table_settings = null;
688 }
689 // Check hyperlink format.
690 if ( isset( $table_settings['table_settings']['hyperlink_definition'] ) && 'text' === $table_settings['table_settings']['hyperlink_definition'] ) {
691 $media[$column_name] = 'HyperlinkURL';
692 } else {
693 $media[$column_name] = 'HyperlinkObject';
694 }
695 break;
696 default:
697 if ( false !== $media_type ) {
698 // Handle WordPress Media Library integration
699 $media[$column_name] = "WP-{$media_type}";
700 }
701 }
702 $wp_media[$column_name] = $media_type;
703 }
704 return [
705 'media' => $media,
706 'wp_media' => $wp_media,
707 ];
708 }
709
710 /**
711 * Write standard JSON response.
712 *
713 * @param string $message Response text message.
714 * @param mixed $data Response data.
715 * @param mixed $context Context data.
716 * @param mixed $meta Meta data.
717 * @return \WP_REST_Response
718 */
719 protected static function WPDA_Rest_Response(
720 $message = '',
721 $data = null,
722 $context = null,
723 $meta = null
724 ) {
725 // Prepare response.
726 $response = new \WP_REST_Response(array(
727 'code' => 'ok',
728 'message' => $message,
729 'data' => $data,
730 'context' => $context,
731 'meta' => $meta,
732 ), 200);
733 // Disable caching.
734 $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0' );
735 $response->header( 'Pragma', 'no-cache' );
736 $response->header( 'Expires', '0' );
737 return $response;
738 }
739
740 protected static function WPDA_Rest_Response_Info( $message = '' ) {
741 // Prepare response.
742 $response = new \WP_REST_Response(array(
743 'code' => 'info',
744 'message' => $message,
745 'data' => null,
746 'context' => null,
747 'meta' => null,
748 ), 200);
749 // Disable caching.
750 $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0' );
751 $response->header( 'Pragma', 'no-cache' );
752 $response->header( 'Expires', '0' );
753 return $response;
754 }
755
756 }
757