Passed
Branch master (026d5a)
by Paul
02:10
created

GL_Plugin_Check::deactivate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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;
1 ignored issue
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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