| 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\Controllers; |
| 11 |
|
| 12 |
use RadiusTheme\SB\Helpers\Cache; |
| 13 |
use RadiusTheme\SB\Helpers\Fns; |
| 14 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 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 CacheController { |
| 25 |
/** |
| 26 |
* Singleton Trait |
| 27 |
*/ |
| 28 |
use SingletonTrait; |
| 29 |
|
| 30 |
/** |
| 31 |
* Class Constructor |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
// Hook to add custom item to admin bar. |
| 35 |
add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 9999 ); |
| 36 |
add_action( 'init', [ $this, 'admin_bar_menu_action' ] ); |
| 37 |
add_action( 'admin_notices', [ $this, 'cache_notice' ] ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param object $wp_admin_bar hello. |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function admin_bar_menu( $wp_admin_bar ) { |
| 45 |
// Only add for administrators. |
| 46 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
// Add a top-level item. |
| 50 |
$wp_admin_bar->add_node( |
| 51 |
[ |
| 52 |
'id' => 'shop_builder', |
| 53 |
'title' => 'ShopBuilder', |
| 54 |
] |
| 55 |
); |
| 56 |
// Add submenu items. |
| 57 |
$wp_admin_bar->add_node( |
| 58 |
[ |
| 59 |
'id' => 'clear_all_cache', |
| 60 |
'parent' => 'shop_builder', // Set the parent to the top-level item ID. |
| 61 |
'title' => esc_html__( 'Purge All Cache', 'shopbuilder' ), |
| 62 |
'href' => wp_nonce_url( add_query_arg( 'rtsb_clear_cache', 'all', add_query_arg( null, null ) ), rtsb()->nonceText ), |
| 63 |
] |
| 64 |
); |
| 65 |
|
| 66 |
if ( Fns::is_optimization_enabled() ) { |
| 67 |
$wp_admin_bar->add_node( |
| 68 |
[ |
| 69 |
'id' => 'clear_asset_cache', |
| 70 |
'parent' => 'shop_builder', |
| 71 |
'title' => esc_html__( 'Purge Asset Cache', 'shopbuilder' ), |
| 72 |
'href' => wp_nonce_url( add_query_arg( 'rtsb_clear_cache', 'asset', add_query_arg( null, null ) ), rtsb()->nonceText ), |
| 73 |
] |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
$wp_admin_bar->add_node( |
| 78 |
[ |
| 79 |
'id' => 'clear_object_cache', |
| 80 |
'parent' => 'shop_builder', |
| 81 |
'title' => esc_html__( 'Purge Object Cache', 'shopbuilder' ), |
| 82 |
'href' => wp_nonce_url( add_query_arg( 'rtsb_clear_cache', 'data', add_query_arg( null, null ) ), rtsb()->nonceText ), |
| 83 |
] |
| 84 |
); |
| 85 |
$wp_admin_bar->add_node( |
| 86 |
[ |
| 87 |
'id' => 'clear_template_cache', |
| 88 |
'parent' => 'shop_builder', |
| 89 |
'title' => esc_html__( 'Purge Template Cache', 'shopbuilder' ), |
| 90 |
'href' => wp_nonce_url( add_query_arg( 'rtsb_clear_cache', 'template', add_query_arg( null, null ) ), rtsb()->nonceText ), |
| 91 |
] |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* |
| 98 |
* @return mixed |
| 99 |
*/ |
| 100 |
public function admin_bar_menu_action() { |
| 101 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ?? '' ) ), rtsb()->nonceText ) ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$clear_cache = sanitize_text_field( wp_unslash( $_GET['rtsb_clear_cache'] ?? '' ) ); |
| 109 |
if ( empty( $clear_cache ) ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
switch ( $clear_cache ) { |
| 114 |
case 'all': |
| 115 |
Cache::clear_all_cache(); |
| 116 |
break; |
| 117 |
case 'data': |
| 118 |
Cache::clear_data_cache(); |
| 119 |
break; |
| 120 |
case 'template': |
| 121 |
Cache::clear_template_cache(); |
| 122 |
break; |
| 123 |
case 'asset': |
| 124 |
Cache::clear_asset_cache(); |
| 125 |
break; |
| 126 |
} |
| 127 |
// Set the notice flag. |
| 128 |
set_transient( 'rtsb_cache_cleared_notice', true, 30 ); |
| 129 |
$referrer = wp_get_referer(); // Get the referrer URL. |
| 130 |
if ( $referrer ) { |
| 131 |
// Rebuild the URL with the original path and query string. |
| 132 |
$redirect_url = $referrer; |
| 133 |
// Redirect to the referrer URL with its query string intact. |
| 134 |
wp_safe_redirect( $redirect_url ); |
| 135 |
exit; |
| 136 |
} |
| 137 |
// Fallback: if referrer is not available, redirect to home_url(). |
| 138 |
wp_safe_redirect( home_url() ); |
| 139 |
exit; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function cache_notice() { |
| 146 |
if ( get_transient( 'rtsb_cache_cleared_notice' ) ) { |
| 147 |
echo '<div class="notice notice-success is-dismissible">'; |
| 148 |
echo '<p>' . esc_html__( 'Shopbuilder cache has been successfully cleared.', 'shopbuilder' ) . '</p>'; |
| 149 |
echo '</div>'; |
| 150 |
delete_transient( 'rtsb_cache_cleared_notice' ); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|