function fix_in_wpadmin($config, $force_all_checks = false) { $exs = new SelfTestExceptions(); $fix_on_event = false; if (w3_is_multisite() && w3_get_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 (SelfTestExceptions $ex) { $exs->push($ex); } } if (count($exs->exceptions()) > 0) { throw $exs; } }
/** * @param W3_Config $config * @param W3_Config $config_master * @param bool $do_merge if to merge with network main site * @return string */ public static function get_wordpress_appname($config, $config_master, $do_merge = true) { if (w3_is_network()) { if ($config_master->get_boolean('newrelic.use_network_wide_id')) { $appname = $config_master->get_string('newrelic.appname'); } else { $merge = $config->get_boolean('newrelic.merge_with_network'); $merge_name = ''; if ($do_merge && $merge && w3_get_blog_id() != 0) { $merge_name = ';' . $config_master->get_string('newrelic.appname'); } if (w3_get_blog_id() != 0 && !$config->get_boolean('common.force_master')) { $appname = $config->get_string('newrelic.appname', ''); if (empty($appname)) { $prefix = $config->get_string('newrelic.appname_prefix'); $appname = $prefix . trim(w3_get_home_domain() . w3_get_site_path(), '/'); } } else { if (w3_get_blog_id() != 0) { $prefix = $config->get_string('newrelic.appname_prefix'); $appname = $prefix . trim(w3_get_home_domain() . w3_get_site_path(), '/'); } else { $appname = $config->get_string('newrelic.appname'); } } $appname = $appname . $merge_name; } } else { $appname = $config->get_string('newrelic.appname'); } return $appname; }
/** * 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; }
/** * Updates the plugin from older version. */ function w3_run_legacy_update() { w3_require_once(W3TC_LIB_W3_DIR . '/ConfigWriter.php'); $writer = new W3_ConfigWriter(w3_get_blog_id(), w3_is_preview_mode()); $writer->import_legacy_config_and_save(); // Only remove folders when master blog is running. if (w3_get_blog_id() == 0) { w3_remove_old_folders(); } }
/** * 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'] = w3_get_blog_id(); } $instance_key = sprintf('%s_%s', $engine, md5(serialize($config))); if (!isset($instances[$instance_key])) { switch ($engine) { case W3TC_CACHE_MEMCACHED: w3_require_once(W3TC_LIB_W3_DIR . '/Cache/Memcached.php'); $instances[$instance_key] = new W3_Cache_Memcached($config); break; case W3TC_CACHE_APC: w3_require_once(W3TC_LIB_W3_DIR . '/Cache/Apc.php'); $instances[$instance_key] = new W3_Cache_Apc($config); break; case W3TC_CACHE_EACCELERATOR: w3_require_once(W3TC_LIB_W3_DIR . '/Cache/Eaccelerator.php'); $instances[$instance_key] = new W3_Cache_Eaccelerator($config); break; case W3TC_CACHE_XCACHE: w3_require_once(W3TC_LIB_W3_DIR . '/Cache/Xcache.php'); $instances[$instance_key] = new W3_Cache_Xcache($config); break; case W3TC_CACHE_WINCACHE: w3_require_once(W3TC_LIB_W3_DIR . '/Cache/Wincache.php'); $instances[$instance_key] = new W3_Cache_Wincache($config); break; case W3TC_CACHE_FILE: w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File.php'); $instances[$instance_key] = new W3_Cache_File($config); break; case W3TC_CACHE_FILE_GENERIC: w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File/Generic.php'); $instances[$instance_key] = new W3_Cache_File_Generic($config); break; default: trigger_error('Incorrect cache engine', E_USER_WARNING); w3_require_once(W3TC_LIB_W3_DIR . '/Cache/Base.php'); $instances[$instance_key] = new W3_Cache_Base($config); break; } if (!$instances[$instance_key]->available()) { w3_require_once(W3TC_LIB_W3_DIR . '/Cache/Base.php'); $instances[$instance_key] = new W3_Cache_Base($config); } } return $instances[$instance_key]; }
/** * Constructor */ function __construct($master = false) { $preview = w3_is_preview_mode(); if (defined('WP_ADMIN')) { $config_admin = w3_instance('W3_ConfigAdmin'); $preview = $config_admin->get_boolean('previewmode.enabled'); } if ($master) { $this->_blog_id = 0; } else { $this->_blog_id = w3_get_blog_id(); } $this->_preview = $preview; $this->load(); }
function change_filename_length() { try { w3_require_once(W3TC_LIB_W3_DIR . '/Request.php'); $new = W3_Request::get_integer('maxlength'); $this->_config->set('minify.auto.filename_length', $new); set_transient('w3tc_minify_tested_filename_length', true, 3600 * 24); $this->_config->save(); w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/Cache/File.php'); $cache = new Minify_Cache_File(w3_cache_blog_dir('minify'), array('.htaccess', 'index.php', '*.old'), $this->_config->get_boolean('minify.file.locking'), $this->_config->get_integer('timelimit.cache_flush'), w3_get_blog_id() == 0 ? W3TC_CACHE_MINIFY_DIR : null); $cache->flush(); echo 1; } catch (Exception $ex) { echo $ex->getMessage(); } exit; }
function cleanup_local() { $engine = $this->_config->get_string('pgcache.engine'); switch ($engine) { case 'file': w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File/Cleaner.php'); $w3_cache_file_cleaner = new W3_Cache_File_Cleaner(array('cache_dir' => w3_cache_blog_dir('page'), 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc'))); $w3_cache_file_cleaner->clean(); break; case 'file_generic': w3_require_once(W3TC_LIB_W3_DIR . '/Cache/File/Cleaner/Generic.php'); if (w3_get_blog_id() == 0) { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR; } else { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . w3_get_domain(w3_get_host()); } $w3_cache_file_cleaner_generic = new W3_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; } }
function admin_notices() { /** * @var $nerser W3_NewRelicService */ $nerser = w3_instance('W3_NewRelicService'); $new_relic_configured = $this->_config->get_string('newrelic.account_id') && $this->_config->get_string('newrelic.api_key'); if (w3_get_blog_id() == 0 || !$this->_config->get_boolean('common.force_master')) { $new_relic_configured = $new_relic_configured && $this->_config->get_string('newrelic.application_id'); } $verify_running_result = $nerser->verify_running(); $not_running = is_array($verify_running_result); if ($not_running) { $message = '<p>' . __('New Relic is not running correctly. The plugin has detected the following issues:', 'w3-total-cache') . "</p>\n"; $message .= "<ul class=\"w3-bullet-list\">\n"; foreach ($verify_running_result as $cause) { $message .= "<li>{$cause}</li>"; } $message .= "</ul>\n"; $message .= "<p>" . sprintf(__('Please review the <a href="%s">settings</a>.', 'w3-total-cache'), network_admin_url('admin.php?page=w3tc_general#monitoring')) . "</p>"; w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/ui.php'); w3_e_error_box($message); } }
/** * Returns page key * * @param string $request_uri * @param string $compression * @return string */ function _get_page_key($request_uri, $compression) { if ($this->_config->get_string('pgcache.engine') == 'file_pgcache') { $request_uri = preg_replace('~\\?.*$~', '', $request_uri); $request_uri = str_replace('/index.php', '/', $request_uri); $request_uri = preg_replace('~[/\\\\]+~', '/', $request_uri); $request_uri = w3_realpath($request_uri); if (empty($request_uri)) { $request_uri = '/'; } if (substr($request_uri, -1) == '/') { $request_uri .= 'index.html'; } $request_uri = ltrim($request_uri, '/'); $key = sprintf('%s/%s', $_SERVER['HTTP_HOST'], $request_uri); if (!empty($compression)) { $key .= '.' . $compression; } } else { $blog_id = w3_get_blog_id(); if (empty($blog_id)) { $blog_id = $_SERVER['HTTP_HOST']; } $key = sprintf('w3tc_%s_page_%s', md5($blog_id), md5($request_uri)); if (!empty($compression)) { $key .= '_' . $compression; } } return $key; }
/** * PHP5 style constructor */ function __construct() { global $_wp_using_ext_object_cache; $this->_config = w3_instance('W3_Config'); $this->_lifetime = $this->_config->get_integer('fragmentcache.lifetime'); $this->_debug = $this->_config->get_boolean('fragmentcache.debug'); $this->_caching = $_wp_using_ext_object_cache = $this->_can_cache(); $this->_blog_id = w3_get_blog_id(); $this->_fragmentcache = w3_instance('W3_Pro_Plugin_FragmentCache'); }
/** * Returns cache object * * @return W3_Cache_Base */ function _get_cache() { static $cache = array(); if (!isset($cache[0])) { $engine = $this->_config->get_string('pgcache.engine'); switch ($engine) { case 'memcached': $engineConfig = array('servers' => $this->_config->get_array('pgcache.memcached.servers'), 'persistant' => $this->_config->get_boolean('pgcache.memcached.persistant')); break; case 'file': $engineConfig = array('section' => 'page', 'flush_parent' => w3_get_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 (w3_get_blog_id() == 0) { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR; } else { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . w3_get_domain(w3_get_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'] = w3_get_host(); $engineConfig['instance_id'] = w3_get_instance_id(); w3_require_once(W3TC_LIB_W3_DIR . '/Cache.php'); $cache[0] = W3_Cache::instance($engine, $engineConfig); } return $cache[0]; }
<td> <input name="newrelic.include_rum" type="hidden" value="0" /> <input id="newrelic_include_rum" name="newrelic.include_rum" type="checkbox" value="1" <?php checked($this->_config->get_boolean('newrelic.include_rum')); ?> /> <p><span class="description"> <?php _e('This enables inclusion of <acronym title="Real User Monitoring">RUM</acronym> when using Page Cache together with Browser Cache gzip or when using Page Cache with Disc: Enhanced', 'w3-total-cache'); ?> </span> </p> </td> </tr> <?php if (w3_is_network() && w3_get_blog_id() == 0) { ?> <tr> <th><label for="newrelic_appname_prefix"><?php _e('Prefix network sites:', 'w3-total-cache'); ?> </label></th> <td><input id="newrelic_appname_prefix" name="newrelic.appname_prefix" type="text" value="<?php echo esc_attr($this->_config->get_string('newrelic.appname_prefix')); ?> " /></td> </tr> <tr> <th><label for="newrelic_merge_with_network"><?php _e('Include network sites stats in network:', 'w3-total-cache'); ?>
/** * 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': w3_require_once(W3TC_LIB_W3_DIR . '/Cache/Memcached.php'); w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/Cache/Memcache.php'); $w3_cache_memcached = new W3_Cache_Memcached(array('blog_id' => w3_get_blog_id(), 'instance_id' => w3_get_instance_id(), 'host' => w3_get_host(), 'module' => 'minify', 'servers' => $this->_config->get_array('minify.memcached.servers'), 'persistant' => $this->_config->get_boolean('minify.memcached.persistant'))); $cache[0] = new Minify_Cache_Memcache($w3_cache_memcached, 0, w3_get_blog_id(), w3_get_instance_id()); break; case 'apc': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/Cache/APC.php'); $cache[0] = new Minify_Cache_APC(0, w3_get_blog_id(), w3_get_instance_id()); break; case 'eaccelerator': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/Cache/Eaccelerator.php'); $cache[0] = new Minify_Cache_Eaccelerator(0, w3_get_blog_id(), w3_get_instance_id()); break; case 'xcache': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/Cache/XCache.php'); $cache[0] = new Minify_Cache_XCache(0, w3_get_blog_id(), w3_get_instance_id()); break; case 'wincache': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/Cache/Wincache.php'); $cache[0] = new Minify_Cache_Wincache(0, w3_get_blog_id(), w3_get_instance_id()); break; case 'file': default: w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/Cache/File.php'); $cache[0] = new Minify_Cache_File(w3_cache_blog_dir('minify'), array('.htaccess', 'index.php', '*.old'), $this->_config->get_boolean('minify.file.locking'), $this->_config->get_integer('timelimit.cache_flush'), w3_get_blog_id() == 0 ? W3TC_CACHE_MINIFY_DIR : null); break; } } return $cache[0]; }
/** * Called from admin interface after configuration is changed */ function after_config_change() { $this->schedule(); if ($this->_config->get_boolean('minify.enabled') && $this->_config->get_boolean('minify.rewrite')) { if (w3_can_modify_rules(w3_get_minify_rules_core_path())) { $this->write_rules_core(); } if ($this->_config->get_string('minify.engine') == 'file') { if (w3_can_modify_rules(w3_get_minify_rules_cache_path())) { try { $this->write_rules_cache(); } catch (Exception $e) { } } } else { if (w3_can_modify_rules(w3_get_minify_rules_cache_path()) && w3_get_blog_id() == 0) { $this->remove_rules_cache(); } } } else { if (w3_can_modify_rules(w3_get_minify_rules_core_path()) && w3_get_blog_id() == 0) { $this->remove_rules_core(); } if (w3_can_modify_rules(w3_get_minify_rules_cache_path()) && w3_get_blog_id() == 0) { $this->remove_rules_cache(); } } }
/** * Options save action * * @return void */ function action_save_options() { if (!current_user_can('manage_options')) { wp_die(__('You do not have the rights to perform this action.', 'w3-total-cache')); } /** * Redirect params */ $params = array(); /** * Store error message regarding permalink not enabled */ $redirect_permalink_error = ''; /** * Read config * We should use new instance of WP_Config object here */ $config = new W3_Config(); $this->read_request($config); $config_admin = new W3_ConfigAdmin(); $this->read_request($config_admin); if ($this->_page == 'w3tc_dashboard') { if (W3_Request::get_boolean('maxcdn')) { $config->set('cdn.enabled', true); $config->set('cdn.engine', 'maxcdn'); } } /** * General tab */ if ($this->_page == 'w3tc_general') { $file_nfs = W3_Request::get_boolean('file_nfs'); $file_locking = W3_Request::get_boolean('file_locking'); $config->set('pgcache.file.nfs', $file_nfs); $config->set('minify.file.nfs', $file_nfs); $config->set('dbcache.file.locking', $file_locking); $config->set('objectcache.file.locking', $file_locking); $config->set('pgcache.file.locking', $file_locking); $config->set('minify.file.locking', $file_locking); if (is_network_admin()) { if ($this->_config->get_boolean('common.force_master') !== $config->get_boolean('common.force_master') || !w3_force_master() && $this->_config->get_boolean('common.force_master') && $config->get_boolean('common.force_master') || w3_force_master() && !$this->_config->get_boolean('common.force_master') && !$config->get_boolean('common.force_master')) { @unlink(W3TC_CACHE_BLOGMAP_FILENAME); $blogmap_dir = dirname(W3TC_CACHE_BLOGMAP_FILENAME) . '/' . basename(W3TC_CACHE_BLOGMAP_FILENAME, '.php') . '/'; if (@is_dir($blogmap_dir)) { w3_rmdir($blogmap_dir); } } if ($config->get_boolean('common.force_master')) { $config_admin->set('common.visible_by_master_only', true); } } /** * Check permalinks for page cache */ if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_generic' && !get_option('permalink_structure')) { $config->set('pgcache.enabled', false); $redirect_permalink_error = 'fancy_permalinks_disabled_pgcache'; } /** * Get New Relic application id */ if ($config->get_boolean('newrelic.enabled')) { $method = W3_Request::get_string('application_id_method'); $newrelic_prefix = ''; if (w3_is_network() && w3_get_blog_id() != 0) { $newrelic_prefix = $this->_config->get_string('newrelic.appname_prefix'); } if (($newrelic_api_key = $config->get_string('newrelic.api_key')) && !$config->get_string('newrelic.account_id')) { $nerser = w3_instance('W3_NewRelicService'); $account_id = $nerser->get_account_id($newrelic_api_key); $config->set('newrelic.account_id', $account_id); } if ($method == 'dropdown' && $config->get_string('newrelic.application_id')) { $application_id = $config->get_string('newrelic.application_id'); if ($config->get_string('newrelic.api_key') && $config->get_string('newrelic.account_id')) { w3_require_once(W3TC_LIB_W3_DIR . '/NewRelicService.php'); $nerser = new W3_NewRelicService($config->get_string('newrelic.api_key'), $config->get_string('newrelic.account_id')); $appname = $nerser->get_application_name($application_id); $config->set('newrelic.appname', $appname); } } else { if ($method == 'manual' && $config->get_string('newrelic.appname')) { if ($newrelic_prefix != '' && strpos($config->get_string('newrelic.appname'), $newrelic_prefix) === false) { $application_name = $newrelic_prefix . $config->get_string('newrelic.appname'); $config->set('newrelic.appname', $application_name); } else { $application_name = $config->get_string('newrelic.appname'); } if ($config->get_string('newrelic.api_key') && $config->get_string('newrelic.account_id')) { w3_require_once(W3TC_LIB_W3_DIR . '/NewRelicService.php'); $nerser = new W3_NewRelicService($config->get_string('newrelic.api_key'), $config->get_string('newrelic.account_id')); $application_id = $nerser->get_application_id($application_name); if ($application_id) { $config->set('newrelic.application_id', $application_id); } } } } } if ($config->get_boolean('minify.enabled') && !$this->_config->get_boolean('minify.enabled') || $config->get_boolean('minify.enabled') && $config->get_boolean('browsercache.enabled') && !$this->_config->get_boolean('browsercache.enabled') || $config->get_boolean('minify.enabled') && $config->get_boolean('minify.auto') && !$this->_config->get_boolean('minify.auto') || $config->get_boolean('minify.enabled') && $config->get_string('minify.engine') != $this->_config->get_string('minify.engine')) { delete_transient('w3tc_minify_tested_filename_length'); } if (!w3_is_pro($this->_config)) { delete_transient('w3tc_license_status'); } } /** * Minify tab */ if ($this->_page == 'w3tc_minify' && !$this->_config->get_boolean('minify.auto')) { $js_groups = array(); $css_groups = array(); $js_files = W3_Request::get_array('js_files'); $css_files = W3_Request::get_array('css_files'); foreach ($js_files as $theme => $templates) { foreach ($templates as $template => $locations) { foreach ((array) $locations as $location => $types) { foreach ((array) $types as $files) { foreach ((array) $files as $file) { if (!empty($file)) { $js_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file); } } } } } } foreach ($css_files as $theme => $templates) { foreach ($templates as $template => $locations) { foreach ((array) $locations as $location => $files) { foreach ((array) $files as $file) { if (!empty($file)) { $css_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file); } } } } } $config->set('minify.js.groups', $js_groups); $config->set('minify.css.groups', $css_groups); $js_theme = W3_Request::get_string('js_theme'); $css_theme = W3_Request::get_string('css_theme'); $params = array_merge($params, array('js_theme' => $js_theme, 'css_theme' => $css_theme)); } if ($this->_page == 'w3tc_minify') { if ($config->get_integer('minify.auto.filename_length') > 246) { $config->set('minify.auto.filename_length', 246); } delete_transient('w3tc_minify_tested_filename_length'); } /** * Browser Cache tab */ if ($this->_page == 'w3tc_browsercache') { if ($config->get_boolean('browsercache.enabled') && $config->get_boolean('browsercache.no404wp') && !get_option('permalink_structure')) { $config->set('browsercache.no404wp', false); $redirect_permalink_error = 'fancy_permalinks_disabled_browsercache'; } $config->set('browsercache.timestamp', time()); if (in_array($engine = $this->_config->get_string('cdn.engine'), array('netdna', 'maxcdn'))) { w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php'); $keys = explode('+', $this->_config->get_string('cdn.' . $engine . '.authorization_key')); if (sizeof($keys) == 3) { list($alias, $consumerkey, $consumersecret) = $keys; try { $api = new NetDNA($alias, $consumerkey, $consumersecret); $disable_cooker_header = $config->get_boolean('browsercache.other.nocookies') || $config->get_boolean('browsercache.cssjs.nocookies'); $api->update_pull_zone($this->_config->get_string('cdn.' . $engine . '.zone_id'), array('ignore_setcookie_header' => $disable_cooker_header)); } catch (Exception $ex) { } } } } /** * Mobile tab */ if ($this->_page == 'w3tc_mobile') { $groups = W3_Request::get_array('mobile_groups'); $mobile_groups = array(); $cached_mobile_groups = array(); foreach ($groups as $group => $group_config) { $group = strtolower($group); $group = preg_replace('~[^0-9a-z_]+~', '_', $group); $group = trim($group, '_'); if ($group) { $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default'; $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true; $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : ''; $agents = isset($group_config['agents']) ? explode("\r\n", trim($group_config['agents'])) : array(); $mobile_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'agents' => $agents); $cached_mobile_groups[$group] = $agents; } } /** * Allow plugins modify WPSC mobile groups */ $cached_mobile_groups = apply_filters('cached_mobile_groups', $cached_mobile_groups); /** * Merge existent and delete removed groups */ foreach ($mobile_groups as $group => $group_config) { if (isset($cached_mobile_groups[$group])) { $mobile_groups[$group]['agents'] = (array) $cached_mobile_groups[$group]; } else { unset($mobile_groups[$group]); } } /** * Add new groups */ foreach ($cached_mobile_groups as $group => $agents) { if (!isset($mobile_groups[$group])) { $mobile_groups[$group] = array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => $agents); } } /** * Allow plugins modify W3TC mobile groups */ $mobile_groups = apply_filters('w3tc_mobile_groups', $mobile_groups); /** * Sanitize mobile groups */ foreach ($mobile_groups as $group => $group_config) { $mobile_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => array()), $group_config); $mobile_groups[$group]['agents'] = array_unique($mobile_groups[$group]['agents']); $mobile_groups[$group]['agents'] = array_map('strtolower', $mobile_groups[$group]['agents']); sort($mobile_groups[$group]['agents']); } $enable_mobile = false; foreach ($mobile_groups as $group_config) { if ($group_config['enabled']) { $enable_mobile = true; break; } } $config->set('mobile.enabled', $enable_mobile); $config->set('mobile.rgroups', $mobile_groups); } /** * Referrer tab */ if ($this->_page == 'w3tc_referrer') { $groups = W3_Request::get_array('referrer_groups'); $referrer_groups = array(); foreach ($groups as $group => $group_config) { $group = strtolower($group); $group = preg_replace('~[^0-9a-z_]+~', '_', $group); $group = trim($group, '_'); if ($group) { $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default'; $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true; $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : ''; $referrers = isset($group_config['referrers']) ? explode("\r\n", trim($group_config['referrers'])) : array(); $referrer_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'referrers' => $referrers); } } /** * Allow plugins modify W3TC referrer groups */ $referrer_groups = apply_filters('w3tc_referrer_groups', $referrer_groups); /** * Sanitize mobile groups */ foreach ($referrer_groups as $group => $group_config) { $referrer_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'referrers' => array()), $group_config); $referrer_groups[$group]['referrers'] = array_unique($referrer_groups[$group]['referrers']); $referrer_groups[$group]['referrers'] = array_map('strtolower', $referrer_groups[$group]['referrers']); sort($referrer_groups[$group]['referrers']); } $enable_referrer = false; foreach ($referrer_groups as $group_config) { if ($group_config['enabled']) { $enable_referrer = true; break; } } $config->set('referrer.enabled', $enable_referrer); $config->set('referrer.rgroups', $referrer_groups); } /** * CDN tab */ if ($this->_page == 'w3tc_cdn') { $cdn_cnames = W3_Request::get_array('cdn_cnames'); $cdn_domains = array(); foreach ($cdn_cnames as $cdn_cname) { $cdn_cname = trim($cdn_cname); /** * Auto expand wildcard domain to 10 subdomains */ $matches = null; if (preg_match('~^\\*\\.(.*)$~', $cdn_cname, $matches)) { $cdn_domains = array(); for ($i = 1; $i <= 10; $i++) { $cdn_domains[] = sprintf('cdn%d.%s', $i, $matches[1]); } break; } if ($cdn_cname) { $cdn_domains[] = $cdn_cname; } } switch ($this->_config->get_string('cdn.engine')) { case 'ftp': $config->set('cdn.ftp.domain', $cdn_domains); break; case 's3': $config->set('cdn.s3.cname', $cdn_domains); break; case 'cf': $config->set('cdn.cf.cname', $cdn_domains); break; case 'cf2': $config->set('cdn.cf2.cname', $cdn_domains); break; case 'rscf': $config->set('cdn.rscf.cname', $cdn_domains); break; case 'azure': $config->set('cdn.azure.cname', $cdn_domains); break; case 'mirror': $config->set('cdn.mirror.domain', $cdn_domains); break; case 'maxcdn': $config->set('cdn.maxcdn.domain', $cdn_domains); break; case 'netdna': $config->set('cdn.netdna.domain', $cdn_domains); break; case 'cotendo': $config->set('cdn.cotendo.domain', $cdn_domains); break; case 'edgecast': $config->set('cdn.edgecast.domain', $cdn_domains); break; case 'att': $config->set('cdn.att.domain', $cdn_domains); break; case 'akamai': $config->set('cdn.akamai.domain', $cdn_domains); break; } } w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/extensions.php'); w3_extensions_admin_init(); $all_extensions = w3_get_extensions($config); $old_extensions = $this->_config->get_array('extensions.settings', array()); foreach ($all_extensions as $extension => $descriptor) { $extension_values = W3_Request::get_as_array('extensions.settings.'); $extension_keys = array(); $extension_settings = array(); $tmp_grp = str_replace('.', '_', $extension) . '_'; foreach ($extension_values as $key => $value) { if (strpos($key, $tmp_grp) !== false) { $extension_settings[str_replace($tmp_grp, '', $key)] = $value; } } if ($extension_settings) { $old_extension_settings = isset($old_extensions[$extension]) ? $old_extensions[$extension] : array(); if (!isset($old_extensions[$extension])) { $old_extensions[$extension] = array(); } $extension_keys[$extension] = apply_filters("w3tc_save_extension_settings-{$extension}", $extension_settings, $old_extension_settings); $new_settings = array_merge($old_extensions, $extension_keys); $config->set("extensions.settings", $new_settings); $old_extensions = $config->get_array('extensions.settings', array()); } } //CloudFront does not support expires header. So disable it when its used if ($config->get_string('cdn.engine') == 'cf2') { $config->set('browsercache.cssjs.expires', false); $config->set('browsercache.html.expires', false); $config->set('browsercache.other.expires', false); } $config = apply_filters('w3tc_save_options', $config, $this->_config, $config_admin); $config = apply_filters("w3tc_save_options-{$this->_page}", $config, $this->_config, $config_admin); do_action('w3tc_saving_options', $config, $this->_config, $config_admin); do_action("w3tc_saving_options-{$this->_page}", $config, $this->_config, $config_admin); w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/admin.php'); w3_config_save($this->_config, $config, $config_admin); switch ($this->_page) { case 'w3tc_cdn': /** * Handle Set Cookie Domain */ $set_cookie_domain_old = W3_Request::get_boolean('set_cookie_domain_old'); $set_cookie_domain_new = W3_Request::get_boolean('set_cookie_domain_new'); if ($set_cookie_domain_old != $set_cookie_domain_new) { if ($set_cookie_domain_new) { if (!$this->enable_cookie_domain()) { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'enable_cookie_domain'))); } } else { if (!$this->disable_cookie_domain()) { w3_admin_redirect(array_merge($params, array('w3tc_error' => 'disable_cookie_domain'))); } } } break; case 'w3tc_general': break; } $notes[] = 'config_save'; if ($redirect_permalink_error) { w3_admin_redirect(array('w3tc_error' => $redirect_permalink_error, 'w3tc_note' => 'config_save')); } w3_admin_redirect_with_custom_messages($params, null, $notes, true); }
/** * PHP5 style constructor */ function __construct() { global $_wp_using_ext_object_cache; $this->_config = w3_instance('W3_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 = w3_get_blog_id(); }
function setup() { /** * @var $nerser W3_NewRelicService */ $nerser = w3_instance('W3_NewRelicService'); $config_master = new W3_Config(true); if ($this->_config->get_boolean('newrelic.use_php_function')) { if (!$config_master->get_boolean('newrelic.use_network_wide_id') && (w3_get_blog_id() == 0 || !$this->_config->get_boolean('common.force_master'))) { $view_application = $this->_config->get_string('newrelic.application_id', 0); } else { $appname = NewRelicWrapper::get_wordpress_appname($this->_config, $config_master, false); $view_application = $nerser->get_application_id($appname); $nerser->set_application_id($view_application); } } else { $view_application = $this->_config->get_string('newrelic.application_id', 0); } $this->_application_id = $view_application; $this->_account_id = $this->_config->get_string('newrelic.account_id', 0); }
/** * Returns true if config section is sealed * @param string $section * @param W3_Config $config_master * @param W3_ConfigAdmin $config_admin * @return boolean */ private function is_sealed($section, $config_master, $config_admin) { if (w3_get_blog_id() == 0) { return false; } if (w3_is_network() && w3_get_blog_id() != 0 && w3_force_master()) { return true; } // browsercache settings change rules, so not available in child settings if ($section == 'browsercache') { return true; } if ($section == 'minify' && !$config_master->get_boolean('minify.enabled')) { return true; } return $config_admin->get_boolean($section . '.configuration_sealed'); }
/** * Parses pattransparentcdnh * * @param string $path * @return mixed */ function w3_parse_path($path) { $path = str_replace(array('%BLOG_ID%', '%POST_ID%', '%BLOG_ID%', '%HOST%', '%DOMAIN%', '%BASE_PATH%'), array(isset($GLOBALS['blog_id']) ? (int) $GLOBALS['blog_id'] : 0, isset($GLOBALS['post_id']) ? (int) $GLOBALS['post_id'] : 0, w3_get_blog_id(), w3_get_host(), w3_get_domain(w3_get_host()), trim(w3_get_base_path(), '/')), $path); return $path; }
/** * Convert relative file which is relative to ABSPATH (wp folder on disc) to path uri * * @param $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/' . w3_get_blog_id() . '/', '', $file); if (strpos($file, basename(WP_CONTENT_DIR)) === 0 && !w3_is_multisite()) { $remote_path = ''; } else { $remote_path = ltrim(w3_get_site_path(), "/"); } if ($remote_path == substr($file, 0, strlen($remote_path))) { return $file; } return $remote_path . $file; }
/** * @param W3_Config $config * @param $old * @param $config_admin * @return \W3_Config */ public function save_settings($config, $old, $config_admin) { w3_require_once(W3TC_CORE_EXTENSION_DIR . '/CloudFlare/CloudFlareAPI.php'); $this->cloudflareAPI = new CloudFlareAPI(); $this->cloudflareAPI->reset_settings_cache(); if ((bool) w3tc_get_extension_config('cloudflare', 'enabled') && $this->cloudflareAPI->minify_enabled() && $config->get_boolean('minify.enabled')) { $config->set('minify.enabled', false); } /** * Handle CloudFlare changes */ if (w3_get_blog_id() == 0 || w3_get_blog_id() != 0 && !w3_extension_is_sealed('cloudflare')) { /** * @var $this->cloudflareAPI W3_CloudFlare */ $cf_values = W3_Request::get_as_array('cloudflare_'); $this->cloudflareAPI->save_settings($cf_values); } return $config; }
/** * Returns server info * * @return array */ 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' => w3_get_blog_id(), 'document_root' => w3_get_document_root(), 'home_root' => w3_get_home_root(), 'site_root' => w3_get_site_root(), 'base_path' => w3_get_base_path(), 'home_path' => w3_get_home_path(), 'site_path' => w3_get_site_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) w3_upload_info(), 'theme' => w3tc_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; }
/** * Returns cache key * * @param string $id * @param string $group * @return string */ function _get_cache_key($id, $group = 'default') { if (!$group) { $group = 'default'; } $blog_id = w3_get_blog_id(); $key_cache_id = $blog_id . $group . $id; if (isset($this->_key_cache[$key_cache_id])) { $key = $this->_key_cache[$key_cache_id]; } else { $host = w3_get_host(); if (in_array($group, $this->global_groups)) { $host_id = $host; } else { $host_id = sprintf('%s_%d', $host, $blog_id); } $key = sprintf('w3tc_%s_object_%s', $host_id, md5($group . $id)); $this->_key_cache[$key_cache_id] = $key; } /** * Allow to modify cache key by W3TC plugins */ $key = w3tc_do_action('w3tc_objectcache_cache_key', $key); return $key; }
/** * Returns host ID * * @return string */ function w3_get_host_id() { static $host_id = null; if ($host_id === null) { $host = w3_get_host(); $blog_id = w3_get_blog_id(); $host_id = sprintf('%s_%d', $host, $blog_id); } return $host_id; }
/** * Exports custom files to CDN * * @return array */ function get_files_custom() { $files = array(); $document_root = w3_get_document_root(); $custom_files = $this->_config->get_array('cdn.custom.files'); $custom_files = array_map('w3_parse_path', $custom_files); $site_root = w3_get_site_root(); $path = w3_get_site_path(); if (strstr(WP_CONTENT_DIR, w3_get_site_root()) === false) { $site_root = w3_get_document_root(); $path = ''; } $content_path = trim(substr(WP_CONTENT_DIR, strlen($site_root)), '/\\'); $wp_content_folder = basename(WP_CONTENT_DIR); foreach ($custom_files as $custom_file) { if ($custom_file != '') { $custom_file = w3_normalize_file($custom_file); if (!w3_is_multisite()) { $dir = trim(dirname($path . $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/' . w3_get_blog_id(), $dir); } if ($content_path && $content_path != $wp_content_folder) { $dir = str_replace($wp_content_folder, $content_path, $dir); $rel_path = str_replace($wp_content_folder, $content_path, $rel_path); } if ($dir == '.') { $rel_path = $dir = ''; } $mask = basename($custom_file); $files = array_merge($files, $this->search_files($document_root . '/' . $dir, $rel_path, $mask)); } } return $files; }
/** * Export config action * * @return void */ function action_config_export() { @header(sprintf(__('Content-Disposition: attachment; filename=%s.php', 'w3-total-cache'), w3_get_blog_id())); echo $this->_config->export(); die; }
/** * Sends messages stored in $messages * * @return boolean */ public function send_messages() { if (count($this->messages) <= 0) { return true; } $this->_log($this->_get_action() . ' sending messages'); $message = array(); $message['actions'] = $this->messages; $message['blog_id'] = w3_get_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) { $backtrace_optimized[] = $b['function'] . ' ' . $b['file'] . '#' . $b['line']; } $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; }
function setup() { /** * @var $nerser W3_NewRelicService */ $nerser = w3_instance('W3_NewRelicService'); w3_require_once(W3TC_LIB_NEWRELIC_DIR . '/NewRelicWrapper.php'); $config_master = new W3_Config(true); $view_application = 0; if ($this->_config->get_boolean('newrelic.enabled')) { if ($this->_config->get_boolean('newrelic.use_php_function') || w3_is_multisite()) { if (!$config_master->get_boolean('newrelic.use_network_wide_id') && (w3_get_blog_id() == 0 || !$this->_config->get_boolean('common.force_master'))) { $view_application = $this->_config->get_string('newrelic.application_id', 0); } else { $appname = NewRelicWrapper::get_wordpress_appname($this->_config, $config_master, false); try { $view_application = $nerser->get_application_id($appname); $nerser->set_application_id($view_application); } catch (Exception $ex) { } } } else { $view_application = $this->_config->get_string('newrelic.application_id', 0); } } $this->_application_id = $view_application; $this->_account_id = $this->_config->get_string('newrelic.account_id', 0); }
/** * Returns cache key * * @param string $sql * @return string */ function _get_cache_key($sql) { $blog_id = w3_get_blog_id(); if (empty($blog_id)) { $blog_id = $_SERVER['HTTP_HOST']; } return sprintf('w3tc_%s_sql_%s', md5($blog_id), md5($sql)); }