* LiteSpeed CLI - database cleanup
* Add CLI database cleanup commands.
defined('WPINC') || exit();
* LiteSpeed Cache Database CLI
* Current blog id the optimization is working on.
* @var int|false $current_blog Current blog id.
private $current_blog = false;
* @var DB_Optim $db Database class.
public function __construct() {
Debug2::debug('CLI_Database init');
$this->db = DB_Optm::cls();
* List all site domains and ids on the network.
public function network_list() {
WP_CLI::error('This is not a multisite installation!');
$buf = WP_CLI::colorize("%CThe list of installs:%n\n");
foreach ( $sites as $site ) {
$buf .= WP_CLI::colorize( '%Y' . $site->domain . $site->path . ':%n ID ' . $site->blog_id ) . "\n";
* Change to blog sent as param.
* @param array $args Description.
private function change_to_blog( $args ) {
if ( !isset( $args[0] ) || 'blog' !== $args[0] ) {
$this->current_blog = get_current_blog_id();
if ( !is_numeric( $blogid ) ) {
$error = WP_CLI::colorize( '%RError: invalid blog id entered.%n' );
$this->network_list( $args );
$site = get_blog_details( $blogid );
$error = WP_CLI::colorize( '%RError: invalid blog id entered.%n' );
$this->network_list( $args );
switch_to_blog( $blogid );
* Change to previous blog.
private function change_to_default() {
// Check if previous blog set.
if ( $this->current_blog ) {
switch_to_blog( $this->current_blog );
// Switched to previous blog.
$this->current_blog = false;
* @param boolean $result Flag if result is success or failure.
* @param string $action Action name.
private function show_response( $result, $action ) {
WP_CLI::success( $result );
WP_CLI::error( 'Error running optimization: ' . $action );
* Clean actions function.
* @param int $args Action arguments.
* @param array $types What data to clean.
private function clean_action( $args, $types ) {
$this->change_to_blog( $args );
foreach ( $types as $type ) {
$result = $this->db->handler_clean_db_cli( $type );
$this->show_response( $result, $type );
$this->change_to_default();
* Clear posts data(revisions, orphaned, auto drafts, trashed posts).
* # Start clearing posts data.
* $ wp litespeed-database clear_posts
* $ wp litespeed-database clear_posts blog 2
* @param string $args Action arguments.
public function clear_posts( $args ) {
$this->clean_action( $args, $types );
* Clear comments(spam and trash comments).
* # Start clearing comments.
* $ wp litespeed-database clear_comments
* $ wp litespeed-database clear_comments blog 2
* @param string $args Action arguments.
public function clear_comments( $args ) {
$this->clean_action( $args, $types );
* Clear trackbacks/pingbacks.
* # Start clearing trackbacks/pingbacks.
* $ wp litespeed-database clear_trackbacks
* $ wp litespeed-database clear_trackbacks blog 2
* @param string $args Action arguments.
public function clear_trackbacks( $args ) {
$this->clean_action( $args, $types );
* # Start clearing transients.
* $ wp litespeed-database clear_transients
* $ wp litespeed-database clear_transients blog 2
* @param string $args Action arguments.
public function clear_transients( $args ) {
$this->clean_action( $args, $types );
* # Start optimizing tables.
* $ wp litespeed-database optimize_tables
* $ wp litespeed-database optimize_tables blog 2
* @param string $args Action arguments.
public function optimize_tables( $args ) {
$this->clean_action( $args, $types );
* Optimize database by running all possible operations.
* # Start optimizing all.
* $ wp litespeed-database optimize_all
* $ wp litespeed-database optimize_all blog 2
* @param string $args Action arguments.
public function optimize_all( $args ) {
$this->clean_action( $args, $types );