Application::bootstrap()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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