示例#1
0
 /**
  * @param $buffer
  * @param $has_dynamic
  * @return array
  */
 private function _can_cache_buffer(&$buffer, $has_dynamic)
 {
     $mobile_group = $this->_get_mobile_group();
     $referrer_group = $this->_get_referrer_group();
     $encryption = $this->_get_encryption();
     $compression = $this->_get_compression();
     $compressions = $this->_get_compressions();
     /**
      * Don't use compression for debug mode or dynamic tags
      * because we need to modify buffer before send it to client
      */
     $raw = $this->_debug || $has_dynamic;
     if ($raw) {
         $compression = false;
         $compressions = array(false);
     }
     $content_type = '';
     $is_404 = function_exists('is_404') ? is_404() : false;
     $headers = $this->_get_cached_headers();
     if ($this->_enhanced_mode && !$this->_late_init) {
         $this->_check_rules_present();
         if (isset($headers['Content-Type'])) {
             $content_type = $headers['Content-Type'];
         }
     }
     $time = time();
     $cache = $this->_get_cache();
     /**
      * Store different versions of cache
      */
     $buffers = array();
     $group = '0';
     if (!isset($this->_sitemap_matched)) {
         $sitemap_regex = $this->_config->get_string('pgcache.purge.sitemap_regex');
         if ($sitemap_regex && preg_match('/' . $sitemap_regex . '/', basename($this->_request_uri))) {
             $group = 'sitemaps';
             $this->_sitemap_matched = true;
         }
     } elseif ($this->_sitemap_matched) {
         $group = 'sitemaps';
     }
     foreach ($compressions as $_compression) {
         $_page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $_compression, $content_type);
         /**
          * Compress content
          */
         $buffers[$_compression] = $buffer;
         $this->_compress($buffers[$_compression], $_compression);
         /**
          * Store cache data
          */
         $_data = array('404' => $is_404, 'headers' => $headers, 'time' => $time, 'content' => &$buffers[$_compression]);
         $cache->set($_page_key, $_data, $this->_lifetime, $group);
     }
     /**
      * Change buffer if using compression
      */
     if ($compression && isset($buffers[$compression])) {
         $buffer =& $buffers[$compression];
     }
     /**
      * Calculate content etag
      */
     $etag = md5($buffer);
     /**
      * Send headers
      */
     $this->_send_headers($is_404, $time, $etag, $compression, $headers);
     if ($raw) {
         if ($this->_debug && w3_can_print_comment($buffer)) {
             $buffer = $this->_debugging_caching($buffer, $mobile_group, $referrer_group, $encryption, $compression, $content_type);
         }
         /**
          * Don't use shutdown function below
          */
         if (!$has_dynamic) {
             $this->_compress($buffer, $compression);
             return array($compression, $buffer);
         }
         return array($compression, $buffer);
     }
     return array($compression, $buffer);
 }
