get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->options} WHERE option_name LIKE %s AND option_value < %d", '_transient_timeout_%', $now ) ); // Count spam comments // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $spam_comments = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved = 'spam'" ); // Count trashed posts // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $trashed_posts = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = 'trash'" ); // Count auto-drafts // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $auto_drafts = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = 'auto-draft'" ); return array( 'expired_transients' => $expired_transients, 'spam_comments' => $spam_comments, 'trashed_posts' => $trashed_posts, 'auto_drafts' => $auto_drafts, ); } /** * Safely delete only expired transients from wp_options. * * @return int Number of deleted transients. */ public static function clean_expired_transients() { global $wpdb; $now = time(); // Find expired transient keys // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $expired = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s AND option_value < %d", '_transient_timeout_%', $now ) ); $cleaned_count = 0; if ( ! empty( $expired ) ) { foreach ( $expired as $timeout_key ) { $transient_key = str_replace( '_transient_timeout_', '', $timeout_key ); delete_transient( $transient_key ); $cleaned_count++; } } return $cleaned_count; } }