-
Notifications
You must be signed in to change notification settings - Fork 2
/
activate.php
143 lines (130 loc) · 3.52 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
<?php
defined( 'WPINC' ) || die;
/**
* Check for minimum system requirments on plugin activation
* @version 3.0.0
*/
class GL_Plugin_Check_v3
{
const MIN_PHP_VERSION = '5.6.0';
const MIN_WORDPRESS_VERSION = '4.7.0';
/**
* @var string
*/
protected $file;
/**
* @var array
*/
protected $versions;
/**
* @param string $file
*/
public function __construct( $file, array $versions = array() )
{
$this->file = realpath( $file );
$this->versions = wp_parse_args( $versions, array(
'php' => static::MIN_PHP_VERSION,
'wordpress' => static::MIN_WORDPRESS_VERSION,
));
}
/**
* @return bool
*/
public function canProceed()
{
if( $this->isValid() ) {
return true;
}
add_action( 'activated_plugin', array( $this, 'deactivate' ));
add_action( 'admin_notices', array( $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, array( 'name' => 'Plugin Name' ), 'plugin' );
deactivate_plugins( $pluginSlug );
$this->printNotice( $pluginData['name'] );
}
/**
* @return array
*/
protected function getMessages()
{
return array(
__( 'The %s plugin was deactivated.', 'pollux' ),
__( 'This plugin requires %s or greater in order to work properly.', 'pollux' ),
__( '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.', 'pollux' ),
__( 'PHP version', 'pollux' ),
__( 'WordPress version', 'pollux' ),
__( 'Update WordPress', 'pollux' ),
__( 'You can use the %s plugin to restore %s to the previous version.', 'pollux' ),
);
}
/**
* @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[6].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/">WP Rollback</a>', $pluginName );
if( !$this->isPhpValid() ) {
printf( $noticeTemplate,
sprintf( $messages[0], $pluginName ),
sprintf( $messages[1], $messages[3].' '.$this->versions['php'] ),
sprintf( $messages[2], PHP_VERSION ).'</p><p>'.$rollbackMessage
);
}
else if( !$this->isWpValid() ) {
printf( $noticeTemplate,
sprintf( $messages[0], $pluginName ),
sprintf( $messages[1], $messages[4].' '.$this->versions['wordpress'] ),
$rollbackMessage.'</p><p>'.sprintf( '<a href="%s">%s</a>', admin_url( 'update-core.php' ), $messages[5] )
);
}
}
/**
* @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;
}
}