-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompatibility.php
More file actions
205 lines (165 loc) · 3.93 KB
/
compatibility.php
File metadata and controls
205 lines (165 loc) · 3.93 KB
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
<?php
/**
* Check WP and PHP compatibility
*
* @codingStandardsIgnoreFile
*
* @package Gutenberg Posts Block
* @author Adrian P
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Compatibility class
*
* @class GPB_Compatibility
* @since 1.0.0
*/
class GPB_Compatibility {
/**
* Minium required WordPress version
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $wp_min;
/**
* Minium required PHP version
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $php_min;
/**
* Holds plugin name
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $plugin;
/**
* Holds plugin basename
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $basename;
/**
* Holds notice messages
*
* @since 1.0.0
* @access private
*
* @var array
*/
private $notices = array();
/**
* Constructor
*
* @since 1.0.0
* @access public
*
* @param string $wp_min Minium required WordPress version.
* @param string $php_min Minium required PHP version.
* @param string $plugin Plugin name.
* @param string $basename Plugin basename.
*/
public function __construct( $wp_min = '3.7', $php_min = '5.2.4', $plugin = 'Plugin', $basename = '' ) {
$this->wp_min = $wp_min;
$this->php_min = $php_min;
$this->plugin = $plugin;
$this->basename = $basename;
}
/**
* Check if it's compatible
*
* @since 1.0.0
* @access public
*
* @return boolean
*/
public function check() {
$this->compare_versions();
if ( empty( $this->notices ) ) {
return true;
}
add_action( 'admin_init', array( $this, 'add_notices' ) );
return false;
}
/**
* Compare compatibility versions.
*
* @since 1.0.0
* @access private
*/
private function compare_versions() {
global $wp_version;
// Check WordPress version compatibility.
if ( version_compare( $wp_version, $this->wp_min, '<' ) ) {
$this->notices[] = sprintf(
/* translators: %1$s: Plugin name, %2$s: Minium required WordPress version */
__( "<strong>%1\$s</strong> requires at least <code>WordPress %2\$s</code>.\nPlease update WordPress to activate the plugin.", 'gutenberg-posts-block' ),
$this->plugin,
$this->wp_min
);
}
// Check PHP version compatibility.
if ( version_compare( PHP_VERSION, $this->php_min, '<' ) ) {
$this->notices[] = sprintf(
/* translators: %1$s: Plugin name, %2$s: Minium required PHP version */
__( "<strong>%1\$s</strong> requires at least <code>PHP %2\$s</code>.\nPlease contact your hosting provider to upgrade your PHP version.", 'gutenberg-posts-block' ),
$this->plugin,
$this->php_min
);
}
// Check if plugin was built with npm.
if ( ! file_exists( dirname( __FILE__ ) . '/assets' ) ) {
$this->notices[] = sprintf(
/* translators: %1$s: Plugin name */
__( "%1\$s plugin requires files to be built.\nRun <code>npm install</code> to install dependencies\n<code>npm run build</code> to build the files or <code>npm run dev</code> to build the files and watch for changes.", 'gutenberg-posts-block' ),
$this->plugin,
$this->php_min
);
}
}
/**
* Add admin notices.
*
* @since 1.0.0
* @access public
*/
public function add_notices() {
// To take into account translations after localization.
$this->notices = array();
$this->compare_versions();
if ( ! empty( $this->basename ) ) {
// Suppress "Plugin activated" notice.
unset( $_GET['activate'] );
// Deactivate plugin.
deactivate_plugins( $this->basename );
}
add_action( 'admin_notices', array( $this, 'display_notices' ) );
}
/**
* Display admin notices.
*
* @since 1.0.0
* @access public
*/
public function display_notices() {
array_map( function( $notice ) {
echo '<div class="error">' . wp_kses_post( wpautop( $notice ) ) . '</div>';
}, $this->notices );
}
}
return new GPB_Compatibility( '4.7.0', '5.4.0', GPB_NAME, GPB_BASE );