1

I want to change one of the public functions inside a class contained in a WordPress plugin.

I don't want to edit that function directly because I will lose the edit when I update the plugin.

This is what the class and function looks like in the plugin:

/**
 * 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;
    }
}

The function I want to change is tcm_validate_gravatar. Specifically I want to change the variable $uri = $protocol . '://www.gravatar.com/avatar/' . $hash . '?s=512&d=404'; into $uri = $protocol . '://www.gravatar.com/avatar/' . $hash . '?s=512&d=mp'; because d=404 is giving me 404 warnings in Query Monitor.

I have tried putting something like this inside the functions.php of my child theme:

function tcm_validate_gravatar( $email ) {
  //My changes to the function go here
}
add_action( 'init', 'tcm_validate_gravatar', 20 );

I have also tried extending the class and putting that inside the functions.php of my child theme:

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 );

I have tried many variations of the above examples. For the sake of brevity I won't list them all out here. None of them have worked. The original function is always called, not the new one inside my functions.php child theme.

What is the best way to make changes to the tcm_validate_gravatar function inside the Thrive_Comments_Helper class without directly editing the plugin file?

6
  • 1
    Unless there’s a hook, you can’t. It’s not possible to just change arbitrary pieces of code. Where and how is the origins function used? You might be able to remove it and replace it with a different plugin that does whatever you want. Sep 23, 2020 at 11:52
  • 1
    Ah dam :( It's called inside other public functions within the same class.
    – Cat
    Sep 23, 2020 at 11:53
  • 1
    Does it ever pass through apply_filters()? Sep 23, 2020 at 11:55
  • 1
    Wait! yes it does. I just updated the question. New tcm_get_avatar_url function added to the class which calls tcm_validate_gravatar and returns apply_filters. Does that help? Sorry I tried to remove as much extra code as possible to simplify the question.
    – Cat
    Sep 23, 2020 at 11:59
  • 1
    So that filter will let you change the avatar URL, but not the validation. Perhaps it would help if you explained the precise nature of what you’re trying to change. Sep 23, 2020 at 12:39

1 Answer 1

2

It's not possible to overwrite the class function without modifying the file (unless your hosting supports some form of "monkey patching" as its called) you could check if this function "tve_dash_api_remote_get" has a filter in it that allows you to manipulate the uri

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.