<?php
/**
 * Handles comments for more precise control over their display.
 * Many major issues with WP comments are dealt with.
 *
 * @package Hybrid
 * @subpackage Functions 
 */

/**
 * Better display of avatars in comments.  Should only be used 
 * in comment sections (may update in future).
 *
 * Checks for false empty commenter URLs 'http://' w/registered users.  
 * Adds the class 'photo' to the image.
 * Adds a call to HYBRID_IMAGES . '/trackback.jpg' for trackbacks.
 * Adds a call to HYBRID_IMAGES . '/pingback.jpg' for pingbacks.
 * Checks for trackbacks and pingbacks (jpg, png, gif) files in child theme's '/images' folder first.
 *
 * Filter hybrid_avatar to override default avatar functionality
 * Prior to version 0.5, only the $default_avatar could be set
 *
 * @since 0.2
 * @global $comment The current comment's DB object.
 * @global $hybrid_settings The global Hybrid settings variable.
 */
function hybrid_avatar() {
	global $comment, $hybrid_settings;

	/* Image types that can be used for trackback/pingback avatars. */
	$img_arr = array( '.jpg', '.png', '.gif' );

	/* Get/set some comment variables. */
	$comment_type = get_comment_type();
	$author = get_comment_author();
	$url = get_comment_author_url();

	if ( $url == 'http://' )
		$url = false;

	/* Default avatar images. */
	if ( $comment_type == 'pingback' )
		$default_avatar = HYBRID_IMAGES . '/pingback.jpg';

	elseif ( $comment_type == 'trackback' )
		$default_avatar = HYBRID_IMAGES . '/trackback.jpg';

	elseif ( $hybrid_settings['default_avatar'] )
		$default_avatar = esc_url( $hybrid_settings['default_avatar'] );

	/* Check for the trackback avatar in the child theme 'images' directory, then in the parent directory. */
	if ( $comment_type == 'trackback' ) :
		foreach ( $img_arr as $img ) :
			if ( file_exists( HYBRID_CHILD_DIR . '/images/trackback' . $img ) ) :
				$default_avatar = HYBRID_CHILD_URL . '/images/trackback' . $img;
				break;
			endif;
		endforeach;

	/* Check for the pingback avatar in the child theme 'images' directory, then in the parent directory. */
	elseif ( $comment_type == 'pingback' ) :
		foreach ( $img_arr as $img ) :
			if ( file_exists( HYBRID_CHILD_DIR . '/images/pingback' . $img ) ) :
				$default_avatar = HYBRID_CHILD_URL . '/images/pingback' . $img;
				break;
			endif;
		endforeach;

	endif;

	/* If URL input, open hyperlink. */
	if ( $url )
		$avatar .= '<a href="' . $url . '" rel="external nofollow" title="' . wp_specialchars( $author, 1 ) . '">';

	/* Get the avatar. */
	$avatar .= get_avatar( get_comment_author_email(), '80', $default_avatar, wp_specialchars( $author, 1 ) );

	/* Close hyperlink. */
	if ( $url )
		$avatar .= '</a>';

	return apply_filters( 'hybrid_avatar', $avatar );
}

/**
 * Displays individual comments.  Uses the callback parameter for
 * wp_list_comments().  Overwrites the default display of comments.
 *
 * @since 0.2.3
 * @param $comment The comment variable
 * @param $args Array of arguments passed from wp_list_comments()
 * @param $depth What level the particular comment is
 */
