Dependencies.php
5.37 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace AC;
/**
* Show a notice when plugin dependencies are not met
* @version 1.5
*/
final class Dependencies {
const ACP_PLUGIN = 'Admin Columns Pro';
/**
* Basename of this plugin
* @var string
*/
private $basename;
/**
* @var string
*/
private $version;
/**
* Missing dependency messages
* @var string[]
*/
private $messages = [];
/**
* @param string $basename
* @param string $version
*/
public function __construct( $basename, $version ) {
$this->basename = $basename;
$this->version = $version;
}
/**
* @return string
*/
public function get_basename() {
return $this->basename;
}
/**
* @return string
*/
public function get_version() {
return $this->version;
}
/**
* Register hooks
*/
private function register() {
add_action( 'after_plugin_row_' . $this->basename, [ $this, 'display_notice' ], 5 );
add_action( 'admin_head', [ $this, 'display_notice_css' ] );
}
/**
* Add missing dependency
*
* @param string $message
* @param string $key
*/
public function add_missing( $message, $key ) {
if ( ! $this->has_missing() ) {
$this->register();
}
$this->messages[ $key ] = $this->sanitize_message( $message );
}
/**
* Add missing dependency
*
* @param string $plugin
* @param string $url
* @param string $version
*/
public function add_missing_plugin( $plugin, $url = null, $version = null ) {
$this->add_missing(
$this->get_missing_plugin_message( $plugin, $url, $version ),
$plugin
);
}
/**
* @param string $plugin
* @param null $url
* @param null $version
*
* @return string
*/
private function get_missing_plugin_message( $plugin, $url = null, $version = null ) {
$plugin = esc_html( $plugin );
if ( $url ) {
$plugin = sprintf( '<a href="%s">%s</a>', esc_url( $url ), $plugin );
}
if ( $version ) {
$plugin .= ' ' . sprintf( 'version %s+', esc_html( $version ) );
}
return sprintf( '%s needs to be installed and activated.', $plugin );
}
/**
* @return bool
*/
public function has_missing() {
return ! empty( $this->messages );
}
/**
* @param string $message
*
* @return string
*/
private function sanitize_message( $message ) {
return wp_kses( $message, [
'a' => [
'href' => true,
'target' => true,
],
] );
}
/**
* @return string
*/
private function get_download_acp_message() {
return sprintf(
'Download the latest version from <a target="_blank" href="%s">your account.</a>',
esc_url( 'https://www.admincolumns.com/my-account' )
);
}
/**
* Check if Admin Columns Pro is installed
*
* @param $version
*
* @return bool
*/
public function requires_acp( $version ) {
if ( ! function_exists( 'ACP' ) ) {
$this->add_missing(
$this->get_missing_plugin_message( self::ACP_PLUGIN ) . ' ' . $this->get_download_acp_message(),
self::ACP_PLUGIN
);
return false;
}
if ( ! ACP()->is_version_gte( $version ) || ! acp_is_addon_compatible( __NAMESPACE__, $this->version ) ) {
$message = sprintf(
'this plugin is not compatible with the current version of %1$s. Make sure this plugin and %1$s are updated to the most recent version.',
self::ACP_PLUGIN
);
$this->add_missing( $message, self::ACP_PLUGIN );
return false;
}
return true;
}
/**
* Check current PHP version
*
* @param string $version
*
* @return bool
*/
public function requires_php( $version ) {
if ( ! version_compare( PHP_VERSION, $version, '>=' ) ) {
$message = sprintf(
'PHP %s+ is required. Your server currently runs PHP %s. <a href="%s" target="_blank">Learn more about requirements.</a>',
$version,
PHP_VERSION,
esc_url( 'https://www.admincolumns.com/documentation/getting-started/requirements/' )
);
$this->add_missing( $message, 'PHP Version' );
return false;
}
return true;
}
/**
* URL that performs a search in the WordPress repository
*
* @param string $keywords
*
* @return string
*/
public function get_search_url( $keywords ) {
$url = add_query_arg( [
'tab' => 'search',
's' => $keywords,
], admin_url( 'plugin-install.php' ) );
return $url;
}
/**
* @return bool
*/
private function is_plugin_active() {
return is_multisite() && is_network_admin()
? is_plugin_active_for_network( $this->basename )
: is_plugin_active( $this->basename );
}
/**
* Show a warning when dependencies are not met
*/
public function display_notice() {
$intro = "This plugin can't load because";
?>
<tr class="plugin-update-tr <?= $this->is_plugin_active() ? 'active' : 'inactive'; ?>">
<td colspan="3" class="plugin-update colspanchange">
<div class="update-message notice inline notice-error notice-alt">
<?php if ( count( $this->messages ) > 1 ) : ?>
<p>
<?php echo $intro . ':' ?>
</p>
<ul>
<?php foreach ( $this->messages as $message ) : ?>
<li><?php echo $message; ?></li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<p>
<?php echo $intro . ' ' . current( $this->messages ); ?>
</p>
<?php endif; ?>
</div>
</td>
</tr>
<?php
}
/**
* Load additional CSS for the warning
*/
public function display_notice_css() {
?>
<style>
.plugins tr[data-plugin='<?php echo $this->basename; ?>'] th,
.plugins tr[data-plugin='<?php echo $this->basename; ?>'] td {
box-shadow: none;
}
</style>
<?php
}
}