crawler.cls.php
2 months ago
database.cls.php
2 months ago
debug.cls.php
2 months ago
image.cls.php
2 months ago
online.cls.php
2 months ago
option.cls.php
2 months ago
presets.cls.php
2 months ago
purge.cls.php
2 months ago
database.cls.php
228 lines
| 1 | <?php |
| 2 | /** |
| 3 | * LiteSpeed CLI - database cleanup |
| 4 | * |
| 5 | * Add CLI database cleanup commands. |
| 6 | * |
| 7 | * @package LiteSpeed |
| 8 | * @since 7.3 |
| 9 | */ |
| 10 | |
| 11 | namespace LiteSpeed\CLI; |
| 12 | |
| 13 | defined('WPINC') || exit(); |
| 14 | |
| 15 | use LiteSpeed\Debug2; |
| 16 | use LiteSpeed\DB_Optm; |
| 17 | use WP_CLI; |
| 18 | |
| 19 | /** |
| 20 | * LiteSpeed Cache Database CLI |
| 21 | */ |
| 22 | class Database { |
| 23 | /** |
| 24 | * Current blog id the optimization is working on. |
| 25 | * |
| 26 | * @var int|false $current_blog Current blog id. |
| 27 | */ |
| 28 | private $current_blog = false; |
| 29 | /** |
| 30 | * Database class. |
| 31 | * |
| 32 | * @var DB_Optim $db Database class. |
| 33 | */ |
| 34 | private $db; |
| 35 | |
| 36 | /** |
| 37 | * Class constructor. |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | Debug2::debug('CLI_Database init'); |
| 41 | |
| 42 | $this->db = DB_Optm::cls(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * List all site domains and ids on the network. |
| 47 | */ |
| 48 | public function network_list() { |
| 49 | if ( !is_multisite() ) { |
| 50 | WP_CLI::error('This is not a multisite installation!'); |
| 51 | |
| 52 | return; |
| 53 | } |
| 54 | $buf = WP_CLI::colorize("%CThe list of installs:%n\n"); |
| 55 | |
| 56 | $sites = get_sites(); |
| 57 | foreach ( $sites as $site ) { |
| 58 | $buf .= WP_CLI::colorize( '%Y' . $site->domain . $site->path . ':%n ID ' . $site->blog_id ) . "\n"; |
| 59 | } |
| 60 | |
| 61 | WP_CLI::line($buf); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Change to blog sent as param. |
| 66 | * |
| 67 | * @param array $args Description. |
| 68 | */ |
| 69 | private function change_to_blog( $args ) { |
| 70 | if ( !isset( $args[0] ) || 'blog' !== $args[0] ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $this->current_blog = get_current_blog_id(); |
| 75 | $blogid = $args[1]; |
| 76 | if ( !is_numeric( $blogid ) ) { |
| 77 | $error = WP_CLI::colorize( '%RError: invalid blog id entered.%n' ); |
| 78 | WP_CLI::line( $error ); |
| 79 | $this->network_list( $args ); |
| 80 | return; |
| 81 | } |
| 82 | $site = get_blog_details( $blogid ); |
| 83 | if ( false === $site ) { |
| 84 | $error = WP_CLI::colorize( '%RError: invalid blog id entered.%n' ); |
| 85 | WP_CLI::line( $error ); |
| 86 | $this->network_list( $args ); |
| 87 | return; |
| 88 | } |
| 89 | switch_to_blog( $blogid ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Change to previous blog. |
| 94 | */ |
| 95 | private function change_to_default() { |
| 96 | // Check if previous blog set. |
| 97 | if ( $this->current_blog ) { |
| 98 | switch_to_blog( $this->current_blog ); |
| 99 | // Switched to previous blog. |
| 100 | $this->current_blog = false; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Show CLI response. |
| 106 | * |
| 107 | * @param boolean $result Flag if result is success or failure. |
| 108 | * @param string $action Action name. |
| 109 | */ |
| 110 | private function show_response( $result, $action ) { |
| 111 | if ($result) { |
| 112 | WP_CLI::success( $result ); |
| 113 | } else { |
| 114 | WP_CLI::error( 'Error running optimization: ' . $action ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Clean actions function. |
| 120 | * |
| 121 | * @param int $args Action arguments. |
| 122 | * @param array $types What data to clean. |
| 123 | */ |
| 124 | private function clean_action( $args, $types ) { |
| 125 | $this->change_to_blog( $args ); |
| 126 | foreach ( $types as $type ) { |
| 127 | $result = $this->db->handler_clean_db_cli( $type ); |
| 128 | $this->show_response( $result, $type ); |
| 129 | } |
| 130 | $this->change_to_default(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Clear posts data(revisions, orphaned, auto drafts, trashed posts). |
| 135 | * # Start clearing posts data. |
| 136 | * $ wp litespeed-database clear_posts |
| 137 | * $ wp litespeed-database clear_posts blog 2 |
| 138 | * |
| 139 | * @param string $args Action arguments. |
| 140 | */ |
| 141 | public function clear_posts( $args ) { |
| 142 | $types = array( |
| 143 | 'revision', |
| 144 | 'orphaned_post_meta', |
| 145 | 'auto_draft', |
| 146 | 'trash_post', |
| 147 | ); |
| 148 | $this->clean_action( $args, $types ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Clear comments(spam and trash comments). |
| 153 | * # Start clearing comments. |
| 154 | * $ wp litespeed-database clear_comments |
| 155 | * $ wp litespeed-database clear_comments blog 2 |
| 156 | * |
| 157 | * @param string $args Action arguments. |
| 158 | */ |
| 159 | public function clear_comments( $args ) { |
| 160 | $types = array( |
| 161 | 'spam_comment', |
| 162 | 'trash_comment', |
| 163 | ); |
| 164 | $this->clean_action( $args, $types ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Clear trackbacks/pingbacks. |
| 169 | * # Start clearing trackbacks/pingbacks. |
| 170 | * $ wp litespeed-database clear_trackbacks |
| 171 | * $ wp litespeed-database clear_trackbacks blog 2 |
| 172 | * |
| 173 | * @param string $args Action arguments. |
| 174 | */ |
| 175 | public function clear_trackbacks( $args ) { |
| 176 | $types = array( |
| 177 | 'trackback-pingback', |
| 178 | ); |
| 179 | $this->clean_action( $args, $types ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Clear transients. |
| 184 | * # Start clearing transients. |
| 185 | * $ wp litespeed-database clear_transients |
| 186 | * $ wp litespeed-database clear_transients blog 2 |
| 187 | * |
| 188 | * @param string $args Action arguments. |
| 189 | */ |
| 190 | public function clear_transients( $args ) { |
| 191 | $types = array( |
| 192 | 'expired_transient', |
| 193 | 'all_transients', |
| 194 | ); |
| 195 | $this->clean_action( $args, $types ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Optimize tables. |
| 200 | * # Start optimizing tables. |
| 201 | * $ wp litespeed-database optimize_tables |
| 202 | * $ wp litespeed-database optimize_tables blog 2 |
| 203 | * |
| 204 | * @param string $args Action arguments. |
| 205 | */ |
| 206 | public function optimize_tables( $args ) { |
| 207 | $types = array( |
| 208 | 'optimize_tables', |
| 209 | ); |
| 210 | $this->clean_action( $args, $types ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Optimize database by running all possible operations. |
| 215 | * # Start optimizing all. |
| 216 | * $ wp litespeed-database optimize_all |
| 217 | * $ wp litespeed-database optimize_all blog 2 |
| 218 | * |
| 219 | * @param string $args Action arguments. |
| 220 | */ |
| 221 | public function optimize_all( $args ) { |
| 222 | $types = array( |
| 223 | 'all', |
| 224 | ); |
| 225 | $this->clean_action( $args, $types ); |
| 226 | } |
| 227 | } |
| 228 |