/** * Constructor */ function __construct() { $this->_blog_id = w3_get_blog_id(); // defines $keys_admin with descriptors include W3TC_LIB_W3_DIR . '/ConfigKeys.php'; $this->_keys = $keys_admin; $this->_aggregated_data_object = new W3_ConfigData($keys_admin); $this->_aggregated_data_object->set_defaults(); // load master-config data $filename1 = $this->_get_config_filename(true); $this->_aggregated_data_object->read($filename1); // load blog-config data (or master if we are master) $filename2 = $this->_get_config_filename(); $this->_data_object = new W3_ConfigData($keys_admin); // set defaults $this->_data_object->set_group($this->_aggregated_data_object->data); $data = W3_ConfigData::get_array_from_file($filename2); if (!is_null($data)) { foreach ($data as $key => $value) { if ($this->_writable_key($key)) { $this->_data_object->set($key, $value); } } } // merge blog-specific config data to aggregated data if ($this->_blog_id > 0) { $this->_aggregated_data_object->set_group($this->_data_object->data); } $this->_data =& $this->_aggregated_data_object->data; }
/** * Reads legacy config file * @param int $blog_id * @param bool $force_master * @return array */ public function get_imported_legacy_config_keys($blog_id, $force_master = false) { $suffix = ''; if ($force_master) { } else { if ($blog_id > 0) { if (w3_is_network()) { if (w3_is_subdomain_install()) { $suffix = '-' . w3_get_domain(w3_get_host()); } else { // try subdir blog $request_uri = rtrim($_SERVER['REQUEST_URI'], '/'); $site_home_uri = w3_get_base_path(); if (substr($request_uri, 0, strlen($site_home_uri)) == $site_home_uri) { $request_path_in_wp = '/' . substr($request_uri, strlen($site_home_uri)); $n = strpos($request_path_in_wp, '/', 1); if ($n === false) { $blog_path_in_wp = substr($request_path_in_wp, 1); } else { $blog_path_in_wp = substr($request_path_in_wp, 1, $n - 1); } $suffix = '-' . ($blog_path_in_wp != 'wp-admin' ? $blog_path_in_wp . '.' : '') . w3_get_domain(w3_get_host()); } } } } } $filename = WP_CONTENT_DIR . '/w3-total-cache-config' . $suffix . '.php'; $legacy_config = W3_ConfigData::get_array_from_file($filename); if (is_array($legacy_config) && isset($legacy_config['pgcache.engine']) && $legacy_config['pgcache.engine'] == 'file_pgcache') { $legacy_config['pgcache.engine'] = 'file_generic'; } return $legacy_config; }
/** * Reads config from file using "set" method to fill object with data. * * @param string $filename * @return boolean */ function read($filename) { $config = W3_ConfigData::get_array_from_file($filename); if (is_null($config)) { return false; } foreach ($config as $key => $value) { $this->set($key, $value); } return true; }
/** * @synopsis [--check] [--default-settings] [--preserve-cdn] * @subcommand total-cache * */ function w3tc($args, $assoc_args) { if (!file_exists(WP_CONTENT_DIR . "/plugins/w3-total-cache/")) { WP_CLI::warning("W3 Total Cache plugin is not installed into plugins/w3-total-cache/. Skipping."); exit; } if (!is_plugin_active("w3-total-cache/w3-total-cache.php")) { WP_CLI::warning("W3 Total Cache plugin is not enabled. Skipping."); exit; } $w3tc_version = get_plugin_data(WP_CONTENT_DIR . "/plugins/w3-total-cache/w3-total-cache.php"); $w3tc_version = $w3tc_version['Version']; $w3tc_master = $w3tc_version >= "0.9.2.6" ? WP_CONTENT_DIR . "/w3tc-config/master.php" : WP_CONTENT_DIR . "/w3-total-cache-config.php"; if (!file_exists($w3tc_master)) { WP_CLI::warning("W3 Total Cache config file can not be found. Skipping."); exit; } if (isset($assoc_args['check'])) { $file = fopen($w3tc_master, "r"); while (!feof($file)) { $line = fgets($file); if (preg_match('/pgcache\\.enabled\' => true/', $line)) { $pgcache = true; continue; } if (preg_match('/pgcache\\.engine\' => \'file\'/', $line)) { $pgcache_method = "disk: basic"; continue; } if (preg_match('/pgcache\\.engine\' => \'file_generic\'/', $line)) { $pgcache_method = "disk: enhanced"; continue; } if (preg_match('/pgcache\\.engine\' => \'apc\'/', $line)) { $pgcache_method = "opcode: apc"; continue; } if (preg_match('/minify\\.enabled\' => true/', $line)) { $minify = true; continue; } if (preg_match('/dbcache\\.enabled\' => true/', $line)) { $dbcache = true; continue; } if (preg_match('/objectcache\\.enabled\' => true/', $line)) { $objectcache = true; continue; } if (preg_match('/objectcache\\.engine\' => \'file\'/', $line)) { $objectcache_method = "disk"; continue; } if (preg_match('/objectcache\\.engine\' => \'apc\'/', $line)) { $objectcache_method = "apc"; continue; } if (preg_match('/fragmentcache\\.enabled\' => true/', $line)) { $fragmentcache = true; continue; } if (preg_match('/fragmentcache\\.engine\' => \'file\'/', $line)) { $fragmentcache_method = "disk"; continue; } if (preg_match('/fragmentcache\\.engine\' => \'apc\'/', $line)) { $fragmentcache_method = "apc"; continue; } if (preg_match('/browsercache\\.enabled\' => true/', $line)) { $browsercache = true; continue; } if (preg_match('/cdn\\.enabled\' => true/', $line)) { $cdn = true; continue; } } fclose($file); echo "[1;34mW3 Total Cache " . $w3tc_version . " (" . (file_exists("/var/run/apache2.pid") || file_exists("/run/apache2/apache2.pid") ? "apache+nginx" : "nginx+php5-fpm") . ")[0m\n"; echo "\n[34mPage Cache:[0m " . (isset($pgcache) ? "enabled (" . $pgcache_method . ")" : "-"); echo "\n[34mMinify:[0m " . (isset($minify) ? "enabled" : "-"); echo "\n[34mDatabase Cache:[0m " . (isset($dbcache) ? "enabled" : "-"); echo "\n[34mObject Cache:[0m " . (isset($objectcache) ? "enabled (" . $objectcache_method . ")" : "-"); echo "\n[34mFragment Cache:[0m " . (isset($fragmentcache) ? "enabled (" . $fragmentcache_method . ")" : "-"); echo "\n[34mBrowser Cache:[0m " . (isset($browsercache) ? "enabled" : "-"); echo "\n[34mCDN:[0m " . (isset($cdn) ? "enabled" : "-") . "\n"; } else { if (isset($assoc_args['default-settings'])) { $siteurl = preg_replace("/https?:\\/\\/(www.)?/", "", home_url()); if (strlen($siteurl) == 0 || !is_dir("/var/www/" . $siteurl)) { WP_CLI::error("Unable to find site root. Skipping."); return; } if (!is_writable("/var/www/" . $siteurl)) { WP_CLI::error("Site root is not writable. Please check the permissions. Skipping."); return; } $config = file_exists("/var/run/apache2.pid") || file_exists("/run/apache2/apache2.pid") ? "master_apache" : "master_nginx"; if (file_exists("/tmp/" . $config)) { WP_CLI::launch("rm /tmp/" . $config); } if (file_exists("/tmp/cdn_settings")) { WP_CLI::launch("rm /tmp/cdn_settings"); } $url = "https://tiagohillebrandt.eti.br/w3tc/" . $w3tc_version . "/" . $config; WP_CLI::launch("wget -q " . $url . " -P /tmp/ || echo -n"); if (!file_exists("/tmp/" . $config)) { WP_CLI::warning("W3TC version not compatible or server not available at this moment. Please import the settings manually."); exit; } $old_config = null; if (isset($assoc_args['preserve-cdn'])) { require_once W3TC_LIB_W3_DIR . "/ConfigData.php"; $cdn_url = "https://tiagohillebrandt.eti.br/w3tc/" . $w3tc_version . "/cdn_settings"; WP_CLI::launch("wget -q " . $cdn_url . " -P /tmp/ || echo -n"); if (!file_exists("/tmp/cdn_settings")) { WP_CLI::warning("Not able to preserve the CDN settings. Skipping."); exit; } $old_config = W3_ConfigData::get_array_from_file($w3tc_master); } $backup_file = preg_replace("/https?:\\/\\/(www.)?/", "", home_url()); $backup_file = $backup_file . ".master.php"; $backup_file = preg_replace("/\\//", "_", $backup_file); WP_CLI::launch("mv " . $w3tc_master . " /var/www/" . $siteurl . "/." . $backup_file); WP_CLI::line("Current settings backup: /var/www/" . $siteurl . "/." . $backup_file); WP_CLI::launch("rm " . WP_CONTENT_DIR . "/advanced-cache.php 2> /dev/null || echo -n"); WP_CLI::launch("rm " . WP_CONTENT_DIR . "/db.php 2> /dev/null || echo -n"); WP_CLI::launch("rm " . WP_CONTENT_DIR . "/object-cache.php 2> /dev/null || echo -n"); WP_CLI::launch("rm -r " . WP_CONTENT_DIR . "/cache/* 2> /dev/null || echo -n"); WP_CLI::launch("rm -r " . WP_CONTENT_DIR . "/w3tc/* 2> /dev/null || echo -n"); WP_CLI::launch("mv /tmp/" . $config . " " . $w3tc_master); WP_CLI::launch("chown -R \$(stat -c %U " . WP_CONTENT_DIR . "):\$(stat -c %U " . WP_CONTENT_DIR . ") " . $w3tc_master); if (!is_null($old_config)) { require_once W3TC_LIB_W3_DIR . "/Config.php"; $config = new W3_Config(); $cdn = fopen("/tmp/cdn_settings", "r"); while (!feof($cdn)) { $property = trim(fgets($cdn)); $config->set($property, $old_config[$property]); } fclose($cdn); $config->save(); } WP_CLI::success("W3TC recommended settings have been imported."); } } }
function create_compiled_config($cache_filename, $write = true) { /** * defines $keys with descriptors * @var array $keys config keys */ include W3TC_LIB_W3_DIR . '/ConfigKeys.php'; $compiled_config = new W3_ConfigData($keys); $compiled_config->set_defaults(); // collect config data from master config if (!$compiled_config->read($this->_get_config_filename(true))) { // try to read production master config $success = false; if ($this->_preview) { $success = $compiled_config->read($this->_get_config_filename(true, false)); } // try to read legacy master config if (!$success) { /** * @var W3_ConfigCompatibility $config_comp */ $config_comp = w3_instance('W3_ConfigCompatibility'); $data = $config_comp->get_imported_legacy_config_keys($this->_blog_id, true); if (!is_null($data)) { foreach ($data as $key => $value) { $compiled_config->set($key, $value); } } } } $this->_put_instance_value_in_config($compiled_config); // append data from blog config $config_admin = w3_instance('W3_ConfigAdmin'); $data = W3_ConfigData::get_array_from_file($this->_get_config_filename()); if (isset($data['version']) && version_compare($data['version'], W3TC_VERSION, '<')) { /** * @var W3_ConfigCompatibility $config_comp */ $config_comp = w3_instance('W3_ConfigCompatibility'); $config_comp->load_new_settings($data['version']); } if (is_null($data)) { /** * @var W3_ConfigCompatibility $config_comp */ $config_comp = w3_instance('W3_ConfigCompatibility'); $data = $config_comp->get_imported_legacy_config_keys($this->_blog_id); } if (!is_null($data)) { foreach ($data as $key => $value) { if (!$this->_key_sealed($key, $compiled_config->data, $config_admin, $value)) { $compiled_config->set($key, $value); } } } // save the value for 'home' in the config $this->_put_home_value_in_config($compiled_config->data); // save the bad_behavior path in config if plugin exists $this->_put_bad_behavior_in_config($compiled_config->data); $this->_post_process_values($compiled_config); // write cache if ($write) { try { $compiled_config->write($cache_filename, true); } catch (Exception $ex) { // dont care here about file permissions } $this->flush_apc($cache_filename, true); } return $compiled_config->data; }