Make WordPress Core

Ticket #18803: cache-load-template.php

File cache-load-template.php, 1.3 KB (added by sivel, 14 years ago)
Line 
1<?php
2if ( ! defined( 'WP_CACHE' ) || WP_CACHE !== true )
3        return;
4
5function do_cache_template_file( $template ) {
6        $template_files = array(
7                'sidebar.php'
8        );
9
10        if ( in_array( $template, $template_files ) )
11                return true;
12        else
13                return false;
14}
15
16add_filter( 'pre_load_template', 'maybe_deliver_cache', 10, 3 );
17function maybe_deliver_cache( $stop, $template, $require_once ) {
18        $template = basename( $template );
19
20        if ( ! do_cache_template_file( $template ) )
21                return $stop;
22
23        if ( $html = wp_cache_get( $template, 'theme' ) ) {
24                echo "<!-- Start Cached {$template} -->\n{$html}\n<!-- End Cached {$template} -->\n";
25                $stop = true;
26        }
27
28        return $stop;
29}
30
31add_action( 'load_template_pre_require', 'start_load_template_cache', 10, 2 );
32function start_load_template_cache( $template, $require_once ) {
33        $template = basename( $template );
34        if ( ! do_cache_template_file( $template ) )
35                return;
36        ob_start();
37}
38
39add_action( 'load_template_post_require', 'end_load_template_cache', 10, 2 );
40function end_load_template_cache( $template, $require_once ) {
41        $template = basename( $template );
42        if ( ! do_cache_template_file( $template ) )
43                return;
44
45        global $batcache;
46        $html = ob_get_contents();
47        echo $html;
48        wp_cache_set( $template, $html, 'theme', isset( $batcache->max_age ) ? $batcache->max_age : 3600 );
49}