-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Activation.php
134 lines (100 loc) · 2.74 KB
/
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
<?php declare(strict_types = 1);
/**
* Plugin activation handler.
*
* @package query-monitor
*/
class QM_Activation extends QM_Plugin {
/**
* @param string $file
*/
protected function __construct( $file ) {
# Filters
add_filter( 'pre_update_option_active_plugins', array( $this, 'filter_active_plugins' ) );
add_filter( 'pre_update_site_option_active_sitewide_plugins', array( $this, 'filter_active_sitewide_plugins' ) );
# Activation and deactivation
register_activation_hook( $file, array( $this, 'activate' ) );
register_deactivation_hook( $file, array( $this, 'deactivate' ) );
# Parent setup:
parent::__construct( $file );
}
/**
* @param bool $sitewide
* @return void
*/
public function activate( $sitewide = false ) {
$db = WP_CONTENT_DIR . '/db.php';
$create_symlink = defined( 'QM_DB_SYMLINK' ) ? QM_DB_SYMLINK : true;
if ( $create_symlink && ! file_exists( $db ) && function_exists( 'symlink' ) ) {
@symlink( $this->plugin_path( 'wp-content/db.php' ), $db ); // phpcs:ignore
}
if ( $sitewide ) {
update_site_option( 'active_sitewide_plugins', get_site_option( 'active_sitewide_plugins' ) );
} else {
update_option( 'active_plugins', get_option( 'active_plugins' ) );
}
}
/**
* @return void
*/
public function deactivate() {
$admins = QM_Util::get_admins();
// Remove legacy capability handling:
if ( $admins ) {
$admins->remove_cap( 'view_query_monitor' );
}
# Only delete db.php if it belongs to Query Monitor
if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && class_exists( 'QM_DB', false ) ) {
unlink( WP_CONTENT_DIR . '/db.php' ); // phpcs:ignore
}
}
/**
* @param array<int, string> $plugins
* @return array<int, string>
*/
public function filter_active_plugins( $plugins ) {
// this needs to run on the cli too
if ( empty( $plugins ) ) {
return $plugins;
}
$f = preg_quote( basename( $this->plugin_base() ), '/' );
$qm = preg_grep( '/' . $f . '$/', $plugins );
$notqm = preg_grep( '/' . $f . '$/', $plugins, PREG_GREP_INVERT );
if ( false === $qm || false === $notqm ) {
return $plugins;
}
return array_merge(
$qm,
$notqm
);
}
/**
* @param array<string, int> $plugins
* @return array<string, int>
*/
public function filter_active_sitewide_plugins( $plugins ) {
if ( empty( $plugins ) ) {
return $plugins;
}
$f = $this->plugin_base();
if ( isset( $plugins[ $f ] ) ) {
unset( $plugins[ $f ] );
return array_merge( array(
$f => time(),
), $plugins );
} else {
return $plugins;
}
}
/**
* @param string $file
* @return self
*/
public static function init( $file ) {
static $instance = null;
if ( ! $instance ) {
$instance = new QM_Activation( $file );
}
return $instance;
}
}