Completed
Push — master ( 385022...eefc26 )
by Fulvio
03:54
created

WP_PHP_Console_Loader::add_admin_notice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plugin Name:  WP PHP Console
4
 * Plugin URI:   https://github.com/unfulvio/wp-php-console/
5
 * Description:  An implementation of PHP Console for WordPress. Easily debug and trace PHP errors and warnings from your Chrome dev tools console using a Google Chrome extension.
6
 *
7
 * Version:      1.5.4
8
 *
9
 * Author:       Fulvio Notarstefano
10
 * Author URI:   https://github.com/unfulvio/
11
 *
12
 * License:      GPL-2.0+
13
 * License URI:  http://www.gnu.org/licenses/gpl-2.0.txt
14
 *
15
 * Text Domain:  wp-php-console
16
 * Domain Path:  /languages
17
 *
18
 * WP PHP Console
19
 * Copyright (c) 2014-2019 Fulvio Notarstefano <[email protected]>
20
 * and contributors https://github.com/unfulvio/wp-php-console/graphs/contributors
21
 *
22
 * PHP Console server library
23
 * Copyright (c) 2011-2019 by Barbushin Sergey <[email protected]>
24
 *
25
 * This program is free software; you can redistribute it and/or modify
26
 * it under the terms of the GNU General Public License, version 3 or, at
27
 * your discretion, any later version, as published by the Free
28
 * Software Foundation.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with this program; if not, write to the Free Software
37
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
38
 *
39
 * A copy of the license is also available through the world-wide-web
40
 * at this URL: http://www.gnu.org/licenses/gpl-3.0.html
41
 */
42
43
defined( 'ABSPATH' ) or exit;
44
45
/**
46
 * WP PHP Console loader.
47
 *
48
 * @since 1.5.4
49
 */
