PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
5.5.84 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 All 160 releases
← All changes | WPDataAccess/API/WPDA_Table.php +238 -63 5.5.295.5.84 View file →
@@ -59,8 +59,26 @@
59 59 'sorting' => $this->get_param( 'sorting' ),
60 60 'row_count' => $this->get_param( 'row_count' ),
61 61 'row_count_estimate' => $this->get_param( 'row_count_estimate' ),
62 62 'media' => $this->get_param( 'media' ),
63 + 'client_side' => $this->get_param( 'client_side' ),
64 + 'global_search' => array(
65 + 'required' => false,
66 + 'type' => 'mixed',
67 + 'description' => __( 'Global search', 'wp-data-access' ),
68 + 'sanitize_callback' => function ( $param ) {
69 + $global_search = array();
70 + foreach ( $param as $key => $value ) {
71 + if ( $key === 's' || $key === 'c' ) {
72 + $global_search[sanitize_text_field( wp_unslash( $key ) )] = sanitize_text_field( wp_unslash( $value ) );
73 + }
74 + }
75 + return $global_search;
76 + },
77 + 'validate_callback' => function ( $param ) {
78 + return is_array( $param ) && isset( $param['s'], $param['c'] );
79 + },
80 + ),
63 81 ),
64 82 ) );
65 83 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'table/get', array(
66 84 'methods' => array('GET', 'POST'),
@@ -118,9 +136,9 @@
118 136
119 137 /**
120 138 * Get table meta info.
121 139 *
122 - * @param WP_REST_Request $request Rest API request.
140 + * @param \WP_REST_Request $request Rest API request.
123 141 * @return \WP_Error|\WP_REST_Response
124 142 */
125 143 public function table_meta( $request ) {
126 144 $dbs = $request->get_param( 'dbs' );
@@ -147,9 +165,9 @@
147 165
148 166 /**
149 167 * Database table query using the full primary key. Must return exactly one row.
150 168 *
151 - * @param WP_REST_Request $request Rest API request.
169 + * @param \WP_REST_Request $request Rest API request.
152 170 * @return \WP_Error|\WP_REST_Response
153 171 */
154 172 public function table_get( $request ) {
155 173 $dbs = $request->get_param( 'dbs' );
@@ -182,9 +200,9 @@
182 200
183 201 /**
184 202 * Insert one row.
185 203 *
186 - * @param WP_REST_Request $request Rest API request.
204 + * @param \WP_REST_Request $request Rest API request.
187 205 * @return \WP_Error|\WP_REST_Response
188 206 */
189 207 public function table_insert( $request ) {
190 208 $dbs = $request->get_param( 'dbs' );
@@ -211,9 +229,9 @@
211 229
212 230 /**
213 231 * Update uses primary key. Must return exactly one row.
214 232 *
215 - * @param WP_REST_Request $request Rest API request.
233 + * @param \WP_REST_Request $request Rest API request.
216 234 * @return \WP_Error|\WP_REST_Response
217 235 */
218 236 public function table_update( $request ) {
219 237 $dbs = $request->get_param( 'dbs' );
@@ -246,9 +264,9 @@
246 264
247 265 /**
248 266 * Delete uses primary key. Must return exactly one row.
249 267 *
250 - * @param WP_REST_Request $request Rest API request.
268 + * @param \WP_REST_Request $request Rest API request.
251 269 * @return \WP_Error|\WP_REST_Response
252 270 */
253 271 public function table_delete( $request ) {
254 272 $dbs = $request->get_param( 'dbs' );
@@ -275,12 +293,13 @@
275 293
276 294 /**
277 295 * Database table query to populate a list of values for a specific table/column.
278 296 *
279 - * @param WP_REST_Request $request Rest API request.
297 + * @param \WP_REST_Request $request Rest API request.
280 298 * @return \WP_Error|\WP_REST_Response
281 299 */
282 300 public function table_lov( $request ) {
301 + return null;
283 302 }
284 303
285 304 /**
286 305 * Database table query.
@@ -286,9 +305,9 @@
286 305 * Database table query.
287 306 *
288 307 * Supports: searching, ordering and pagination.
289 308 *
290 - * @param WP_REST_Request $request Rest API request.
309 + * @param \WP_REST_Request $request Rest API request.
291 310 * @return \WP_Error|\WP_REST_Response
292 311 */
293 312 public function table_select( $request ) {
294 313 $dbs = $request->get_param( 'dbs' );
@@ -298,12 +317,15 @@
298 317 $page_size = $request->get_param( 'page_size' );
299 318 $search = $request->get_param( 'search' );
300 319 $search_columns = $request->get_param( 'search_columns' );
301 320 $search_column_fns = $request->get_param( 'search_column_fns' );
321 + $search_data_types = $request->get_param( 'search_data_types' );
302 322 $sorting = $request->get_param( 'sorting' );
303 323 $row_count = $request->get_param( 'row_count' );
304 324 $row_count_estimate = $request->get_param( 'row_count_estimate' );
305 325 $media = $request->get_param( 'media' );
326 + $client_side = '1' === $request->get_param( 'client_side' );
327 + $global_search = $request->get_param( 'global_search' );
306 328 if ( $this->check_table_access(
307 329 $dbs,
308 330 $tbl,
309 331 $request,
@@ -321,9 +343,19 @@
321 343 $search_column_fns,
322 344 $sorting,
323 345 $row_count,
324 346 $row_count_estimate,
325 - $media
347 + $media,
348 + '',
349 + '',
350 + array(),
351 + array(),
352 + array(),
353 + $search_data_types,
354 + $client_side,
355 + array(),
356 + array(),
357 + $global_search
326 358 );
327 359 } else {
328 360 if ( 'rest_cookie_invalid_nonce' === $msg ) {
329 361 return $this->invalid_nonce();
@@ -360,8 +392,9 @@
360 392 $md = array(),
361 393 $m2m_relationship = array(),
362 394 $search_data_types = array()
363 395 ) {
396 + return null;
364 397 }
365 398
366 399 public function lookup(
367 400 $dbs,
@@ -402,21 +435,48 @@
402 435 }
403 436 }
404 437 $dynamic_where = array();
405 438 if ( is_array( $column_dynamic_values ) && 0 < count( $column_dynamic_values ) ) {
439 + $dynamic_allowed = array();
440 + $dynamic_table = WPDA_List_Columns_Cache::get_list_columns( $dbs, $tbl );
441 + $dynamic_columns = $dynamic_table->get_table_columns();
442 + foreach ( $dynamic_columns as $column ) {
443 + if ( isset( $column['column_name'] ) ) {
444 + $dynamic_allowed[] = $column['column_name'];
445 + }
446 + }
406 447 foreach ( $column_dynamic_values as $key => $value ) {
407 - $dynamic_where[] = $wpdadb->prepare( " `{$key}` = %s ", $value );
448 + if ( !in_array( $key, $dynamic_allowed, true ) ) {
449 + continue;
450 + }
451 + $dynamic_where[] = $wpdadb->prepare( " %i = %s ", array($key, $value) );
408 452 }
409 453 $where .= (( '' === $where ? ' where ' : ' and ' )) . ' (' . implode( ' and ', $dynamic_where ) . ') ';
410 454 }
455 + $column_count = ( '' === $subquery ? '' : ", stats.total_rows as 'count'" );
411 456 if ( strpos( $column_value, ',' ) !== false ) {
412 457 $columns = explode( ',', $column_value );
413 - $sql = $wpdadb->prepare( "\n\t\t\t\t\t\t\tselect distinct `%1s` as 'key'\n\t\t\t\t\t\t\t, `%1s`\n\t\t\t\t\t\t\tfrom `%1s`\n\t\t\t\t\t\t", array($column_key, implode( '`,`', $columns ), $tbl) );
458 + $columns = array_map( function ( $column ) use($wpdadb, $tbl) {
459 + return $wpdadb->prepare( "`%1s`.`%1s`", [$tbl, $column] );
460 + }, $columns );
461 + $sql = $wpdadb->prepare( "\n\t\t\t\t\t\t\tselect distinct `%1s`.`%1s` as 'key'\n\t\t\t\t\t\t\t, %1s\n\t\t\t\t\t\t\t{$column_count}\n\t\t\t\t\t\t\tfrom `%1s`\n\t\t\t\t\t\t", array(
462 + $tbl,
463 + $column_key,
464 + implode( ',', $columns ),
465 + $tbl
466 + ) );
414 467 } else {
415 - $sql = $wpdadb->prepare( "\n\t\t\t\t\t\t\tselect distinct `%1s` as 'key'\n\t\t\t\t\t\t\t, `%1s` as 'value' \n\t\t\t\t\t\t\tfrom `%1s`\n\t\t\t\t\t\t", array($column_key, $column_value, $tbl) );
468 + $sql = $wpdadb->prepare( "\n\t\t\t\t\t\t\tselect distinct `%1s`.`%1s` as 'key'\n\t\t\t\t\t\t\t, `%1s`.`%1s` as 'value'\n\t\t\t\t\t\t\t{$column_count}\n\t\t\t\t\t\t\tfrom `%1s`\n\t\t\t\t\t\t", array(
469 + $tbl,
470 + $column_key,
471 + $tbl,
472 + $column_value,
473 + $tbl
474 + ) );
416 475 }
417 - $sql .= " {$where} order by 2 ";
418 - // $where already sanitized
476 + $orderby = ' order by 2 ';
477 + $sql .= " {$where} {$orderby} ";
478 + // $where and $orderby already sanitized and prepared
419 479 $dataset = $wpdadb->get_results( $sql, 'OBJECT' );
420 480 $wpdadb->suppress_errors( $suppress );
421 481 // Send response.
422 482 if ( '' === $wpdadb->last_error ) {
@@ -458,11 +518,12 @@
458 518 public function get(
459 519 $dbs,
460 520 $tbl,
461 521 $primary_key,
462 - $media_columns,
522 + $media_columns = array(),
463 523 $column_names = array(),
464 - $default_where = ''
524 + $default_where = '',
525 + $docs = array()
465 526 ) {
466 527 $wpdadb = WPDADB::get_db_connection( $dbs );
467 528 if ( null === $wpdadb ) {
468 529 // Error connecting.
@@ -483,15 +544,22 @@
483 544 } else {
484 545 $where .= " and {$default_where} ";
485 546 }
486 547 }
487 - $selected_columns = '*';
488 - if ( 0 < count( $column_names ) ) {
489 - $selected_columns = '`' . implode( '`,`', array_map( function ( $column_name ) {
490 - return WPDA::remove_backticks( $column_name );
491 - }, $column_names ) ) . '`';
548 + // Get table column data types
549 + $column_list = WPDA_List_Columns_Cache::get_list_columns( $dbs, $tbl );
550 + $table_columns = $column_list->get_table_columns();
551 + // Prepare selected column list
552 + $columns_selected = array();
553 + $search_data_types = array();
554 + foreach ( $table_columns as $table_column ) {
555 + if ( isset( $table_column['column_name'], $table_column['data_type'] ) && (in_array( $table_column['column_name'], $column_names ) || empty( $column_names )) ) {
556 + $columns_selected[$table_column['column_name']] = true;
557 + $search_data_types[$table_column['column_name']] = $table_column['data_type'];
558 + }
492 559 }
493 - $sql = $wpdadb->prepare( "select {$selected_columns} from `%1s` {$where}", array($tbl) );
560 + $selected_columns = $this->get_selected_columns( $columns_selected, $search_data_types );
561 + $sql = $wpdadb->prepare( "\n select {$selected_columns}\n from `%1s`\n {$where}\n ", array($tbl) );
494 562 $dataset = $wpdadb->get_results( $sql, 'ARRAY_A' );
495 563 // Prepare debug info.
496 564 if ( 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) {
497 565 $debug = array(
@@ -505,9 +573,9 @@
505 573 }
506 574 $wpdadb->suppress_errors( $suppress );
507 575 // Send response.
508 576 $media = array();
509 - if ( 0 < count( $media_columns ) ) {
577 + if ( is_array( $media_columns ) && 0 < count( $media_columns ) ) {
510 578 foreach ( $media_columns as $media_column_name => $media_column_type ) {
511 579 if ( isset( $dataset[0][$media_column_name] ) ) {
512 580 if ( in_array( $media_column_type, [
513 581 'WP-Image',
@@ -520,23 +588,20 @@
520 588 }
521 589 }
522 590 }
523 591 $context = array();
592 + // Add media
524 593 $context['media'] = $media;
525 594 if ( isset( $debug['debug'] ) && 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) {
526 595 $context['debug'] = $debug['debug'];
527 596 }
528 597 if ( 0 === count( $dataset ) ) {
529 - return $this->WPDA_Rest_Response( 'No data found', $dataset, array(
530 - 'debug' => $debug['debug'],
531 - ) );
598 + return $this->WPDA_Rest_Response( 'No data found', $dataset, $context );
532 599 } else {
533 600 if ( 1 === count( $dataset ) ) {
534 601 return $this->WPDA_Rest_Response( '', $dataset, $context );
535 602 } else {
536 - return $this->WPDA_Rest_Response( 'Query returned more than one row', $dataset, array(
537 - 'debug' => $debug['debug'],
538 - ) );
603 + return $this->WPDA_Rest_Response( 'Query returned more than one row', $dataset, $context );
539 604 }
540 605 }
541 606 }
542 607 }
@@ -746,8 +811,9 @@
746 811 return $sql;
747 812 }
748 813
749 814 private function get_md( $md, $wpdadb, $m2m_relationship ) {
815 + return null;
750 816 }
751 817
752 818 private function get_global_filter(
753 819 $wpdadb,
@@ -789,8 +855,9 @@
789 855 $lookups,
790 856 $m2m_relationship,
791 857 $search_data_types
792 858 ) {
859 + return null;
793 860 }
794 861
795 862 private function get_where(
796 863 $wpdadb,
@@ -801,9 +868,11 @@
801 868 $column_names,
802 869 $lookups,
803 870 $search_columns,
804 871 $search_column_fns,
805 - $search_data_types
872 + $search_data_types,
873 + $geo_radius = array(),
874 + $operator = 'and'
806 875 ) {
807 876 // Default where.
808 877 if ( '' !== trim( $default_where ) && 'where' !== strtolower( substr( trim( $default_where ), 0, 5 ) ) ) {
809 878 $where = "where {$default_where}";
@@ -820,11 +889,58 @@
820 889 );
821 890 if ( 0 < count( $where_global ) ) {
822 891 $where .= (( '' === trim( $where ) ? ' where ' : ' and ' )) . $this->add_condition( $where_global, 'or' );
823 892 }
893 + if ( is_array( $geo_radius ) && 0 < count( $geo_radius ) ) {
894 + // Add geo radius to query
895 + // Variable $geo_radius already sanitized in REST API
896 + $unit = ( "km" == $geo_radius['unit'] ? 1000 : 1609.344 );
897 + // km versus miles
898 + if ( $geo_radius['col']['lat'] === $geo_radius['col']['lng'] ) {
899 + // Location stored in GEOMETRY or POINT data type
900 + $geocol = $geo_radius['col']['lat'];
901 + $geo_where = " ( st_distance_sphere(point(st_y(`{$geocol}`), st_x(`{$geocol}`)), point({$geo_radius['loc']['lng']}, {$geo_radius['loc']['lat']})) / {$unit} ) < {$geo_radius['radius']} ";
902 + } else {
903 + // Latitude and longitude stored separately
904 + $geo_where = " ( st_distance_sphere(point(`{$geo_radius['col']['lng']}`, `{$geo_radius['col']['lat']}`), point({$geo_radius['loc']['lng']}, {$geo_radius['loc']['lat']})) / {$unit} ) < {$geo_radius['radius']} ";
905 + }
906 + if ( '' === $where ) {
907 + $where = " where {$geo_where} ";
908 + } else {
909 + $where .= " and {$geo_where} ";
910 + }
911 + }
824 912 return $where;
825 913 }
826 914
915 + private function get_selected_columns( $column_names, $search_data_types ) {
916 + if ( !is_array( $column_names ) ) {
917 + return '*';
918 + // select all columns
919 + }
920 + if ( 0 === count( $column_names ) ) {
921 + return '*';
922 + // select all columns
923 + }
924 + // Check for geo columns
925 + $geometryColumns = array();
926 + if ( is_array( $search_data_types ) ) {
927 + foreach ( $search_data_types as $column_name => $search_data_type ) {
928 + if ( 'geometry' === strtolower( $search_data_type ) || 'point' === strtolower( $search_data_type ) ) {
929 + $geometryColumns[] = $column_name;
930 + }
931 + }
932 + }
933 + return implode( ",", array_map( function ( $column_name ) use($geometryColumns) {
934 + if ( in_array( $column_name, $geometryColumns ) ) {
935 + return 'ST_AsText(`' . WPDA::remove_backticks( $column_name ) . '`) ' . " as `{$column_name}` ";
936 + // Convert geo data to string
937 + } else {
938 + return '`' . WPDA::remove_backticks( $column_name ) . '`';
939 + }
940 + }, array_keys( $column_names ) ) );
941 + }
942 +
827 943 /**
828 944 * Perform query and return result as JSON response.
829 945 *
830 946 * @param string $dbs Schema name (database).
@@ -854,9 +970,9 @@
854 970 $search_column_fns,
855 971 $sorting,
856 972 $last_row_count,
857 973 $row_count_estimate,
858 - $media_columns,
974 + $media_columns = array(),
859 975 $default_where = '',
860 976 $default_orderby = '',
861 977 $lookups = array(),
862 978 $md = array(),
@@ -861,9 +977,12 @@
861 977 $lookups = array(),
862 978 $md = array(),
863 979 $m2m_relationship = array(),
864 980 $search_data_types = array(),
865 - $client_side = false
981 + $client_side = false,
982 + $geo_radius = array(),
983 + $docs = array(),
984 + $search_global = null
866 985 ) {
867 986 $wpdadb = WPDADB::get_db_connection( $dbs );
868 987 if ( null === $wpdadb ) {
869 988 // Error connecting.
@@ -882,10 +1001,32 @@
882 1001 $column_names,
883 1002 $lookups,
884 1003 $search_columns,
885 1004 $search_column_fns,
886 - $search_data_types
1005 + $search_data_types,
1006 + $geo_radius,
1007 + 'and'
887 1008 );
1009 + if ( $this->current_user_can_access() && isset( $search_global['s'], $search_global['c'] ) ) {
1010 + // Perform global search (admins only)
1011 + // ???
1012 + $wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $dbs, $tbl );
1013 + $table_columns = $wpda_list_columns->get_table_columns();
1014 + $where_global = WPDA::construct_where_clause(
1015 + $dbs,
1016 + $tbl,
1017 + $table_columns,
1018 + $search_global['s'],
1019 + 'false' !== $search_global['c']
1020 + );
1021 + if ( trim( $where_global ) !== '' ) {
1022 + if ( '' !== trim( $where ) && 'where' !== strtolower( substr( trim( $where ), 0, 5 ) ) ) {
1023 + $where .= " and {$where_global} ";
1024 + } else {
1025 + $where .= " where {$where_global} ";
1026 + }
1027 + }
1028 + }
888 1029 // Build order by.
889 1030 $sqlorder = '';
890 1031 if ( is_array( $sorting ) && 0 < count( $sorting ) ) {
891 1032 foreach ( $sorting as $sort ) {
@@ -893,9 +1034,31 @@
893 1034 $sqlorder = 'order by ';
894 1035 } else {
895 1036 $sqlorder .= ',';
896 1037 }
897 - $sqlorder .= '`' . $this->convert_column_name( $m2m_relationship, $sort['id'] ) . '` ' . (( $sort['desc'] ? 'desc' : 'asc' ));
1038 + if ( !$client_side && isset( $lookups[$sort['id']] ) ) {
1039 + // Use lookup table to sort
1040 + $lookup = $lookups[$sort['id']];
1041 + $lookup_dbs = $lookup['dbs'];
1042 + $lookup_wpdadb = ( $dbs === $lookup_dbs ? $wpdadb : WPDADB::get_db_connection( $lookup_dbs ) );
1043 + if ( $lookup_wpdadb !== null ) {
1044 + $lookup_tbl = $lookup['tbl'];
1045 + $lookup_key = $lookup['key'];
1046 + $lookup_value = $lookup['value'];
1047 + $lookup_dataset = $lookup_wpdadb->get_results( $lookup_wpdadb->prepare( "select `%1s`, `%1s` from `%1s` order by 2", array($lookup_key, $lookup_value, $lookup_tbl) ), 'ARRAY_N' );
1048 + $lookup_orderby = 'case `' . $this->convert_column_name( $m2m_relationship, $sort['id'] ) . '` ';
1049 + foreach ( $lookup_dataset as $index => $value ) {
1050 + $lookup_orderby .= $lookup_wpdadb->prepare( 'when %s then %d ', array($value[0], $index) );
1051 + }
1052 + $lookup_orderby .= 'else `' . $this->convert_column_name( $m2m_relationship, $sort['id'] ) . '` end ' . (( $sort['desc'] ? 'desc' : 'asc' ));
1053 + $sqlorder .= $lookup_orderby;
1054 + } else {
1055 + $sqlorder .= '`' . $this->convert_column_name( $m2m_relationship, $sort['id'] ) . '` ' . (( $sort['desc'] ? 'desc' : 'asc' ));
1056 + }
1057 + } else {
1058 + // Normal sort
1059 + $sqlorder .= '`' . $this->convert_column_name( $m2m_relationship, $sort['id'] ) . '` ' . (( $sort['desc'] ? 'desc' : 'asc' ));
1060 + }
898 1061 }
899 1062 }
900 1063 if ( '' === $sqlorder && '' !== trim( $default_orderby ) ) {
901 1064 $sqlorder = $default_orderby;
@@ -909,9 +1072,9 @@
909 1072 if ( !is_numeric( $offset ) ) {
910 1073 $offset = 0;
911 1074 }
912 1075 // Prepare query.
913 - $sql = "\n\t\t\t\t\tselect `" . implode( "`,`", array_keys( $column_names ) ) . "`\n\t\t\t\t\tfrom `%1s`\n\t\t\t\t\t{$where}\n\t\t\t\t\t{$sqlorder}\n\t\t\t\t";
1076 + $sql = "\n\t\t\t\t\tselect " . $this->get_selected_columns( $column_names, $search_data_types ) . "\n\t\t\t\t\tfrom `%1s`\n\t\t\t\t\t{$where}\n\t\t\t\t\t{$sqlorder}\n\t\t\t\t";
914 1077 $sql_tables = array($tbl);
915 1078 // Perpare query.
916 1079 $sql = $wpdadb->prepare( ( true === $client_side ? $sql : $sql . (( 0 < $page_size ? " limit {$page_size} offset {$offset} " : '' )) ), $sql_tables );
917 1080 // Prepare debug info.
@@ -971,9 +1134,9 @@
971 1134 $context = array();
972 1135 if ( 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) {
973 1136 $context['debug'] = $debug;
974 1137 }
975 - if ( 0 < count( $media_columns ) ) {
1138 + if ( is_array( $media_columns ) && 0 < count( $media_columns ) ) {
976 1139 // Handle WP media library
977 1140 $media = array();
978 1141 for ($i = 0; $i < count( $dataset ); $i++) {
979 1142 $media_row = array();
@@ -1035,8 +1198,9 @@
1035 1198 $search_value,
1036 1199 $m2m_relationship = array(),
1037 1200 $search_data_types = array()
1038 1201 ) {
1202 + return null;
1039 1203 }
1040 1204
1041 1205 public static function add_condition( $where_lines, $operand = 'and' ) {
1042 1206 if ( 0 < count( array_filter( $where_lines ) ) ) {
@@ -1052,13 +1216,13 @@
1052 1216 *
1053 1217 * @param string $dbs Database schema name.
1054 1218 * @param string $tbl Database table name.
1055 1219 * @param string $waa With admin actions.
1056 - * @return array\object
1220 + * @return array | object
1057 1221 */
1058 1222 public function get_table_meta_data( $dbs, $tbl, $waa ) {
1059 1223 $sql_create_table = '';
1060 - if ( current_user_can( 'manage_options' ) ) {
1224 + if ( WPDA::current_user_is_admin() ) {
1061 1225 // Admin user has access to all resources
1062 1226 $access = array(
1063 1227 'select' => array('POST'),
1064 1228 'insert' => array('POST'),
@@ -1104,18 +1268,21 @@
1104 1268 $wp_nonce_refresh = null;
1105 1269 $connect = null;
1106 1270 global $wpdb;
1107 1271 $settings->wp = [
1108 - 'roles' => $this->get_wp_roles(),
1109 - 'users' => $this->get_wp_users(),
1110 - 'home' => admin_url( 'admin.php' ),
1111 - 'homea' => admin_url( 'admin-ajax.php' ),
1112 - 'tables' => array_values( $wpdb->tables() ),
1113 - 'date_format' => get_option( 'date_format' ),
1114 - 'time_format' => get_option( 'time_format' ),
1115 - 'alter' => $wp_nonce_alter,
1116 - 'refresh' => $wp_nonce_refresh,
1117 - 'connect' => $connect,
1272 + 'roles' => $this->get_wp_roles(),
1273 + 'users' => $this->get_wp_users(),
1274 + 'home' => admin_url( 'admin.php' ),
1275 + 'homea' => admin_url( 'admin-ajax.php' ),
1276 + 'tables' => array_values( $wpdb->tables() ),
1277 + 'date_format' => get_option( 'date_format' ),
1278 + 'time_format' => get_option( 'time_format' ),
1279 + 'alter' => $wp_nonce_alter,
1280 + 'refresh' => $wp_nonce_refresh,
1281 + 'connect' => $connect,
1282 + 'copyinprogress' => WPDA_Actions::copy_in_progress(),
1283 + 'scroll_offset' => WPDA::get_option( WPDA::OPTION_APPS_SCROLL_OFFSET ),
1284 + 'upload' => @ini_get( 'upload_max_filesize' ),
1118 1285 ];
1119 1286 if ( true === $waa ) {
1120 1287 $settings->wp['aonce'] = implode( '-', array(
1121 1288 wp_create_nonce( 'wpda-export-' . json_encode( $tbl ) ),
@@ -1122,26 +1289,34 @@
1122 1289 // Table export
1123 1290 wp_create_nonce( 'wpda-rename-' . $tbl ),
1124 1291 ) );
1125 1292 }
1126 - $media = $this->get_media( $dbs, $tbl, $columns->get_table_columns() );
1293 + $table_columns = $columns->get_table_columns();
1294 + $media = $this->get_media( $dbs, $tbl, $table_columns );
1295 + $columns_sorted = array();
1296 + foreach ( $table_columns as $column ) {
1297 + if ( isset( $column['column_name'] ) ) {
1298 + $columns_sorted[$column['column_name']] = $column;
1299 + }
1300 + }
1127 1301 }
1128 1302 return array(
1129 - 'columns' => $columns->get_table_columns(),
1130 - 'table_labels' => $columns->get_table_header_labels(),
1131 - 'form_labels' => $columns->get_table_column_headers(),
1132 - 'primary_key' => $columns->get_table_primary_key(),
1133 - 'access' => $access,
1134 - 'settings' => $settings,
1135 - 'media' => $media['media'],
1136 - 'wp_media' => $media['wp_media'],
1137 - 'table_info' => $this->get_table_info( $dbs, $tbl ),
1138 - 'create' => $sql_create_table,
1303 + 'columns' => $table_columns,
1304 + 'columns_sorted' => $columns_sorted,
1305 + 'table_labels' => $columns->get_table_header_labels(),
1306 + 'form_labels' => $columns->get_table_column_headers(),
1307 + 'primary_key' => $columns->get_table_primary_key(),
1308 + 'access' => $access,
1309 + 'settings' => $settings,
1310 + 'media' => $media['media'],
1311 + 'wp_media' => $media['wp_media'],
1312 + 'table_info' => $this->get_table_info( $dbs, $tbl ),
1313 + 'create' => $sql_create_table,
1139 1314 );
1140 1315 }
1141 1316
1142 1317 private function get_table_access( $dbs, $tbl ) {
1143 - if ( current_user_can( 'manage_options' ) ) {
1318 + if ( WPDA::current_user_is_admin() ) {
1144 1319 // Check administrator rights
1145 1320 if ( is_admin() ) {
1146 1321 $access = WPDA_Dictionary_Access::check_table_access_backend( $dbs, $tbl, $done );
1147 1322 } else {
@@ -1179,9 +1354,9 @@
1179 1354 if ( isset( $table[$action]['authorized_users'] ) && is_array( $table[$action]['authorized_users'] ) && 0 < count( $table[$action]['authorized_users'] ) && in_array( (string) $this->get_user_login(), $table[$action]['authorized_users'] ) ) {
1180 1355 return $table[$action]['methods'];
1181 1356 }
1182 1357 // Check authorized roles
1183 - if ( isset( $table[$action]['authorized_roles'] ) && is_array( $table[$action]['authorized_roles'] ) && 0 < count( $table[$action]['authorized_roles'] ) && 0 < count( array_intersect( $this->get_user_roles(), $table[$action]['authorized_roles'] ) ) ) {
1358 + if ( isset( $table[$action]['authorized_roles'] ) && is_array( $table[$action]['authorized_roles'] ) && 0 < count( $table[$action]['authorized_roles'] ) && 0 < count( array_intersect( ( is_array( $this->get_user_roles() ) ? $this->get_user_roles() : array() ), $table[$action]['authorized_roles'] ) ) ) {
1184 1359 return $table[$action]['methods'];
1185 1360 }
1186 1361 }
1187 1362 }
@@ -1192,9 +1367,9 @@
1192 1367 * Check if access is grant for requested database/table.
1193 1368 *
1194 1369 * @param string $dbs Remote or local database connection string.
1195 1370 * @param string $tbl Database table name.
1196 - * @param onject $request Request object.
1371 + * @param object $request Request object.
1197 1372 * @param string $action Possible values: select, insert, update, delete.
1198 1373 * @return bool
1199 1374 */
1200 1375 private function check_table_access(
@@ -1203,9 +1378,9 @@
1203 1378 $request,
1204 1379 $action,
1205 1380 &$msg = ''
1206 1381 ) {
1207 - if ( current_user_can( 'manage_options' ) ) {
1382 + if ( WPDA::current_user_is_admin() ) {
1208 1383 // Grant access to administrators always.
1209 1384 return true;
1210 1385 }
1211 1386 $tables = get_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS );
@@ -1219,9 +1394,9 @@
1219 1394 $msg = __( 'Unauthorized', 'wp-data-access' );
1220 1395 return false;
1221 1396 } else {
1222 1397 if ( !in_array( $request->get_method(), $tables[$dbs][$tbl][$action]['methods'] ) ) {
1223 - //phpcs:ignore - 8.1 proof
1398 + // phpcs:ignore -- 8.1 proof
1224 1399 $msg = __( 'Unauthorized', 'wp-data-access' );
1225 1400 return false;
1226 1401 }
1227 1402 }