Repository navigation
Expand file tree
/
Copy pathflywp.php
More file actions
240 lines (204 loc) · 6.28 KB
/
Copy pathflywp.php
File metadata and controls
240 lines (204 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Plugin Name: FlyWP
* Plugin URI: https://flywp.com
* Description: Helper plugin for FlyWP
* Version: 1.9.0
* Author: FlyWP
* Author URI: https://flywp.com/?utm_source=wporg&utm_medium=banner&utm_campaign=author-uri
* License: GPL2
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require __DIR__ . '/vendor/autoload.php';
use WeDevs\WpUtils\ContainerTrait;
use WeDevs\WpUtils\HookTrait;
use WeDevs\WpUtils\SingletonTrait;
/**
* Main FlyWP Class.
*
* @var admin FlyWP\Admin
* @var rest FlyWP\Api
* @var fastcgi FlyWP\Fastcgi_Cache
* @var router FlyWP\Router
*
* @class FlyWP
*/
final class FlyWP_Plugin {
use SingletonTrait;
use ContainerTrait;
use HookTrait;
/**
* Plugin version.
*
* @var string
*/
public $version = '1.9.0';
/**
* The reseller's brand, read once per request.
*
* @var FlyWP\Brand|null
*/
private $brand = null;
/**
* Plugin Constructor.
*
* @return void
*/
private function __construct() {
$this->define_constants();
$this->add_action( 'plugins_loaded', 'init_plugin' );
register_activation_hook( __FILE__, [ $this, 'activate' ] );
register_deactivation_hook( __FILE__, [ $this, 'deactivate' ] );
}
/**
* Define plugin constants.
*
* @return void
*/
private function define_constants() {
define( 'FLYWP_VERSION', $this->version );
define( 'FLYWP_PLUGIN_FILE', __FILE__ );
define( 'FLYWP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'FLYWP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'FLYWP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
if ( ! defined( 'FLYWP_API_KEY' ) ) {
define( 'FLYWP_API_KEY', '' );
}
if ( ! defined( 'FLYWP_LOGIN_PUBLIC_KEY' ) ) {
define( 'FLYWP_LOGIN_PUBLIC_KEY', '' );
}
if ( ! defined( 'FLYWP_BRAND' ) ) {
define( 'FLYWP_BRAND', '' );
}
}
/**
* Plugin activation hook.
*
* @return void
*/
public function activate() {
$router = new FlyWP\Router();
$router->register_routes();
flush_rewrite_rules( false );
FlyWP\StaticSite\Bootstrap::install();
}
/**
* Plugin deactivation hook.
*
* @return void
*/
public function deactivate() {
$timestamp = wp_next_scheduled( FlyWP\Api\UpdatesData::CRON_HOOK );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, FlyWP\Api\UpdatesData::CRON_HOOK );
}
}
/**
* Initialize plugin.
*
* @return void
*/
public function init_plugin() {
// Loaded ahead of the API-key gate below, as it does not use the API key. It answers
// one POST path and does nothing on any other request.
new FlyWP\Frontend\MagicLogin();
// Also ahead of the gate: the Plugins list and core's update email read the header anyway.
new FlyWP\Branding();
// Also ahead of the gate: a full disk refuses uploads whether or not the key is set.
new FlyWP\Uploads();
// The static site CLI commands do not use the API key.
FlyWP\StaticSite\Bootstrap::register_cli();
if ( ! $this->has_key() ) {
$this->add_action( 'admin_notices', 'admin_notice' );
return;
}
if ( is_admin() ) {
$this->admin = new FlyWP\Admin();
} else {
$this->frontend = new FlyWP\Frontend();
}
$this->router = new FlyWP\Router();
$this->rest = new FlyWP\Api();
$this->fastcgi = new FlyWP\Fastcgi_Cache();
$this->opcache = new FlyWP\Opcache();
$this->flyapi = new FlyWP\FlyApi();
$this->email = new FlyWP\Email();
$this->optimize = new FlyWP\Optimizations();
$this->litespeed = new FlyWP\Litespeed();
$this->updates_data = new FlyWP\Api\UpdatesData();
$this->static_site = new FlyWP\StaticSite\Bootstrap();
}
/**
* Show admin notice if API key is not set.
*
* @return void
*/
public function admin_notice() {
/* translators: %s: the plugin's name */
$message = sprintf( __( 'Missing %s API key, plugin requires an API key.', 'flywp' ), $this->brand()->name() );
echo '<div class="notice notice-error"><p>' . esc_html( $message ) . '</p></div>';
}
/**
* Check if API key is set.
*
* @return bool
*/
public function has_key() {
return FLYWP_API_KEY !== '';
}
/**
* Get API key.
*
* @return string
*/
public function get_key() {
return FLYWP_API_KEY;
}
/**
* Public key used to verify magic-login tokens.
*
* Set as a constant on classic WordPress. On Bedrock it may only be present in the
* environment, so fall back to `getenv()`.
*
* @return string
*/
public function get_login_public_key() {
if ( FLYWP_LOGIN_PUBLIC_KEY !== '' ) {
return FLYWP_LOGIN_PUBLIC_KEY;
}
$from_env = getenv( 'FLYWP_LOGIN_PUBLIC_KEY' );
return $from_env === false ? '' : $from_env;
}
/**
* The brand of the reseller who hosts this site, if one does. Without one, FlyWP.
*
* Set as a constant on classic WordPress. On Bedrock it may only be present in the
* environment, so fall back to `getenv()`.
*
* @return FlyWP\Brand
*/
public function brand() {
if ( $this->brand === null ) {
$value = FLYWP_BRAND !== '' ? FLYWP_BRAND : getenv( 'FLYWP_BRAND' );
// A Bedrock that loads its .env without `putenv()` has it in `$_ENV` or `$_SERVER` only.
if ( $value === false || $value === '' ) {
$value = isset( $_ENV['FLYWP_BRAND'] ) ? $_ENV['FLYWP_BRAND'] : ( isset( $_SERVER['FLYWP_BRAND'] ) ? sanitize_text_field( wp_unslash( $_SERVER['FLYWP_BRAND'] ) ) : '' );
}
$this->brand = FlyWP\Brand::decode( $value );
}
return $this->brand;
}
}
/**
* Returns the main instance of FlyWP to prevent the need to use globals.
*
* @return FlyWP_Plugin
*/
function flywp() {
return FlyWP_Plugin::instance();
}
// take off
flywp();