PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.1.1
ShopBuilder – WooCommerce Builder For Elementor v3.1.1
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Helpers / Cache.php

Cache.php in ShopBuilder – WooCommerce Builder For Elementor 3.1.1, at app/Helpers/Cache.php

367 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shortcodes Class.
4 *
5 * This class contains all the Shortcodes.
6 *
7 * @package RadiusTheme\SB
8 */
9
10 namespace RadiusTheme\SB\Helpers;
11
12 use W3TC\Dispatcher;
13 use RadiusTheme\SB\Traits\SingletonTrait;
14 use RadiusTheme\SB\Controllers\AssetRegistry;
15
16 // Do not allow directly accessing this file.
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit( 'This script cannot be accessed directly.' );
19 }
20
21 /**
22 * CacheController Class.
23 */
24 class Cache {
25
26 /**
27 * Clear the template cache.
28 *
29 * @since 4.3.0
30 */
31 public static function clear_all_cache() {
32 self::clear_data_cache();
33 self::clear_template_cache();
34 self::clear_plugins_cache();
35 self::clear_transient_cache();
36 self::clear_asset_cache();
37 }
38 /**
39 * Clear the template cache.
40 *
41 * @since 4.3.0
42 */
43 public static function clear_plugins_cache() {
44 // Clear W3 Total Cache.
45 if ( function_exists( 'w3tc_flush_all' ) ) {
46 w3tc_flush_all();
47 }
48 // Clear WP Super Cache.
49 if ( function_exists( 'wp_cache_clear_cache' ) ) {
50 wp_cache_clear_cache();
51 }
52 // Clear WP Rocket cache.
53 if ( function_exists( 'rocket_clean_domain' ) ) {
54 rocket_clean_domain();
55 }
56 if ( method_exists( 'LiteSpeed_Cache_API', 'purge_all' ) ) {
57 \LiteSpeed_Cache_API::purge_all();
58 }
59 if ( class_exists( 'Endurance_Page_Cache' ) ) {
60 $epc = new \Endurance_Page_Cache();
61 $epc->purge_all();
62 }
63 if ( class_exists( 'SG_CachePress_Supercacher' ) && method_exists( 'SG_CachePress_Supercacher', 'purge_cache' ) ) {
64 \SG_CachePress_Supercacher::purge_cache( true );
65 }
66 if ( class_exists( 'SiteGround_Optimizer\Supercacher\Supercacher' ) ) {
67 \SiteGround_Optimizer\Supercacher\Supercacher::purge_cache();
68 }
69 if ( isset( $GLOBALS['wp_fastest_cache'] ) && method_exists( $GLOBALS['wp_fastest_cache'], 'deleteCache' ) ) {
70 $GLOBALS['wp_fastest_cache']->deleteCache( true );
71 }
72 if ( is_callable( [ 'Swift_Performance_Cache', 'clear_all_cache' ] ) ) {
73 \Swift_Performance_Cache::clear_all_cache();
74 }
75 if ( is_callable( [ 'Hummingbird\WP_Hummingbird', 'flush_cache' ] ) ) {
76 \Hummingbird\WP_Hummingbird::flush_cache( true, false );
77 }
78 if ( class_exists( 'WP_Optimize' ) ) {
79 \WP_Optimize()->get_page_cache()->purge();
80 }
81
82 // Purge WP Engine.
83 if ( class_exists( 'WpeCommon' ) ) {
84 if ( method_exists( 'WpeCommon', 'purge_memcached' ) ) {
85 \WpeCommon::purge_memcached();
86 }
87 if ( method_exists( 'WpeCommon', 'clear_maxcdn_cache' ) ) {
88 \WpeCommon::clear_maxcdn_cache();
89 }
90 if ( method_exists( 'WpeCommon', 'purge_varnish_cache' ) ) {
91 \WpeCommon::purge_varnish_cache();
92 }
93 }
94 // Purge Kinsta.
95 global $kinsta_cache;
96 if ( isset( $kinsta_cache ) && class_exists( '\\Kinsta\\CDN_Enabler' ) ) {
97 if ( ! empty( $kinsta_cache->kinsta_cache_purge ) && is_callable( [ $kinsta_cache->kinsta_cache_purge, 'purge_complete_caches' ] ) ) {
98 $kinsta_cache->kinsta_cache_purge->purge_complete_caches();
99 }
100 }
101 // Purge Pagely.
102 if ( class_exists( 'PagelyCachePurge' ) ) {
103 $purge_pagely = new \PagelyCachePurge();
104 if ( is_callable( [ $purge_pagely, 'purgeAll' ] ) ) {
105 $purge_pagely->purgeAll();
106 }
107 }
108 // Purge Pressidum.
109 if ( defined( 'WP_NINUKIS_WP_NAME' ) && class_exists( 'Ninukis_Plugin' ) && is_callable( [ 'Ninukis_Plugin', 'get_instance' ] ) ) {
110 $purge_pressidum = \Ninukis_Plugin::get_instance();
111 if ( is_callable( [ $purge_pressidum, 'purgeAllCaches' ] ) ) {
112 $purge_pressidum->purgeAllCaches();
113 }
114 }
115 // Purge Savvii.
116 if ( defined( '\Savvii\CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW' ) ) {
117 $purge_savvii = new \Savvii\CacheFlusherPlugin();
118 if ( is_callable( [ $purge_savvii, 'domainflush' ] ) ) {
119 $purge_savvii->domainflush();
120 }
121 }
122 // Purge Hyper Cache.
123 if ( class_exists( 'HyperCache' ) ) {
124 do_action( 'autoptimize_action_cachepurged' );
125 }
126 // purge cache enabler.
127 if ( has_action( 'ce_clear_cache' ) ) {
128 do_action( 'ce_clear_cache' );
129 }
130 // When plugins have a simple method, add them to the array ('Plugin Name' => 'method_name').
131 $others = [
132 'WP Fastest Cache' => 'wpfc_clear_all_cache',
133 'Cachify' => 'cachify_flush_cache',
134 'Comet Cache' => [ 'comet_cache', 'clear' ],
135 'SG Optimizer' => 'sg_cachepress_purge_cache',
136 'Pantheon' => 'pantheon_wp_clear_edge_all',
137 'Zen Cache' => [ 'zencache', 'clear' ],
138 'Breeze' => [ 'Breeze_PurgeCache', 'breeze_cache_flush' ],
139 'Swift Performance' => [ 'Swift_Performance_Cache', 'clear_all_cache' ],
140 ];
141 foreach ( $others as $plugin => $method ) {
142 if ( is_callable( $method ) ) {
143 call_user_func( $method );
144 }
145 }
146 // Purge Godaddy Managed WordPress Hosting (Varnish + APC).
147 if ( class_exists( 'WPaaS\Plugin' ) ) {
148 self::godaddy_request( 'BAN' );
149 }
150
151 wp_cache_flush();
152 }
153
154
155 /**
156 * Purge GoDaddy Managed WordPress Hosting (Varnish)
157 *
158 * Source: https://github.com/wp-media/wp-rocket/blob/master/inc/3rd-party/hosting/godaddy.php
159 *
160 * @param string $method The request method.
161 * @param string $url The request URL.
162 *
163 * @return void
164 */
165 public static function godaddy_request( $method, $url = null ) {
166 $url = empty( $url ) ? home_url() : $url;
167 $host = wp_parse_url( $url, PHP_URL_HOST );
168 $url = set_url_scheme( str_replace( $host, \WPaas\Plugin::vip(), $url ), 'http' );
169 wp_cache_flush();
170 update_option( 'gd_system_last_cache_flush', time() ); // purge apc.
171 wp_remote_request(
172 esc_url_raw( $url ),
173 [
174 'method' => $method,
175 'blocking' => false,
176 'headers' => [ 'Host' => $host ],
177 ]
178 );
179 }
180
181 /**
182 * Add a template to the template cache.
183 *
184 * @since 4.3.0
185 * @param string $cache_key Object cache key.
186 * @param string $template Located template.
187 */
188 public static function set_template_cache( $cache_key, $template ) {
189 wp_cache_set( $cache_key, $template, 'shopbuilder' );
190 $cached_templates = wp_cache_get( 'shopbuilder_cached_templates', 'shopbuilder' );
191 if ( is_array( $cached_templates ) ) {
192 $cached_templates[] = $cache_key;
193 } else {
194 $cached_templates = [ $cache_key ];
195 }
196 wp_cache_set( 'shopbuilder_cached_templates', $cached_templates, 'shopbuilder' );
197 }
198 /**
199 * Clear the template cache.
200 *
201 * @since 4.3.0
202 */
203 public static function clear_template_cache() {
204 $cached_templates = wp_cache_get( 'shopbuilder_cached_templates', 'shopbuilder' );
205 if ( is_array( $cached_templates ) ) {
206 foreach ( $cached_templates as $cache_key ) {
207 wp_cache_delete( $cache_key, 'shopbuilder' );
208 }
209 wp_cache_delete( 'shopbuilder_cached_templates', 'shopbuilder' );
210 }
211 }
212 /**
213 * Added data cache.
214 *
215 * @since 4.3.0
216 * @param string $cache_key Object cache key.
217 *
218 * @return void
219 */
220 public static function set_data_cache_key( $cache_key ) {
221 $cached_data = wp_cache_get( 'shopbuilder_cached_data', 'shopbuilder' );
222 if ( is_array( $cached_data ) ) {
223 $cached_data[] = $cache_key;
224 } else {
225 $cached_data = [ $cache_key ];
226 }
227 wp_cache_set( 'shopbuilder_cached_data', $cached_data, 'shopbuilder' );
228 }
229 /**
230 * Clear data cache.
231 *
232 * @since 4.3.0
233 */
234 public static function clear_data_cache() {
235 $cached_templates = wp_cache_get( 'shopbuilder_cached_data', 'shopbuilder' );
236 if ( is_array( $cached_templates ) ) {
237 foreach ( $cached_templates as $cache_key ) {
238 wp_cache_delete( $cache_key, 'shopbuilder' );
239 }
240 wp_cache_delete( 'shopbuilder_cached_data', 'shopbuilder' );
241 }
242 }
243
244 /**
245 * Clear asset cache.
246 *
247 * @return void
248 */
249 public static function clear_asset_cache() {
250 if ( ! Fns::is_optimization_enabled() ) {
251 return;
252 }
253
254 $upload_dir = wp_upload_dir();
255 $asset_dir = trailingslashit( $upload_dir['basedir'] ) . 'shopbuilder_uploads/cache/';
256
257 // Delete old assets.
258 if ( is_dir( $asset_dir ) ) {
259 self::delete_dir_contents( $asset_dir );
260 }
261
262 // Re-generate assets.
263 AssetRegistry::instance()->regenerate_bundles();
264 }
265
266 /**
267 * Recursively delete directory contents.
268 *
269 * @param string $dir Directory path.
270 * @return void
271 */
272 public static function delete_dir_contents( $dir ) {
273 global $wp_filesystem;
274
275 if ( ! function_exists( 'WP_Filesystem' ) ) {
276 require_once ABSPATH . 'wp-admin/includes/file.php';
277 }
278
279 if ( ! $wp_filesystem ) {
280 WP_Filesystem();
281 }
282
283 if ( ! $wp_filesystem->is_dir( $dir ) ) {
284 return;
285 }
286
287 $contents = $wp_filesystem->dirlist( $dir );
288
289 if ( ! is_array( $contents ) ) {
290 return;
291 }
292
293 foreach ( $contents as $item ) {
294 $path = trailingslashit( $dir ) . $item['name'];
295
296 if ( 'f' === $item['type'] ) {
297 $wp_filesystem->delete( $path, false );
298 } elseif ( 'd' === $item['type'] ) {
299 self::delete_dir_contents( $path );
300
301 $wp_filesystem->delete( $path, true );
302 }
303 }
304 }
305
306
307 /**
308 * Added data cache.
309 *
310 * @since 4.3.0
311 * @param string $cache_key Transient cache key.
312 */
313 public static function set_transient_cache_key( $cache_key ) {
314 $cached_data = get_transient( 'shopbuilder_transient_cached_data' );
315 if ( is_array( $cached_data ) ) {
316 $cached_data[] = $cache_key;
317 } else {
318 $cached_data = [ $cache_key ];
319 }
320
321 set_transient( 'shopbuilder_transient_cached_data', array_unique( $cached_data ) );
322 }
323
324 /**
325 * Clear data cache.
326 *
327 * @since 4.3.0
328 */
329 public static function clear_transient_cache() {
330 $cached_templates = get_transient( 'shopbuilder_transient_cached_data' );
331 if ( is_array( $cached_templates ) ) {
332 foreach ( $cached_templates as $cache_key ) {
333 delete_transient( $cache_key );
334 }
335 delete_transient( 'shopbuilder_transient_cached_data' );
336 }
337
338 self::delete_all_theme_css_handle_transients();
339 }
340
341 /**
342 * Clear all theme css handle transients.
343 *
344 * @return void
345 */
346 public static function delete_all_theme_css_handle_transients() {
347 global $wpdb;
348
349 $prefix = '_transient_';
350 $transient_ns = 'rtsb_theme_css_handle_';
351
352 // Fetch all matching transient option names.
353 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
354 $transients = $wpdb->get_col(
355 $wpdb->prepare(
356 "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s",
357 $wpdb->esc_like( $prefix . $transient_ns ) . '%'
358 )
359 );
360
361 foreach ( $transients as $option_name ) {
362 $key = str_replace( '_transient_', '', $option_name );
363 delete_transient( $key );
364 }
365 }
366 }
367