Plugin   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 175
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstall() 0 7 1
A deactivate() 0 7 1
A __construct() 0 15 1
A getModules() 0 3 2
A activate() 0 14 1
A initModules() 0 4 1
A updater() 0 21 2
A init() 0 11 1
A load() 0 12 1
1
<?php
2
3
/*
4
 * Copyright (c) 2017 Salah Alkhwlani <[email protected]>
5
 *
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Yemenifree\WpSecurity;
11
12
use Illuminate\Support\Collection;
13
use Yemenifree\WpSecurity\Helpers\WPGitHubUpdater;
14
use Yemenifree\WpSecurity\Interfaces\Activable;
15
use Yemenifree\WpSecurity\Interfaces\Initializable;
16
use Yemenifree\WpSecurity\Interfaces\Installable;
17
use Yemenifree\WpSecurity\Interfaces\Loadable;
18
use Yemenifree\WpSecurity\Modules\ConfigSecurity;
19
use Yemenifree\WpSecurity\Modules\CoreSecurity;
20
use Yemenifree\WpSecurity\Modules\HtaccessSecurity;
21
use Yemenifree\WpSecurity\Modules\PluginSecurity;
22
use Yemenifree\WpSecurity\Modules\ResetSecurity;
23
use Yemenifree\WpSecurity\Modules\UploadZipSecurity;
24
use Yemenifree\WpSecurity\Modules\XmlrpcDisabled;
25
26
class Plugin
27
{
28
    /**
29
     * list of active modules.
30
     *
31
     * @var array|Collection
32
     */
33
    protected $modules;
34
35
    /**
36
     * plugin dir.
37
     *
38
     * @var string
39
     */
40
    protected $plugin_dir;
41
42
    /**
43
     * base name of plugin.
44
     *
45
     * @var string
46
     */
47
    private $basename;
48
49
    public function __construct($basename)
50
    {
51
        $this->basename = $basename;
52
        $this->plugin_dir = \dirname($this->basename);
53
        $this->modules = [
54
            HtaccessSecurity::class => [],
55
            ConfigSecurity::class => [],
56
            XmlrpcDisabled::class => [],
57
            CoreSecurity::class => [],
58
            ResetSecurity::class => [],
59
            UploadZipSecurity::class => [],
60
            PluginSecurity::class => [$this->basename],
61
        ];
62
63
        $this->initModules();
64
    }
65
66
    /**
67
     * create object from each modules.
68
     *
69
     * @return Collection
70
     */
71
    protected function initModules()
72
    {
73
        return $this->modules = $this->getModules()->map(function ($args, $module) {
74
            return new $module(...$args);
75
        });
76
    }
77
78
    /**
79
     * get list active modules.
80
     *
81
     * @return Collection
82
     */
83
    public function getModules()
84
    {
85
        return $this->modules instanceof Collection ? $this->modules : collect($this->modules);
86
    }
87
88
    /**
89
     * Load the plugin by hooking into WordPress actions and filters.
90
     * Method should be invoked immediately on plugin load.
91
     */
92
    public function load()
93
    {
94
        // Load all modules that require immediate loading.
95
        $this->getModules()->reject(function ($module) {
96
            return !$module instanceof Loadable;
97
        })->each(function ($module) {
98
            $module->load();
99
        });
100
101
        // Register initialization method.
102
        /** @scrutinizer ignore-call */
103
        add_action('init', [$this, 'init'], 10, 0);
104
    }
105
106
    /**
107
     * Perform initialization tasks.
108
     * Method should be run (early) in init hook.
109
     */
110
    public function init()
111
    {
112
        // Initialize all modules that require initialization.
113
        $this->getModules()->reject(function ($module) {
114
            return !$module instanceof Initializable;
115
        })->each(function ($module) {
116
            $module->init();
117
        });
118
119
        // Initialize updater
120
        $this->updater();
121
    }
122
123
    /**
124
     * update plugin from github.
125
     */
126
    public function updater()
127
    {
128
        // note the use of is_admin() to double check that this is happening in the admin
129
        /** @scrutinizer ignore-call */
130
        if (!is_admin()) {
0 ignored issues
show
Bug introduced by
The function is_admin 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

130
        if (!/** @scrutinizer ignore-call */ is_admin()) {
Loading history...
131
            return;
132
        }
133
134
        $config = [
135
            'slug' => $this->basename,
136
            'proper_folder_name' => $this->plugin_dir,
137
            'api_url' => 'https://api.github.com/repos/yemenifree/wp-security',
138
            'raw_url' => 'https://raw.github.com/yemenifree/wp-security/master',
139
            'github_url' => 'https://github.com/yemenifree/wp-security',
140
            'zip_url' => 'https://github.com/yemenifree/wp-security/archive/{version}.zip',
141
            'sslverify' => false,
142
            'requires' => '4.7',
143
            'tested' => '4.7',
144
            'readme' => 'README.md',
145
        ];
146
        new WPGitHubUpdater($config);
147
    }
148
149
    /**
150
     * Perform activation and installation tasks.
151
     * Method should be run on plugin activation.
152
     *
153
     * @link https://developer.wordpress.org/plugins/the-basics/activation-deactivation-hooks/
154
     */
155
    public function activate()
156
    {
157
        // Install every module that requires it.
158
        $this->getModules()->reject(function ($module) {
159
            return !$module instanceof Installable;
160
        })->each(function ($module) {
161
            $module->install();
162
        });
163
164
        // Activate every module that requires it.
165
        $this->getModules()->reject(function ($module) {
166
            return !$module instanceof Activable;
167
        })->each(function ($module) {
168
            $module->activate();
169
        });
170
    }
171
172
    /**
173
     * Perform deactivation tasks.
174
     * Method should be run on plugin deactivation.
175
     *
176
     * @link https://developer.wordpress.org/plugins/the-basics/activation-deactivation-hooks/
177
     */
178
    public function deactivate()
179
    {
180
        // Activate every module that requires it.
181
        $this->getModules()->reject(function ($module) {
182
            return !$module instanceof Activable;
183
        })->each(function ($module) {
184
            $module->deactivate();
185
        });
186
    }
187
188
    /**
189
     * Perform uninstallation tasks.
190
     * Method should be run on plugin uninstall.
191
     *
192
     * @link https://developer.wordpress.org/plugins/the-basics/uninstall-methods/
193
     */
194
    public function uninstall()
195
    {
196
        // Uninstall every module that requires it.
197
        $this->getModules()->reject(function ($module) {
198
            return !$module instanceof Installable;
199
        })->each(function ($module) {
200
            $module->uninstall();
201
        });
202
    }
203
}
204