PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.5.3
ShopBuilder – WooCommerce Builder For Elementor v2.5.3
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 / Controllers / CacheController.php

CacheController.php in ShopBuilder – WooCommerce Builder For Elementor 2.5.3, at app/Controllers/CacheController.php

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