|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Load WPSC files and config file if they aren't loaded. |
| 5 | + */ |
| 6 | +final class WP_Super_Cache_CLI_Loader { |
| 7 | + |
| 8 | + /** |
| 9 | + * Version of WP Super Cache plugin. |
| 10 | + * |
| 11 | + * @var string Version. |
| 12 | + */ |
| 13 | + protected $wpsc_version; |
| 14 | + |
| 15 | + /** |
| 16 | + * Absolute path to the plugin file. |
| 17 | + * |
| 18 | + * @var string File path. |
| 19 | + */ |
| 20 | + protected $wpsc_plugin_file; |
| 21 | + |
| 22 | + /** |
| 23 | + * Checks status of WP Super Cache and loads config/dependencies if it needs. |
| 24 | + * |
| 25 | + * @return void |
| 26 | + */ |
| 27 | + public function load() { |
| 28 | + // If WP isn't loaded then registers hooks. |
| 29 | + if ( ! function_exists( 'add_filter' ) ) { |
| 30 | + $this->register_hooks(); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + $error_msg = ''; |
| 35 | + |
| 36 | + // Before loading files check is plugin installed/activated. |
| 37 | + if ( $this->get_wpsc_version() === '' ) { |
| 38 | + $error_msg = 'WP Super Cache needs to be installed to use its WP-CLI commands.'; |
| 39 | + } elseif ( version_compare( $this->get_wpsc_version(), '1.5.2', '<' ) ) { |
| 40 | + $error_msg = 'Minimum required version of WP Super Cache is 1.5.2'; |
| 41 | + } elseif ( ! $this->is_wpsc_plugin_active() ) { |
| 42 | + $error_msg = 'WP Super Cache needs to be activated to use its WP-CLI commands.'; |
| 43 | + } elseif ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) { |
| 44 | + $error_msg = 'WP_CACHE constant is false or not defined'; |
| 45 | + } elseif ( defined( 'WP_CACHE' ) && WP_CACHE && defined( 'ADVANCEDCACHEPROBLEM' ) ) { |
| 46 | + $error_msg = 'WP Super Cache caching is broken'; |
| 47 | + } |
| 48 | + |
| 49 | + if ( $error_msg ) { |
| 50 | + WP_CLI::error( $error_msg ); |
| 51 | + } |
| 52 | + |
| 53 | + // Initialization of cache-base. |
| 54 | + $this->init_cache_base(); |
| 55 | + |
| 56 | + // Load dependencies if they aren't loaded. |
| 57 | + $this->maybe_load_files(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Registers the hooks. |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + private function register_hooks() { |
| 66 | + WP_CLI::add_wp_hook( 'muplugins_loaded', array( $this, 'init_cache_base' ) ); |
| 67 | + WP_CLI::add_wp_hook( 'plugins_loaded', array( $this, 'maybe_load_files' ) ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Initialization of cache-base. |
| 72 | + * |
| 73 | + * @global string $WPSC_HTTP_HOST |
| 74 | + * |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function init_cache_base() { |
| 78 | + global $WPSC_HTTP_HOST; |
| 79 | + |
| 80 | + if ( ! defined( 'WPCACHEHOME' ) ) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Loads config file. |
| 85 | + $this->maybe_load_config(); |
| 86 | + |
| 87 | + // If the parameter --url doesn't exist then gets HTTP_HOST from WordPress Address. |
| 88 | + if ( empty( $_SERVER['HTTP_HOST'] ) ) { |
| 89 | + $_SERVER['HTTP_HOST'] = $this->parse_home_url( PHP_URL_HOST ); |
| 90 | + } |
| 91 | + |
| 92 | + if ( empty( $WPSC_HTTP_HOST ) ) { |
| 93 | + $this->maybe_include_file( 'include', 'wp-cache-base.php' ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Loads config file and populates globals. |
| 99 | + * |
| 100 | + * @return void |
| 101 | + */ |
| 102 | + private function maybe_load_config() { |
| 103 | + global $cache_enabled, $super_cache_enabled, $cache_path, $wp_cache_mod_rewrite, $wp_cache_debug_log; |
| 104 | + global $wp_cache_config_file, $wp_cache_config_file_sample, $wp_cache_home_path; |
| 105 | + |
| 106 | + if ( empty( $wp_cache_config_file ) ) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if ( ! isset( $cache_enabled, $super_cache_enabled, $cache_path, $wp_cache_mod_rewrite, $wp_cache_debug_log ) |
| 111 | + && ! $this->maybe_include_file( 'include', $wp_cache_config_file ) |
| 112 | + ) { |
| 113 | + if ( ! defined( 'WPCACHEHOME' ) |
| 114 | + || empty( $wp_cache_config_file_sample ) |
| 115 | + || ! $this->maybe_include_file( 'include', $wp_cache_config_file_sample ) |
| 116 | + ) { |
| 117 | + WP_CLI::error( 'Cannot load cache config file.' ); |
| 118 | + } |
| 119 | + |
| 120 | + WP_CLI::warning( 'Default cache config file loaded - ' . str_replace( ABSPATH, '', $wp_cache_config_file_sample ) ); |
| 121 | + } |
| 122 | + |
| 123 | + $wp_cache_home_path = trailingslashit( $this->parse_home_url( PHP_URL_PATH ) ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Loads config file, PHP files and overrides multisite settings. |
| 128 | + * |
| 129 | + * @return void |
| 130 | + */ |
| 131 | + public function maybe_load_files() { |
| 132 | + // WPSC >= 1.5.2 and it's active? |
| 133 | + if ( ! defined( 'WPCACHEHOME' ) || ! function_exists( 'wpsc_init' ) ) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if ( version_compare( $this->get_wpsc_version(), '1.5.9', '>=' ) ) { |
| 138 | + // In rare cases, loading of wp-cache-phase2.php may be necessary. |
| 139 | + $this->maybe_include_file( 'wp_cache_phase2', 'wp-cache-phase2.php' ); |
| 140 | + } else { |
| 141 | + // Prevents creation of output buffer or serving file for older versions. |
| 142 | + $request_method = $_SERVER['REQUEST_METHOD']; |
| 143 | + $_SERVER['REQUEST_METHOD'] = 'POST'; |
| 144 | + } |
| 145 | + |
| 146 | + // List of required files. |
| 147 | + $include_files = array( |
| 148 | + 'wp_cache_postload' => array( |
| 149 | + 'file' => 'wp-cache-phase1.php', |
| 150 | + 'run' => '', |
| 151 | + ), |
| 152 | + 'domain_mapping_actions' => array( |
| 153 | + 'file' => 'plugins/domain-mapping.php', |
| 154 | + 'run' => 'domain_mapping_actions', |
| 155 | + ), |
| 156 | + 'wp_super_cache_multisite_init' => array( |
| 157 | + 'file' => 'plugins/multisite.php', |
| 158 | + 'run' => 'wp_super_cache_override_on_flag', |
| 159 | + ), |
| 160 | + ); |
| 161 | + |
| 162 | + foreach ( $include_files as $func => $file ) { |
| 163 | + $this->maybe_include_file( $func, $file['file'], $file['run'] ); |
| 164 | + } |
| 165 | + |
| 166 | + if ( ! empty( $request_method ) ) { |
| 167 | + $_SERVER['REQUEST_METHOD'] = $request_method; |
| 168 | + } |
| 169 | + |
| 170 | + $this->multisite_override_settings(); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Overrides multisite settings. |
| 175 | + * |
| 176 | + * @global string $cache_path Absolute path to cache directory. |
| 177 | + * @global string $blogcacheid |
| 178 | + * @global string $blog_cache_dir |
| 179 | + * @global object $current_site The current site. |
| 180 | + * |
| 181 | + * @return void |
| 182 | + */ |
| 183 | + private function multisite_override_settings() { |
| 184 | + global $cache_path, $blogcacheid, $blog_cache_dir, $current_site; |
| 185 | + |
| 186 | + if ( ! is_multisite() ) { |
| 187 | + // Prevents PHP notices for single site installation. |
| 188 | + if ( ! isset( $blog_cache_dir ) ) { |
| 189 | + $blog_cache_dir = $cache_path; |
| 190 | + } |
| 191 | + |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + if ( is_object( $current_site ) ) { |
| 196 | + $blogcacheid = trim( is_subdomain_install() ? $current_site->domain : $current_site->path, '/' ); |
| 197 | + $blog_cache_dir = $cache_path . 'blogs/' . $blogcacheid . '/'; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Gets absolute path for file if file exists. |
| 203 | + * Returns empty string if file doesn't exist or isn't readable. |
| 204 | + * |
| 205 | + * @param string $filename File name. |
| 206 | + * |
| 207 | + * @return string |
| 208 | + */ |
| 209 | + private function get_wpsc_filename( $filename ) { |
| 210 | + if ( 0 !== strpos( $filename, ABSPATH ) ) { |
| 211 | + $filename = WPCACHEHOME . $filename; |
| 212 | + } |
| 213 | + |
| 214 | + if ( ! is_file( $filename ) || ! is_readable( $filename ) ) { |
| 215 | + return ''; |
| 216 | + } |
| 217 | + |
| 218 | + return $filename; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * If function doesn't exist then loads file and ivokes function if it needs. |
| 223 | + * Explicitly declares all globals which WPSC uses. |
| 224 | + * |
| 225 | + * @param string $func Function name. |
| 226 | + * @param string $filename File name. |
| 227 | + * @param string $run Optional function will be called if file is included. |
| 228 | + * |
| 229 | + * @return boolean True if file is included or false if it isn't included. |
| 230 | + */ |
| 231 | + private function maybe_include_file( $func, $filename, $run = '' ) { |
| 232 | + // Globals from wp-cache-config.php. |
| 233 | + global $super_cache_enabled, $cache_enabled, $wp_cache_mod_rewrite, $wp_cache_home_path, $cache_path, $file_prefix; |
| 234 | + global $wp_cache_mutex_disabled, $mutex_filename, $sem_id, $wp_super_cache_late_init; |
| 235 | + global $cache_compression, $cache_max_time, $wp_cache_shutdown_gc, $cache_rebuild_files; |
| 236 | + global $wp_super_cache_debug, $wp_super_cache_advanced_debug, $wp_cache_debug_level, $wp_cache_debug_to_file; |
| 237 | + global $wp_cache_debug_log, $wp_cache_debug_ip, $wp_cache_debug_username, $wp_cache_debug_email; |
| 238 | + global $cache_time_interval, $cache_scheduled_time, $cache_schedule_interval, $cache_schedule_type, $cache_gc_email_me; |
| 239 | + global $wp_cache_preload_on, $wp_cache_preload_interval, $wp_cache_preload_posts, $wp_cache_preload_taxonomies; |
| 240 | + global $wp_cache_preload_email_me, $wp_cache_preload_email_volume; |
| 241 | + global $wp_cache_mobile, $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes; |
| 242 | + // Globals from other files. |
| 243 | + global $wp_cache_config_file, $wp_cache_config_file_sample, $cache_domain_mapping; |
| 244 | + global $WPSC_HTTP_HOST, $blogcacheid, $blog_cache_dir; |
| 245 | + |
| 246 | + $file = $this->get_wpsc_filename( $filename ); |
| 247 | + |
| 248 | + if ( empty( $file ) || |
| 249 | + ( ! in_array( $func, array( 'require', 'require_once', 'include', 'include_once' ), true ) |
| 250 | + && function_exists( $func ) |
| 251 | + ) |
| 252 | + ) { |
| 253 | + return false; |
| 254 | + } |
| 255 | + |
| 256 | + switch ( $func ) { |
| 257 | + case 'require': |
| 258 | + $loaded = require $file; |
| 259 | + break; |
| 260 | + case 'require_once': |
| 261 | + $loaded = require_once $file; |
| 262 | + break; |
| 263 | + case 'include': |
| 264 | + $loaded = include $file; |
| 265 | + break; |
| 266 | + case 'include_once': |
| 267 | + default: |
| 268 | + $loaded = include_once $file; |
| 269 | + break; |
| 270 | + } |
| 271 | + |
| 272 | + if ( $loaded && ! empty( $run ) && function_exists( $run ) ) { |
| 273 | + call_user_func( $run ); |
| 274 | + } |
| 275 | + |
| 276 | + return $loaded; |
| 277 | + } |
| 278 | + |
| 279 | + /** |
| 280 | + * Gets version of WP Super Cache. |
| 281 | + * |
| 282 | + * @global string $wp_cache_config_file_sample Absolute path to wp-cache config sample file. |
| 283 | + * |
| 284 | + * @return string |
| 285 | + */ |
| 286 | + public function get_wpsc_version() { |
| 287 | + global $wp_cache_config_file_sample; |
| 288 | + |
| 289 | + if ( isset( $this->wpsc_version ) ) { |
| 290 | + return $this->wpsc_version; |
| 291 | + } |
| 292 | + |
| 293 | + if ( ! function_exists( 'get_file_data' ) ) { |
| 294 | + return ''; |
| 295 | + } |
| 296 | + |
| 297 | + if ( ! function_exists( 'get_plugin_data' ) ) { |
| 298 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 299 | + } |
| 300 | + |
| 301 | + $this->wpsc_version = ''; |
| 302 | + $this->wpsc_plugin_file = empty( $wp_cache_config_file_sample ) |
| 303 | + ? trailingslashit( WP_PLUGIN_DIR ) . 'wp-super-cache/wp-cache.php' |
| 304 | + : plugin_dir_path( $wp_cache_config_file_sample ) . 'wp-cache.php'; |
| 305 | + |
| 306 | + if ( ! is_file( $this->wpsc_plugin_file ) || ! is_readable( $this->wpsc_plugin_file ) ) { |
| 307 | + return $this->wpsc_version; |
| 308 | + } |
| 309 | + |
| 310 | + $plugin_details = get_plugin_data( $this->wpsc_plugin_file ); |
| 311 | + if ( ! empty( $plugin_details['Version'] ) ) { |
| 312 | + $this->wpsc_version = $plugin_details['Version']; |
| 313 | + } |
| 314 | + |
| 315 | + return $this->wpsc_version; |
| 316 | + } |
| 317 | + |
| 318 | + /** |
| 319 | + * Check whether wp-super-cache plugin is active. |
| 320 | + * |
| 321 | + * @return bool |
| 322 | + */ |
| 323 | + private function is_wpsc_plugin_active() { |
| 324 | + if ( $this->get_wpsc_version() && is_plugin_active( plugin_basename( $this->wpsc_plugin_file ) ) ) { |
| 325 | + return true; |
| 326 | + } |
| 327 | + |
| 328 | + return false; |
| 329 | + } |
| 330 | + |
| 331 | + /** |
| 332 | + * Retrieves the component (PHP_URL_HOST or PHP_URL_PATH) from home URL. |
| 333 | + * |
| 334 | + * @param int $component The component to retrieve. |
| 335 | + * |
| 336 | + * @return string |
| 337 | + */ |
| 338 | + private function parse_home_url( $component ) { |
| 339 | + return function_exists( 'wp_parse_url' ) |
| 340 | + ? (string) wp_parse_url( get_option( 'home' ), $component ) |
| 341 | + : (string) parse_url( get_option( 'home' ), $component ); |
| 342 | + } |
| 343 | +} |
0 commit comments