Passed
Push — master ( acf75d...995929 )
by Paul
03:41
created

GL_Plugin_Check::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
defined( 'WPINC' ) || die;
4
5
/**
6
 * Checks for minimum system requirments on plugin activation
7
 * @version 1.0.1
8
 */
9
class GL_Plugin_Check
10
{
11
	const MIN_PHP_VERSION = '5.6.0';
12
	const MIN_WORDPRESS_VERSION = '4.7.0';
13
14
	/**
15
	 * @var string
16
	 */
17
	protected static $file;
18
19
	/**
20
	 * @var static
21
	 */
22
	protected static $instance;
23
24
	/**
25
	 * @var object
26
	 */
27
	protected static $versions;
28
29
	/**
30
	 * @param string $version
31
	 * @return bool
32
	 */
33
	public static function isPhpValid( $version = '' )
34
	{
35
		if( !empty( $version )) {
36
			static::normalize( array( 'php' => $version ));
37
		}
38
		return !version_compare( PHP_VERSION, static::$versions->php, '<' );
39
	}
40
41
	/**
42
	 * @return bool
43
	 */
44
	public static function isValid( array $args = array() )
45
	{
46
		if( !empty( $args )) {
47
			static::normalize( $args );
48
		}
49
		return static::isPhpValid() && static::isWpValid();
50
	}
51
52
	/**
53
	 * @param string $version
54
	 * @return bool
55
	 */
56
	public static function isWpValid( $version = '' )
57
	{
58
		global $wp_version;
59
		if( !empty( $version )) {
60
			static::normalize( array( 'wordpress' => $version ));
61
		}
62
		return !version_compare( $wp_version, static::$versions->wordpress, '<' );
63
	}
64
65
	/**
66
	 * @return bool
67
	 */
68
	public static function shouldDeactivate( $file, array $args = array() )
69
	{
70
		if( empty( static::$instance )) {
71
			static::$file = realpath( $file );
72
			static::$instance = new static;
73
			static::$versions = static::normalize( $args );
74
		}
75
		if( !static::isValid() ) {
76
			add_action( 'activated_plugin', array( static::$instance, 'deactivate' ));
77
			add_action( 'admin_notices', array( static::$instance, 'deactivate' ));
78
			return true;
79
		}
80
		return false;
81
	}
82
83
	/**
84
	 * @return void
85
	 */
86
	public function deactivate( $plugin )
87
	{
88
		if( static::isValid() )return;
89
		$pluginSlug = plugin_basename( static::$file );
90
		if( $plugin == $pluginSlug ) {
91
			$this->redirect(); //exit
92
		}
93
		$pluginData = get_file_data( static::$file, array( 'name' => 'Plugin Name' ), 'plugin' );
94
		deactivate_plugins( $pluginSlug );
95
		$this->printNotice( $pluginData['name'] );
96
	}
97
98
	/**
99
	 * @return object
100
	 */
101
	protected static function normalize( array $args = array() )
102
	{
103
		return (object)wp_parse_args( $args, array(
104
			'php' => static::MIN_PHP_VERSION,
105
			'wordpress' => static::MIN_WORDPRESS_VERSION,
106
		));
107
	}
108
109
	/**
110
	 * @return void
111
	 */
112
	protected function redirect()
113
	{
114
		wp_safe_redirect( self_admin_url( sprintf( 'plugins.php?plugin_status=%s&paged=%s&s=%s',
115
			filter_input( INPUT_GET, 'plugin_status' ),
116
			filter_input( INPUT_GET, 'paged' ),
117
			filter_input( INPUT_GET, 's' )
118
		)));
119
		exit;
120
	}
121
122
	/**
123
	 * @param string $pluginName
124
	 * @return void
125
	 */
126
	protected function printNotice( $pluginName )
127
	{
128
		$noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>';
129
		$messages = array(
130
			__( 'The %s plugin was deactivated.', 'blackbar' ),
131
			__( 'Sorry, this plugin requires %s or greater in order to work properly.', 'blackbar' ),
132
			__( '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' ),
133
			__( 'PHP version', 'blackbar' ),
134
			__( 'WordPress version', 'blackbar' ),
135
			__( 'Update WordPress', 'blackbar' ),
136
		);
137
		if( !static::isPhpValid() ) {
138
			printf( $noticeTemplate,
139
				sprintf( $messages[0], $pluginName ),
140
				sprintf( $messages[1], $messages[3].' '.static::$versions->php ),
141
				sprintf( $messages[2], PHP_VERSION )
142
			);
143
		}
144
		else if( !static::isWpValid() ) {
145
			printf( $noticeTemplate,
146
				sprintf( $messages[0], $pluginName ),
147
				sprintf( $messages[1], $messages[4].' '.static::$versions->wordpress ),
148
				sprintf( '<a href="%s">%s</a>', admin_url( 'update-core.php' ), $messages[5] )
149
			);
150
		}
151
	}
152
}
153