[WordPress] 輸出系統診斷資訊的方法

本篇文章更新時間:2020/01/29
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~


想到之後如果要做案件管理也需要調出各網站的細節。剛好看到某個外掛裡的片段有寫這個方法就抽出來改一下筆記。

輸出的範例:

site_url: http://localhost/wp5
home_url: http://localhost/wp5
Database Name: wp5
Table Prefix: wp_
WordPress: 5.3.2
Web Server:
PHP: 7.3.7
MySQL: 5.7.26
ext/mysqli: yes
PHP Memory Limit: 128M
WP Memory Limit: 40M
Memory Usage: 47 MB
Blocked External HTTP Requests: None
WP Locale: zh_TW
Organize uploads by month/year: Enabled
WP_DEBUG: Yes
WP_DEBUG_LOG: No
WP_DEBUG_DISPLAY: Yes
SCRIPT_DEBUG: No
WP Max Upload Size: 3 GB
PHP Time Limit: 0
PHP Error Log: /Applications/MAMP/logs/php_error.log
WP Cron: Enabled
fsockopen: Enabled
allow_url_fopen: Enabled
OpenSSL: OpenSSL 1.0.2o  27 Mar 2018
cURL: 7.65.1
Zlib Compression: Enabled
PHP GD: bundled (2.1.0 compatible)
Imagick: Disabled
Basic Auth: Disabled
Proxy: Disabled

WP_CONTENT_DIR: /Users/chun/www/wp5/wp-content
WP_CONTENT_URL: http://localhost/wp5/wp-content
UPLOADS: Not defined
WP_PLUGIN_DIR: /Users/chun/www/wp5/wp-content/plugins
WP_PLUGIN_URL: http://localhost/wp5/wp-content/plugins

Active Theme Name: mxp_tw
Active Theme Version: 1.0.0
Active Theme Folder: mxp_tw
Parent Theme Name: Test
Parent Theme Version: 21.4.9.1
Parent Theme Folder: test

Active Plugins:
Mxp Dev Tools (v1.2.4) by Chun
WooCommerce (v3.9.0) by Automattic

Must-use Plugins:
xxx.php

程式碼片段:

<?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']));
}

Gist: Link

先筆記著來,之後有機會用XD


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[//////////____30%_________]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *