我正在一个客户端网站上工作,该框架需要一个特定的插件版本才能正常工作。
在我的functions.php 文件,它会阻止插件更新自身并向管理员用户显示更新消息:
// Disable Cherry Plugin Updates
function filter_plugin_updates( $value ) {
if(isset($value->response[\'cherry-plugin/cherry-plugin.php\'])) {
unset($value->response[\'cherry-plugin/cherry-plugin.php\']);
}
}
上面的代码工作正常,但是,当从WordPress后端菜单进行某些更改时,它有时会抛出PHP错误消息,并且似乎与特定更改不一致:
"Fatal error: Cannot use object of type WP_Error as array in ../public_html/wp-content/plugins/cherry-plugin/admin/plugin-updater.php on line 63"
查看上述文件,第63行是:
if ($response[\'response\'][\'code\']!=\'200\') {
return;
}
它的整个功能是:
// Remote query, function return any data of xml file on server
function cherry_plugin_remote_query($atts){
global $wp_version;
$default = array(
\'remote_server\' => CHERRY_PLUGIN_REMOTE_SERVER,
\'data_type\' => \'\',//framework, plugin, notice, info (Or any channel in xml)
\'output_type\' => \'return\'//return, echo, notice
);
extract(array_merge($default, $atts));
if($data_type == \'framework\' && defined(\'CHERRY_VER\')){
$current_version = CHERRY_VER;
}else if($data_type == \'plugin\'){
$current_version = CHERRY_PLUGIN_VERSION;
}else{
$current_version = \'\';
}
$response = wp_remote_post( $remote_server, array(
\'body\' => array(\'data_type\' => $data_type,
\'current_version\' => $current_version,
\'api-key\' => md5(get_bloginfo(\'url\'))),
\'user-agent\' => \'WordPress/\' . $wp_version . \'; \' . get_bloginfo(\'url\')
)
);
if ($response[\'response\'][\'code\']!=\'200\') {
return;
}
$response = unserialize($response[\'body\']);
if($response==null){
return;
}
switch ($output_type) {
case \'notice\':;
if(!empty($response) && isset($response[\'action\']) && !empty($response[\'notice_content\'])){
global $notice_attr;
$notice_attr =array();
if(isset($response[\'wrapper_id\'])) $notice_attr[\'wrapper_id\'] = $response[\'wrapper_id\'] ;
if(isset($response[\'wrapper_class\'])) $notice_attr[\'wrapper_class\'] = $response[\'wrapper_class\'] ;
if(isset($response[\'notice_content\'])) $notice_attr[\'notice_content\'] = $response[\'notice_content\'] ;
add_action($response[\'action\'], \'cherry_call_function_add_notice\');
function cherry_call_function_add_notice(){
global $notice_attr;
echo cherry_add_notice($notice_attr);
}
}
break;
case \'echo\':
echo $response;
break;
default:
return $response;
break;
}
}
如何阻止插件更新,向管理员用户显示更新消息,以及不抛出任何PHP错误?
SO网友:kaiser
像@gate_engineer 前面已经说过,您的问题是一个普通的PHP问题,您试图以数组的形式访问对象。
不仅要说明这一点,还要进一步帮助您correct check 对于正确的响应,对象正在检查wp_remote_retrieve_response_code()
和wp_remote_retrieve_response_message()
:
$response = wp_remote_request( array( /* args */ ) );
// or wp_remote_post()
// or wp_remote_get()
// or wp_remote_head()
if (
200 !== wp_remote_retrieve_response_code( $response )
OR \'OK\' !== wp_remote_retrieve_response_message( $response )
OR is_wp_error( $response )
)
return $response->get_error_message();
// ...or something more sophisticated using the returned remote API data.
另一个安全检查是检查
headers
:
$headers = wp_remote_retrieve_headers( $response );
// do some checks and fixes here like UTF-8 incompability fixes
// for e.g.
in_array( \'Content-Type\', $headers )
AND \'utf-8\' !== $headers[\'Content-Type\']
AND $result = utf8_encode( wp_remote_retrieve_body( $response ) );
或者/如果你知道可能有一个习惯
header
远程API返回,直接使用:
\'no-result\' === wp_remote_retrieve_header( $response, \'custom-error-header\' )
AND print \'Response error!\';
那就去拿你的结果吧。这将包含另一个检查
is_wp_error()
以及它将检查
body
实际上已设置。如果出现错误或此处没有正文,则返回空字符串:
\'\'
.
$result = wp_remote_retrieve_body( $response );
// Custom check:
if ( empty( $result ) )
_e( \'Nothing returned from request.\', \'your_textdomain\' );
// Process your result:
echo $result[\'foo\'];