|
12 | 12 | * Plugin URI: https://github.com/soderlind/super-admin-all-sites-menu
|
13 | 13 | * GitHub Plugin URI: https://github.com/soderlind/super-admin-all-sites-menu
|
14 | 14 | * Description: For the super admin, replace WP Admin Bar My Sites menu with an All Sites menu.
|
15 |
| - * Version: 1.7.3 |
| 15 | + * Version: 1.8.0 |
16 | 16 | * Author: Per Soderlind
|
17 | 17 | * Network: true
|
18 | 18 | * Author URI: https://soderlind.no
|
|
27 | 27 | if ( ! defined( 'ABSPATH' ) ) {
|
28 | 28 | wp_die();
|
29 | 29 | }
|
30 |
| -/** |
31 |
| - * Default values for the plugin. |
32 |
| - */ |
33 |
| -const LOADINCREMENTS = 100; // Number of sites to load at a time. |
34 |
| -const SEARCHTHRESHOLD = 2; // Number of sites before showing the search box. |
35 |
| -const CACHE_EXPIRATION = DAY_IN_SECONDS; // Time to cache the site list. |
36 |
| -const ORDERBY = 'name'; // Order by name. |
37 |
| -const PLUGINS = [ 'restricted-site-access/restricted_site_access.php' ]; // Plugins triggering update local storages. |
38 |
| -const REST_NAMESPACE = 'super-admin-all-sites-menu/v1'; // REST API namespace. |
39 |
| -const REST_BASE = 'sites'; // REST API route. |
40 |
| -const REST_ENDPOINT = REST_NAMESPACE . '/' . REST_BASE; // REST API endpoint. |
41 |
| -/** |
42 |
| - * Super Admin All Sites Menu |
43 |
| - */ |
44 |
| -class SuperAdminAllSitesMenu { |
45 |
| - |
46 |
| - /** |
47 |
| - * AJAX load increments. |
48 |
| - * |
49 |
| - * @var [type] |
50 |
| - */ |
51 |
| - private $load_increments; |
52 |
| - |
53 |
| - /** |
54 |
| - * Plugins triggering update local storages. |
55 |
| - * |
56 |
| - * @var array |
57 |
| - */ |
58 |
| - private $plugins; |
59 |
| - |
60 |
| - /** |
61 |
| - * Sort menu by site name. |
62 |
| - * |
63 |
| - * @var string |
64 |
| - */ |
65 |
| - private $order_by; |
66 | 30 |
|
| 31 | +// Configuration constants |
| 32 | +final class Config { |
| 33 | + public const LOAD_INCREMENTS = 100; |
| 34 | + public const SEARCH_THRESHOLD = 20; |
| 35 | + public const CACHE_EXPIRATION = DAY_IN_SECONDS; |
| 36 | + public const ORDER_BY = 'name'; |
| 37 | + public const PLUGINS = [ 'restricted-site-access/restricted_site_access.php' ]; |
| 38 | + public const REST_NAMESPACE = 'super-admin-all-sites-menu/v1'; |
| 39 | + public const REST_BASE = 'sites'; |
| 40 | + public const REST_ENDPOINT = self::REST_NAMESPACE . '/' . self::REST_BASE; |
| 41 | +} |
67 | 42 |
|
68 |
| - /** |
69 |
| - * Store number of sites. |
70 |
| - * |
71 |
| - * @var integer |
72 |
| - */ |
73 |
| - private $number_of_sites = 0; |
74 |
| - |
75 |
| - /** |
76 |
| - * Set the search threshold. |
77 |
| - * |
78 |
| - * @var [type] |
79 |
| - */ |
80 |
| - private $search_threshold; |
| 43 | +/** |
| 44 | + * Super Admin All Sites Menu main class |
| 45 | + */ |
| 46 | +final class SuperAdminAllSitesMenu { |
| 47 | + private int $number_of_sites = 0; |
| 48 | + |
| 49 | + public function __construct( |
| 50 | + private int $load_increments = Config::LOAD_INCREMENTS, |
| 51 | + private array $plugins = Config::PLUGINS, |
| 52 | + private string $order_by = Config::ORDER_BY, |
| 53 | + private int $search_threshold = Config::SEARCH_THRESHOLD, |
| 54 | + private int $cache_expiration = Config::CACHE_EXPIRATION |
| 55 | + ) {} |
| 56 | + |
| 57 | + public function init(): void { |
| 58 | + if ( ! is_multisite() ) { |
| 59 | + return; |
| 60 | + } |
81 | 61 |
|
82 |
| - /** |
83 |
| - * The cache expiration time. |
84 |
| - * |
85 |
| - * @var integer |
86 |
| - */ |
87 |
| - private $cache_expiration; |
88 |
| - /** |
89 |
| - * Constructor. |
90 |
| - */ |
91 |
| - public function __construct() { |
92 |
| - return $this; |
| 62 | + $this->set_properties(); |
| 63 | + $this->register_hooks(); |
93 | 64 | }
|
94 | 65 |
|
95 |
| - /** |
96 |
| - * Initialize the plugin. |
97 |
| - */ |
98 |
| - public function init() { |
| 66 | + private function register_hooks(): void { |
99 | 67 | add_action( 'admin_bar_init', [ $this, 'action_admin_bar_init' ] );
|
100 | 68 | add_action( 'rest_api_init', [ $this, 'action_rest_api_init' ] );
|
101 | 69 | register_deactivation_hook( __FILE__, [ $this, 'deactivate' ] );
|
102 |
| - $this->set_properties(); |
| 70 | + |
| 71 | + // Site changes |
| 72 | + add_action( 'wp_insert_site', [ $this, 'update_local_storage' ] ); |
| 73 | + add_action( 'wp_update_site', [ $this, 'update_local_storage' ] ); |
| 74 | + add_action( 'wp_delete_site', [ $this, 'update_local_storage' ] ); |
| 75 | + add_action( 'update_option_blogname', [ $this, 'action_update_option_blogname' ], 10, 3 ); |
| 76 | + |
| 77 | + // Plugin activation/deactivation |
| 78 | + add_action( 'activated_plugin', [ $this, 'plugin_update_local_storage' ] ); |
| 79 | + add_action( 'deactivated_plugin', [ $this, 'plugin_update_local_storage' ] ); |
103 | 80 | }
|
104 | 81 |
|
105 | 82 | /**
|
@@ -134,28 +111,28 @@ public function action_admin_bar_init(): void {
|
134 | 111 | * @return void
|
135 | 112 | */
|
136 | 113 | public function set_properties(): void {
|
137 |
| - $this->plugins = \apply_filters( 'all_sites_menu_plugin_trigger', PLUGINS ); |
| 114 | + $this->plugins = \apply_filters( 'all_sites_menu_plugin_trigger', Config::PLUGINS ); |
138 | 115 | if ( ! is_array( $this->plugins ) ) {
|
139 |
| - $this->plugins = PLUGINS; |
| 116 | + $this->plugins = Config::PLUGINS; |
140 | 117 | }
|
141 | 118 |
|
142 |
| - $this->order_by = \apply_filters( 'all_sites_menu_order_by', ORDERBY ); |
| 119 | + $this->order_by = \apply_filters( 'all_sites_menu_order_by', Config::ORDER_BY ); |
143 | 120 | if ( ! in_array( $this->order_by, [ 'name', 'url', 'id' ], true ) ) {
|
144 |
| - $this->order_by = ORDERBY; |
| 121 | + $this->order_by = Config::ORDER_BY; |
145 | 122 | }
|
146 | 123 |
|
147 |
| - $this->load_increments = \apply_filters( 'all_sites_menu_load_increments', LOADINCREMENTS ); |
| 124 | + $this->load_increments = \apply_filters( 'all_sites_menu_load_increments', Config::LOAD_INCREMENTS ); |
148 | 125 | if ( ! is_numeric( $this->load_increments ) || $this->load_increments < 1 ) {
|
149 |
| - $this->load_increments = LOADINCREMENTS; |
| 126 | + $this->load_increments = Config::LOAD_INCREMENTS; |
150 | 127 | }
|
151 | 128 |
|
152 |
| - $this->search_threshold = \apply_filters( 'all_sites_menu_search_threshold', SEARCHTHRESHOLD ); |
| 129 | + $this->search_threshold = \apply_filters( 'all_sites_menu_search_threshold', Config::SEARCH_THRESHOLD ); |
153 | 130 | if ( ! is_numeric( $this->search_threshold ) || $this->search_threshold < 1 ) {
|
154 |
| - $this->search_threshold = SEARCHTHRESHOLD; |
| 131 | + $this->search_threshold = Config::SEARCH_THRESHOLD; |
155 | 132 | }
|
156 |
| - $this->cache_expiration = \apply_filters( 'all_sites_menu_force_refresh_expiration', CACHE_EXPIRATION ); |
| 133 | + $this->cache_expiration = \apply_filters( 'all_sites_menu_force_refresh_expiration', Config::CACHE_EXPIRATION ); |
157 | 134 | if ( ! is_numeric( $this->search_threshold ) || $this->cache_expiration < 0 ) {
|
158 |
| - $this->cache_expiration = CACHE_EXPIRATION; |
| 135 | + $this->cache_expiration = Config::CACHE_EXPIRATION; |
159 | 136 | }
|
160 | 137 | }
|
161 | 138 |
|
@@ -312,8 +289,8 @@ public function super_admin_all_sites_menu( \WP_Admin_Bar $wp_admin_bar ): void
|
312 | 289 | */
|
313 | 290 | public function action_rest_api_init( \WP_REST_Server $wp_rest_server ): void {
|
314 | 291 | $is_route_created = register_rest_route(
|
315 |
| - REST_NAMESPACE, |
316 |
| - '/' . REST_BASE, |
| 292 | + Config::REST_NAMESPACE, |
| 293 | + '/' . Config::REST_BASE, |
317 | 294 | [
|
318 | 295 | 'methods' => \WP_REST_Server::CREATABLE,
|
319 | 296 | 'callback' => [ $this, 'get_sites' ],
|
@@ -430,7 +407,7 @@ public function action_enqueue_scripts( string $hook_suffix ): void {
|
430 | 407 | $data = wp_json_encode(
|
431 | 408 | [
|
432 | 409 | 'nonce' => wp_create_nonce( 'wp_rest' ),
|
433 |
| - 'restURL' => rest_url() . REST_ENDPOINT, |
| 410 | + 'restURL' => rest_url() . Config::REST_ENDPOINT, |
434 | 411 | 'loadincrements' => $this->load_increments,
|
435 | 412 | 'orderBy' => $this->order_by,
|
436 | 413 | 'displaySearch' => ( $this->number_of_sites > $this->search_threshold ) ? true : false,
|
@@ -540,7 +517,5 @@ private function remove_timestamp(): void {
|
540 | 517 |
|
541 | 518 | }
|
542 | 519 |
|
543 |
| -if ( \is_multisite() ) { |
544 |
| - $super_admin_menu = new SuperAdminAllSitesMenu(); |
545 |
| - $super_admin_menu->init(); |
546 |
| -} |
| 520 | +// Initialize plugin |
| 521 | +( new SuperAdminAllSitesMenu() )->init(); |
0 commit comments