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