| @@ -60,8 +60,25 @@ | ||
| 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 | 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 | + ), | |
| 64 | 81 | ), |
| 65 | 82 | ) ); |
| 66 | 83 | register_rest_route( WPDA_API::WPDA_NAMESPACE, 'table/get', array( |
| 67 | 84 | 'methods' => array('GET', 'POST'), |
| @@ -119,9 +136,9 @@ | ||
| 119 | 136 | |
| 120 | 137 | /** |
| 121 | 138 | * Get table meta info. |
| 122 | 139 | * |
| 123 | - * @param WP_REST_Request $request Rest API request. | |
| 140 | + * @param \WP_REST_Request $request Rest API request. | |
| 124 | 141 | * @return \WP_Error|\WP_REST_Response |
| 125 | 142 | */ |
| 126 | 143 | public function table_meta( $request ) { |
| 127 | 144 | $dbs = $request->get_param( 'dbs' ); |
| @@ -148,9 +165,9 @@ | ||
| 148 | 165 | |
| 149 | 166 | /** |
| 150 | 167 | * Database table query using the full primary key. Must return exactly one row. |
| 151 | 168 | * |
| 152 | - * @param WP_REST_Request $request Rest API request. | |
| 169 | + * @param \WP_REST_Request $request Rest API request. | |
| 153 | 170 | * @return \WP_Error|\WP_REST_Response |
| 154 | 171 | */ |
| 155 | 172 | public function table_get( $request ) { |
| 156 | 173 | $dbs = $request->get_param( 'dbs' ); |
| @@ -183,9 +200,9 @@ | ||
| 183 | 200 | |
| 184 | 201 | /** |
| 185 | 202 | * Insert one row. |
| 186 | 203 | * |
| 187 | - * @param WP_REST_Request $request Rest API request. | |
| 204 | + * @param \WP_REST_Request $request Rest API request. | |
| 188 | 205 | * @return \WP_Error|\WP_REST_Response |
| 189 | 206 | */ |
| 190 | 207 | public function table_insert( $request ) { |
| 191 | 208 | $dbs = $request->get_param( 'dbs' ); |
| @@ -212,9 +229,9 @@ | ||
| 212 | 229 | |
| 213 | 230 | /** |
| 214 | 231 | * Update uses primary key. Must return exactly one row. |
| 215 | 232 | * |
| 216 | - * @param WP_REST_Request $request Rest API request. | |
| 233 | + * @param \WP_REST_Request $request Rest API request. | |
| 217 | 234 | * @return \WP_Error|\WP_REST_Response |
| 218 | 235 | */ |
| 219 | 236 | public function table_update( $request ) { |
| 220 | 237 | $dbs = $request->get_param( 'dbs' ); |
| @@ -247,9 +264,9 @@ | ||
| 247 | 264 | |
| 248 | 265 | /** |
| 249 | 266 | * Delete uses primary key. Must return exactly one row. |
| 250 | 267 | * |
| 251 | - * @param WP_REST_Request $request Rest API request. | |
| 268 | + * @param \WP_REST_Request $request Rest API request. | |
| 252 | 269 | * @return \WP_Error|\WP_REST_Response |
| 253 | 270 | */ |
| 254 | 271 | public function table_delete( $request ) { |
| 255 | 272 | $dbs = $request->get_param( 'dbs' ); |
| @@ -276,12 +293,13 @@ | ||
| 276 | 293 | |
| 277 | 294 | /** |
| 278 | 295 | * Database table query to populate a list of values for a specific table/column. |
| 279 | 296 | * |
| 280 | - * @param WP_REST_Request $request Rest API request. | |
| 297 | + * @param \WP_REST_Request $request Rest API request. | |
| 281 | 298 | * @return \WP_Error|\WP_REST_Response |
| 282 | 299 | */ |
| 283 | 300 | public function table_lov( $request ) { |
| 301 | + return null; | |
| 284 | 302 | } |
| 285 | 303 | |
| 286 | 304 | /** |
| 287 | 305 | * Database table query. |
| @@ -287,9 +305,9 @@ | ||
| 287 | 305 | * Database table query. |
| 288 | 306 | * |
| 289 | 307 | * Supports: searching, ordering and pagination. |
| 290 | 308 | * |
| 291 | - * @param WP_REST_Request $request Rest API request. | |
| 309 | + * @param \WP_REST_Request $request Rest API request. | |
| 292 | 310 | * @return \WP_Error|\WP_REST_Response |
| 293 | 311 | */ |
| 294 | 312 | public function table_select( $request ) { |
| 295 | 313 | $dbs = $request->get_param( 'dbs' ); |
| @@ -305,8 +323,9 @@ | ||
| 305 | 323 | $row_count = $request->get_param( 'row_count' ); |
| 306 | 324 | $row_count_estimate = $request->get_param( 'row_count_estimate' ); |
| 307 | 325 | $media = $request->get_param( 'media' ); |
| 308 | 326 | $client_side = '1' === $request->get_param( 'client_side' ); |
| 327 | + $global_search = $request->get_param( 'global_search' ); | |
| 309 | 328 | if ( $this->check_table_access( |
| 310 | 329 | $dbs, |
| 311 | 330 | $tbl, |
| 312 | 331 | $request, |
| @@ -331,9 +350,12 @@ | ||
| 331 | 350 | array(), |
| 332 | 351 | array(), |
| 333 | 352 | array(), |
| 334 | 353 | $search_data_types, |
| 335 | - $client_side | |
| 354 | + $client_side, | |
| 355 | + array(), | |
| 356 | + array(), | |
| 357 | + $global_search | |
| 336 | 358 | ); |
| 337 | 359 | } else { |
| 338 | 360 | if ( 'rest_cookie_invalid_nonce' === $msg ) { |
| 339 | 361 | return $this->invalid_nonce(); |
| @@ -370,8 +392,9 @@ | ||
| 370 | 392 | $md = array(), |
| 371 | 393 | $m2m_relationship = array(), |
| 372 | 394 | $search_data_types = array() |
| 373 | 395 | ) { |
| 396 | + return null; | |
| 374 | 397 | } |
| 375 | 398 | |
| 376 | 399 | public function lookup( |
| 377 | 400 | $dbs, |
| @@ -412,21 +435,48 @@ | ||
| 412 | 435 | } |
| 413 | 436 | } |
| 414 | 437 | $dynamic_where = array(); |
| 415 | 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 | + } | |
| 416 | 447 | foreach ( $column_dynamic_values as $key => $value ) { |
| 417 | - $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) ); | |
| 418 | 452 | } |
| 419 | 453 | $where .= (( '' === $where ? ' where ' : ' and ' )) . ' (' . implode( ' and ', $dynamic_where ) . ') '; |
| 420 | 454 | } |
| 455 | + $column_count = ( '' === $subquery ? '' : ", stats.total_rows as 'count'" ); | |
| 421 | 456 | if ( strpos( $column_value, ',' ) !== false ) { |
| 422 | 457 | $columns = explode( ',', $column_value ); |
| 423 | - $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 | + ) ); | |
| 424 | 467 | } else { |
| 425 | - $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 | + ) ); | |
| 426 | 475 | } |
| 427 | - $sql .= " {$where} order by 2 "; | |
| 428 | - // $where already sanitized | |
| 476 | + $orderby = ' order by 2 '; | |
| 477 | + $sql .= " {$where} {$orderby} "; | |
| 478 | + // $where and $orderby already sanitized and prepared | |
| 429 | 479 | $dataset = $wpdadb->get_results( $sql, 'OBJECT' ); |
| 430 | 480 | $wpdadb->suppress_errors( $suppress ); |
| 431 | 481 | // Send response. |
| 432 | 482 | if ( '' === $wpdadb->last_error ) { |
| @@ -468,11 +518,12 @@ | ||
| 468 | 518 | public function get( |
| 469 | 519 | $dbs, |
| 470 | 520 | $tbl, |
| 471 | 521 | $primary_key, |
| 472 | - $media_columns, | |
| 522 | + $media_columns = array(), | |
| 473 | 523 | $column_names = array(), |
| 474 | - $default_where = '' | |
| 524 | + $default_where = '', | |
| 525 | + $docs = array() | |
| 475 | 526 | ) { |
| 476 | 527 | $wpdadb = WPDADB::get_db_connection( $dbs ); |
| 477 | 528 | if ( null === $wpdadb ) { |
| 478 | 529 | // Error connecting. |
| @@ -500,10 +551,12 @@ | ||
| 500 | 551 | // Prepare selected column list |
| 501 | 552 | $columns_selected = array(); |
| 502 | 553 | $search_data_types = array(); |
| 503 | 554 | foreach ( $table_columns as $table_column ) { |
| 504 | - $columns_selected[$table_column['column_name']] = true; | |
| 505 | - $search_data_types[$table_column['column_name']] = $table_column['data_type']; | |
| 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 | + } | |
| 506 | 559 | } |
| 507 | 560 | $selected_columns = $this->get_selected_columns( $columns_selected, $search_data_types ); |
| 508 | 561 | $sql = $wpdadb->prepare( "\n select {$selected_columns}\n from `%1s`\n {$where}\n ", array($tbl) ); |
| 509 | 562 | $dataset = $wpdadb->get_results( $sql, 'ARRAY_A' ); |
| @@ -520,9 +573,9 @@ | ||
| 520 | 573 | } |
| 521 | 574 | $wpdadb->suppress_errors( $suppress ); |
| 522 | 575 | // Send response. |
| 523 | 576 | $media = array(); |
| 524 | - if ( 0 < count( $media_columns ) ) { | |
| 577 | + if ( is_array( $media_columns ) && 0 < count( $media_columns ) ) { | |
| 525 | 578 | foreach ( $media_columns as $media_column_name => $media_column_type ) { |
| 526 | 579 | if ( isset( $dataset[0][$media_column_name] ) ) { |
| 527 | 580 | if ( in_array( $media_column_type, [ |
| 528 | 581 | 'WP-Image', |
| @@ -535,23 +588,20 @@ | ||
| 535 | 588 | } |
| 536 | 589 | } |
| 537 | 590 | } |
| 538 | 591 | $context = array(); |
| 592 | + // Add media | |
| 539 | 593 | $context['media'] = $media; |
| 540 | 594 | if ( isset( $debug['debug'] ) && 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) { |
| 541 | 595 | $context['debug'] = $debug['debug']; |
| 542 | 596 | } |
| 543 | 597 | if ( 0 === count( $dataset ) ) { |
| 544 | - return $this->WPDA_Rest_Response( 'No data found', $dataset, array( | |
| 545 | - 'debug' => $debug['debug'], | |
| 546 | - ) ); | |
| 598 | + return $this->WPDA_Rest_Response( 'No data found', $dataset, $context ); | |
| 547 | 599 | } else { |
| 548 | 600 | if ( 1 === count( $dataset ) ) { |
| 549 | 601 | return $this->WPDA_Rest_Response( '', $dataset, $context ); |
| 550 | 602 | } else { |
| 551 | - return $this->WPDA_Rest_Response( 'Query returned more than one row', $dataset, array( | |
| 552 | - 'debug' => $debug['debug'], | |
| 553 | - ) ); | |
| 603 | + return $this->WPDA_Rest_Response( 'Query returned more than one row', $dataset, $context ); | |
| 554 | 604 | } |
| 555 | 605 | } |
| 556 | 606 | } |
| 557 | 607 | } |
| @@ -761,8 +811,9 @@ | ||
| 761 | 811 | return $sql; |
| 762 | 812 | } |
| 763 | 813 | |
| 764 | 814 | private function get_md( $md, $wpdadb, $m2m_relationship ) { |
| 815 | + return null; | |
| 765 | 816 | } |
| 766 | 817 | |
| 767 | 818 | private function get_global_filter( |
| 768 | 819 | $wpdadb, |
| @@ -804,8 +855,9 @@ | ||
| 804 | 855 | $lookups, |
| 805 | 856 | $m2m_relationship, |
| 806 | 857 | $search_data_types |
| 807 | 858 | ) { |
| 859 | + return null; | |
| 808 | 860 | } |
| 809 | 861 | |
| 810 | 862 | private function get_where( |
| 811 | 863 | $wpdadb, |
| @@ -817,9 +869,10 @@ | ||
| 817 | 869 | $lookups, |
| 818 | 870 | $search_columns, |
| 819 | 871 | $search_column_fns, |
| 820 | 872 | $search_data_types, |
| 821 | - $geo_radius = array() | |
| 873 | + $geo_radius = array(), | |
| 874 | + $operator = 'and' | |
| 822 | 875 | ) { |
| 823 | 876 | // Default where. |
| 824 | 877 | if ( '' !== trim( $default_where ) && 'where' !== strtolower( substr( trim( $default_where ), 0, 5 ) ) ) { |
| 825 | 878 | $where = "where {$default_where}"; |
| @@ -859,8 +912,16 @@ | ||
| 859 | 912 | return $where; |
| 860 | 913 | } |
| 861 | 914 | |
| 862 | 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 | + } | |
| 863 | 924 | // Check for geo columns |
| 864 | 925 | $geometryColumns = array(); |
| 865 | 926 | if ( is_array( $search_data_types ) ) { |
| 866 | 927 | foreach ( $search_data_types as $column_name => $search_data_type ) { |
| @@ -909,9 +970,9 @@ | ||
| 909 | 970 | $search_column_fns, |
| 910 | 971 | $sorting, |
| 911 | 972 | $last_row_count, |
| 912 | 973 | $row_count_estimate, |
| 913 | - $media_columns, | |
| 974 | + $media_columns = array(), | |
| 914 | 975 | $default_where = '', |
| 915 | 976 | $default_orderby = '', |
| 916 | 977 | $lookups = array(), |
| 917 | 978 | $md = array(), |
| @@ -917,9 +978,11 @@ | ||
| 917 | 978 | $md = array(), |
| 918 | 979 | $m2m_relationship = array(), |
| 919 | 980 | $search_data_types = array(), |
| 920 | 981 | $client_side = false, |
| 921 | - $geo_radius = array() | |
| 982 | + $geo_radius = array(), | |
| 983 | + $docs = array(), | |
| 984 | + $search_global = null | |
| 922 | 985 | ) { |
| 923 | 986 | $wpdadb = WPDADB::get_db_connection( $dbs ); |
| 924 | 987 | if ( null === $wpdadb ) { |
| 925 | 988 | // Error connecting. |
| @@ -939,10 +1002,31 @@ | ||
| 939 | 1002 | $lookups, |
| 940 | 1003 | $search_columns, |
| 941 | 1004 | $search_column_fns, |
| 942 | 1005 | $search_data_types, |
| 943 | - $geo_radius | |
| 1006 | + $geo_radius, | |
| 1007 | + 'and' | |
| 944 | 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 | + } | |
| 945 | 1029 | // Build order by. |
| 946 | 1030 | $sqlorder = ''; |
| 947 | 1031 | if ( is_array( $sorting ) && 0 < count( $sorting ) ) { |
| 948 | 1032 | foreach ( $sorting as $sort ) { |
| @@ -950,9 +1034,31 @@ | ||
| 950 | 1034 | $sqlorder = 'order by '; |
| 951 | 1035 | } else { |
| 952 | 1036 | $sqlorder .= ','; |
| 953 | 1037 | } |
| 954 | - $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 | + } | |
| 955 | 1061 | } |
| 956 | 1062 | } |
| 957 | 1063 | if ( '' === $sqlorder && '' !== trim( $default_orderby ) ) { |
| 958 | 1064 | $sqlorder = $default_orderby; |
| @@ -1028,9 +1134,9 @@ | ||
| 1028 | 1134 | $context = array(); |
| 1029 | 1135 | if ( 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) { |
| 1030 | 1136 | $context['debug'] = $debug; |
| 1031 | 1137 | } |
| 1032 | - if ( 0 < count( $media_columns ) ) { | |
| 1138 | + if ( is_array( $media_columns ) && 0 < count( $media_columns ) ) { | |
| 1033 | 1139 | // Handle WP media library |
| 1034 | 1140 | $media = array(); |
| 1035 | 1141 | for ($i = 0; $i < count( $dataset ); $i++) { |
| 1036 | 1142 | $media_row = array(); |
| @@ -1092,8 +1198,9 @@ | ||
| 1092 | 1198 | $search_value, |
| 1093 | 1199 | $m2m_relationship = array(), |
| 1094 | 1200 | $search_data_types = array() |
| 1095 | 1201 | ) { |
| 1202 | + return null; | |
| 1096 | 1203 | } |
| 1097 | 1204 | |
| 1098 | 1205 | public static function add_condition( $where_lines, $operand = 'and' ) { |
| 1099 | 1206 | if ( 0 < count( array_filter( $where_lines ) ) ) { |
| @@ -1109,9 +1216,9 @@ | ||
| 1109 | 1216 | * |
| 1110 | 1217 | * @param string $dbs Database schema name. |
| 1111 | 1218 | * @param string $tbl Database table name. |
| 1112 | 1219 | * @param string $waa With admin actions. |
| 1113 | - * @return array\object | |
| 1220 | + * @return array | object | |
| 1114 | 1221 | */ |
| 1115 | 1222 | public function get_table_meta_data( $dbs, $tbl, $waa ) { |
| 1116 | 1223 | $sql_create_table = ''; |
| 1117 | 1224 | if ( WPDA::current_user_is_admin() ) { |
| @@ -1161,18 +1268,21 @@ | ||
| 1161 | 1268 | $wp_nonce_refresh = null; |
| 1162 | 1269 | $connect = null; |
| 1163 | 1270 | global $wpdb; |
| 1164 | 1271 | $settings->wp = [ |
| 1165 | - 'roles' => $this->get_wp_roles(), | |
| 1166 | - 'users' => $this->get_wp_users(), | |
| 1167 | - 'home' => admin_url( 'admin.php' ), | |
| 1168 | - 'homea' => admin_url( 'admin-ajax.php' ), | |
| 1169 | - 'tables' => array_values( $wpdb->tables() ), | |
| 1170 | - 'date_format' => get_option( 'date_format' ), | |
| 1171 | - 'time_format' => get_option( 'time_format' ), | |
| 1172 | - 'alter' => $wp_nonce_alter, | |
| 1173 | - 'refresh' => $wp_nonce_refresh, | |
| 1174 | - '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' ), | |
| 1175 | 1285 | ]; |
| 1176 | 1286 | if ( true === $waa ) { |
| 1177 | 1287 | $settings->wp['aonce'] = implode( '-', array( |
| 1178 | 1288 | wp_create_nonce( 'wpda-export-' . json_encode( $tbl ) ), |
| @@ -1179,26 +1289,34 @@ | ||
| 1179 | 1289 | // Table export |
| 1180 | 1290 | wp_create_nonce( 'wpda-rename-' . $tbl ), |
| 1181 | 1291 | ) ); |
| 1182 | 1292 | } |
| 1183 | - $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 | + } | |
| 1184 | 1301 | } |
| 1185 | 1302 | return array( |
| 1186 | - 'columns' => $columns->get_table_columns(), | |
| 1187 | - 'table_labels' => $columns->get_table_header_labels(), | |
| 1188 | - 'form_labels' => $columns->get_table_column_headers(), | |
| 1189 | - 'primary_key' => $columns->get_table_primary_key(), | |
| 1190 | - 'access' => $access, | |
| 1191 | - 'settings' => $settings, | |
| 1192 | - 'media' => $media['media'], | |
| 1193 | - 'wp_media' => $media['wp_media'], | |
| 1194 | - 'table_info' => $this->get_table_info( $dbs, $tbl ), | |
| 1195 | - '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, | |
| 1196 | 1314 | ); |
| 1197 | 1315 | } |
| 1198 | 1316 | |
| 1199 | 1317 | private function get_table_access( $dbs, $tbl ) { |
| 1200 | - if ( current_user_can( 'manage_options' ) ) { | |
| 1318 | + if ( WPDA::current_user_is_admin() ) { | |
| 1201 | 1319 | // Check administrator rights |
| 1202 | 1320 | if ( is_admin() ) { |
| 1203 | 1321 | $access = WPDA_Dictionary_Access::check_table_access_backend( $dbs, $tbl, $done ); |
| 1204 | 1322 | } else { |
| @@ -1236,9 +1354,9 @@ | ||
| 1236 | 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'] ) ) { |
| 1237 | 1355 | return $table[$action]['methods']; |
| 1238 | 1356 | } |
| 1239 | 1357 | // Check authorized roles |
| 1240 | - 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'] ) ) ) { | |
| 1241 | 1359 | return $table[$action]['methods']; |
| 1242 | 1360 | } |
| 1243 | 1361 | } |
| 1244 | 1362 | } |
| @@ -1249,9 +1367,9 @@ | ||
| 1249 | 1367 | * Check if access is grant for requested database/table. |
| 1250 | 1368 | * |
| 1251 | 1369 | * @param string $dbs Remote or local database connection string. |
| 1252 | 1370 | * @param string $tbl Database table name. |
| 1253 | - * @param onject $request Request object. | |
| 1371 | + * @param object $request Request object. | |
| 1254 | 1372 | * @param string $action Possible values: select, insert, update, delete. |
| 1255 | 1373 | * @return bool |
| 1256 | 1374 | */ |
| 1257 | 1375 | private function check_table_access( |
| @@ -1276,9 +1394,9 @@ | ||
| 1276 | 1394 | $msg = __( 'Unauthorized', 'wp-data-access' ); |
| 1277 | 1395 | return false; |
| 1278 | 1396 | } else { |
| 1279 | 1397 | if ( !in_array( $request->get_method(), $tables[$dbs][$tbl][$action]['methods'] ) ) { |
| 1280 | - //phpcs:ignore - 8.1 proof | |
| 1398 | + // phpcs:ignore -- 8.1 proof | |
| 1281 | 1399 | $msg = __( 'Unauthorized', 'wp-data-access' ); |
| 1282 | 1400 | return false; |
| 1283 | 1401 | } |
| 1284 | 1402 | } |