' . __('WP_DEBUG', 'wp-china-yes') . ''; } return '' . __('WP_DEBUG', 'wp-china-yes') . ''; } /** * 获取 CPU 使用率 */ private function get_cpu_usage() { if (function_exists('sys_getloadavg') && is_callable('sys_getloadavg')) { $load = sys_getloadavg(); return round($load[0] * 100 / 4, 2); // 假设是4核CPU } return false; } /** * 获取 MySQL 版本 */ private function get_mysql_version() { global $wpdb; return $wpdb->get_var("SELECT VERSION()"); } /** * 检查 PHP 内存限制 */ public function check_memory_limit() { $this->memory['limit'] = (int) ini_get('memory_limit'); } /** * 检查内存使用情况 */ private function check_memory_usage() { $this->memory['usage'] = function_exists('memory_get_peak_usage') ? round(memory_get_peak_usage(true) / 1024 / 1024, 2) : 0; if (!empty($this->memory['usage']) && !empty($this->memory['limit'])) { $this->memory['percent'] = round($this->memory['usage'] / $this->memory['limit'] * 100, 0); $this->memory['color'] = $this->get_memory_color($this->memory['percent']); } } /** * 获取内存使用率的颜色 */ private function get_memory_color($percent) { if ($percent > 90) { return 'font-weight:bold;color:red'; } elseif ($percent > 75) { return 'font-weight:bold;color:#E66F00'; } return 'font-weight:normal;'; } /** * 格式化 WordPress 内存限制 */ private function format_wp_limit($size) { $unit = strtoupper(substr($size, -1)); $value = (int) substr($size, 0, -1); switch ($unit) { case 'P': $value *= 1024; case 'T': $value *= 1024; case 'G': $value *= 1024; case 'M': $value *= 1024; case 'K': $value *= 1024; } return $value; } /** * 获取 WordPress 内存限制 */ private function check_wp_limit() { $memory = $this->format_wp_limit(WP_MEMORY_LIMIT); return $memory ? size_format($memory) : __('N/A', 'wp-china-yes'); } /** * 添加信息到管理界面页脚 */ public function add_footer($content) { $settings = get_settings(); // 设置默认显示选项 $default_options = [ 'memory_usage', 'wp_limit', 'server_ip', ]; // 如果设置为空或不是数组,使用默认选项 $display_options = isset($settings['memory_display']) && is_array($settings['memory_display']) ? $settings['memory_display'] : $default_options; // 如果 memory 设置未启用,直接返回原始内容 if (empty($settings['memory'])) { return $content; } $this->check_memory_usage(); $this->server_ip_address = $_SERVER['SERVER_ADDR'] ?? ($_SERVER['LOCAL_ADDR'] ?? ''); $this->os_info = $this->get_os_info(); $footer_parts = []; // 内存使用量 if (in_array('memory_usage', $display_options)) { $footer_parts[] = sprintf('%s: %s %s %s MB (%s%%)', __('Memory', 'wp-china-yes'), $this->memory['usage'], __('of', 'wp-china-yes'), $this->memory['limit'], $this->memory['color'], $this->memory['percent'] ); } // WP内存限制 if (in_array('wp_limit', $display_options)) { $footer_parts[] = sprintf('%s: %s', __('WP LIMIT', 'wp-china-yes'), $this->check_wp_limit() ); } // 服务器IP和主机名 if (in_array('server_ip', $display_options)) { $hostname_part = in_array('hostname', $display_options) ? " (" . gethostname() . ")" : ""; $footer_parts[] = sprintf('IP: %s%s', $this->server_ip_address, $hostname_part ); } // 操作系统信息 if (in_array('os_info', $display_options)) { $footer_parts[] = sprintf('OS: %s', $this->os_info); } // PHP信息 if (in_array('php_info', $display_options)) { $footer_parts[] = sprintf('PHP: %s @%sBitOS', PHP_VERSION, PHP_INT_SIZE * 8 ); } // Debug状态 if (in_array('debug_status', $display_options)) { $footer_parts[] = $this->get_debug_status(); } // CPU使用率 if (in_array('cpu_usage', $display_options)) { $cpu_usage = $this->get_cpu_usage(); if ($cpu_usage !== false) { $footer_parts[] = sprintf('CPU: %s%%', $cpu_usage); } } // MySQL版本 if (in_array('mysql_version', $display_options)) { $footer_parts[] = sprintf('MySQL: %s', $this->get_mysql_version()); } if (!empty($footer_parts)) { $content .= ' | WPCY - ' . implode(' | ', $footer_parts); } return $content; } /** * 检查 PHP 版本 */ public function check_php_version() { if (version_compare(PHP_VERSION, '7.0', '<')) { deactivate_plugins(plugin_basename(CHINA_YES_PLUGIN_FILE)); wp_die( sprintf( '

%s

%s

', __('插件无法激活:PHP 版本过低', 'wp-china-yes'), __('请升级 PHP 至 7.0 或更高版本。', 'wp-china-yes') ), __('PHP 版本错误', 'wp-china-yes'), ['back_link' => true] ); } } /** * 更新设置 */ private function update_settings() { if ( is_multisite() ) { update_site_option( 'wp_china_yes', $this->settings ); } else { update_option( 'wp_china_yes', $this->settings, true ); } } }