Skip to content

Commit 23d2238

Browse files
authored
Merge pull request #34 from soderlind/php8
Code modernization: Refactored to use PHP 8.0+ features
2 parents 7edf423 + d3a9378 commit 23d2238

7 files changed

+97
-89
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# Changelog
22

3+
### 1.8.0
4+
5+
> REQUIRE PHP 8.0 OR HIGHER
6+
7+
#### Added
8+
- New Config class to manage plugin constants
9+
- Constructor property promotion for better code organization
10+
- Strict type declarations throughout the codebase
11+
12+
#### Changed
13+
- Increased default search threshold from 2 to 20 sites
14+
- Refactored main class to use modern PHP 8.0+ features
15+
- Improved error handling and null checks
16+
- Better type safety with more specific type declarations
17+
- Simplified plugin initialization
18+
19+
#### Developer Notes
20+
- The Config class now centralizes all plugin constants
21+
- Removed redundant property declarations in favor of constructor property promotion
22+
- Added strict typing for better code reliability
23+
- REST endpoints now use Config class constants
24+
325
### 1.7.3
426

527
- Fixed search functionality:
@@ -259,3 +281,5 @@
259281
### 1.0.x
260282

261283
- Initial release.
284+
285+

build/index.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('wp-api-fetch', 'wp-i18n'), 'version' => '6df7975b628e4eda2261');
1+
<?php return array('dependencies' => array('wp-api-fetch', 'wp-i18n'), 'version' => '8a8e912db6e7c0137fcb');

build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "soderlind/super-admin-all-sites-menu",
33
"description": "For the super admin, replace WP Admin Bar My Sites menu with an All Sites menu.",
4-
"version": "1.7.3",
4+
"version": "1.8.0",
55
"keywords": [
66
"wordpress",
77
"multisite",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "super-admin-all-sites-menu",
3-
"version": "1.7.3",
3+
"version": "1.8.0",
44
"description": "For the super admin, replace WP Admin Bar My Sites menu with an All Sites menu.",
55
"main": "index.js",
66
"scripts": {

readme.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
=== Super Admin All Sites Menu ===
2-
Stable tag: 1.7.3
2+
Stable tag: 1.8.0
33
Requires at least: 5.6
44
Tested up to: 6.7
5-
Requires PHP: 7.3
5+
Requires PHP: 8.0
66
License: GPL v2 or later
77
Tags: superadmin, multisite, management
88
Contributors: PerS
@@ -108,6 +108,15 @@ You can use the following filters to override the defaults:
108108

109109
== Changelog ==
110110

111+
= 1.8.0 =
112+
113+
REQUIRE PHP 8.0 OR HIGHER
114+
115+
* Code modernization: Refactored to use PHP 8.0+ features
116+
* Enhancement: Improved type safety and error handling
117+
* Performance: Better code organization with Config class
118+
* Enhancement: Increased default search threshold to 20 sites
119+
111120
= 1.7.3 =
112121
* Fixed search functionality:
113122
* Improved search performance with better indexing

super-admin-all-sites-menu.php

Lines changed: 58 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Plugin URI: https://github.com/soderlind/super-admin-all-sites-menu
1313
* GitHub Plugin URI: https://github.com/soderlind/super-admin-all-sites-menu
1414
* 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
1616
* Author: Per Soderlind
1717
* Network: true
1818
* Author URI: https://soderlind.no
@@ -27,79 +27,56 @@
2727
if ( ! defined( 'ABSPATH' ) ) {
2828
wp_die();
2929
}
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;
6630

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+
}
6742

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+
}
8161

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();
9364
}
9465

95-
/**
96-
* Initialize the plugin.
97-
*/
98-
public function init() {
66+
private function register_hooks(): void {
9967
add_action( 'admin_bar_init', [ $this, 'action_admin_bar_init' ] );
10068
add_action( 'rest_api_init', [ $this, 'action_rest_api_init' ] );
10169
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' ] );
10380
}
10481

10582
/**
@@ -134,28 +111,28 @@ public function action_admin_bar_init(): void {
134111
* @return void
135112
*/
136113
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 );
138115
if ( ! is_array( $this->plugins ) ) {
139-
$this->plugins = PLUGINS;
116+
$this->plugins = Config::PLUGINS;
140117
}
141118

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 );
143120
if ( ! in_array( $this->order_by, [ 'name', 'url', 'id' ], true ) ) {
144-
$this->order_by = ORDERBY;
121+
$this->order_by = Config::ORDER_BY;
145122
}
146123

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 );
148125
if ( ! is_numeric( $this->load_increments ) || $this->load_increments < 1 ) {
149-
$this->load_increments = LOADINCREMENTS;
126+
$this->load_increments = Config::LOAD_INCREMENTS;
150127
}
151128

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 );
153130
if ( ! is_numeric( $this->search_threshold ) || $this->search_threshold < 1 ) {
154-
$this->search_threshold = SEARCHTHRESHOLD;
131+
$this->search_threshold = Config::SEARCH_THRESHOLD;
155132
}
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 );
157134
if ( ! is_numeric( $this->search_threshold ) || $this->cache_expiration < 0 ) {
158-
$this->cache_expiration = CACHE_EXPIRATION;
135+
$this->cache_expiration = Config::CACHE_EXPIRATION;
159136
}
160137
}
161138

@@ -312,8 +289,8 @@ public function super_admin_all_sites_menu( \WP_Admin_Bar $wp_admin_bar ): void
312289
*/
313290
public function action_rest_api_init( \WP_REST_Server $wp_rest_server ): void {
314291
$is_route_created = register_rest_route(
315-
REST_NAMESPACE,
316-
'/' . REST_BASE,
292+
Config::REST_NAMESPACE,
293+
'/' . Config::REST_BASE,
317294
[
318295
'methods' => \WP_REST_Server::CREATABLE,
319296
'callback' => [ $this, 'get_sites' ],
@@ -430,7 +407,7 @@ public function action_enqueue_scripts( string $hook_suffix ): void {
430407
$data = wp_json_encode(
431408
[
432409
'nonce' => wp_create_nonce( 'wp_rest' ),
433-
'restURL' => rest_url() . REST_ENDPOINT,
410+
'restURL' => rest_url() . Config::REST_ENDPOINT,
434411
'loadincrements' => $this->load_increments,
435412
'orderBy' => $this->order_by,
436413
'displaySearch' => ( $this->number_of_sites > $this->search_threshold ) ? true : false,
@@ -540,7 +517,5 @@ private function remove_timestamp(): void {
540517

541518
}
542519

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

Comments
 (0)