*/ private function groups( array $tasks ): array { $map = array( 'posts' => array( __( 'Posts', 'all-in-one-utilities' ), array( 'revisions', 'auto_drafts', 'trashed_posts' ) ), 'comments' => array( __( 'Comments', 'all-in-one-utilities' ), array( 'spam_comments', 'trashed_comments', 'pingbacks_trackbacks' ) ), 'orphans' => array( __( 'Orphaned data', 'all-in-one-utilities' ), array( 'orphaned_postmeta', 'orphaned_commentmeta', 'orphaned_usermeta', 'orphaned_term_relationships' ) ), 'maintenance' => array( __( 'Maintenance', 'all-in-one-utilities' ), array( 'expired_transients', 'optimize_tables' ) ), ); $groups = array(); $placed = array(); foreach ( $map as $key => list( $label, $ids ) ) { $groups[ $key ] = array( 'label' => $label, 'tasks' => array(), ); foreach ( $ids as $id ) { foreach ( $tasks as $task ) { if ( $task->id === $id ) { $groups[ $key ]['tasks'][] = $task; $placed[] = $id; } } } } $other = array_values( array_filter( $tasks, static function ( CleanupTask $task ) use ( $placed ) { return ! in_array( $task->id, $placed, true ); } ) ); if ( $other ) { $groups['other'] = array( 'label' => __( 'Other', 'all-in-one-utilities' ), 'tasks' => $other, ); } return array_filter( $groups, static function ( array $group ) { return ! empty( $group['tasks'] ); } ); } /** * Size and reclaimable space of this site's own tables, in bytes. * * @return array{size: int, free: int, tables: int} */ private function database_size(): array { global $wpdb; $row = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- live figures for an admin tool. $wpdb->prepare( 'SELECT COUNT(*) AS tables, COALESCE(SUM(data_length + index_length), 0) AS size, COALESCE(SUM(data_free), 0) AS free FROM information_schema.TABLES WHERE table_schema = %s AND table_name LIKE %s', $wpdb->dbname, $wpdb->esc_like( $wpdb->prefix ) . '%' ) ); return array( 'size' => $row ? (int) $row->size : 0, 'free' => $row ? (int) $row->free : 0, 'tables' => $row ? (int) $row->tables : 0, ); } /** * Render the screen. * * @return void */ public function render(): void { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } $notice = get_transient( 'aiou_database_cleanup_notice_' . get_current_user_id() ); delete_transient( 'aiou_database_cleanup_notice_' . get_current_user_id() ); $tasks = CleanupTasks::all(); $counts = array(); foreach ( $tasks as $task ) { $counts[ $task->id ] = (int) ( $task->count )(); } $to_clean = 0; $dirty = 0; $content = 0; foreach ( $counts as $id => $count ) { if ( in_array( $id, self::MAINTENANCE, true ) ) { continue; } ++$content; $to_clean += $count; if ( $count > 0 ) { ++$dirty; } } $db = $this->database_size(); AdminShell::page_start( array( 'title' => __( 'Database Cleanup', 'all-in-one-utilities' ), 'description' => __( 'Remove leftover data WordPress no longer needs. Nothing runs automatically -- each category is only cleaned when you click its button.', 'all-in-one-utilities' ), 'class' => 'aiou-dbc', ) ); ?>

groups( $tasks ) as $key => $group ) : ?>

id ]; $maintenance = in_array( $task->id, self::MAINTENANCE, true ); $field_id = 'aiou-dbc-task-' . $task->id; ?>
/> 0 ) : ?> 0 && $task->preview ) : ?> 0 ) : ?>

