PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.3.2
Gallery : FooGallery v3.3.2
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / class-foogallery-cache.php
foogallery / includes Last commit date
abilities 2 days ago admin 2 days ago compatibility 2 days ago extensions 2 days ago foopluginbase 2 days ago public 2 days ago thumbs 2 days ago asset-manifest.php 2 days ago class-attachment-filters.php 2 days ago class-command-palette.php 2 days ago class-foogallery-animated-gif-support.php 2 days ago class-foogallery-attachment-custom-class.php 2 days ago class-foogallery-attachment-filename.php 2 days ago class-foogallery-attachment-type.php 2 days ago class-foogallery-attachment.php 2 days ago class-foogallery-cache.php 2 days ago class-foogallery-common-fields.php 2 days ago class-foogallery-crop-position.php 2 days ago class-foogallery-datasource-media_library.php 2 days ago class-foogallery-debug.php 2 days ago class-foogallery-delayed-runtime-loader.php 2 days ago class-foogallery-extensions-compatibility.php 2 days ago class-foogallery-force-https.php 2 days ago class-foogallery-lazyload.php 2 days ago class-foogallery-license-constant-handler.php 2 days ago class-foogallery-lightbox.php 2 days ago class-foogallery-no-javascript-fallback.php 2 days ago class-foogallery-paging.php 2 days ago class-foogallery-password-protect.php 2 days ago class-foogallery-sitemaps.php 2 days ago class-foogallery-widget.php 2 days ago class-foogallery.php 2 days ago class-gallery-advanced-settings.php 2 days ago class-il8n.php 2 days ago class-override-thumbnail.php 2 days ago class-posttypes.php 2 days ago class-previews.php 2 days ago class-retina.php 2 days ago class-thumbnail-dimensions.php 2 days ago class-thumbnails.php 2 days ago class-version-check.php 2 days ago constants.php 2 days ago functions.php 2 days ago gallery-management-functions.php 2 days ago includes.php 2 days ago index.php 2 days ago render-functions.php 2 days ago
class-foogallery-cache.php
275 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Class used to cache gallery HTML output to save requests to the database
9 * Date: 20/03/2017
10 */
11 if ( ! class_exists( 'FooGallery_Cache' ) ) {
12
13 class FooGallery_Cache {
14
15 function __construct() {
16 if ( is_admin() ) {
17 //intercept the gallery save and save the html output to post meta
18 add_action( 'foogallery_after_save_gallery', array( $this, 'cache_gallery_html_output_after_save' ), 10, 2 );
19
20 //add some settings to allow the clearing and disabling of the cache
21 add_filter( 'foogallery_admin_settings_override', array( $this, 'add_cache_settings' ) );
22
23 //render the clear HTML cache button
24 add_action( 'foogallery_admin_settings_custom_type_render_setting', array( $this, 'render_settings' ) );
25
26 // Ajax call for clearing HTML cache
27 add_action( 'wp_ajax_foogallery_clear_html_cache', array( $this, 'ajax_clear_all_caches' ) );
28
29 add_action( 'foogallery_admin_new_version_detected', array( $this, 'clear_cache_on_update' ) );
30
31 //clear the gallery caches when settings are updated or reset
32 add_action( 'foogallery_settings_updated', array( $this, 'clear_all_gallery_caches' ) );
33 add_action( 'foogallery_settings_reset', array( $this, 'clear_all_gallery_caches' ) );
34 }
35
36 add_filter( 'foogallery_load_gallery_template', array( $this, 'fetch_gallery_html_from_cache' ), 10, 3 );
37
38 add_filter( 'foogallery_html_cache_disabled', array( $this, 'disable_html_cache' ), 10, 3 );
39
40 add_filter( 'foogallery_render_template_clear_globals' , array( $this, 'render_template_clear_globals' ) );
41 }
42
43 /**
44 * Override if the gallery html cache is disabled
45 *
46 * @param $disabled bool
47 * @param $gallery FooGallery
48 * @return bool
49 */
50 function disable_html_cache( $disabled, $gallery ) {
51
52 //check if the gallery sorting is random
53 if ( 'rand' === $gallery->sorting ) {
54 $disabled = true;
55 }
56
57 return $disabled;
58 }
59
60 /**
61 * Save the HTML output of the gallery after the gallery has been saved
62 *
63 * @param $post_id
64 * @param $form_post
65 */
66 function cache_gallery_html_output_after_save( $post_id, $form_post ) {
67 $this->cache_gallery_html_output( $post_id );
68 }
69
70 /**
71 * Save the HTML output of the gallery to post meta so that it can be used in future requests
72 *
73 * @param $foogallery_id
74 */
75 function cache_gallery_html_output( $foogallery_id ) {
76 $caching_enabled = $this->is_caching_enabled();
77
78 //check if caching is disabled and quit early
79 if ( !$caching_enabled ) {
80 return;
81 }
82
83 //we need a way to force the gallery to render it's output every time it is saved
84 global $foogallery_force_gallery_cache;
85 $foogallery_force_gallery_cache = true;
86
87 //capture the html output
88 ob_start();
89 foogallery_render_gallery( $foogallery_id );
90 $gallery_html = ob_get_contents();
91 ob_end_clean();
92
93 if ( $caching_enabled ) {
94 //save the output to post meta for later use
95 update_post_meta( $foogallery_id, FOOGALLERY_META_CACHE, $gallery_html );
96 update_post_meta( $foogallery_id, FOOGALLERY_META_CACHE . '_mobile_access', foogallery_mobile_settings_is_entitled() ? '1' : '0' );
97 }
98
99 $foogallery_force_gallery_cache = false;
100 }
101
102 function is_caching_enabled() {
103 global $current_foogallery_arguments;
104
105 //do some checks if we are using arguments
106 if ( isset( $current_foogallery_arguments ) ) {
107
108 //never cache if showing a preview
109 if ( array_key_exists( 'preview', $current_foogallery_arguments ) &&
110 true === $current_foogallery_arguments['preview'] ) {
111 return false;
112 }
113
114 //never cache if we are passing in extra arguments via the shortcode
115 $array_keys = array_keys( $current_foogallery_arguments );
116 if ( $array_keys != array( 'id', 'gallery' ) ) {
117 return false;
118 }
119 }
120
121 //next, check the settings
122 if ( 'on' === foogallery_get_setting( 'enable_html_cache' ) ) {
123 return true;
124 }
125
126 return false;
127 }
128
129 /**
130 * Override the template output
131 *
132 * @param $override
133 * @param $gallery
134 * @param $template_location
135 *
136 * @return bool
137 */
138 function fetch_gallery_html_from_cache( $override, $gallery, $template_location ) {
139 global $foogallery_force_gallery_cache;
140 if ( $foogallery_force_gallery_cache ) {
141 return false;
142 }
143
144 //check if caching is disabled and quit early
145 if ( !$this->is_caching_enabled() ) {
146 return false;
147 }
148
149 //allow extensions of others disable the html cache
150 if ( apply_filters( 'foogallery_html_cache_disabled', false, $gallery ) ) {
151 return false;
152 }
153
154 $gallery_cache = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE, true );
155 $mobile_access = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE . '_mobile_access', true );
156 $current_mobile_access = foogallery_mobile_settings_is_entitled() ? '1' : '0';
157 if ( $current_mobile_access !== (string) $mobile_access ) {
158 // Older caches lack an entitlement marker, while upgraded and
159 // downgraded installs carry the opposite marker. Rebuild once even
160 // if a declaration was removed during that same transition.
161 delete_post_meta( $gallery->ID, FOOGALLERY_META_CACHE );
162 delete_post_meta( $gallery->ID, FOOGALLERY_META_CACHE . '_mobile_access' );
163 $this->cache_gallery_html_output( $gallery->ID );
164 $gallery_cache = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE, true );
165 }
166
167 if ( !empty( $gallery_cache ) && is_string( $gallery_cache ) ) {
168 //output the cached gallery html
169 echo $gallery_cache; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Cached HTML output
170 return true; //return that we will override
171 } else {
172 //we should cache the result for next time
173 $this->cache_gallery_html_output( $gallery->ID );
174 }
175
176 return false;
177 }
178
179 /**
180 * Add some caching settings
181 * @param $settings
182 *
183 * @return array
184 */
185 function add_cache_settings( $settings ) {
186
187 $cache_settings[] = array(
188 'id' => 'enable_html_cache',
189 'title' => __( 'Enable HTML Cache', 'foogallery' ),
190 'desc' => __( 'The gallery HTML that is generated can be cached. This can reduce the number of calls to the database when displaying a gallery and can increase site performance.', 'foogallery' ),
191 'type' => 'checkbox',
192 'tab' => 'general',
193 'section' => __( 'Performance', 'foogallery' )
194 );
195
196 $cache_settings[] = array(
197 'id' => 'clear_html_cache',
198 'title' => __( 'Clear HTML Cache', 'foogallery' ),
199 'desc' => __( 'If you enable the HTML cache, then you can use this button to clear the gallery HTML that has been cached for all galleries.', 'foogallery' ),
200 'type' => 'clear_gallery_cache_button',
201 'tab' => 'general',
202 'section' => __( 'Performance', 'foogallery' )
203 );
204
205 $new_settings = array_merge( $cache_settings, $settings['settings'] );
206
207 $settings['settings'] = $new_settings;
208
209 return $settings;
210 }
211
212 /**
213 * Render any custom setting types to the settings page
214 */
215 function render_settings( $args ) {
216 if ('clear_gallery_cache_button' === $args['type'] ) { ?>
217 <div id="foogallery_clear_html_cache_container">
218 <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 esc_attr_e( 'Clear Gallery HTML Cache', 'foogallery' ); ?>">
219 <span id="foogallery_clear_html_cache_spinner" style="position: absolute" class="spinner"></span>
220 </div>
221 <?php }
222 }
223
224 /**
225 * AJAX endpoint for clearing all gallery caches
226 */
227 function ajax_clear_all_caches() {
228 if ( check_admin_referer( 'foogallery_clear_html_cache' ) ) {
229
230 if ( ! current_user_can( 'manage_options' ) ) {
231 wp_send_json_error( array(
232 'message' => __( 'You do not have permission!', 'foogallery' ),
233 ), 403 );
234 }
235
236 $this->clear_all_gallery_caches();
237
238 esc_html_e('The cache for all galleries has been cleared!', 'foogallery' );
239 wp_die( '', '', array( 'response' => null ) );
240 }
241 }
242
243 /**
244 * Clears all caches for all galleries
245 */
246 function clear_all_gallery_caches() {
247 delete_post_meta_by_key( FOOGALLERY_META_CACHE );
248 delete_post_meta_by_key( FOOGALLERY_META_CACHE . '_mobile_access' );
249 }
250
251 /**
252 * Clear all caches when the plugin has been updated. This is to account for changes in the HTML when a new version is released.
253 */
254 function clear_cache_on_update() {
255 $this->clear_all_gallery_caches();
256 }
257
258 /**
259 * Determine if the globals should be cleared when rendering a gallery
260 *
261 * @param $clear
262 *
263 * @return bool
264 */
265 function render_template_clear_globals( $clear ) {
266 global $foogallery_force_gallery_cache;
267 if ( $foogallery_force_gallery_cache ) {
268 return false;
269 }
270
271 return $clear;
272 }
273 }
274 }
275