id === $id ) { return $task; } } return null; } /** * Delete posts of a type matching $post_status one chunk at a time via * wp_delete_post(), up to the per-run safety cap. * * @param string $where SQL WHERE clause (no leading "WHERE"). * @return int Number of posts deleted. */ private static function purge_posts( string $where ): int { global $wpdb; return self::purge_in_chunks( static function ( int $chunk ) use ( $wpdb, $where ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $where is one of this file's own fixed WHERE fragments, never user input; $chunk still goes through a real placeholder below. return $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE {$where} LIMIT %d", $chunk ) ); }, static function ( int $id ) { return false !== wp_delete_post( $id, true ); } ); } /** * Delete comments matching $where one chunk at a time via * wp_delete_comment(), up to the per-run safety cap. * * @param string $where SQL WHERE clause (no leading "WHERE"). * @return int Number of comments deleted. */ private static function purge_comments( string $where ): int { global $wpdb; return self::purge_in_chunks( static function ( int $chunk ) use ( $wpdb, $where ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $where is one of this file's own fixed WHERE fragments, never user input; $chunk still goes through a real placeholder below. return $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE {$where} LIMIT %d", $chunk ) ); }, static function ( int $id ) { return (bool) wp_delete_comment( $id, true ); } ); } /** * Generic chunked purge loop: repeatedly fetch up to CHUNK ids via * $fetch_ids, deleting each via $delete_one, until there are none left * or the per-run safety cap is reached. * * @param callable $fetch_ids `(int $chunk): array` Next batch of ids still matching. * @param callable $delete_one `(int $id): bool` Delete a single id. * @return int Number successfully deleted. */ private static function purge_in_chunks( callable $fetch_ids, callable $delete_one ): int { $deleted = 0; while ( $deleted < self::MAX_PER_RUN ) { $remaining_budget = self::MAX_PER_RUN - $deleted; $chunk = min( self::CHUNK, $remaining_budget ); $ids = $fetch_ids( $chunk ); if ( empty( $ids ) ) { break; } foreach ( $ids as $id ) { if ( $delete_one( (int) $id ) ) { ++$deleted; } } } return $deleted; } /** * Shorten a stored value for display. * * @param mixed $value Raw value. * @param int $chars Maximum characters. * @return string */ private static function excerpt( $value, int $chars = 80 ): string { $value = trim( (string) preg_replace( '/\s+/', ' ', (string) $value ) ); return mb_strlen( $value ) > $chars ? mb_substr( $value, 0, $chars - 1 ) . '…' : $value; } /** * A database date in the site's date and time format. * * @param string $mysql_date `Y-m-d H:i:s`. * @return string */ private static function date( string $mysql_date ): string { return ( '' === $mysql_date || str_starts_with( $mysql_date, '0000' ) ) ? '' : (string) mysql2date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $mysql_date ); } /** * Titles of the given posts, by ID. * * @param int[] $ids Post IDs. * @return array */ private static function post_titles( array $ids ): array { global $wpdb; $ids = array_values( array_unique( array_filter( array_map( 'intval', $ids ) ) ) ); if ( ! $ids ) { return array(); } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per id, built just above. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE ID IN (" . implode( ',', array_fill( 0, count( $ids ), '%d' ) ) . ')', $ids ) ); $titles = array(); foreach ( (array) $rows as $row ) { $titles[ (int) $row->ID ] = (string) $row->post_title; } return $titles; } /** * Preview of the posts matching $where. * * @param string $where SQL WHERE clause (one of this file's fixed fragments). * @param int $limit Maximum rows. * @param bool $show_parent Show the parent post (for revisions) instead of the type. * @return array{columns: array, rows: array>} */ private static function preview_posts( string $where, int $limit, bool $show_parent = false ): array { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $where is one of this file's own fixed WHERE fragments, never user input. $posts = (array) $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title, post_type, post_parent, post_modified FROM {$wpdb->posts} WHERE {$where} ORDER BY ID DESC LIMIT %d", $limit ) ); $parents = $show_parent ? self::post_titles( wp_list_pluck( $posts, 'post_parent' ) ) : array(); $rows = array(); foreach ( $posts as $post ) { $row = array( 'id' => (string) $post->ID, 'title' => '' !== $post->post_title ? self::excerpt( $post->post_title ) : __( '(no title)', 'all-in-one-utilities' ), ); if ( $show_parent ) { $parent = (int) $post->post_parent; $row['parent'] = isset( $parents[ $parent ] ) ? self::excerpt( '' !== $parents[ $parent ] ? $parents[ $parent ] : __( '(no title)', 'all-in-one-utilities' ), 50 ) . ' (#' . $parent . ')' : '#' . $parent; } else { $row['type'] = (string) $post->post_type; } $row['date'] = self::date( (string) $post->post_modified ); $rows[] = $row; } return array( 'columns' => array( 'id' => __( 'ID', 'all-in-one-utilities' ), 'title' => __( 'Title', 'all-in-one-utilities' ), ( $show_parent ? 'parent' : 'type' ) => $show_parent ? __( 'Revision of', 'all-in-one-utilities' ) : __( 'Type', 'all-in-one-utilities' ), 'date' => __( 'Last modified', 'all-in-one-utilities' ), ), 'rows' => $rows, ); } /** * Preview of the comments matching $where. * * @param string $where SQL WHERE clause (one of this file's fixed fragments). * @param int $limit Maximum rows. * @return array{columns: array, rows: array>} */ private static function preview_comments( string $where, int $limit ): array { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $where is one of this file's own fixed WHERE fragments, never user input. $comments = (array) $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_author, comment_content, comment_post_ID, comment_date FROM {$wpdb->comments} WHERE {$where} ORDER BY comment_ID DESC LIMIT %d", $limit ) ); $titles = self::post_titles( wp_list_pluck( $comments, 'comment_post_ID' ) ); $rows = array(); foreach ( $comments as $comment ) { $post_id = (int) $comment->comment_post_ID; $rows[] = array( 'id' => (string) $comment->comment_ID, 'author' => self::excerpt( $comment->comment_author, 40 ), 'comment' => self::excerpt( wp_strip_all_tags( (string) $comment->comment_content ) ), 'post' => isset( $titles[ $post_id ] ) ? self::excerpt( $titles[ $post_id ], 40 ) : '#' . $post_id, 'date' => self::date( (string) $comment->comment_date ), ); } return array( 'columns' => array( 'id' => __( 'ID', 'all-in-one-utilities' ), 'author' => __( 'Author', 'all-in-one-utilities' ), 'comment' => __( 'Comment', 'all-in-one-utilities' ), 'post' => __( 'On', 'all-in-one-utilities' ), 'date' => __( 'Date', 'all-in-one-utilities' ), ), 'rows' => $rows, ); } /** * Preview of orphaned meta rows. * * @param string $sql This file's own fixed query, selecting id, object_id, meta_key, meta_value, with a LIMIT %d. * @param string $object_label Column label for the missing object's id. * @param int $limit Maximum rows. * @return array{columns: array, rows: array>} */ private static function preview_meta( string $sql, string $object_label, int $limit ): array { global $wpdb; $rows = array(); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $sql is one of this file's own fixed queries; its only variable part is the %d placeholder. foreach ( (array) $wpdb->get_results( $wpdb->prepare( $sql, $limit ) ) as $meta ) { $rows[] = array( 'id' => (string) $meta->id, 'object' => '#' . $meta->object_id, 'key' => self::excerpt( $meta->meta_key, 50 ), 'value' => self::excerpt( $meta->meta_value ), ); } return array( 'columns' => array( 'id' => __( 'Meta ID', 'all-in-one-utilities' ), 'object' => $object_label, 'key' => __( 'Key', 'all-in-one-utilities' ), 'value' => __( 'Value', 'all-in-one-utilities' ), ), 'rows' => $rows, ); } /** * Post revisions. * * @return CleanupTask */ private static function revisions(): CleanupTask { global $wpdb; $where = "post_type = 'revision'"; return new CleanupTask( id: 'revisions', label: __( 'Post Revisions', 'all-in-one-utilities' ), description: __( 'Old saved revisions of posts and pages.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $where ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- fixed literal WHERE clause above, never user input. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE {$where}" ); }, clean: static function () use ( $where ) { return self::purge_posts( $where ); }, preview: static function ( int $limit ) use ( $where ) { return self::preview_posts( $where, $limit, true ); } ); } /** * Auto-draft posts. * * @return CleanupTask */ private static function auto_drafts(): CleanupTask { global $wpdb; $where = "post_status = 'auto-draft'"; return new CleanupTask( id: 'auto_drafts', label: __( 'Auto Drafts', 'all-in-one-utilities' ), description: __( 'Empty drafts WordPress creates automatically when opening the editor.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $where ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- fixed literal WHERE clause above, never user input. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE {$where}" ); }, clean: static function () use ( $where ) { return self::purge_posts( $where ); }, preview: static function ( int $limit ) use ( $where ) { return self::preview_posts( $where, $limit ); } ); } /** * Trashed posts/pages (core post types only -- see core_post_types()). * * @return CleanupTask */ private static function trashed_posts(): CleanupTask { global $wpdb; $types = "'" . implode( "','", esc_sql( self::core_post_types() ) ) . "'"; $where = "post_status = 'trash' AND post_type IN ({$types})"; return new CleanupTask( id: 'trashed_posts', label: __( 'Trashed Posts', 'all-in-one-utilities' ), description: __( 'Posts and pages already in the Trash.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $where ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $where is built from a fixed, hardcoded list of post types (escaped), never user input. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE {$where}" ); }, clean: static function () use ( $where ) { return self::purge_posts( $where ); }, preview: static function ( int $limit ) use ( $where ) { return self::preview_posts( $where, $limit ); } ); } /** * Spam comments. * * @return CleanupTask */ private static function spam_comments(): CleanupTask { global $wpdb; $where = "comment_approved = 'spam'"; return new CleanupTask( id: 'spam_comments', label: __( 'Spam Comments', 'all-in-one-utilities' ), description: __( 'Comments marked as spam.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $where ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- fixed literal WHERE clause above, never user input. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE {$where}" ); }, clean: static function () use ( $where ) { return self::purge_comments( $where ); }, preview: static function ( int $limit ) use ( $where ) { return self::preview_comments( $where, $limit ); } ); } /** * Trashed comments. * * @return CleanupTask */ private static function trashed_comments(): CleanupTask { global $wpdb; $where = "comment_approved = 'trash'"; return new CleanupTask( id: 'trashed_comments', label: __( 'Trashed Comments', 'all-in-one-utilities' ), description: __( 'Comments already in the Trash.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $where ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- fixed literal WHERE clause above, never user input. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE {$where}" ); }, clean: static function () use ( $where ) { return self::purge_comments( $where ); }, preview: static function ( int $limit ) use ( $where ) { return self::preview_comments( $where, $limit ); } ); } /** * Pingbacks and trackbacks. * * @return CleanupTask */ private static function pingbacks_and_trackbacks(): CleanupTask { global $wpdb; $where = "comment_type IN ('pingback','trackback')"; return new CleanupTask( id: 'pingbacks_trackbacks', label: __( 'Pingbacks & Trackbacks', 'all-in-one-utilities' ), description: __( 'Automatic link notifications from other sites.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $where ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- fixed literal WHERE clause above, never user input. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE {$where}" ); }, clean: static function () use ( $where ) { return self::purge_comments( $where ); }, preview: static function ( int $limit ) use ( $where ) { return self::preview_comments( $where, $limit ); } ); } /** * Orphaned post meta: rows whose post no longer exists. * * @return CleanupTask */ private static function orphaned_postmeta(): CleanupTask { global $wpdb; $count_sql = "SELECT COUNT(*) FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.ID IS NULL"; $ids_sql = "SELECT pm.meta_id FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.ID IS NULL LIMIT %d"; $view_sql = "SELECT pm.meta_id AS id, pm.post_id AS object_id, pm.meta_key, pm.meta_value FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.ID IS NULL ORDER BY pm.meta_id DESC LIMIT %d"; return new CleanupTask( id: 'orphaned_postmeta', label: __( 'Orphaned Post Meta', 'all-in-one-utilities' ), description: __( 'Post metadata left behind after its post was deleted directly from the database.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $count_sql ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $count_sql is this method's own fixed query above, with no variable input to prepare. return (int) $wpdb->get_var( $count_sql ); }, clean: static function () use ( $wpdb, $ids_sql ) { return self::purge_in_chunks( static function ( int $chunk ) use ( $wpdb, $ids_sql ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $ids_sql is this method's own fixed query above (its only variable part is the %d placeholder prepare() fills in here). return $wpdb->get_col( $wpdb->prepare( $ids_sql, $chunk ) ); }, static function ( int $id ) { return (bool) delete_metadata_by_mid( 'post', $id ); } ); }, preview: static function ( int $limit ) use ( $view_sql ) { return self::preview_meta( $view_sql, __( 'Missing post', 'all-in-one-utilities' ), $limit ); } ); } /** * Orphaned comment meta: rows whose comment no longer exists. * * @return CleanupTask */ private static function orphaned_commentmeta(): CleanupTask { global $wpdb; $count_sql = "SELECT COUNT(*) FROM {$wpdb->commentmeta} cm LEFT JOIN {$wpdb->comments} c ON c.comment_ID = cm.comment_id WHERE c.comment_ID IS NULL"; $ids_sql = "SELECT cm.meta_id FROM {$wpdb->commentmeta} cm LEFT JOIN {$wpdb->comments} c ON c.comment_ID = cm.comment_id WHERE c.comment_ID IS NULL LIMIT %d"; $view_sql = "SELECT cm.meta_id AS id, cm.comment_id AS object_id, cm.meta_key, cm.meta_value FROM {$wpdb->commentmeta} cm LEFT JOIN {$wpdb->comments} c ON c.comment_ID = cm.comment_id WHERE c.comment_ID IS NULL ORDER BY cm.meta_id DESC LIMIT %d"; return new CleanupTask( id: 'orphaned_commentmeta', label: __( 'Orphaned Comment Meta', 'all-in-one-utilities' ), description: __( 'Comment metadata left behind after its comment was deleted directly from the database.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $count_sql ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $count_sql is this method's own fixed query above, with no variable input to prepare. return (int) $wpdb->get_var( $count_sql ); }, clean: static function () use ( $wpdb, $ids_sql ) { return self::purge_in_chunks( static function ( int $chunk ) use ( $wpdb, $ids_sql ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $ids_sql is this method's own fixed query above (its only variable part is the %d placeholder prepare() fills in here). return $wpdb->get_col( $wpdb->prepare( $ids_sql, $chunk ) ); }, static function ( int $id ) { return (bool) delete_metadata_by_mid( 'comment', $id ); } ); }, preview: static function ( int $limit ) use ( $view_sql ) { return self::preview_meta( $view_sql, __( 'Missing comment', 'all-in-one-utilities' ), $limit ); } ); } /** * Orphaned user meta: rows whose user no longer exists. * * @return CleanupTask */ private static function orphaned_usermeta(): CleanupTask { global $wpdb; $count_sql = "SELECT COUNT(*) FROM {$wpdb->usermeta} um LEFT JOIN {$wpdb->users} u ON u.ID = um.user_id WHERE u.ID IS NULL"; $ids_sql = "SELECT um.umeta_id FROM {$wpdb->usermeta} um LEFT JOIN {$wpdb->users} u ON u.ID = um.user_id WHERE u.ID IS NULL LIMIT %d"; $view_sql = "SELECT um.umeta_id AS id, um.user_id AS object_id, um.meta_key, um.meta_value FROM {$wpdb->usermeta} um LEFT JOIN {$wpdb->users} u ON u.ID = um.user_id WHERE u.ID IS NULL ORDER BY um.umeta_id DESC LIMIT %d"; return new CleanupTask( id: 'orphaned_usermeta', label: __( 'Orphaned User Meta', 'all-in-one-utilities' ), description: __( 'User metadata left behind after its user account was deleted directly from the database.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $count_sql ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $count_sql is this method's own fixed query above, with no variable input to prepare. return (int) $wpdb->get_var( $count_sql ); }, clean: static function () use ( $wpdb, $ids_sql ) { return self::purge_in_chunks( static function ( int $chunk ) use ( $wpdb, $ids_sql ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $ids_sql is this method's own fixed query above (its only variable part is the %d placeholder prepare() fills in here). return $wpdb->get_col( $wpdb->prepare( $ids_sql, $chunk ) ); }, static function ( int $id ) { return (bool) delete_metadata_by_mid( 'user', $id ); } ); }, preview: static function ( int $limit ) use ( $view_sql ) { return self::preview_meta( $view_sql, __( 'Missing user', 'all-in-one-utilities' ), $limit ); } ); } /** * Orphaned term relationships: rows linking to a post that no longer exists. * * @return CleanupTask */ private static function orphaned_term_relationships(): CleanupTask { global $wpdb; $count_sql = "SELECT COUNT(*) FROM {$wpdb->term_relationships} tr LEFT JOIN {$wpdb->posts} p ON p.ID = tr.object_id WHERE p.ID IS NULL"; $ids_sql = "SELECT tr.object_id FROM {$wpdb->term_relationships} tr LEFT JOIN {$wpdb->posts} p ON p.ID = tr.object_id WHERE p.ID IS NULL LIMIT %d"; $view_sql = "SELECT tr.object_id, t.name, tt.taxonomy FROM {$wpdb->term_relationships} tr LEFT JOIN {$wpdb->posts} p ON p.ID = tr.object_id LEFT JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id WHERE p.ID IS NULL ORDER BY tr.object_id DESC LIMIT %d"; return new CleanupTask( id: 'orphaned_term_relationships', label: __( 'Orphaned Term Relationships', 'all-in-one-utilities' ), description: __( 'Category/tag links left behind after the post they pointed to was deleted directly from the database.', 'all-in-one-utilities' ), count: static function () use ( $wpdb, $count_sql ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $count_sql is this method's own fixed query above, with no variable input to prepare. return (int) $wpdb->get_var( $count_sql ); }, clean: static function () use ( $wpdb, $ids_sql ) { return self::purge_in_chunks( static function ( int $chunk ) use ( $wpdb, $ids_sql ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $ids_sql is this method's own fixed query above (its only variable part is the %d placeholder prepare() fills in here). return $wpdb->get_col( $wpdb->prepare( $ids_sql, $chunk ) ); }, static function ( int $object_id ) use ( $wpdb ) { // These rows are already orphaned (their post is gone), so there's // no live object cache to invalidate -- a direct DELETE is safe and // avoids needing the taxonomy of an object that no longer exists. return (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->term_relationships} WHERE object_id = %d", $object_id ) ); } ); }, preview: static function ( int $limit ) use ( $wpdb, $view_sql ) { $rows = array(); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $view_sql is this method's own fixed query above (its only variable part is the %d placeholder). foreach ( (array) $wpdb->get_results( $wpdb->prepare( $view_sql, $limit ) ) as $row ) { $rows[] = array( 'object' => '#' . $row->object_id, 'term' => null !== $row->name ? self::excerpt( $row->name, 50 ) : __( '(deleted term)', 'all-in-one-utilities' ), 'taxonomy' => (string) $row->taxonomy, ); } return array( 'columns' => array( 'object' => __( 'Missing post', 'all-in-one-utilities' ), 'term' => __( 'Term', 'all-in-one-utilities' ), 'taxonomy' => __( 'Taxonomy', 'all-in-one-utilities' ), ), 'rows' => $rows, ); } ); } /** * Expired transients (uses WordPress core's own cleanup function). * * @return CleanupTask */ private static function expired_transients(): CleanupTask { global $wpdb; $count_sql = " SELECT COUNT(*) FROM ( SELECT a.option_id FROM {$wpdb->options} a INNER JOIN {$wpdb->options} b ON b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) WHERE a.option_name LIKE %s AND a.option_name NOT LIKE %s AND b.option_value < UNIX_TIMESTAMP() UNION ALL SELECT a.option_id FROM {$wpdb->options} a INNER JOIN {$wpdb->options} b ON b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) WHERE a.option_name LIKE %s AND a.option_name NOT LIKE %s AND b.option_value < UNIX_TIMESTAMP() ) expired "; $view_sql = " SELECT a.option_name, b.option_value AS expires, LENGTH( a.option_value ) AS size FROM {$wpdb->options} a INNER JOIN {$wpdb->options} b ON b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) WHERE a.option_name LIKE %s AND a.option_name NOT LIKE %s AND b.option_value < UNIX_TIMESTAMP() UNION ALL SELECT a.option_name, b.option_value AS expires, LENGTH( a.option_value ) AS size FROM {$wpdb->options} a INNER JOIN {$wpdb->options} b ON b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) WHERE a.option_name LIKE %s AND a.option_name NOT LIKE %s AND b.option_value < UNIX_TIMESTAMP() ORDER BY expires ASC LIMIT %d "; $count = static function () use ( $wpdb, $count_sql ) { return (int) $wpdb->get_var( $wpdb->prepare( $count_sql, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- this method's own fixed query above; every variable part goes through the %s placeholders that follow. $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%' ) ); }; return new CleanupTask( id: 'expired_transients', label: __( 'Expired Transients', 'all-in-one-utilities' ), description: __( 'Cached data past its own expiration time.', 'all-in-one-utilities' ), count: $count, clean: static function () use ( $count ) { $before = $count(); delete_expired_transients( true ); return $before; }, preview: static function ( int $limit ) use ( $wpdb, $view_sql ) { $found = $wpdb->get_results( $wpdb->prepare( $view_sql, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- this method's own fixed query above; every variable part goes through the placeholders that follow. $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', $limit ) ); $rows = array(); foreach ( (array) $found as $row ) { $rows[] = array( 'name' => self::excerpt( (string) preg_replace( '/^_(site_)?transient_/', '', (string) $row->option_name ), 60 ), /* translators: %s: time span, e.g. "3 days" */ 'expired' => sprintf( __( '%s ago', 'all-in-one-utilities' ), human_time_diff( (int) $row->expires ) ), 'size' => size_format( (int) $row->size ) ? size_format( (int) $row->size ) : '0 B', ); } return array( 'columns' => array( 'name' => __( 'Transient', 'all-in-one-utilities' ), 'expired' => __( 'Expired', 'all-in-one-utilities' ), 'size' => __( 'Size', 'all-in-one-utilities' ), ), 'rows' => $rows, ); } ); } /** * Optimize every database table using this site's prefix. Non- * destructive -- reclaims space and defragments indexes, deletes no * data. * * @return CleanupTask */ private static function optimize_tables(): CleanupTask { global $wpdb; $list_tables = static function () use ( $wpdb ) { $like = $wpdb->esc_like( $wpdb->prefix ) . '%'; return $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $like ) ); }; return new CleanupTask( id: 'optimize_tables', label: __( 'Optimize Database Tables', 'all-in-one-utilities' ), description: __( 'Reclaims unused space and defragments indexes. Does not delete any data.', 'all-in-one-utilities' ), count: static function () use ( $list_tables ) { return count( $list_tables() ); }, clean: static function () use ( $wpdb, $list_tables ) { $tables = $list_tables(); if ( empty( $tables ) ) { return 0; } $escaped = array_map( static function ( $table ) { return '`' . str_replace( '`', '', $table ) . '`'; }, $tables ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $escaped is built entirely from this site's own backtick-stripped table names returned by SHOW TABLES above, not from user input; OPTIMIZE TABLE has no placeholder syntax for table names. $wpdb->query( 'OPTIMIZE TABLE ' . implode( ', ', $escaped ) ); return count( $tables ); }, preview: static function ( int $limit ) use ( $wpdb ) { $found = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- live figures for an admin tool. $wpdb->prepare( 'SELECT table_name AS name, table_rows AS row_count, data_length + index_length AS size, data_free AS free FROM information_schema.TABLES WHERE table_schema = %s AND table_name LIKE %s ORDER BY data_free DESC, table_name ASC LIMIT %d', $wpdb->dbname, $wpdb->esc_like( $wpdb->prefix ) . '%', $limit ) ); $rows = array(); foreach ( (array) $found as $row ) { $rows[] = array( 'name' => (string) $row->name, 'rows' => number_format_i18n( (int) $row->row_count ), 'size' => size_format( (int) $row->size, 1 ) ? size_format( (int) $row->size, 1 ) : '0 B', 'free' => (int) $row->free ? size_format( (int) $row->free, 1 ) : '0 B', ); } return array( 'columns' => array( 'name' => __( 'Table', 'all-in-one-utilities' ), 'rows' => __( 'Rows (approx.)', 'all-in-one-utilities' ), 'size' => __( 'Size', 'all-in-one-utilities' ), 'free' => __( 'Reclaimable', 'all-in-one-utilities' ), ), 'rows' => $rows, ); } ); } }