/**
  * Runs plugin
  */
 function run()
 {
     add_filter('cron_schedules', array($this, 'cron_schedules'));
     if ($this->_config->get_string('dbcache.engine') == 'file') {
         add_action('w3_dbcache_cleanup', array($this, 'cleanup'));
     }
     add_action('publish_phone', array($this, 'on_change'), 0);
     add_action('wp_trash_post', array($this, 'on_post_change'), 0);
     add_action('save_post', array($this, 'on_post_change'), 0);
     add_action('clean_post_cache', array($this, 'on_post_change'), 0, 2);
     add_action('comment_post', array($this, 'on_comment_change'), 0);
     add_action('edit_comment', array($this, 'on_comment_change'), 0);
     add_action('delete_comment', array($this, 'on_comment_change'), 0);
     add_action('wp_set_comment_status', array($this, 'on_comment_status'), 0, 2);
     add_action('trackback_post', array($this, 'on_comment_change'), 0);
     add_action('pingback_post', array($this, 'on_comment_change'), 0);
     add_action('switch_theme', array($this, 'on_change'), 0);
     add_action('edit_user_profile_update', array($this, 'on_change'), 0);
     if (Util_Environment::is_wpmu()) {
         add_action('delete_blog', array($this, 'on_change'), 0);
     }
     add_action('delete_post', array($this, 'on_post_change'), 0);
     add_filter('w3tc_admin_bar_menu', array($this, 'w3tc_admin_bar_menu'));
     // usage statistics handling
     add_filter('w3tc_usage_statistics_metrics', array($this, 'w3tc_usage_statistics_metrics'));
 }
 /**
  * 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;
     }
 }
 /**
  * General tab
  *
  * @return void
  */
 function view()
 {
     global $current_user;
     $config_master = $this->_config_master;
     /**
      *
      *
      * @var $modules W3_ModuleStatus
      */
     $modules = Dispatcher::component('ModuleStatus');
     $pgcache_enabled = $modules->is_enabled('pgcache');
     $dbcache_enabled = $modules->is_enabled('dbcache');
     $objectcache_enabled = $modules->is_enabled('objectcache');
     $browsercache_enabled = $modules->is_enabled('browsercache');
     $minify_enabled = $modules->is_enabled('minify');
     $cdn_enabled = $modules->is_enabled('cdn');
     $varnish_enabled = $modules->is_enabled('varnish');
     $enabled = $modules->plugin_is_enabled();
     $enabled_checkbox = $modules->all_modules_enabled();
     $check_rules = Util_Rule::can_check_rules();
     $disc_enhanced_enabled = !(!$check_rules || !$this->is_master() && Util_Environment::is_wpmu() && $config_master->get_string('pgcache.engine') != 'file_generic');
     $can_empty_file = $modules->can_empty_file();
     $can_empty_varnish = $modules->can_empty_varnish();
     $cdn_mirror_purge = Cdn_Util::can_purge_all($modules->get_module_engine('cdn'));
     $file_nfs = $this->_config->get_boolean('pgcache.file.nfs') || $this->_config->get_boolean('minify.file.nfs');
     $file_locking = $this->_config->get_boolean('dbcache.file.locking') || $this->_config->get_boolean('objectcache.file.locking') || $this->_config->get_boolean('pgcache.file.locking') || $this->_config->get_boolean('minify.file.locking');
     $licensing_visible = (!Util_Environment::is_wpmu() || is_network_admin()) && !ini_get('w3tc.license_key') && get_transient('w3tc_license_status') != 'host_valid';
     $custom_areas = apply_filters("w3tc_settings_general_anchors", array());
     include W3TC_INC_DIR . '/options/general.php';
 }
 function generate($base_capability)
 {
     $pages = $this->generate_menu_array();
     add_menu_page(__('Performance', 'w3-total-cache'), __('Performance', 'w3-total-cache'), apply_filters('w3tc_capability_menu_w3tc_dashboard', $base_capability), 'w3tc_dashboard', '', 'div');
     $submenu_pages = array();
     $is_master = is_network_admin() || !Util_Environment::is_wpmu();
     $remaining_visible = !$this->_config->get_boolean('common.force_master');
     foreach ($pages as $slug => $titles) {
         if ($is_master || $titles['visible_always'] || $remaining_visible) {
             $submenu_pages[] = add_submenu_page('w3tc_dashboard', $titles['page_title'] . ' | W3 Total Cache', $titles['menu_text'], apply_filters('w3tc_capability_menu_' . $slug, $base_capability), $slug, array($this, 'options'));
         }
     }
     return $submenu_pages;
 }
 static function flush_posts_on_actions()
 {
     static $attached = false;
     if ($attached) {
         return;
     }
     $attached = true;
     $o = new Util_AttachToActions();
     add_action('clean_post_cache', array($o, 'on_post_change'), 0, 2);
     add_action('publish_post', array($o, 'on_post_change'), 0, 2);
     add_action('switch_theme', array($o, 'on_change'), 0);
     add_action('wp_update_nav_menu', array($o, 'on_change'), 0);
     add_action('edit_user_profile_update', array($o, 'on_change'), 0);
     if (Util_Environment::is_wpmu()) {
         add_action('delete_blog', array($o, 'on_change'), 0);
     }
     add_action('edited_term', array($o, 'on_change'), 0);
 }
 /**
  * Activate plugin action
  *
  * @param bool    $network_wide
  * @return void
  */
 public static function activate($network_wide)
 {
     // decline non-network activation at WPMU
     if (Util_Environment::is_wpmu()) {
         if ($network_wide) {
             // we are in network activation
         } else {
             if ($_GET['action'] == 'error_scrape' && strpos($_SERVER['REQUEST_URI'], '/network/') !== false) {
                 // workaround for error_scrape page called after error
                 // really we are in network activation and going to throw some error
             } else {
                 echo 'Please <a href="' . network_admin_url('plugins.php') . '">network activate</a> W3 Total Cache when using WordPress Multisite.';
                 die;
             }
         }
     }
     try {
         $e = Dispatcher::component('Root_Environment');
         $config = Dispatcher::config();
         $e->fix_on_event($config, 'activate');
         Generic_AdminLinks::link_update($config);
         // try to save config file if needed, optional thing so exceptions
         // hidden
         if (!file_exists(Config::util_config_filename(0, false))) {
             try {
                 // create folders
                 $e->fix_in_wpadmin($config);
             } catch (\Exception $ex) {
             }
             try {
                 Util_Admin::config_save(Dispatcher::config(), $config);
             } catch (\Exception $ex) {
             }
         }
     } catch (\Exception $e) {
         Util_Activation::error_on_exception($e);
     }
 }
 /**
  * Generates rules
  *
  * @param Config  $config
  * @return string
  */
 function rules_core_generate_nginx($config)
 {
     $cache_dir = Util_Rule::filename_to_uri(W3TC_CACHE_MINIFY_DIR);
     $first_regex_var = '$1';
     // for subdir - need to count subdir in url
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         // take into accont case when whole subdir wpmu is installed in subdir
         $home_uri = network_home_url('', 'relative');
         if (substr($cache_dir, 0, strlen($home_uri)) == $home_uri) {
             $cache_dir = $home_uri . '([a-z0-9]+/)?' . substr($cache_dir, strlen($home_uri));
         } else {
             $cache_dir = '(/[a-z0-9]+)?' . $cache_dir;
         }
         $first_regex_var = '$2';
     }
     $minify_uri = rtrim(network_site_url('', 'relative'), '/') . '/';
     $engine = $config->get_string('minify.engine');
     $browsercache = $config->get_boolean('browsercache.enabled');
     $compression = $browsercache && $config->get_boolean('browsercache.cssjs.compression') && !defined('W3TC_PAGECACHE_OUTPUT_COMPRESSION_OFF');
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_MINIFY_CORE . "\n";
     if ($engine == 'file') {
         $rules .= "set \$w3tc_enc \"\";\n";
         if ($compression) {
             $rules .= "if (\$http_accept_encoding ~ gzip) {\n";
             $rules .= "    set \$w3tc_enc .gzip;\n";
             $rules .= "}\n";
         }
         $rules .= "if (-f \$request_filename\$w3tc_enc) {\n";
         $rules .= "    rewrite (.*) \$1\$w3tc_enc break;\n";
         $rules .= "}\n";
     }
     $rules .= "rewrite ^{$cache_dir}/ {$minify_uri}index.php last;\n";
     $rules .= W3TC_MARKER_END_MINIFY_CORE . "\n";
     return $rules;
 }
 /**
  * Setup notices actions
  */
 function admin_init()
 {
     $capability = apply_filters('w3tc_capability_admin_notices', 'manage_options');
     if (current_user_can($capability)) {
         if (is_admin() && Util_Admin::is_w3tc_admin_page()) {
             /**
              * Only admin can see W3TC notices and errors
              */
             if (!Util_Environment::is_wpmu()) {
                 add_action('admin_notices', array($this, 'admin_notices'), 1, 1);
             }
             add_action('network_admin_notices', array($this, 'admin_notices'), 1, 1);
         }
     }
 }
