Completed
Push — develop ( 300ef2...e7ec38 )
by Paul
04:06 queued 42s
created

Application::onDeactivation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use GeminiLabs\Pollux\AliasLoader;
6
use GeminiLabs\Pollux\Config\ConfigManager;
7
use GeminiLabs\Pollux\Container;
8
use GeminiLabs\Pollux\Facade;
9
use GeminiLabs\Pollux\GateKeeper;
10
use GeminiLabs\Pollux\PostType\PostType;
11
12
final class Application extends Container
13
{
14
	const PREFIX = 'pollux_';
15
16
	public $config;
17
	public $file;
18
	public $gatekeeper;
19
	public $id;
20
	public $name;
21
	public $notices = [];
22
	public $version;
23
24
	/**
25
	 * @return string
26
	 */
27
	public static function prefix()
28
	{
29
		return apply_filters( 'pollux/prefix', self::PREFIX );
30
	}
31
32
	public function __construct()
33
	{
34
		$this->file = realpath( dirname( dirname( __FILE__ )) . '/pollux.php' );
35
		$this->gatekeeper = new GateKeeper( plugin_basename( $this->file ));
36
37
		$data = get_file_data( $this->file, array(
38
			'id' => 'Text Domain',
39
			'name' => 'Plugin Name',
40
			'version' => 'Version',
41
		), 'plugin' );
42
43
		foreach( $data as $key => $value ) {
44
			$this->$key = $value;
45
		}
46
	}
47
48
	/**
49
	 * The Application entry point
50
	 *
51
	 * @return void
52
	 */
53
	public function init()
54
	{
55
		$basename = plugin_basename( $this->file );
56
		$controller = $this->make( 'Controller' );
57
58
		add_action( 'plugins_loaded', function() {
59
			$this->bootstrap();
60
		});
61
		add_action( 'admin_enqueue_scripts',           array( $controller, 'registerAssets' ));
62
		add_action( 'admin_init',                      array( $controller, 'removeDashboardWidgets' ));
63
		add_action( 'wp_before_admin_bar_render',      array( $controller, 'removeWordPressMenu' ));
64
		add_filter( "plugin_action_links_{$basename}", array( $controller, 'filterPluginLinks' ));
65
		add_filter( 'admin_footer_text',               array( $controller, 'filterWordPressFooter' ));
66
67
		// Disallow indexing of the site on non-production environments
68
		if( !$this->environment( 'production' ) && !is_admin() ) {
69
			add_filter( 'pre_option_blog_public', '__return_zero' );
70
		}
71
	}
72
73
	/**
74
	 * @param string $checkFor
75
	 * @return string|bool
76
	 */
77
	public function environment( $checkFor = '' )
78
	{
79
		$environment = defined( 'WP_ENV' ) ? WP_ENV : 'production';
80
		if( !empty( $checkFor )) {
81
			return $environment == $checkFor;
82
		}
83
		return $environment;
84
	}
85
86
	/**
87
	 * @param string $filename
88
	 * @return string|null
89
	 */
90
	public function getFile( $filename )
91
	{
92
		$theme = wp_get_theme();
93
		$filename = apply_filters( 'pollux/file', $filename );
94
		$locations = apply_filters( 'pollux/file/locations', [
95
			trailingslashit( trailingslashit( $theme->theme_root ) . $theme->stylesheet ),
96
			trailingslashit( trailingslashit( $theme->theme_root ) . $theme->template ),
97
			trailingslashit( WP_CONTENT_DIR ),
98
			trailingslashit( ABSPATH ),
99
			trailingslashit( dirname( ABSPATH )),
100
			trailingslashit( dirname( dirname( ABSPATH ))),
101
		]);
102
		foreach( (array) $locations as $location ) {
103
			if( !file_exists( $location . $filename ))continue;
104
			return $location . $filename;
105
		}
106
		return null;
107
	}
108
109
	/**
110
	 * @return void
111
	 */
112 14
	public function onActivation()
113
	{
114 14
	}
115
116
	/**
117
	 * @return void
118
	 */
119
	public function onDeactivation()
120
	{
121
	}
122
123
	/**
124
	 * @param string $file
125
	 * @return string
126
	 */
127
	public function path( $file = '' )
128
	{
129
		return plugin_dir_path( $this->file ) . ltrim( trim( $file ), '/' );
130
	}
131
132
	/**
133
	 * @param string $view
134
	 * @return void|null
135
	 */
136
	public function render( $view, array $data = [] )
137
	{
138
		$file = apply_filters( 'pollux/views/file',
139
			$this->path( sprintf( 'views/%s.php', str_replace( '.php', '', $view ))),
140
			$view,
141
			$data
142
		);
143
		if( !file_exists( $file ))return;
144
		extract( $data );
145
		include $file;
146
	}
147
148
	/**
149
	 * @param string $path
150
	 * @return string
151
	 */
152
	public function url( $path = '' )
153
	{
154
		return esc_url( plugin_dir_url( $this->file ) . ltrim( trim( $path ), '/' ));
155
	}
156
157
	/**
158
	 * @return void
159
	 */
160
	protected function bootstrap()
161
	{
162
		Facade::clearResolvedInstances();
163
		Facade::setFacadeApplication( $this );
164
		$this->registerAliases();
165
		$this->loadTextdomain();
166
		$this->loadHooks();
167
		$this->config = $this->make( ConfigManager::class )->compile();
168
		$classNames = array(
169
			'Config\Config',
170
			'MetaBox\MetaBox',
171
			'PostType\Archive',
172
			'PostType\DisablePosts',
173
			'PostType\PostType',
174
			'Settings\Settings',
175
			'Taxonomy\Taxonomy',
176
		);
177
		foreach( $classNames as $className ) {
178
			$this->make( $className )->init();
179
 		}
180
	}
181
182
	/**
183
	 * @return void
184
	 */
185
	protected function loadHooks()
186
	{
187
		if( $file = $this->getFile( 'pollux-hooks.php' )) {
188
			include_once $file;
189
		}
190
	}
191
192
	/**
193
	 * @return void
194
	 */
195
	protected function loadTextdomain()
196
	{
197
		load_plugin_textdomain( $this->id, false, plugin_basename( $this->path() ) . '/languages/' );
198
	}
199
200
	/**
201
	 * @return void
202
	 */
203
	protected function registerAliases()
204
	{
205
		AliasLoader::getInstance( apply_filters( 'pollux/aliases', array(
206
			'ArchiveMeta' => 'GeminiLabs\Pollux\Facades\ArchiveMeta',
207
			'PostMeta' => 'GeminiLabs\Pollux\Facades\PostMeta',
208
			'SiteMeta' => 'GeminiLabs\Pollux\Facades\SiteMeta',
209
		)))->register();
210
	}
211
}
212