Passed
Push — master ( 51995c...0bccda )
by Paul
06:07 queued 02:50
created

Application::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
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 $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_init',                      array( $controller, 'removeDashboardWidgets' ));
60
		add_action( 'wp_before_admin_bar_render',      array( $controller, 'removeWordPressMenu' ));
61
		add_filter( "plugin_action_links_{$basename}", array( $controller, 'filterPluginLinks' ));
62
		add_filter( 'admin_footer_text',               array( $controller, 'filterWordPressFooter' ));
63
64
		// Disallow indexing of the site on non-production environments
65
		if( !$this->environment( 'production' ) && !is_admin() ) {
66
			add_filter( 'pre_option_blog_public', '__return_zero' );
67
		}
68
	}
69
70
	/**
71
	 * @param string $checkFor
72
	 * @return string|bool
73
	 */
74
	public function environment( $checkFor = '' )
75
	{
76
		$environment = defined( 'WP_ENV' ) ? WP_ENV : 'production';
77
		if( !empty( $checkFor )) {
78
			return $environment == $checkFor;
79
		}
80
		return $environment;
81
	}
82
83
	/**
84
	 * @return void
85
	 */
86
	public function onActivation()
87
	{
88
	}
89
90
	/**
91
	 * @return void
92
	 */
93
	public function onDeactivation()
94
	{
95
	}
96
97
	/**
98
	 * @param string $file
99
	 * @return string
100
	 */
101
	public function path( $file = '' )
102
	{
103
		return plugin_dir_path( $this->file ) . ltrim( trim( $file ), '/' );
104
	}
105
106
	/**
107
	 * @param string $view
108
	 * @return bool
109
	 */
110
	public function render( $view, array $data = [] )
111
	{
112
		$file = apply_filters( 'pollux/views/file',
113
			$this->path( sprintf( 'views/%s.php', str_replace( '.php', '', $view ))),
114
			$view,
115
			$data
116
		);
117
		if( file_exists( $file )) {
118
			extract( $data );
119
			return include $file;
120
		}
121
		return false;
122
	}
123
124
	/**
125
	 * @param string $path
126
	 * @return string
127
	 */
128
	public function url( $path = '' )
129
	{
130
		return esc_url( plugin_dir_url( $this->file ) . ltrim( trim( $path ), '/' ));
131
	}
132
133
	/**
134
	 * @return void
135
	 */
136
	protected function bootstrap()
137
	{
138
		Facade::clearResolvedInstances();
139
		Facade::setFacadeApplication( $this );
140
		$this->registerAliases();
141
		$this->config = $this->make( ConfigManager::class )->compile();
142
		$classNames = array(
143
			'Config\Config',
144
			'MetaBox\MetaBox',
145
			'PostType\Archive',
146
			'PostType\DisablePosts',
147
			'PostType\PostType',
148
			'Settings\Settings',
149
			'Taxonomy\Taxonomy',
150
		);
151
		foreach( $classNames as $className ) {
152
			$this->make( $className )->init();
153
 		}
154
	}
155
156
	/**
157
	 * @return void
158
	 */
159
	protected function registerAliases()
160
	{
161
		AliasLoader::getInstance( apply_filters( 'pollux/aliases', array(
162
			'ArchiveMeta' => 'GeminiLabs\Pollux\Facades\ArchiveMeta',
163
			'PostMeta' => 'GeminiLabs\Pollux\Facades\PostMeta',
164
			'SiteMeta' => 'GeminiLabs\Pollux\Facades\SiteMeta',
165
		)))->register();
166
	}
167
}
168