Passed
Push — hotfix/fix-counts ( 1543e7...1ce239 )
by Paul
03:48
created

Application::getPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 7
	public function activate()
51
	{
52 7
		$this->make( DefaultsManager::class )->set();
53 7
		$this->scheduleCronJob();
54 7
		$this->upgrade();
55 7
	}
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 7
	public function config( $name )
72
	{
73 7
		$configFile = $this->path( 'config/'.$name.'.php' );
74 7
		$config = file_exists( $configFile )
75 7
			? include $configFile
76 7
			: [];
77 7
		return apply_filters( 'site-reviews/config/'.$name, $config );
78
	}
79
80
	/**
81
	 * @param string $property
82
	 * @return string
83
	 */
84 7
	public function constant( $property, $className = 'static' )
85
	{
86 7
		$constant = $className.'::'.$property;
87 7
		return defined( $constant )
88 7
			? apply_filters( 'site-reviews/const/'.$property, constant( $constant ))
89 7
			: '';
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 7
	public function file( $view )
105
	{
106 7
		$view.= '.php';
107 7
		$filePaths = [];
108 7
		if( glsr( Helper::class )->startsWith( 'templates/', $view )) {
109 7
			$filePaths[] = $this->themePath( glsr( Helper::class )->removePrefix( 'templates/', $view ));
110
		}
111 7
		$filePaths[] = $this->path( $view );
112 7
		$filePaths[] = $this->path( 'views/'.$view );
113 7
		foreach( $filePaths as $file ) {
114 7
			if( !file_exists( $file ))continue;
115 7
			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 7
	public function path( $file = '', $realpath = true )
174
	{
175 7
		$path = plugin_dir_path( $this->file );
176 7
		if( !$realpath ) {
177 1
			$path = trailingslashit( WP_PLUGIN_DIR ).basename( dirname( $this->file ));
178
		}
179 7
		$path = trailingslashit( $path ).ltrim( trim( $file ), '/' );
180 7
		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 7
	public function render( $view, array $data = [] )
217
	{
218 7
		$view = apply_filters( 'site-reviews/render/view', $view, $data );
219 7
		$file = apply_filters( 'site-reviews/views/file', $this->file( $view ), $view, $data );
220 7
		if( !file_exists( $file )) {
221
			glsr_log()->error( 'File not found: '.$file );
222
			return;
223
		}
224 7
		$data = apply_filters( 'site-reviews/views/data', $data, $view );
225 7
		extract( $data );
226 7
		include $file;
227 7
	}
228
229
	/**
230
	 * @return void
231
	 */
232 7
	public function scheduleCronJob()
233
	{
234 7
		if( wp_next_scheduled( static::CRON_EVENT ))return;
235 7
		wp_schedule_event( time(), 'twicedaily', static::CRON_EVENT );
236 7
	}
237
238
	/**
239
	 * @param string $file
240
	 * @return string
241
	 */
242 7
	public function themePath( $file = '' )
243
	{
244 7
		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 7
	public function upgrade()
259
	{
260 7
		$this->make( Upgrader::class )->run();
261 7
	}
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