Test Failed
Push — master ( 29faa5...7f1413 )
by Paul
04:52
created

GL_Plugin_Check_v2::printNotice()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 17
rs 9.7
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 3.0.0
8
 */
9
class GL_Plugin_Check_v3
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 $file;
18
19
	/**
20
	 * @var array
21
	 */
22
	protected $versions;
23
24
	public function __construct( $file, array $versions = [] )
25
	{
26
		$this->file = realpath( $file );
27
		$this->versions = $this->normalize( $versions );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->normalize($versions) of type object is incompatible with the declared type array of property $versions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
	}
29
30
	/**
31
	 * @param string $version
32
	 * @return bool
33
	 */
34
	public function isPhpValid()
35
	{
36
		return !version_compare( PHP_VERSION, $this->versions['php'], '<' );
37
	}
38
39
	/**
40
	 * @return bool
41
	 */
42
	public function isValid()
43
	{
44
		return $this->isPhpValid() && $this->isWpValid();
45
	}
46
47
	/**
48
	 * @param string $version
49
	 * @return bool
50
	 */
51
	public function isWpValid()
52
	{
53
		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...
54
		return !version_compare( $wp_version, $this->versions['wordpress'], '<' );
55
	}
56
57
	/**
58
	 * @param string $plugin
59
	 * @return void
60
	 */
61
	public function deactivate( $plugin )
62
	{
63
		if( $this->isValid() )return;
64
		$pluginSlug = plugin_basename( $this->file );
65
		if( $plugin == $pluginSlug ) {
66
			$this->redirect(); //exit
67
		}
68
		$pluginData = get_file_data( $this->file, array( 'name' => 'Plugin Name' ), 'plugin' );
69
		deactivate_plugins( $pluginSlug );
70
		$this->printNotice( $pluginData['name'] );
71
	}
72
73
	/**
74
	 * @return bool
75
	 */
76
	public function shouldDeactivate()
77
	{
78
		if( !$this->isValid() ) {
79
			add_action( 'activated_plugin', array( $this, 'deactivate' ));
80
			add_action( 'admin_notices', array( $this, 'deactivate' ));
81
			return true;
82
		}
83
		return false;
84
	}
85
86
	/**
87
	 * @return object
88
	 */
89
	protected function normalize( array $args = array() )
90
	{
91
		return wp_parse_args( $args, array(
0 ignored issues
show
Bug Best Practice introduced by
The expression return wp_parse_args($ar...MIN_WORDPRESS_VERSION)) returns the type array which is incompatible with the documented return type object.
Loading history...
92
			'php' => static::MIN_PHP_VERSION,
93
			'wordpress' => static::MIN_WORDPRESS_VERSION,
94
		));
95
	}
96
97
	/**
98
	 * @return array
99
	 */
100
	protected function getMessages()
101
	{
102
		return array(
103
			__( 'The %s plugin was deactivated.', 'site-reviews' ),
104
			__( 'This plugin requires %s or greater in order to work properly.', 'site-reviews' ),
105
			__( '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' ),
106
			__( 'PHP version', 'site-reviews' ),
107
			__( 'WordPress version', 'site-reviews' ),
108
			__( 'Update WordPress', 'site-reviews' ),
109
			__( 'You can use the %s plugin to restore %s to the previous version.', 'site-reviews' ),
110
		);
111
	}
112
113
	/**
114
	 * @return void
115
	 */
116
	protected function redirect()
117
	{
118
		wp_safe_redirect( self_admin_url( sprintf( 'plugins.php?plugin_status=%s&paged=%s&s=%s',
119
			filter_input( INPUT_GET, 'plugin_status' ),
120
			filter_input( INPUT_GET, 'paged' ),
121
			filter_input( INPUT_GET, 's' )
122
		)));
123
		exit;
124
	}
125
126
	/**
127
	 * @param string $pluginName
128
	 * @return void
129
	 */
130
	protected function printNotice( $pluginName )
131
	{
132
		$noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>';
133
		$messages = $this->getMessages();
134
		if( !$this->isPhpValid() ) {
135
			$rollbackMessage = sprintf( '<strong>'.$messages[6].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/">WP Rollback</a>', $pluginName );
136
			printf( $noticeTemplate,
137
				sprintf( $messages[0], $pluginName ),
138
				sprintf( $messages[1], $messages[3].' '.$this->versions['php'] ),
139
				sprintf( $messages[2], PHP_VERSION ).'</p><p>'.$rollbackMessage
140
			);
141
		}
142
		else if( !$this->isWpValid() ) {
143
			printf( $noticeTemplate,
144
				sprintf( $messages[0], $pluginName ),
145
				sprintf( $messages[1], $messages[4].' '.$this->versions['wordpress'] ),
146
				sprintf( '<a href="%s">%s</a>', admin_url( 'update-core.php' ), $messages[5] )
147
			);
148
		}
149
	}
150
}
151