Repository navigation
Expand file tree
/
Copy pathcache-bar.php
More file actions
208 lines (158 loc) · 5.78 KB
/
Copy pathcache-bar.php
File metadata and controls
208 lines (158 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Plugin Name: Cache Bar
* Description: Streamline your cache clearing.
* Version: 0.1.0
* Plugin URI: https://mediumrare.dev
* Author: Medium Rare
* Author URI: https://mediumrare.dev
* Text Domain: cache-bar
*/
namespace CCC;
defined( 'ABSPATH' ) || exit;
define( 'CCC_FILE', __FILE__ );
define( 'CCC_BASENAME', plugin_basename( CCC_FILE ) );
define( 'CCC_VERSION', '0.1.0' );
final class Plugin
{
private static $instance = null;
private $asp = null;
public static function instance()
{
if ( self::$instance === null )
self::$instance = new self();
return self::$instance;
}
private function __construct()
{
$this->register_hooks();
}
private function register_hooks()
{
add_action( 'admin_enqueue_scripts', [ $this, 'register_backend_styles' ] );
add_action( 'admin_bar_menu', [ $this, 'add_ccc_toolbar' ], 500 );
add_action( 'wp_before_admin_bar_render', [ $this, 'remove_third_party_toolbars' ], 100 );
add_action( 'admin_post_ccc_clear_transients', [ $this, 'handle_clear_transients' ] );
add_action( 'admin_notices', [ $this, 'transients_cleared_notice' ] );
}
public function register_backend_styles()
{
wp_register_style(
'ccc',
plugin_dir_url( CCC_FILE ).'assets/ccc.min.css',
[],
CCC_VERSION,
'all'
);
wp_enqueue_style( 'ccc' );
}
public function add_ccc_toolbar( $wp_admin_bar )
{
if ( is_null( $this->asp ) )
$this->asp = $this->active_supported_plugins();
if ( !current_user_can( apply_filters( 'ccc_add_toolbar', 'manage_options' ) ) )
return;
$parent = apply_filters( 'ccc_toolbar_position_right', false ) ? 'top-secondary' : false;
$wp_admin_bar->add_node([
'id' => 'ccc-toolbar',
'title' => 'Cache',
'parent' => $parent,
]);
$wp_admin_bar->add_node([
'id' => 'ccc-toolbar-transients',
'title' => 'Clear transients',
'parent' => 'ccc-toolbar',
'href' => wp_nonce_url( admin_url('admin-post.php?action=ccc_clear_transients'), 'ccc_clear_transients' ),
]);
foreach ( $this->asp as $plugin )
{
foreach ( $plugin['links'] as $index => $link )
{
$title = $link['label'];
$href = $link['url'];
if ( isset( $link['settings'] ) )
{
$title = '';
$title .= '<a href="'.$link['url'].'" class="ei-left">'.$link['label'].'</a>';
$title .= '<a href="'.$link['settings'].'" class="ei-right"><span class="ab-icon"></span></a>';
$href = false;
}
$wp_admin_bar->add_node([
'id' => 'ccc-toolbar-'.$plugin['id'].'-'.$index,
'title' => $title,
'parent' => 'ccc-toolbar',
'href' => $href,
]);
}
}
}
public function remove_third_party_toolbars()
{
if ( is_null( $this->asp ) )
$this->asp = $this->active_supported_plugins();
if ( empty( $this->asp ) )
return;
if ( current_user_can( apply_filters( 'ccc_keep_third_party_toolbars', 'loremipsumdolorsitamet' ) ) )
return;
global $wp_admin_bar;
foreach ( $this->asp as $plugin )
foreach ( $plugin['rn'] ?? [] as $node )
$wp_admin_bar->remove_node( $node );
}
private function active_supported_plugins()
{
$all_supported_plugins = $this->all_supported_plugins();
$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
$active_plugins = array_flip( $active_plugins );
return array_filter( $all_supported_plugins, function ( $supported_plugin ) use ( $active_plugins ) {
return isset( $active_plugins[ $supported_plugin['slug'] ] );
});
}
private function all_supported_plugins()
{
$all_supported_plugins = [];
foreach ( glob( __DIR__.'/supported-plugins/*.php' ) as $file )
$all_supported_plugins[] = include $file;
return apply_filters( 'cache_bar_plugins', $all_supported_plugins );
}
private function delete_all_transients()
{
$transient_names = $this->get_transient_names();
foreach ( $transient_names as $transient_name )
delete_transient( $transient_name );
return count( $transient_names );
}
private function get_transient_names()
{
global $wpdb;
$transient_names = $wpdb->get_col("
SELECT option_name
FROM {$wpdb->options}
WHERE option_name LIKE '_transient_%'
");
return array_map( fn ( $name ) => substr( $name, 11 ), $transient_names );
}
public function handle_clear_transients()
{
if ( !current_user_can( apply_filters( 'ccc_add_toolbar', 'manage_options' ) ) )
wp_die('Insufficient permissions');
check_admin_referer('ccc_clear_transients');
$count = $this->delete_all_transients();
wp_safe_redirect( add_query_arg( [
'ccc-deleted-transients' => true,
'count' => $count,
], admin_url() ) );
exit;
}
public function transients_cleared_notice()
{
if ( !isset( $_GET['ccc-deleted-transients'] ) )
return;
?>
<div class="notice notice-success is-dismissible">
<p>Cleared all <?=absint( $_GET['count'] );?> transients.</p>
</div>
<?php
}
}
add_action( 'plugins_loaded', [ Plugin::class, 'instance' ] );