1 | <?php |
---|
2 | if ( ! defined( 'WP_CACHE' ) || WP_CACHE !== true ) |
---|
3 | return; |
---|
4 | |
---|
5 | function 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 | |
---|
16 | add_filter( 'pre_load_template', 'maybe_deliver_cache', 10, 3 ); |
---|
17 | function 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 | |
---|
31 | add_action( 'load_template_pre_require', 'start_load_template_cache', 10, 2 ); |
---|
32 | function 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 | |
---|
39 | add_action( 'load_template_post_require', 'end_load_template_cache', 10, 2 ); |
---|
40 | function 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 | } |
---|