Passed
Push — master ( 3b6fab...7d2cd0 )
by Paul
04:26
created

Application   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Test Coverage

Coverage 36.67%

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 214
ccs 33
cts 90
cp 0.3667
rs 9.92
c 0
b 0
f 0
wmc 31

18 Methods

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