对 class-vc-notice-controller.php
文件进行修改,屏蔽远程请求(get_notice_list_from_api_request()
方法)以避免后台加载缓慢或超时问题。以下是修改后的代码,您可以直接覆盖原文件。
<!–?php /** * Autoload notice controller. * * @note we require our autoload files everytime and everywhere after plugin load. * @since 7.0 */ if ( ! defined( 'ABSPATH' ) ) { die( '-1' ); } /** * Controller for plugin notice system. * * @description: We use it to notify users about, * emergency updates, new features and some promotions stuff etc. * * @since 7.0 */ class Vc_Notice_Controller { /** * Notification API URL. * * @version 7.0 * @var string */ protected $notification_api_url = 'https://support.wpbakery.com/api/external/notifications'; /** * The slug of transient that stores notice list that we show to user. * * @var string */ public $transient_notice_list = 'wpb_notice_list'; /** * The slug of user meta that stores notice list that user close. * * @var string */ public $user_notice_close_list = 'wpb_notice_close_list'; /** * Vc_Notice_Controller constructor. * * @since 7.0 */ public function __construct() { add_action( 'admin_init', [ $this, 'init', ] ); add_action( 'wp_ajax_wpb_add_notice_to_close_list', [ $this, 'add_notice_to_close_list' ] ); } /** * Init notice system. * * @since 7.0 */ public function init() { // 禁用通知功能,直接返回。 return; // 以下是原始逻辑,如果需要恢复通知功能,请删除上面的 return 语句。 $notice_list = $this->get_notice_list();</p>
<p> $this->show_notices( $notice_list );<br ?–> }
/**
* Show notices to user.
*
* @since 7.0
* @param mixed $notice_list
*/
public function show_notices( $notice_list ) {
if ( empty( $notice_list ) || ! is_array( $notice_list ) ) {
return;
}
// last api request was empty or failed.
if ( $this->is_api_response_empty( $notice_list ) ) {
return;
}
$is_show_at_least_one_notice = false;
foreach ( $notice_list as $notice ) {
if ( ! $this->is_notice_valid( $notice ) ) {
continue;
}
if ( ! $this->is_show_notice( $notice['id'] ) ) {
continue;
}
$this->output_notice( $notice );
$is_show_at_least_one_notice = true;
}
if ( $is_show_at_least_one_notice ) {
add_action(
'admin_notices',
function () {
vc_include_template( 'params/notice/notice-assets.php' );
}
);
}
}
/**
* Check if notice is valid.
*
* @since 7.0
* @param mixed $notice
* @return bool
*/
public function is_notice_valid( $notice ) {
if ( empty( $notice ) || ! is_array( $notice ) ) {
return false;
}
if ( ! $this->is_notice_version_valid( $notice, WPB_VC_VERSION ) ) {
return false;
}
// phpcs:ignore
if ( ! $this->is_notice_date_valid( $notice, current_time( 'timestamp' ) ) ) {
return false;
}
if ( ! $this->is_notice_content_valid( $notice ) ) {
return false;
}
return true;
}
/**
* Check if we should show notice.
*
* @note we don't show notice if user already closed it.
*
* @since 7.0
* @param int $notice_id
* @return bool
*/
public function is_show_notice( $notice_id ) {
$notice_close_list = get_user_meta( get_current_user_id(), $this->user_notice_close_list, true );
if ( empty( $notice_close_list ) ) {
return true;
}
$notice_close_list = is_string( $notice_close_list ) ? json_decode( $notice_close_list ) : [];
return ! ( is_array( $notice_close_list ) && in_array( $notice_id, $notice_close_list ) );
}
/**
* Get notices that should be displayed.
*
* @since 7.0
* @return array
*/
public function get_notice_list() {
// 直接返回空数组,屏蔽远程请求。
return [];
// 以下是原始逻辑,如果需要恢复原始功能,请删除上面的 return 语句。
$notice_list = get_transient( $this->transient_notice_list );
if ( $this->is_api_response_empty( $notice_list ) ) {
return [];
}
if ( ! $this->is_notice_list_valid( $notice_list ) ) {
$notice_list = $this->get_notice_list_from_api_request();
$this->save_notice_list_to_transient( $notice_list );
}
return json_decode( $notice_list, true );
}
/**
* Get notices from notice API.
*
* @note we fire up request to our API once per 12 hours.
*
* @since 7.0
* @return string
*/
public function get_notice_list_from_api_request() {
// 屏蔽远程请求,直接返回空字符串。
return '';
// 以下是原始逻辑,如果需要恢复远程功能,请删除上面的 return 语句。
$empty_notice_list = '';
$response = wp_remote_get( $this->notification_api_url, [ 'timeout' => 30 ] );
if ( is_wp_error( $response ) ) {
return $empty_notice_list;
}
if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
return $empty_notice_list;
}
$response_body = wp_remote_retrieve_body( $response );
if ( $this->is_notice_list_valid( $response_body ) ) {
$notice_list = $response_body;
} else {
$notice_list = $empty_notice_list;
}
return $notice_list;
}
}
new Vc_Notice_Controller();
修改内容说明
- 禁用通知功能:
- 在
init()
方法中直接return;
,完全禁用通知功能,防止后台加载通知。 - 如果需要恢复通知功能,只需删除
return;
语句。
- 在
- 屏蔽远程请求:
- 在
get_notice_list()
方法中直接返回空数组[]
,避免调用远程 API。 - 在
get_notice_list_from_api_request()
方法中直接返回空字符串''
,屏蔽实际的远程请求。
- 在
- 保留原始逻辑:
- 原始逻辑仍然保留在代码中,方便在需要时恢复功能。
使用方法
- 将修改后的代码复制并覆盖
wp-content/plugins/js_composer/include/autoload/class-vc-notice-controller.php
文件。 - 保存文件后刷新 WordPress 后台,通知功能将被禁用,远程请求也会被屏蔽。
注意事项
- 插件更新后修改可能失效:
- 如果插件更新,修改的文件可能会被覆盖。建议在更新后重新应用修改,或者使用更持久的解决方案(例如自定义钩子)。
- 备份文件:
- 在修改之前,请备份原始文件,以便在出现问题时可以恢复。
通过以上修改,您可以有效屏蔽插件的通知功能和远程请求,从而提升后台加载速度并避免相关问题。