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
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->file = realpath( dirname( dirname( __FILE__ )) . '/pollux.php' ); |
26
|
|
|
$this->gatekeeper = new GateKeeper( plugin_basename( $this->file )); |
27
|
|
|
|
28
|
|
|
$data = get_file_data( $this->file, array( |
29
|
|
|
'id' => 'Text Domain', |
30
|
|
|
'name' => 'Plugin Name', |
31
|
|
|
'version' => 'Version', |
32
|
|
|
), 'plugin' ); |
33
|
|
|
array_walk( $data, function( $value, $key ) { |
34
|
|
|
$this->$key = $value; |
35
|
|
|
}); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The Application entry point |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function init() |
44
|
|
|
{ |
45
|
|
|
$this->bootstrap(); |
46
|
|
|
|
47
|
|
|
$controller = $this->make( 'Controller' ); |
48
|
|
|
|
49
|
|
|
add_action( 'admin_enqueue_scripts', array( $controller, 'registerAssets' )); |
50
|
|
|
add_action( 'admin_init', array( $controller, 'removeDashboardWidgets' )); |
51
|
|
|
add_action( 'wp_before_admin_bar_render', array( $controller, 'removeWordPressMenu' )); |
52
|
|
|
add_filter( 'admin_footer_text', array( $controller, 'filterWordPressFooter' )); |
53
|
|
|
|
54
|
|
|
// Disallow indexing of the site on non-production environments |
55
|
|
|
if( !$this->environment( 'production' ) && !is_admin() ) { |
56
|
|
|
add_filter( 'pre_option_blog_public', '__return_zero' ); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function bootstrap() |
64
|
|
|
{ |
65
|
|
|
Facade::clearResolvedInstances(); |
66
|
|
|
Facade::setFacadeApplication( $this ); |
67
|
|
|
$this->config = (new Config( $this ))->get(); |
68
|
|
|
$this->registerAliases(); |
69
|
|
|
$classNames = array( |
70
|
|
|
'MetaBox\MetaBox', 'PostType\PostType', 'Settings\Settings', 'Taxonomy\Taxonomy', |
71
|
|
|
); |
72
|
|
|
foreach( $classNames as $className ) { |
73
|
|
|
$this->make( $className )->init(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $checkFor |
79
|
|
|
* @return string|bool |
80
|
|
|
*/ |
81
|
|
|
public function environment( $checkFor = '' ) |
82
|
|
|
{ |
83
|
|
|
$environment = defined( 'WP_ENV' ) ? WP_ENV : 'production'; |
84
|
|
|
if( !empty( $checkFor )) { |
85
|
|
|
return $environment == $checkFor; |
86
|
|
|
} |
87
|
|
|
return $environment; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function onActivation() |
94
|
|
|
{ |
95
|
|
|
$option = apply_filters( 'pollux/settings/option', Settings::ID ); |
96
|
|
|
$settings = get_option( $option ); |
97
|
|
|
if( !$settings ) { |
98
|
|
|
update_option( $option, [] ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function onDeactivation() |
106
|
|
|
{ |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $file |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function path( $file = '' ) |
114
|
|
|
{ |
115
|
|
|
return plugin_dir_path( $this->file ) . ltrim( trim( $file ), '/' ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function registerAliases() |
122
|
|
|
{ |
123
|
|
|
$aliases = array( |
124
|
|
|
'PostMeta' => 'GeminiLabs\Pollux\Facades\PostMeta', |
125
|
|
|
'SiteMeta' => 'GeminiLabs\Pollux\Facades\SiteMeta', |
126
|
|
|
); |
127
|
|
|
AliasLoader::getInstance( $aliases )->register(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* get_current_screen() is unreliable because it is defined on most admin pages, but not all. |
132
|
|
|
* @return WP_Screen|null |
133
|
|
|
*/ |
134
|
|
|
public function screen() |
135
|
|
|
{ |
136
|
|
|
global $current_screen; |
137
|
|
|
return isset( $current_screen ) ? $current_screen : (object) [ |
138
|
|
|
'base' => '', |
139
|
|
|
'id' => '', |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $path |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function url( $path = '' ) |
148
|
|
|
{ |
149
|
|
|
return esc_url( plugin_dir_url( $this->file ) . ltrim( trim( $path ), '/' )); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|