How to change a public function inside a class of a plugin?

Question

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 Thrive_Comments_Helper {

    /**
     * The single instance of the class.
     *
     * prince varghese Thrive_Comments_Helper singleton instance.
     */
    protected static $_instance = null;

    /**
     * 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() ) {
        //This function does a bunch stuff here and then call the other function that I'm trying to change.
        if ( $this->tcm_validate_gravatar( $email ) ) {
            $picture = get_avatar_url( $email, $args );
        }
        //After doing that, it applies a filter.
        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 ) {
        // This is the function does a bunch of stuff in here. This is what I want to change.
        return $valid_avatar;
    }

}

The function I want to change is tcm_validate_gravatar.

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?

0
Ben Turner 4 months 0 Answers 12 views 0

Leave an answer