-
Notifications
You must be signed in to change notification settings - Fork 3
/
widget-output-cache.php
183 lines (115 loc) · 3.98 KB
/
widget-output-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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
Plugin Name: Widget Output Cache
Description: Caches widget output in WordPress object cache.
Version: 0.5.2
Plugin URI: https://wordpress.org/plugins/widget-output-cache/
GitHub URI: https://github.com/kasparsd/widget-output-cache
Author: Kaspars Dambis
Author URI: https://kaspars.net
*/
WidgetOutputCache::instance();
class WidgetOutputCache {
// Store IDs of widgets to exclude from cache
private $excluded_ids = array();
protected function __construct() {
// Enable localization
add_action( 'plugins_loaded', array( $this, 'init_l10n' ) );
// Overwrite widget callback to cache the output
add_filter( 'widget_display_callback', array( $this, 'widget_callback' ), 10, 3 );
// Cache invalidation for widgets
add_filter( 'widget_update_callback', array( $this, 'cache_bump' ) );
// Allow widgets to be excluded from the cache
add_action( 'in_widget_form', array( $this, 'widget_controls' ), 10, 3 );
// Load widget cache exclude settings
add_action( 'init', array( $this, 'init' ), 10 );
// Save widget cache settings
add_action( 'sidebar_admin_setup', array( $this, 'save_widget_controls' ) );
}
public static function instance() {
static $instance;
if ( ! $instance )
$instance = new self();
return $instance;
}
function init_l10n() {
load_plugin_textdomain( 'widget-output-cache', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
function init() {
$this->excluded_ids = (array) get_option( 'cache-widgets-excluded', array() );
}
function widget_callback( $instance, $widget_object, $args ) {
// Don't return the widget
if ( false === $instance || ! is_subclass_of( $widget_object, 'WP_Widget' ) )
return $instance;
if ( in_array( $widget_object->id, $this->excluded_ids ) )
return $instance;
$timer_start = microtime(true);
$cache_key = sprintf(
'cwdgt-%s',
md5( $widget_object->id . get_option( 'cache-widgets-version', 1 ) )
);
$cached_widget = get_transient( $cache_key );
if ( empty( $cached_widget ) ) {
ob_start();
$widget_object->widget( $args, $instance );
$cached_widget = ob_get_contents();
ob_end_clean();
set_transient(
$cache_key,
$cached_widget,
apply_filters( 'widget_output_cache_ttl', 60 * 12, $args )
);
printf(
'%s <!-- Stored in widget cache in %s seconds (%s) -->',
$cached_widget,
round( microtime(true) - $timer_start, 4 ),
$cache_key
);
} else {
printf(
'%s <!-- From widget cache in %s seconds (%s) -->',
$cached_widget,
round( microtime(true) - $timer_start, 4 ),
$cache_key
);
}
// We already echoed the widget, so return false
return false;
}
function cache_bump( $instance ) {
update_option( 'cache-widgets-version', time() );
return $instance;
}
function widget_controls( $object, $return, $instance ) {
$is_excluded = in_array( $object->id, $this->excluded_ids );
printf(
'<p>
<label>
<input type="checkbox" name="widget-cache-exclude" value="%s" %s />
%s
</label>
</p>',
esc_attr( $object->id ),
checked( $is_excluded, true, false ),
esc_html__( 'Exclude this widget from cache', 'widget-output-cache' )
);
}
function save_widget_controls() {
// current_user_can( 'edit_theme_options' ) is already being checked in widgets.php
if ( empty( $_POST ) || ! isset( $_POST['widget-id'] ) )
return;
$widget_id = $_POST['widget-id'];
$is_excluded = isset( $_POST['widget-cache-exclude'] );
if ( ! isset($_POST['delete_widget']) && $is_excluded ) {
// Wiget is being saved and it is being excluded too
$this->excluded_ids[] = $widget_id;
} elseif ( in_array( $widget_id, $this->excluded_ids ) ) {
// Widget is being removed, remove it from exclusions too
$exclude_pos_key = array_search( $widget_id, $this->excluded_ids );
unset( $this->excluded_ids[ $exclude_pos_key ] );
}
$this->excluded_ids = array_unique( $this->excluded_ids );
update_option( 'cache-widgets-excluded', $this->excluded_ids );
}
}