Example #1
0
 /**
  * Link replace callback
  *
  * @param array $matches
  * @return string
  */
 function link_replace_callback($matches)
 {
     global $wpdb;
     static $queue = null, $reject_files = null;
     list($match, $quote, $url, , , , $path) = $matches;
     $path = ltrim($path, '/');
     /**
      * Check if URL was already replaced
      */
     if (isset($this->replaced_urls[$url])) {
         return $quote . $this->replaced_urls[$url];
     }
     /**
      * Check URL for rejected files
      */
     if ($reject_files === null) {
         $reject_files = $this->_config->get_array('cdn.reject.files');
     }
     foreach ($reject_files as $reject_file) {
         if ($reject_file != '') {
             $reject_file = w3_normalize_file($reject_file);
             $reject_file_regexp = '~^(' . $this->get_regexp_by_mask($reject_file) . ')~i';
             if (preg_match($reject_file_regexp, $path)) {
                 return $match;
             }
         }
     }
     /**
      * Don't replace URL for files that are in the CDN queue
      */
     if ($queue === null) {
         if (!w3_is_cdn_mirror($this->_config->get_string('cdn.engine'))) {
             $sql = sprintf('SELECT remote_path FROM %s', $wpdb->prefix . W3TC_CDN_TABLE_QUEUE);
             $queue = $wpdb->get_col($sql);
         } else {
             $queue = false;
         }
     }
     if ($queue && in_array($path, $queue)) {
         return $match;
     }
     /**
      * Do replacement
      */
     $cdn =& $this->_get_common()->get_cdn();
     $new_url = $cdn->format_url($path);
     if ($new_url) {
         $this->replaced_urls[$url] = $new_url;
         return $quote . $new_url;
     }
     return $match;
 }
Example #2
0
 /**
  * Link replace callback
  *
  * @param array $matches
  * @return string
  */
 function link_replace_callback($matches)
 {
     global $wpdb;
     static $queue = null, $reject_files = null;
     if (in_array($matches[2], $this->replaced_urls)) {
         return $matches[0];
     }
     if ($queue === null) {
         $sql = sprintf('SELECT remote_path FROM %s', $wpdb->prefix . W3TC_CDN_TABLE_QUEUE);
         $queue = $wpdb->get_col($sql);
     }
     if ($reject_files === null) {
         $reject_files = $this->_config->get_array('cdn.reject.files');
     }
     /**
      * Don't replace links that are in the queue
      */
     if (in_array(ltrim($matches[4], '/'), $queue)) {
         return $matches[0];
     }
     /**
      * Don't replace link for rejected files
      */
     foreach ($reject_files as $reject_file) {
         if ($reject_file != '') {
             $reject_file = w3_normalize_file($reject_file);
             $reject_file_regexp = '~^' . $this->get_regexp_by_mask($reject_file) . '$~i';
             if (preg_match($reject_file_regexp, $matches[4])) {
                 return $matches[0];
             }
         }
     }
     /**
      * Do replacement
      */
     $path = '/' . w3_get_site_path() . $matches[4];
     $cdn =& $this->get_cdn();
     $url = $cdn->format_url($path);
     if (!$url) {
         return $matches[0];
     }
     $this->replaced_urls[] = $matches[2];
     $replacement = sprintf('%s%s', $matches[1], $url);
     return $replacement;
 }
