-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragment_cache.php
161 lines (126 loc) · 4.19 KB
/
fragment_cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* @package Fragment_Cache
* @version 0.6.0
*/
/*
Plugin Name: Fragment Cache
Plugin URI: http://wordpress.org/extend/plugins/
Description: Boost your page performance by caching individual page fragments. Works with logged-in users too!
Author: Dave Kaplan
Version: 0.6.0
Author URI: http://exygy.com/
*/
/*
---- File-based Fragment Caching -----
adapted from https://gist.github.com/markjaquith/2653957#file-gistfile1-aw
Usage:
$cache = new FragmentCache( $options );
if ( ! $cache->output() ) { // testing for a value of false
functions_that_do_stuff_live();
these_should_echo();
// IMPORTANT
$cache->store();
// YOU CANNOT FORGET THIS. If you do, the site will break.
}
*/
class FragmentCache {
const GROUP = 'fragment-cache';
public static $filesystem = true;
var $key;
public function __construct( $options=array() ) {
if (self::$filesystem) {
if ( ! file_exists(ABSPATH.'wp-content/cache/fragment-cache')) {
wp_mkdir_p(ABSPATH.'wp-content/cache/fragment-cache');
}
}
$date = isset($options['date']) ? $options['date'] : null;
$key = isset($options['key']) ? $options['key'] : self::page_cache_key($date);
$logged_in = isset($options['logged_in']) ? intval($options['logged_in']) : 0; //convert true,false to 1,0
$fullkey = "{$key}__{$logged_in}";
if (self::$filesystem) {
$this->key = "wp-content/cache/fragment-cache/$fullkey.txt";
} else {
$this->key = $fullkey;
}
}
public static function page_cache_key($date=null) {
$url = parse_url(get_permalink());
$path = $url['path'];
$path = rtrim(substr($path, 1), '/');
// replace '/' with '-'
$path = preg_replace('[\/]', '__', $path);
if ($path == '') $path = 'home';
if ($date) {
$date = date('Ymd_His', strtotime($date));
} else {
$date = the_modified_date('Ymd_His', '', '', false);
}
$path .= '__'.$date;
return $path;
}
public static function flush() {
if (self::$filesystem) {
$files = glob(ABSPATH.'wp-content/cache/fragment-cache/*'); // get all file names
foreach ($files as $file) { // iterate files
if (is_file($file)) unlink($file); // delete file
}
} else {
wp_cache_flush();
}
}
public function output($echo=true) {
$cached = false;
if (self::$filesystem && (file_exists($this->key))) {
$cached = file_get_contents($this->key);
} else {
$cached = wp_cache_get($this->key, self::GROUP);
}
if ($cached) {
// cache was found...
if ($echo) echo $cached . "\n <!-- Serving FragmentCache from: {$this->key} --> \n";
return true;
} else {
ob_start();
return false;
}
}
public function store() {
$output = ob_get_flush(); // Flushes the buffers
if (self::$filesystem) {
$fh = fopen($this->key, 'w'); //or die("can't open file");
fwrite($fh, $output);
fclose($fh);
} else {
wp_cache_add($this->key, $output, self::GROUP, 3600*48);
}
return true;
}
}
// object cache is UNTESTED! disabled for now
// global $_wp_using_ext_object_cache;
// if ($_wp_using_ext_object_cache) FragmentCache::filesystem = false;
function plugin_menu() {
add_submenu_page('plugins.php', __('Clear FragmentCache'), __('Clear FragmentCache'), 'manage_options', 'frag-cache-opts', 'fragment_cache_options');
}
add_action('admin_menu', 'plugin_menu');
function fragment_cache_options() {
?>
<?php if ( !empty($_POST['submit'] ) ) : ?>
<?php FragmentCache::flush() ?>
<div id="message" class="updated fade"><p><strong><?php _e('Cache has been cleared!') ?></strong></p></div>
<?php endif; ?>
<form action="" method="post" id="frag-cache">
<h1>Fragment Cache options:</h1>
<input type="submit" name="submit" value="Clear the cache" />
</form>
<?
}
function fragment_cache_plugin_action_links( $links, $file ) {
if ( $file == plugin_basename( dirname(__FILE__).'/fragment_cache.php' ) ) {
$links[] = '<a href="' . admin_url( 'admin.php?page=frag-cache-opts' ) . '">'.__( 'Settings' ).'</a>';
}
return $links;
}
add_filter( 'plugin_action_links', 'fragment_cache_plugin_action_links', 10, 2 );
?>