50
class WP_PHP_Console_Loader {
51
52
53
	/** minimum PHP version required by this plugin */
54
	const MINIMUM_PHP_VERSION = '5.6.0';
55
56
	/** minimum WordPress version required by this plugin */
57
	const MINIMUM_WP_VERSION = '3.6.0';
58
59
	/** the plugin name, for displaying notices */
60
	const PLUGIN_NAME = 'WP PHP Console';
61
62
63
	/** @var \WP_PHP_Console_Loader single instance of this class */
64
	protected static $instance;
65
66
	/** @var array the admin notices to add */
67
	protected $notices = array();
68
69
70
	/**
71
	 * Loads WP PHP Console after performing compatibility checks.
72
	 *
73
	 * @since 1.5.4
74
	 */
75
	protected function __construct() {
76
77
		register_activation_hook( __FILE__, array( $this, 'activation_check' ) );
78
79
		add_action( 'admin_init',    array( $this, 'check_environment' ) );
80
		add_action( 'admin_init',    array( $this, 'add_plugin_notices' ) );
81
		add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
82
83
		// if the environment check fails, initialize the plugin
84
		if ( $this->is_environment_compatible() && $this->is_wp_compatible() ) {
85
			$this->init_plugin();
86
		}
87
	}
88
89
90
	/**
91
	 * Cloning instances is forbidden due to singleton pattern.
92
	 *
93
	 * @since 1.5.4
94
	 */
95
	public function __clone() {
96
97
		_doing_it_wrong( __FUNCTION__, sprintf( 'You cannot clone instances of %s.', get_class( $this ) ), '1.5.4' );
98
	}
99
100
101
	/**
102
	 * Unserializing instances is forbidden due to singleton pattern.
103
	 *
104
	 * @since 1.5.4
105
	 */
106
	public function __wakeup() {
107
108
		_doing_it_wrong( __FUNCTION__, sprintf( 'You cannot unserialize instances of %s.', get_class( $this ) ), '1.5.4' );
109
	}
110
111
112
	/**
113
	 * Initializes the plugin.
114
	 *
115
	 * @internal
116
	 *
117
	 * @since 1.5.4
118
	 */
119
	private function init_plugin() {
120
121
		// autoload plugin and vendor files
122
		$loader = require_once( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' );
123
124
		// register plugin namespace with autoloader
125
		$loader->addPsr4( 'WP_PHP_Console\\', __DIR__ . '/includes' );
126
127
		require_once plugin_dir_path( __FILE__ ) . 'includes/Functions.php';
128
129
		wp_php_console();
130
	}
131
132
133
	/**
134
	 * Checks the server environment and other factors and deactivates plugins as necessary.
135
	 *
136
	 * Based on http://wptavern.com/how-to-prevent-wordpress-plugins-from-activating-on-sites-with-incompatible-hosting-environments
137
	 *
138
	 * @internal
139
	 *
140
	 * @since 1.5.4
141
	 */
142
	public function activation_check() {
143
144
		if ( ! $this->is_environment_compatible() ) {
145
146
			$this->deactivate_plugin();
147
148
			wp_die( self::PLUGIN_NAME . ' could not be activated. ' . $this->get_environment_message() );
149
		}
150
	}
151
152
153
	/**
154
	 * Checks the environment on loading WordPress, just in case the environment changes after activation.
155
	 *
156
	 * @internal
157
	 *
158
	 * @since 1.5.4
159
	 */
160
	public function check_environment() {
161
162
		if ( ! $this->is_environment_compatible() && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
163
164
			$this->deactivate_plugin();
165
166
			$this->add_admin_notice( 'bad_environment', 'error', self::PLUGIN_NAME . ' has been deactivated. ' . $this->get_environment_message() );
167
		}
168
	}
169
170
171
	/**
172
	 * Adds notices for out-of-date WordPress and/or WooCommerce versions.
173
	 *
174
	 * @internal
175
	 *
176
	 * @since 1.5.4
177
	 */
178
	public function add_plugin_notices() {
179
180
		if ( ! $this->is_wp_compatible() ) {
181
182
			$this->add_admin_notice( 'update_wordpress', 'error', sprintf(
183
				'%s requires WordPress version %s or higher. Please %supdate WordPress &raquo;%s',
184
				'<strong>' . self::PLUGIN_NAME . '</strong>',
185
				self::MINIMUM_WP_VERSION,
186
				'<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">', '</a>'
187
			) );
188
		}
189
	}
190
191
192
	/**
193
	 * Determines if the plugin is WordPress compatible.
194
	 *
195
	 * @since 1.5.4
196
	 *
197
	 * @return bool
198
	 */
199
	private function is_wp_compatible() {
200
		global $wp_version;
201
202
		return version_compare( $wp_version, self::MINIMUM_WP_VERSION, '>=' );
203
	}
204
205
206
	/**
207
	 * Deactivates the plugin.
208
	 *
209
	 * @since 1.5.4
210
	 */
211
	private function deactivate_plugin() {
212
213
		deactivate_plugins( plugin_basename( __FILE__ ) );
214
215
		if ( isset( $_GET['activate'] ) ) {
216
			unset( $_GET['activate'] );
217
		}
218
	}
219
220
221
	/**
222
	 * Adds an admin notice to be displayed.
223
	 *
224
	 * @since 1.5.4
225
	 *
226
	 * @param string $slug notice ID
227
	 * @param string $class CSS class
228
	 * @param string $message message content
229
	 */
230
	private function add_admin_notice( $slug, $class, $message ) {
231
232
		$this->notices[ $slug ] = array(
233
			'class'   => $class,
234
			'message' => $message
235
		);
236
	}
237
238
239
	/**
240
	 * Displays admin notices.
241
	 *
242
	 * @internal
243
	 *
244
	 * @since 1.5.4
245
	 */
246
	public function admin_notices() {
247
248
		foreach ( (array) $this->notices as $notice_key => $notice ) :
249
250
			?>
251
			<div class="<?php echo esc_attr( $notice['class'] ); ?>">
252
				<p><?php echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) ); ?></p>
253
			</div>
254
			<?php
255
256
		endforeach;
257
	}
258
259
260
	/**
261
	 * Determines if the server environment is compatible with this plugin.
262
	 *
263
	 * @since 1.5.4
264
	 *
265
	 * @return bool
266
	 */
267
	private function is_environment_compatible() {
268
269
		return version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=' );
270
	}
271
272
273
	/**
274
	 * Returns the message for display when the environment is incompatible with this plugin.
275
	 *
276
	 * @since 1.5.4
277
	 *
278
	 * @return string
279
	 */
280
	private function get_environment_message() {
281
282
		return sprintf( 'The minimum PHP version required for this plugin is %1$s. You are running %2$s.', self::MINIMUM_PHP_VERSION, PHP_VERSION );
283
	}
284
285
286
	/**
287
	 * Returns the main \WP_PHP_Console_Loader instance.
288
	 *
289
	 * Ensures only one instance can be loaded at any given time.
290
	 *
291
	 * @since 1.5.4
292
	 *
293
	 * @return \WP_PHP_Console_Loader
294
	 */
295
	public static function instance() {
296
297
		if ( null === self::$instance ) {
298
			self::$instance = new self();
299
		}
300
301
		return self::$instance;
302
	}
303
304
305
}
306
307
// initialize the plugin loader
308
WP_PHP_Console_Loader::instance();
309