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

744 lines 31.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\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' => 'sanitize_textarea_field',
167 'validate_callback' => 'rest_validate_request_arg',
168 ),
169 'col' => array(
170 'required' => true,
171 'type' => 'string',
172 'description' => __( 'Column name', 'wp-data-access' ),
173 'sanitize_callback' => function ( $param ) {
174 return $this->sanitize_db_identifier( $param );
175 },
176 'validate_callback' => function ( $param ) {
177 return $this->validate_db_identifier( $param );
178 },
179 ),
180 'cols' => array(
181 'required' => false,
182 'type' => 'mixed',
183 'description' => __( 'Table or view columns', 'wp-data-access' ),
184 'sanitize_callback' => function ( $param ) {
185 $columns = array();
186 foreach ( rest_sanitize_object( $param ) as $column_name => $queryable ) {
187 $columns[$this->sanitize_db_identifier( $column_name )] = $queryable === true;
188 }
189 return $columns;
190 },
191 'validate_callback' => function ( $param ) {
192 return is_array( $param );
193 },
194 ),
195 'page_index' => array(
196 'required' => false,
197 'type' => 'integer',
198 'description' => __( 'Page number', 'wp-data-access' ),
199 'default' => 0,
200 'minimum' => 0,
201 'sanitize_callback' => 'absint',
202 'validate_callback' => 'rest_validate_request_arg',
203 ),
204 'page_size' => array(
205 'required' => false,
206 'type' => 'integer',
207 'description' => __( 'Rows per page (0=all)', 'wp-data-access' ),
208 'default' => 10,
209 'minimum' => 1,
210 'sanitize_callback' => 'absint',
211 'validate_callback' => 'rest_validate_request_arg',
212 ),
213 'search' => array(
214 'required' => false,
215 'type' => 'string',
216 'description' => __( 'Global search filter', 'wp-data-access' ),
217 'sanitize_callback' => 'sanitize_text_field',
218 'validate_callback' => 'rest_validate_request_arg',
219 ),
220 'search_columns' => array(
221 'required' => false,
222 'type' => 'mixed',
223 'description' => __( 'Column search filters', 'wp-data-access' ),
224 'sanitize_callback' => function ( $param ) {
225 $search = array();
226 foreach ( rest_sanitize_array( $param ) as $value ) {
227 if ( isset( $value['id'], $value['value'] ) ) {
228 $search[] = array(
229 'id' => $this->sanitize_db_identifier( $value['id'] ),
230 'value' => ( is_array( $value['value'] ) ? map_deep( $value['value'], 'sanitize_text_field' ) : sanitize_text_field( $value['value'] ) ),
231 );
232 }
233 }
234 return $search;
235 },
236 'validate_callback' => function ( $param ) {
237 return is_array( $param );
238 },
239 ),
240 'search_column_fns' => array(
241 'required' => false,
242 'description' => __( 'Column search filter modes', 'wp-data-access' ),
243 'sanitize_callback' => function ( $param ) {
244 $search_modes = array();
245 foreach ( $param as $key => $value ) {
246 if ( in_array( $value, WPDA_Table::WPDA_SEARCH_MODES ) ) {
247 // Accepting only valid modes
248 $search_modes[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
249 }
250 }
251 return $search_modes;
252 },
253 'validate_callback' => function ( $param ) {
254 return is_array( $param );
255 },
256 ),
257 'search_column_lov' => array(
258 'required' => false,
259 'type' => 'mixed',
260 'description' => __( 'Search columns for lov support', 'wp-data-access' ),
261 'sanitize_callback' => function ( $param ) {
262 $lovs = array();
263 foreach ( $param as $value ) {
264 $lovs[] = $this->sanitize_db_identifier( $value );
265 }
266 return $lovs;
267 },
268 'validate_callback' => function ( $param ) {
269 return is_array( $param );
270 },
271 ),
272 'search_data_types' => array(
273 'required' => false,
274 'type' => 'mixed',
275 'description' => __( 'Search columns for lov support', 'wp-data-access' ),
276 'sanitize_callback' => function ( $param ) {
277 $date_types = array();
278 foreach ( $param as $key => $value ) {
279 $date_types[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
280 }
281 return $date_types;
282 },
283 'validate_callback' => function ( $param ) {
284 return is_array( $param );
285 },
286 ),
287 'search_custom' => array(
288 'required' => false,
289 'description' => __( 'Custom search filters auto generated from http parameter requirements in default where', 'wp-data-access' ),
290 'sanitize_callback' => function ( $param ) {
291 $search_custom = array();
292 foreach ( $param as $key => $value ) {
293 if ( is_array( $value ) ) {
294 foreach ( $value as $column_name => $column_value ) {
295 $search_custom[$key][$this->sanitize_db_identifier( $column_name )] = sanitize_text_field( $column_value );
296 }
297 }
298 }
299 return $search_custom;
300 },
301 'validate_callback' => function ( $param ) {
302 return is_array( $param );
303 },
304 ),
305 'search_params' => array(
306 'required' => false,
307 'description' => __( 'Shortcode parameters', 'wp-data-access' ),
308 'sanitize_callback' => function ( $param ) {
309 $search_custom = array();
310 foreach ( $param as $key => $value ) {
311 $search_custom[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
312 }
313 return $search_custom;
314 },
315 'validate_callback' => function ( $param ) {
316 return is_array( $param );
317 },
318 ),
319 'sorting' => array(
320 'required' => false,
321 'description' => __( 'Order by (array of { id and desc })', 'wp-data-access' ),
322 'sanitize_callback' => function ( $param ) {
323 $order_by = array();
324 foreach ( rest_sanitize_object( $param ) as $value ) {
325 if ( isset( $value['id'], $value['desc'] ) ) {
326 $order_by[] = array(
327 'id' => $this->sanitize_db_identifier( $value['id'] ),
328 'desc' => sanitize_text_field( $value['desc'] ),
329 );
330 }
331 }
332 return $order_by;
333 },
334 'validate_callback' => function ( $param ) {
335 if ( !is_array( $param ) ) {
336 return false;
337 }
338 foreach ( $param as $value ) {
339 if ( !isset( $value['id'], $value['desc'] ) ) {
340 return false;
341 }
342 }
343 return true;
344 },
345 ),
346 'row_count' => array(
347 'required' => false,
348 'type' => 'integer',
349 'description' => __( 'Row count', 'wp-data-access' ),
350 'minimum' => 0,
351 'sanitize_callback' => 'absint',
352 'validate_callback' => 'rest_validate_request_arg',
353 ),
354 'row_count_estimate' => array(
355 'required' => false,
356 'type' => 'boolean',
357 'description' => __( 'Calculate row count estimate', 'wp-data-access' ),
358 'sanitize_callback' => 'sanitize_text_field',
359 'validate_callback' => 'rest_validate_request_arg',
360 ),
361 'key' => array(
362 'required' => true,
363 'type' => 'mixed',
364 'description' => __( 'Primary key', 'wp-data-access' ),
365 'sanitize_callback' => function ( $param ) {
366 $primary_keys = array();
367 foreach ( $param as $key => $value ) {
368 $primary_keys[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
369 }
370 return $primary_keys;
371 },
372 'validate_callback' => function ( $param ) {
373 return is_array( $param );
374 },
375 ),
376 'val' => array(
377 'required' => true,
378 'type' => 'mixed',
379 'description' => __( 'Column values', 'wp-data-access' ),
380 'validate_callback' => function ( $param ) {
381 return is_array( $param );
382 },
383 ),
384 'typ' => array(
385 'required' => true,
386 'type' => 'integer',
387 'description' => __( 'Type = 0, view = 1', 'wp-data-access' ),
388 'minimum' => 0,
389 'maximum' => 1,
390 'sanitize_callback' => 'absint',
391 'validate_callback' => 'rest_validate_request_arg',
392 ),
393 'media' => array(
394 'required' => false,
395 'type' => 'mixed',
396 'description' => __( 'Media columns', 'wp-data-access' ),
397 'sanitize_callback' => function ( $param ) {
398 $media = array();
399 foreach ( $param as $key => $value ) {
400 $media[$this->sanitize_db_identifier( $key )] = sanitize_text_field( $value );
401 }
402 return $media;
403 },
404 'validate_callback' => function ( $param ) {
405 return is_array( $param );
406 },
407 ),
408 'access' => array(
409 'required' => true,
410 'type' => 'string',
411 'description' => __( 'Access (user | global) ', 'wp-data-access' ),
412 'sanitize_callback' => function ( $param ) {
413 return ( 'global' === strtolower( $param ) ? 'global' : 'user' );
414 },
415 'validate_callback' => function ( $param ) {
416 return 'global' === strtolower( $param ) || 'user' === strtolower( $param );
417 },
418 ),
419 'query' => array(
420 'required' => true,
421 'type' => 'string',
422 'description' => __( 'SQL query', 'wp-data-access' ),
423 'sanitize_callback' => function ( $param ) {
424 return html_entity_decode( wp_unslash( $param ), ENT_QUOTES );
425 // Preserve SQL operators
426 },
427 ),
428 'name' => array(
429 'required' => true,
430 'type' => 'string',
431 'description' => __( 'Query name', 'wp-data-access' ),
432 'sanitize_callback' => 'sanitize_text_field',
433 'validate_callback' => 'rest_validate_request_arg',
434 ),
435 'vqb' => array(
436 'required' => false,
437 'type' => 'boolean',
438 'description' => __( 'Query uses Visual Query Builder', 'wp-data-access' ),
439 'sanitize_callback' => 'sanitize_text_field',
440 'validate_callback' => 'rest_validate_request_arg',
441 ),
442 'params' => array(
443 'required' => false,
444 'type' => 'array',
445 'description' => __( 'Cron job parameters', 'wp-data-access' ),
446 'sanitize_callback' => function ( $param ) {
447 $params = array();
448 foreach ( $param as $key => $value ) {
449 if ( 'params' === $key || 'notify' === $key ) {
450 // Sanitize custom parameters
451 $custom_params = array();
452 foreach ( $value as $param_key => $param_value ) {
453 $custom_params[sanitize_text_field( $param_key )] = sanitize_text_field( $param_value );
454 }
455 $params[sanitize_text_field( $key )] = $custom_params;
456 } else {
457 $params[sanitize_text_field( $key )] = sanitize_text_field( $value );
458 }
459 }
460 return $params;
461 },
462 'validate_callback' => function ( $param ) {
463 return is_array( $param );
464 },
465 ),
466 );
467 }
468
469 protected function get_param( $key, $description = null ) {
470 if ( isset( $this->params[$key] ) ) {
471 $param = $this->params[$key];
472 if ( null !== $description ) {
473 $param['description'] = $description;
474 }
475 return $param;
476 } else {
477 // Force REST API error
478 return false;
479 }
480 }
481
482 protected function get_user_roles() {
483 if ( null === WPDA_API_Core::$user_roles ) {
484 WPDA_API_Core::$user_roles = WPDA::get_current_user_roles();
485 if ( false === WPDA_API_Core::$user_roles ) {
486 WPDA_API_Core::$user_roles = array();
487 }
488 }
489 return WPDA_API_Core::$user_roles;
490 }
491
492 protected function get_user_login() {
493 if ( null === WPDA_API_Core::$user_login ) {
494 WPDA_API_Core::$user_login = WPDA::get_current_user_login();
495 }
496 return WPDA_API_Core::$user_login;
497 }
498
499 protected function current_user_can_access() {
500 return WPDA::current_user_is_admin();
501 }
502
503 protected function unauthorized() {
504 return new \WP_Error('error', __( 'Unauthorized', 'wp-data-access' ), array(
505 'status' => 401,
506 ));
507 }
508
509 protected function current_user_token_valid( $request ) {
510 return wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' );
511 }
512
513 protected function invalid_nonce() {
514 return new \WP_Error('rest_cookie_invalid_nonce', 'Cookie check failed', array(
515 'status' => 403,
516 ));
517 }
518
519 protected function bad_request() {
520 return new \WP_Error('error', __( 'Bad request', 'wp-data-access' ), array(
521 'status' => 400,
522 ));
523 }
524
525 protected function invalid_app_settings() {
526 return new \WP_Error('error', __( 'Invalid app settings - contact support', 'wp-data-access' ), array(
527 'status' => 403,
528 ));
529 }
530
531 protected function current_user_can_remote() {
532 return false;
533 }
534
535 public static function sanitize_db_identifier( $param ) {
536 if ( !is_string( $param ) ) {
537 return null;
538 }
539 // Preserve starting and trailing spaces
540 $spaces_before = strlen( $param ) - strlen( ltrim( $param ) );
541 $spaces_after = strlen( $param ) - strlen( rtrim( $param ) );
542 return str_repeat( ' ', $spaces_before ) . WPDA::remove_backticks( sanitize_text_field( $param ) ) . str_repeat( ' ', $spaces_after );
543 }
544
545 public static function validate_db_identifier( $param ) {
546 return !empty( WPDA::remove_backticks( $param ) );
547 }
548
549 protected function sanitize_columns( $param ) {
550 $sanitized_param = array();
551 foreach ( $param as $p ) {
552 $sanitized_param[] = array(
553 'columnName' => $this->sanitize_db_identifier( $p['columnName'] ),
554 'isSelected' => $p['isSelected'],
555 );
556 }
557 return $sanitized_param;
558 }
559
560 protected function validate_columns( $param ) {
561 if ( !is_array( $param ) ) {
562 return false;
563 }
564 foreach ( $param as $p ) {
565 if ( !isset( $p['columnName'], $p['isSelected'] ) || !$this->validate_db_identifier( $p['columnName'] ) || 'boolean' !== gettype( $p['isSelected'] ) ) {
566 return false;
567 }
568 }
569 return true;
570 }
571
572 protected function get_wp_roles() {
573 $roles = array();
574 global $wp_roles;
575 foreach ( $wp_roles->roles as $role => $role_object ) {
576 if ( isset( $role_object['name'] ) ) {
577 $roles[$role] = $role_object['name'];
578 }
579 }
580 return $roles;
581 }
582
583 protected function get_wp_users() {
584 $users = array();
585 foreach ( get_users() as $user ) {
586 if ( isset( $user->user_login, $user->display_name ) ) {
587 $users[$user->user_login] = $user->display_name;
588 }
589 }
590 return $users;
591 }
592
593 protected function get_env() {
594 return array(
595 'ip' => $_SERVER['REMOTE_ADDR'],
596 'id' => WPDA::get_current_user_id(),
597 'user' => WPDA::get_current_user_login(),
598 'roles' => WPDA::get_current_user_roles(),
599 'login' => 'anonymous' !== WPDA::get_current_user_login(),
600 );
601 }
602
603 protected function get_table_info( $dbs, $tbl, $default_where = '' ) {
604 $wpdadb = WPDADB::get_db_connection( $dbs );
605 if ( $wpdadb === null ) {
606 return array(
607 'type' => null,
608 'engine' => null,
609 'count' => null,
610 );
611 }
612 $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\t order by table_name\n\t\t\t\t", array($wpdadb->dbname, $tbl) );
613 $resultset = $wpdadb->get_results( $query, 'ARRAY_N' );
614 // phpcs:ignore Standard.Category.SniffName.ErrorCode
615 if ( count( $resultset ) === 1 ) {
616 if ( null !== $resultset[0][2] ) {
617 return array(
618 'type' => $resultset[0][0],
619 'engine' => $resultset[0][1],
620 'count' => ( '' === $default_where ? ( $resultset[0][2] == 0 ? null : $resultset[0][2] ) : null ),
621 );
622 } else {
623 $count = $this->get_row_count_estimate( $dbs, $tbl );
624 return array(
625 'type' => $resultset[0][0],
626 'engine' => $resultset[0][1],
627 'count' => ( $count === 0 ? null : $count ),
628 );
629 }
630 } else {
631 return array(
632 'type' => null,
633 'engine' => null,
634 'count' => null,
635 );
636 }
637 }
638
639 protected function get_row_count_estimate( $dbs, $tbl ) {
640 $wpdadb = WPDADB::get_db_connection( $dbs );
641 if ( null === $wpdadb ) {
642 return -1;
643 }
644 $explain = $wpdadb->get_results( 'explain select count(*) from `' . str_replace( '`', '', $tbl ) . '`', 'ARRAY_A' );
645 if ( isset( $explain[0]['rows'] ) ) {
646 return $explain[0]['rows'];
647 } else {
648 // This should never happen
649 return -1;
650 }
651 }
652
653 protected function get_media(
654 $dbs,
655 $tbl,
656 $columns,
657 $prefix = ''
658 ) {
659 $media = array();
660 $wp_media = array();
661 foreach ( $columns as $column ) {
662 $media_type = WPDA_Media_Model::get_column_media( $tbl, $column['column_name'], $dbs );
663 $column_name = $prefix . $column['column_name'];
664 switch ( $media_type ) {
665 case 'ImageURL':
666 $media[$column_name] = $media_type;
667 break;
668 case 'Hyperlink':
669 // Get table settings.
670 $table_settings_db = WPDA_Table_Settings_Model::query( $tbl, $dbs );
671 if ( isset( $table_settings_db[0]['wpda_table_settings'] ) ) {
672 $table_settings = json_decode( $table_settings_db[0]['wpda_table_settings'], true );
673 } else {
674 $table_settings = null;
675 }
676 // Check hyperlink format.
677 if ( isset( $table_settings['table_settings']['hyperlink_definition'] ) && 'text' === $table_settings['table_settings']['hyperlink_definition'] ) {
678 $media[$column_name] = 'HyperlinkURL';
679 } else {
680 $media[$column_name] = 'HyperlinkObject';
681 }
682 break;
683 default:
684 if ( false !== $media_type ) {
685 // Handle WordPress Media Library integration
686 $media[$column_name] = "WP-{$media_type}";
687 }
688 }
689 $wp_media[$column_name] = $media_type;
690 }
691 return [
692 'media' => $media,
693 'wp_media' => $wp_media,
694 ];
695 }
696
697 /**
698 * Write standard JSON response.
699 *
700 * @param string $message Response text message.
701 * @param mixed $data Response data.
702 * @param mixed $context Context data.
703 * @param mixed $meta Meta data.
704 * @return \WP_REST_Response
705 */
706 protected static function WPDA_Rest_Response(
707 $message = '',
708 $data = null,
709 $context = null,
710 $meta = null
711 ) {
712 // Prepare response.
713 $response = new \WP_REST_Response(array(
714 'code' => 'ok',
715 'message' => $message,
716 'data' => $data,
717 'context' => $context,
718 'meta' => $meta,
719 ), 200);
720 // Disable caching.
721 $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0' );
722 $response->header( 'Pragma', 'no-cache' );
723 $response->header( 'Expires', '0' );
724 return $response;
725 }
726
727 protected static function WPDA_Rest_Response_Info( $message = '' ) {
728 // Prepare response.
729 $response = new \WP_REST_Response(array(
730 'code' => 'info',
731 'message' => $message,
732 'data' => null,
733 'context' => null,
734 'meta' => null,
735 ), 200);
736 // Disable caching.
737 $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0' );
738 $response->header( 'Pragma', 'no-cache' );
739 $response->header( 'Expires', '0' );
740 return $response;
741 }
742
743 }
744