1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class used to cache gallery HTML output to save requests to the database |
4
|
|
|
* Date: 20/03/2017 |
5
|
|
|
*/ |
6
|
|
|
if ( ! class_exists( 'FooGallery_Cache' ) ) { |
7
|
|
|
|
8
|
|
|
class FooGallery_Cache { |
|
|
|
|
9
|
|
|
|
10
|
|
|
function __construct() { |
|
|
|
|
11
|
|
|
if ( is_admin() ) { |
12
|
|
|
//intercept the gallery save and save the html output to post meta |
13
|
|
|
add_action( 'foogallery_after_save_gallery', array( $this, 'cache_gallery_html_output_after_save' ), 10, 2 ); |
14
|
|
|
|
15
|
|
|
//add some settings to allow the clearing and disabling of the cache |
16
|
|
|
add_filter( 'foogallery_admin_settings_override', array( $this, 'add_cache_settings' ) ); |
17
|
|
|
|
18
|
|
|
//render the clear HTML cache button |
19
|
|
|
add_action( 'foogallery_admin_settings_custom_type_render_setting', array( $this, 'render_settings' ) ); |
20
|
|
|
|
21
|
|
|
// Ajax call for clearing HTML cache |
22
|
|
|
add_action( 'wp_ajax_foogallery_clear_html_cache', array( $this, 'ajax_clear_all_caches' ) ); |
23
|
|
|
|
24
|
|
|
add_action( 'foogallery_admin_new_version_detected', array( $this, 'clear_cache_on_update' ) ); |
25
|
|
|
|
26
|
|
|
//clear the gallery caches when settings are updated or reset |
27
|
|
|
add_action( 'foogallery_settings_updated', array( $this, 'clear_all_gallery_caches' ) ); |
28
|
|
|
add_action( 'foogallery_settings_reset', array( $this, 'clear_all_gallery_caches' ) ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
add_filter( 'foogallery_load_gallery_template', array( $this, 'fetch_gallery_html_from_cache' ), 10, 3 ); |
32
|
|
|
|
33
|
|
|
add_filter( 'foogallery_html_cache_disabled', array( $this, 'disable_html_cache' ), 10, 3 ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Override if the gallery html cache is disabled |
38
|
|
|
* |
39
|
|
|
* @param $disabled bool |
40
|
|
|
* @param $gallery FooGallery |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
function disable_html_cache( $disabled, $gallery ) { |
|
|
|
|
44
|
|
|
|
45
|
|
|
//check if the gallery sorting is random |
46
|
|
|
if ( 'rand' === $gallery->sorting ) { |
47
|
|
|
$disabled = true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $disabled; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Save the HTML output of the gallery after the gallery has been saved |
55
|
|
|
* |
56
|
|
|
* @param $post_id |
57
|
|
|
* @param $form_post |
58
|
|
|
*/ |
59
|
|
|
function cache_gallery_html_output_after_save( $post_id, $form_post ) { |
|
|
|
|
60
|
|
|
$this->cache_gallery_html_output( $post_id ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Save the HTML output of the gallery to post meta so that it can be used in future requests |
65
|
|
|
* |
66
|
|
|
* @param $foogallery_id |
67
|
|
|
*/ |
68
|
|
|
function cache_gallery_html_output( $foogallery_id ) { |
|
|
|
|
69
|
|
|
//check if caching is disabled and quit early |
70
|
|
|
if ( 'on' === foogallery_get_setting( 'disable_html_cache' ) ) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
//we need a way to force the gallery to render it's output every time it is saved |
75
|
|
|
global $foogallery_force_gallery_cache; |
|
|
|
|
76
|
|
|
$foogallery_force_gallery_cache = true; |
77
|
|
|
|
78
|
|
|
//capture the html output |
79
|
|
|
ob_start(); |
80
|
|
|
foogallery_render_gallery( $foogallery_id ); |
81
|
|
|
$gallery_html = ob_get_contents(); |
82
|
|
|
ob_end_clean(); |
83
|
|
|
|
84
|
|
|
//save the output to post meta for later use |
85
|
|
|
update_post_meta( $foogallery_id, FOOGALLERY_META_CACHE, $gallery_html ); |
86
|
|
|
|
87
|
|
|
$foogallery_force_gallery_cache = false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Override the template output |
92
|
|
|
* |
93
|
|
|
* @param $override |
94
|
|
|
* @param $gallery |
95
|
|
|
* @param $template_location |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
function fetch_gallery_html_from_cache( $override, $gallery, $template_location ) { |
|
|
|
|
100
|
|
|
global $foogallery_force_gallery_cache; |
|
|
|
|
101
|
|
|
if ( $foogallery_force_gallery_cache ) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
//check if caching is disabled and quit early |
106
|
|
|
if ( 'on' === foogallery_get_setting( 'disable_html_cache' ) ) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
//allow extensions of others disable the html cache |
111
|
|
|
if ( apply_filters( 'foogallery_html_cache_disabled', false, $gallery ) ) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$gallery_cache = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE, true ); |
116
|
|
|
|
117
|
|
|
if ( !empty( $gallery_cache ) && is_string( $gallery_cache ) ) { |
118
|
|
|
//output the cached gallery html |
119
|
|
|
echo $gallery_cache; |
120
|
|
|
return true; //return that we will override |
121
|
|
|
} else { |
122
|
|
|
//we should cache the result for next time |
123
|
|
|
$this->cache_gallery_html_output( $gallery->ID ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add some caching settings |
131
|
|
|
* @param $settings |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
function add_cache_settings( $settings ) { |
|
|
|
|
136
|
|
|
|
137
|
|
|
$cache_settings[] = array( |
|
|
|
|
138
|
|
|
'id' => 'disable_html_cache', |
139
|
|
|
'title' => __( 'Disable HTML Cache', 'foogallery' ), |
140
|
|
|
'desc' => __( 'The gallery HTML is cached by default. You can choose to disable the cache for debugging purposes. It is NOT recommended.', 'foogallery' ), |
141
|
|
|
'type' => 'checkbox', |
142
|
|
|
'tab' => 'general', |
143
|
|
|
'section' => __( 'Cache', 'foogallery' ) |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$cache_settings[] = array( |
147
|
|
|
'id' => 'clear_html_cache', |
148
|
|
|
'title' => __( 'Clear HTML Cache', 'foogallery' ), |
149
|
|
|
'desc' => sprintf( __( '%s caches the gallery HTML output to improve page performance. Use this button to clear the gallery HTML that has been cached across all galleries.', 'foogallery' ), foogallery_plugin_name() ), |
150
|
|
|
'type' => 'clear_gallery_cache_button', |
151
|
|
|
'tab' => 'general', |
152
|
|
|
'section' => __( 'Cache', 'foogallery' ) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$new_settings = array_merge( $cache_settings, $settings['settings'] ); |
156
|
|
|
|
157
|
|
|
$settings['settings'] = $new_settings; |
158
|
|
|
|
159
|
|
|
return $settings; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Render any custom setting types to the settings page |
164
|
|
|
*/ |
165
|
|
|
function render_settings( $args ) { |
|
|
|
|
166
|
|
|
if ('clear_gallery_cache_button' === $args['type'] ) { ?> |
167
|
|
|
<div id="foogallery_clear_html_cache_container"> |
168
|
|
|
<input type="button" data-nonce="<?php echo esc_attr( wp_create_nonce( 'foogallery_clear_html_cache' ) ); ?>" class="button-primary foogallery_clear_html_cache" value="<?php _e( 'Clear Gallery HTML Cache', 'foogallery' ); ?>"> |
169
|
|
|
<span id="foogallery_clear_html_cache_spinner" style="position: absolute" class="spinner"></span> |
170
|
|
|
</div> |
171
|
|
|
<?php } |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* AJAX endpoint for clearing all gallery caches |
176
|
|
|
*/ |
177
|
|
|
function ajax_clear_all_caches() { |
|
|
|
|
178
|
|
|
if ( check_admin_referer( 'foogallery_clear_html_cache' ) ) { |
179
|
|
|
|
180
|
|
|
$this->clear_all_gallery_caches(); |
181
|
|
|
|
182
|
|
|
_e('The cache for all galleries has been cleared!', 'foogallery' ); |
183
|
|
|
die(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Clears all caches for all galleries |
189
|
|
|
*/ |
190
|
|
|
function clear_all_gallery_caches() { |
|
|
|
|
191
|
|
|
delete_post_meta_by_key( FOOGALLERY_META_CACHE ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Clear all caches when the plugin has been updated. This is to account for changes in the HTML when a new version is released. |
196
|
|
|
*/ |
197
|
|
|
function clear_cache_on_update() { |
|
|
|
|
198
|
|
|
$this->clear_all_gallery_caches(); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.