purge_everything(); $this->display_purge_table( $results ); WP_CLI::success( 'Nuclear cache purge completed.' ); return; } if ( isset( $assoc_args['css'] ) ) { $res = $purger->purge_css_cache(); WP_CLI::success( sprintf( 'CSS Cache: %s (%s)', $res['status'], $res['message'] ) ); return; } if ( isset( $assoc_args['js'] ) ) { $res = $purger->purge_js_cache(); WP_CLI::success( sprintf( 'JS Cache: %s (%s)', $res['status'], $res['message'] ) ); return; } if ( isset( $assoc_args['html'] ) ) { $res = $purger->purge_html_cache(); WP_CLI::success( sprintf( 'HTML Page Cache: %s (%s)', $res['status'], $res['message'] ) ); return; } // Default clear action: purge all accessible layers. WP_CLI::log( 'Purging all accessible cache layers...' ); $results = $purger->purge_all(); $this->display_purge_table( $results ); WP_CLI::success( 'Cache purge complete.' ); return; } if ( 'status' === $action ) { $this->status( $args, $assoc_args ); return; } WP_CLI::error( sprintf( 'Unknown cache action "%s". Use: wp wpso cache clear [--all|--css|--js|--html] or wp wpso cache status', $action ) ); } /** * Display cache status. * * ## EXAMPLES * * wp wpso cache status * * @param array $args Positional arguments. * @param array $assoc_args Associative flags. */ public function status( $args = array(), $assoc_args = array() ) { $cache_dir = function_exists( 'wpsco_get_cache_dir' ) ? wpsco_get_cache_dir() : ( defined( 'WPSCO_CACHE_DIR' ) ? WPSCO_CACHE_DIR : '' ); $dir_exists = is_dir( $cache_dir ); $total_size = $dir_exists ? wpsco_get_dir_size( $cache_dir ) : 0; $css_size = is_dir( $cache_dir . 'css/' ) ? wpsco_get_dir_size( $cache_dir . 'css/' ) : 0; $js_size = is_dir( $cache_dir . 'js/' ) ? wpsco_get_dir_size( $cache_dir . 'js/' ) : 0; $html_size = is_dir( $cache_dir . 'html/' ) ? wpsco_get_dir_size( $cache_dir . 'html/' ) : 0; $table_data = array( array( 'Layer', 'Status', 'Size' ), array( 'Cache Directory', $dir_exists ? 'Writable' : 'Missing', wpsco_format_size( $total_size ) ), array( 'CSS Cache', wpsco_get_setting( 'css_minify' ) ? 'Enabled' : 'Disabled', wpsco_format_size( $css_size ) ), array( 'JS Cache', wpsco_get_setting( 'js_minify' ) ? 'Enabled' : 'Disabled', wpsco_format_size( $js_size ) ), array( 'HTML Page Cache', wpsco_get_setting( 'page_cache_enabled' ) ? 'Enabled' : 'Disabled', wpsco_format_size( $html_size ) ), array( 'Object Cache', wp_using_ext_object_cache() ? 'Persistent External' : 'Internal', 'N/A' ), array( 'OPcache', function_exists( 'opcache_reset' ) ? 'Active' : 'Disabled/Restricted', 'N/A' ), array( 'Cache Version', wpsco_get_cache_version(), 'N/A' ), ); WP_CLI\Utils\format_items( 'table', array_map( function( $row ) { return array( 'Component' => $row[0], 'Status' => $row[1], 'Details' => $row[2] ); }, array_slice( $table_data, 1 ) ), array( 'Component', 'Status', 'Details' ) ); } /** * Run diagnostics. * * ## EXAMPLES * * wp wpso diagnostics * * @param array $args Positional arguments. * @param array $assoc_args Associative flags. */ public function diagnostics( $args = array(), $assoc_args = array() ) { WP_CLI::log( 'Running performance diagnostics...' ); $diag = WPSCO_Diagnostics::run_local_diagnostics(); WP_CLI::log( 'Server Software: ' . $diag['server']['server_software'] ); WP_CLI::log( 'PHP Version: ' . $diag['server']['php_version'] ); WP_CLI::log( 'Memory Limit: ' . $diag['server']['memory_limit'] ); WP_CLI::log( 'Compression: ' . ( $diag['compression']['status'] ? 'Active (' . $diag['compression']['active_compression'] . ')' : 'Inactive' ) ); WP_CLI::log( 'OPcache: ' . ( $diag['opcache']['enabled'] ? 'Enabled (Hit Rate: ' . $diag['opcache']['hit_rate'] . ')' : 'Disabled' ) ); WP_CLI::log( 'Object Cache: ' . $diag['object_cache']['engine'] ); if ( 'success' === $diag['homepage']['status'] ) { WP_CLI::log( sprintf( 'Homepage TTFB: %d ms', $diag['homepage']['ttfb_ms'] ) ); WP_CLI::log( sprintf( 'HTML Size: %s KB', $diag['homepage']['html_size_kb'] ) ); WP_CLI::log( sprintf( 'Stylesheets: %d', $diag['homepage']['css_count'] ) ); WP_CLI::log( sprintf( 'Scripts: %d', $diag['homepage']['js_count'] ) ); WP_CLI::log( sprintf( 'Images: %d (Lazy: %d, Missing Dims: %d)', $diag['homepage']['img_count'], $diag['homepage']['lazy_img_count'], $diag['homepage']['missing_dims'] ) ); } WP_CLI::success( 'Diagnostics finished.' ); } /** * Run one-click safe optimization. * * ## EXAMPLES * * wp wpso optimize * * @param array $args Positional arguments. * @param array $assoc_args Associative flags. */ public function optimize( $args = array(), $assoc_args = array() ) { WP_CLI::log( 'Applying One-Click Safe Optimization preset...' ); WPSCO_Settings::apply_one_click_safe_optimization(); WP_CLI::success( 'Safe performance optimization applied. Caches purged.' ); } /** * Helper to render purge result table. * * @param array $results Purge results. */ private function display_purge_table( $results ) { $rows = array(); foreach ( $results as $key => $res ) { $rows[] = array( 'Cache Layer' => $res['name'] ?? $key, 'Status' => $res['status'] ?? 'unknown', 'Message' => $res['message'] ?? '', ); } WP_CLI\Utils\format_items( 'table', $rows, array( 'Cache Layer', 'Status', 'Message' ) ); } } WP_CLI::add_command( 'wpso', 'WPSCO_CLI' );