batcache::ob PHP Method

ob() public method

public ob ( $output )
    function ob($output)
    {
        // PHP5 and objects disappearing before output buffers?
        wp_cache_init();
        // Remember, $wp_object_cache was clobbered in wp-settings.php so we have to repeat this.
        $this->configure_groups();
        if ($this->cancel !== false) {
            wp_cache_delete("{$this->url_key}_genlock", $this->group);
            return $output;
        }
        // Do not batcache blank pages unless they are HTTP redirects
        $output = trim($output);
        if ($output === '' && (!$this->redirect_status || !$this->redirect_location)) {
            wp_cache_delete("{$this->url_key}_genlock", $this->group);
            return;
        }
        // Do not cache 5xx responses
        if (isset($this->status_code) && intval($this->status_code / 100) == 5) {
            wp_cache_delete("{$this->url_key}_genlock", $this->group);
            return $output;
        }
        $this->do_variants($this->vary);
        $this->generate_keys();
        // Construct and save the batcache
        $this->cache = array('output' => $output, 'time' => isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(), 'timer' => $this->timer_stop(false, 3), 'headers' => array(), 'status_header' => $this->status_header, 'redirect_status' => $this->redirect_status, 'redirect_location' => $this->redirect_location, 'version' => $this->url_version);
        foreach (headers_list() as $header) {
            list($k, $v) = array_map('trim', explode(':', $header, 2));
            $this->cache['headers'][$k][] = $v;
        }
        if (!empty($this->cache['headers']) && !empty($this->uncached_headers)) {
            foreach ($this->uncached_headers as $header) {
                unset($this->cache['headers'][$header]);
            }
        }
        foreach ($this->cache['headers'] as $header => $values) {
            // Do not cache if cookies were set
            if (strtolower($header) === 'set-cookie') {
                wp_cache_delete("{$this->url_key}_genlock", $this->group);
                return $output;
            }
            foreach ((array) $values as $value) {
                if (preg_match('/^Cache-Control:.*max-?age=(\\d+)/i', "{$header}: {$value}", $matches)) {
                    $this->max_age = intval($matches[1]);
                }
            }
        }
        $this->cache['max_age'] = $this->max_age;
        wp_cache_set($this->key, $this->cache, $this->group, $this->max_age + $this->seconds + 30);
        // Unlock regeneration
        wp_cache_delete("{$this->url_key}_genlock", $this->group);
        if ($this->cache_control) {
            // Don't clobber Last-Modified header if already set, e.g. by WP::send_headers()
            if (!isset($this->cache['headers']['Last-Modified'])) {
                header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->cache['time']) . ' GMT', true);
            }
            if (!isset($this->cache['headers']['Cache-Control'])) {
                header("Cache-Control: max-age={$this->max_age}, must-revalidate", false);
            }
        }
        $this->do_headers($this->headers);
        // Add some debug info just before <head
        if ($this->debug) {
            $this->add_debug_just_cached();
        }
        // Pass output to next ob handler
        batcache_stats('batcache', 'total_page_views');
        return $this->cache['output'];
    }