Passed
Push — master ( 28ffd7...b2dc5c )
by Paul
03:45
created

GL_BlackBar_Activate::isWpValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
defined( 'WPINC' ) || die;
4
5
class GL_BlackBar_Activate
6
{
7
	const BASENAME = 'blackbar.php';
8
	const MIN_PHP_VERSION = '5.6.0';
9
	const MIN_WORDPRESS_VERSION = '4.0.0';
10
11
	/**
12
	 * @var static
13
	 */
14
	protected static $instance;
15
16
	/**
17
	 * @return bool
18
	 */
19
	public static function isValid()
20
	{
21
		return static::isPhpValid() && static::isWpValid();
22
	}
23
24
	/**
25
	 * @return bool
26
	 */
27
	public static function isPhpValid()
28
	{
29
		return !version_compare( PHP_VERSION, static::MIN_PHP_VERSION, '<' );
30
	}
31
32
	/**
33
	 * @return bool
34
	 */
35
	public static function isWpValid()
36
	{
37
		global $wp_version;
38
		return !version_compare( $wp_version, static::MIN_WORDPRESS_VERSION, '<' );
39
	}
40
41
	/**
42
	 * @return bool
43
	 */
44
	public static function shouldDeactivate()
45
	{
46
		if( empty( static::$instance )) {
47
			static::$instance = new static;
48
		}
49
		if( !static::isValid() ) {
50
			add_action( 'activated_plugin', array( static::$instance, 'deactivate' ));
51
			add_action( 'admin_notices', array( static::$instance, 'deactivate' ));
52
			return true;
53
		}
54
		return false;
55
	}
56
57
	/**
58
	 * @return void
59
	 */
60
	public function deactivate( $plugin )
61
	{
62
		if( static::isValid() )return;
63
		$pluginName = plugin_basename( dirname( realpath( __FILE__ )).'/'.static::BASENAME );
64
		if( $plugin == $pluginName ) {
65
			$this->redirect(); //exit
66
		}
67
		deactivate_plugins( $pluginName );
68
		$this->printNotice();
69
	}
70
71
	/**
72
	 * @return void
73
	 */
74
	protected function redirect()
75
	{
76
		wp_safe_redirect( self_admin_url( sprintf( 'plugins.php?plugin_status=%s&paged=%s&s=%s',
77
			filter_input( INPUT_GET, 'plugin_status' ),
78
			filter_input( INPUT_GET, 'paged' ),
79
			filter_input( INPUT_GET, 's' )
80
		)));
81
		exit;
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
82
	}
83
84
	/**
85
	 * @return void
86
	 */
87
	protected function printNotice()
88
	{
89
		$noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>';
90
		$messages = array(
91
			__( 'The BlackBar plugin was deactivated.', 'blackbar' ),
92
			__( 'Sorry, this plugin requires %s or greater in order to work properly.', 'blackbar' ),
93
			__( 'Please contact your hosting provider or server administrator to upgrade the version of PHP on your server (your server is running PHP version %s), or try to find an alternative plugin.', 'blackbar' ),
94
			__( 'PHP version', 'blackbar' ).' '.static::MIN_PHP_VERSION,
95
			__( 'WordPress version', 'blackbar' ).' '.static::MIN_WORDPRESS_VERSION,
96
			__( 'Update WordPress', 'blackbar' ),
97
		);
98
		if( !static::isPhpValid() ) {
99
			printf( $noticeTemplate,
100
				$messages[0],
101
				sprintf( $messages[1], $messages[3] ),
102
				sprintf( $messages[2], PHP_VERSION )
103
			);
104
		}
105
		else if( !static::isWpValid() ) {
106
			printf( $noticeTemplate,
107
				$messages[0],
108
				sprintf( $messages[1], $messages[4] ),
109
				sprintf( '<a href="%s">%s</a>', admin_url( 'update-core.php' ), $messages[5] )
110
			);
111
		}
112
	}
113
}
114