/
hello-server.php
292 lines (249 loc) · 9.04 KB
/
hello-server.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
<?php
/**
* Plugin Name: Hello Server by Liquid Web
* Plugin URI: https://wordpress.org/plugins/hello-server
* Description: This is a simple plugin that tells you what server your WordPress is running on. It's super useful for when you run WordPress in a clustered, or mulit-server, environment.
* Author: Liquid Web
* Author URI: https://www.liquidweb.com/
* Version: 1.3.1
* License: GPL-2.0
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: lw-hello-server
* Requires WP: 5.0
* Domain Path: languages
* @package Hello_Server
*/
/*
Copyright (c) 2017 Dan Pock (Liquid Web) <dpock@liquidweb.com>
All rights reserved.
Hello Server by Liquid Web is distributed under the GNU General Public License, Version 2,
June 1991. Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin
St, Fifth Floor, Boston, MA 02110, USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Hello Server by Liquid Web
*
* Say hello to your server and maybe it will say it back.
* With this WordPress plugin you can easily see what server your current
* web request was processed on. If you run your WordPress in a cluster style
* setup then this will simplify tracking server requests.
*
* PHP version 7.3
*
* @package Hello_Server
* @version 1.3.1
* @author Dan Pock <dpock@liquidweb.com>
* @copyright 2017 Liquid Web
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0
* @link https://github.com/liquidweb/hello-server
*/
/**
* Load our textdomain for internationalization support.
*
* @return void
*/
function lw_load_plugins_textdomain() {
load_plugin_textdomain( 'lw-hello-server', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'lw_load_plugins_textdomain' );
/**
* Pull our server info for display use.
*
* @return array $info The IP and hostname being displayed.
*/
function lw_hello_server_get_info() {
// Bail if we don't have a server address.
if ( empty( $_SERVER['SERVER_ADDR'] ) ) {
return false;
}
// Set our transient key for use later.
$cache_key = 'hello_server_info_cache_' . sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) );
// If we don't want the cache'd version, delete the transient first.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
delete_transient( $cache_key );
}
// It wasn't there, so regenerate the data and save the transient.
if ( false === $data = get_transient( $cache_key ) ) {
// Call the global WPDB class to get our version.
global $wpdb;
// Get our software name with a fallback.
$svsoft = ! empty( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : __( 'unknown', 'lw-hello-server' );
// Build my PHP version string.
$phpver = absint( PHP_MAJOR_VERSION ) . '.' . absint( PHP_MINOR_VERSION ) . '.' . absint( PHP_RELEASE_VERSION );
// Set up our array of info.
$data = array(
'address' => lw_get_server_ip(),
'software' => $svsoft,
'hostname' => gethostname(),
'phpvers' => $phpver,
'dbvers' => $wpdb->db_version(),
);
// Store our data in the transient.
set_transient( $cache_key, $data, '', DAY_IN_SECONDS );
}
// Return our array of data.
return $data;
}
/**
* Get our server IP address by checking various PHP vars.
*
* @return string
*/
function lw_get_server_ip() {
// Bail if we don't have a server address.
if ( empty( $_SERVER['SERVER_ADDR'] ) ) {
return false;
}
// Fetch our server address.
$server_ip = sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) );
// If we're on a local, do some additional checks.
if ( '127.0.0.1' === $server_ip ) {
// Get our hostname data.
$res_ip = gethostbyname( gethostname() );
// Run a validation check on our IP to make sure it's legit.
if ( filter_var( $res_ip, FILTER_VALIDATE_IP ) === true ) {
return $res_ip;
}
}
// Return our IP.
return $server_ip;
}
/**
* Include a small bit of CSS to display and position the icon.
*
* @return void
*/
function lw_add_admin_bar_css() {
// Bail if current user doesnt have cap.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Echo out the CSS.
echo '
<style>
li#wp-admin-bar-lw-hello-server {
display: block;
}
li#wp-admin-bar-lw-hello-server .ab-item.ab-empty-item .ab-lw-icon {}
li#wp-admin-bar-lw-hello-server .ab-item.ab-empty-item .ab-lw-icon:before {
content: "\f115";
top: 2px;
}
li#wp-admin-bar-lw-hello-server .lw-admin-child-menu .ab-item.ab-empty-item span.ab-lw-inner-data {
color: #fff;
}
@media screen and ( max-width: 782px ) {
li#wp-admin-bar-lw-hello-server {
display: block !important;
}
}
</style>
';
}
add_action( 'wp_head', 'lw_add_admin_bar_css' );
add_action( 'admin_head', 'lw_add_admin_bar_css' );
/**
* Display our server info in the admin bar.
*
* @param WP_Admin_Bar $wp_admin_bar The global WP_Admin_Bar object.
*
* @return void
*/
function lw_load_hello_server( WP_Admin_Bar $wp_admin_bar ) {
// Bail if current user doesnt have cap.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Fetch our server info, and bail if we have none.
if ( false === $info = lw_hello_server_get_info() ) {
return;
}
// Add a parent item.
$wp_admin_bar->add_node(
array(
'id' => 'lw-hello-server',
'title' => '<span class="ab-icon ab-lw-icon"></span><span class="ab-label ab-lw-label">' . esc_html__( 'Hello Server', 'lw-hello-server' ) . '</span>',
'meta' => array(
'title' => esc_html__( 'View the current server info.', 'lw-hello-server' ),
'class' => 'lw-admin-parent-menu',
),
)
);
// Add the host server info.
$wp_admin_bar->add_node(
array(
'id' => 'lw-host-server',
'title' => '<span class="ab-lw-inner-label">' . esc_html__( "Hello, I'm:", 'lw-hello-server' ) . ' </span><span class="ab-lw-inner-data" itemprop="server-name">' . esc_html( $info['hostname'] ) . '</span>',
'position' => 0,
'parent' => 'lw-hello-server',
'meta' => array(
'title' => esc_html__( 'View the current server hostname.', 'lw-hello-server' ),
'class' => 'lw-admin-child-menu',
),
)
);
// Add the server IP info.
$wp_admin_bar->add_node(
array(
'id' => 'lw-ip-address',
'title' => '<span class="ab-lw-inner-label">' . esc_html__( 'IP Address:', 'lw-hello-server' ) . ' </span><span class="ab-lw-inner-data" itemprop="ip-address">' . esc_html( $info['address'] ) . '</span>',
'position' => 1,
'parent' => 'lw-hello-server',
'meta' => array(
'title' => esc_html__( 'View the current server IP address.', 'lw-hello-server' ),
'class' => 'lw-admin-child-menu',
),
)
);
// Add the PHP software info.
$wp_admin_bar->add_node(
array(
'id' => 'lw-php-version',
'title' => '<span class="ab-lw-inner-label">' . esc_html__( 'PHP Version:', 'lw-hello-server' ) . ' </span><span class="ab-lw-inner-data" itemprop="php-version">' . esc_html( $info['phpvers'] ) . '</span>',
'position' => 2,
'parent' => 'lw-hello-server',
'meta' => array(
'title' => esc_html__( 'View the current PHP version.', 'lw-hello-server' ),
'class' => 'lw-admin-child-menu',
),
)
);
// Add the database version info.
$wp_admin_bar->add_node(
array(
'id' => 'lw-database-version',
'title' => '<span class="ab-lw-inner-label">' . esc_html__( 'DB Version:', 'lw-hello-server' ) . ' </span><span class="ab-lw-inner-data" itemprop="database-version">' . esc_html( $info['dbvers'] ) . '</span>',
'position' => 3,
'parent' => 'lw-hello-server',
'meta' => array(
'title' => esc_html__( 'View the current database version.', 'lw-hello-server' ),
'class' => 'lw-admin-child-menu',
),
)
);
// Add the server software info.
$wp_admin_bar->add_node(
array(
'id' => 'lw-server-software',
'title' => '<span class="ab-lw-inner-label">' . esc_html__( 'Software:', 'lw-hello-server' ) . ' </span><span class="ab-lw-inner-data" itemprop="server-software">' . esc_html( $info['software'] ) . '</span>',
'position' => 4,
'parent' => 'lw-hello-server',
'meta' => array(
'title' => esc_html__( 'View the current server software.', 'lw-hello-server' ),
'class' => 'lw-admin-child-menu',
),
)
);
}
// Now we set that function up to execute when the admin_notices action is called.
add_action( 'admin_bar_menu', 'lw_load_hello_server', 999 );