Example #3
0
 /**
  * Options save action
  */
 function options_save()
 {
     require_once W3TC_LIB_W3_DIR . '/Request.php';
     /**
      * Redirect params
      */
     $params = array();
     /**
      * Read config
      */
     $config =& new W3_Config();
     $config->read_request();
     /**
      * General tab
      */
     if ($this->_tab == 'general') {
         $debug = W3_Request::get_array('debug');
         $config->set('dbcache.debug', in_array('dbcache', $debug));
         $config->set('pgcache.debug', in_array('pgcache', $debug));
         $config->set('minify.debug', in_array('minify', $debug));
         $config->set('cdn.debug', in_array('cdn', $debug));
         /**
          * Page cache tab
          */
         if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_pgcache' && get_option('permalink_structure') == '') {
             $this->redirect(array('error' => 'fancy_permalinks_disabled'));
         }
         /**
          * Show notification when CDN enabled
          */
         if ($this->_config->get_boolean('cdn.enabled') == false && $config->get_boolean('cdn.enabled') == true && $config->get_string('cdn.engine') != 'mirror') {
             $config->set('notes.cdn_upload', true);
         }
     }
     /**
      * Minify tab
      */
     if ($this->_tab == 'minify') {
         $groups = $this->minify_get_groups();
         $js_group = W3_Request::get_string('js_group');
         $js_files = W3_Request::get_array('js_files');
         $css_group = W3_Request::get_string('css_group');
         $css_files = W3_Request::get_array('css_files');
         $js_groups = array();
         $css_groups = array();
         foreach ($js_files as $group => $locations) {
             if (!array_key_exists($group, $groups)) {
                 continue;
             }
             foreach ((array) $locations as $location => $files) {
                 switch ($location) {
                     case 'include':
                         $js_groups[$group][$location]['blocking'] = true;
                         break;
                     case 'include-nb':
                         $js_groups[$group][$location]['blocking'] = false;
                         break;
                     case 'include-footer':
                         $js_groups[$group][$location]['blocking'] = true;
                         break;
                     case 'include-footer-nb':
                         $js_groups[$group][$location]['blocking'] = false;
                         break;
                 }
                 foreach ((array) $files as $file) {
                     if (!empty($file)) {
                         $js_groups[$group][$location]['files'][] = w3_normalize_file($file);
                     }
                 }
             }
         }
         foreach ($css_files as $group => $locations) {
             if (!array_key_exists($group, $groups)) {
                 continue;
             }
             foreach ((array) $locations as $location => $files) {
                 foreach ((array) $files as $file) {
                     if (!empty($file)) {
                         $css_groups[$group][$location]['files'][] = w3_normalize_file($file);
                     }
                 }
             }
         }
         $config->set('minify.js.groups', $js_groups);
         $config->set('minify.css.groups', $css_groups);
         $params = array_merge($params, array('js_group' => $js_group, 'css_group' => $css_group));
     }
     /**
      * Handle settings change that require pgcache and minify empty
      */
     $pgcache_dependencies = array('pgcache.debug', 'dbcache.enabled', 'minify.enabled', 'cdn.enabled');
     if ($config->get_boolean('dbcache.enabled')) {
         $pgcache_dependencies = array_merge($pgcache_dependencies, array('dbcache.debug'));
     }
     if ($config->get_boolean('minify.enabled')) {
         $pgcache_dependencies = array_merge($pgcache_dependencies, array('minify.debug', 'minify.rewrite', 'minify.options', 'minify.html.enable', 'minify.html.reject.admin', 'minify.html.inline.css', 'minify.html.inline.js', 'minify.html.strip.crlf', 'minify.css.enable', 'minify.css.groups', 'minify.js.enable', 'minify.js.groups'));
     }
     if ($config->get_boolean('cdn.enabled')) {
         $pgcache_dependencies = array_merge($pgcache_dependencies, array('cdn.debug', 'cdn.engine', 'cdn.includes.enable', 'cdn.includes.files', 'cdn.theme.enable', 'cdn.theme.files', 'cdn.minify.enable', 'cdn.custom.enable', 'cdn.custom.files', 'cdn.ftp.domain', 'cdn.s3.bucket', 'cdn.cf.id', 'cdn.cf.cname'));
     }
     $minify_dependencies = array('minify.debug', 'minify.css.combine', 'minify.css.strip.comments', 'minify.css.strip.crlf', 'minify.css.groups', 'minify.js.combine.header', 'minify.js.combine.footer', 'minify.js.strip.comments', 'minify.js.strip.crlf', 'minify.js.groups');
     $old_pgcache_dependencies_values = array();
     $new_pgcache_dependencies_values = array();
     $old_minify_dependencies_values = array();
     $new_minify_dependencies_values = array();
     foreach ($pgcache_dependencies as $pgcache_dependency) {
         $old_pgcache_dependencies_values[] = $this->_config->get($pgcache_dependency);
         $new_pgcache_dependencies_values[] = $config->get($pgcache_dependency);
     }
     foreach ($minify_dependencies as $minify_dependency) {
         $old_minify_dependencies_values[] = $this->_config->get($minify_dependency);
         $new_minify_dependencies_values[] = $config->get($minify_dependency);
     }
     if ($this->_config->get_boolean('pgcache.enabled') && serialize($old_pgcache_dependencies_values) != serialize($new_pgcache_dependencies_values)) {
         $config->set('notes.need_empty_pgcache', true);
     }
     if ($this->_config->get_boolean('minify.enabled') && serialize($old_minify_dependencies_values) != serialize($new_minify_dependencies_values)) {
         $config->set('notes.need_empty_minify', true);
     }
     /**
      * Save config
      */
     if ($config->save()) {
         require_once W3TC_LIB_W3_DIR . '/Plugin/PgCache.php';
         require_once W3TC_LIB_W3_DIR . '/Plugin/DbCache.php';
         require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
         $w3_plugin_pgcache =& W3_Plugin_PgCache::instance();
         $w3_plugin_dbcache =& W3_Plugin_DbCache::instance();
         $w3_plugin_cdn =& W3_Plugin_Cdn::instance();
         if (W3TC_PHP5) {
             require_once W3TC_LIB_W3_DIR . '/Plugin/Minify.php';
             $w3_plugin_minify =& W3_Plugin_Minify::instance();
         }
         /**
          * Empty caches on engine change or cache enable/disable
          */
         if ($this->_config->get_string('pgcache.engine') != $config->get_string('pgcache.engine') || $this->_config->get_string('pgcache.enabled') != $config->get_string('pgcache.enabled')) {
             $this->flush_pgcache();
         }
         if ($this->_config->get_string('dbcache.engine') != $config->get_string('dbcache.engine') || $this->_config->get_string('dbcache.enabled') != $config->get_string('dbcache.enabled')) {
             $this->flush_dbcache();
         }
         if ($this->_config->get_string('minify.engine') != $config->get_string('minify.engine') || $this->_config->get_string('minify.enabled') != $config->get_string('minify.enabled')) {
             $this->flush_minify();
         }
         /**
          * Unschedule events if changed file gc interval
          */
         if ($this->_config->get_boolean('pgcache.file.gc') != $config->get_boolean('pgcache.file.gc')) {
             $w3_plugin_pgcache->unschedule();
         }
         if ($this->_config->get_boolean('dbcache.file.gc') != $config->get_boolean('dbcache.file.gc')) {
             $w3_plugin_dbcache->unschedule();
         }
         if (W3TC_PHP5 && $this->_config->get_boolean('minify.file.gc') != $config->get_boolean('minify.file.gc')) {
             $w3_plugin_minify->unschedule();
         }
         $this->_config->load();
         /**
          * Schedule events
          */
         $w3_plugin_pgcache->schedule();
         $w3_plugin_dbcache->schedule();
         $w3_plugin_cdn->schedule();
         if (W3TC_PHP5) {
             $w3_plugin_minify->schedule();
         }
         /**
          * Update support us option
          */
         $this->link_update();
         /**
          * Auto upload minify files to CDN
          */
         if ($this->_tab == 'minify' && $this->_config->get_boolean('minify.upload') && $this->_config->get_boolean('cdn.enabled') && $this->_config->get_string('cdn.engine') != 'mirror') {
             $this->cdn_upload_minify();
         }
         /**
          * Write page cache rewrite rules
          */
         if ($this->_tab == 'general' || $this->_tab == 'pgcache') {
             $is_wpmu = w3_is_wpmu();
             if ($this->_config->get_boolean('pgcache.enabled') && $this->_config->get_string('pgcache.engine') == 'file_pgcache') {
                 if (!$is_wpmu) {
                     $w3_plugin_pgcache->write_rules_core();
                 }
                 $w3_plugin_pgcache->write_rules_cache();
             } else {
                 if (!$is_wpmu) {
                     $w3_plugin_pgcache->remove_rules_core();
                 }
                 $w3_plugin_pgcache->remove_rules_cache();
             }
         }
         /**
          * Write minify rewrite rules
          */
         if (W3TC_PHP5 && ($this->_tab == 'general' || $this->_tab == 'minify')) {
             if ($this->_config->get_boolean('minify.enabled') && $this->_config->get_boolean('minify.rewrite')) {
                 $w3_plugin_minify->write_rules();
             } else {
                 require_once W3TC_DIR . '/lib/W3/Plugin/Minify.php';
                 $w3_plugin_minify->remove_rules();
             }
         }
         $this->redirect(array_merge($params, array('note' => 'config_save')));
     } else {
         $this->redirect(array_merge($params, array('error' => 'config_save')));
     }
 }
 /**
  * Normalizes URL
  * @param string $url
  * @return string
  */
 function _normalize_file($url)
 {
     $url = preg_replace('~\\?ver=[0-9\\.]+$~', '', $url);
     $url = w3_normalize_file($url);
     $url = w3_translate_file($url);
     return $url;
 }
