'wenpai', 'admincdn' => [ 'admin' => 'admin', ], 'cravatar' => 'cn', 'windfonts' => 'off', 'adblock' => 'off', ]; public function __construct() { $this->settings = is_multisite() ? get_site_option( 'wp_china_yes' ) : get_option( 'wp_china_yes' ); $this->settings = wp_parse_args( $this->settings, $this->default ); /** * WordPress.Org API 替换 */ if ( is_admin() || wp_doing_cron() ) { if ( $this->settings['store'] != 'off' ) { add_filter( 'pre_http_request', [ $this, 'filter_wordpress_org' ], 100, 3 ); } } /** * 添加「文派茶馆」小组件 */ if ( is_admin() ) { add_action( 'wp_dashboard_setup', function () { global $wp_meta_boxes; unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_primary'] ); wp_add_dashboard_widget( 'wenpai_tea', '文派茶馆', function () { echo <<
HTML; wp_widget_rss_output( 'https://wptea.com/feed/', [ 'items' => 5, 'show_summary' => 1, ] ); echo <<
HTML; } ); } ); add_action( 'wp_network_dashboard_setup', function () { global $wp_meta_boxes; unset( $wp_meta_boxes['dashboard-network']['side']['core']['dashboard_primary'] ); wp_add_dashboard_widget( 'wenpai_tea', '文派茶馆', function () { echo <<
HTML; wp_widget_rss_output( 'https://wptea.com/feed/', [ 'items' => 5, 'show_summary' => 1, ] ); echo <<
HTML; } ); } ); } /** * WordPress 核心静态文件链接替换 */ if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { if ( ! empty( $this->settings['admincdn']['admin'] ) && ! stristr( $GLOBALS['wp_version'], 'alpha' ) && ! stristr( $GLOBALS['wp_version'], 'beta' ) && ! stristr( $GLOBALS['wp_version'], 'RC' ) ) { // 禁用合并加载,以便于使用公共资源节点 global $concatenate_scripts; $concatenate_scripts = false; $this->page_str_replace( 'preg_replace', [ '~' . home_url( '/' ) . '(wp-admin|wp-includes)/(css|js)/~', sprintf( 'https://wpstatic.admincdn.com/%s/$1/$2/', $GLOBALS['wp_version'] ) ] ); } } if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { /** * 前台静态加速 */ if ( ! empty( $this->settings['admincdn']['frontend'] ) ) { add_action( 'template_redirect', function () { ob_start( function ( $content ) { $regex = '#(?<=[(\"\'])(?:' . quotemeta( home_url() ) . ')?/(?:((?:wp-content|wp-includes)[^\"\')]+\.(css|js)[^\"\')]+))(?=[\"\')])#'; return preg_replace_callback( $regex, function ( $asset ) { return 'https://public.admincdn.com/' . $asset[0]; }, $content ); } ); } ); } /** * Google 字体替换 */ if ( ! empty( $this->settings['admincdn']['googlefonts'] ) ) { $this->page_str_replace( 'str_replace', [ 'fonts.googleapis.com', 'googlefonts.admincdn.com' ] ); } /** * Google 前端公共库替换 */ if ( ! empty( $this->settings['admincdn']['googleajax'] ) ) { $this->page_str_replace( 'str_replace', [ 'ajax.googleapis.com', 'googleajax.admincdn.com' ] ); } /** * CDNJS 前端公共库替换 */ if ( ! empty( $this->settings['admincdn']['cdnjs'] ) ) { $this->page_str_replace( 'str_replace', [ 'cdnjs.cloudflare.com/ajax/libs', 'cdnjs.admincdn.com' ] ); } } /** * 初认头像 */ if ( ! empty( $this->settings['cravatar'] ) ) { add_filter( 'user_profile_picture_description', [ $this, 'set_user_profile_picture_for_cravatar' ], 1 ); add_filter( 'avatar_defaults', [ $this, 'set_defaults_for_cravatar' ], 1 ); add_filter( 'um_user_avatar_url_filter', [ $this, 'get_cravatar_url' ], 1 ); add_filter( 'bp_gravatar_url', [ $this, 'get_cravatar_url' ], 1 ); add_filter( 'get_avatar_url', [ $this, 'get_cravatar_url' ], 1 ); } } /** * WordPress.Org 替换 */ public function filter_wordpress_org( $preempt, $args, $url ) { if ( ( ! strpos( $url, 'api.wordpress.org' ) && ! strpos( $url, 'downloads.wordpress.org' ) ) ) { return $preempt; } if ( $this->settings['store'] == 'wenpai' ) { $url = str_replace( 'api.wordpress.org', 'api.wenpai.org', $url ); } else { $url = str_replace( 'api.wordpress.org', 'wpa.cdn.haozi.net', $url ); } $url = str_replace( 'downloads.wordpress.org', 'wpd.cdn.haozi.net', $url ); $curl_version = '1.0.0'; if ( function_exists( 'curl_version' ) ) { $curl_version_array = curl_version(); if ( is_array( $curl_version_array ) && key_exists( 'version', $curl_version_array ) ) { $curl_version = $curl_version_array['version']; } } if ( version_compare( $curl_version, '7.15.0', '<' ) ) { $url = str_replace( 'https://', 'http://', $url ); } return wp_remote_request( $url, $args ); } /** * 初认头像替换 */ public function get_cravatar_url( $url ) { switch ( $this->settings['cravatar'] ) { case 'cn': return $this->replace_avatar_url( $url, 'cn.cravatar.com' ); case 'global': return $this->replace_avatar_url( $url, 'en.cravatar.com' ); case 'weavatar': return $this->replace_avatar_url( $url, 'weavatar.com' ); default: return $url; } } /** * 头像 URL 替换 */ public function replace_avatar_url( $url, $domain ) { $sources = array( 'www.gravatar.com', '0.gravatar.com', '1.gravatar.com', '2.gravatar.com', 's.gravatar.com', 'secure.gravatar.com', 'cn.gravatar.com', 'en.gravatar.com', 'gravatar.com', 'sdn.geekzu.org', 'gravatar.duoshuo.com', 'gravatar.loli.net', 'dn-qiniu-avatar.qbox.me' ); return str_replace( $sources, $domain, $url ); } /** * WordPress 讨论设置中的默认 LOGO 名称替换 */ public function set_defaults_for_cravatar( $avatar_defaults ) { $avatar_defaults['gravatar_default'] = '初认头像'; return $avatar_defaults; } /** * 个人资料卡中的头像上传地址替换 */ public function set_user_profile_picture_for_cravatar() { return '您可以在初认头像修改您的资料图片'; } /** * 页面替换 * * @param $replace_func string 要调用的字符串关键字替换函数 * @param $param array 传递给字符串替换函数的参数 */ private function page_str_replace( $replace_func, $param ) { add_action( 'init', function () use ( $replace_func, $param ) { ob_start( function ( $buffer ) use ( $replace_func, $param ) { $param[] = $buffer; return call_user_func_array( $replace_func, $param ); } ); }, 999999 ); } }