From ba5140d2193ec3406e7e0bace16ca0d5917591c6 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Wed, 20 Aug 2008 23:48:09 +0000 Subject: [PATCH] comment_class(). Props sandbox theme. see #7560 git-svn-id: https://develop.svn.wordpress.org/trunk@8695 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-content/themes/classic/comments.php | 2 +- wp-content/themes/default/comments.php | 6 +-- wp-includes/comment-template.php | 64 ++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/wp-content/themes/classic/comments.php b/wp-content/themes/classic/comments.php index bf02bedd78..4f92f65f7f 100644 --- a/wp-content/themes/classic/comments.php +++ b/wp-content/themes/classic/comments.php @@ -12,7 +12,7 @@
    -
  1. +
  2. id="comment-">

    @

    diff --git a/wp-content/themes/default/comments.php b/wp-content/themes/default/comments.php index ec2f6c4f5c..b28f49a054 100644 --- a/wp-content/themes/default/comments.php +++ b/wp-content/themes/default/comments.php @@ -14,7 +14,7 @@ } /* This variable is for alternating comment background */ - $oddcomment = 'class="alt" '; + $oddcomment = 'alt'; ?> @@ -26,7 +26,7 @@ -
  3. id="comment-"> +
  4. id="comment-"> Says: comment_approved == '0') : ?> @@ -42,7 +42,7 @@ diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index 9f173a153b..956dc7d86a 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -218,6 +218,70 @@ function comment_author_url_link( $linktext = '', $before = '', $after = '' ) { echo get_comment_author_url_link( $linktext, $before, $after ); } +/** + * Generates semantic classes for each comment element + * + * @since 2.7 + * + * @param string|array $class One or more classes to add to the class list + * @param int $comment_id An optional comment ID + * @param int $post_id An optional post ID + */ +function comment_class( $class = '', $comment_id = null, $post_id = null ) { + // Separates classes with a single space, collates classes for post DIV + echo 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"'; +} + +/** + * Returns the classes for the comment div as an array + * + * @since 2.7 + * + * @param string|array $class One or more classes to add to the class list + * @param int $comment_id An optional comment ID + * @param int $post_id An optional post ID + * @return array Array of classes + */ +function get_comment_class( $class = '', $comment_id = null, $post_id = null ) { + static $comment_alt; + + $comment = get_comment($comment_id); + + $classes = array(); + + // Get the comment type (comment, trackback), + $classes[] = $comment->comment_type; + + // If the comment author has an id (registered), then print the log in name + if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) { + // For all registered users, 'byuser' + $classes[] = 'byuser comment-author-' . $user->user_nicename; + // For comment authors who are the author of the post + if ( $post = get_post($post_id) ) { + if ( $comment->user_id === $post->post_author ) + $classes[] = 'bypostauthor'; + } + } + + if ( empty($comment_alt) ) + $comment_alt = 0; + + if ( $comment_alt % 2 ) + $classes[] = 'odd'; + else + $classes[] = 'even'; + + $comment_alt++; + + if ( !empty($class) ) { + if ( !is_array( $class ) ) + $class = preg_split('#\s+#', $class); + $classes = array_merge($classes, $class); + } + + return apply_filters('comment_class', $classes, $class, $comment_id, $post_id); +} + /** * Retrieve the comment date of the current comment. *