Passed
Push — master ( bd5512...75cbf8 )
by Paul
04:33
created

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