-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Application.php
372 lines (342 loc) · 9.53 KB
/
Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
namespace GeminiLabs\SiteReviews;
use GeminiLabs\SiteReviews\Database\DefaultsManager;
use GeminiLabs\SiteReviews\Helpers\Arr;
use GeminiLabs\SiteReviews\Helpers\Str;
/**
* @property string $capability
* @property string $cron_event
* @property string $id
* @property string $paged_query_var
* @property string $post_type
* @property string $prefix
* @property string $taxonomy
*/
final class Application extends Container
{
const CAPABILITY = 'edit_others_posts';
const CRON_EVENT = 'site-reviews/schedule/session/purge';
const ID = 'site-reviews';
const PAGED_QUERY_VAR = 'reviews-page';
const POST_TYPE = 'site-review';
const PREFIX = 'glsr_';
const TAXONOMY = 'site-review-category';
public $addons = [];
public $defaults;
public $deprecated = [];
public $file;
public $languages;
public $mceShortcodes = []; //defined elsewhere
public $name;
public $postTypeColumns = []; // defined elsewhere
public $reviewTypes;
public $schemas = []; //defined elsewhere
public $version;
public function __construct()
{
static::$instance = $this;
$this->file = realpath(trailingslashit(dirname(__DIR__)).static::ID.'.php');
$plugin = get_file_data($this->file, [
'languages' => 'Domain Path',
'name' => 'Plugin Name',
'version' => 'Version',
], 'plugin');
array_walk($plugin, function ($value, $key) {
$this->$key = $value;
});
}
/**
* @return void
*/
public function activate()
{
$this->scheduleCronJob();
add_option(static::PREFIX.'activated', true);
}
/**
* @param string $view
* @return string
*/
public function build($view, array $data = [])
{
ob_start();
$this->render($view, $data);
return ob_get_clean();
}
/**
* @param string $capability
* @return bool
*/
public function can($capability)
{
return $this->make(Role::class)->can($capability);
}
/**
* @return void
*/
public function catchFatalError()
{
$error = error_get_last();
if (E_ERROR !== Arr::get($error, 'type') || !Str::contains(Arr::get($error, 'message'), $this->path())) {
return;
}
glsr_log()->error($error['message']);
}
/**
* @param string $name
* @return array
*/
public function config($name)
{
$configFile = $this->path('config/'.$name.'.php');
$config = file_exists($configFile)
? include $configFile
: [];
return apply_filters('site-reviews/config/'.$name, $config);
}
/**
* @param string $property
* @return string
*/
public function constant($property, $className = 'static')
{
$constant = $className.'::'.$property;
return defined($constant)
? apply_filters('site-reviews/const/'.$property, constant($constant))
: '';
}
/**
* @return void
*/
public function deactivate()
{
$this->unscheduleCronJob();
}
/**
* @param string $view
* @return void|string
*/
public function file($view)
{
$view.= '.php';
$filePaths = [];
if (Str::startsWith('templates/', $view)) {
$filePaths[] = $this->themePath(Str::removePrefix('templates/', $view));
}
$filePaths[] = $this->path($view);
$filePaths[] = $this->path('views/'.$view);
foreach ($filePaths as $file) {
if (!file_exists($file)) {
continue;
}
return $file;
}
}
/**
* @return array
*/
public function getDefaults()
{
if (empty($this->defaults)) {
$this->defaults = $this->make(DefaultsManager::class)->get();
}
return apply_filters('site-reviews/get/defaults', $this->defaults);
}
/**
* @param string $page
* @param string $tab
* @return string
*/
public function getPermission($page = '', $tab = 'index')
{
$fallback = 'edit_posts';
$permissions = [
'addons' => 'install_plugins',
'documentation' => [
'faq' => 'edit_others_posts',
'functions' => 'manage_options',
'hooks' => 'edit_others_posts',
'index' => 'edit_posts',
'support' => 'edit_others_posts',
],
'settings' => 'manage_options',
'tools' => [
'console' => 'edit_others_posts',
'general' => 'edit_others_posts',
'index' => 'edit_others_posts',
'sync' => 'manage_options',
'system-info' => 'edit_others_posts',
]
];
$permission = Arr::get($permissions, $page, $fallback);
if (is_array($permission)) {
$permission = Arr::get($permission, $tab, $fallback);
}
return empty($permission) || !is_string($permission)
? $fallback
: $permission;
}
/**
* @param string $page
* @param string $tab
* @return bool
*/
public function hasPermission($page = '', $tab = 'index')
{
$isAdmin = $this->isAdmin();
return !$isAdmin || ($isAdmin && $this->can($this->getPermission($page, $tab)));
}
/**
* @return void
*/
public function init()
{
$this->make(Actions::class)->run();
$this->make(Filters::class)->run();
}
/**
* @return bool
*/
public function isAdmin()
{
return is_admin() && !wp_doing_ajax();
}
/**
* @param string $file
* @return string
*/
public function path($file = '', $realpath = true)
{
$path = plugin_dir_path($this->file);
if (!$realpath) {
$path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file));
}
$path = trailingslashit($path).ltrim(trim($file), '/');
return apply_filters('site-reviews/path', $path, $file);
}
/**
* @param object $addon
* @return void
*/
public function register($addon)
{
try {
$reflection = new \ReflectionClass($addon);
if ($id = $reflection->getConstant('ID')) {
$this->addons[] = $id;
$this->bind($id, $addon);
$addon->init();
}
} catch(\ReflectionException $e) {
glsr_log()->error('Attempted to register an invalid addon.');
}
}
/**
* @return void
*/
public function registerAddons()
{
do_action('site-reviews/addon/register', $this);
}
/**
* @return void
*/
public function registerLanguages()
{
load_plugin_textdomain(static::ID, false,
trailingslashit(plugin_basename($this->path()).'/'.$this->languages)
);
}
/**
* @return void
*/
public function registerReviewTypes()
{
$types = apply_filters('site-reviews/addon/types', []);
$this->reviewTypes = wp_parse_args($types, [
'local' => __('Local', 'site-reviews'),
]);
}
/**
* @param string $view
* @return void
*/
public function render($view, array $data = [])
{
$view = apply_filters('site-reviews/render/view', $view, $data);
$file = apply_filters('site-reviews/views/file', $this->file($view), $view, $data);
if (!file_exists($file)) {
glsr_log()->error('File not found: '.$file);
return;
}
$data = apply_filters('site-reviews/views/data', $data, $view);
extract($data);
include $file;
}
/**
* @return void
*/
public function scheduleCronJob()
{
if (false === wp_next_scheduled(static::CRON_EVENT)) {
wp_schedule_event(time(), 'twicedaily', static::CRON_EVENT);
}
}
/**
* @return void
*/
public function setDefaults()
{
if (get_option(static::PREFIX.'activated')) {
$this->make(DefaultsManager::class)->set();
delete_option(static::PREFIX.'activated');
}
}
/**
* @param string $file
* @return string
*/
public function themePath($file = '')
{
return get_stylesheet_directory().'/'.static::ID.'/'.ltrim(trim($file), '/');
}
/**
* @return void
*/
public function unscheduleCronJob()
{
wp_unschedule_event(intval(wp_next_scheduled(static::CRON_EVENT)), static::CRON_EVENT);
}
/**
* @param string $path
* @return string
*/
public function url($path = '')
{
$url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/'));
return apply_filters('site-reviews/url', $url, $path);
}
/**
* @param string $versionLevel
* @return string
*/
public function version($versionLevel = '')
{
$pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i';
preg_match($pattern, $this->version, $matches);
switch ($versionLevel) {
case 'major':
$version = Arr::get($matches, 1);
break;
case 'minor':
$version = Arr::get($matches, 1).Arr::get($matches, 2);
break;
case 'patch':
$version = Arr::get($matches, 1).Arr::get($matches, 2).Arr::get($matches, 3);
break;
}
return empty($version)
? $this->version
: $version;
}
}