| @@ -8,10 +8,11 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace RadiusTheme\SB\Helpers; |
| 11 | 11 | |
| 12 | +use W3TC\Dispatcher; | |
| 12 | 13 | use RadiusTheme\SB\Traits\SingletonTrait; |
| 13 | -use W3TC\Dispatcher; | |
| 14 | +use RadiusTheme\SB\Controllers\AssetRegistry; | |
| 14 | 15 | |
| 15 | 16 | // Do not allow directly accessing this file. |
| 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | 18 | exit( 'This script cannot be accessed directly.' ); |
| @@ -30,8 +31,10 @@ | ||
| 30 | 31 | public static function clear_all_cache() { |
| 31 | 32 | self::clear_data_cache(); |
| 32 | 33 | self::clear_template_cache(); |
| 33 | 34 | self::clear_plugins_cache(); |
| 35 | + self::clear_transient_cache(); | |
| 36 | + self::clear_asset_cache(); | |
| 34 | 37 | } |
| 35 | 38 | /** |
| 36 | 39 | * Clear the template cache. |
| 37 | 40 | * |
| @@ -52,8 +55,11 @@ | ||
| 52 | 55 | } |
| 53 | 56 | if ( method_exists( 'LiteSpeed_Cache_API', 'purge_all' ) ) { |
| 54 | 57 | \LiteSpeed_Cache_API::purge_all(); |
| 55 | 58 | } |
| 59 | + if ( class_exists( '\LiteSpeed\Purge' ) ) { | |
| 60 | + \LiteSpeed\Purge::purge_all(); | |
| 61 | + } | |
| 56 | 62 | if ( class_exists( 'Endurance_Page_Cache' ) ) { |
| 57 | 63 | $epc = new \Endurance_Page_Cache(); |
| 58 | 64 | $epc->purge_all(); |
| 59 | 65 | } |
| @@ -153,14 +159,16 @@ | ||
| 153 | 159 | * Purge GoDaddy Managed WordPress Hosting (Varnish) |
| 154 | 160 | * |
| 155 | 161 | * Source: https://github.com/wp-media/wp-rocket/blob/master/inc/3rd-party/hosting/godaddy.php |
| 156 | 162 | * |
| 157 | - * @param String $method | |
| 158 | - * @param String|Null $url | |
| 163 | + * @param string $method The request method. | |
| 164 | + * @param string $url The request URL. | |
| 165 | + * | |
| 166 | + * @return void | |
| 159 | 167 | */ |
| 160 | 168 | public static function godaddy_request( $method, $url = null ) { |
| 161 | 169 | $url = empty( $url ) ? home_url() : $url; |
| 162 | - $host = parse_url( $url, PHP_URL_HOST ); | |
| 170 | + $host = wp_parse_url( $url, PHP_URL_HOST ); | |
| 163 | 171 | $url = set_url_scheme( str_replace( $host, \WPaas\Plugin::vip(), $url ), 'http' ); |
| 164 | 172 | wp_cache_flush(); |
| 165 | 173 | update_option( 'gd_system_last_cache_flush', time() ); // purge apc. |
| 166 | 174 | wp_remote_request( |
| @@ -208,9 +216,10 @@ | ||
| 208 | 216 | * Added data cache. |
| 209 | 217 | * |
| 210 | 218 | * @since 4.3.0 |
| 211 | 219 | * @param string $cache_key Object cache key. |
| 212 | - * @param string $data Located template. | |
| 220 | + * | |
| 221 | + * @return void | |
| 213 | 222 | */ |
| 214 | 223 | public static function set_data_cache_key( $cache_key ) { |
| 215 | 224 | $cached_data = wp_cache_get( 'shopbuilder_cached_data', 'shopbuilder' ); |
| 216 | 225 | if ( is_array( $cached_data ) ) { |
| @@ -231,7 +240,130 @@ | ||
| 231 | 240 | foreach ( $cached_templates as $cache_key ) { |
| 232 | 241 | wp_cache_delete( $cache_key, 'shopbuilder' ); |
| 233 | 242 | } |
| 234 | 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 ); | |
| 235 | 367 | } |
| 236 | 368 | } |
| 237 | 369 | } |