Passed
Push — master ( 5e727b...fa3796 )
by Paul
07:53
created

activate.php (10 issues)

Labels
Severity
1
<?php
2
3
defined('ABSPATH') || die;
4
5
/**
6
 * Check for minimum system requirments on plugin activation.
7
 * @version 5.5.0
8
 */
9
class GL_Plugin_Check_v5
10
{
11
    const MIN_PHP_VERSION = '5.6.20';
12
    const MIN_WORDPRESS_VERSION = '5.5.0';
13
14
    /**
15
     * @var array
16
     */
17
    public $versions;
18
19
    /**
20
     * @var string
21
     */
22
    protected $file;
23
24
    /**
25
     * @param string $file
26
     */
27
    public function __construct($file)
28
    {
29
        $this->file = realpath($file);
30
        $versionRequirements = get_file_data($this->file, [
0 ignored issues
show
The function get_file_data was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $versionRequirements = /** @scrutinizer ignore-call */ get_file_data($this->file, [
Loading history...
31
            'php' => 'Requires PHP',
32
            'wordpress' => 'Requires at least',
33
        ]);
34
        $this->versions = wp_parse_args(array_filter($versionRequirements), [
0 ignored issues
show
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $this->versions = /** @scrutinizer ignore-call */ wp_parse_args(array_filter($versionRequirements), [
Loading history...
35
            'php' => static::MIN_PHP_VERSION,
36
            'wordpress' => static::MIN_WORDPRESS_VERSION,
37
        ]);
38
    }
39
40
    /**
41
     * @return bool
42
     */
43
    public function canProceed()
44
    {
45
        if ($this->isValid()) {
46
            return true;
47
        }
48
        add_action('activated_plugin', [$this, 'deactivate']);
0 ignored issues
show
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        /** @scrutinizer ignore-call */ 
49
        add_action('activated_plugin', [$this, 'deactivate']);
Loading history...
49
        add_action('admin_notices', [$this, 'deactivate']);
50
        return false;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function isPhpValid()
57
    {
58
        return !version_compare(PHP_VERSION, $this->versions['php'], '<');
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function isValid()
65
    {
66
        return $this->isPhpValid() && $this->isWpValid();
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function isWpValid()
73
    {
74
        global $wp_version;
75
        return !version_compare($wp_version, $this->versions['wordpress'], '<');
76
    }
77
78
    /**
79
     * @param string $plugin
80
     * @return void
81
     */
82
    public function deactivate($plugin)
83
    {
84
        if ($this->isValid()) {
85
            return;
86
        }
87
        $pluginSlug = plugin_basename($this->file);
0 ignored issues
show
The function plugin_basename was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        $pluginSlug = /** @scrutinizer ignore-call */ plugin_basename($this->file);
Loading history...
88
        if ($plugin == $pluginSlug) {
89
            $this->redirect(); //exit
90
        }
91
        $pluginData = get_file_data($this->file, ['name' => 'Plugin Name'], 'plugin');
0 ignored issues
show
The function get_file_data was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $pluginData = /** @scrutinizer ignore-call */ get_file_data($this->file, ['name' => 'Plugin Name'], 'plugin');
Loading history...
92
        deactivate_plugins($pluginSlug);
0 ignored issues
show
The function deactivate_plugins was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        /** @scrutinizer ignore-call */ 
93
        deactivate_plugins($pluginSlug);
Loading history...
93
        $this->printNotice($pluginData['name']);
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    protected function getMessages()
100
    {
101
        return [
102
            'notice' => _x('The %s plugin was deactivated.', 'admin-text', 'site-reviews'),
0 ignored issues
show
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
            'notice' => /** @scrutinizer ignore-call */ _x('The %s plugin was deactivated.', 'admin-text', 'site-reviews'),
Loading history...
103
            'php_version' => _x('PHP version', 'admin-text', 'site-reviews'),
104
            'rollback' => _x('You can use the %s plugin to restore %s to the previous version.', 'admin-text', 'site-reviews'),
105
            'update_php' => _x('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.', 'admin-text', 'site-reviews'),
106
            'update_wp' => _x('Update WordPress', 'admin-text', 'site-reviews'),
107
            'wp_version' => _x('WordPress version', 'admin-text', 'site-reviews'),
108
            'wrong_version' => _x('This plugin requires %s or greater in order to work properly.', 'admin-text', 'site-reviews'),
109
        ];
110
    }
111
112
    /**
113
     * @param string $pluginName
114
     * @return void
115
     */
116
    protected function printNotice($pluginName)
117
    {
118
        $noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>';
119
        $messages = $this->getMessages();
120
        $rollbackMessage = sprintf('<strong>'.$messages['rollback'].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/">WP Rollback</a>', $pluginName);
121
        if (!$this->isPhpValid()) {
122
            printf($noticeTemplate,
123
                sprintf($messages['notice'], $pluginName),
124
                sprintf($messages['wrong_version'], $messages['php_version'].' '.$this->versions['php']),
125
                sprintf($messages['update_php'], PHP_VERSION).'</p><p>'.$rollbackMessage
126
            );
127
        } elseif (!$this->isWpValid()) {
128
            printf($noticeTemplate,
129
                sprintf($messages['notice'], $pluginName),
130
                sprintf($messages['wrong_version'], $messages['wp_version'].' '.$this->versions['wordpress']),
131
                $rollbackMessage.'</p><p>'.sprintf('<a href="%s">%s</a>', admin_url('update-core.php'), $messages['update_wp'])
0 ignored issues
show
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
                $rollbackMessage.'</p><p>'.sprintf('<a href="%s">%s</a>', /** @scrutinizer ignore-call */ admin_url('update-core.php'), $messages['update_wp'])
Loading history...
132
            );
133
        }
134
    }
135
136
    /**
137
     * @return void
138
     */
139
    protected function redirect()
140
    {
141
        wp_safe_redirect(self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s',
0 ignored issues
show
The function wp_safe_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        /** @scrutinizer ignore-call */ 
142
        wp_safe_redirect(self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s',
Loading history...
The function self_admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        wp_safe_redirect(/** @scrutinizer ignore-call */ self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s',
Loading history...
142
            filter_input(INPUT_GET, 'plugin_status'),
143
            filter_input(INPUT_GET, 'paged'),
144
            filter_input(INPUT_GET, 's')
145
        )));
146
        exit;
147
    }
148
}
149