Passed
Push — master ( 6375bf...7ef419 )
by Paul
05:58
created

Application::file()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 1
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 4
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 $file;
25
	public $languages;
26
	public $mceShortcodes = []; //defined elsewhere
27
	public $name;
28
	public $reviewTypes;
29
	public $schemas = []; //defined elsewhere
30
	public $version;
31
32
	public function __construct()
33
	{
34
		static::$instance = $this;
35
		$this->file = realpath( dirname( __DIR__ ).'/'.static::ID.'.php' );
36
		$plugin = get_file_data( $this->file, [
37
			'languages' => 'Domain Path',
38
			'name' => 'Plugin Name',
39
			'version' => 'Version',
40
		], 'plugin' );
41
		array_walk( $plugin, function( $value, $key ) {
42
			$this->$key = $value;
43
		});
44
	}
45
46
	/**
47
	 * @return void
48
	 */
49 7
	public function activate()
50
	{
51 7
		$this->make( DefaultsManager::class )->set();
52 7
		$this->scheduleCronJob();
53 7
		$this->upgrade();
54 7
	}
55
56
	/**
57
	 * @param string $name
58
	 * @return array
59
	 */
60 7
	public function config( $name )
61
	{
62 7
		$configFile = $this->path( 'config/'.$name.'.php' );
63 7
		$config = file_exists( $configFile )
64 7
			? include $configFile
65 7
			: [];
66 7
		return apply_filters( 'site-reviews/config', $config, $name );
67
	}
68
69
	/**
70
	 * @return void
71
	 */
72
	public function deactivate()
73
	{
74
		$this->unscheduleCronJob();
75
	}
76
77
	/**
78
	 * @param string $view
79
	 * @return string
80
	 */
81 7
	public function file( $view )
82
	{
83 7
		$file = '';
84 7
		if( glsr( Helper::class )->startsWith( 'templates/', $view )) {
85 7
			$file = str_replace( 'templates/', 'site-reviews/', $view ).'.php';
86 7
			$file = get_stylesheet_directory().'/'.$file;
87 7
			if( !file_exists( $file )) {
88 7
				$file = $this->path( $view.'.php' );
89
			}
90
		}
91 7
		if( !file_exists( $file )) {
92 1
			$file = $this->path( 'views/'.$view.'.php' );
93
		}
94 7
		return $file;
95
	}
96
97
	/**
98
	 * @return array
99
	 */
100
	public function getDefaults()
101
	{
102
		if( empty( $this->defaults )) {
103
			$this->defaults = $this->make( DefaultsManager::class )->get();
104
			$this->upgrade();
105
		}
106
		return apply_filters( 'site-reviews/get/defaults', $this->defaults );
107
	}
108
109
	/**
110
	 * @return bool
111
	 */
112
	public function hasPermission()
113
	{
114
		$isAdmin = $this->isAdmin();
115
		return !$isAdmin || ( $isAdmin && current_user_can( static::CAPABILITY ));
116
	}
117
118
	/**
119
	 * @return void
120
	 */
121
	public function init()
122
	{
123
		$this->make( Actions::class )->run();
124
		$this->make( Filters::class )->run();
125
	}
126
127
	/**
128
	 * @return bool
129
	 */
130
	public function isAdmin()
131
	{
132
		return is_admin() && !wp_doing_ajax();
133
	}
134
135
	/**
136
	 * @param string $file
137
	 * @return string
138
	 */
139 7
	public function path( $file = '' )
140
	{
141 7
		$path = plugin_dir_path( $this->file ).ltrim( trim( $file ), '/' );
142 7
		return apply_filters( 'site-reviews/path', $path, $file );
143
	}
144
145
	/**
146
	 * @return void
147
	 */
148
	public function registerAddons()
149
	{
150
		do_action( 'site-reviews/addon/register', $this );
151
	}
152
153
	/**
154
	 * @return void
155
	 */
156
	public function registerLanguages()
157
	{
158
		load_plugin_textdomain( static::ID, false,
159
			trailingslashit( plugin_basename( $this->path() ).'/'.$this->languages )
160
		);
161
	}
162
163
	/**
164
	 * @return void
165
	 */
166
	public function registerReviewTypes()
167
	{
168
		$types = apply_filters( 'site-reviews/addon/types', [] );
169
		$this->reviewTypes = wp_parse_args( $types, [
170
			'local' => __( 'Local', 'site-reviews' ),
171
		]);
172
	}
173
174
	/**
175
	 * @param string $view
176
	 * @return void
177
	 */
178 7
	public function render( $view, array $data = [] )
179
	{
180 7
		$view = apply_filters( 'site-reviews/render/view', $view, $data );
181 7
		$file = apply_filters( 'site-reviews/views/file', $this->file( $view ), $view, $data );
182 7
		if( !file_exists( $file )) {
183
			glsr_log()->error( 'File not found: '.$file );
184
			return;
185
		}
186 7
		$data = apply_filters( 'site-reviews/views/data', $data, $view );
187 7
		extract( $data );
188 7
		include $file;
189 7
	}
190
191
	/**
192
	 * @return void
193
	 */
194 7
	public function scheduleCronJob()
195
	{
196 7
		if( wp_next_scheduled( static::CRON_EVENT ))return;
197 7
		wp_schedule_event( time(), 'twicedaily', static::CRON_EVENT );
198 7
	}
199
200
	/**
201
	 * @return void
202
	 */
203
	public function unscheduleCronJob()
204
	{
205
		wp_unschedule_event( intval( wp_next_scheduled( static::CRON_EVENT )), static::CRON_EVENT );
206
	}
207
208
	/**
209
	 * @return void
210
	 */
211 7
	public function upgrade()
212
	{
213 7
		$this->make( Upgrader::class )->run();
214 7
	}
215
216
	/**
217
	 * @param mixed $upgrader
218
	 * @return void
219
	 * @action upgrader_process_complete
220
	 */
221
	public function upgraded( $upgrader, array $data )
222
	{
223
		if( !array_key_exists( 'plugins', $data )
224
			|| !in_array( plugin_basename( $this->file ), $data['plugins'] )
225
			|| $data['action'] != 'update'
226
			|| $data['type'] != 'plugin'
227
		)return;
228
		$this->upgrade();
229
	}
230
231
	/**
232
	 * @param string $path
233
	 * @return string
234
	 */
235
	public function url( $path = '' )
236
	{
237
		$url = esc_url( plugin_dir_url( $this->file ).ltrim( trim( $path ), '/' ));
238
		return apply_filters( 'site-reviews/url', $url, $path );
239
	}
240
}
241