class-adbc-automation-endpoints.php
3 months ago
class-adbc-common-endpoints.php
4 months ago
class-adbc-cron-jobs-endpoints.php
3 months ago
class-adbc-general-cleanup-endpoints.php
3 months ago
class-adbc-info-endpoints.php
3 months ago
class-adbc-logs-endpoints.php
3 months ago
class-adbc-options-endpoints.php
3 months ago
class-adbc-post-types-endpoints.php
3 months ago
class-adbc-posts-meta-endpoints.php
3 months ago
class-adbc-settings-endpoints.php
3 months ago
class-adbc-tables-endpoints.php
3 months ago
class-adbc-transients-endpoints.php
3 months ago
class-adbc-users-meta-endpoints.php
3 months ago
class-adbc-tables-endpoints.php
491 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC Tables Endpoints. |
| 9 | * |
| 10 | * This class provides the endpoints (controllers) for the tables routes. |
| 11 | */ |
| 12 | class ADBC_Tables_Endpoints { |
| 13 | |
| 14 | /** |
| 15 | * Get the tables list. |
| 16 | * |
| 17 | * @param WP_REST_Request $filters_request The request with the filters. |
| 18 | * @return WP_REST_Response The list of tables. |
| 19 | */ |
| 20 | public static function get_tables_list( WP_REST_Request $filters_request ) { |
| 21 | |
| 22 | try { |
| 23 | |
| 24 | $filters = ADBC_Common_Validator::sanitize_filters( $filters_request ); |
| 25 | $rest_response = ADBC_Tables::get_tables_list( $filters ); |
| 26 | return $rest_response; |
| 27 | |
| 28 | } catch (Throwable $e) { |
| 29 | |
| 30 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 31 | |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the names of all tables. |
| 37 | * |
| 38 | * @return WP_REST_Response The response with the tables names. |
| 39 | */ |
| 40 | public static function get_tables_names() { |
| 41 | |
| 42 | try { |
| 43 | |
| 44 | $show_tables_with_invalid_prefix = ADBC_Settings::instance()->get_setting( 'show_tables_with_invalid_prefix' ) === '1'; |
| 45 | $tables_names = ADBC_Tables::get_tables_names( PHP_INT_MAX, 0, true, $show_tables_with_invalid_prefix ); |
| 46 | |
| 47 | return ADBC_Rest::success( "", $tables_names ); |
| 48 | |
| 49 | } catch (Throwable $e) { |
| 50 | |
| 51 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 52 | |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Edit scan results of tables. |
| 58 | * |
| 59 | * @param WP_REST_Request $request_data The request with the tables to edit. |
| 60 | * @return WP_REST_Response The response. |
| 61 | */ |
| 62 | public static function edit_scan_results_tables( WP_REST_Request $request_data ) { |
| 63 | |
| 64 | try { |
| 65 | |
| 66 | return ADBC_Scan_Utils::edit_scan_results( $request_data, 'edit_scan_results_tables', 'tables' ); |
| 67 | |
| 68 | } catch (Throwable $e) { |
| 69 | |
| 70 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 71 | |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Optimize tables. |
| 77 | * |
| 78 | * @param WP_REST_Request $request_data The request with the tables to optimize. |
| 79 | * @return WP_REST_Response The response. |
| 80 | */ |
| 81 | public static function optimize_tables( WP_REST_Request $request_data ) { |
| 82 | |
| 83 | try { |
| 84 | |
| 85 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "optimize_tables", "tables", $request_data, true ); |
| 86 | |
| 87 | // If $validation_answer is not an array, it means that the validation failed and we have an error message. |
| 88 | if ( ! is_array( $validation_answer ) ) |
| 89 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 90 | |
| 91 | // Create an array containing only the table names. |
| 92 | $tables_names = array_column( $validation_answer, 'name' ); |
| 93 | $not_processed = ADBC_Tables::optimize_tables( $tables_names ); // Optimize the tables |
| 94 | |
| 95 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 96 | |
| 97 | } catch (Throwable $e) { |
| 98 | |
| 99 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Refresh counts of tables (ANALYZE). |
| 106 | * |
| 107 | * @param WP_REST_Request $request_data The request with the tables to analyze. |
| 108 | * @return WP_REST_Response The response. |
| 109 | */ |
| 110 | public static function refresh_counts_tables( WP_REST_Request $request_data ) { |
| 111 | |
| 112 | try { |
| 113 | |
| 114 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "refresh_counts_tables", "tables", $request_data, true ); |
| 115 | |
| 116 | // If $validation_answer is not an array, it means that the validation failed and we have an error message. |
| 117 | if ( ! is_array( $validation_answer ) ) |
| 118 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 119 | |
| 120 | // Create an array containing only the table names. |
| 121 | $tables_names = array_column( $validation_answer, 'name' ); |
| 122 | $not_processed = ADBC_Tables::refresh_tables_counts( $tables_names ); |
| 123 | |
| 124 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 125 | |
| 126 | } catch (Throwable $e) { |
| 127 | |
| 128 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 129 | |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Convert a single table to InnoDB. |
| 135 | * |
| 136 | * @param WP_REST_Request $request_data The request with the table to convert (selectedItems with one table). |
| 137 | * @return WP_REST_Response The response. |
| 138 | */ |
| 139 | public static function convert_to_innodb_tables( WP_REST_Request $request_data ) { |
| 140 | |
| 141 | try { |
| 142 | |
| 143 | if ( ! ADBC_Tables::is_innodb_supported() ) |
| 144 | return ADBC_Rest::error( __( 'The InnoDB storage engine is not supported by your MySQL server. Conversion cannot be performed.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST ); |
| 145 | |
| 146 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "convert_to_innodb_tables", "tables", $request_data, true ); |
| 147 | |
| 148 | if ( ! is_array( $validation_answer ) ) |
| 149 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 150 | |
| 151 | if ( count( $validation_answer ) !== 1 ) |
| 152 | return ADBC_Rest::error( 'Only one table can be converted per request.', ADBC_Rest::BAD_REQUEST ); |
| 153 | |
| 154 | $table_name = $validation_answer[0]['name']; |
| 155 | |
| 156 | // If setting is enabled, reject protected WordPress core tables. |
| 157 | if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) { |
| 158 | |
| 159 | $cleaned = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'tables', 'wp' ); |
| 160 | |
| 161 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) |
| 162 | $cleaned = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned, 'tables' ); |
| 163 | |
| 164 | if ( empty( $cleaned ) ) |
| 165 | return ADBC_Rest::error( |
| 166 | __( 'This table cannot be converted because it belongs to WordPress core and is protected.', 'advanced-database-cleaner' ), |
| 167 | ADBC_Rest::BAD_REQUEST, |
| 168 | 0, |
| 169 | [ |
| 170 | "message_links" => [ |
| 171 | [ |
| 172 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 173 | "tab_id" => "settings", |
| 174 | "anchor_id" => "other_settings", |
| 175 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 176 | ] |
| 177 | ] |
| 178 | ] |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | $result = ADBC_Tables::convert_tables_to_innodb( [ $table_name ] ); |
| 183 | |
| 184 | if ( $result['skipped_locked'] > 0 ) |
| 185 | return ADBC_Rest::error( __( 'This table is currently being converted in the background. Please check back later.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST ); |
| 186 | |
| 187 | if ( ! empty( $result['not_converted'] ) ) |
| 188 | return ADBC_Rest::error( __( 'The table could not be converted. Please try again later.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST ); |
| 189 | |
| 190 | return ADBC_Rest::success( "", 0 ); |
| 191 | |
| 192 | } catch (Throwable $e) { |
| 193 | |
| 194 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 195 | |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Empty rows of tables. |
| 201 | * |
| 202 | * @param WP_REST_Request $request_data The request with the tables to empty. |
| 203 | * @return WP_REST_Response The response. |
| 204 | */ |
| 205 | public static function empty_rows_tables( WP_REST_Request $request_data ) { |
| 206 | |
| 207 | try { |
| 208 | |
| 209 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "empty_rows_tables", "tables", $request_data, true ); |
| 210 | |
| 211 | // If $validation_answer is not an array, it means that the validation failed and we have an error message. |
| 212 | if ( ! is_array( $validation_answer ) ) |
| 213 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 214 | |
| 215 | $cleaned_tables = $validation_answer; |
| 216 | |
| 217 | // Conditionally exclude WordPress tables. |
| 218 | if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) { |
| 219 | |
| 220 | $cleaned_tables = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'tables', 'wp' ); |
| 221 | |
| 222 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) |
| 223 | $cleaned_tables = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_tables, 'tables' ); |
| 224 | |
| 225 | if ( empty( $cleaned_tables ) ) |
| 226 | return ADBC_Rest::error( |
| 227 | __( 'The selected tables could not be emptied because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 228 | ADBC_Rest::BAD_REQUEST, |
| 229 | 0, |
| 230 | [ |
| 231 | "message_links" => [ |
| 232 | [ |
| 233 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 234 | "tab_id" => "settings", |
| 235 | "anchor_id" => "other_settings", |
| 236 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 237 | ] |
| 238 | ] |
| 239 | ] |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | // Create an array containing only the table names. |
| 244 | $tables_names = array_column( $cleaned_tables, 'name' ); |
| 245 | $not_processed = ADBC_Tables::empty_tables( $tables_names ); // Empty the tables |
| 246 | |
| 247 | if ( count( $cleaned_tables ) < count( $validation_answer ) ) { |
| 248 | return ADBC_Rest::success( |
| 249 | __( 'Some tables were emptied successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 250 | count( $not_processed ), |
| 251 | [ |
| 252 | "message_links" => [ |
| 253 | [ |
| 254 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 255 | "tab_id" => "settings", |
| 256 | "anchor_id" => "other_settings", |
| 257 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 258 | ] |
| 259 | ] |
| 260 | ] |
| 261 | ); |
| 262 | } |
| 263 | |
| 264 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 265 | |
| 266 | } catch (Throwable $e) { |
| 267 | |
| 268 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 269 | |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Delete tables. |
| 275 | * |
| 276 | * @param WP_REST_Request $request_data The request with the tables to delete. |
| 277 | * @return WP_REST_Response The response. |
| 278 | */ |
| 279 | public static function delete_tables( WP_REST_Request $request_data ) { |
| 280 | |
| 281 | try { |
| 282 | |
| 283 | // Verify if there is a scan in progress. If there is, return an error to prevent conflicts. |
| 284 | if ( ADBC_VERSION_TYPE === 'PREMIUM' && ADBC_Scan_Utils::is_scan_exists( 'tables' ) ) |
| 285 | return ADBC_Rest::error( __( 'A scan is in progress. Please wait until it finishes before performing this action.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST ); |
| 286 | |
| 287 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "delete_tables", "tables", $request_data, true ); |
| 288 | |
| 289 | // If $validation_answer is not an array, it means that the validation failed and we have an error message. |
| 290 | if ( ! is_array( $validation_answer ) ) |
| 291 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 292 | |
| 293 | $cleaned_tables = $validation_answer; |
| 294 | |
| 295 | // Conditionally exclude WordPress tables. |
| 296 | if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) { |
| 297 | |
| 298 | $cleaned_tables = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'tables', 'wp' ); |
| 299 | |
| 300 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) |
| 301 | $cleaned_tables = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_tables, 'tables' ); |
| 302 | |
| 303 | if ( empty( $cleaned_tables ) ) |
| 304 | return ADBC_Rest::error( |
| 305 | __( 'The selected tables could not be deleted because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 306 | ADBC_Rest::BAD_REQUEST, |
| 307 | 0, |
| 308 | [ |
| 309 | "message_links" => [ |
| 310 | [ |
| 311 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 312 | "tab_id" => "settings", |
| 313 | "anchor_id" => "other_settings", |
| 314 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 315 | ] |
| 316 | ] |
| 317 | ] |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | // Create an array containing only the table names. |
| 322 | $tables_names = array_column( $cleaned_tables, 'name' ); |
| 323 | $not_processed = ADBC_Tables::delete_tables( $tables_names ); |
| 324 | |
| 325 | // Delete the tables from the scan results |
| 326 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) |
| 327 | ADBC_Scan_Utils::update_scan_results_file_after_deletion( 'tables', $tables_names, $not_processed ); |
| 328 | |
| 329 | if ( count( $cleaned_tables ) < count( $validation_answer ) ) { |
| 330 | return ADBC_Rest::success( |
| 331 | __( 'Some tables were deleted successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 332 | count( $not_processed ), |
| 333 | [ |
| 334 | "message_links" => [ |
| 335 | [ |
| 336 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 337 | "tab_id" => "settings", |
| 338 | "anchor_id" => "other_settings", |
| 339 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 340 | ] |
| 341 | ] |
| 342 | ] |
| 343 | ); |
| 344 | } |
| 345 | |
| 346 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 347 | |
| 348 | } catch (Throwable $e) { |
| 349 | |
| 350 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 351 | |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Repair tables. |
| 357 | * |
| 358 | * @param WP_REST_Request $request_data The request with the tables to repair. |
| 359 | * @return WP_REST_Response The response. |
| 360 | */ |
| 361 | public static function repair_tables( WP_REST_Request $request_data ) { |
| 362 | |
| 363 | try { |
| 364 | |
| 365 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "repair_tables", "tables", $request_data, true ); |
| 366 | |
| 367 | // If $validation_answer is not an array, it means that the validation failed and we have an error message. |
| 368 | if ( ! is_array( $validation_answer ) ) |
| 369 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 370 | |
| 371 | // Create an array containing only the table names. |
| 372 | $tables_names = array_column( $validation_answer, 'name' ); |
| 373 | $not_processed = ADBC_Tables::repair_tables( $tables_names ); // Repair the tables |
| 374 | |
| 375 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 376 | |
| 377 | } catch (Throwable $e) { |
| 378 | |
| 379 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 380 | |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Count the total number of tables that are not scanned. |
| 386 | * |
| 387 | * @return WP_REST_Response The response. |
| 388 | */ |
| 389 | public static function count_total_not_scanned_tables() { |
| 390 | try { |
| 391 | return ADBC_Rest::success( "", ADBC_Tables::count_total_not_scanned_tables() ); |
| 392 | } catch (Throwable $e) { |
| 393 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Count the total number of tables that are not repaired. |
| 399 | * |
| 400 | * @return WP_REST_Response The response. |
| 401 | */ |
| 402 | public static function count_total_tables_to_repair() { |
| 403 | try { |
| 404 | return ADBC_Rest::success( "", ADBC_Tables::count_total_tables_to_repair() ); |
| 405 | } catch (Throwable $e) { |
| 406 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Count the total number of tables that are not optimized. |
| 412 | * |
| 413 | * @return WP_REST_Response The response. |
| 414 | */ |
| 415 | public static function count_total_tables_to_optimize() { |
| 416 | try { |
| 417 | return ADBC_Rest::success( "", ADBC_Tables::count_total_tables_to_optimize() ); |
| 418 | } catch (Throwable $e) { |
| 419 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Count the total number of tables that have invalid prefix. |
| 425 | * |
| 426 | * @return WP_REST_Response The response. |
| 427 | */ |
| 428 | public static function count_total_tables_with_invalid_prefix() { |
| 429 | try { |
| 430 | return ADBC_Rest::success( "", ADBC_Tables::get_total_tables_with_invalid_prefix_count() ); |
| 431 | } catch (Throwable $e) { |
| 432 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Get the rows of a table. |
| 438 | * |
| 439 | * @param WP_REST_Request $request_data The request with the filters. |
| 440 | * @return WP_REST_Response The response with the rows of the table. |
| 441 | */ |
| 442 | public static function get_table_rows( WP_REST_Request $request_data ) { |
| 443 | |
| 444 | try { |
| 445 | |
| 446 | $filters = ADBC_Common_Validator::sanitize_filters( $request_data ); |
| 447 | $table_name = $request_data->get_param( 'tableName' ); |
| 448 | |
| 449 | if ( ! ADBC_Tables::is_table_exists( $table_name ) ) |
| 450 | return ADBC_Rest::error( 'The table does not exist.', ADBC_Rest::UNPROCESSABLE_ENTITY ); |
| 451 | |
| 452 | if ( ADBC_Tables::is_table_corrupted( $table_name ) ) |
| 453 | return ADBC_Rest::error( __( 'The table is corrupted and cannot be accessed.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST ); |
| 454 | |
| 455 | $data = ADBC_Tables::get_table_rows( $table_name, $filters ); |
| 456 | |
| 457 | return ADBC_Rest::success( "", $data ); |
| 458 | |
| 459 | } catch (Throwable $e) { |
| 460 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 461 | } |
| 462 | |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Get the structure of a table. |
| 467 | * |
| 468 | * @param WP_REST_Request $request_data The request with the table name. |
| 469 | * @return WP_REST_Response The response with the structure of the table. |
| 470 | */ |
| 471 | public static function get_table_structure( WP_REST_Request $request_data ) { |
| 472 | |
| 473 | try { |
| 474 | |
| 475 | $table_name = $request_data->get_param( 'tableName' ); |
| 476 | |
| 477 | if ( ! ADBC_Tables::is_table_exists( $table_name ) ) |
| 478 | return ADBC_Rest::error( 'The table does not exist.', ADBC_Rest::UNPROCESSABLE_ENTITY ); |
| 479 | |
| 480 | if ( ADBC_Tables::is_table_corrupted( $table_name ) ) |
| 481 | return ADBC_Rest::error( __( 'The table is corrupted and cannot be accessed.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST ); |
| 482 | |
| 483 | return ADBC_Rest::success( "", ADBC_Tables::get_table_structure( $table_name ) ); |
| 484 | |
| 485 | } catch (Throwable $e) { |
| 486 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 487 | } |
| 488 | |
| 489 | } |
| 490 | |
| 491 | } |