Files
wordpress-theme-aksrt/src/Extensions/SystemTools.php
T

206 lines
7.4 KiB
PHP

<?php
/**
* 系统工具:WordPress 优化、上传优化与编辑器显示开关。
*
* 对应后台「扩展&增强 → 系统工具」设置(ExtensionRuntime::get_system_tools_settings()),
* 行为对齐参考主题 zib-theme.php / zib-head.php / admin-main.php / admin-set.php 的系统工具套件。
*
* @package AKSRT
*/
namespace AKSRT\Extensions;
defined('ABSPATH') || exit;
class SystemTools {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
$settings = \AKSRT\Settings\ExtensionRuntime::get_system_tools_settings();
if (!empty($settings['hide_admin_bar']) || !is_super_admin()) {
add_filter('show_admin_bar', '__return_false');
}
if (!empty($settings['remove_emoji'])) {
$this->remove_emoji();
}
if (!empty($settings['remove_open_sans'])) {
add_action('init', [$this, 'remove_open_sans']);
}
if (!empty($settings['remove_more_wp_head'])) {
add_action('after_setup_theme', [$this, 'remove_more_wp_head']);
}
if (!empty($settings['remove_dashboard_widgets'])) {
add_action('do_meta_boxes', [$this, 'remove_dashboard_widgets'], 10, 3);
add_filter('site_status_tests', [$this, 'filter_site_status_tests']);
add_filter('pre_http_request', [$this, 'block_site_health_requests'], 10, 3);
}
if (!empty($settings['display_wp_update'])) {
$this->disable_wp_update();
}
if (!empty($settings['disabled_large_thumbnail'])) {
add_action('after_setup_theme', [$this, 'disable_large_thumbnail']);
}
if (!empty($settings['close_gutenberg'])) {
add_filter('use_block_editor_for_post', '__return_false');
}
if (!empty($settings['disabled_autoembed'])) {
$this->disable_autoembed();
}
if (!empty($settings['admin_user_del_fields'])) {
add_action('show_user_profile', [$this, 'hide_user_profile_fields']);
add_action('edit_user_profile', [$this, 'hide_user_profile_fields']);
}
}
/** 删除WordPress自带Emoji表情(对齐参考主题 remove_emoji 套件)。 */
private function remove_emoji(): void {
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
}
/** 禁用Google字体(Open Sans)。 */
public function remove_open_sans(): void {
wp_deregister_style('open-sans');
wp_register_style('open-sans', false);
wp_enqueue_style('open-sans', '');
}
/** 清理wp_head带入的多余Meta标签。 */
public function remove_more_wp_head(): void {
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('wp_head', 'rest_output_link_wp_head', 10, 0);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10, 1);
remove_action('wp_head', 'rel_canonical', 10, 0);
}
/** 移除仪表盘的站点健康、PHP/浏览器版本检查小组件。 */
public function remove_dashboard_widgets($screen_id, $context): void {
if ('dashboard' !== $screen_id || 'normal' !== $context) {
return;
}
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['high']['dashboard_php_nag']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
}
/** 站点健康检查中移除页面缓存与PHP版本检测(非常耗时)。 */
public function filter_site_status_tests(array $tests): array {
if (isset($tests['async']['page_cache'])) {
unset($tests['async']['page_cache']);
}
if (isset($tests['direct']['php_version'])) {
unset($tests['direct']['php_version']);
}
return $tests;
}
/** 禁止后台联网检测PHP版本、浏览器与贡献者信息,提高后台加载速度。 */
public function block_site_health_requests($pre, $parsed_args, $url) {
$blocked = [
'api.wordpress.org/core/serve-happy/',
'api.wordpress.org/core/browse-happy/',
'api.wordpress.org/core/credits/',
];
foreach ($blocked as $arg) {
if (strpos((string) $url, $arg) !== false) {
return [
'headers' => [],
'body' => '',
'response' => [
'code' => false,
'message' => false,
],
'cookies' => [],
'http_response' => null,
];
}
}
return $pre;
}
/** 禁止WordPress检测核心/插件/主题更新,并屏蔽更新通知。 */
private function disable_wp_update(): void {
remove_action('admin_init', '_maybe_update_core');
remove_action('admin_init', '_maybe_update_plugins');
remove_action('admin_init', '_maybe_update_themes');
add_action('admin_menu', [$this, 'hide_update_notices']);
add_filter('automatic_updater_disabled', '__return_true');
remove_action('init', 'wp_schedule_update_checks');
remove_action('wp_version_check', 'wp_version_check');
remove_action('wp_update_plugins', 'wp_update_plugins');
remove_action('wp_update_themes', 'wp_update_themes');
add_filter('auto_update_core', '__return_false');
add_filter('allow_major_auto_core_updates', '__return_false');
add_filter('allow_dev_auto_core_updates', '__return_false');
add_filter('allow_minor_auto_core_updates', '__return_false');
}
/** 禁用更新通知。 */
public function hide_update_notices(): void {
remove_action('admin_notices', 'update_nag', 3);
remove_action('network_admin_notices', 'update_nag', 3);
}
/** 禁止WP大尺寸缩略图裁剪(medium_large、1536x1536、2048x2048)。 */
public function disable_large_thumbnail(): void {
remove_image_size('1536x1536');
remove_image_size('2048x2048');
add_filter('intermediate_image_sizes', static function ($sizes) {
return array_merge(array_diff((array) $sizes, ['medium_large', '1536x1536', '2048x2048']));
}, 20);
}
/** 禁用Auto Embed自动嵌入功能,提高页面渲染速度。 */
private function disable_autoembed(): void {
remove_filter('the_content', [$GLOBALS['wp_embed'], 'autoembed'], 8);
remove_filter('widget_text_content', [$GLOBALS['wp_embed'], 'autoembed'], 8);
remove_filter('widget_block_content', [$GLOBALS['wp_embed'], 'autoembed'], 8);
remove_action('edit_form_advanced', [$GLOBALS['wp_embed'], 'maybe_run_ajax_cache']);
remove_action('edit_page_form', [$GLOBALS['wp_embed'], 'maybe_run_ajax_cache']);
}
/** 关闭后台用户编辑页面中的多余选项。 */
public function hide_user_profile_fields(): void {
echo '<style>.user-first-name-wrap,.user-last-name-wrap,.user-admin-bar-front-wrap,.user-comment-shortcuts-wrap,.user-syntax-highlighting-wrap,.user-rich-editing-wrap,.user-profile-picture .description,.user-language-wrap{display:none}</style>';
}
}