Passed
Push — master ( e8093c...ec23d4 )
by Paul
04:14
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 2
rs 9.9666
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_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( 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 )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
	 * @return void
82
	 */
83
	public function deactivate()
84
	{
85
		$this->unscheduleCronJob();
86
	}
87
88
	/**
89
	 * @param string $view
90
	 * @return string
91
	 */
92 7
	public function file( $view )
93
	{
94 7
		$file = '';
95 7
		if( glsr( Helper::class )->startsWith( 'templates/', $view )) {
96 7
			$file = str_replace( 'templates/', 'site-reviews/', $view ).'.php';
97 7
			$file = get_stylesheet_directory().'/'.$file;
98 7
			if( !file_exists( $file )) {
99 7
				$file = $this->path( $view.'.php' );
100
			}
101
		}
102 7
		if( !file_exists( $file )) {
103 1
			$file = $this->path( 'views/'.$view.'.php' );
104
		}
105 7
		return $file;
106
	}
107
108
	/**
109
	 * @return array
110
	 */
111
	public function getDefaults()
112
	{
113
		if( empty( $this->defaults )) {
114
			$this->defaults = $this->make( DefaultsManager::class )->get();
115
			$this->upgrade();
116
		}
117
		return apply_filters( 'site-reviews/get/defaults', $this->defaults );
118
	}
119
120
	/**
121
	 * @return bool
122
	 */
123
	public function hasPermission()
124
	{
125
		$isAdmin = $this->isAdmin();
126
		return !$isAdmin || ( $isAdmin && current_user_can( static::CAPABILITY ));
127
	}
128
129
	/**
130
	 * @return void
131
	 */
132
	public function init()
133
	{
134
		$this->make( Actions::class )->run();
135
		$this->make( Filters::class )->run();
136
	}
137
138
	/**
139
	 * @return bool
140
	 */
141
	public function isAdmin()
142
	{
143
		return is_admin() && !wp_doing_ajax();
144
	}
145
146
	/**
147
	 * @param string $file
148
	 * @return string
149
	 */
150 7
	public function path( $file = '' )
151
	{
152 7
		$path = plugin_dir_path( $this->file ).ltrim( trim( $file ), '/' );
153 7
		return apply_filters( 'site-reviews/path', $path, $file );
154
	}
155
156
	/**
157
	 * @return void
158
	 */
159
	public function registerAddons()
160
	{
161
		do_action( 'site-reviews/addon/register', $this );
162
	}
163
164
	/**
165
	 * @return void
166
	 */
167
	public function registerLanguages()
168
	{
169
		load_plugin_textdomain( static::ID, false,
170
			trailingslashit( plugin_basename( $this->path() ).'/'.$this->languages )
171
		);
172
	}
173
174
	/**
175
	 * @return void
176
	 */
177
	public function registerReviewTypes()
178
	{
179
		$types = apply_filters( 'site-reviews/addon/types', [] );
180
		$this->reviewTypes = wp_parse_args( $types, [
181
			'local' => __( 'Local', 'site-reviews' ),
182
		]);
183
	}
184
185
	/**
186
	 * @param string $view
187
	 * @return void
188
	 */
189 7
	public function render( $view, array $data = [] )
190
	{
191 7
		$view = apply_filters( 'site-reviews/render/view', $view, $data );
192 7
		$file = apply_filters( 'site-reviews/views/file', $this->file( $view ), $view, $data );
193 7
		if( !file_exists( $file )) {
194
			glsr_log()->error( 'File not found: '.$file );
195
			return;
196
		}
197 7
		$data = apply_filters( 'site-reviews/views/data', $data, $view );
198 7
		extract( $data );
199 7
		include $file;
200 7
	}
201
202
	/**
203
	 * @return void
204
	 */
205 7
	public function scheduleCronJob()
206
	{
207 7
		if( wp_next_scheduled( static::CRON_EVENT ))return;
208 7
		wp_schedule_event( time(), 'twicedaily', static::CRON_EVENT );
209 7
	}
210
211
	/**
212
	 * @return void
213
	 */
214
	public function unscheduleCronJob()
215
	{
216
		wp_unschedule_event( intval( wp_next_scheduled( static::CRON_EVENT )), static::CRON_EVENT );
217
	}
218
219
	/**
220
	 * @return void
221
	 */
222 7
	public function upgrade()
223
	{
224 7
		$this->make( Upgrader::class )->run();
225 7
	}
226
227
	/**
228
	 * @param mixed $upgrader
229
	 * @return void
230
	 * @action upgrader_process_complete
231
	 */
232
	public function upgraded( $upgrader, array $data )
233
	{
234
		if( !array_key_exists( 'plugins', $data )
235
			|| !in_array( plugin_basename( $this->file ), $data['plugins'] )
236
			|| $data['action'] != 'update'
237
			|| $data['type'] != 'plugin'
238
		)return;
239
		$this->upgrade();
240
	}
241
242
	/**
243
	 * @param string $path
244
	 * @return string
245
	 */
246
	public function url( $path = '' )
247
	{
248
		$url = esc_url( plugin_dir_url( $this->file ).ltrim( trim( $path ), '/' ));
249
		return apply_filters( 'site-reviews/url', $url, $path );
250
	}
251
}
252