-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgeneral.php
224 lines (207 loc) · 6.44 KB
/
general.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
<?php
namespace Pantheon\Checks;
use Pantheon\Utils;
use Pantheon\Checkimplementation;
use Pantheon\Messenger;
use Pantheon\View;
use WP_CLI;
class General extends Checkimplementation {
public function init() {
$this->name = 'general';
$this->action = 'No action required';
$this->description = 'Checking for WordPress best practice';
$this->score = 0;
$this->result = '';
$this->label = 'Best practice';
$this->alerts = array();
self::$instance = $this;
return $this;
}
public function run() {
$this->checkDebug();
$this->checkCaching();
$this->checkPluginCount();
$this->checkUrls();
$this->checkRegisteredDomains();
$this->checkCoreUpdates();
}
public function checkCaching() {
if (\is_plugin_active('w3-total-cache/w3-total-cache.php')) {
$this->alerts[] = array(
'code' => 2,
'class' => 'warning',
'message' => 'W3 Total Cache plugin found. This plugin is not needed on Pantheon and should be removed.',
);
} else {
$this->alerts[] = array(
'code' => 0,
'class' => 'ok',
'message' => 'W3 Total Cache not found.',
);
}
if (\is_plugin_active('wp-super-cache/wp-cache.php')) {
$this->alerts[] = array(
'code' => 2,
'class' => 'warning',
'message' => 'WP Super Cache plugin found. This plugin is not needed on Pantheon and should be removed.',
);
} else {
$this->alerts[] = array(
'code' => 0,
'class' => 'ok',
'message' => 'WP Super Cache not found.',
);
}
}
public function checkURLS() {
$siteurl = \get_option('siteurl');
$home = \get_option('home');
if ( $siteurl !== $home ) {
$this->alerts[] = array(
'code' => 2,
'class' => 'error',
'message' => "Site url and home settings do not match. ( 'siteurl'=$siteurl and 'home'=>$home )",
);
} else {
$this->alerts[] = array(
'code' => 0,
'class' => 'ok',
'message' => "Site and home url settings match. ( $siteurl )",
);
}
}
public function checkPluginCount() {
$active = get_option('active_plugins');
$plugins = count($active);
if ( 100 <= $plugins ) {
$this->alerts[] = array(
'code' => 1,
'class' => 'warning',
'message' => sprintf('%d active plugins found. You are running more than 100 plugins. The more plugins you run the worse your performance will be. You should uninstall any plugin that is not necessary.', $plugins),
);
} else {
$this->alerts[] = array(
'code' => 0,
'class' => 'ok',
'message' => sprintf('%d active plugins found.',$plugins),
);
}
}
public function checkDebug() {
if (defined('WP_DEBUG') AND WP_DEBUG ) {
if (getenv('PANTHEON_ENVIRONMENT') AND 'live' === getenv('PANTHEON_ENVIRONMENT')) {
$this->alerts[] = array(
'code' => 1,
'class' => 'warning',
'message' => 'The WP_DEBUG constant is set. You should not run debug mode in production.',
);
} else {
$this->alerts[] = array(
'code' => 0,
'class' => 'ok',
'message' => 'The WP_DEBUG constant is set. You should remove this before deploying to live.',
);
}
} else {
$this->alerts[] = array(
'code' => 0,
'class' => 'ok',
'message' => 'WP_DEBUG not found or is set to false.',
);
}
if (!function_exists('is_plugin_active')) {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
if (\is_plugin_active('debug-bar/debug-bar.php')) {
if (getenv('PANTHEON_ENVIRONMENT') AND 'live' === getenv('PANTHEON_ENVIRONMENT')) {
$this->alerts[] = array(
'code' => 1,
'class' => 'warning',
'message' => 'Looks like you are running the debug bar plugin. You should disable this plugin in the live environment'
);
}
}
}
public function checkRegisteredDomains() {
if ( ! is_multisite() || ! function_exists( 'pantheon_curl' ) || empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
return;
}
$bits = parse_url( 'https://api.live.getpantheon.com:8443/sites/self/state' );
$response = pantheon_curl( sprintf( '%s://%s%s', $bits['scheme'], $bits['host'], $bits['path'] ), null, $bits['port'] );
$body = ! empty( $response['body'] ) ? json_decode( $response['body'], true ) : '';
$pantheon_domains = ! empty( $body['environments'][ $_ENV['PANTHEON_ENVIRONMENT'] ]['urls'] ) ? $body['environments'][ $_ENV['PANTHEON_ENVIRONMENT'] ]['urls'] : array();
$site_domains = array();
$it = new \WP_CLI\Iterators\Table( array(
'table' => $GLOBALS['wpdb']->blogs,
) );
foreach( $it as $blog ) {
$site_domains[] = parse_url( get_site_url( $blog->blog_id ), PHP_URL_HOST );
}
if ( $diff = array_diff( $site_domains, $pantheon_domains ) ) {
$this->alerts[] = array(
'code' => 1,
'class' => 'warning',
'message' => 'One or more WordPress domains are not registered as Pantheon domains: ' . implode( ', ', $diff ),
);
} else {
$this->alerts[] = array(
'code' => 0,
'class' => 'info',
'message' => 'WordPress domains are verified to be in sync with Pantheon domains.'
);
}
}
public function message(Messenger $messenger) {
if (!empty($this->alerts)) {
$total = 0;
$rows = array();
// this is dumb and left over from the previous iterationg. @TODO move scoring to run() method
foreach ($this->alerts as $alert) {
$total += $alert['code'];
$rows[] = $alert;
}
$avg = $total/count($this->alerts);
$this->result = View::make('checklist', array('rows'=> $rows) );
$this->score = $avg;
$this->action = $this->action ?? "You should use object caching";
}
$messenger->addMessage(get_object_vars($this));
}
public function checkCoreUpdates() {
$updates = WP_CLI::runcommand( 'core check-update --format=json', array( 'return' => true, 'parse' => 'json' ) );
$has_minor = $has_major = FALSE;
foreach ($updates as $update) {
switch ($update['update_type']) {
case 'minor':
$has_minor = TRUE;
break;
case 'major':
$has_major = TRUE;
break;
}
}
if ( $has_minor ) {
$action = "Updating to WordPress' newest minor version is strongly recommended.";
$this->alerts[] = array(
'code' => 2,
'class' => 'error',
'message' => $action,
);
$this->action = $action;
} else if ( $has_major ) {
$action = 'A new major version of WordPress is available for update.';
$this->alerts[] = array(
'code' => 1,
'class' => 'warning',
'message' => $action,
);
$this->action = $action;
} else {
$this->alerts[] = array(
'code' => 0,
'class' => 'ok',
'message' => 'WordPress is at the latest version.',
);
}
}
}