Example #5
0
 /**
  * Link replace callback, basic checks step
  *
  * @param $match
  * @param $quote
  * @param $url
  * @param $path
  * @return null|string
  */
 function _link_replace_callback_checks($match, $quote, $url, $path)
 {
     /**
      * @var wpdb $wpdb
      */
     global $wpdb;
     static $queue = null, $reject_files = null;
     /**
      * Check if URL was already replaced
      */
     if (isset($this->replaced_urls[$url])) {
         return $quote . $this->replaced_urls[$url];
     }
     /**
      * Check URL for rejected files
      */
     if ($reject_files === null) {
         $reject_files = $this->_config->get_array('cdn.reject.files');
     }
     foreach ($reject_files as $reject_file) {
         if ($reject_file != '') {
             $reject_file = $this->_replace_folder_placeholders($reject_file);
             $reject_file = w3_normalize_file($reject_file);
             $reject_file_regexp = '~^(' . $this->get_regexp_by_mask($reject_file) . ')~i';
             if (preg_match($reject_file_regexp, $path)) {
                 return $match;
             }
         }
     }
     /**
      * Don't replace URL for files that are in the CDN queue
      */
     if ($queue === null) {
         if (!w3_is_cdn_mirror($this->_config->get_string('cdn.engine'))) {
             $sql = $wpdb->prepare('SELECT remote_path FROM ' . $wpdb->prefix . W3TC_CDN_TABLE_QUEUE . ' WHERE remote_path = %s', $path);
             $queue = $wpdb->get_var($sql);
         } else {
             $queue = false;
         }
     }
     if ($queue) {
         return $match;
     }
     return null;
 }