|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\DefaultsManager; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\PermissionDefaults; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Install; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @property array $addons |
|
14
|
|
|
* @property string $capability |
|
15
|
|
|
* @property string $cron_event |
|
16
|
|
|
* @property array $defaults |
|
17
|
|
|
* @property string $export_key |
|
18
|
|
|
* @property string $file |
|
19
|
|
|
* @property string $id |
|
20
|
|
|
* @property string $languages |
|
21
|
|
|
* @property string $name |
|
22
|
|
|
* @property string $paged_handle |
|
23
|
|
|
* @property string $paged_query_var |
|
24
|
|
|
* @property string $post_type |
|
25
|
|
|
* @property string $prefix |
|
26
|
|
|
* @property array $session |
|
27
|
|
|
* @property \GeminiLabs\SiteReviews\Arguments $storage |
|
28
|
|
|
* @property string $taxonomy |
|
29
|
|
|
* @property string $version |
|
30
|
|
|
* @property string $testedTo; |
|
31
|
|
|
*/ |
|
32
|
|
|
final class Application extends Container |
|
33
|
|
|
{ |
|
34
|
|
|
use Plugin; |
|
35
|
|
|
use Session; |
|
36
|
|
|
use Storage; |
|
37
|
|
|
|
|
38
|
|
|
const EXPORT_KEY = '_glsr_export'; |
|
39
|
|
|
const ID = 'site-reviews'; |
|
40
|
|
|
const PAGED_HANDLE = 'pagination_request'; |
|
41
|
|
|
const PAGED_QUERY_VAR = 'reviews-page'; // filtered |
|
42
|
|
|
const POST_TYPE = 'site-review'; |
|
43
|
|
|
const PREFIX = 'glsr_'; |
|
44
|
|
|
const TAXONOMY = 'site-review-category'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $addons = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $defaults; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $name; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $view |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
15 |
|
public function build($view, array $data = []) |
|
66
|
|
|
{ |
|
67
|
15 |
|
ob_start(); |
|
68
|
15 |
|
$this->render($view, $data); |
|
69
|
15 |
|
return ob_get_clean(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $capability |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function can($capability) |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->make(Role::class)->can($capability); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function catchFatalError() |
|
85
|
|
|
{ |
|
86
|
|
|
$error = error_get_last(); |
|
87
|
|
|
if (E_ERROR === Arr::get($error, 'type') && Str::contains($this->path(), Arr::get($error, 'message'))) { |
|
88
|
|
|
glsr_log()->error($error['message']); |
|
89
|
|
|
} |
|
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 $view |
|
104
|
|
|
* @return void|string |
|
105
|
|
|
*/ |
|
106
|
15 |
|
public function file($view) |
|
107
|
|
|
{ |
|
108
|
15 |
|
$view .= '.php'; |
|
109
|
15 |
|
$filePaths = []; |
|
110
|
15 |
|
if (Str::startsWith('templates/', $view)) { |
|
111
|
14 |
|
$filePaths[] = $this->themePath(Str::removePrefix($view, 'templates/')); |
|
112
|
|
|
} |
|
113
|
15 |
|
$filePaths[] = $this->path($view); |
|
114
|
15 |
|
$filePaths[] = $this->path('views/'.$view); |
|
115
|
15 |
|
foreach ($filePaths as $file) { |
|
116
|
15 |
|
if (file_exists($file)) { |
|
117
|
15 |
|
return $file; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $page |
|
124
|
|
|
* @param string $tab |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getPermission($page = '', $tab = 'index') |
|
128
|
|
|
{ |
|
129
|
|
|
$fallback = 'edit_posts'; |
|
130
|
|
|
$permissions = $this->make(PermissionDefaults::class)->defaults(); |
|
131
|
|
|
$permission = Arr::get($permissions, $page, $fallback); |
|
132
|
|
|
if (is_array($permission)) { |
|
133
|
|
|
$permission = Arr::get($permission, $tab, $fallback); |
|
134
|
|
|
} |
|
135
|
|
|
return empty($permission) || !is_string($permission) |
|
136
|
|
|
? $fallback |
|
137
|
|
|
: $permission; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $page |
|
142
|
|
|
* @param string $tab |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function hasPermission($page = '', $tab = 'index') |
|
146
|
|
|
{ |
|
147
|
|
|
$isAdmin = $this->isAdmin(); |
|
148
|
|
|
return !$isAdmin || $this->can($this->getPermission($page, $tab)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
public function init() |
|
155
|
|
|
{ |
|
156
|
|
|
// Ensure the custom database tables exist, this is needed in cases |
|
157
|
|
|
// where the plugin has been updated instead of activated. |
|
158
|
|
|
if (empty(get_option(static::PREFIX.'db_version'))) { |
|
159
|
|
|
$this->make(Install::class)->run(); |
|
160
|
|
|
} |
|
161
|
|
|
$this->make(Hooks::class)->run(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
15 |
|
public function isAdmin() |
|
168
|
|
|
{ |
|
169
|
15 |
|
return (is_admin() || is_network_admin()) && !wp_doing_ajax(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param object $addon |
|
174
|
|
|
* @return void |
|
175
|
|
|
*/ |
|
176
|
|
|
public function register($addon) |
|
177
|
|
|
{ |
|
178
|
|
|
try { |
|
179
|
|
|
$reflection = new \ReflectionClass($addon); |
|
180
|
|
|
if ($id = $reflection->getConstant('ID')) { |
|
181
|
|
|
$this->addons[] = $id; |
|
182
|
|
|
$this->bind($id, function () use ($addon) { |
|
183
|
|
|
return $addon; |
|
184
|
|
|
}); |
|
185
|
|
|
$addon->init(); |
|
186
|
|
|
} |
|
187
|
|
|
} catch (\ReflectionException $e) { |
|
188
|
|
|
glsr_log()->error('Attempted to register an invalid addon.'); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param string $view |
|
194
|
|
|
* @return void |
|
195
|
|
|
*/ |
|
196
|
15 |
|
public function render($view, array $data = []) |
|
197
|
|
|
{ |
|
198
|
15 |
|
$view = $this->filterString('render/view', $view, $data); |
|
199
|
15 |
|
$file = $this->filterString('views/file', $this->file($view), $view, $data); |
|
200
|
15 |
|
if (!file_exists($file)) { |
|
201
|
|
|
glsr_log()->error(sprintf('File not found: (%s) %s', $view, $file)); |
|
202
|
|
|
return; |
|
203
|
|
|
} |
|
204
|
15 |
|
$data = $this->filterArray('views/data', $data, $view); |
|
205
|
15 |
|
extract($data); |
|
206
|
15 |
|
include $file; |
|
207
|
15 |
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return void |
|
211
|
|
|
*/ |
|
212
|
|
|
public function storeDefaults() |
|
213
|
|
|
{ |
|
214
|
|
|
if (empty($this->defaults)) { |
|
215
|
|
|
$defaults = $this->make(DefaultsManager::class)->get(); |
|
216
|
|
|
$this->defaults = $this->filterArray('get/defaults', $defaults); |
|
217
|
|
|
} |
|
218
|
|
|
if (empty(get_option(OptionManager::databaseKey()))) { |
|
219
|
|
|
update_option(OptionManager::databaseKey(), $this->defaults); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $file |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
14 |
|
public function themePath($file = '') |
|
228
|
|
|
{ |
|
229
|
14 |
|
return get_stylesheet_directory().'/'.static::ID.'/'.ltrim(trim($file), '/'); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|