-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe-core.php
408 lines (310 loc) · 10.1 KB
/
frame-core.php
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/*
Plugin Name: F / R / A / M / E / Core
Plugin URI: https://framecreative.com.au
Version: 2.0.4
Author: Frame
Author URI: https://framecreative.com.au
Description: Tools & Helpers to take WordPress to the next level. Works best on Frame Servers, and in projects built using Frame's WP-Boilerplate.
Bitbucket Plugin URI: https://github.com/framecreative/frame-core-wordpress-plugin
Bitbucket Branch: master
*/
class Frame_Core
{
/**
* @var string
*/
public $dir;
/**
* @var string
*/
public $uri;
/**
* @var FC_Password_Protected
*/
public $password_protect;
public $dev_user;
public $is_dev_user;
public $is_code_managed;
public $is_site_maintained;
public $is_live_env;
/**
* The single instance of the class
*/
protected static $_instance = null;
/**
* Init
*/
public function __construct()
{
self::$_instance = $this;
/**
* Useful...
*/
$this->dir = plugin_dir_path(__FILE__);
$this->uri = plugins_url('', __FILE__);
/*
* Disable automatic updates these should be managed through git
*/
if (! defined('WP_AUTO_UPDATE_CORE')) {
define('WP_AUTO_UPDATE_CORE', 'minor');
}
if (! defined('DISALLOW_FILE_EDIT')) {
define('DISALLOW_FILE_EDIT', true);
}
$this->is_live_env = ( self::env() === 'live' );
$this->is_code_managed = self::config('FC_CODE_MANAGED', true);
$this->is_site_maintained = self::config('FC_SITE_MAINTAINED', false);
$this->dev_user = self::config('FC_DEV_USER', 'frame');
add_action('plugins_loaded', [ $this, 'check_for_dev_user' ] );
add_action('init', [ $this, 'prevent_robots' ] );
add_filter('user_has_cap', [ $this, 'modify_user_capabilities' ], 10, 3);
add_action('admin_notices', [ $this, 'admin_notices' ]);
add_action('admin_menu', [ $this,'admin_remove_menu_pages'], 999);
add_action('template_redirect', [ $this, 'force_url']);
add_action('customize_register', [$this, 'prefix_remove_css_section'], 15);
add_filter('redirect_canonical', [$this, 'prevent_author_enum'], 10, 2 );
$this->remove_headers();
$this->load_components();
}
// Remove the additional CSS section
public function prefix_remove_css_section($wp_customize) {
$wp_customize->remove_section('custom_css');
}
/**
* Include components
*/
public function load_components()
{
require_once $this->dir . 'components/password-protect.php';
require_once $this->dir . 'components/env-tag.php';
require_once $this->dir . 'components/disable-emojis.php';
require_once $this->dir . 'components/proxy-uploads.php';
require_once $this->dir . 'components/smtp.php';
require_once $this->dir . 'components/helpers.php';
require_once $this->dir . 'components/google-tag-manager.php';
require_once $this->dir . 'components/content-freeze.php';
require_once $this->dir . 'components/conditional-plugin-loading.php';
require_once $this->dir . 'components/login-screen.php';
require_once $this->dir . 'components/hide-site-health.php';
require_once $this->dir . 'components/rollbar-error-logging.php';
if ( is_admin() ) {
require_once $this->dir . 'components/disable-admin-nags.php';
new FC_Disable_Admin_Nags();
}
$this->password_protect = new FC_Password_Protected();
new FC_Env_Tag();
new FC_Proxy_Uploads();
new FC_SMTP();
new FC_Google_Tag_Manager();
new FC_Content_Freeze();
new FC_Login_Screen();
new FC_Conditional_Plugin_Loading();
if ( ! $this->is_dev_user ){
new FC_Hide_Site_Health();
}
if ( self::env() !== 'dev' ){
new FC_Rollbar_Error_Logging();
}
}
public function check_for_dev_user()
{
if (WP_ENV == 'dev') {
// all are devs in the dev environment
$this->is_dev_user = true;
} else {
$this->is_dev_user = ($this->dev_user == wp_get_current_user()->user_login);
}
}
public function prevent_robots()
{
if ( $this->is_live_env && ! self::is_staging_domain() ) return;
add_action( 'send_headers', function(){
header("X-Robots-Tag: noindex", true);
} );
add_action( 'wp_head', function(){
echo '<!-- NoIndex Added by Frame Core MU Plugin -->';
echo '<meta name="robots" content="noindex">';
}, 99 );
add_filter( 'robots_txt', function(){
$output = "User-agent: *\n";
$output .= "Disallow: /\n";
return $output;
} );
}
public function modify_user_capabilities($allcaps)
{
if ($this->is_dev_user || !is_admin()) {
return $allcaps;
}
if ($this->is_code_managed || $this->is_site_maintained) {
$allcaps['install_themes'] = false;
$allcaps['switch_themes'] = false;
$allcaps['install_plugins'] = false;
$allcaps['delete_plugins'] = false;
}
if ($this->is_site_maintained) {
$allcaps['update_plugins'] = false;
$allcaps['update_core'] = false;
$allcaps['update_themes'] = false;
}
return $allcaps;
}
public function admin_notices()
{
if ($this->is_dev_user || get_current_screen()->id != 'plugins') {
return;
}
if ($this->is_code_managed || $this->is_site_maintained) {
?>
<div class="notice notice-warning">
<p>
<strong>Plugin Installation Disabled</strong> - Dependencies for this site are version controlled.
Please contact Frame to discuss new functionality so that the correct process can be followed.
</p>
</div>
<?php
}
}
/**
* Clean up admin menu
*/
public function admin_remove_menu_pages()
{
if (!$this->is_dev_user) {
// Hide ACF
remove_menu_page('edit.php?post_type=acf-field-group');
}
}
/**
* Force site URL based on constants
*/
public function force_url()
{
$forceDomain = $this->get_configuration_value('FC_FORCE_DOMAIN', false);
$forceSSL = $this->get_configuration_value('FC_FORCE_SSL', false);
$preferSSL = $this->get_configuration_value('FC_PREFER_SSL', false);
if (! $forceDomain) {
return;
}
if ($_SERVER['HTTP_HOST'] != $forceDomain) {
$ssl = $forceSSL || $preferSSL || is_ssl();
$url = 'http' . ($ssl ? 's' : '') . '://' . $forceDomain . $_SERVER['REQUEST_URI'];
wp_redirect(esc_url($url), 301);
exit();
}
if ($forceSSL && ! is_ssl()) {
wp_redirect(esc_url("https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]), 301);
exit();
}
}
/**
* Left for backwards compatibility, use FrameCore::config()
* @deprecated 1.10.0
*
* */
public function get_configuration_value($name, $default = null)
{
return self::config( $name, $default );
}
/**
* Get a configuration value from a constant or .env file
*
* New static method for better flexibility
*
* @since 1.10.0
* @param string $name
* @param string|int|bool|null $default
* @return string|int|bool|null
*/
public static function config( $name, $default = null )
{
if (defined($name)) {
return constant($name);
}
if ($envValue = getenv($name)) {
switch ($envValue) {
case 'true':
return true;
case 'false':
return false;
default:
return $envValue;
}
}
return $default;
}
public function remove_headers()
{
remove_action('wp_head', 'wp_generator');
// Hides version of Yoast if premium
add_filter('wpseo_hide_version', '__return_true');
}
/**
* Get's the env and allows for some flexibility
* @deprecated 1.10.0
* @return string|null
*/
public function get_current_environment(){
return self::env();
}
/**
* Get's the env and allows for some flexibility
*
* New Static Method, allows for mre use in other places.
*
* @since 1.10.0
* @return string|null
*/
public static function env(){
if ( ! defined( 'WP_ENV') ){
$environment = self::config( 'WP_ENV', null );
define( 'WP_ENV', $environment );
}
/* Synonyms for the 3 ENV we use */
$env_names = [
'dev' => 'dev',
'local' => 'dev',
'staging' => 'staging',
'uat' => 'staging',
'feature' => 'staging',
'live' => 'live',
'production' => 'live',
];
$env_names = apply_filters( 'frame/core/env_names', $env_names );
if ( array_key_exists( WP_ENV, $env_names) ) return $env_names[ WP_ENV ];
return WP_ENV;
}
static function is_staging_domain(){
$staging_domains = apply_filters( 'frame/core/staging_domains', [ 'frmdv.com, frame.hosting' ] );
$staging_prefixes = apply_filters( 'frame/core/staging_prefixes', [ 'dev', 'staging', 'uat', ] );
$httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
if ( ! $httpHost ) return false;
if ( stristr( join( ' ', $staging_domains), $httpHost ) ) return true;
$domain_pieces = (array) explode( '.', $httpHost );
if ( empty( $domain_pieces ) ) return false;
return !! array_search( $domain_pieces[0], $staging_prefixes );
}
public function prevent_author_enum( $redirect, $request ) {
if ( is_admin() )
return $redirect;
if ( preg_match('/\?author=([0-9]*)(\/*)/i', $request) )
return false;
return $redirect;
}
/**
* @return Frame_Core
*/
public static function instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
}
function FC()
{
return Frame_Core::instance();
}
FC();