我想更改WordPress插件中包含的类中的一个公共函数。
我不想直接编辑该函数,因为更新插件时会丢失编辑。
这就是插件中的类和函数的外观:
/**
* Class TCM_Comment_Helper
*
* Helper with the comment CRUD
*/
class Thrive_Comments_Helper {
/**
* The single instance of the class.
*
* @var Thrive_Comments_Helper singleton instance.
*/
protected static $_instance = null;
/**
* Comment Extra fields
*
* @var array fileds from comment object that we do not use
*/
protected static $_extra_fields
= array(
\'comment_date_gmt\',
\'comment_author_IP\',
\'comment_author_email\',
);
/**
* Thrive_Comments_Helper constructor.
*/
public function __construct() {
}
/**
* Main Thrive Comments Instance.
* Ensures only one instance of Thrive Comments Helper is loaded or can be loaded.
*
* @return Thrive_Comments_Helper
*/
public static function instance() {
if ( empty( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Get photo avatar url based on the email
*
* @param string $email for the avatar.
* @param array $args extra arguments.
*
* @return string
*/
public function tcm_get_avatar_url( $email, $args = array() ) {
$default_picture = tcms()->tcm_get_setting_by_name( Thrive_Comments_Constants::TCM_DEFAULT_PICTURE_OPTION );
$args[\'size\'] = Thrive_Comments_Constants::AVATAR_SIZE;
$args[\'default_pic\'] = $default_picture;
if ( isset( $args[\'comment_id\'] ) ) {
$args[\'author_avatar\'] = get_comment_meta( $args[\'comment_id\'], \'comment_author_picture\', true );
}
if ( $this->tcm_validate_gravatar( $email ) ) {
$picture = get_avatar_url( $email, $args );
} else if ( isset( $args[\'author_avatar\'] ) && \'\' !== $args[\'author_avatar\'] ) {
$picture = $args[\'author_avatar\'];
} else {
$picture = ( empty( $default_picture ) ) ? tcm()->plugin_url( \'assets/images/\' . Thrive_Comments_Constants::TCM_DEFAULT_PICTURE ) : $default_picture;
}
return apply_filters( \'get_avatar_url\', $picture, $email, $args );
}
/**
* Check if an email has gravatar and return if true
*
* @param string $email user email.
*
* @return bool|string
*/
public function tcm_validate_gravatar( $email ) {
// Craft a potential url and test its headers.
$protocol = is_ssl() ? \'https\' : \'http\';
$hash = md5( strtolower( trim( $email ) ) );
$uri = $protocol . \'://www.gravatar.com/avatar/\' . $hash . \'?s=512&d=404\';
$response = tve_dash_api_remote_get( $uri );
$header_type = wp_remote_retrieve_header( $response, \'content-type\' );
if ( ! $header_type || strpos( $header_type, \'image\' ) === false ) {
$valid_avatar = false;
} else {
$valid_avatar = $uri;
}
return $valid_avatar;
}
}
我想更改的功能是
tcm_validate_gravatar
. 具体来说,我想更改变量
$uri = $protocol . \'://www.gravatar.com/avatar/\' . $hash . \'?s=512&d=404\';
进入
$uri = $protocol . \'://www.gravatar.com/avatar/\' . $hash . \'?s=512&d=mp\';
因为
d=404
正在查询监视器中给我404个警告。
我试过在functions.php
我的孩子主题:
function tcm_validate_gravatar( $email ) {
//My changes to the function go here
}
add_action( \'init\', \'tcm_validate_gravatar\', 20 );
我也尝试过扩展类并将其放入
functions.php
我的孩子主题:
class Extended_Thrive_Comments_Helper extends Thrive_Comments_Helper {
function tcm_validate_gravatar( $email ) {
//My changes to the function go here
return $valid_avatar;
}
}
}
add_action( \'init\', \'tcm_validate_gravatar\', 20 );
我尝试了上述例子的许多变体。为了简洁起见,我不会在这里全部列出。他们都没有成功。始终调用原始函数,而不是
functions.php
子主题。
对tcm_validate_gravatar
中的函数Thrive_Comments_Helper
在不直接编辑插件文件的情况下初始化?