Passed
Push — master ( b3d616...379d80 )
by Paul
04:39
created

Application   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Test Coverage

Coverage 36.36%

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 212
ccs 32
cts 88
cp 0.3636
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 registerReviewTypes() 0 5 1
A upgrade() 0 3 1
A hasPermission() 0 4 3
A registerLanguages() 0 4 1
A path() 0 3 1
A getDefaults() 0 7 2
A init() 0 4 1
A unscheduleCronJob() 0 3 1
A isAdmin() 0 3 2
A scheduleCronJob() 0 4 2
A upgraded() 0 8 5
A registerAddons() 0 3 1
A url() 0 3 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
		return plugin_dir_path( $this->file ).ltrim( trim( $file ), '/' );
122
	}
123
124
	/**
125
	 * @return void
126
	 */
127
	public function registerAddons()
128
	{
129
		do_action( 'site-reviews/addon/register', $this );
130
	}
131
132
	/**
133
	 * @return void
134
	 */
135
	public function registerLanguages()
136
	{
137
		load_plugin_textdomain( static::ID, false,
138
			trailingslashit( plugin_basename( $this->path() ).'/'.$this->languages )
139
		);
140
	}
141
142
	/**
143
	 * @return void
144
	 */
145
	public function registerReviewTypes()
146
	{
147
		$types = apply_filters( 'site-reviews/addon/types', [] );
148
		$this->reviewTypes = wp_parse_args( $types, [
149
			'local' => __( 'Local', 'site-reviews' ),
150
		]);
151
	}
152
153
	/**
154
	 * @param string $view
155
	 * @return void
156
	 */
157 7
	public function render( $view, array $data = [] )
158
	{
159 7
		$view = apply_filters( 'site-reviews/render/view', $view, $data );
160 7
		$file = '';
161 7
		if( glsr( Helper::class )->startsWith( $view, 'templates/' )) {
162
			$file = str_replace( 'templates/', 'site-reviews/', $view ).'.php';
163
			$file = get_stylesheet_directory().'/'.$file;
164
		}
165 7
		if( !file_exists( $file )) {
166 7
			$file = $this->path( 'views/'.$view.'.php' );
167
		}
168 7
		$file = apply_filters( 'site-reviews/views/file', $file, $view, $data );
169 7
		if( !file_exists( $file )) {
170
			glsr_log()->error( 'File not found: '.$file );
171
			return;
172
		}
173 7
		$data = apply_filters( 'site-reviews/views/data', $data, $view );
174 7
		extract( $data );
175 7
		include $file;
176 7
	}
177
178
	/**
179
	 * @return void
180
	 */
181 7
	public function scheduleCronJob()
182
	{
183 7
		if( wp_next_scheduled( static::CRON_EVENT ))return;
184 7
		wp_schedule_event( time(), 'twicedaily', static::CRON_EVENT );
185 7
	}
186
187
	/**
188
	 * @return void
189
	 */
190
	public function unscheduleCronJob()
191
	{
192
		wp_unschedule_event( intval( wp_next_scheduled( static::CRON_EVENT )), static::CRON_EVENT );
193
	}
194
195
	/**
196
	 * @return void
197
	 */
198 7
	public function upgrade()
199
	{
200 7
		$this->make( Upgrader::class )->run();
201 7
	}
202
203
	/**
204
	 * @param mixed $upgrader
205
	 * @return void
206
	 * @action upgrader_process_complete
207
	 */
208
	public function upgraded( $upgrader, array $data )
209
	{
210
		if( !array_key_exists( 'plugins', $data )
211
			|| !in_array( plugin_basename( $this->file ), $data['plugins'] )
212
			|| $data['action'] != 'update'
213
			|| $data['type'] != 'plugin'
214
		)return;
215
		$this->upgrade();
216
	}
217
218
	/**
219
	 * @param string $path
220
	 * @return string
221
	 */
222
	public function url( $path = '' )
223
	{
224
		return esc_url( plugin_dir_url( $this->file ).ltrim( trim( $path ), '/' ));
225
	}
226
}
227