Skip to content

Commit

Permalink
feat: 性能优化
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 19, 2024
1 parent 789c920 commit 7121b4b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 57 deletions.
20 changes: 14 additions & 6 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
defined( 'ABSPATH' ) || exit;

use WenPai\ChinaYes\Service\Base;
use WenPai\ChinaYes\Service\Setting;

class Plugin {

Expand All @@ -14,8 +13,9 @@ class Plugin {
*/
public function __construct() {
new Base();
new Setting();
add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
if ( is_admin() ) {
add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
}
}

/**
Expand All @@ -40,16 +40,25 @@ public static function uninstall() {
public function plugins_loaded() {
load_plugin_textdomain( 'wp-china-yes', false, CHINA_YES_PLUGIN_PATH . 'languages' );
add_action( 'admin_notices', [ $this, 'admin_notices' ] );
/**
* 插件列表页中所有插件增加「参与翻译」链接
*/
add_filter( sprintf( '%splugin_action_links', is_multisite() ? 'network_admin_' : '' ), function ( $links, $plugin = '' ) {
$links[] = '<a target="_blank" href="https://translate.wenpai.org/projects/plugins/' . substr( $plugin, 0, strpos( $plugin, '/' ) ) . '/">参与翻译</a>';
$links[] = '<a target="_blank" href="https://wp-china-yes.com/plugins/' . substr( $plugin, 0, strpos( $plugin, '/' ) ) . '/">去广告</a>';

return $links;
}, 10, 2 );
}

/**
* 插件兼容性检测函数
*/
public static function check() {
$notices = [];
if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
if ( version_compare( PHP_VERSION, '7.0.0', '<' ) ) {
deactivate_plugins( 'wp-china-yes/wp-china-yes.php' );
$notices[] = '<div class="notice notice-error"><p>' . sprintf( __( 'WP-China-Yes 插件需要 PHP 5.6.0 或更高版本,当前版本为 %s,插件已自动禁用。',
$notices[] = '<div class="notice notice-error"><p>' . sprintf( __( 'WP-China-Yes 插件需要 PHP 7.0.0 或更高版本,当前版本为 %s,插件已自动禁用。',
'wp-china-yes' ),
PHP_VERSION ) . '</p></div>';
}
Expand All @@ -70,7 +79,6 @@ public static function check() {
$notices[] = '<div class="notice notice-error is-dismissible">
<p><strong>' . __( '检测到不兼容的插件 Kill 429,已自动禁用!', 'wp-china-yes' ) . '</strong></p>
</div>';

}
// 代理服务器检测
if ( defined( 'WP_PROXY_HOST' ) || defined( 'WP_PROXY_PORT' ) ) {
Expand Down
14 changes: 4 additions & 10 deletions Service/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@
class Base {

public function __construct() {
/**
* 插件列表页中所有插件增加「参与翻译」链接
*/
add_filter( sprintf( '%splugin_action_links', is_multisite() ? 'network_admin_' : '' ), function ( $links, $plugin = '' ) {
$links[] = '<a target="_blank" href="https://translate.wenpai.org/projects/plugins/' . substr( $plugin, 0, strpos( $plugin, '/' ) ) . '/">参与翻译</a>';
$links[] = '<a target="_blank" href="https://wp-china-yes.com/plugins/' . substr( $plugin, 0, strpos( $plugin, '/' ) ) . '/">去广告</a>';

return $links;
}, 10, 2 );

// 加速服务
new Super();
// 监控服务
new Monitor();
// 更新服务
new Update();
if ( is_admin() ) {
// 设置服务
new Setting();
}
}
}
56 changes: 30 additions & 26 deletions Service/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
}

/**
* 运行监控
*/
public function run_monitor() {
if ( $this->settings['store'] != 'off' ) {
$this->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 ( $this->settings['cravatar'] != 'off' ) {
$this->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' );
if ( ! empty( $this->settings['admincdn'] ) ) {
$this->maybe_check_admincdn();
}
}

Expand Down Expand Up @@ -105,7 +109,7 @@ public function maybe_check_cravatar() {
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();
Expand Down Expand Up @@ -161,7 +165,7 @@ private function update_settings() {
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 );
}
}
}
10 changes: 5 additions & 5 deletions Service/Super.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public function __construct() {
<a href="https://wenpai.org/support" target="_blank">支持论坛</a>
|
<a href="https://translate.wenpai.org/" target="_blank">翻译平台</a>
|
<a href="https://wptea.com/instructions-for-submission/" target="_blank">文章投稿</a>
|
<a href="https://wp-china-yes.com/document/news-source" target="_blank">自选新闻源</a>
<a href="https://wptea.com/instructions-for-submission/" target="_blank">文章投稿</a>
|
<a href="https://wp-china-yes.com/document/news-source" target="_blank">自选新闻源</a>
</p>
<style>
#wenpai_tea .rss-widget {
Expand All @@ -83,8 +83,8 @@ public function __construct() {
margin:0
}
#wenpai_tea .community-events-footer a {
line-height: 2;
padding: 0.5em;
line-height: 2;
padding: 0.5em;
}
</style>
HTML;
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion framework/classes/admin-options.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public function save_options( $data ) {
} else if ( $this->args['database'] === 'network' ) {
update_site_option( $this->unique, $data );
} else {
update_option( $this->unique, $data );
update_option( $this->unique, $data, true );
}

do_action( "wp_china_yes_{$this->unique}_saved", $data, $this );
Expand Down
2 changes: 1 addition & 1 deletion framework/functions/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function wp_china_yes_import_ajax() {
}

// Success
update_option( $unique, $data );
update_option( $unique, $data, true );

wp_send_json_success();

Expand Down
2 changes: 1 addition & 1 deletion wp-china-yes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: 文派叶子 🍃(WP-China-Yes)是中国 WordPress 生态基础设施软件,犹如落叶新芽,生生不息。
* Author: 文派开源
* Author URI: https://wp-china-yes.com
* Version: 3.7.0
* Version: 3.7.1
* License: GPLv3 or later
* Text Domain: wp-china-yes
* Domain Path: /languages
Expand Down

0 comments on commit 7121b4b

Please sign in to comment.