Completed
Branch dev (58959f)
by
unknown
02:13
created

WP_Update_Php::__construct()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 25
Code Lines 17

Duplication

Lines 20
Ratio 80 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 9
eloc 17
c 1
b 0
f 1
nc 16
nop 2
dl 20
loc 25
rs 4.909
1
<?php
2
/**
3
 * WordPress Update PHP Class
4
 *
5
 * @package   WP_Update_Php
6
 * @author    Coen Jacobs, Sébastien Dumont
7
 * @license   GPL-2.0+
8
 */
9
10
class WP_Update_Php {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
	/**
12
	 * @access private
13
	 * @var    string
14
	 */
15
	private $plugin_name;
16
17
	/**
18
	 * @access private
19
	 * @var    string
20
	 */
21
	private $textdomain;
22
23
	/**
24
	 * @access private
25
	 * @var    string
26
	 */
27
	private $minimum_version;
28
29
	/**
30
	 * @access private
31
	 * @var    string
32
	 */
33
	private $recommended_version;
34
35
	/**
36
	 * @access private
37
	 * @var    string
38
	 */
39
	private $wpupdatephp_site = 'http://www.wpupdatephp.com/update/';
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @access public
45
	 * @param  array $plugin Plugin Name and Text Domain
46
	 * @param  array $requirements Minimum and Recommended version of PHP.
47
	 */
48
	public function __construct($plugin = array(), $requirements = array()) {
49 View Code Duplication
		if ( is_array($plugin) && !empty($plugin['name']) ) {
50
			$this->plugin_name = $plugin['name'];
51
		} else {
52
			$this->plugin_name = '';
53
		}
54
55 View Code Duplication
		if ( is_array($plugin) && !empty($plugin['textdomain']) ) {
56
			$this->textdomain = $plugin['textdomain'];
57
		} else {
58
			$this->textdomain = 'wpupdatephp';
59
		}
60
61 View Code Duplication
		if ( is_array($requirements) && !empty($requirements['recommended_version']) ) {
62
			$this->minimum_version = $requirements['minimum_version'];
63
		} else {
64
			$this->minimum_version = '5.3.0';
65
		}
66
67 View Code Duplication
		if ( is_array($requirements) && !empty($requirements['recommended_version']) ) {
68
			$this->recommended_version = $requirements['recommended_version'];
69
		} else {
70
			$this->recommended_version = null;
71
		}
72
	} // END __construct()
73
74
	/**
75
	 * Check given PHP version against minimum required version.
76
	 *
77
	 * @access public
78
	 * @param  string $version Optional. PHP version to check against.
79
	 *                        Default is the current PHP version as a string in
80
	 *                        "major.minor.release[extra]" notation.
81
	 * @return bool True if supplied PHP version meets minimum required version.
82
	 */
83 View Code Duplication
	public function does_it_meet_required_php_version($version = PHP_VERSION) {
84
		if ($this->version_passes_requirement($this->minimum_version, $version)) {
85
			return true;
86
		}
87
88
		$this->load_version_notice(array($this, 'minimum_admin_notice'));
89
		return false;
90
	} // END does_it_meet_required_php_version()
91
92
	/**
93
	 * Check given PHP version against recommended version.
94
	 *
95
	 * @access public
96
	 * @param  string $version Optional. PHP version to check against.
97
	 *                        Default is the current PHP version as a string in
98
	 *                        "major.minor.release[extra]" notation.
99
	 * @return bool True if supplied PHP version meets recommended version.
100
	 */
101 View Code Duplication
	public function does_it_meet_recommended_php_version($version = PHP_VERSION) {
102
		if ($this->version_passes_requirement($this->recommended_version, $version)) {
103
			return true;
104
		}
105
106
		$this->load_version_notice(array($this, 'recommended_admin_notice'));
107
		return false;
108
	} // END does_it_meet_recommended_php_version()
109
110
	/**
111
	 * Check that one PHP version is less than or equal to another.
112
	 *
113
	 * @access private
114
	 * @param  string $recommended The baseline version of PHP.
115
	 * @param  string $version     The given version of PHP.
116
	 * @return bool True if the requirement is less than or equal to given version.
117
	 */
118
	private function version_passes_requirement($recommended, $version) {
119
		return version_compare($recommended, $version, '<=');
120
	} // END version_passes_requirement()
121
122
	/**
123
	 * Conditionally hook in an admin notice.
124
	 *
125
	 * @access private
126
	 * @param  callable $callback Callable that displays admin notice.
127
	 */
128
	private function load_version_notice($callback) {
129
		if ( is_admin() && ! defined('DOING_AJAX')) {
130
			add_action('admin_notices', $callback);
131
			add_action('network_admin_notices', $callback);
132
		}
133
	} // END load_version_notice()
134
135
	/**
136
	 * Return the string to be shown in the admin notice.
137
	 *
138
	 * This is based on the level (`recommended` or default `minimum`) of the
139
	 * notice. This will also add the plugin name to the notice string, if set.
140
	 *
141
	 * @access public
142
	 * @param  string $level Optional. Admin notice level, `recommended` or `minimum`.
143
	 *                       Default is `minimum`.
144
	 * @return string
145
	 */
146
	public function get_admin_notice($level = 'minimum') {
147
		$notice = '<div class="error is-dismissible">';
148
149 View Code Duplication
		if ('recommended' === $level) {
150
			if ( ! empty($this->plugin_name)) {
151
				$notice .= '<p>'.sprintf(__('%s recommends a PHP version higher than %s. Read more information about <a href="%s" target="_blank">how you can update</a>.', $this->textdomain), $this->plugin_name, $this->recommended_version, $this->wpupdatephp_site).'</p>';
152
			} else {
153
				$notice .= '<p>'.sprintf(__('This plugin recommends a PHP version higher than %s. Read more information about <a href="%s" target="_blank">how you can update</a>.', $this->textdomain), $this->recommended_version, $this->wpupdatephp_site).'</p>';
154
			}
155
		}
156
157 View Code Duplication
		if ( ! empty($this->plugin_name)) {
158
			$notice .= '<p>'.sprintf(__('Unfortunately, %s cannot run on PHP versions older than %s. Read more information about <a href="%s" target="_blank">how you can update</a>.', $this->textdomain), $this->plugin_name, $this->minimum_version, $this->wpupdatephp_site).'</p>';
159
		} else {
160
			$notice .= '<p>'.sprintf(__('Unfortunately, this plugin cannot run on PHP versions older than %s. Read more information about <a href="%s" target="_blank">how you can update</a>.', $this->textdomain), $this->minimum_version, $this->wpupdatephp_site).'</p>';
161
		}
162
163
		$notice .= '</div>';
164
165
		return $notice;
166
	} // END get_admin_notice()
167
168
	/**
169
	 * Method hooked into admin_notices when minimum required PHP version is not
170
	 * available to show this in a notice.
171
	 *
172
	 * @access public
173
	 * @hook   admin_notices
174
	 */
175
	public function minimum_admin_notice() {
176
		echo $this->get_admin_notice('minimum');
177
	} // END minimum_admin_notice()
178
179
	/**
180
	 * Method hooked into admin_notices when recommended PHP version is not
181
	 * available to show this in a notice.
182
	 *
183
	 * @access public
184
	 * @hook   admin_notices
185
	 */
186
	public function recommended_admin_notice() {
187
		echo $this->get_admin_notice('recommended');
188
	} // END recommended_admin_notice()
189
190
} // END Class
191