Passed
Push — develop ( e6e477...3b1736 )
by Paul
03:05
created

Application::environment()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 8
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\Config;
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 $version;
22
23
	/**
24
	 * @return string
25
	 */
26
	public static function prefix()
27
	{
28
		return apply_filters( 'pollux/prefix', self::PREFIX );
29
	}
30
31
	public function __construct()
32
	{
33
		$this->file = realpath( dirname( dirname( __FILE__ )) . '/pollux.php' );
34
		$this->gatekeeper = new GateKeeper( plugin_basename( $this->file ));
35
36
		$data = get_file_data( $this->file, array(
37
			'id' => 'Text Domain',
38
			'name' => 'Plugin Name',
39
			'version' => 'Version',
40
		), 'plugin' );
41
		array_walk( $data, function( $value, $key ) {
42
			$this->$key = $value;
43
		});
44
	}
45
46
	/**
47
	 * The Application entry point
48
	 *
49
	 * @return void
50
	 */
51
	public function init()
52
	{
53
		$this->bootstrap();
54
55
		$basename = plugin_basename( $this->file );
56
		$controller = $this->make( 'Controller' );
57
58
		add_action( 'admin_enqueue_scripts',           array( $controller, 'registerAssets' ));
59
		add_action( 'admin_menu',                      array( $controller, 'registerPage' ));
60
		add_action( 'admin_init',                      array( $controller, 'removeDashboardWidgets' ));
61
		add_action( 'wp_before_admin_bar_render',      array( $controller, 'removeWordPressMenu' ));
62
		add_filter( "plugin_action_links_{$basename}", array( $controller, 'filterPluginLinks' ));
63
		add_filter( 'admin_footer_text',               array( $controller, 'filterWordPressFooter' ));
64
65
		// Disallow indexing of the site on non-production environments
66
		if( !$this->environment( 'production' ) && !is_admin() ) {
67
			add_filter( 'pre_option_blog_public', '__return_zero' );
68
		}
69
	}
70
71
	/**
72
	 * @param string $checkFor
73
	 * @return string|bool
74
	 */
75
	public function environment( $checkFor = '' )
76
	{
77
		$environment = defined( 'WP_ENV' ) ? WP_ENV : 'production';
78
		if( !empty( $checkFor )) {
79
			return $environment == $checkFor;
80
		}
81
		return $environment;
82
	}
83
84
	/**
85
	 * @return void
86
	 */
87
	public function onActivation()
88
	{
89
		if( !get_option( Settings::id() )) {
90
			update_option( Settings::id(), [] );
91
		}
92
	}
93
94
	/**
95
	 * @return void
96
	 */
97
	public function onDeactivation()
98
	{
99
	}
100
101
	/**
102
	 * @param string $file
103
	 * @return string
104
	 */
105
	public function path( $file = '' )
106
	{
107
		return plugin_dir_path( $this->file ) . ltrim( trim( $file ), '/' );
108
	}
109
110
	/**
111
	 * @param string $view
112
	 * @return bool
113
	 */
114
	public function render( $view, array $data = [] )
115
	{
116
		$file = apply_filters( 'pollux/views/file',
117
			$this->path( sprintf( 'views/%s.php', str_replace( '.php', '', $view ))),
118
			$view,
119
			$data
120
		);
121
		if( file_exists( $file )) {
122
			extract( $data );
123
			return include $file;
124
		}
125
		return false;
126
	}
127
128
	/**
129
	 * @param string $path
130
	 * @return string
131
	 */
132
	public function url( $path = '' )
133
	{
134
		return esc_url( plugin_dir_url( $this->file ) . ltrim( trim( $path ), '/' ));
135
	}
136
137
	/**
138
	 * @return void
139
	 */
140
	protected function bootstrap()
141
	{
142
		Facade::clearResolvedInstances();
143
		Facade::setFacadeApplication( $this );
144
		$this->config = ( new Config( $this ))->get();
145
		$this->registerAliases();
146
		$classNames = array(
147
			'MetaBox\MetaBox',
148
			'PostType\Archive',
149
			'PostType\DisablePosts',
150
			'PostType\PostType',
151
			'Settings\Settings',
152
			'Taxonomy\Taxonomy',
153
		);
154
		foreach( $classNames as $className ) {
155
			$this->make( $className )->init();
156
 		}
157
	}
158
159
	/**
160
	 * @return void
161
	 */
162
	protected function registerAliases()
163
	{
164
		$aliases = array(
165
			'ArchiveMeta' => 'GeminiLabs\Pollux\Facades\ArchiveMeta',
166
			'PostMeta' => 'GeminiLabs\Pollux\Facades\PostMeta',
167
			'SiteMeta' => 'GeminiLabs\Pollux\Facades\SiteMeta',
168
		);
169
		AliasLoader::getInstance( $aliases )->register();
170
	}
171
}
172