-
Notifications
You must be signed in to change notification settings - Fork 11
/
WPUpdatePhp.php
148 lines (133 loc) · 4.85 KB
/
WPUpdatePhp.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
/**
* WPUpdatePHP
*
* @package WPUpdatePhp
* @author Coen Jacobs
* @license GPL-2.0+
* @link https://github.com/WPupdatePHP/wp-update-php
*/
/**
* WPUpdatePhp.
*/
class WPUpdatePhp {
/** @var string */
private $minimum_version;
/** @var string */
private $recommended_version;
/** @var string */
private $plugin_name = '';
/**
* @param string $minimum_version Minimum version of PHP.
* @param string $recommended_version Recommended version of PHP.
*/
public function __construct( $minimum_version, $recommended_version = null ) {
$this->minimum_version = $minimum_version;
$this->recommended_version = $recommended_version;
}
/**
* Set the plugin name for the admin notice.
*
* @param string $name Name of the plugin to be used in admin notices.
*/
public function set_plugin_name( $name ) {
$this->plugin_name = $name;
}
/**
* Check given PHP version against minimum required version.
*
* @param string $version Optional. PHP version to check against.
* Default is the current PHP version as a string in
* "major.minor.release[extra]" notation.
* @return bool True if supplied PHP version meets minimum required version.
*/
public function does_it_meet_required_php_version( $version = PHP_VERSION ) {
if ( $this->version_passes_requirement( $this->minimum_version, $version ) ) {
return true;
}
$this->load_version_notice( array( $this, 'minimum_admin_notice' ) );
return false;
}
/**
* Check given PHP version against recommended version.
*
* @param string $version Optional. PHP version to check against.
* Default is the current PHP version as a string in
* "major.minor.release[extra]" notation.
* @return bool True if supplied PHP version meets recommended version.
*/
public function does_it_meet_recommended_php_version( $version = PHP_VERSION ) {
if ( $this->version_passes_requirement( $this->recommended_version, $version ) ) {
return true;
}
$this->load_version_notice( array( $this, 'recommended_admin_notice' ) );
return false;
}
/**
* Check that one PHP version is less than or equal to another.
*
* @param string $requirement The baseline version of PHP.
* @param string $version The given version of PHP.
* @return bool True if the requirement is less than or equal to given version.
*/
private function version_passes_requirement( $requirement, $version ) {
return version_compare( $requirement, $version, '<=' );
}
/**
* Conditionally hook in an admin notice.
*
* @param callable $callback Callable that displays admin notice.
*/
private function load_version_notice( $callback ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
add_action( 'admin_notices', $callback );
add_action( 'network_admin_notices', $callback );
}
}
/**
* Return the string to be shown in the admin notice.
*
* This is based on the level (`recommended` or default `minimum`) of the
* notice. This will also add the plugin name to the notice string, if set.
*
* @param string $level Optional. Admin notice level, `recommended` or `minimum`.
* Default is `minimum`.
* @return string
*/
public function get_admin_notice( $level = 'minimum' ) {
if ( 'recommended' === $level ) {
if ( ! empty( $this->plugin_name ) ) {
return '<p>' . $this->plugin_name . ' recommends a PHP version higher than ' . $this->recommended_version . '. Read more information about <a href="http://www.wpupdatephp.com/update/">how you can update</a>.</p>';
} else {
return '<p>This plugin recommends a PHP version higher than ' . $this->recommended_version . '. Read more information about <a href="http://www.wpupdatephp.com/update/">how you can update</a>.</p>';
}
}
if ( ! empty( $this->plugin_name ) ) {
return '<p>Unfortunately, ' . $this->plugin_name . ' cannot run on PHP versions older than ' . $this->minimum_version . '. Read more information about <a href="http://www.wpupdatephp.com/update/">how you can update</a>.</p>';
} else {
return '<p>Unfortunately, this plugin cannot run on PHP versions older than ' . $this->minimum_version . '. Read more information about <a href="http://www.wpupdatephp.com/update/">how you can update</a>.</p>';
}
}
/**
* Method hooked into admin_notices when minimum required PHP version is not
* available to show this in a notice.
*
* @hook admin_notices
*/
public function minimum_admin_notice() {
echo '<div class="error">';
echo $this->get_admin_notice( 'minimum' );
echo '</div>';
}
/**
* Method hooked into admin_notices when recommended PHP version is not
* available to show this in a notice.
*
* @hook admin_notices
*/
public function recommended_admin_notice() {
echo '<div class="error">';
echo $this->get_admin_notice( 'recommended' );
echo '</div>';
}
}