feat: 性能优化

This commit is contained in:
耗子 2024-11-19 17:47:44 +08:00
parent 789c920032
commit 7121b4b7ef
No known key found for this signature in database
GPG key ID: C964D7226D045DAA
8 changed files with 63 additions and 57 deletions

View file

@ -17,40 +17,44 @@ class Monitor {
public function __construct() {
$this->settings = get_settings();
wp_clear_scheduled_hook( 'wp_china_yes_maybe_check_store' ); // TODO 下个版本移除
wp_clear_scheduled_hook( 'wp_china_yes_maybe_check_cravatar' ); // TODO 下个版本移除
wp_clear_scheduled_hook( 'wp_china_yes_maybe_check_admincdn' ); // TODO 下个版本移除
if ( $this->settings['monitor'] ) {
return;
// 站点网络下只在主站运行
if ( is_main_site() ) {
add_action( 'init', [ $this, 'init' ] );
add_action( 'wp_china_yes_monitor_hook', [
$this,
'run_monitor'
] );
}
} else {
wp_clear_scheduled_hook( 'wp_china_yes_monitor' );
}
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_monitor' ) ) {
wp_schedule_event( time(), 'hourly', 'wp_china_yes_monitor_hook' );
}
// 检查初认头像可用性
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' );
}
/**
* 运行监控
*/
public function run_monitor() {
if ( $this->settings['store'] != 'off' ) {
$this->maybe_check_store();
}
// 检查萌芽加速可用性
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' );
if ( $this->settings['cravatar'] != 'off' ) {
$this->maybe_check_cravatar();
}
if ( ! empty( $this->settings['admincdn'] ) ) {
$this->maybe_check_admincdn();
}
}
@ -105,7 +109,7 @@ class Monitor {
public function maybe_check_admincdn() {
// 后台加速
if ( in_array( 'admin', $this->settings['admincdn'] ) ) {
$response = wp_remote_get( 'https://wpstatic.admincdn.com/6.4.3/wp-includes/js/wp-sanitize.min.js' );
$response = wp_remote_get( 'https://wpstatic.admincdn.com/6.7/wp-includes/js/wp-sanitize.min.js' );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
$this->settings['admincdn'] = array_values( array_diff( $this->settings['admincdn'], [ 'admin' ] ) );
$this->update_settings();
@ -161,7 +165,7 @@ class Monitor {
if ( is_multisite() ) {
update_site_option( 'wp_china_yes', $this->settings );
} else {
update_option( 'wp_china_yes', $this->settings );
update_option( 'wp_china_yes', $this->settings, true );
}
}
}