Passed
Push — master ( 41079d...742cf3 )
by Paul
04:23
created

GL_Plugin_Check_v2::isWpValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
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 2.0.0
8
 */
9
class GL_Plugin_Check_v2
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
	 * @param string $file
67
	 * @return bool
68
	 */
69
	public static function shouldDeactivate( $file, array $args = array() )
70
	{
71
		if( empty( static::$instance )) {
72
			static::$file = realpath( $file );
73
			static::$instance = new static;
74
			static::$versions = static::normalize( $args );
75
		}
76
		if( !static::isValid() ) {
77
			add_action( 'activated_plugin', array( static::$instance, 'deactivate' ));
78
			add_action( 'admin_notices', array( static::$instance, 'deactivate' ));
79
			return true;
80
		}
81
		return false;
82
	}
83
84
	/**
85
	 * @param string $plugin
86
	 * @return void
87
	 */
88
	public function deactivate( $plugin )
89
	{
90
		if( static::isValid() )return;
91
		$pluginSlug = plugin_basename( static::$file );
92
		if( $plugin == $pluginSlug ) {
93
			$this->redirect(); //exit
94
		}
95
		$pluginData = get_file_data( static::$file, array( 'name' => 'Plugin Name' ), 'plugin' );
96
		deactivate_plugins( $pluginSlug );
97
		$this->printNotice( $pluginData['name'] );
98
	}
99
100
	/**
101
	 * @return object
102
	 */
103
	protected static function normalize( array $args = array() )
104
	{
105
		return (object)wp_parse_args( $args, array(
106
			'php' => static::MIN_PHP_VERSION,
107
			'wordpress' => static::MIN_WORDPRESS_VERSION,
108
		));
109
	}
110
111
	/**
112
	 * @return array
113
	 */
114
	protected static function getMessages()
115
	{
116
		return array(
117
			__( 'The %s plugin was deactivated.', 'site-reviews' ),
118
			__( 'This plugin requires %s or greater in order to work properly.', 'site-reviews' ),
119
			__( '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.', 'site-reviews' ),
120
			__( 'PHP version', 'site-reviews' ),
121
			__( 'WordPress version', 'site-reviews' ),
122
			__( 'Update WordPress', 'site-reviews' ),
123
			__( 'If you need to restore %s to the previous version, you can use the %s plugin to do so.', 'site-reviews' ),
124
		);
125
	}
126
127
	/**
128
	 * @return void
129
	 */
130
	protected function redirect()
131
	{
132
		wp_safe_redirect( self_admin_url( sprintf( 'plugins.php?plugin_status=%s&paged=%s&s=%s',
133
			filter_input( INPUT_GET, 'plugin_status' ),
134
			filter_input( INPUT_GET, 'paged' ),
135
			filter_input( INPUT_GET, 's' )
136
		)));
137
		exit;
138
	}
139
140
	/**
141
	 * @param string $pluginName
142
	 * @return void
143
	 */
144
	protected function printNotice( $pluginName )
145
	{
146
		$noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>';
147
		$messages = static::getMessages();
148
		if( !static::isPhpValid() ) {
149
			$rollbackMessage = sprintf( $messages[6], $pluginName, '<a href="https://wordpress.org/plugins/wp-rollback/">WP Rollback</a>' );
150
			printf( $noticeTemplate,
151
				sprintf( $messages[0], $pluginName ),
152
				sprintf( $messages[1], $messages[3].' '.static::$versions->php ),
153
				sprintf( $messages[2], PHP_VERSION ).'</p><p>'.$rollbackMessage
154
			);
155
		}
156
		else if( !static::isWpValid() ) {
157
			printf( $noticeTemplate,
158
				sprintf( $messages[0], $pluginName ),
159
				sprintf( $messages[1], $messages[4].' '.static::$versions->wordpress ),
160
				sprintf( '<a href="%s">%s</a>', admin_url( 'update-core.php' ), $messages[5] )
161
			);
162
		}
163
	}
164
}
165