如何更改插件类中的公共函数?

时间:2020-09-23 作者:Ben Turner

我想更改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 在不直接编辑插件文件的情况下初始化?

1 个回复
最合适的回答,由SO网友:Jasper B 整理而成

在不修改文件的情况下,不可能覆盖类函数(除非您的主机支持某种形式的“monkey patching”调用),您可以检查此函数是否;tve\\U dash\\U api\\U remote\\U get;其中有一个过滤器,允许您操作uri

相关推荐

如何从数组中获取特定的字符串/值?PHP

我正在创建一个自定义的wordpress主题,我几乎陷入了一种情况。我创建了一个数组$ark并在其中获取了一些值<?php $ark[] = esc_url( add_query_arg( \'pdff\', $post->ID ) ); print_r($ark) ; echo \'jonty\'; ?> 下面是上述代码的输出;Array ( [0] => /jobifylocal/wp-admin/admin-ajax.php?pdff=127 ) jonty&#