Completed
Push — master ( a00b53...3dc4d8 )
by
unknown
21:18
created

SimCal_WP_Requirements::__construct()   C

Complexity

Conditions 18
Paths 37

Size

Total Lines 62
Code Lines 34

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 62
rs 6.0989
cc 18
eloc 34
nc 37
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * WP Requirements
4
 *
5
 * Utility to check current PHP version, WordPress version and PHP extensions.
6
 *
7
 * @package WP_Requirements
8
 * @version 1.4.0
9
 * @author  Fulvio Notarstefano <[email protected]>
10
 * @link    https://github.com/nekojira/wp-requirements
11
 * @license GPL2+
12
 */
13
14
if ( ! class_exists( 'SimCal_WP_Requirements' ) ) {
15
16
	class SimCal_WP_Requirements {
17
18
		/**
19
		 * Plugin name.
20
		 *
21
		 * @access private
22
		 * @var string
23
		 */
24
		private $name = '';
25
26
		/**
27
		 * Plugin main file.
28
		 *
29
		 * plugin_basename( __FILE__ )
30
		 *
31
		 * @access private
32
		 * @var string
33
		 */
34
		private $plugin = '';
35
36
		/**
37
		 * WordPress.
38
		 *
39
		 * @access private
40
		 * @var bool
41
		 */
42
		private $wp = true;
43
44
		/**
45
		 * PHP.
46
		 *
47
		 * @access private
48
		 * @var bool
49
		 */
50
		private $php = true;
51
52
		/**
53
		 * PHP Extensions.
54
		 *
55
		 * @access private
56
		 * @var bool
57
		 */
58
		private $extensions = true;
59
60
		/**
61
		 * Requirements to check.
62
		 *
63
		 * @access private
64
		 * @var array
65
		 */
66
		private $requirements = array();
67
68
		/**
69
		 * Results failures.
70
		 *
71
		 * Associative array with requirements results.
72
		 *
73
		 * @access private
74
		 * @var array
75
		 */
76
		private $failures = array();
77
78
		/**
79
		 * Admin notice.
80
		 *
81
		 * @access private
82
		 * @var string
83
		 */
84
		private $notice = '';
85
86
		/**
87
		 * Run checks.
88
		 *
89
		 * @param string $name         The plugin name.
90
		 * @param string $plugin       Output of `plugin_basename( __FILE__ )`.
91
		 * @param array  $requirements Associative array with requirements.
92
		 */
93
		public function __construct( $name, $plugin, $requirements ) {
94
95
			$this->name = htmlspecialchars( strip_tags( $name ) );
96
			$this->plugin = $plugin;
97
			$this->requirements = $requirements;
98
99
			if ( ! empty( $requirements ) && is_array( $requirements ) ) {
100
101
				$failures = $extensions = array();
102
103
				$requirements = array_merge(
104
					array(
105
						'WordPress'  => '',
106
						'PHP'        => '',
107
						'Extensions' => '',
108
					), $requirements
109
				);
110
111
				// Check for WordPress version.
112
				if ( $requirements['WordPress'] && is_string( $requirements['WordPress'] ) ) {
113
					if ( function_exists( 'get_bloginfo' ) ) {
114
						$wp_version = get_bloginfo( 'version' );
115
						if ( version_compare( $wp_version, $requirements['WordPress'] ) === - 1 ) {
116
							$failures['WordPress'] = $wp_version;
117
							$this->wp = false;
118
						}
119
					}
120
				}
121
122
				// Check fo PHP version.
123
				if ( $requirements['PHP'] && is_string( $requirements['PHP'] ) ) {
124
					if ( version_compare( PHP_VERSION, $requirements['PHP'] ) === -1 ) {
125
						$failures['PHP'] = PHP_VERSION;
126
						$this->php = false;
127
					}
128
				}
129
130
				// Check fo PHP Extensions.
131
				if ( $requirements['Extensions'] && is_array( $requirements['Extensions'] ) ) {
132
					foreach ( $requirements['Extensions'] as $extension ) {
133
						if ( $extension && is_string( $extension ) ) {
134
							$extensions[ $extension ] = extension_loaded( $extension );
135
						}
136
					}
137
					if ( in_array( false, $extensions ) ) {
138
						foreach ( $extensions as $extension_name => $found  ) {
139
							if ( $found === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
140
								$failures['Extensions'][ $extension_name ] = $extension_name;
141
							}
142
						}
143
						$this->extensions = false;
144
					}
145
				}
146
147
				$this->failures = $failures;
148
149
			} else {
150
151
				trigger_error( 'WP Requirements: the requirements are invalid.', E_USER_ERROR );
152
153
			}
154
		}
155
156
		/**
157
		 * Get requirements results.
158
		 *
159
		 * @return array
160
		 */
161
		public function failures() {
162
			return $this->failures;
163
		}
164
165
		/**
166
		 * Check if versions check pass.
167
		 *
168
		 * @return bool
169
		 */
170
		public function pass() {
171
			if ( in_array( false, array(
172
				$this->wp,
173
				$this->php,
174
				$this->extensions,
175
			) ) ) {
176
				return false;
177
			}
178
			return true;
179
		}
180
181
		/**
182
		 * Notice message.
183
		 *
184
		 * @param  string $message An additional message.
185
		 *
186
		 * @return string
187
		 */
188
		public function get_notice( $message = '' ) {
189
190
			$notice   = '';
191
			$name     = $this->name;
192
			$failures = $this->failures;
193
194
			if ( ! empty( $failures ) && is_array( $failures ) ) {
195
196
				$notice  = '<div class="error">' . "\n";
197
				$notice .= "\t" . '<p>' . "\n";
198
				$notice .= '<strong>' . sprintf( '%s could not be activated.', $name ) . '</strong><br>';
199
200
				foreach ( $failures as $requirement => $found ) {
201
202
					$required = $this->requirements[ $requirement ];
203
204
					if ( 'Extensions' == $requirement ) {
205
						if ( is_array( $found ) ) {
206
							$notice .= sprintf( 
207
									'Required PHP Extension(s) not found: %s.', 
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 32 spaces, but found 36.
Loading history...
208
									join( ', ', $found ) 
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 32 spaces, but found 36.
Loading history...
209
								) . '<br>';
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 28 spaces, but found 32.
Loading history...
210
						}
211
					} else {
212
						$notice .= sprintf( 
213
								'Required %1$s version: %2$s - Version found: %3$s',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 28 spaces, but found 32.
Loading history...
214
								$requirement, 
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 28 spaces, but found 32.
Loading history...
215
								$required, 
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 28 spaces, but found 32.
Loading history...
216
								$found 
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 28 spaces, but found 32.
Loading history...
217
							) . '<br>';
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 28.
Loading history...
218
					}
219
220
				}
221
222
				$notice .= '<em>' . sprintf( 'Please update to meet %s requirements.', $name ) . '</em>' . "\n";
223
				$notice .= "\t" . '</p>' . "\n";
224
				if ( $message ) {
225
					$notice .= $message;
226
				}
227
				$notice .= '</div>';
228
			}
229
230
			return $notice;
231
		}
232
233
		/**
234
		 * Print notice.
235
		 */
236
		public function print_notice() {
237
			echo $this->notice;
238
		}
239
240
		/**
241
		 * Deactivate plugin.
242
		 */
243
		public function deactivate_plugin() {
244
			if ( function_exists( 'deactivate_plugins' ) && function_exists( 'plugin_basename' ) ) {
245
				deactivate_plugins( $this->plugin );
246
			}
247
		}
248
249
		/**
250
		 * Deactivate plugin and display admin notice.
251
		 *
252
		 * @param string $message An additional message in notice.
253
		 */
254
		public function halt( $message = '' ) {
255
256
			$this->notice = $this->get_notice( $message );
257
258
			if ( $this->notice && function_exists( 'add_action' ) ) {
259
260
				add_action( 'admin_notices', array( $this, 'print_notice' ) );
261
				add_action( 'admin_init', array( $this, 'deactivate_plugin' ) );
262
263
				if ( isset( $_GET['activate'] ) ) {
264
					unset( $_GET['activate'] );
265
				}
266
			}
267
		}
268
269
	}
270
271
}
272