forked from scribu/wp-proper-network-activation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proper-network-activation.php
187 lines (142 loc) · 4.38 KB
/
proper-network-activation.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
<?php
/*
Plugin Name: Proper Network Activation
Plugin URI: http://scribu.net/wordpress/proper-network-activation
Version: 1.0.5
Description: Use the network activation feature of WP MultiSite without problems
Author: scribu
Author URI: http://scribu.net/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Network: true
Text Domain: proper-network-activation
Domain Path: /languages
*/
class Proper_Network_Activation {
const AJAX_KEY = 'pna';
static function init() {
add_action( 'activated_plugin', array( __CLASS__, 'update_queue' ), 10, 2 );
add_action( 'deactivated_plugin', array( __CLASS__, 'update_queue' ), 10, 2 );
add_action( 'network_admin_notices', array( __CLASS__, 'admin_notices' ) );
load_plugin_textdomain( 'proper-network-activation', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
add_action( 'wp_ajax_' . self::AJAX_KEY, array( __CLASS__, 'ajax_response' ) );
add_action( 'wpmu_new_blog', array( __CLASS__, 'setup' ) );
}
static function update_queue( $plugin, $network_wide = null ) {
if ( !$network_wide )
return;
list( $action ) = explode( '_', current_filter(), 2 );
$action = str_replace( 'activated', 'activate', $action );
$queue = get_site_option( "network_{$action}_queue", array() );
$queue[$plugin] = ( has_filter( $action . '_' . $plugin ) || has_filter( $action . '_plugin' ) );
update_site_option( "network_{$action}_queue", $queue );
}
static function admin_notices() {
if ( 'plugins-network' != get_current_screen()->id )
return;
$action = false;
foreach ( array( 'activate', 'deactivate' ) as $key ) {
if ( isset( $_REQUEST[ $key ] ) || isset( $_REQUEST[ $key . '-multi' ] ) ) {
$action = $key;
break;
}
}
if ( !$action )
return;
$queue = get_site_option( "network_{$action}_queue", array() );
if ( empty( $queue ) )
return;
if ( !in_array( true, $queue ) ) {
delete_site_option( "network_{$action}_queue" );
echo '<div class="updated"><p>' . __( 'Network (de)activation: no further action necessary.', 'proper-network-activation' ) . '</p></div>';
return;
}
$total = get_blog_count();
$messages = array(
'activate' => __( 'Network activation: installed on %s / %s sites.', 'proper-network-activation' ),
'deactivate' => __( 'Network deactivation: uninstalled on %s / %s sites.', 'proper-network-activation' ),
);
$message = sprintf( $messages[ $action ],
"<span id='pna-count-current'>0</span>",
"<span id='pna-count-total'>$total</span>"
);
echo "<div class='updated'><p id='pna'>$message</p></div>";
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var
_action = '<?php echo $action; ?>',
total = <?php echo $total; ?>,
offset = 0,
count = 5;
var $display = $('#pna-count-current');
function done() {
var data = {
action: 'pna',
_action: _action,
done: 1
}
$.post(ajaxurl, data, jQuery.noop);
}
function call_again() {
var data = {
action: 'pna',
_action: _action,
offset: offset
}
if ( offset > total ) {
done();
$display.html(total);
return;
}
$.post(ajaxurl, data, function(response) {
$display.html(offset);
offset += count;
call_again();
});
}
call_again();
});
</script>
<?php
}
static function ajax_response() {
$action = $_POST['_action'];
if ( isset( $_POST['done'] ) ) {
delete_site_option( "network_{$action}_queue" );
die(1);
}
$offset = (int) $_POST['offset'];
$queue = get_site_option( "network_{$action}_queue", array() );
global $wpdb;
$blogs = $wpdb->get_col( $wpdb->prepare( "
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = %d
AND blog_id <> %d
AND spam = '0'
AND deleted = '0'
AND archived = '0'
ORDER BY registered DESC
LIMIT %d, 5
", $wpdb->siteid, $wpdb->blogid, $offset ) );
foreach ( $blogs as $blog_id ) {
switch_to_blog( $blog_id );
foreach ( $queue as $plugin => $actionable ) {
self::do_action( $action, $plugin );
}
}
die(1);
}
static function setup( $blog_id ) {
switch_to_blog( $blog_id );
foreach ( array_keys( get_site_option( 'active_sitewide_plugins' ) ) as $plugin )
self::do_action( 'activate', $plugin );
restore_current_blog();
}
private static function do_action( $action, $plugin ) {
do_action( $action . '_' . $plugin, false );
do_action( $action . '_plugin', $plugin, false );
}
}
Proper_Network_Activation::init();