示例#2
0
 /**
  * Output buffering callback
  *
  * @param string $buffer
  * @return string
  */
 function ob_callback($buffer)
 {
     global $wpdb;
     if ($buffer != '') {
         if (w3_is_database_error($buffer)) {
             status_header(503);
         } else {
             if (w3_can_print_comment($buffer)) {
                 /**
                  * Add footer comment
                  */
                 $date = date_i18n('Y-m-d H:i:s');
                 $host = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost';
                 if (w3_is_preview_mode()) {
                     $buffer .= "\r\n<!-- W3 Total Cache used in preview mode -->";
                 }
                 if ($this->_config->get_string('common.support') != '' || $this->_config->get_boolean('common.tweeted')) {
                     $buffer .= sprintf("\r\n<!-- Served from: %s @ %s by W3 Total Cache -->", w3_escape_comment($host), $date);
                 } else {
                     $strings = array();
                     if ($this->_config->get_boolean('minify.enabled') && !$this->_config->get_boolean('minify.debug')) {
                         $w3_plugin_minify = w3_instance('W3_Plugin_Minify');
                         $strings[] = sprintf(__('Minified using %s%s', 'w3-total-cache'), w3_get_engine_name($this->_config->get_string('minify.engine')), $w3_plugin_minify->minify_reject_reason != '' ? sprintf(' (%s)', $w3_plugin_minify->minify_reject_reason) : '');
                     }
                     if ($this->_config->get_boolean('pgcache.enabled') && !$this->_config->get_boolean('pgcache.debug')) {
                         $w3_pgcache = w3_instance('W3_PgCache');
                         $strings[] = sprintf(__('Page Caching using %s%s', 'w3-total-cache'), w3_get_engine_name($this->_config->get_string('pgcache.engine')), $w3_pgcache->cache_reject_reason != '' ? sprintf(' (%s)', $w3_pgcache->cache_reject_reason) : '');
                     }
                     if ($this->_config->get_boolean('dbcache.enabled') && !$this->_config->get_boolean('dbcache.debug')) {
                         /**
                          * @var W3_DbCache $db
                          */
                         $db = w3_instance('W3_DbCache');
                         $append = ($reason = $db->get_reject_reason()) ? sprintf(' (%s)', $reason) : '';
                         if ($db->query_hits) {
                             $strings[] = sprintf(__('Database Caching %d/%d queries in %.3f seconds using %s%s', 'w3-total-cache'), $db->query_hits, $db->query_total, $db->time_total, w3_get_engine_name($this->_config->get_string('dbcache.engine')), $append);
                         } else {
                             $strings[] = sprintf(__('Database Caching using %s%s', 'w3-total-cache'), w3_get_engine_name($this->_config->get_string('dbcache.engine')), $append);
                         }
                     }
                     if (w3_is_dbcluster()) {
                         $db_cluster = w3_instance('W3_Enterprise_DbCluster');
                         $strings[] = $db_cluster->status_message();
                     }
                     if ($this->_config->get_boolean('objectcache.enabled') && !$this->_config->get_boolean('objectcache.debug')) {
                         /**
                          * @var W3_ObjectCache $w3_objectcache
                          */
                         $w3_objectcache = w3_instance('W3_ObjectCache');
                         $append = ($reason = $w3_objectcache->get_reject_reason()) ? sprintf(' (%s)', $reason) : '';
                         $strings[] = sprintf(__('Object Caching %d/%d objects using %s%s', 'w3-total-cache'), $w3_objectcache->cache_hits, $w3_objectcache->cache_total, w3_get_engine_name($this->_config->get_string('objectcache.engine')), $append);
                     }
                     if (w3_is_pro($this->_config) || w3_is_enterprise($this->_config)) {
                         if ($this->_config->get_boolean('fragmentcache.enabled') && !$this->_config->get_boolean('fragmentcache.debug')) {
                             $w3_fragmentcache = w3_instance('W3_Pro_FragmentCache');
                             $append = $w3_fragmentcache->cache_reject_reason != '' ? sprintf(' (%s)', $w3_fragmentcache->cache_reject_reason) : '';
                             $strings[] = sprintf(__('Fragment Caching %d/%d fragments using %s%s', 'w3-total-cache'), $w3_fragmentcache->cache_hits, $w3_fragmentcache->cache_total, w3_get_engine_name($this->_config->get_string('fragmentcache.engine')), $append);
                         }
                     }
                     if ($this->_config->get_boolean('cdn.enabled') && !$this->_config->get_boolean('cdn.debug')) {
                         $w3_plugin_cdn = w3_instance('W3_Plugin_Cdn');
                         $w3_plugin_cdncommon = w3_instance('W3_Plugin_CdnCommon');
                         $cdn = $w3_plugin_cdncommon->get_cdn();
                         $via = $cdn->get_via();
                         $strings[] = sprintf(__('Content Delivery Network via %s%s', 'w3-total-cache'), $via ? $via : 'N/A', $w3_plugin_cdn->cdn_reject_reason != '' ? sprintf(' (%s)', $w3_plugin_cdn->cdn_reject_reason) : '');
                     }
                     if ($this->_config->get_boolean('newrelic.enabled')) {
                         $w3_newrelic = w3_instance('W3_Plugin_NewRelic');
                         $append = $w3_newrelic->newrelic_reject_reason != '' ? sprintf(' (%s)', $w3_newrelic->newrelic_reject_reason) : '';
                         $strings[] = sprintf(__("Application Monitoring using New Relic%s", 'w3-total-cache'), $append);
                     }
                     $buffer .= "\r\n<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/\r\n";
                     if (count($strings)) {
                         $buffer .= "\r\n" . implode("\r\n", $strings) . "\r\n";
                     }
                     $buffer .= sprintf("\r\n Served from: %s @ %s by W3 Total Cache -->", w3_escape_comment($host), $date);
                 }
                 if ($this->is_debugging()) {
                     if ($this->_config->get_boolean('dbcache.enabled') && $this->_config->get_boolean('dbcache.debug')) {
                         $db = w3_instance('W3_DbCache');
                         $buffer .= "\r\n\r\n" . $db->_get_debug_info();
                     }
                     if ($this->_config->get_boolean('objectcache.enabled') && $this->_config->get_boolean('objectcache.debug')) {
                         $w3_objectcache = w3_instance('W3_ObjectCache');
                         $buffer .= "\r\n\r\n" . $w3_objectcache->_get_debug_info();
                     }
                     if (w3_is_pro($this->_config) || w3_is_enterprise($this->_config)) {
                         if ($this->_config->get_boolean('fragmentcache.enabled') && $this->_config->get_boolean('fragmentcache.debug')) {
                             $w3_fragmentcache = w3_instance('W3_Pro_FragmentCache');
                             $buffer .= "\r\n\r\n" . $w3_fragmentcache->_get_debug_info();
                         }
                     }
                 }
             }
             $buffer = w3tc_do_ob_callbacks(array('minify', 'newrelic', 'cdn', 'browsercache', 'pagecache'), $buffer);
         }
     }
     return $buffer;
 }
