forked from Automattic/vip-go-mu-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-cli.php
76 lines (62 loc) · 1.84 KB
/
wp-cli.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
<?php
/**
* Plugin Name: WP-CLI for VIP Go
* Description: Scripts for VIP Go
* Author: Automattic
*/
namespace Automattic\VIP\WP_CLI;
function init_is_ssl_toggle() {
maybe_toggle_is_ssl();
if ( is_multisite() ) {
init_is_ssl_toggle_for_multisite();
}
}
// Any time a blog is switched, we should toggle is_ssl() based on their preferred scheme.
function init_is_ssl_toggle_for_multisite() {
add_action( 'switch_blog', function( $new_blog_id, $prev_blog_id ) {
// Not a strict equality check to match core
if ( $new_blog_id == $prev_blog_id ) {
return;
}
maybe_toggle_is_ssl();
}, 0, 2 ); // run early since this could impact other filters
}
/**
* Fixes is_ssl() for wp-cli requests.
*
* `get_site_url()` will force the scheme to `http` when `$_SEVER['HTTPS']` is not set.
* This can be problematic if the site is always using SSL (i.e. home/siteurl have `https` URLs).
* This function toggles the setting so we get correct URLs generated in the wp-cli context.
*/
function maybe_toggle_is_ssl() {
$is_ssl_siteurl = wp_startswith( get_option( 'siteurl' ), 'https:' );
if ( $is_ssl_siteurl && ! is_ssl() ) {
$_SERVER['HTTPS'] = 'on';
} elseif ( ! $is_ssl_siteurl && is_ssl() ) {
unset( $_SERVER['HTTPS'] );
}
}
/**
* Disable `display_errors` for all wp-cli interactions on production servers.
*
* Warnings and notices can break things like JSON output,
* especially for critical plugins like cron-control.
*
* Only do this on production servers to allow local and sandbox debugging.
*/
function disable_display_errors() {
if ( true !== WPCOM_IS_VIP_ENV ) {
return;
}
if ( true === WPCOM_SANDBOXED ) {
return;
}
ini_set( 'display_errors', 0 );
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
disable_display_errors();
init_is_ssl_toggle();
foreach ( glob( __DIR__ . '/wp-cli/*.php' ) as $command ) {
require( $command );
}
}