| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP-CLI commands for OPcache management. |
| 4 |
* |
| 5 |
* @package opcache-reset |
| 6 |
*/ |
| 7 |
|
| 8 |
// Prevent direct access. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
// Only load if WP-CLI is available. |
| 14 |
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Manage OPcache from the command line. |
| 20 |
* |
| 21 |
* ## EXAMPLES |
| 22 |
* |
| 23 |
* # Reset OPcache |
| 24 |
* $ wp opcache reset |
| 25 |
* Success: OPcache has been reset. |
| 26 |
* |
| 27 |
* # Show OPcache status |
| 28 |
* $ wp opcache status |
| 29 |
* OPcache is enabled. |
| 30 |
* Cached scripts: 1234 |
| 31 |
* Memory usage: 64.5 MB / 128 MB |
| 32 |
* |
| 33 |
* @package opcache-reset |
| 34 |
*/ |
| 35 |
class OPcache_CLI_Command { |
| 36 |
|
| 37 |
/** |
| 38 |
* Reset OPcache. |
| 39 |
* |
| 40 |
* Clears both memory-based and file-based OPcache. |
| 41 |
* Uses the same reset mechanism as automatic plugin/theme updates. |
| 42 |
* |
| 43 |
* ## OPTIONS |
| 44 |
* |
| 45 |
* [--force] |
| 46 |
* : Force reset even if OPcache appears disabled. |
| 47 |
* |
| 48 |
* ## EXAMPLES |
| 49 |
* |
| 50 |
* # Reset OPcache |
| 51 |
* $ wp opcache reset |
| 52 |
* Success: OPcache has been reset. |
| 53 |
* |
| 54 |
* # Force reset |
| 55 |
* $ wp opcache reset --force |
| 56 |
* Success: OPcache has been reset. |
| 57 |
* |
| 58 |
* @param array $args Positional arguments. |
| 59 |
* @param array $assoc_args Associative arguments. |
| 60 |
*/ |
| 61 |
public function reset( $args, $assoc_args ) { |
| 62 |
$force = WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ); |
| 63 |
|
| 64 |
if ( ! function_exists( 'opcache_reset' ) && ! $force ) { |
| 65 |
WP_CLI::error( 'OPcache extension is not loaded. Use --force to attempt reset anyway.' ); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if ( empty( ini_get( 'opcache.enable' ) ) && ! $force ) { |
| 70 |
WP_CLI::error( 'OPcache is disabled. Use --force to attempt reset anyway.' ); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Call the plugin's reset function. |
| 75 |
if ( function_exists( 'gps_opcache_reset' ) ) { |
| 76 |
gps_opcache_reset(); |
| 77 |
WP_CLI::success( 'OPcache has been reset.' ); |
| 78 |
} else { |
| 79 |
WP_CLI::error( 'OPcache Reset plugin function not found. Is the plugin active?' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Show OPcache status and statistics. |
| 85 |
* |
| 86 |
* Displays current OPcache configuration and usage statistics. |
| 87 |
* |
| 88 |
* ## OPTIONS |
| 89 |
* |
| 90 |
* [--format=<format>] |
| 91 |
* : Output format. |
| 92 |
* --- |
| 93 |
* default: table |
| 94 |
* options: |
| 95 |
* - table |
| 96 |
* - json |
| 97 |
* - yaml |
| 98 |
* --- |
| 99 |
* |
| 100 |
* ## EXAMPLES |
| 101 |
* |
| 102 |
* # Show status as table |
| 103 |
* $ wp opcache status |
| 104 |
* |
| 105 |
* # Show status as JSON |
| 106 |
* $ wp opcache status --format=json |
| 107 |
* |
| 108 |
* @param array $args Positional arguments. |
| 109 |
* @param array $assoc_args Associative arguments. |
| 110 |
*/ |
| 111 |
public function status( $args, $assoc_args ) { |
| 112 |
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' ); |
| 113 |
|
| 114 |
if ( ! function_exists( 'opcache_get_status' ) ) { |
| 115 |
WP_CLI::error( 'OPcache extension is not loaded.' ); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
$status = opcache_get_status( false ); |
| 120 |
$config = opcache_get_configuration(); |
| 121 |
|
| 122 |
if ( false === $status ) { |
| 123 |
WP_CLI::error( 'Failed to get OPcache status. OPcache may be disabled.' ); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$stats = $status['opcache_statistics'] ?? array(); |
| 128 |
$memory = $status['memory_usage'] ?? array(); |
| 129 |
$directives = $config['directives'] ?? array(); |
| 130 |
|
| 131 |
// Calculate memory usage. |
| 132 |
$used_memory = $memory['used_memory'] ?? 0; |
| 133 |
$free_memory = $memory['free_memory'] ?? 0; |
| 134 |
$total_memory = $used_memory + $free_memory; |
| 135 |
|
| 136 |
$data = array( |
| 137 |
array( |
| 138 |
'Property' => 'Status', |
| 139 |
'Value' => ( $status['opcache_enabled'] ?? false ) ? 'Enabled' : 'Disabled', |
| 140 |
), |
| 141 |
array( |
| 142 |
'Property' => 'Cached Scripts', |
| 143 |
'Value' => $stats['num_cached_scripts'] ?? 0, |
| 144 |
), |
| 145 |
array( |
| 146 |
'Property' => 'Cache Hits', |
| 147 |
'Value' => $stats['hits'] ?? 0, |
| 148 |
), |
| 149 |
array( |
| 150 |
'Property' => 'Cache Misses', |
| 151 |
'Value' => $stats['misses'] ?? 0, |
| 152 |
), |
| 153 |
array( |
| 154 |
'Property' => 'Memory Used', |
| 155 |
'Value' => $this->format_bytes( $used_memory ), |
| 156 |
), |
| 157 |
array( |
| 158 |
'Property' => 'Memory Free', |
| 159 |
'Value' => $this->format_bytes( $free_memory ), |
| 160 |
), |
| 161 |
array( |
| 162 |
'Property' => 'Memory Total', |
| 163 |
'Value' => $this->format_bytes( $total_memory ), |
| 164 |
), |
| 165 |
array( |
| 166 |
'Property' => 'Hit Rate', |
| 167 |
'Value' => round( $stats['opcache_hit_rate'] ?? 0, 2 ) . '%', |
| 168 |
), |
| 169 |
array( |
| 170 |
'Property' => 'File Cache', |
| 171 |
'Value' => $directives['opcache.file_cache'] ?? 'Disabled', |
| 172 |
), |
| 173 |
array( |
| 174 |
'Property' => 'Validate Timestamps', |
| 175 |
'Value' => ( $directives['opcache.validate_timestamps'] ?? true ) ? 'Yes' : 'No', |
| 176 |
), |
| 177 |
); |
| 178 |
|
| 179 |
if ( 'json' === $format ) { |
| 180 |
// For JSON, output raw data. |
| 181 |
$json_data = array( |
| 182 |
'enabled' => $status['opcache_enabled'] ?? false, |
| 183 |
'cached_scripts' => $stats['num_cached_scripts'] ?? 0, |
| 184 |
'hits' => $stats['hits'] ?? 0, |
| 185 |
'misses' => $stats['misses'] ?? 0, |
| 186 |
'memory_used' => $used_memory, |
| 187 |
'memory_free' => $free_memory, |
| 188 |
'memory_total' => $total_memory, |
| 189 |
'hit_rate' => $stats['opcache_hit_rate'] ?? 0, |
| 190 |
'file_cache' => $directives['opcache.file_cache'] ?? null, |
| 191 |
'validate_timestamps' => $directives['opcache.validate_timestamps'] ?? true, |
| 192 |
); |
| 193 |
WP_CLI::line( wp_json_encode( $json_data, JSON_PRETTY_PRINT ) ); |
| 194 |
} else { |
| 195 |
WP_CLI\Utils\format_items( $format, $data, array( 'Property', 'Value' ) ); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Format bytes to human-readable string. |
| 201 |
* |
| 202 |
* @param int $bytes Number of bytes. |
| 203 |
* @return string Formatted string. |
| 204 |
*/ |
| 205 |
private function format_bytes( $bytes ) { |
| 206 |
if ( $bytes >= 1073741824 ) { |
| 207 |
return round( $bytes / 1073741824, 2 ) . ' GB'; |
| 208 |
} elseif ( $bytes >= 1048576 ) { |
| 209 |
return round( $bytes / 1048576, 2 ) . ' MB'; |
| 210 |
} elseif ( $bytes >= 1024 ) { |
| 211 |
return round( $bytes / 1024, 2 ) . ' KB'; |
| 212 |
} |
| 213 |
return $bytes . ' bytes'; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
WP_CLI::add_command( 'opcache', 'OPcache_CLI_Command' ); |
| 218 |
|