PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.3.0
Gallery : FooGallery v3.3.0
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 1 week ago admin 1 week ago compatibility 1 week ago extensions 1 week ago foopluginbase 1 week ago public 1 week ago thumbs 1 week ago asset-manifest.php 1 week ago class-attachment-filters.php 1 week ago class-command-palette.php 1 week ago class-foogallery-animated-gif-support.php 1 week ago class-foogallery-attachment-custom-class.php 1 week ago class-foogallery-attachment-filename.php 1 week ago class-foogallery-attachment-type.php 1 week ago class-foogallery-attachment.php 1 week ago class-foogallery-cache.php 1 week ago class-foogallery-common-fields.php 1 week ago class-foogallery-crop-position.php 1 week ago class-foogallery-datasource-media_library.php 1 week ago class-foogallery-debug.php 1 week ago class-foogallery-delayed-runtime-loader.php 1 week ago class-foogallery-extensions-compatibility.php 1 week ago class-foogallery-force-https.php 1 week ago class-foogallery-lazyload.php 1 week ago class-foogallery-license-constant-handler.php 1 week ago class-foogallery-lightbox.php 1 week ago class-foogallery-no-javascript-fallback.php 1 week ago class-foogallery-paging.php 1 week ago class-foogallery-password-protect.php 1 week ago class-foogallery-sitemaps.php 1 week ago class-foogallery-widget.php 1 week ago class-foogallery.php 1 week ago class-gallery-advanced-settings.php 1 week ago class-il8n.php 1 week ago class-override-thumbnail.php 1 week ago class-posttypes.php 1 week ago class-previews.php 1 week ago class-retina.php 1 week ago class-thumbnail-dimensions.php 1 week ago class-thumbnails.php 1 week ago class-version-check.php 1 week ago constants.php 1 week ago functions.php 1 week ago gallery-management-functions.php 1 week ago includes.php 1 week ago index.php 1 week ago render-functions.php 1 week ago
class-foogallery-cache.php
262 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 }
97
98 $foogallery_force_gallery_cache = false;
99 }
100
101 function is_caching_enabled() {
102 global $current_foogallery_arguments;
103
104 //do some checks if we are using arguments
105 if ( isset( $current_foogallery_arguments ) ) {
106
107 //never cache if showing a preview
108 if ( array_key_exists( 'preview', $current_foogallery_arguments ) &&
109 true === $current_foogallery_arguments['preview'] ) {
110 return false;
111 }
112
113 //never cache if we are passing in extra arguments via the shortcode
114 $array_keys = array_keys( $current_foogallery_arguments );
115 if ( $array_keys != array( 'id', 'gallery' ) ) {
116 return false;
117 }
118 }
119
120 //next, check the settings
121 if ( 'on' === foogallery_get_setting( 'enable_html_cache' ) ) {
122 return true;
123 }
124
125 return false;
126 }
127
128 /**
129 * Override the template output
130 *
131 * @param $override
132 * @param $gallery
133 * @param $template_location
134 *
135 * @return bool
136 */
137 function fetch_gallery_html_from_cache( $override, $gallery, $template_location ) {
138 global $foogallery_force_gallery_cache;
139 if ( $foogallery_force_gallery_cache ) {
140 return false;
141 }
142
143 //check if caching is disabled and quit early
144 if ( !$this->is_caching_enabled() ) {
145 return false;
146 }
147
148 //allow extensions of others disable the html cache
149 if ( apply_filters( 'foogallery_html_cache_disabled', false, $gallery ) ) {
150 return false;
151 }
152
153 $gallery_cache = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE, true );
154
155 if ( !empty( $gallery_cache ) && is_string( $gallery_cache ) ) {
156 //output the cached gallery html
157 echo $gallery_cache; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Cached HTML output
158 return true; //return that we will override
159 } else {
160 //we should cache the result for next time
161 $this->cache_gallery_html_output( $gallery->ID );
162 }
163
164 return false;
165 }
166
167 /**
168 * Add some caching settings
169 * @param $settings
170 *
171 * @return array
172 */
173 function add_cache_settings( $settings ) {
174
175 $cache_settings[] = array(
176 'id' => 'enable_html_cache',
177 'title' => __( 'Enable HTML Cache', 'foogallery' ),
178 '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' ),
179 'type' => 'checkbox',
180 'tab' => 'general',
181 'section' => __( 'Performance', 'foogallery' )
182 );
183
184 $cache_settings[] = array(
185 'id' => 'clear_html_cache',
186 'title' => __( 'Clear HTML Cache', 'foogallery' ),
187 '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' ),
188 'type' => 'clear_gallery_cache_button',
189 'tab' => 'general',
190 'section' => __( 'Performance', 'foogallery' )
191 );
192
193 $new_settings = array_merge( $cache_settings, $settings['settings'] );
194
195 $settings['settings'] = $new_settings;
196
197 return $settings;
198 }
199
200 /**
201 * Render any custom setting types to the settings page
202 */
203 function render_settings( $args ) {
204 if ('clear_gallery_cache_button' === $args['type'] ) { ?>
205 <div id="foogallery_clear_html_cache_container">
206 <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' ); ?>">
207 <span id="foogallery_clear_html_cache_spinner" style="position: absolute" class="spinner"></span>
208 </div>
209 <?php }
210 }
211
212 /**
213 * AJAX endpoint for clearing all gallery caches
214 */
215 function ajax_clear_all_caches() {
216 if ( check_admin_referer( 'foogallery_clear_html_cache' ) ) {
217
218 if ( ! current_user_can( 'manage_options' ) ) {
219 wp_send_json_error( array(
220 'message' => __( 'You do not have permission!', 'foogallery' ),
221 ), 403 );
222 }
223
224 $this->clear_all_gallery_caches();
225
226 esc_html_e('The cache for all galleries has been cleared!', 'foogallery' );
227 wp_die( '', '', array( 'response' => null ) );
228 }
229 }
230
231 /**
232 * Clears all caches for all galleries
233 */
234 function clear_all_gallery_caches() {
235 delete_post_meta_by_key( FOOGALLERY_META_CACHE );
236 }
237
238 /**
239 * Clear all caches when the plugin has been updated. This is to account for changes in the HTML when a new version is released.
240 */
241 function clear_cache_on_update() {
242 $this->clear_all_gallery_caches();
243 }
244
245 /**
246 * Determine if the globals should be cleared when rendering a gallery
247 *
248 * @param $clear
249 *
250 * @return bool
251 */
252 function render_template_clear_globals( $clear ) {
253 global $foogallery_force_gallery_cache;
254 if ( $foogallery_force_gallery_cache ) {
255 return false;
256 }
257
258 return $clear;
259 }
260 }
261 }
262