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