示例#3
0
 /**
  * Output buffering callback
  *
  * @param string $buffer
  * @return string
  */
 function ob_callback(&$buffer)
 {
     if ($buffer != '' && $this->_is_cacheable_content_type()) {
         $compression = false;
         $has_dynamic = $this->_has_dynamic($buffer);
         $can_cache = $this->_can_cache2($buffer);
         if ($can_cache) {
             $mobile_group = $this->_get_mobile_group();
             $referrer_group = $this->_get_referrer_group();
             $encryption = $this->_get_encryption();
             $compression = $this->_get_compression();
             $compressions = $this->_get_compressions();
             /**
              * Don't use compression for debug mode or dynamic tags
              * because we need to modify buffer before send it to client
              */
             $raw = $this->_debug || $has_dynamic;
             if ($raw) {
                 $compression = false;
                 $compressions = array(false);
             }
             $content_type = '';
             $is_404 = function_exists('is_404') ? is_404() : false;
             $headers = $this->_get_cached_headers();
             if ($this->_enhanced_mode && !$this->_late_init) {
                 $this->_check_rules_present();
                 if (isset($headers['Content-Type'])) {
                     $content_type = $headers['Content-Type'];
                 }
             }
             $time = time();
             $cache = $this->_get_cache();
             /**
              * Store different versions of cache
              */
             $buffers = array();
             $group = '0';
             if (!isset($this->_sitemap_matched)) {
                 $sitemap_regex = $this->_config->get_string('pgcache.purge.sitemap_regex');
                 if ($sitemap_regex && preg_match('/' . $sitemap_regex . '/', basename($this->_request_uri))) {
                     $group = 'sitemaps';
                     $this->_sitemap_matched = true;
                 }
             } elseif ($this->_sitemap_matched) {
                 $group = 'sitemaps';
             }
             foreach ($compressions as $_compression) {
                 $_page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $_compression, $content_type);
                 /**
                  * Compress content
                  */
                 $buffers[$_compression] = $buffer;
                 $this->_compress($buffers[$_compression], $_compression);
                 /**
                  * Store cache data
                  */
                 $_data = array('404' => $is_404, 'headers' => $headers, 'time' => $time, 'content' => &$buffers[$_compression]);
                 $cache->set($_page_key, $_data, $this->_lifetime, $group);
             }
             /**
              * Change buffer if using compression
              */
             if ($compression && isset($buffers[$compression])) {
                 $buffer =& $buffers[$compression];
             }
             /**
              * Calculate content etag
              */
             $etag = md5($buffer);
             /**
              * Send headers
              */
             $this->_send_headers($is_404, $time, $etag, $compression, $headers);
             if ($raw) {
                 if ($this->_debug && w3_can_print_comment($buffer)) {
                     /**
                      * Set page key for debug
                      */
                     $this->_page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $compression, $content_type);
                     /**
                      * Append debug info
                      */
                     $time_total = w3_microtime() - $this->_time_start;
                     $debug_info = $this->_get_debug_info(true, '', false, $time_total);
                     $buffer .= "\r\n\r\n" . $debug_info;
                 }
                 /**
                  * Don't use shutdown function below
                  */
                 if (!$has_dynamic) {
                     $this->_compress($buffer, $compression);
                 }
             }
         } elseif ($this->_debug && w3_can_print_comment($buffer)) {
             $mobile_group = $this->_get_mobile_group();
             $referrer_group = $this->_get_referrer_group();
             $encryption = $this->_get_encryption();
             /**
              * Set page key for debug
              */
             $this->_page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $compression);
             /**
              * Append debug info
              */
             $time_total = w3_microtime() - $this->_time_start;
             $debug_info = $this->_get_debug_info(false, $this->cache_reject_reason, false, $time_total);
             $buffer .= "\r\n\r\n" . $debug_info;
         }
         /**
          * We can't capture output in ob_callback
          * so we use shutdown function
          */
         if ($has_dynamic) {
             $this->_shutdown_buffer = $buffer;
             $this->_shutdown_compression = $compression;
             $buffer = '';
             register_shutdown_function(array(&$this, 'shutdown'));
         }
     }
     return $buffer;
 }