-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiconic-tpd.php
246 lines (198 loc) · 6.05 KB
/
iconic-tpd.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* Plugin Name: Temporarily Disable Plugins by Iconic
* Plugin URI: https://iconicwp.com/
* Description: A "must-use" plugin which allows you to temporarily disable plugins via the admin bar.
* Version: 1.0.0
* Author: Iconic
* Author URI: https://iconicwp.com
* Text Domain: iconic-tdp
*/
/**
* Deactivate plugins temporarily.
*
* @param array $active_plugins
* @param string $option_name
*
* @return mixed
*/
function iconic_tpd_option_active_plugins( $active_plugins, $option_name ) {
if ( ! iconic_tpd_is_enabled() ) {
return $active_plugins;
}
static $new_active_plugins = null;
if ( ! is_null( $new_active_plugins ) ) {
// Return normal list of active plugins after
// first time, to avoid conflicts.
return $active_plugins;
}
$to_remove = iconic_tpd_get_disabled_plugins();
$new_active_plugins = $active_plugins;
foreach ( $active_plugins as $key => $value ) {
if ( in_array( $value, $to_remove, true ) ) {
unset( $new_active_plugins[ $key ] );
}
}
return $new_active_plugins;
}
add_filter( 'option_active_plugins', 'iconic_tpd_option_active_plugins', 999, 2 );
/**
* Add versions to admin bar.
*/
function iconic_tpd_active_plugins_menu() {
if ( ! iconic_tpd_is_enabled() ) {
return;
}
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
global $wp_admin_bar, $wp_version;
$menu_id = 'iconic-active-plugins';
$plugins = get_plugins();
$active_plugins = get_option( 'active_plugins' );
$disabled_plugins = iconic_tpd_get_disabled_plugins();
$unfiltered_active_plugins = iconic_tpd_get_unfiltered_active_plugins( $active_plugins );
$wp_admin_bar->add_menu(
array(
'id' => $menu_id,
'title' => __( 'Plugins', 'iconic-tdp' ),
'href' => '',
)
);
foreach ( $plugins as $path => $plugin ) {
if ( ! in_array( $path, $unfiltered_active_plugins, true ) ) {
continue;
}
$is_active = ! in_array( $path, $disabled_plugins, true );
$id = sprintf( 'iconic-tpd-%s', sanitize_title( $plugin['Name'] ) );
$title = $is_active ? sprintf( '<span>%s</span>', $plugin['Name'] ) : sprintf( '<del>%s</del>', $plugin['Name'] );
$href = iconic_tpd_get_action_url( $path, $is_active ? 'disable' : 'enable' );
$wp_admin_bar->add_menu(
array(
'parent' => $menu_id,
'title' => $title,
'id' => $id,
'href' => $href,
)
);
}
}
add_action( 'admin_bar_menu', 'iconic_tpd_active_plugins_menu', 5000 );
/**
* Get action URL.
*
* @param string $plugin_path
* @param string $type
*
* @return string
*/
function iconic_tpd_get_action_url( $plugin_path, $type ) {
$original_url = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ? "https" : "http" ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = $original_url;
$url = add_query_arg( 'url', $original_url, $url );
$url = add_query_arg( 'action', 'iconic-tpd-set', $url );
$url = add_query_arg( 'plugin_path', $plugin_path, $url );
$url = add_query_arg( 'type', $type, $url );
return add_query_arg( '_wpnonce', wp_create_nonce( 'iconic-tpd' ), $url );
}
/**
* Get unfiltered_active_plugins.
*
* @return mixed|void
*/
function iconic_tpd_get_unfiltered_active_plugins( $active_plugins = array() ) {
global $wpdb;
static $active_plugins = array();
if ( ! empty( $active_plugins ) ) {
return $active_plugins;
}
$active_plugins_var = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'active_plugins'" );
if ( empty( $active_plugins_var ) || is_wp_error( $active_plugins_var ) ) {
return $active_plugins;
}
$active_plugins = maybe_unserialize( $active_plugins_var );
return (array) $active_plugins;
}
/**
* Add scroll to admin menu.
*/
function iconic_tpd_header_styles() {
if ( ! iconic_tpd_is_enabled() ) {
return;
}
?>
<style type="text/css">
.ab-sub-wrapper {
max-height: 402px;
overflow-y: scroll;
}
#wp-admin-bar-iconic-active-plugins .ab-sub-wrapper a:hover span {
text-decoration: line-through;
}
#wp-admin-bar-iconic-active-plugins .ab-sub-wrapper a del {
text-decoration: line-through;
opacity: 0.5;
}
#wp-admin-bar-iconic-active-plugins .ab-sub-wrapper a:hover del {
text-decoration: none;
opacity: 1;
}
</style>
<?php
}
add_action( 'wp_head', 'iconic_tpd_header_styles' );
add_action( 'admin_head', 'iconic_tpd_header_styles' );
/**
* Process action URL.
*/
function iconic_tpd_process_action() {
if ( ! iconic_tpd_is_enabled() ) {
return;
}
$action = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_STRING );
if ( 'iconic-tpd-set' !== $action ) {
return;
}
$nonce = filter_input( INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING );
if ( ! wp_verify_nonce( $nonce, 'iconic-tpd' ) ) {
die( __( 'Nope!', 'iconic-tdp' ) );
}
$url = filter_input( INPUT_GET, 'url', FILTER_SANITIZE_STRING );
$plugin_path = filter_input( INPUT_GET, 'plugin_path', FILTER_SANITIZE_STRING );
$type = filter_input( INPUT_GET, 'type', FILTER_SANITIZE_STRING );
$temporary_deactivations = iconic_tpd_get_disabled_plugins();
if ( 'enable' === $type && ! empty( $temporary_deactivations ) ) {
foreach ( $temporary_deactivations as $key => $value ) {
if ( $plugin_path !== $value ) {
continue;
}
unset( $temporary_deactivations[ $key ] );
}
} elseif ( 'disable' === $type ) {
$temporary_deactivations[] = $plugin_path;
}
update_option( 'iconic_tpd', $temporary_deactivations );
wp_safe_redirect( $url );
exit;
}
add_action( 'template_redirect', 'iconic_tpd_process_action' );
add_action( 'admin_init', 'iconic_tpd_process_action' );
/**
* Get disabled_plugins.
*
* @return array
*/
function iconic_tpd_get_disabled_plugins() {
return array_filter( get_option( 'iconic_tpd', array() ) );
}
/**
* Check if TPD should be enabled.
*
* @return bool
*/
function iconic_tpd_is_enabled() {
if ( ! function_exists( 'wp_get_current_user' ) ) {
include ABSPATH . 'wp-includes/pluggable.php';
}
return current_user_can( 'administrator' ) && strpos( $_SERVER['REQUEST_URI'], '/wp-admin/plugins.php' ) === false;
}