Completed
Push — develop ( 509eab...41e75c )
by Paul
04:07
created

activate.php (2 issues)

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