Passed
Push — master ( 4562dd...74d0bd )
by Paul
04:12
created

Application::upgrade()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
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_pages';
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 )
85
	{
86 7
		$constant = 'static::'.$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 hasPermission()
135
	{
136
		$isAdmin = $this->isAdmin();
137
		return !$isAdmin || ( $isAdmin && current_user_can( $this->constant( 'CAPABILITY' )));
138
	}
139
140
	/**
141
	 * @return void
142
	 */
143
	public function init()
144
	{
145
		$this->make( Actions::class )->run();
146
		$this->make( Filters::class )->run();
147
	}
148
149
	/**
150
	 * @return bool
151
	 */
152
	public function isAdmin()
153
	{
154
		return is_admin() && !wp_doing_ajax();
155
	}
156
157
	/**
158
	 * @param string $file
159
	 * @return string
160
	 */
161 7
	public function path( $file = '', $realpath = true )
162
	{
163 7
		$path = plugin_dir_path( $this->file );
164 7
		if( !$realpath ) {
165 1
			$path = trailingslashit( WP_PLUGIN_DIR ).basename( dirname( $this->file ));
166
		}
167 7
		$path = trailingslashit( $path ).ltrim( trim( $file ), '/' );
168 7
		return apply_filters( 'site-reviews/path', $path, $file );
169
	}
170
171
	/**
172
	 * @return void
173
	 */
174
	public function registerAddons()
175
	{
176
		do_action( 'site-reviews/addon/register', $this );
177
	}
178
179
	/**
180
	 * @return void
181
	 */
182
	public function registerLanguages()
183
	{
184
		load_plugin_textdomain( static::ID, false,
185
			trailingslashit( plugin_basename( $this->path() ).'/'.$this->languages )
186
		);
187
	}
188
189
	/**
190
	 * @return void
191
	 */
192
	public function registerReviewTypes()
193
	{
194
		$types = apply_filters( 'site-reviews/addon/types', [] );
195
		$this->reviewTypes = wp_parse_args( $types, [
196
			'local' => __( 'Local', 'site-reviews' ),
197
		]);
198
	}
199
200
	/**
201
	 * @param string $view
202
	 * @return void
203
	 */
204 7
	public function render( $view, array $data = [] )
205
	{
206 7
		$view = apply_filters( 'site-reviews/render/view', $view, $data );
207 7
		$file = apply_filters( 'site-reviews/views/file', $this->file( $view ), $view, $data );
208 7
		if( !file_exists( $file )) {
209
			glsr_log()->error( 'File not found: '.$file );
210
			return;
211
		}
212 7
		$data = apply_filters( 'site-reviews/views/data', $data, $view );
213 7
		extract( $data );
214 7
		include $file;
215 7
	}
216
217
	/**
218
	 * @return void
219
	 */
220 7
	public function scheduleCronJob()
221
	{
222 7
		if( wp_next_scheduled( static::CRON_EVENT ))return;
223 7
		wp_schedule_event( time(), 'twicedaily', static::CRON_EVENT );
224 7
	}
225
226
	/**
227
	 * @param string $file
228
	 * @return string
229
	 */
230 7
	public function themePath( $file = '' )
231
	{
232 7
		return get_stylesheet_directory().'/'.static::ID.'/'.ltrim( trim( $file ), '/' );
233
	}
234
235
	/**
236
	 * @return void
237
	 */
238
	public function unscheduleCronJob()
239
	{
240
		wp_unschedule_event( intval( wp_next_scheduled( static::CRON_EVENT )), static::CRON_EVENT );
241
	}
242
243
	/**
244
	 * @return void
245
	 */
246 7
	public function upgrade()
247
	{
248 7
		$this->make( Upgrader::class )->run();
249 7
	}
250
251
	/**
252
	 * @param mixed $upgrader
253
	 * @return void
254
	 * @action upgrader_process_complete
255
	 */
256
	public function upgraded( $upgrader, array $data )
257
	{
258
		if( !array_key_exists( 'plugins', $data )
259
			|| !in_array( plugin_basename( $this->file ), $data['plugins'] )
260
			|| $data['action'] != 'update'
261
			|| $data['type'] != 'plugin'
262
		)return;
263
		$this->upgrade();
264
	}
265
266
	/**
267
	 * @param string $path
268
	 * @return string
269
	 */
270
	public function url( $path = '' )
271
	{
272
		$url = esc_url( plugin_dir_url( $this->file ).ltrim( trim( $path ), '/' ));
273
		return apply_filters( 'site-reviews/url', $url, $path );
274
	}
275
}
276