__( 'You are not allowed to do this.', 'all-in-one-utilities' ) ), 403 ); } check_ajax_referer( 'aiou-database-cleanup-preview' ); $task = CleanupTasks::get( isset( $_POST['task'] ) ? sanitize_key( wp_unslash( $_POST['task'] ) ) : '' ); if ( ! $task || ! $task->preview ) { wp_send_json_error( array( 'message' => __( 'There is no preview for this category.', 'all-in-one-utilities' ) ), 400 ); } $maintenance = in_array( $task->id, self::MAINTENANCE, true ); $count = (int) ( $task->count )(); $preview = ( $task->preview )( self::PREVIEW_LIMIT ); $rows = array_values( (array) ( $preview['rows'] ?? array() ) ); $shown = count( $rows ); if ( $maintenance ) { $note = __( 'These tables are optimized. No data is deleted.', 'all-in-one-utilities' ); $button = __( 'Optimize', 'all-in-one-utilities' ); } else { $note = $shown < $count /* translators: 1: rows shown, 2: total rows */ ? sprintf( __( 'Showing the first %1$s of %2$s items. Cleaning deletes all of them permanently.', 'all-in-one-utilities' ), number_format_i18n( $shown ), number_format_i18n( $count ) ) /* translators: %s: number of items */ : sprintf( _n( 'This %s item is deleted permanently.', 'These %s items are deleted permanently.', $count, 'all-in-one-utilities' ), number_format_i18n( $count ) ); /* translators: %s: number of items */ $button = sprintf( _n( 'Clean %s item', 'Clean %s items', $count, 'all-in-one-utilities' ), number_format_i18n( $count ) ); } wp_send_json_success( array( 'task' => $task->id, 'title' => $task->label, 'maintenance' => $maintenance, 'count' => $count, 'columns' => (array) ( $preview['columns'] ?? array() ), 'rows' => $rows, 'note' => $note, 'button' => $button, 'confirm' => $maintenance ? __( 'Optimize all of this site\'s database tables now? No data is deleted, but it can take a moment on a large site.', 'all-in-one-utilities' ) : '', 'empty' => __( 'Nothing to clean in this category any more.', 'all-in-one-utilities' ), ) ); } /** * Handle a "Clean" (one category) or "Clean selected" submission. * * @return void */ public function handle_clean(): void { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } check_admin_referer( self::NONCE ); $task_id = isset( $_POST['task'] ) ? sanitize_key( wp_unslash( $_POST['task'] ) ) : ''; $redirect = wp_get_referer(); $redirect = $redirect ? $redirect : admin_url( 'admin.php?page=' . self::PAGE_SLUG ); $ids = 'selected' === $task_id ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['tasks'] ?? array() ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each id is sanitize_key()'d. : array( $task_id ); $tasks = array_filter( array_map( array( CleanupTasks::class, 'get' ), array_unique( $ids ) ) ); if ( ! $tasks ) { $this->notice( $redirect, 'error', 'selected' === $task_id ? __( 'Choose at least one category to clean.', 'all-in-one-utilities' ) : __( 'Unknown cleanup category.', 'all-in-one-utilities' ) ); } $affected = 0; $labels = array(); foreach ( $tasks as $task ) { $affected += (int) ( $task->clean )(); $labels[] = $task->label; } if ( 0 === $affected ) { $message = __( 'Nothing needed cleaning in the chosen categories.', 'all-in-one-utilities' ); } elseif ( 1 === count( $tasks ) ) { /* translators: 1: number of items affected, 2: category label */ $message = sprintf( __( 'Cleaned %1$s item(s) from "%2$s".', 'all-in-one-utilities' ), number_format_i18n( $affected ), $labels[0] ); } else { /* translators: 1: number of items affected, 2: number of categories */ $message = sprintf( __( 'Cleaned %1$s item(s) across %2$s categories.', 'all-in-one-utilities' ), number_format_i18n( $affected ), number_format_i18n( count( $tasks ) ) ); } $this->notice( $redirect, 'success', $message ); } /** * Store a one-time notice and redirect back to the cleanup screen. * * @param string $redirect Redirect URL. * @param string $type 'success' or 'error'. * @param string $message Notice text. * @return never */ private function notice( string $redirect, string $type, string $message ) { set_transient( 'aiou_database_cleanup_notice_' . get_current_user_id(), array( 'type' => $type, 'message' => $message, ), MINUTE_IN_SECONDS ); wp_safe_redirect( $redirect ); exit; } }