function hybrid_comments_callback( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	$GLOBALS['comment_depth'] = $depth;
?>

	<li id="comment-<?php echo $comment->comment_ID; ?>" class="<?php hybrid_comment_class(); ?>">

		<?php hybrid_before_comment(); // Before comment hook ?>

		<?php echo hybrid_avatar(); // Avatar filter hook ?><div class="comment-meta-data">

			<div class="comment-author vcard">
				<?php echo hybrid_comment_author(); ?>
			</div><!-- .comment-author .vcard -->

			<abbr class="comment-time" title="<?php comment_date( __('l, F jS, Y, g:i a', 'hybrid') ); ?>">
				<?php printf( __('%1$s at %2$s', 'hybrid'), get_comment_date(), get_comment_time() ); ?>
			</abbr> 

			<span class="comment-sep comment-sep-permalink separator">|</span> 
			<a class="permalink" href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>" title="<?php printf( __('Permalink to comment %1$s', 'hybrid'), $comment->comment_ID ); ?>"><?php _e('Permalink', 'hybrid'); ?></a>

			<?php
				if ( ( get_option('thread_comments') ) && ( $args['type'] == 'all' || get_comment_type() == 'comment' ) ) :
					$max_depth = get_option( 'thread_comments_depth' );
					echo comment_reply_link( array(
						'reply_text' => __('Reply', 'hybrid'), 
						'login_text' => __('Log in to reply.', 'hybrid'),
						'depth' => $depth,
						'max_depth' => $max_depth, 
						'before' => '<span class="comment-sep comment-sep-reply separator">|</span> <span class="comment-reply-link">', 
						'after' => '</span>'
					) );
				endif;
			?>

			<?php edit_comment_link( '<span class="edit">' . __('Edit', 'hybrid') . '</span>', ' <span class="comment-sep comment-sep-edit separator">|</span> ', '' ); ?> 

			<?php if ( $comment->comment_approved == '0' ) : ?>
				<em><?php _e('Your comment is awaiting moderation.', 'hybrid'); ?></em>
			<?php endif; ?>

		</div><!-- .comment-meta-data -->

		<div class="comment-text">
			<?php comment_text(); ?>
		</div><!-- .comment-text -->
<?php
}

/**
 * Ends the display of individual comments. Uses the callback parameter for 
 * wp_list_comments(). Needs to be used in conjunction with hybrid_comments_callback().
 * Not needed but used just in case something is changed.
 *
 * @since 0.2.3
 */
function hybrid_comments_end_callback() {
		hybrid_after_comment(); // After comment hook
	echo '</li><!-- .comment -->';
}

/**
 * Properly displays comment author name/link.  bbPress and other external
 * systems sometimes don't set a display name for registrations. WP has problems
 * if no display name is set. WP gives registered users URL of 'http://' if none is set.
 *
 * @since 0.2.2
 * @global $comment The current comment's DB object.
 * @return string
 */
function hybrid_comment_author() {
	global $comment;

	$author = get_comment_author();
	$url = get_comment_author_url();

	/* Registered members w/o URL defaults to 'http://'. */
	if ( $url == 'http://' )
		$url = false;

	/*
	* Registered through bbPress sometimes leaves no display name.
	* Bug with bbPress 0.9 series and WP 2.5 (no later testing).
	*/
	if ( $comment->user_id > 0 ) :
		$user = get_userdata( $comment->user_id );
		if ( $user->display_name )
			$author = $user->display_name;
		elseif ( $user->user_nickname )
			$author = $user->nickname;
		elseif ( $user->user_nicename )
			$author = $user->user_nicename;
		elseif ( $user->user_login )
			$author = $user->user_login;
	endif;

	/* Display link and cite if URL is set. Also, properly cites trackbacks/pingbacks. */
	if ( $url )
		$output = '<cite class="fn" title="' . $url . '"><a href="' . $url . '" title="' . wp_specialchars( $author, 1 ) . '" class="url external nofollow">' . $author . '</a></cite>';
	else
		$output = '<cite class="fn">' . $author . '</cite>';

	return apply_filters( 'hybrid_comment_author', $output );
}

/**
 * Displays individual pings. Uses the callback parameter for wp_list_comments().
 *
 * @uses hybrid_comments_callback
 * Use the hybrid_pings_callback filter to customize.
 *
 * @since 0.6
 * @param $comment The comment variable.
 * @param $args Array of arguments passed from wp_list_comments().
 * @param $depth What level the particular comment is.
 */
function hybrid_pings_callback( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	$GLOBALS['comment_depth'] = $depth;

	$ping = apply_filters( 'hybrid_pings_callback', false );

	if ( $ping )
		echo $ping;
	else
		hybrid_comments_callback( $comment, $args, $depth );
}

/**
 * Ends the display of individual pings. Uses the callback parameter for
 * wp_list_comments(). Needs to be used in conjunction with hybrid_pings_callback().
 *
 * @uses hybrid_comments_callback
 * Use the hybrid_pings_callback filter to customize.
 *
 * @since 0.6
 */
function hybrid_pings_end_callback() {

	$ping = apply_filters( 'hybrid_pings_end_callback', false );

	if ( $ping )
		echo $ping;
	else
		hybrid_comments_end_callback();
}

?>