Skip to content

Instantly share code, notes, and snippets.

@nczz
Created January 29, 2020 15:27
Show Gist options
  • Save nczz/45b00f2cc5ebb02a7abb8867c4c79529 to your computer and use it in GitHub Desktop.
Save nczz/45b00f2cc5ebb02a7abb8867c4c79529 to your computer and use it in GitHub Desktop.
[WordPress] 輸出系統診斷資訊的方法
<?php
function mxp_wp_diagnostic_info() {
global $table_prefix;
global $wpdb;
/*
* WordPress & Server Environment
*/
$output = 'site_url: ';
$output .= esc_html(site_url());
$output .= "\r\n";
$output .= 'home_url: ';
$output .= esc_html(home_url());
$output .= "\r\n";
$output .= 'Database Name: ';
$output .= esc_html($wpdb->dbname);
$output .= "\r\n";
$output .= 'Table Prefix: ';
$output .= esc_html($table_prefix);
$output .= "\r\n";
$output .= 'WordPress: ';
$output .= get_bloginfo('version', 'display');
if (is_multisite()) {
$output .= ' Multisite ';
$output .= '(' . (is_subdomain_install() ? 'subdomain' : 'subdirectory') . ')';
$output .= "\r\n";
$output .= 'Multisite Site Count: ';
$output .= esc_html(get_blog_count());
$output .= "\r\n";
$output .= 'Domain Mapping: ' . (defined('SUNRISE') && SUNRISE ? 'Enabled' : 'Disabled');
}
$output .= "\r\n";
$output .= 'Web Server: ';
$output .= esc_html(!empty($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : '');
$output .= "\r\n";
$output .= 'PHP: ';
if (function_exists('phpversion')) {
$output .= esc_html(phpversion());
}
$output .= "\r\n";
$output .= 'MySQL: ';
$output .= esc_html($wpdb->db_version());
$output .= "\r\n";
$output .= 'ext/mysqli: ';
$output .= empty($wpdb->use_mysqli) ? 'no' : 'yes';
$output .= "\r\n";
$output .= 'PHP Memory Limit: ';
if (function_exists('ini_get')) {
$output .= esc_html(ini_get('memory_limit'));
}
$output .= "\r\n";
$output .= 'WP Memory Limit: ';
$output .= esc_html(WP_MEMORY_LIMIT);
$output .= "\r\n";
$output .= 'Memory Usage: ';
$output .= size_format(memory_get_usage(true));
$output .= "\r\n";
$output .= 'Blocked External HTTP Requests: ';
if (!defined('WP_HTTP_BLOCK_EXTERNAL') || !WP_HTTP_BLOCK_EXTERNAL) {
$output .= 'None';
} else {
$accessible_hosts = (defined('WP_ACCESSIBLE_HOSTS')) ? WP_ACCESSIBLE_HOSTS : '';
if (empty($accessible_hosts)) {
$output .= 'ALL';
} else {
$output .= 'Partially (Accessible Hosts: ' . esc_html($accessible_hosts) . ')';
}
}
$output .= "\r\n";
$output .= 'WP Locale: ';
$output .= esc_html(get_locale());
$output .= "\r\n";
$output .= 'Organize uploads by month/year: ';
$output .= esc_html(get_option('uploads_use_yearmonth_folders') ? 'Enabled' : 'Disabled');
$output .= "\r\n";
$output .= 'WP_DEBUG: ';
$output .= esc_html((defined('WP_DEBUG') && WP_DEBUG) ? 'Yes' : 'No');
$output .= "\r\n";
$output .= 'WP_DEBUG_LOG: ';
$output .= esc_html((defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) ? 'Yes' : 'No');
$output .= "\r\n";
$output .= 'WP_DEBUG_DISPLAY: ';
$output .= esc_html((defined('WP_DEBUG_DISPLAY') && WP_DEBUG_DISPLAY) ? 'Yes' : 'No');
$output .= "\r\n";
$output .= 'SCRIPT_DEBUG: ';
$output .= esc_html((defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? 'Yes' : 'No');
$output .= "\r\n";
$output .= 'WP Max Upload Size: ';
$output .= esc_html(size_format(wp_max_upload_size()));
$output .= "\r\n";
$output .= 'PHP Time Limit: ';
if (function_exists('ini_get')) {
$output .= esc_html(ini_get('max_execution_time'));
}
$output .= "\r\n";
$output .= 'PHP Error Log: ';
if (function_exists('ini_get')) {
$output .= esc_html(ini_get('error_log'));
}
$output .= "\r\n";
$output .= 'WP Cron: ';
$output .= esc_html((defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) ? 'Disabled' : 'Enabled');
$output .= "\r\n";
$output .= 'fsockopen: ';
if (function_exists('fsockopen')) {
$output .= 'Enabled';
} else {
$output .= 'Disabled';
}
$output .= "\r\n";
$output .= 'allow_url_fopen: ';
$allow_url_fopen = ini_get('allow_url_fopen');
if (empty($allow_url_fopen)) {
$output .= 'Disabled';
} else {
$output .= 'Enabled';
}
$output .= "\r\n";
$output .= 'OpenSSL: ';
if (defined('OPENSSL_VERSION_TEXT')) {
$output .= esc_html(OPENSSL_VERSION_TEXT);
} else {
$output .= 'Disabled';
}
$output .= "\r\n";
$output .= 'cURL: ';
if (function_exists('curl_init')) {
$curl = curl_version();
$output .= esc_html($curl['version']);
} else {
$output .= 'Disabled';
}
$output .= "\r\n";
$output .= 'Zlib Compression: ';
if (function_exists('gzcompress')) {
$output .= 'Enabled';
} else {
$output .= 'Disabled';
}
$output .= "\r\n";
$output .= 'PHP GD: ';
if (extension_loaded('gd') && function_exists('gd_info')) {
$gd_info = gd_info();
$output .= isset($gd_info['GD Version']) ? esc_html($gd_info['GD Version']) : 'Enabled';
} else {
$output .= 'Disabled';
}
$output .= "\r\n";
$output .= 'Imagick: ';
if (extension_loaded('imagick') && class_exists('Imagick') && class_exists('ImagickPixel')) {
$output .= 'Enabled';
} else {
$output .= 'Disabled';
}
$output .= "\r\n";
$output .= 'Basic Auth: ';
if (isset($_SERVER['REMOTE_USER']) || isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REDIRECT_REMOTE_USER'])) {
$output .= 'Enabled';
} else {
$output .= 'Disabled';
}
$output .= "\r\n";
$output .= 'Proxy: ';
if (defined('WP_PROXY_HOST') || defined('WP_PROXY_PORT')) {
$output .= 'Enabled';
} else {
$output .= 'Disabled';
}
$output .= "\r\n\r\n";
/*
* Defines
*/
$output .= 'WP_CONTENT_DIR: ';
$output .= esc_html((defined('WP_CONTENT_DIR')) ? WP_CONTENT_DIR : 'Not defined');
$output .= "\r\n";
$output .= 'WP_CONTENT_URL: ';
$output .= esc_html((defined('WP_CONTENT_URL')) ? WP_CONTENT_URL : 'Not defined');
$output .= "\r\n";
$output .= 'UPLOADS: ';
$output .= esc_html((defined('UPLOADS')) ? UPLOADS : 'Not defined');
$output .= "\r\n";
$output .= 'WP_PLUGIN_DIR: ';
$output .= esc_html((defined('WP_PLUGIN_DIR')) ? WP_PLUGIN_DIR : 'Not defined');
$output .= "\r\n";
$output .= 'WP_PLUGIN_URL: ';
$output .= esc_html((defined('WP_PLUGIN_URL')) ? WP_PLUGIN_URL : 'Not defined');
$output .= "\r\n\r\n";
/*
* Settings
*/
$theme_info = wp_get_theme();
if (!empty($theme_info) && is_a($theme_info, 'WP_Theme')) {
$output .= "Active Theme Name: " . esc_html($theme_info->get('Name'));
$output .= "\r\n";
$output .= "Active Theme Version: " . esc_html($theme_info->get('Version'));
$output .= "\r\n";
$output .= "Active Theme Folder: " . esc_html($theme_info->get_stylesheet());
$output .= "\r\n";
if (is_child_theme()) {
$parent_info = $theme_info->parent();
if (!empty($parent_info) && is_a($parent_info, 'WP_Theme')) {
$output .= "Parent Theme Name: " . esc_html($parent_info->get('Name'));
$output .= "\r\n";
$output .= "Parent Theme Version: " . esc_html($parent_info->get('Version'));
$output .= "\r\n";
$output .= "Parent Theme Folder: " . esc_html($parent_info->get_stylesheet());
$output .= "\r\n";
} else {
$output .= "WARNING: Parent theme metadata not found\r\n";
}
}
if (!file_exists($theme_info->get_stylesheet_directory())) {
$output .= "WARNING: Active theme folder not found\r\n";
}
} else {
$output .= "WARNING: Theme metadata not found\r\n";
}
$output .= "\r\n";
$output .= "Active Plugins:\r\n";
$active_plugins = (array) get_option('active_plugins', array());
$plugin_details = array();
if (is_multisite()) {
$network_active_plugins = wp_get_active_network_plugins();
$active_plugins = array_map(function ($path) {
$plugin_dir = trailingslashit(WP_PLUGIN_DIR);
$plugin = str_replace($plugin_dir, '', $path);
return $plugin;
}, $network_active_plugins);
}
foreach ($active_plugins as $plugin) {
$plugin_details[] = mxp_get_plugin_details(WP_PLUGIN_DIR . '/' . $plugin);
}
asort($plugin_details);
$output .= implode('', $plugin_details);
$mu_plugins = wp_get_mu_plugins();
if ($mu_plugins) {
$mu_plugin_details = array();
$output .= "\r\n";
$output .= "Must-use Plugins:\r\n";
foreach ($mu_plugins as $mu_plugin) {
$mu_plugin_details[] = mxp_get_plugin_details($mu_plugin);
}
asort($mu_plugin_details);
$output .= implode('', $mu_plugin_details);
}
$dropins = get_dropins();
if ($dropins) {
$output .= "\r\n\r\n";
$output .= "Drop-ins:\r\n";
foreach ($dropins as $file => $dropin) {
$output .= $file . (isset($dropin['Name']) ? ' - ' . $dropin['Name'] : '');
$output .= "\r\n";
}
}
return $output;
}
function mxp_get_plugin_details($plugin_path, $suffix = '') {
$plugin_data = get_plugin_data($plugin_path);
if (empty($plugin_data['Name'])) {
return basename($plugin_path);
}
return sprintf("%s%s (v%s) by %s\r\n", $plugin_data['Name'], $suffix, $plugin_data['Version'], strip_tags($plugin_data['AuthorName']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment