-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivate.php
148 lines (135 loc) · 4.51 KB
/
activate.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
<?php
defined('ABSPATH') || exit;
/**
* Check for minimum system requirements on plugin activation.
* @version 6.0.0
*/
class GL_Plugin_Check_v6
{
const MIN_PHP_VERSION = '7.2';
const MIN_WORDPRESS_VERSION = '5.8';
/**
* @var array
*/
public $versions;
/**
* @var string
*/
protected $file;
/**
* @param string $file
*/
public function __construct($file)
{
$this->file = realpath($file);
$versionRequirements = get_file_data($this->file, [
'php' => 'Requires PHP',
'wordpress' => 'Requires at least',
]);
$this->versions = wp_parse_args(array_filter($versionRequirements), [
'php' => static::MIN_PHP_VERSION,
'wordpress' => static::MIN_WORDPRESS_VERSION,
]);
}
/**
* @return bool
*/
public function canProceed()
{
if ($this->isValid()) {
return true;
}
add_action('activated_plugin', [$this, 'deactivate']);
add_action('admin_notices', [$this, 'deactivate']);
return false;
}
/**
* @return bool
*/
public function isPhpValid()
{
return version_compare(PHP_VERSION, $this->versions['php'], '>=');
}
/**
* @return bool
*/
public function isValid()
{
return $this->isPhpValid() && $this->isWpValid();
}
/**
* @return bool
*/
public function isWpValid()
{
global $wp_version;
return version_compare($wp_version, $this->versions['wordpress'], '>=');
}
/**
* @param string $plugin
* @return void
*/
public function deactivate($plugin)
{
if ($this->isValid()) {
return;
}
$pluginSlug = plugin_basename($this->file);
if ($plugin == $pluginSlug) {
$this->redirect(); // exit
}
$pluginData = get_file_data($this->file, ['name' => 'Plugin Name'], 'plugin');
deactivate_plugins($pluginSlug);
$this->printNotice($pluginData['name']);
}
/**
* @return array
*/
protected function getMessages()
{
return [
'notice' => _x('The %s plugin was deactivated.', 'admin-text', 'blackbar'),
'php_version' => _x('PHP version', 'admin-text', 'blackbar'),
'rollback' => _x('You can use the %s plugin to restore %s to the previous version.', 'admin-text', 'blackbar'),
'update_php' => _x('Please contact your hosting provider or server administrator to upgrade the version of PHP on your server (your server is running PHP version %s), or try to find an alternative plugin.', 'admin-text', 'blackbar'),
'update_wp' => _x('Update WordPress', 'admin-text', 'blackbar'),
'wp_version' => _x('WordPress version', 'admin-text', 'blackbar'),
'wrong_version' => _x('This plugin requires %s or greater in order to work properly.', 'admin-text', 'blackbar'),
];
}
/**
* @param string $pluginName
* @return void
*/
protected function printNotice($pluginName)
{
$noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>';
$messages = $this->getMessages();
$rollbackMessage = sprintf('<strong>'.$messages['rollback'].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/" target="_blank">WP Rollback</a>', $pluginName);
if (!$this->isPhpValid()) {
printf($noticeTemplate,
sprintf($messages['notice'], $pluginName),
sprintf($messages['wrong_version'], $messages['php_version'].' '.$this->versions['php']),
sprintf($messages['update_php'], PHP_VERSION).'</p><p>'.$rollbackMessage
);
} elseif (!$this->isWpValid()) {
printf($noticeTemplate,
sprintf($messages['notice'], $pluginName),
sprintf($messages['wrong_version'], $messages['wp_version'].' '.$this->versions['wordpress']),
$rollbackMessage.'</p><p>'.sprintf('<a href="%s">%s</a>', admin_url('update-core.php'), $messages['update_wp'])
);
}
}
/**
* @return void
*/
protected function redirect()
{
wp_safe_redirect(self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s',
filter_input(INPUT_GET, 'plugin_status'),
filter_input(INPUT_GET, 'paged'),
filter_input(INPUT_GET, 's')
)));
exit;
}
}