-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-global-admin.php
217 lines (193 loc) · 6.21 KB
/
wp-global-admin.php
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
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Plugin initialization file
*
* @package WPGlobalAdmin
* @since 1.0.0
*
* @wordpress-plugin
* Plugin Name: WP Global Admin
* Plugin URI: https://github.com/felixarntz/wp-global-admin
* Description: Introduces a global admin panel in WordPress.
* Version: 1.0.0-beta.1
* Author: Felix Arntz
* Author URI: https://leaves-and-love.net
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-global-admin
* Domain Path: /languages/
* Network: true
* Tags: global admin, network, multisite, multinetwork
*/
/**
* Loads the plugin textdomain.
*
* @since 1.0.0
*/
function ga_load_textdomain() {
load_plugin_textdomain( 'wp-global-admin', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Initializes the plugin.
*
* Loads the required files, registers the new DB table, global cache groups and global capabilities.
*
* @since 1.0.0
*/
function ga_init() {
if ( function_exists( 'go_init' ) ) {
require_once GA_PATH . 'wp-global-admin/wp-includes/load.php';
require_once GA_PATH . 'wp-global-admin/wp-includes/capabilities.php';
require_once GA_PATH . 'wp-global-admin/wp-includes/link-template.php';
require_once GA_PATH . 'wp-global-admin/wp-includes/admin-bar.php';
require_once GA_PATH . 'wp-global-admin/wp-includes/ms-functions.php';
require_once GA_PATH . 'wp-global-admin/wp-includes/ms-default-filters.php';
require_once GA_PATH . 'wp-global-admin/wp-includes/setup.php';
if ( is_admin() ) {
require_once GA_PATH . 'wp-global-admin/wp-admin/includes/hacks.php';
}
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( is_plugin_active( 'wp-network-roles/wp-network-roles.php' ) ) {
require_once GA_PATH . 'wp-global-admin/network-roles-compat.php';
}
if ( is_plugin_active( 'wp-multi-network/wpmn-loader.php' ) ) {
require_once GA_PATH . 'wp-global-admin/multi-network-compat.php';
}
if ( is_plugin_active( 'wp-user-signups/wp-user-signups.php' ) ) {
require_once GA_PATH . 'wp-global-admin/user-signups-compat.php';
}
} else {
add_action( 'admin_notices', 'ga_requirements_notice_plugins' );
add_action( 'network_admin_notices', 'ga_requirements_notice_plugins' );
}
}
/**
* Shows an admin notice if the WordPress version installed is not supported.
*
* @since 1.0.0
*/
function ga_requirements_notice() {
$plugin_file = plugin_basename( __FILE__ );
if ( ! current_user_can( 'deactivate_plugin', $plugin_file ) ) {
return;
}
?>
<div class="notice notice-warning is-dismissible">
<p>
<?php
printf(
/* translators: %s: URL to deactivate plugin */
__( 'Please note: WP Global Admin requires WordPress 4.9 or higher. <a href="%s">Deactivate plugin</a>.', 'wp-global-admin' ),
wp_nonce_url(
add_query_arg(
array(
'action' => 'deactivate',
'plugin' => $plugin_file,
'plugin_status' => 'all',
),
network_admin_url( 'plugins.php' )
),
'deactivate-plugin_' . $plugin_file
)
);
?>
</p>
</div>
<?php
}
/**
* Shows an admin notice if the WP Global Options plugin is not installed and activated.
*
* @since 1.0.0
*/
function ga_requirements_notice_plugins() {
$plugin_file = plugin_basename( __FILE__ );
if ( ! current_user_can( 'install_plugins' ) ) {
return;
}
?>
<div class="notice notice-warning is-dismissible">
<p>
<?php
printf(
/* translators: %s: URL to deactivate plugin */
__( 'Please note: WP Global Admin requires the WP Global Options plugin to be installed and activated. <a href="%s">Install plugin</a>.', 'wp-global-admin' ),
wp_nonce_url(
add_query_arg(
array(
'action' => 'install-plugin',
'plugin' => 'wp-global-options',
),
network_admin_url( 'update.php' )
),
'install-plugin_wp-global-options'
)
);
?>
</p>
</div>
<?php
}
/**
* Ensures that this plugin gets activated in every new network by filtering the `active_sitewide_plugins` option.
*
* @since 1.0.0
*
* @param array $network_options All network options for the new network.
* @return array Modified network options including the plugin.
*/
function ga_activate_on_new_network( $network_options ) {
$plugin_file = plugin_basename( __FILE__ );
if ( ! isset( $network_options['active_sitewide_plugins'][ $plugin_file ] ) ) {
$network_options['active_sitewide_plugins'][ $plugin_file ] = time();
}
return $network_options;
}
/**
* Ensures that this plugin gets activated on a request to update the network-active plugins.
*
* @since 1.0.0
*
* @param array $plugins Associative array of `$plugin_basename => $time` pairs.
* @return array Modified array.
*/
function ga_activate_on_update_request( $plugins ) {
$network_options = ga_activate_on_new_network( array(
'active_sitewide_plugins' => $plugins,
) );
remove_filter( 'pre_update_site_option_active_sitewide_plugins', 'ga_activate_on_update_request' );
return $network_options['active_sitewide_plugins'];
}
/**
* Adds the hook to ensure that this plugin gets activated in every network created by WP Multi Network.
*
* @since 1.0.0
*/
function ga_activate_on_new_wpmn_network_add_hook() {
add_filter( 'pre_update_site_option_active_sitewide_plugins', 'ga_activate_on_update_request' );
}
/**
* Hooks in plugin initialization functionality.
*
* @since 1.0.0
*/
function ga_add_hooks() {
$file = wp_normalize_path( __FILE__ );
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
$is_mu_plugin = (bool) preg_match( '#^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', $file );
$plugin_hook = $is_mu_plugin ? 'muplugins_loaded' : 'plugins_loaded';
add_action( $plugin_hook, 'ga_load_textdomain', 1 );
if ( version_compare( $GLOBALS['wp_version'], '4.9', '<' ) ) {
add_action( 'admin_notices', 'ga_requirements_notice' );
add_action( 'network_admin_notices', 'ga_requirements_notice' );
return;
}
define( 'GA_PATH', plugin_dir_path( __FILE__ ) );
define( 'GA_URL', plugin_dir_url( __FILE__ ) );
add_action( $plugin_hook, 'ga_init' );
if ( ! $is_mu_plugin ) {
add_filter( 'populate_network_meta', 'ga_activate_on_new_network', 10, 1 );
add_action( 'add_network', 'ga_activate_on_new_wpmn_network_add_hook', 10, 0 );
}
}
ga_add_hooks();