Exemple #9
0
<?php

namespace W3TC;

if (!defined('W3TC')) {
    die;
}
$config = Dispatcher::config();
$page = Util_Admin::get_current_page();
$licensing_visible = (!Util_Environment::is_wpmu() || is_network_admin()) && !ini_get('w3tc.license_key') && get_transient('w3tc_license_status') != 'host_valid';
?>

<?php 
do_action('w3tc-dashboard-head');
?>
<div class="wrap" id="w3tc">
    <h2 class="logo"><?php 
_e('W3 Total Cache <span>by W3 EDGE <sup>&reg;</sup></span>', 'w3-total-cache');
?>
</h2>
<?php 
if (!Util_Environment::is_w3tc_pro($config)) {
    ?>
    <?php 
    include W3TC_INC_OPTIONS_DIR . '/edd/buy.php';
}
?>
    <?php 
switch ($page) {
    case 'w3tc_general':
        $anchors = array(array('id' => 'general', 'text' => __('General', 'w3-total-cache')), array('id' => 'page_cache', 'text' => __('Page Cache', 'w3-total-cache')), array('id' => 'minify', 'text' => 'Minify'), array('id' => 'system_opcache', 'text' => __('Opcode Cache', 'w3-total-cache')), array('id' => 'database_cache', 'text' => __('Database Cache', 'w3-total-cache')), array('id' => 'object_cache', 'text' => __('Object Cache', 'w3-total-cache')));
 function w3tc_errors($errors)
 {
     $c = Dispatcher::config();
     $state = Dispatcher::config_state();
     $cdn_engine = $c->get_string('cdn.engine');
     if (Cdn_Util::is_engine_push($cdn_engine)) {
         /**
          * Show notification if upload queue is not empty
          */
         try {
             if (!($error = get_transient('w3tc_cdn_error')) && !$this->_is_queue_empty()) {
                 $errors['cdn_unsuccessful_queue'] = sprintf(__('The %s has unresolved errors. Empty the queue to restore normal operation.', 'w3-total-cache'), Util_Ui::button_popup(__('unsuccessful transfer queue', 'w3-total-cache'), 'cdn_queue'));
             } elseif ($error) {
                 $errors['cdn_generic'] = $error;
             }
         } catch (\Exception $ex) {
             $errors[] = $ex->getMessage();
             set_transient('w3tc_cdn_error', $ex->getMessage(), 30);
         }
         /**
          * Check upload settings
          */
         $upload_info = Util_Http::upload_info();
         if (!$upload_info) {
             $upload_path = get_option('upload_path');
             $upload_path = trim($upload_path);
             if (empty($upload_path)) {
                 $upload_path = WP_CONTENT_DIR . '/uploads';
                 $errors['cdn_uploads_folder_empty'] = sprintf(__('The uploads directory is not available. Default WordPress directories will be created: <strong>%s</strong>.', 'w3-total-cache'), $upload_path);
             }
             if (!Util_Environment::is_wpmu()) {
                 $errors['cdn_uploads_folder_not_found'] = sprintf(__('The uploads path found in the database (%s) is inconsistent with the actual path. Please manually adjust the upload path either in miscellaneous settings or if not using a custom path %s automatically to resolve the issue.', 'w3-total-cache'), $upload_path, Util_Ui::button_link(__('update the path', 'w3-total-cache'), Util_Ui::url(array('w3tc_config_update_upload_path' => 'y'))));
             }
         }
     }
     /**
      * Check CDN settings
      */
     $error = '';
     switch (true) {
         case $cdn_engine == 'ftp' && !count($c->get_array('cdn.ftp.domain')):
             $errors['cdn_ftp_empty'] = __('A configuration issue prevents <acronym title="Content Delivery Network">CDN</acronym> from working:
                                     The <strong>"Replace default hostname with"</strong>
                                     field cannot be empty. Enter <acronym
                                     title="Content Delivery Network">CDN</acronym>
                                     provider hostname <a href="?page=w3tc_cdn#configuration">here</a>.
                                     <em>(This is the hostname used in order to view objects
                                     in a browser.)</em>', 'w3-total-cache');
             break;
         case $cdn_engine == 's3' && ($c->get_string('cdn.s3.key') == '' || $c->get_string('cdn.s3.secret') == '' || $c->get_string('cdn.s3.bucket') == ''):
             $error = __('The <strong>"Access key", "Secret key" and "Bucket"</strong> fields cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'cf' && ($c->get_string('cdn.cf.key') == '' || $c->get_string('cdn.cf.secret') == '' || $c->get_string('cdn.cf.bucket') == '' || $c->get_string('cdn.cf.id') == '' && !count($c->get_array('cdn.cf.cname'))):
             $error = __('The <strong>"Access key", "Secret key", "Bucket" and "Replace default hostname with"</strong> fields cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'cf2' && ($c->get_string('cdn.cf2.key') == '' || $c->get_string('cdn.cf2.secret') == '' || $c->get_string('cdn.cf2.id') == '' && !count($c->get_array('cdn.cf2.cname'))):
             $error = __('The <strong>"Access key", "Secret key" and "Replace default hostname with"</strong> fields cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'rscf' && ($c->get_string('cdn.rscf.user') == '' || $c->get_string('cdn.rscf.key') == '' || $c->get_string('cdn.rscf.container') == ''):
             $error = __('The <strong>"Username", "API key", "Container" and "Replace default hostname with"</strong> fields cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'azure' && ($c->get_string('cdn.azure.user') == '' || $c->get_string('cdn.azure.key') == '' || $c->get_string('cdn.azure.container') == ''):
             $error = __('The <strong>"Account name", "Account key" and "Container"</strong> fields cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'mirror' && !count($c->get_array('cdn.mirror.domain')):
             $error = __('The <strong>"Replace default hostname with"</strong> field cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'netdna':
             $fields = array();
             if ($c->get_string('cdn.netdna.authorization_key') == '') {
                 $fields[] = '"' . __('Authorization key', 'w3-total-cache') . '"';
             }
             if (!count($c->get_array('cdn.netdna.domain'))) {
                 $fields[] = '"' . __('Replace default hostname with', 'w3-total-cache') . '"';
             }
             if ($fields) {
                 $error = sprintf(__('The <strong>%s</strong> field(s) cannot be empty.', 'w3-total-cache'), implode(__(' and ', 'w3-total-cache'), $fields));
             }
             if ($c->get_string('cdn.netdna.authorization_key') != '' && sizeof(explode('+', $c->get_string('cdn.netdna.authorization_key'))) != 3) {
                 $error .= __('The <strong>"Authorization key"</strong> is not correct.', 'w3-total-cache');
             } elseif ($c->get_integer('cdn.netdna.zone_id', 0) <= 0) {
                 $error .= __('You need to select / create a pull zone.', 'w3-total-cache');
             }
             break;
         case $cdn_engine == 'maxcdn':
             $fields = array();
             if ($c->get_string('cdn.maxcdn.authorization_key') == '') {
                 $fields[] = '"' . __('Authorization key', 'w3-total-cache') . '"';
             }
             if (!count($c->get_array('cdn.maxcdn.domain'))) {
                 $fields[] = '"' . __('Replace default hostname with', 'w3-total-cache') . '"';
             }
             if ($fields) {
                 $error = sprintf(__('The <strong>%s</strong> field(s) cannot be empty.', 'w3-total-cache'), implode(__(' and ', 'w3-total-cache'), $fields));
             }
             if ($c->get_string('cdn.maxcdn.authorization_key') != '' && sizeof(explode('+', $c->get_string('cdn.maxcdn.authorization_key'))) != 3) {
                 $error .= __('The <strong>"Authorization key"</strong> is not correct.', 'w3-total-cache');
             } elseif ($c->get_integer('cdn.maxcdn.zone_id', 0) <= 0) {
                 $error .= __('You need to select / create a pull zone.', 'w3-total-cache');
             }
             break;
         case $cdn_engine == 'cotendo' && !count($c->get_array('cdn.cotendo.domain')):
             $error = __('The <strong>"Replace default hostname with"</strong> field cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'edgecast' && !count($c->get_array('cdn.edgecast.domain')):
             $error = __('The <strong>"Replace default hostname with"</strong> field cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'att' && !count($c->get_array('cdn.att.domain')):
             $error = __('The <strong>"Replace default hostname with"</strong> field cannot be empty.', 'w3-total-cache');
             break;
         case $cdn_engine == 'akamai' && !count($c->get_array('cdn.akamai.domain')):
             $error = 'The <strong>"Replace default hostname with"</strong> field cannot be empty.';
             break;
     }
     if ($error) {
         $errors['cdn_not_configured'] = __('A configuration issue prevents <acronym title="Content Delivery Network">CDN</acronym> from working: ', 'w3-total-cache') . $error . __(' <a href="?page=w3tc_cdn#configuration">Specify it here</a>.', 'w3-total-cache');
     }
     return $errors;
 }
                <?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>
        <tr>
            <th>
                <label for="newrelic_use_php_function"><?php 
_e('Use <acronym title=\\"Hypertext Preprocessor\\">PHP</acronym> function to set application name:', 'w3-total-cache');
?>
</label></th>
            <td>
                <?php 
if (Util_Environment::is_wpmu()) {
    ?>
                <input id="newrelic_use_php_function" name="extension__newrelic__use_php_function" type="checkbox" value="1" checked="checked" disabled="disabled" />
                    <p><span class="description">
                        <?php 
    _e('This is required when using New Relic on a network install to set the proper names for sites.', 'w3-total-cache');
    ?>
</span></p>
                <?php 
} else {
    ?>
                <input name="extension__newrelic__use_php_function" type="hidden" value="0" />
                <input id="newrelic_use_php_function" name="extension__newrelic__use_php_function" type="checkbox" value="1" <?php 
    checked($config->get_boolean(array('newrelic', 'use_php_function')));
    ?>
/>
 /**
  * Generate rules related to prevent for media 404 error by WP
  *
  * @param Config  $config
  * @return string
  */
 private function rules_no404wp_generate_apache($config)
 {
     $a = $this->get_mime_types();
     $cssjs_types = $a['cssjs'];
     $html_types = $a['html'];
     $other_types = $a['other'];
     $extensions = array_merge(array_keys($cssjs_types), array_keys($html_types), array_keys($other_types));
     $permalink_structure = get_option('permalink_structure');
     $permalink_structure_ext = ltrim(strrchr($permalink_structure, '.'), '.');
     if ($permalink_structure_ext != '') {
         foreach ($extensions as $index => $extension) {
             if (strstr($extension, $permalink_structure_ext) !== false) {
                 $extensions[$index] = preg_replace('~\\|?' . Util_Environment::preg_quote($permalink_structure_ext) . '\\|?~', '', $extension);
             }
         }
     }
     $exceptions = $config->get_array('browsercache.no404wp.exceptions');
     $wp_uri = network_home_url('', 'relative');
     $wp_uri = rtrim($wp_uri, '/');
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP . "\n";
     $rules .= "<IfModule mod_rewrite.c>\n";
     $rules .= "    RewriteEngine On\n";
     // in subdir - rewrite theme files and similar to upper folder if file exists
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         $rules .= "    RewriteCond %{REQUEST_FILENAME} !-f\n";
         $rules .= "    RewriteCond %{REQUEST_FILENAME} !-d\n";
         $rules .= "    RewriteCond %{REQUEST_URI} ^{$wp_uri}/([_0-9a-zA-Z-]+/)(.*\\.)(" . implode('|', $extensions) . ")\$ [NC]\n";
         $document_root = Util_Rule::apache_docroot_variable();
         $rules .= '    RewriteCond "' . $document_root . $wp_uri . '/%2%3" -f' . "\n";
         $rules .= "    RewriteRule .* {$wp_uri}/%2%3 [L]\n\n";
     }
     $rules .= "    RewriteCond %{REQUEST_FILENAME} !-f\n";
     $rules .= "    RewriteCond %{REQUEST_FILENAME} !-d\n";
     $imploded = implode('|', $exceptions);
     if (!empty($imploded)) {
         $rules .= "    RewriteCond %{REQUEST_URI} !(" . $imploded . ")\n";
     }
     $rules .= "    RewriteCond %{REQUEST_URI} \\.(" . implode('|', $extensions) . ")\$ [NC]\n";
     $rules .= "    RewriteRule .* - [L]\n";
     $rules .= "</IfModule>\n";
     $rules .= W3TC_MARKER_END_BROWSERCACHE_NO404WP . "\n";
     return $rules;
 }
 public function set_appname()
 {
     static $appname_set;
     if (!$appname_set && ($this->_config->get_boolean(array('newrelic', 'use_php_function')) || Util_Environment::is_wpmu())) {
         $appname_set = true;
         $service = Dispatcher::component('Extension_NewRelic_Service');
         $appname = $service->get_effective_appname();
         $enable_xmit = $this->_config->get_boolean(array('newrelic', 'enable_xmit'));
         \NewRelicWrapper::set_appname($appname, '', $enable_xmit);
     }
 }
</label>
                    <br /><span class="description"><?php 
_e('Try this option if your hosting environment uses a network based file system for a possible performance improvement.', 'w3-total-cache');
?>
</span>
                </th>
            </tr>
            <?php 
Util_Ui::config_item(array('key' => 'common.track_usage', 'control' => 'checkbox', 'checkbox_label' => __('Anonymously track usage to improve product quality', 'w3-total-cache'), 'style' => '2'));
?>

            <?php 
do_action('w3tc_settings_general_boxarea_miscellaneous_content');
?>
            <?php 
if (is_network_admin() || !Util_Environment::is_wpmu()) {
    ?>
            <tr id="edge_mode">
                <th colspan="2">
                    <?php 
    if (!Util_Environment::is_w3tc_edge($this->_config)) {
        echo '<a href="' . Util_Ui::url(array('w3tc_edge_mode_enable' => 'y')) . '"><strong>' . __('Enable Edge mode', 'w3-total-cache') . '</strong></a>';
    } else {
        echo '<a href="' . Util_Ui::url(array('w3tc_edge_mode_disable' => 'y')) . '"><strong>' . __('Disable Edge mode', 'w3-total-cache') . '</strong></a>';
    }
    ?>
                    <br /><span class="description"><?php 
    _e('Enable this to try out new functionality under development. Might cause issues on some sites.', 'w3-total-cache');
    ?>
</span>
                </th>
 /**
  * Flush cache when user profile is updated
  *
  * @param int     $user_id
  */
 function on_change_profile($user_id)
 {
     static $flushed = false;
     if (!$flushed) {
         if (Util_Environment::is_wpmu()) {
             $blogs = get_blogs_of_user($user_id, true);
             if ($blogs) {
                 global $w3_multisite_blogs;
                 $w3_multisite_blogs = $blogs;
             }
         }
         $flush = Dispatcher::component('CacheFlush');
         $flush->objectcache_flush();
         $flushed = true;
     }
 }
Exemple #16
0
 /**
  * Convert local uri path to CDN type specific path
  *
  * @param unknown $local_uri_path
  * @return string
  */
 function uri_to_cdn_uri($local_uri)
 {
     $local_uri = ltrim($local_uri, '/');
     $remote_uri = $local_uri;
     if (Util_Environment::is_wpmu() && defined('DOMAIN_MAPPING') && DOMAIN_MAPPING) {
         $remote_uri = str_replace(site_url(), '', $local_uri);
     }
     $engine = $this->_config->get_string('cdn.engine');
     if (Cdn_Util::is_engine_mirror($engine)) {
         if (Util_Environment::is_wpmu() && strpos($local_uri, 'files') === 0) {
             $upload_dir = Util_Environment::wp_upload_dir();
             $remote_uri = $this->abspath_to_relative_path(dirname($upload_dir['basedir'])) . '/' . $local_uri;
         }
     } elseif (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain() && Util_Environment::is_using_master_config() && Cdn_Util::is_engine_push($engine)) {
         // in common config files are uploaded for network home url
         // so mirror will not contain /subblog/ path in uri
         $home = trim(home_url('', 'relative'), '/') . '/';
         $network_home = trim(network_home_url('', 'relative'), '/') . '/';
         if ($home != $network_home && substr($local_uri, 0, strlen($home)) == $home) {
             $remote_uri = $network_home . substr($local_uri, strlen($home));
         }
     }
     return ltrim($remote_uri, '/');
 }
Exemple #17
0
 /**
  * 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;
 }
 /**
  * Flush cache based on regex
  *
  * @param string  $regex
  */
 private function _flush_based_on_regex($regex)
 {
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         $domain = get_home_url();
         $parsed = parse_url($domain);
         $host = $parsed['host'];
         $path = isset($parsed['path']) ? '/' . trim($parsed['path'], '/') : '';
         $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . $host . $path;
     } else {
         $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . Util_Environment::host();
     }
     $dir = @opendir($flush_dir);
     if ($dir) {
         while (($entry = @readdir($dir)) !== false) {
             if ($entry == '.' || $entry == '..') {
                 continue;
             }
             if (preg_match('/' . $regex . '/', basename($entry))) {
                 Util_File::rmdir($flush_dir . DIRECTORY_SEPARATOR . $entry);
             }
         }
         @closedir($dir);
     }
 }
 /**
  * Remove script tags from the source
  *
  * @param string  $content
  * @param array   $files
  * @return void
  */
 function remove_scripts(&$content, $files)
 {
     $regexps = array();
     $home_url_regexp = Util_Environment::home_url_regexp();
     $path = '';
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         $path = ltrim(Util_Environment::home_url_uri(), '/');
     }
     foreach ($files as $file) {
         if ($path && strpos($file, $path) === 0) {
             $file = substr($file, strlen($path));
         }
         $this->replaced_scripts[] = $file;
         if (Util_Environment::is_url($file) && !preg_match('~' . $home_url_regexp . '~i', $file)) {
             // external JS files
             $regexps[] = Util_Environment::preg_quote($file);
         } else {
             // local JS files
             $file = ltrim($file, '/');
             if (home_url() == site_url() && ltrim(Util_Environment::site_url_uri(), '/') && strpos($file, ltrim(Util_Environment::site_url_uri(), '/')) === 0) {
                 $file = str_replace(ltrim(Util_Environment::site_url_uri(), '/'), '', $file);
             }
             $file = ltrim(preg_replace('~' . $home_url_regexp . '~i', '', $file), '/\\');
             $regexps[] = '(' . $home_url_regexp . ')?/?' . Util_Environment::preg_quote($file);
         }
     }
     foreach ($regexps as $regexp) {
         $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content);
     }
 }
 /**
  * Returns path of pgcache cache rules file
  * Moved to separate file to not load rule.php for each disk enhanced request
  *
  * @return string
  */
 public static function get_pgcache_rules_cache_path()
 {
     switch (true) {
         case Util_Environment::is_apache():
         case Util_Environment::is_litespeed():
             if (Util_Environment::is_wpmu()) {
                 $url = get_home_url();
                 $match = null;
                 if (preg_match('~http(s)?://(.+?)(/)?$~', $url, $match)) {
                     $home_path = $match[2];
                     return W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . $home_path . '/.htaccess';
                 }
             }
             return W3TC_CACHE_PAGE_ENHANCED_DIR . '/.htaccess';
         case Util_Environment::is_nginx():
             return Util_Rule::get_nginx_rules_path();
     }
     return false;
 }
 /**
  * Generates directives for file cache dir
  *
  * @param Config  $config
  * @return string
  */
 private function rules_cache_generate_nginx($config)
 {
     $cache_root = Util_Environment::normalize_path(W3TC_CACHE_PAGE_ENHANCED_DIR);
     $cache_dir = rtrim(str_replace(Util_Environment::document_root(), '', $cache_root), '/');
     if (Util_Environment::is_wpmu()) {
         $cache_dir = preg_replace('~/w3tc.*?/~', '/w3tc.*?/', $cache_dir, 1);
     }
     $browsercache = $config->get_boolean('browsercache.enabled');
     $compression = $browsercache && $config->get_boolean('browsercache.html.compression');
     $expires = $browsercache && $config->get_boolean('browsercache.html.expires');
     $lifetime = $browsercache ? $config->get_integer('browsercache.html.lifetime') : 0;
     $cache_control = $browsercache && $config->get_boolean('browsercache.html.cache.control');
     $w3tc = $browsercache && $config->get_integer('browsercache.html.w3tc');
     $common_rules = '';
     if ($expires) {
         $common_rules .= "    expires modified " . $lifetime . "s;\n";
     }
     if ($w3tc) {
         $common_rules .= "    add_header X-Powered-By \"" . Util_Environment::w3tc_header($config) . "\";\n";
     }
     if ($expires) {
         $common_rules .= "    add_header Vary \"Accept-Encoding, Cookie\";\n";
     }
     if ($cache_control) {
         $cache_policy = $config->get_string('browsercache.html.cache.policy');
         switch ($cache_policy) {
             case 'cache':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"public\";\n";
                 break;
             case 'cache_public_maxage':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n";
                 break;
             case 'cache_validation':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n";
                 break;
             case 'cache_noproxy':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"private, must-revalidate\";\n";
                 break;
             case 'cache_maxage':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n";
                 break;
             case 'no_cache':
                 $common_rules .= "    add_header Pragma \"no-cache\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n";
                 break;
         }
     }
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_PGCACHE_CACHE . "\n";
     $rules .= "location ~ " . $cache_dir . ".*html\$ {\n";
     $rules .= $common_rules;
     $rules .= "}\n";
     if ($compression) {
         $rules .= "location ~ " . $cache_dir . ".*gzip\$ {\n";
         $rules .= "    gzip off;\n";
         $rules .= "    types {}\n";
         $rules .= "    default_type text/html;\n";
         $rules .= $common_rules;
         $rules .= "    add_header Content-Encoding gzip;\n";
         $rules .= "}\n";
     }
     $rules .= W3TC_MARKER_END_PGCACHE_CACHE . "\n";
     return $rules;
 }
 public static function cache_blog_minify_dir()
 {
     // when minify manual used with a shared config - shared
     // minify urls has to be used too, since CDN upload is possible
     // only from network admin
     if (Util_Environment::is_wpmu() && Util_Environment::is_using_master_config() && !Dispatcher::config()->get_boolean('minify.auto')) {
         $path = Util_Environment::cache_blog_dir('minify', 0);
     } else {
         $path = Util_Environment::cache_blog_dir('minify');
     }
     return $path;
 }