diff --git a/Service/Base.php b/Service/Base.php index 03bbf19..d1dd9e5 100644 --- a/Service/Base.php +++ b/Service/Base.php @@ -23,6 +23,8 @@ class Base { // 加速服务 new Super(); + // 监控服务 + new Monitor(); // 更新服务 new Update(); } diff --git a/Service/Monitor.php b/Service/Monitor.php new file mode 100644 index 0000000..a147a36 --- /dev/null +++ b/Service/Monitor.php @@ -0,0 +1,156 @@ +settings = get_settings(); + + add_action( 'init', [ $this, 'init' ] ); + add_action( 'wp_china_yes_maybe_check_store', [ + $this, + 'maybe_check_store' + ] ); + add_action( 'wp_china_yes_maybe_check_cravatar', [ + $this, + 'maybe_check_cravatar' + ] ); + add_action( 'wp_china_yes_maybe_check_admincdn', [ + $this, + 'maybe_check_admincdn' + ] ); + } + + /** + * 初始化 + */ + public function init() { + // 检查应用市场可用性 + if ( ! wp_next_scheduled( 'wp_china_yes_maybe_check_store' ) && $this->settings['store'] != 'off' ) { + wp_schedule_event( time(), 'hourly', 'wp_china_yes_maybe_check_store' ); + } + // 检查初认头像可用性 + if ( ! wp_next_scheduled( 'wp_china_yes_maybe_check_cravatar' ) && $this->settings['cravatar'] != 'off' ) { + wp_schedule_event( time(), 'hourly', 'wp_china_yes_maybe_check_cravatar' ); + } + // 检查萌芽加速可用性 + if ( ! wp_next_scheduled( 'wp_china_yes_maybe_check_admincdn' ) && ! empty( $this->settings['admincdn'] ) ) { + wp_schedule_event( time(), 'hourly', 'wp_china_yes_maybe_check_admincdn' ); + } + } + + /** + * 检查应用市场可用性 + */ + public function maybe_check_store() { + $test_url = 'https://api.wenpai.org/china-yes/version-check'; + if ( $this->settings['store'] == 'proxy' ) { + $test_url = 'http://wpa.cdn.haozi.net/core/version-check/1.7/'; + } + $response = wp_remote_get( $test_url ); + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { + if ( $this->settings['store'] == 'wenpai' ) { + $this->settings['store'] = 'proxy'; + } elseif ( $this->settings['store'] == 'proxy' ) { + $this->settings['store'] = 'off'; + } + $this->update_settings(); + } + } + + /** + * 检查初认头像可用性 + */ + public function maybe_check_cravatar() { + $test_url = 'https://cn.cravatar.com/avatar/'; + switch ( $this->settings['cravatar'] ) { + case 'global': + $test_url = 'https://en.cravatar.com/avatar/'; + break; + case 'weavatar': + $test_url = 'https://weavatar.com/avatar/'; + break; + } + $response = wp_remote_get( $test_url ); + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { + if ( $this->settings['cravatar'] == 'cn' ) { + $this->settings['cravatar'] = 'global'; + } elseif ( $this->settings['cravatar'] == 'global' ) { + $this->settings['cravatar'] = 'weavatar'; + } elseif ( $this->settings['cravatar'] == 'weavatar' ) { + $this->settings['cravatar'] = 'cn'; + } + $this->update_settings(); + } + } + + /** + * 检查萌芽加速可用性 + */ + public function maybe_check_admincdn() { + // 后台加速 + if ( ! empty( $this->settings['admincdn']['admin'] ) ) { + $response = wp_remote_get( 'https://wpstatic.admincdn.com/6.4.3/wp-includes/js/wp-sanitize.min.js' ); + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { + unset( $this->settings['admincdn']['admin'] ); + $this->update_settings(); + } + } + // 前台加速 + if ( ! empty( $this->settings['admincdn']['frontend'] ) ) { + $url = network_site_url( '/wp-includes/js/wp-sanitize.min.js' ); + $response = wp_remote_get( 'https://public.admincdn.com/' . $url ); + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { + unset( $this->settings['admincdn']['frontend'] ); + $this->update_settings(); + } + } + // Google 字体 + if ( ! empty( $this->settings['admincdn']['googlefonts'] ) ) { + $response = wp_remote_get( 'https://googlefonts.admincdn.com/css?family=Roboto:400,700' ); + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { + unset( $this->settings['admincdn']['googlefonts'] ); + $this->update_settings(); + } + } + // Google 前端公共库 + if ( ! empty( $this->settings['admincdn']['googleajax'] ) ) { + $response = wp_remote_get( 'https://googleajax.admincdn.com/ajax/libs/jquery/3.5.1/jquery.min.js' ); + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { + unset( $this->settings['admincdn']['googleajax'] ); + $this->update_settings(); + } + } + // CDNJS 前端公共库 + if ( ! empty( $this->settings['admincdn']['cdnjs'] ) ) { + $response = wp_remote_get( 'https://cdnjs.admincdn.com/jquery/3.5.1/jquery.min.js' ); + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { + unset( $this->settings['admincdn']['cdnjs'] ); + $this->update_settings(); + } + } + } + + /** + * 更新设置 + */ + private function update_settings() { + if ( is_multisite() ) { + update_site_option( 'wp_china_yes', $this->settings ); + } else { + update_option( 'wp_china_yes', $this->settings ); + } + } +} diff --git a/Service/Setting.php b/Service/Setting.php index 7b13d7f..ded015a 100644 --- a/Service/Setting.php +++ b/Service/Setting.php @@ -37,20 +37,20 @@ class Setting { [ 'name' => 'store', 'label' => __( '应用市场', 'wp-china-yes' ), - 'desc' => __( '官方加速源(WPMirror):直接从 .org 反代至大陆分发;文派存储库:中国境内自建托管仓库,同时集成文派集市产品更新。', + 'desc' => __( '官方加速源(WPMirror)直接从 .org 反代至大陆分发;文派开源(WenPai.org)中国境内自建托管仓库,同时集成文派翻译平台。', 'wp-china-yes' ), 'type' => 'radio', 'default' => 'wenpai', 'options' => [ 'proxy' => '官方镜像', - 'wenpai' => '文派中国', + 'wenpai' => '文派开源', 'off' => '不启用' ] ], [ 'name' => 'admincdn', 'label' => __( '萌芽加速', 'wp-china-yes' ), - 'desc' => __( '萌芽加速(adminCDN):将 WordPress 依赖的静态文件切换为公共资源,加快网站访问速度。您可按需启用需要加速的项目,更多细节控制和功能,请关注 adminCDN 项目。', + 'desc' => __( '萌芽加速(adminCDN)将 WordPress 依赖的静态文件切换为公共资源,加快网站访问速度。您可按需启用需要加速的项目,更多细节控制和功能,请关注 adminCDN 项目。', 'wp-china-yes' ), 'type' => 'multicheck', 'default' => [ @@ -67,21 +67,21 @@ class Setting { [ 'name' => 'cravatar', 'label' => __( '初认头像', 'wp-china-yes' ), - 'desc' => __( '初认头像(Cravatar):是 Gravatar 在中国的完美替代方案,您可以在 https://cravatar.com 上传头像,更多选项请安装 WPAavatar 插件。(任何开发者均可在自己的产品中集成该服务,不局限于 WordPress)', + 'desc' => __( '初认头像(Cravatar)Gravatar 在中国的完美替代方案,您可以在 https://cravatar.com 上传头像,更多选项请安装 WPAavatar 插件。(任何开发者均可在自己的产品中集成该服务,不局限于 WordPress)', 'wp-china-yes' ), 'type' => 'radio', 'default' => 'cn', 'options' => [ - 'cn' => '全局启用', + 'cn' => '默认线路', 'global' => '国际线路', - 'weavatar' => '备用源', + 'weavatar' => '备用源(WeAvatar)', 'off' => '不启用' ] ], [ 'name' => 'windfonts', 'label' => __( '文风字体', 'wp-china-yes' ), - 'desc' => __( '文风字体(Windfonts):即将为您的网页渲染中文字体并对主题、插件内的 Google 字体进行加速。', + 'desc' => __( '文风字体(Windfonts)即将为您的网页渲染中文字体并对主题、插件内的字体进行加速。', 'wp-china-yes' ), 'type' => 'radio', 'default' => 'off', @@ -92,7 +92,7 @@ class Setting { [ 'name' => 'adblock', 'label' => __( '广告拦截', 'wp-china-yes' ), - 'desc' => __( '文派叶子🍃(WP-China-Yes):独家特色功能,让您拥有清爽整洁的 WordPress 后台,清除各类常用插件侵入式后台广告、通知及无用信息;启用后若存在异常拦截,请切换为手动模式,查看可优化插件列表。', + 'desc' => __( '文派叶子🍃(WP-China-Yes)独家特色功能,让您拥有清爽整洁的 WordPress 后台,清除各类常用插件侵入式后台广告、通知及无用信息;启用后若存在异常拦截,请切换为手动模式,查看可优化插件列表。', 'wp-china-yes' ), 'type' => 'radio', 'default' => 'off', @@ -169,7 +169,7 @@ class Setting { .card h3 { margin-top: 0; } - .card a { + .card a, .left-column a { text-decoration: none; } .card-body, .card-footer { @@ -221,8 +221,6 @@ HTML; 了解更多 - -

赞助商

- -

建站套件

@@ -259,7 +255,7 @@ HTML;
-

提示:此处选项设置并不与任何文派插件及独立功能扩展冲突,可放心安装启用。

+

提示:插件会定期检查节点可用性,并在节点不可用时自动切换至可用节点,以保证您的网站正常访问。如您发现设置项被自动切换,可在此页面重新设置。

帮助:您可以随时在此处调整个性化设置以便适应不同的业务场景,萌新请保持默认即可。此项目的发展离不开您的支持和建议,查看联系方式

HTML; diff --git a/Service/Super.php b/Service/Super.php index b2962b7..fc55de4 100644 --- a/Service/Super.php +++ b/Service/Super.php @@ -4,6 +4,8 @@ namespace WenPai\ChinaYes\Service; defined( 'ABSPATH' ) || exit; +use function WenPai\ChinaYes\get_settings; + /** * Class Super * 插件加速服务 @@ -12,19 +14,9 @@ defined( 'ABSPATH' ) || exit; class Super { private $settings; - private $default = [ - 'store' => '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 ); + $this->settings = get_settings(); /** * WordPress.Org API 替换 diff --git a/composer.json b/composer.json index c1e9b4d..8f816bf 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,10 @@ "autoload": { "psr-4": { "WenPai\\ChinaYes\\": "./" - } + }, + "files": [ + "helpers.php" + ] }, "authors": [ { diff --git a/helpers.php b/helpers.php new file mode 100644 index 0000000..e3e545d --- /dev/null +++ b/helpers.php @@ -0,0 +1,20 @@ + 'wenpai', + 'admincdn' => [ + 'admin' => 'admin', + ], + 'cravatar' => 'cn', + 'windfonts' => 'off', + 'adblock' => 'off', + ] ); +}