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

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