Test Failed
Push — develop ( 3e3cf0...6ef91c )
by Paul
03:33
created

Application::loadHooks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 6
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\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
		$this->bootstrap();
56
57
		$basename = plugin_basename( $this->file );
58
		$controller = $this->make( 'Controller' );
59
60
		add_action( 'admin_enqueue_scripts',           array( $controller, 'registerAssets' ));
61
		add_action( 'admin_init',                      array( $controller, 'removeDashboardWidgets' ));
62
		add_action( 'wp_before_admin_bar_render',      array( $controller, 'removeWordPressMenu' ));
63
		add_filter( "plugin_action_links_{$basename}", array( $controller, 'filterPluginLinks' ));
64
		add_filter( 'admin_footer_text',               array( $controller, 'filterWordPressFooter' ));
65
66
		// Disallow indexing of the site on non-production environments
67
		if( !$this->environment( 'production' ) && !is_admin() ) {
68
			add_filter( 'pre_option_blog_public', '__return_zero' );
69
		}
70
	}
71
72
	/**
73
	 * @param string $checkFor
74
	 * @return string|bool
75
	 */
76
	public function environment( $checkFor = '' )
77
	{
78
		$environment = defined( 'WP_ENV' ) ? WP_ENV : 'production';
79
		if( !empty( $checkFor )) {
80
			return $environment == $checkFor;
81
		}
82
		return $environment;
83
	}
84
85
	/**
86
	 * @param string $filename
87
	 * @return string|null
88 14
	 */
89
	public function getFile( $filename )
90 14
	{
91
		$theme = wp_get_theme();
92
		$filename = apply_filters( 'pollux/file', $filename );
93
		$locations = apply_filters( 'pollux/file/locations', [
94
			trailingslashit( trailingslashit( $theme->theme_root ) . $theme->stylesheet ),
95
			trailingslashit( trailingslashit( $theme->theme_root ) . $theme->template ),
96
			trailingslashit( WP_CONTENT_DIR ),
97
			trailingslashit( ABSPATH ),
98
			trailingslashit( dirname( ABSPATH )),
99
			trailingslashit( dirname( dirname( ABSPATH ))),
100
		]);
101
		foreach( (array) $locations as $location ) {
102
			if( !file_exists( $location . $filename ))continue;
103
			return $location . $filename;
104
		}
105
		return null;
106
	}
107
108
	/**
109
	 * @return void
110
	 */
111
	public function onActivation()
112
	{
113
	}
114
115
	/**
116
	 * @return void
117
	 */
118
	public function onDeactivation()
119
	{
120
	}
121
122
	/**
123
	 * @param string $file
124
	 * @return string
125
	 */
126
	public function path( $file = '' )
127
	{
128
		return plugin_dir_path( $this->file ) . ltrim( trim( $file ), '/' );
129
	}
130
131
	/**
132
	 * @param string $view
133
	 * @return bool
134
	 */
135
	public function render( $view, array $data = [] )
136
	{
137
		$file = apply_filters( 'pollux/views/file',
138
			$this->path( sprintf( 'views/%s.php', str_replace( '.php', '', $view ))),
139
			$view,
140
			$data
141
		);
142
		if( file_exists( $file )) {
143
			extract( $data );
144
			return include $file;
145
		}
146
		return false;
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->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->app->getFile( 'pollux-hooks.php' )) {
0 ignored issues
show
Documentation introduced by
The property app does not exist on object<GeminiLabs\Pollux\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
188
			include_once $file;
189
		}
190
	}
191
192
	/**
193
	 * @return void
194
	 */
195
	protected function registerAliases()
196
	{
197
		AliasLoader::getInstance( apply_filters( 'pollux/aliases', array(
198
			'ArchiveMeta' => 'GeminiLabs\Pollux\Facades\ArchiveMeta',
199
			'PostMeta' => 'GeminiLabs\Pollux\Facades\PostMeta',
200
			'SiteMeta' => 'GeminiLabs\Pollux\Facades\SiteMeta',
201
		)))->register();
202
	}
203
}
204