/** * Return full path to log file for module * Path used in priority * 1) W3TC_DEBUG_DIR * 2) WP_DEBUG_LOG * 3) W3TC_CACHE_DIR * * @param unknown $module * @param null $blog_id * @return string */ public static function log_filename($module, $blog_id = null) { if (is_null($blog_id)) { $blog_id = Util_Environment::blog_id(); } $postfix = sprintf('%06d', $blog_id); if (defined('W3TC_BLOG_LEVELS')) { for ($n = 0; $n < W3TC_BLOG_LEVELS; $n++) { $postfix = substr($postfix, strlen($postfix) - 1 - $n, 1) . '/' . $postfix; } } $from_dir = W3TC_CACHE_DIR; if (defined('W3TC_DEBUG_DIR') && W3TC_DEBUG_DIR) { $dir_path = W3TC_DEBUG_DIR; if (!is_dir(W3TC_DEBUG_DIR)) { $from_dir = dirname(W3TC_DEBUG_DIR); } } else { $dir_path = Util_Environment::cache_dir('log'); } $filename = $dir_path . '/' . $postfix . '/' . $module . '.log'; if (!is_dir(dirname($filename))) { Util_File::mkdir_from(dirname($filename), $from_dir); } return $filename; }
/** * Fixes environment * * @param Config $config * @throws Util_Environment_Exceptions */ function fix_in_wpadmin($config, $force_all_checks = false) { $exs = new Util_Environment_Exceptions(); $fix_on_event = false; if (Util_Environment::is_wpmu() && Util_Environment::blog_id() != 0) { if (get_transient('w3tc_config_changes') != ($md5_string = $config->get_md5())) { $fix_on_event = true; set_transient('w3tc_config_changes', $md5_string, 3600); } } // call plugin-related handlers foreach ($this->get_handlers($config) as $h) { try { $h->fix_on_wpadmin_request($config, $force_all_checks); if ($fix_on_event) { $this->fix_on_event($config, 'admin_request'); } } catch (Util_Environment_Exceptions $ex) { $exs->push($ex); } } try { do_action('w3tc_environment_fix_on_wpadmin_request', $config, $force_all_checks); } catch (Util_Environment_Exceptions $ex) { $exs->push($ex); } if (count($exs->exceptions()) > 0) { throw $exs; } }
/** * Returns cache engine instance * * @param string $engine * @param array $config * @return W3_Cache_Base */ static function instance($engine, $config = array()) { static $instances = array(); // common configuration data if (!isset($config['blog_id'])) { $config['blog_id'] = Util_Environment::blog_id(); } $instance_key = sprintf('%s_%s', $engine, md5(serialize($config))); if (!isset($instances[$instance_key])) { switch ($engine) { case 'apc': if (function_exists('apcu_store')) { $instances[$instance_key] = new Cache_Apcu($config); } else { if (function_exists('apc_store')) { $instances[$instance_key] = new Cache_Apc($config); } } break; case 'eaccelerator': $instances[$instance_key] = new Cache_Eaccelerator($config); break; case 'file': $instances[$instance_key] = new Cache_File($config); break; case 'file_generic': $instances[$instance_key] = new Cache_File_Generic($config); break; case 'memcached': if (class_exists('\\Memcached')) { $instances[$instance_key] = new Cache_Memcached($config); } else { if (class_exists('\\Memcache')) { $instances[$instance_key] = new Cache_Memcache($config); } } break; case 'redis': $instances[$instance_key] = new Cache_Redis($config); break; case 'wincache': $instances[$instance_key] = new Cache_Wincache($config); break; case 'xcache': $instances[$instance_key] = new Cache_Xcache($config); break; default: trigger_error('Incorrect cache engine ' . $engine, E_USER_WARNING); $instances[$instance_key] = new Cache_Base($config); break; } if (!isset($instances[$instance_key]) || !$instances[$instance_key]->available()) { $instances[$instance_key] = new Cache_Base($config); } } return $instances[$instance_key]; }
public static function config_state() { if (Util_Environment::blog_id() <= 0) { return self::config_state_master(); } static $config_state = null; if (is_null($config_state)) { $config_state = new ConfigState(false); } return $config_state; }
public function __construct($blog_id = null) { if (!is_null($blog_id)) { $this->_blog_id = $blog_id; $this->_is_master = $this->_blog_id == 0; } else { if (Util_Environment::is_using_master_config()) { $this->_blog_id = 0; } else { $this->_blog_id = Util_Environment::blog_id(); } $this->_is_master = Util_Environment::blog_id() == 0; } $this->_preview = Util_Environment::is_preview_mode(); $this->load(); }
function cleanup_local() { $engine = $this->_config->get_string('pgcache.engine'); switch ($engine) { case 'file': $w3_cache_file_cleaner = new Cache_File_Cleaner(array('cache_dir' => Util_Environment::cache_blog_dir('page'), 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc'))); $w3_cache_file_cleaner->clean(); break; case 'file_generic': if (Util_Environment::blog_id() == 0) { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR; } else { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . Util_Environment::host(); } $w3_cache_file_cleaner_generic = new Cache_File_Cleaner_Generic(array('exclude' => array('.htaccess'), 'cache_dir' => $flush_dir, 'expire' => $this->_config->get_integer('browsercache.html.lifetime'), 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc'))); $w3_cache_file_cleaner_generic->clean(); break; } }
/** * Returns minify cache object * * @return object */ function _get_cache() { static $cache = array(); if (!isset($cache[0])) { switch ($this->_config->get_string('minify.engine')) { case 'memcached': $config = array('blog_id' => Util_Environment::blog_id(), 'instance_id' => Util_Environment::instance_id(), 'host' => Util_Environment::host(), 'module' => 'minify', 'servers' => $this->_config->get_array('minify.memcached.servers'), 'persistent' => $this->_config->get_boolean('minify.memcached.persistent'), 'aws_autodiscovery' => $this->_config->get_boolean('minify.memcached.aws_autodiscovery'), 'username' => $this->_config->get_boolean('minify.memcached.username'), 'password' => $this->_config->get_boolean('minify.memcached.password')); if (class_exists('Memcached')) { $w3_cache = new Cache_Memcached($config); } else { if (class_exists('Memcache')) { $w3_cache = new Cache_Memcache($config); } } $cache[0] = new \Minify_Cache_W3TCDerived($w3_cache); break; case 'redis': $config = array('blog_id' => Util_Environment::blog_id(), 'instance_id' => Util_Environment::instance_id(), 'host' => Util_Environment::host(), 'module' => 'minify', 'servers' => $this->_config->get_array('minify.redis.servers'), 'persistent' => $this->_config->get_boolean('minify.redis.persistent'), 'dbid' => $this->_config->get_boolean('minify.redis.dbid'), 'password' => $this->_config->get_boolean('minify.redis.password')); if (class_exists('Memcached')) { $w3_cache = new Cache_Memcached($config); } else { if (class_exists('Memcache')) { $w3_cache = new Cache_Memcache($config); } } $cache[0] = new \Minify_Cache_W3TCDerived($w3_cache); break; case 'apc': $config = array('blog_id' => Util_Environment::blog_id(), 'instance_id' => Util_Environment::instance_id(), 'host' => Util_Environment::host(), 'module' => 'minify'); if (function_exists('apcu_store')) { $w3_cache = new Cache_Apcu($config); } else { if (function_exists('apc_store')) { $w3_cache = new Cache_Apc($config); } } $cache[0] = new \Minify_Cache_W3TCDerived($w3_cache); break; case 'eaccelerator': $config = array('blog_id' => Util_Environment::blog_id(), 'instance_id' => Util_Environment::instance_id(), 'host' => Util_Environment::host(), 'module' => 'minify'); $w3_cache = new Cache_Eaccelerator($config); $cache[0] = new \Minify_Cache_W3TCDerived($w3_cache); break; case 'xcache': $config = array('blog_id' => Util_Environment::blog_id(), 'instance_id' => Util_Environment::instance_id(), 'host' => Util_Environment::host(), 'module' => 'minify'); $w3_cache = new Cache_Xcache($config); $cache[0] = new \Minify_Cache_W3TCDerived($w3_cache); break; case 'wincache': $config = array('blog_id' => Util_Environment::blog_id(), 'instance_id' => Util_Environment::instance_id(), 'host' => Util_Environment::host(), 'module' => 'minify'); $w3_cache = new Cache_Wincache($config); $cache[0] = new \Minify_Cache_W3TCDerived($w3_cache); break; case 'file': default: $cache[0] = new \Minify_Cache_File(Util_Environment::cache_blog_minify_dir(), array('.htaccess', 'index.php', '*.old'), $this->_config->get_boolean('minify.file.locking'), $this->_config->get_integer('timelimit.cache_flush'), Util_Environment::blog_id() == 0 ? W3TC_CACHE_MINIFY_DIR : null); break; } } return $cache[0]; }
/** * Convert relative file which is relative to ABSPATH (wp folder on disc) to path uri * * @param unknown $file * @return string */ function docroot_filename_to_uri($file) { $file = ltrim($file, '/'); // Translate multisite subsite uploads paths $file = str_replace(basename(WP_CONTENT_DIR) . '/blogs.dir/' . Util_Environment::blog_id() . '/', '', $file); return $file; }
/** * Returns server info * * @return array */ private function get_server_info() { global $wp_version, $wp_db_version, $wpdb; $wordpress_plugins = get_plugins(); $wordpress_plugins_active = array(); foreach ($wordpress_plugins as $wordpress_plugin_file => $wordpress_plugin) { if (is_plugin_active($wordpress_plugin_file)) { $wordpress_plugins_active[$wordpress_plugin_file] = $wordpress_plugin; } } $mysql_version = $wpdb->get_var('SELECT VERSION()'); $mysql_variables_result = (array) $wpdb->get_results('SHOW VARIABLES', ARRAY_N); $mysql_variables = array(); foreach ($mysql_variables_result as $mysql_variables_row) { $mysql_variables[$mysql_variables_row[0]] = $mysql_variables_row[1]; } $server_info = array('w3tc' => array('version' => W3TC_VERSION, 'server' => !empty($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : 'Unknown', 'dir' => W3TC_DIR, 'cache_dir' => W3TC_CACHE_DIR, 'blog_id' => Util_Environment::blog_id(), 'home_domain_root_url' => Util_Environment::home_domain_root_url(), 'home_url_maybe_https' => Util_Environment::home_url_maybe_https(), 'site_path' => Util_Environment::site_path(), 'document_root' => Util_Environment::document_root(), 'site_root' => Util_Environment::site_root(), 'site_url_uri' => Util_Environment::site_url_uri(), 'home_url_host' => Util_Environment::home_url_host(), 'home_url_uri' => Util_Environment::home_url_uri(), 'network_home_url_uri' => Util_Environment::network_home_url_uri(), 'host_port' => Util_Environment::host_port(), 'host' => Util_Environment::host(), 'wp_config_path' => Util_Environment::wp_config_path()), 'wp' => array('version' => $wp_version, 'db_version' => $wp_db_version, 'abspath' => ABSPATH, 'home' => get_option('home'), 'siteurl' => get_option('siteurl'), 'email' => get_option('admin_email'), 'upload_info' => (array) Util_Http::upload_info(), 'theme' => Util_Theme::get_current_theme(), 'wp_cache' => defined('WP_CACHE') && WP_CACHE ? 'true' : 'false', 'plugins' => $wordpress_plugins_active), 'mysql' => array('version' => $mysql_version, 'variables' => $mysql_variables)); return $server_info; }
/** * PHP5 style constructor */ function __construct() { global $_wp_using_ext_object_cache; $this->_config = Dispatcher::config(); $this->_lifetime = $this->_config->get_integer(array('fragmentcache', 'lifetime')); $this->_debug = $this->_config->get_boolean(array('fragmentcache', 'debug')); $this->_caching = $_wp_using_ext_object_cache = $this->_can_cache(); $this->_blog_id = Util_Environment::blog_id(); $this->_core = Dispatcher::component('Extension_FragmentCache_Core'); }
/** * Exports custom files to CDN * * @return array */ function get_files_custom() { $files = array(); $document_root = Util_Environment::document_root(); $custom_files = $this->_config->get_array('cdn.custom.files'); $custom_files = array_map(array('\\W3TC\\Util_Environment', 'parse_path'), $custom_files); $site_root = Util_Environment::site_root(); $path = Util_Environment::site_url_uri(); $site_root_dir = str_replace($document_root, '', $site_root); if (strstr(WP_CONTENT_DIR, Util_Environment::site_root()) === false) { $site_root = Util_Environment::document_root(); $path = ''; } $content_path = trim(str_replace(WP_CONTENT_DIR, '', $site_root), '/\\'); foreach ($custom_files as $custom_file) { if ($custom_file != '') { $custom_file = Cdn_Util::replace_folder_placeholders($custom_file); $custom_file = Util_Environment::normalize_file($custom_file); if (!Util_Environment::is_wpmu()) { $dir = trim(dirname($custom_file), '/\\'); $rel_path = trim(dirname($custom_file), '/\\'); } else { $rel_path = $dir = trim(dirname($custom_file), '/\\'); } if (strpos($dir, '<currentblog>') != false) { $rel_path = $dir = str_replace('<currentblog>', 'blogs.dir/' . Util_Environment::blog_id(), $dir); } if ($dir == '.') { $rel_path = $dir = ''; } $mask = basename($custom_file); $files = array_merge($files, Cdn_Util::search_files($document_root . '/' . $dir, $rel_path, $mask)); } } return $files; }
/** * Sends messages stored in $messages * * @return boolean */ public function execute_delayed_operations() { if (count($this->messages) <= 0) { return true; } $this->_log($this->_get_action() . ' sending messages'); $message = array(); $message['actions'] = $this->messages; $message['blog_id'] = Util_Environment::blog_id(); $message['host'] = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null; $message['hostname'] = @gethostname(); $v = json_encode($message); try { $api = $this->_get_api(); if (defined('WP_CLI') && WP_CLI) { $origin = 'WP CLI'; } else { $origin = 'WP'; } $this->_log($origin . ' sending message ' . $v); $this->_log('Host: ' . $message['host']); if (isset($_SERVER['REQUEST_URI'])) { $this->_log('URL: ' . $_SERVER['REQUEST_URI']); } if (function_exists('current_filter')) { $this->_log('Current WP hook: ' . current_filter()); } $backtrace = debug_backtrace(); $backtrace_optimized = array(); foreach ($backtrace as $b) { $opt = isset($b['function']) ? $b['function'] . ' ' : ''; $opt .= isset($b['file']) ? $b['file'] . ' ' : ''; $opt .= isset($b['line']) ? '#' . $b['line'] . ' ' : ''; $backtrace_optimized[] = $opt; } $this->_log('Backtrace ', $backtrace_optimized); $r = $api->publish($this->_topic_arn, $v); if ($r->status != 200) { $this->_log("Error: {$r->body->Error->Message}"); return false; } } catch (\Exception $e) { $this->_log('Error ' . $e->getMessage()); return false; } // on success - reset messages array, but not hash (not resent repeatedly the same messages) $this->messages = array(); return true; }
/** * Deploys the config file from a preview config file * * @param integer $direction +1: preview->production * -1: production->preview * @param boolean $remove_source remove source file */ private function preview_production_copy($direction = 1) { $blog_id = Util_Environment::blog_id(); $preview_filename = Config::util_config_filename($blog_id, true); $production_filename = Config::util_config_filename($blog_id, false); if ($direction > 0) { $src = $preview_filename; $dest = $production_filename; } else { $src = $production_filename; $dest = $preview_filename; } if (!@copy($src, $dest)) { Util_Activation::throw_on_write_error($dest); } }
/** * Parses path * * @param string $path * @return mixed */ public static function parse_path($path) { $path = str_replace(array('%BLOG_ID%', '%POST_ID%', '%BLOG_ID%', '%HOST%'), array(isset($GLOBALS['blog_id']) ? (int) $GLOBALS['blog_id'] : 0, isset($GLOBALS['post_id']) ? (int) $GLOBALS['post_id'] : 0, Util_Environment::blog_id(), Util_Environment::host()), $path); return $path; }
/** * PHP5 style constructor */ function __construct() { global $_wp_using_ext_object_cache; $this->_config = Dispatcher::config(); $this->_lifetime = $this->_config->get_integer('objectcache.lifetime'); $this->_debug = $this->_config->get_boolean('objectcache.debug'); $this->_caching = $_wp_using_ext_object_cache = $this->_can_cache(); $this->global_groups = $this->_config->get_array('objectcache.groups.global'); $this->nonpersistent_groups = $this->_config->get_array('objectcache.groups.nonpersistent'); $this->_blog_id = Util_Environment::blog_id(); }
/** * Returns cache object * * @return W3_Cache_Base */ function _get_cache() { static $cache = null; if (is_null($cache)) { $engine = $this->_config->get_string('pgcache.engine'); switch ($engine) { case 'memcached': $engineConfig = array('servers' => $this->_config->get_array('pgcache.memcached.servers'), 'persistent' => $this->_config->get_boolean('pgcache.memcached.persistent'), 'aws_autodiscovery' => $this->_config->get_boolean('pgcache.memcached.aws_autodiscovery'), 'username' => $this->_config->get_boolean('pgcache.memcached.username'), 'password' => $this->_config->get_boolean('pgcache.memcached.password')); break; case 'redis': $engineConfig = array('servers' => $this->_config->get_array('pgcache.redis.servers'), 'persistent' => $this->_config->get_boolean('pgcache.redis.persistent'), 'dbid' => $this->_config->get_boolean('pgcache.redis.dbid'), 'password' => $this->_config->get_boolean('pgcache.redis.password')); break; case 'file': $engineConfig = array('section' => 'page', 'flush_parent' => Util_Environment::blog_id() == 0, 'locking' => $this->_config->get_boolean('pgcache.file.locking'), 'flush_timelimit' => $this->_config->get_integer('timelimit.cache_flush')); break; case 'file_generic': if (Util_Environment::blog_id() == 0) { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR; } else { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . Util_Environment::host(); } $engineConfig = array('exclude' => array('.htaccess'), 'expire' => $this->_lifetime, 'cache_dir' => W3TC_CACHE_PAGE_ENHANCED_DIR, 'locking' => $this->_config->get_boolean('pgcache.file.locking'), 'flush_timelimit' => $this->_config->get_integer('timelimit.cache_flush'), 'flush_dir' => $flush_dir); break; default: $engineConfig = array(); } $engineConfig['use_expired_data'] = true; $engineConfig['module'] = 'pgcache'; $engineConfig['host'] = ''; // host is always put to a key $engineConfig['instance_id'] = Util_Environment::instance_id(); $cache = Cache::instance($engine, $engineConfig); } return $cache; }