mirror of
https://github.com/WenPai-org/wp-china-yes.git
synced 2025-08-03 11:11:30 +08:00
feat: 节点可用性监控
This commit is contained in:
parent
e1a6083779
commit
646c032e70
6 changed files with 195 additions and 26 deletions
|
@ -23,6 +23,8 @@ class Base {
|
|||
|
||||
// 加速服务
|
||||
new Super();
|
||||
// 监控服务
|
||||
new Monitor();
|
||||
// 更新服务
|
||||
new Update();
|
||||
}
|
||||
|
|
156
Service/Monitor.php
Normal file
156
Service/Monitor.php
Normal file
|
@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -37,20 +37,20 @@ class Setting {
|
|||
[
|
||||
'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' => [
|
||||
|
@ -67,21 +67,21 @@ class Setting {
|
|||
[
|
||||
'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',
|
||||
|
@ -92,7 +92,7 @@ class Setting {
|
|||
[
|
||||
'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',
|
||||
|
@ -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;
|
|||
<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">
|
||||
|
@ -240,8 +238,6 @@ HTML;
|
|||
<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">
|
||||
|
@ -259,7 +255,7 @@ HTML;
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>提示:此处选项设置并不与任何文派插件及独立功能扩展冲突,可放心安装启用。</p>
|
||||
<p>提示:插件会定期检查节点可用性,并在节点不可用时自动切换至可用节点,以保证您的网站正常访问。如您发现设置项被自动切换,可在此页面重新设置。</p>
|
||||
<p>帮助:您可以随时在此处调整个性化设置以便适应不同的业务场景,萌新请保持默认即可。此项目的发展离不开您的支持和建议,<a href="https://wp-china-yes.com/contact" target="_blank">查看联系方式</a>。</p>
|
||||
HTML;
|
||||
|
||||
|
|
|
@ -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 替换
|
||||
|
|
|
@ -10,7 +10,10 @@
|
|||
"autoload": {
|
||||
"psr-4": {
|
||||
"WenPai\\ChinaYes\\": "./"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"helpers.php"
|
||||
]
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
|
|
20
helpers.php
Normal file
20
helpers.php
Normal file
|
@ -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',
|
||||
] );
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue