| 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 RadiusTheme\SB\Traits\SingletonTrait; |
| 13 |
use W3TC\Dispatcher; |
| 14 |
|
| 15 |
// Do not allow directly accessing this file. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit( 'This script cannot be accessed directly.' ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* CacheController Class. |
| 22 |
*/ |
| 23 |
class Cache { |
| 24 |
|
| 25 |
/** |
| 26 |
* Clear the template cache. |
| 27 |
* |
| 28 |
* @since 4.3.0 |
| 29 |
*/ |
| 30 |
public static function clear_all_cache() { |
| 31 |
self::clear_data_cache(); |
| 32 |
self::clear_template_cache(); |
| 33 |
self::clear_plugins_cache(); |
| 34 |
self::clear_transient_cache(); |
| 35 |
} |
| 36 |
/** |
| 37 |
* Clear the template cache. |
| 38 |
* |
| 39 |
* @since 4.3.0 |
| 40 |
*/ |
| 41 |
public static function clear_plugins_cache() { |
| 42 |
// Clear W3 Total Cache. |
| 43 |
if ( function_exists( 'w3tc_flush_all' ) ) { |
| 44 |
w3tc_flush_all(); |
| 45 |
} |
| 46 |
// Clear WP Super Cache. |
| 47 |
if ( function_exists( 'wp_cache_clear_cache' ) ) { |
| 48 |
wp_cache_clear_cache(); |
| 49 |
} |
| 50 |
// Clear WP Rocket cache. |
| 51 |
if ( function_exists( 'rocket_clean_domain' ) ) { |
| 52 |
rocket_clean_domain(); |
| 53 |
} |
| 54 |
if ( method_exists( 'LiteSpeed_Cache_API', 'purge_all' ) ) { |
| 55 |
\LiteSpeed_Cache_API::purge_all(); |
| 56 |
} |
| 57 |
if ( class_exists( 'Endurance_Page_Cache' ) ) { |
| 58 |
$epc = new \Endurance_Page_Cache(); |
| 59 |
$epc->purge_all(); |
| 60 |
} |
| 61 |
if ( class_exists( 'SG_CachePress_Supercacher' ) && method_exists( 'SG_CachePress_Supercacher', 'purge_cache' ) ) { |
| 62 |
\SG_CachePress_Supercacher::purge_cache( true ); |
| 63 |
} |
| 64 |
if ( class_exists( 'SiteGround_Optimizer\Supercacher\Supercacher' ) ) { |
| 65 |
\SiteGround_Optimizer\Supercacher\Supercacher::purge_cache(); |
| 66 |
} |
| 67 |
if ( isset( $GLOBALS['wp_fastest_cache'] ) && method_exists( $GLOBALS['wp_fastest_cache'], 'deleteCache' ) ) { |
| 68 |
$GLOBALS['wp_fastest_cache']->deleteCache( true ); |
| 69 |
} |
| 70 |
if ( is_callable( [ 'Swift_Performance_Cache', 'clear_all_cache' ] ) ) { |
| 71 |
\Swift_Performance_Cache::clear_all_cache(); |
| 72 |
} |
| 73 |
if ( is_callable( [ 'Hummingbird\WP_Hummingbird', 'flush_cache' ] ) ) { |
| 74 |
\Hummingbird\WP_Hummingbird::flush_cache( true, false ); |
| 75 |
} |
| 76 |
if ( class_exists( 'WP_Optimize' ) ) { |
| 77 |
\WP_Optimize()->get_page_cache()->purge(); |
| 78 |
} |
| 79 |
|
| 80 |
// Purge WP Engine. |
| 81 |
if ( class_exists( 'WpeCommon' ) ) { |
| 82 |
if ( method_exists( 'WpeCommon', 'purge_memcached' ) ) { |
| 83 |
\WpeCommon::purge_memcached(); |
| 84 |
} |
| 85 |
if ( method_exists( 'WpeCommon', 'clear_maxcdn_cache' ) ) { |
| 86 |
\WpeCommon::clear_maxcdn_cache(); |
| 87 |
} |
| 88 |
if ( method_exists( 'WpeCommon', 'purge_varnish_cache' ) ) { |
| 89 |
\WpeCommon::purge_varnish_cache(); |
| 90 |
} |
| 91 |
} |
| 92 |
// Purge Kinsta. |
| 93 |
global $kinsta_cache; |
| 94 |
if ( isset( $kinsta_cache ) && class_exists( '\\Kinsta\\CDN_Enabler' ) ) { |
| 95 |
if ( ! empty( $kinsta_cache->kinsta_cache_purge ) && is_callable( [ $kinsta_cache->kinsta_cache_purge, 'purge_complete_caches' ] ) ) { |
| 96 |
$kinsta_cache->kinsta_cache_purge->purge_complete_caches(); |
| 97 |
} |
| 98 |
} |
| 99 |
// Purge Pagely. |
| 100 |
if ( class_exists( 'PagelyCachePurge' ) ) { |
| 101 |
$purge_pagely = new \PagelyCachePurge(); |
| 102 |
if ( is_callable( [ $purge_pagely, 'purgeAll' ] ) ) { |
| 103 |
$purge_pagely->purgeAll(); |
| 104 |
} |
| 105 |
} |
| 106 |
// Purge Pressidum. |
| 107 |
if ( defined( 'WP_NINUKIS_WP_NAME' ) && class_exists( 'Ninukis_Plugin' ) && is_callable( [ 'Ninukis_Plugin', 'get_instance' ] ) ) { |
| 108 |
$purge_pressidum = \Ninukis_Plugin::get_instance(); |
| 109 |
if ( is_callable( [ $purge_pressidum, 'purgeAllCaches' ] ) ) { |
| 110 |
$purge_pressidum->purgeAllCaches(); |
| 111 |
} |
| 112 |
} |
| 113 |
// Purge Savvii. |
| 114 |
if ( defined( '\Savvii\CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW' ) ) { |
| 115 |
$purge_savvii = new \Savvii\CacheFlusherPlugin(); |
| 116 |
if ( is_callable( [ $purge_savvii, 'domainflush' ] ) ) { |
| 117 |
$purge_savvii->domainflush(); |
| 118 |
} |
| 119 |
} |
| 120 |
// Purge Hyper Cache. |
| 121 |
if ( class_exists( 'HyperCache' ) ) { |
| 122 |
do_action( 'autoptimize_action_cachepurged' ); |
| 123 |
} |
| 124 |
// purge cache enabler. |
| 125 |
if ( has_action( 'ce_clear_cache' ) ) { |
| 126 |
do_action( 'ce_clear_cache' ); |
| 127 |
} |
| 128 |
// When plugins have a simple method, add them to the array ('Plugin Name' => 'method_name'). |
| 129 |
$others = [ |
| 130 |
'WP Fastest Cache' => 'wpfc_clear_all_cache', |
| 131 |
'Cachify' => 'cachify_flush_cache', |
| 132 |
'Comet Cache' => [ 'comet_cache', 'clear' ], |
| 133 |
'SG Optimizer' => 'sg_cachepress_purge_cache', |
| 134 |
'Pantheon' => 'pantheon_wp_clear_edge_all', |
| 135 |
'Zen Cache' => [ 'zencache', 'clear' ], |
| 136 |
'Breeze' => [ 'Breeze_PurgeCache', 'breeze_cache_flush' ], |
| 137 |
'Swift Performance' => [ 'Swift_Performance_Cache', 'clear_all_cache' ], |
| 138 |
]; |
| 139 |
foreach ( $others as $plugin => $method ) { |
| 140 |
if ( is_callable( $method ) ) { |
| 141 |
call_user_func( $method ); |
| 142 |
} |
| 143 |
} |
| 144 |
// Purge Godaddy Managed WordPress Hosting (Varnish + APC). |
| 145 |
if ( class_exists( 'WPaaS\Plugin' ) ) { |
| 146 |
self::godaddy_request( 'BAN' ); |
| 147 |
} |
| 148 |
|
| 149 |
wp_cache_flush(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/** |
| 154 |
* Purge GoDaddy Managed WordPress Hosting (Varnish) |
| 155 |
* |
| 156 |
* Source: https://github.com/wp-media/wp-rocket/blob/master/inc/3rd-party/hosting/godaddy.php |
| 157 |
* |
| 158 |
* @param String $method |
| 159 |
* @param String|Null $url |
| 160 |
*/ |
| 161 |
public static function godaddy_request( $method, $url = null ) { |
| 162 |
$url = empty( $url ) ? home_url() : $url; |
| 163 |
$host = wp_parse_url( $url, PHP_URL_HOST ); |
| 164 |
$url = set_url_scheme( str_replace( $host, \WPaas\Plugin::vip(), $url ), 'http' ); |
| 165 |
wp_cache_flush(); |
| 166 |
update_option( 'gd_system_last_cache_flush', time() ); // purge apc. |
| 167 |
wp_remote_request( |
| 168 |
esc_url_raw( $url ), |
| 169 |
[ |
| 170 |
'method' => $method, |
| 171 |
'blocking' => false, |
| 172 |
'headers' => [ 'Host' => $host ], |
| 173 |
] |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Add a template to the template cache. |
| 179 |
* |
| 180 |
* @since 4.3.0 |
| 181 |
* @param string $cache_key Object cache key. |
| 182 |
* @param string $template Located template. |
| 183 |
*/ |
| 184 |
public static function set_template_cache( $cache_key, $template ) { |
| 185 |
wp_cache_set( $cache_key, $template, 'shopbuilder' ); |
| 186 |
$cached_templates = wp_cache_get( 'shopbuilder_cached_templates', 'shopbuilder' ); |
| 187 |
if ( is_array( $cached_templates ) ) { |
| 188 |
$cached_templates[] = $cache_key; |
| 189 |
} else { |
| 190 |
$cached_templates = [ $cache_key ]; |
| 191 |
} |
| 192 |
wp_cache_set( 'shopbuilder_cached_templates', $cached_templates, 'shopbuilder' ); |
| 193 |
} |
| 194 |
/** |
| 195 |
* Clear the template cache. |
| 196 |
* |
| 197 |
* @since 4.3.0 |
| 198 |
*/ |
| 199 |
public static function clear_template_cache() { |
| 200 |
$cached_templates = wp_cache_get( 'shopbuilder_cached_templates', 'shopbuilder' ); |
| 201 |
if ( is_array( $cached_templates ) ) { |
| 202 |
foreach ( $cached_templates as $cache_key ) { |
| 203 |
wp_cache_delete( $cache_key, 'shopbuilder' ); |
| 204 |
} |
| 205 |
wp_cache_delete( 'shopbuilder_cached_templates', 'shopbuilder' ); |
| 206 |
} |
| 207 |
} |
| 208 |
/** |
| 209 |
* Added data cache. |
| 210 |
* |
| 211 |
* @since 4.3.0 |
| 212 |
* @param string $cache_key Object cache key. |
| 213 |
* @param string $data Located template. |
| 214 |
*/ |
| 215 |
public static function set_data_cache_key( $cache_key ) { |
| 216 |
$cached_data = wp_cache_get( 'shopbuilder_cached_data', 'shopbuilder' ); |
| 217 |
if ( is_array( $cached_data ) ) { |
| 218 |
$cached_data[] = $cache_key; |
| 219 |
} else { |
| 220 |
$cached_data = [ $cache_key ]; |
| 221 |
} |
| 222 |
wp_cache_set( 'shopbuilder_cached_data', $cached_data, 'shopbuilder' ); |
| 223 |
} |
| 224 |
/** |
| 225 |
* Clear data cache. |
| 226 |
* |
| 227 |
* @since 4.3.0 |
| 228 |
*/ |
| 229 |
public static function clear_data_cache() { |
| 230 |
$cached_templates = wp_cache_get( 'shopbuilder_cached_data', 'shopbuilder' ); |
| 231 |
if ( is_array( $cached_templates ) ) { |
| 232 |
foreach ( $cached_templates as $cache_key ) { |
| 233 |
wp_cache_delete( $cache_key, 'shopbuilder' ); |
| 234 |
} |
| 235 |
wp_cache_delete( 'shopbuilder_cached_data', 'shopbuilder' ); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
/** |
| 241 |
* Added data cache. |
| 242 |
* |
| 243 |
* @since 4.3.0 |
| 244 |
* @param string $cache_key Transient cache key. |
| 245 |
*/ |
| 246 |
public static function set_transient_cache_key( $cache_key ) { |
| 247 |
$cached_data = get_transient( 'shopbuilder_transient_cached_data' ); |
| 248 |
if ( is_array( $cached_data ) ) { |
| 249 |
$cached_data[] = $cache_key; |
| 250 |
} else { |
| 251 |
$cached_data = [ $cache_key ]; |
| 252 |
} |
| 253 |
|
| 254 |
set_transient( 'shopbuilder_transient_cached_data', array_unique( $cached_data ) ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Clear data cache. |
| 259 |
* |
| 260 |
* @since 4.3.0 |
| 261 |
*/ |
| 262 |
public static function clear_transient_cache() { |
| 263 |
$cached_templates = get_transient( 'shopbuilder_transient_cached_data' ); |
| 264 |
if ( is_array( $cached_templates ) ) { |
| 265 |
foreach ( $cached_templates as $cache_key ) { |
| 266 |
delete_transient( $cache_key ); |
| 267 |
} |
| 268 |
delete_transient( 'shopbuilder_transient_cached_data' ); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|