-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache-bar.php
More file actions
150 lines (113 loc) · 4.03 KB
/
cache-bar.php
File metadata and controls
150 lines (113 loc) · 4.03 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
<?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 );
}
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 ( empty( $this->asp ) )
return;
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,
]);
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 );
}
}
add_action( 'plugins_loaded', [ Plugin::class, 'instance' ] );