Skip to content

Commit

Permalink
feat: 节点可用性监控
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Mar 8, 2024
1 parent e1a6083 commit 646c032
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Service/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function __construct() {

// 加速服务
new Super();
// 监控服务
new Monitor();
// 更新服务
new Update();
}
Expand Down
156 changes: 156 additions & 0 deletions Service/Monitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace WenPai\ChinaYes\Service;

defined( 'ABSPATH' ) || exit;

use function WenPai\ChinaYes\get_settings;

/**
* Class Monitor
* 插件监控服务
* @package WenPai\ChinaYes\Service
*/
class Monitor {

private $settings;

public function __construct() {
$this->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 );
}
}
}
24 changes: 10 additions & 14 deletions Service/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ public function admin_init() {
[
'name' => 'store',
'label' => __( '应用市场', 'wp-china-yes' ),
'desc' => __( '<a href="https://wpmirror.com/" target="_blank">官方加速源(WPMirror)</a>直接从 .org 反代至大陆分发;文派存储库:中国境内自建托管仓库,同时集成文派集市产品更新',
'desc' => __( '<a href="https://wpmirror.com/" target="_blank">官方加速源WPMirror</a>直接从 .org 反代至大陆分发;<a href="https://wenpai.org/" target="_blank">文派开源(WenPai.org)</a>中国境内自建托管仓库,同时集成文派翻译平台',
'wp-china-yes' ),
'type' => 'radio',
'default' => 'wenpai',
'options' => [
'proxy' => '官方镜像',
'wenpai' => '文派中国',
'wenpai' => '文派开源',
'off' => '不启用'
]
],
[
'name' => 'admincdn',
'label' => __( '萌芽加速', 'wp-china-yes' ),
'desc' => __( '<a href="https://admincdn.com/" target="_blank">萌芽加速(adminCDN)</a>将 WordPress 依赖的静态文件切换为公共资源,加快网站访问速度。您可按需启用需要加速的项目,更多细节控制和功能,请关注 adminCDN 项目。',
'desc' => __( '<a href="https://admincdn.com/" target="_blank">萌芽加速adminCDN</a>将 WordPress 依赖的静态文件切换为公共资源,加快网站访问速度。您可按需启用需要加速的项目,更多细节控制和功能,请关注 adminCDN 项目。',
'wp-china-yes' ),
'type' => 'multicheck',
'default' => [
Expand All @@ -67,21 +67,21 @@ public function admin_init() {
[
'name' => 'cravatar',
'label' => __( '初认头像', 'wp-china-yes' ),
'desc' => __( '<a href="https://cravatar.com/" target="_blank">初认头像(Cravatar)</a>:是 Gravatar 在中国的完美替代方案,您可以在 https://cravatar.com 上传头像,更多选项请安装 WPAavatar 插件。(任何开发者均可在自己的产品中集成该服务,不局限于 WordPress)',
'desc' => __( '<a href="https://cravatar.com/" target="_blank">初认头像Cravatar</a>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' => __( '<a href="https://windfonts.com/" target="_blank">文风字体(Windfonts)</a>即将为您的网页渲染中文字体并对主题、插件内的 Google 字体进行加速',
'desc' => __( '<a href="https://windfonts.com/" target="_blank">文风字体Windfonts</a>即将为您的网页渲染中文字体并对主题、插件内的字体进行加速',
'wp-china-yes' ),
'type' => 'radio',
'default' => 'off',
Expand All @@ -92,7 +92,7 @@ public function admin_init() {
[
'name' => 'adblock',
'label' => __( '广告拦截', 'wp-china-yes' ),
'desc' => __( '<a href="https://wp-china-yes.com/ads" target="_blank">文派叶子🍃(WP-China-Yes)</a>独家特色功能,让您拥有清爽整洁的 WordPress 后台,清除各类常用插件侵入式后台广告、通知及无用信息;启用后若存在异常拦截,请切换为手动模式,查看<a href="https://wp-china-yes.com/" target="_blank">可优化插件列表</a>。',
'desc' => __( '<a href="https://wp-china-yes.com/ads" target="_blank">文派叶子🍃WP-China-Yes</a>独家特色功能,让您拥有清爽整洁的 WordPress 后台,清除各类常用插件侵入式后台广告、通知及无用信息;启用后若存在异常拦截,请切换为手动模式,查看<a href="https://wp-china-yes.com/" target="_blank">可优化插件列表</a>。',
'wp-china-yes' ),
'type' => 'radio',
'default' => 'off',
Expand Down Expand Up @@ -169,7 +169,7 @@ public function setting_page() {
.card h3 {
margin-top: 0;
}
.card a {
.card a, .left-column a {
text-decoration: none;
}
.card-body, .card-footer {
Expand Down Expand Up @@ -221,8 +221,6 @@ public function setting_page() {
<a class="button button-primary" href="https://wp-china-yes.com/" target="_blank">了解更多</a>
</div>
</div>
<!-- 第二个卡片 -->
<div class="card">
<h3>赞助商</h3>
<div class="card-body sponsor-logos">
Expand All @@ -240,8 +238,6 @@ public function setting_page() {
<a class="button button-primary" href="https://wp-china-yes.com/about/sponsor" target="_blank">成为赞助商</a>
</div>
</div>
<!-- 第三个卡片 -->
<div class="card">
<h3>建站套件</h3>
<div class="card-body">
Expand All @@ -259,7 +255,7 @@ public function setting_page() {
</div>
</div>
</div>
<p>提示:此处选项设置并不与任何文派插件及独立功能扩展冲突,可放心安装启用。</p>
<p>提示:插件会定期检查节点可用性,并在节点不可用时自动切换至可用节点,以保证您的网站正常访问。如您发现设置项被自动切换,可在此页面重新设置。</p>
<p>帮助:您可以随时在此处调整个性化设置以便适应不同的业务场景,萌新请保持默认即可。此项目的发展离不开您的支持和建议,<a href="https://wp-china-yes.com/contact" target="_blank">查看联系方式</a>。</p>
HTML;

Expand Down
14 changes: 3 additions & 11 deletions Service/Super.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

defined( 'ABSPATH' ) || exit;

use function WenPai\ChinaYes\get_settings;

/**
* Class Super
* 插件加速服务
Expand All @@ -12,19 +14,9 @@
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 替换
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"autoload": {
"psr-4": {
"WenPai\\ChinaYes\\": "./"
}
},
"files": [
"helpers.php"
]
},
"authors": [
{
Expand Down
20 changes: 20 additions & 0 deletions helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace WenPai\ChinaYes;

defined( 'ABSPATH' ) || exit;

// 获取插件设置
function get_settings() {
$settings = is_multisite() ? get_site_option( 'wp_china_yes' ) : get_option( 'wp_china_yes' );

return wp_parse_args( $settings, [
'store' => 'wenpai',
'admincdn' => [
'admin' => 'admin',
],
'cravatar' => 'cn',
'windfonts' => 'off',
'adblock' => 'off',
] );
}

0 comments on commit 646c032

Please sign in to comment.