<?php
/**
*
* Class CommentAuthorUserRoleLabel
* This show the user role on comments
*
*/
if ( ! class_exists( 'CommentAuthorUserRoleLabel' ) ) {
	class CommentAuthorUserRoleLabel {
		protected $user_role;
		public function __construct() {
			add_filter( 'get_comment_author', array( $this, 'create_comment_author_role_html' ), 10, 3 );
			add_filter( 'get_comment_author_link', array( $this, 'display_comment_author_role_html' ) );
		}
		function create_comment_author_role_html( $author, $comment_id, $comment ) {
				
			// Check if the commenter is a registered user
			$user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
				if ( $user ) {
					$commenter_user_role = $user->roles[0];
					// HTML output added after the comment author name
					$this->user_role = ' <span class="comment-author-label ' . sanitize_html_class($commenter_user_role) . '-label">' . ucfirst( tag_escape( $commenter_user_role ) ) . '</span>';
					} else {
						$this->user_role = '';
					}
			return $author;
		}
		function display_comment_author_role_html( $author ) {
			return $author .= $this->user_role;
		}
	}
new CommentAuthorUserRoleLabel;
}
