Passed
Push — master ( 58283b...9d21ea )
by Paul
02:29
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use GeminiLabs\Pollux\AliasLoader;
6
use GeminiLabs\Pollux\Config;
7
use GeminiLabs\Pollux\Container;
8
use GeminiLabs\Pollux\Controller;
9
use GeminiLabs\Pollux\Facade;
10
use GeminiLabs\Pollux\GateKeeper;
11
use GeminiLabs\Pollux\PostType;
12
13
final class Application extends Container
14
{
15
	const PREFIX = 'pollux_';
16
17
	public $config;
18
	public $file;
19
	public $gatekeeper;
20
	public $id;
21
	public $name;
22
	public $version;
23
24
	public function __construct()
25
	{
26
		$this->file = realpath( dirname( dirname( __FILE__ )) . '/pollux.php' );
27
		$this->gatekeeper = new GateKeeper( plugin_basename( $this->file ));
28
29
		$data = get_file_data( $this->file, array(
30
			'id' => 'Text Domain',
31
			'name' => 'Plugin Name',
32
			'version' => 'Version',
33
		), 'plugin' );
34
		array_walk( $data, function( $value, $key ) {
35
			$this->$key = $value;
36
		});
37
	}
38
39
	/**
40
	 * @return void
41
	 */
42
	public function bootstrap()
43
	{
44
		Facade::clearResolvedInstances();
45
		Facade::setFacadeApplication( $this );
46
		$this->config = (new Config( $this ))->get();
47
		$this->registerAliases();
48
		$classNames = array(
49
			'MetaBox', 'PostType', 'Taxonomy', 'Settings',
50
		);
51
		foreach( $classNames as $className ) {
52
			$this->make( $className )->init();
53
 		}
54
	}
55
56
	/**
57
	 * @param string $name
58
	 * @param string $path
59
	 * @return string
60
	 */
61
	public function buildClassName( $name, $path = '' )
62
	{
63
		$className = array_map( 'ucfirst', array_map( 'strtolower', preg_split( '/[-_]/', $name )));
64
		$className = implode( '', $className );
65
		return !empty( $path )
66
			? str_replace( '\\\\', '\\', sprintf( '%s\%s', $path, $className ))
67
			: $className;
68
	}
69
70
	/**
71
	 * @param string $name
72
	 * @param string $prefix
73
	 * @return string
74
	 */
75
	public function buildMethodName( $name, $prefix = 'get' )
76
	{
77
		return lcfirst( $this->buildClassName( $prefix . '-' . $name ));
78
	}
79
80
	/**
81
	 * @param string $checkFor
82
	 * @return string|bool
83
	 */
84
	public function environment( $checkFor = '' )
85
	{
86
		$environment = defined( 'WP_ENV' ) ? WP_ENV : 'production';
87
		if( !empty( $checkFor )) {
88
			return $environment == $checkFor;
89
		}
90
		return $environment;
91
	}
92
93
	/**
94
	 * The Application entry point
95
	 *
96
	 * @return void
97
	 */
98
	public function init()
99
	{
100
		$this->bootstrap();
101
102
		$controller = $this->make( 'Controller' );
103
104
		add_filter( 'admin_footer_text',          array( $controller, 'filterWordPressFooter' ));
105
		add_action( 'admin_enqueue_scripts',      array( $controller, 'registerAssets' ));
106
		add_action( 'admin_init',                 array( $controller, 'removeDashboardWidgets' ));
107
		add_action( 'wp_before_admin_bar_render', array( $controller, 'removeWordPressMenu' ));
108
109
		// Disallow indexing of the site on non-production environments
110
		if( !$this->environment( 'production' ) && !is_admin() ) {
111
			add_filter( 'pre_option_blog_public', '__return_zero' );
112
		}
113
	}
114
115
	/**
116
	 * @return void
117
	 */
118
	public function onActivation()
119
	{
120
		$settings = get_option( Settings::ID );
121
		if( !$settings ) {
122
			update_option( Settings::ID, [] );
123
		}
124
	}
125
126
	/**
127
	 * @return void
128
	 */
129
	public function onDeactivation()
130
	{
131
	}
132
133
	/**
134
	 * @return void
135
	 */
136
	public function registerAliases()
137
	{
138
		$aliases = array(
139
			'PostMeta' => 'GeminiLabs\Pollux\Facades\PostMeta',
140
			'SiteMeta' => 'GeminiLabs\Pollux\Facades\PostMeta',
141
		);
142
		AliasLoader::getInstance( $aliases )->register();
143
	}
144
145
	/**
146
	 * @param string $file
147
	 * @return string
148
	 */
149
	public function path( $file = '' )
150
	{
151
		return plugin_dir_path( $this->file ) . ltrim( trim( $file ), '/' );
152
	}
153
154
	/**
155
	 * get_current_screen() is unreliable because it is defined on most admin pages, but not all.
156
	 * @return WP_Screen|null
157
	 */
158
	public function screen()
159
	{
160
		global $current_screen;
161
		return isset( $current_screen ) ? $current_screen : (object) [
162
			'base' => '',
163
			'id' => '',
164
		];
165
	}
166
167
	/**
168
	 * @param string $path
169
	 * @return string
170
	 */
171
	public function url( $path = '' )
172
	{
173
		return esc_url( plugin_dir_url( $this->file ) . ltrim( trim( $path ), '/' ));
174
	}
175
}
176