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