1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Actions; |
6
|
|
|
use GeminiLabs\SiteReviews\Container; |
7
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
8
|
|
|
use GeminiLabs\SiteReviews\Database\DefaultsManager; |
9
|
|
|
use GeminiLabs\SiteReviews\Filters; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Html; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Upgrader; |
12
|
|
|
|
13
|
|
|
final class Application extends Container |
14
|
|
|
{ |
15
|
|
|
const CAPABILITY = 'edit_others_posts'; |
16
|
|
|
const CRON_EVENT = 'site-reviews/schedule/session/purge'; |
17
|
|
|
const ID = 'site-reviews'; |
18
|
|
|
const PAGED_QUERY_VAR = 'reviews-page'; |
19
|
|
|
const POST_TYPE = 'site-review'; |
20
|
|
|
const PREFIX = 'glsr_'; |
21
|
|
|
const TAXONOMY = 'site-review-category'; |
22
|
|
|
|
23
|
|
|
public $defaults; |
24
|
|
|
public $deprecated = []; |
25
|
|
|
public $file; |
26
|
|
|
public $languages; |
27
|
|
|
public $mceShortcodes = []; //defined elsewhere |
28
|
|
|
public $name; |
29
|
|
|
public $reviewTypes; |
30
|
|
|
public $schemas = []; //defined elsewhere |
31
|
|
|
public $version; |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
static::$instance = $this; |
36
|
|
|
$this->file = realpath( trailingslashit( dirname( __DIR__ )).static::ID.'.php' ); |
37
|
|
|
$plugin = get_file_data( $this->file, [ |
38
|
|
|
'languages' => 'Domain Path', |
39
|
|
|
'name' => 'Plugin Name', |
40
|
|
|
'version' => 'Version', |
41
|
|
|
], 'plugin' ); |
42
|
|
|
array_walk( $plugin, function( $value, $key ) { |
43
|
|
|
$this->$key = $value; |
44
|
|
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
6 |
|
public function activate() |
51
|
|
|
{ |
52
|
6 |
|
$this->make( DefaultsManager::class )->set(); |
53
|
6 |
|
$this->scheduleCronJob(); |
54
|
6 |
|
$this->upgrade(); |
55
|
6 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function catchFatalError() |
61
|
|
|
{ |
62
|
|
|
$error = error_get_last(); |
63
|
|
|
if( $error['type'] !== E_ERROR || strpos( $error['message'], $this->path() ) === false )return; |
64
|
|
|
glsr_log()->error( $error['message'] ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
6 |
|
public function config( $name ) |
72
|
|
|
{ |
73
|
6 |
|
$configFile = $this->path( 'config/'.$name.'.php' ); |
74
|
6 |
|
$config = file_exists( $configFile ) |
75
|
6 |
|
? include $configFile |
76
|
6 |
|
: []; |
77
|
6 |
|
return apply_filters( 'site-reviews/config/'.$name, $config ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $property |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
6 |
|
public function constant( $property, $className = 'static' ) |
85
|
|
|
{ |
86
|
6 |
|
$constant = $className.'::'.$property; |
87
|
6 |
|
return defined( $constant ) |
88
|
6 |
|
? apply_filters( 'site-reviews/const/'.$property, constant( $constant )) |
89
|
6 |
|
: ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function deactivate() |
96
|
|
|
{ |
97
|
|
|
$this->unscheduleCronJob(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $view |
102
|
|
|
* @return void|string |
103
|
|
|
*/ |
104
|
6 |
|
public function file( $view ) |
105
|
|
|
{ |
106
|
6 |
|
$view.= '.php'; |
107
|
6 |
|
$filePaths = []; |
108
|
6 |
|
if( glsr( Helper::class )->startsWith( 'templates/', $view )) { |
109
|
6 |
|
$filePaths[] = $this->themePath( glsr( Helper::class )->removePrefix( 'templates/', $view )); |
110
|
|
|
} |
111
|
6 |
|
$filePaths[] = $this->path( $view ); |
112
|
6 |
|
$filePaths[] = $this->path( 'views/'.$view ); |
113
|
6 |
|
foreach( $filePaths as $file ) { |
114
|
6 |
|
if( !file_exists( $file ))continue; |
115
|
6 |
|
return $file; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getDefaults() |
123
|
|
|
{ |
124
|
|
|
if( empty( $this->defaults )) { |
125
|
|
|
$this->defaults = $this->make( DefaultsManager::class )->get(); |
126
|
|
|
$this->upgrade(); |
127
|
|
|
} |
128
|
|
|
return apply_filters( 'site-reviews/get/defaults', $this->defaults ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function getPermission( $page = '' ) |
135
|
|
|
{ |
136
|
|
|
$permissions = [ |
137
|
|
|
'addons' => 'install_plugins', |
138
|
|
|
'settings' => 'manage_options', |
139
|
|
|
]; |
140
|
|
|
return glsr_get( $permissions, $page, $this->constant( 'CAPABILITY' )); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function hasPermission( $page = '' ) |
147
|
|
|
{ |
148
|
|
|
$isAdmin = $this->isAdmin(); |
149
|
|
|
return !$isAdmin || ( $isAdmin && current_user_can( $this->getPermission( $page ))); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function init() |
156
|
|
|
{ |
157
|
|
|
$this->make( Actions::class )->run(); |
158
|
|
|
$this->make( Filters::class )->run(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function isAdmin() |
165
|
|
|
{ |
166
|
|
|
return is_admin() && !wp_doing_ajax(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $file |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
6 |
|
public function path( $file = '', $realpath = true ) |
174
|
|
|
{ |
175
|
6 |
|
$path = plugin_dir_path( $this->file ); |
176
|
6 |
|
if( !$realpath ) { |
177
|
1 |
|
$path = trailingslashit( WP_PLUGIN_DIR ).basename( dirname( $this->file )); |
178
|
|
|
} |
179
|
6 |
|
$path = trailingslashit( $path ).ltrim( trim( $file ), '/' ); |
180
|
6 |
|
return apply_filters( 'site-reviews/path', $path, $file ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function registerAddons() |
187
|
|
|
{ |
188
|
|
|
do_action( 'site-reviews/addon/register', $this ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return void |
193
|
|
|
*/ |
194
|
|
|
public function registerLanguages() |
195
|
|
|
{ |
196
|
|
|
load_plugin_textdomain( static::ID, false, |
197
|
|
|
trailingslashit( plugin_basename( $this->path() ).'/'.$this->languages ) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function registerReviewTypes() |
205
|
|
|
{ |
206
|
|
|
$types = apply_filters( 'site-reviews/addon/types', [] ); |
207
|
|
|
$this->reviewTypes = wp_parse_args( $types, [ |
208
|
|
|
'local' => __( 'Local', 'site-reviews' ), |
209
|
|
|
]); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $view |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
6 |
|
public function render( $view, array $data = [] ) |
217
|
|
|
{ |
218
|
6 |
|
$view = apply_filters( 'site-reviews/render/view', $view, $data ); |
219
|
6 |
|
$file = apply_filters( 'site-reviews/views/file', $this->file( $view ), $view, $data ); |
220
|
6 |
|
if( !file_exists( $file )) { |
221
|
|
|
glsr_log()->error( 'File not found: '.$file ); |
222
|
|
|
return; |
223
|
|
|
} |
224
|
6 |
|
$data = apply_filters( 'site-reviews/views/data', $data, $view ); |
225
|
6 |
|
extract( $data ); |
226
|
6 |
|
include $file; |
227
|
6 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
6 |
|
public function scheduleCronJob() |
233
|
|
|
{ |
234
|
6 |
|
if( wp_next_scheduled( static::CRON_EVENT ))return; |
235
|
6 |
|
wp_schedule_event( time(), 'twicedaily', static::CRON_EVENT ); |
236
|
6 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $file |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
6 |
|
public function themePath( $file = '' ) |
243
|
|
|
{ |
244
|
6 |
|
return get_stylesheet_directory().'/'.static::ID.'/'.ltrim( trim( $file ), '/' ); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return void |
249
|
|
|
*/ |
250
|
|
|
public function unscheduleCronJob() |
251
|
|
|
{ |
252
|
|
|
wp_unschedule_event( intval( wp_next_scheduled( static::CRON_EVENT )), static::CRON_EVENT ); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return void |
257
|
|
|
*/ |
258
|
6 |
|
public function upgrade() |
259
|
|
|
{ |
260
|
6 |
|
$this->make( Upgrader::class )->run(); |
261
|
6 |
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param mixed $upgrader |
265
|
|
|
* @return void |
266
|
|
|
* @action upgrader_process_complete |
267
|
|
|
*/ |
268
|
|
|
public function upgraded( $upgrader, array $data ) |
269
|
|
|
{ |
270
|
|
|
if( !array_key_exists( 'plugins', $data ) |
271
|
|
|
|| !in_array( plugin_basename( $this->file ), $data['plugins'] ) |
272
|
|
|
|| $data['action'] != 'update' |
273
|
|
|
|| $data['type'] != 'plugin' |
274
|
|
|
)return; |
275
|
|
|
$this->upgrade(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param string $path |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
public function url( $path = '' ) |
283
|
|
|
{ |
284
|
|
|
$url = esc_url( plugin_dir_url( $this->file ).ltrim( trim( $path ), '/' )); |
285
|
|
|
return apply_filters( 'site-reviews/url', $url, $path ); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|