Passed
Push — master ( f0530b...3e604e )
by Paul
14:52 queued 08:11
created

Application::storeDefaults()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
cc 3
nc 4
nop 0
crap 12
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\DefaultsManager;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Database\SqlSchema;
8
use GeminiLabs\SiteReviews\Defaults\PermissionDefaults;
9
use GeminiLabs\SiteReviews\Helpers\Arr;
10
use GeminiLabs\SiteReviews\Modules\Migrate;
11
12
/**
13
 * @property array $addons
14
 * @property string $capability
15
 * @property string $cron_event
16
 * @property array $db_version
17
 * @property array $defaults
18
 * @property string $export_key
19
 * @property string $file
20
 * @property string $id
21
 * @property string $languages
22
 * @property string $name
23
 * @property string $paged_handle
24
 * @property string $paged_query_var
25
 * @property string $post_type
26
 * @property string $prefix
27
 * @property array $session
28
 * @property \GeminiLabs\SiteReviews\Arguments $storage
29
 * @property string $taxonomy
30
 * @property string $version
31
 * @property string $testedTo;
32
 */
33
final class Application extends Container
34
{
35
    use Plugin;
36
    use Session;
37
    use Storage;
38
39
    const DB_VERSION = '1.1';
40
    const EXPORT_KEY = '_glsr_export';
41
    const ID = 'site-reviews';
42
    const PAGED_HANDLE = 'pagination_request';
43
    const PAGED_QUERY_VAR = 'reviews-page'; // filtered
44
    const POST_TYPE = 'site-review';
45
    const PREFIX = 'glsr_';
46
    const TAXONOMY = 'site-review-category';
47
48
    /**
49
     * @var array
50
     */
51
    protected $addons = [];
52
53
    /**
54
     * @var array
55
     */
56
    protected $defaults;
57
58
    /**
59
     * @var string
60
     */
61
    protected $name;
62
63
    /**
64
     * @param string $addonId
65
     * @return false|\GeminiLabs\SiteReviews\Addons\Addon
66
     */
67
    public function addon($addonId)
68
    {
69
        if (isset($this->addons[$addonId])) {
70
            return $this->addons[$addonId];
71
        }
72
        return false;
73
    }
74
75
    /**
76
     * @param string $capability
77
     * @return bool
78
     */
79
    public function can($capability)
80
    {
81
        return $this->make(Role::class)->can($capability);
82
    }
83
84
    /**
85
     * @param bool $networkDeactivating
86
     * @return void
87
     * @callback register_deactivation_hook
88
     */
89
    public function deactivate($networkDeactivating)
90
    {
91
        $this->make(Install::class)->deactivate($networkDeactivating);
92
    }
93
94
    /**
95
     * @param string $page
96
     * @param string $tab
97
     * @return string
98
     */
99
    public function getPermission($page = '', $tab = 'index')
100
    {
101
        $fallback = 'edit_posts';
102
        $permissions = $this->make(PermissionDefaults::class)->defaults();
103
        $permission = Arr::get($permissions, $page, $fallback);
104
        if (is_array($permission)) {
105
            $permission = Arr::get($permission, $tab, $fallback);
106
        }
107
        return empty($permission) || !is_string($permission)
108
            ? $fallback
109
            : $permission;
110
    }
111
112
    /**
113
     * @param string $page
114
     * @param string $tab
115
     * @return bool
116
     */
117
    public function hasPermission($page = '', $tab = 'index')
118
    {
119
        $isAdmin = $this->isAdmin();
120
        return !$isAdmin || $this->can($this->getPermission($page, $tab));
121
    }
122
123
    /**
124
     * @return void
125
     */
126
    public function init()
127
    {
128
        // Ensure the custom database tables exist, this is needed in cases
129
        // where the plugin has been updated instead of activated.
130
        $version = get_option(static::PREFIX.'db_version');
131
        if (empty($version)) {
132
            $this->make(Install::class)->run();
133
        } elseif ('1.1' === $version) { // @todo remove this in v5.12.0
134
            if (!$this->make(SqlSchema::class)->columnExists('ratings', 'terms')) {
135
                $this->make(Migrate::class)->reset();
136
                update_option(static::PREFIX.'db_version', '1.0');
137
            }
138
        }
139
        $this->make(Hooks::class)->run();
140
    }
141
142
    /**
143
     * @return bool
144
     */
145 15
    public function isAdmin()
146
    {
147 15
        return (is_admin() || is_network_admin()) && !wp_doing_ajax();
148
    }
149
150
    /**
151
     * @param object $addon
152
     * @return void
153
     */
154
    public function register($addon)
155
    {
156
        try {
157
            $reflection = new \ReflectionClass($addon); // make sure that the class exists
158
            $addon = $reflection->getName();
159
            $this->addons[$addon::ID] = $addon;
160
            $this->singleton($addon); // this goes first!
161
            $this->alias($addon::ID, $this->make($addon)); // @todo for some reason we have to link an alias to an instantiated class
162
            $this->make($addon)->init();
163
        } catch (\ReflectionException $e) {
164
            glsr_log()->error('Attempted to register an invalid addon.');
165
        }
166
    }
167
168
    /**
169
     * @return void
170
     */
171
    public function storeDefaults()
172
    {
173
        if (empty($this->defaults)) {
174
            $defaults = $this->make(DefaultsManager::class)->get();
175
            $this->defaults = $this->filterArray('get/defaults', $defaults);
176
        }
177
        if (empty(get_option(OptionManager::databaseKey()))) {
178
            update_option(OptionManager::databaseKey(), $this->defaults);
179
        }
180
    }
181
}
182