This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | add_filter( 'foogallery_render_template_clear_globals' , array( $this, 'render_template_clear_globals' ) ); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Override if the gallery html cache is disabled |
||
40 | * |
||
41 | * @param $disabled bool |
||
42 | * @param $gallery FooGallery |
||
43 | * @return bool |
||
44 | */ |
||
45 | function disable_html_cache( $disabled, $gallery ) { |
||
46 | |||
47 | //check if the gallery sorting is random |
||
48 | if ( 'rand' === $gallery->sorting ) { |
||
49 | $disabled = true; |
||
50 | } |
||
51 | |||
52 | return $disabled; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Save the HTML output of the gallery after the gallery has been saved |
||
57 | * |
||
58 | * @param $post_id |
||
59 | * @param $form_post |
||
60 | */ |
||
61 | function cache_gallery_html_output_after_save( $post_id, $form_post ) { |
||
62 | $this->cache_gallery_html_output( $post_id ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Save the HTML output of the gallery to post meta so that it can be used in future requests |
||
67 | * |
||
68 | * @param $foogallery_id |
||
69 | */ |
||
70 | function cache_gallery_html_output( $foogallery_id ) { |
||
71 | $caching_enabled = $this->is_caching_enabled(); |
||
72 | |||
73 | //check if caching is disabled and quit early |
||
74 | if ( !$caching_enabled ) { |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | //we need a way to force the gallery to render it's output every time it is saved |
||
79 | global $foogallery_force_gallery_cache; |
||
80 | $foogallery_force_gallery_cache = true; |
||
81 | |||
82 | //capture the html output |
||
83 | ob_start(); |
||
84 | foogallery_render_gallery( $foogallery_id ); |
||
85 | $gallery_html = ob_get_contents(); |
||
86 | ob_end_clean(); |
||
87 | |||
88 | if ( $caching_enabled ) { |
||
89 | //save the output to post meta for later use |
||
90 | update_post_meta( $foogallery_id, FOOGALLERY_META_CACHE, $gallery_html ); |
||
91 | } |
||
92 | |||
93 | $foogallery_force_gallery_cache = false; |
||
94 | } |
||
95 | |||
96 | function is_caching_enabled() { |
||
97 | global $current_foogallery_arguments; |
||
98 | |||
99 | //do some checks if we are using arguments |
||
100 | if ( isset( $current_foogallery_arguments ) ) { |
||
101 | |||
102 | //never cache if showing a preview |
||
103 | if ( array_key_exists( 'preview', $current_foogallery_arguments ) && |
||
104 | true === $current_foogallery_arguments['preview'] ) { |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | //never cache if we are passing in extra arguments via the shortcode |
||
109 | $array_keys = array_keys( $current_foogallery_arguments ); |
||
110 | if ( $array_keys != array( 'id', 'gallery' ) ) { |
||
111 | return false; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | //next, check the settings |
||
116 | if ( 'on' === foogallery_get_setting( 'enable_html_cache' ) ) { |
||
117 | return true; |
||
118 | } |
||
119 | |||
120 | return false; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Override the template output |
||
125 | * |
||
126 | * @param $override |
||
127 | * @param $gallery |
||
128 | * @param $template_location |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | function fetch_gallery_html_from_cache( $override, $gallery, $template_location ) { |
||
133 | global $foogallery_force_gallery_cache; |
||
134 | if ( $foogallery_force_gallery_cache ) { |
||
135 | return false; |
||
136 | } |
||
137 | |||
138 | //check if caching is disabled and quit early |
||
139 | if ( !$this->is_caching_enabled() ) { |
||
140 | return false; |
||
141 | } |
||
142 | |||
143 | //allow extensions of others disable the html cache |
||
144 | if ( apply_filters( 'foogallery_html_cache_disabled', false, $gallery ) ) { |
||
145 | return false; |
||
146 | } |
||
147 | |||
148 | $gallery_cache = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE, true ); |
||
149 | |||
150 | if ( !empty( $gallery_cache ) && is_string( $gallery_cache ) ) { |
||
151 | //output the cached gallery html |
||
152 | echo $gallery_cache; |
||
153 | return true; //return that we will override |
||
154 | } else { |
||
155 | //we should cache the result for next time |
||
156 | $this->cache_gallery_html_output( $gallery->ID ); |
||
157 | } |
||
158 | |||
159 | return false; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Add some caching settings |
||
164 | * @param $settings |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | function add_cache_settings( $settings ) { |
||
169 | |||
170 | $cache_settings[] = array( |
||
0 ignored issues
–
show
|
|||
171 | 'id' => 'enable_html_cache', |
||
172 | 'title' => __( 'Enable HTML Cache', 'foogallery' ), |
||
173 | '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' ), |
||
174 | 'type' => 'checkbox', |
||
175 | 'tab' => 'general', |
||
176 | 'section' => __( 'Cache', 'foogallery' ) |
||
177 | ); |
||
178 | |||
179 | $cache_settings[] = array( |
||
180 | 'id' => 'clear_html_cache', |
||
181 | 'title' => __( 'Clear HTML Cache', 'foogallery' ), |
||
182 | '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' ), |
||
183 | 'type' => 'clear_gallery_cache_button', |
||
184 | 'tab' => 'general', |
||
185 | 'section' => __( 'Cache', 'foogallery' ) |
||
186 | ); |
||
187 | |||
188 | $new_settings = array_merge( $cache_settings, $settings['settings'] ); |
||
189 | |||
190 | $settings['settings'] = $new_settings; |
||
191 | |||
192 | return $settings; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Render any custom setting types to the settings page |
||
197 | */ |
||
198 | function render_settings( $args ) { |
||
199 | if ('clear_gallery_cache_button' === $args['type'] ) { ?> |
||
200 | <div id="foogallery_clear_html_cache_container"> |
||
201 | <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' ); ?>"> |
||
202 | <span id="foogallery_clear_html_cache_spinner" style="position: absolute" class="spinner"></span> |
||
203 | </div> |
||
204 | <?php } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * AJAX endpoint for clearing all gallery caches |
||
209 | */ |
||
210 | function ajax_clear_all_caches() { |
||
211 | if ( check_admin_referer( 'foogallery_clear_html_cache' ) ) { |
||
212 | |||
213 | $this->clear_all_gallery_caches(); |
||
214 | |||
215 | _e('The cache for all galleries has been cleared!', 'foogallery' ); |
||
216 | die(); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Clears all caches for all galleries |
||
222 | */ |
||
223 | function clear_all_gallery_caches() { |
||
224 | delete_post_meta_by_key( FOOGALLERY_META_CACHE ); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Clear all caches when the plugin has been updated. This is to account for changes in the HTML when a new version is released. |
||
229 | */ |
||
230 | function clear_cache_on_update() { |
||
231 | $this->clear_all_gallery_caches(); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Determine if the globals should be cleared when rendering a gallery |
||
236 | * |
||
237 | * @param $clear |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | function render_template_clear_globals( $clear ) { |
||
242 | global $foogallery_force_gallery_cache; |
||
243 | if ( $foogallery_force_gallery_cache ) { |
||
244 | return false; |
||
245 | } |
||
246 | |||
247 | return $clear; |
||
248 | } |
||
249 | } |
||
250 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.