From 4bec99a3382f70684fdfb7b90a61f55689967b4c Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 24 Feb 2007 07:33:29 +0000 Subject: [PATCH] Comment feeds everywhere. Props mdawaffe and rob1n. fixes #2485 git-svn-id: http://svn.automattic.com/wordpress/trunk@4934 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/comment-template.php | 4 +- wp-includes/feed-atom-comments.php | 64 +++++------------ wp-includes/feed-rss2-comments.php | 82 +++++++-------------- wp-includes/feed.php | 5 ++ wp-includes/functions.php | 19 ++--- wp-includes/query.php | 111 +++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+), 117 deletions(-) diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index d57aff5f7..0782f33eb 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -290,7 +290,9 @@ function comments_template( $file = '/comments.php' ) { $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date"); } - $comments = apply_filters( 'comments_array', $comments, $post->ID ); + // keep $comments for legacy's sake (remember $table*? ;) ) + $comments = $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID ); + $wp_query->comment_count = count($wp_query->comments); define('COMMENTS_TEMPLATE', true); $include = apply_filters('comments_template', TEMPLATEPATH . $file ); diff --git a/wp-includes/feed-atom-comments.php b/wp-includes/feed-atom-comments.php index 460b8e5b9..7687a8c09 100644 --- a/wp-includes/feed-atom-comments.php +++ b/wp-includes/feed-atom-comments.php @@ -8,11 +8,12 @@ echo '' > <?php - if (is_single() || is_page()) { + if ( is_singular() ) printf(__('Comments on: %s'), get_the_title_rss()); - } else { - printf(__('Comments for %s'), get_bloginfo_rss('name')); - } + elseif ( is_search() ) + printf(__('Comments for %s searching on %s'), get_bloginfo_rss( 'name' ), attribute_escape($wp_query->query_vars['s'])); + else + printf(__('Comments for %s'), get_bloginfo_rss( 'name' ) . get_wp_title_rss()); ?> @@ -24,39 +25,14 @@ echo '' get_results("SELECT comment_ID, comment_author, comment_author_email, - comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, - $wpdb->posts.ID, $wpdb->posts.post_password FROM $wpdb->comments - LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE comment_post_ID = '" . get_the_ID() . "' - AND $wpdb->comments.comment_approved = '1' AND $wpdb->posts.post_status = 'publish' - AND post_date_gmt < '" . gmdate("Y-m-d H:i:59") . "' - ORDER BY comment_date_gmt ASC" ); - } else { // if no post id passed in, we'll just use the last posts_per_rss comments. - $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_author_email, - comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, - $wpdb->posts.ID, $wpdb->posts.post_password FROM $wpdb->comments - LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE $wpdb->posts.post_status = 'publish' - AND $wpdb->comments.comment_approved = '1' AND post_date_gmt < '" . gmdate("Y-m-d H:i:s") . "' - ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss') ); - } - - if ($comments) { - foreach ($comments as $comment) { - $GLOBALS['comment'] =& $comment; - get_post_custom($comment->comment_post_ID); +if ( have_comments() ) : while ( have_comments() ) : the_comment(); + $comment_post = get_post($comment->comment_post_ID); + get_post_custom($comment_post->ID); ?> <?php - if (!(is_single() || is_page())) { - $title = get_the_title($comment->comment_post_ID); + if ( !is_singular() ) { + $title = get_the_title($comment_post->ID); $title = apply_filters('the_title', $title); $title = apply_filters('the_title_rss', $title); printf(__('Comment on %1$s by %2$s'), $title, get_comment_author_rss()); @@ -69,23 +45,19 @@ if (have_posts()) : <author> <name><?php comment_author_rss(); ?></name> <?php if (get_comment_author_url()) echo '<uri>' . get_comment_author_url() . '</uri>'; ?> + </author> <id><?php comment_link(); ?></id> <updated><?php echo mysql2date('D, d M Y H:i:s +0000', get_comment_time('Y-m-d H:i:s', true), false); ?></updated> <published><?php echo mysql2date('D, d M Y H:i:s +0000', get_comment_time('Y-m-d H:i:s', true), false); ?></published> - - <?php if (!empty($comment->post_password) && $_COOKIE['wp-postpass'] != $comment->post_password) { ?> +<?php if (!empty($comment_post->post_password) && $_COOKIE['wp-postpass'] != $comment_post->post_password) : ?> <content type="html" xml:base="<?php comment_link(); ?>"><![CDATA[<?php echo get_the_password_form(); ?>]]></content> - <?php } else { ?> +<?php else : // post pass ?> <content type="html" xml:base="<?php comment_link(); ?>"><![CDATA[<?php comment_text(); ?>]]></content> - <?php } ?> - </entry> -<?php - } - } - - endwhile; -endif; +<?php endif; // post pass + do_action('comment_atom_entry', $comment->comment_ID, $comment_post->ID); ?> -</feed> \ No newline at end of file + </entry> +<?php endwhile; endif; ?> +</feed> diff --git a/wp-includes/feed-rss2-comments.php b/wp-includes/feed-rss2-comments.php index 4e4989849..50f5e3dae 100644 --- a/wp-includes/feed-rss2-comments.php +++ b/wp-includes/feed-rss2-comments.php @@ -7,77 +7,49 @@ echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"> <channel> -<?php -$i = 0; -if (have_posts()) : - while (have_posts()) : the_post(); - if ($i < 1) { - $i++; -?> - <title><?php if (is_single() || is_page() ) { printf(__('Comments on: %s'), get_the_title_rss()); } else { printf(__('Comments for %s'), get_bloginfo_rss("name")); } ?> + <?php + if ( is_singular() ) + printf(__('Comments on: %s'), get_the_title_rss()); + elseif ( is_search() ) + printf(__('Comments for %s searching on %s'), get_bloginfo_rss( 'name' ), attribute_escape($wp_query->query_vars['s'])); + else + printf(__('Comments for %s'), get_bloginfo_rss( 'name' ) . get_wp_title_rss()); + ?> http://wordpress.org/?v= get_results("SELECT comment_ID, comment_author, comment_author_email, - comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, - $wpdb->posts.ID, $wpdb->posts.post_password FROM $wpdb->comments - LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE comment_post_ID = '" . get_the_ID() . "' - AND $wpdb->comments.comment_approved = '1' AND $wpdb->posts.post_status = 'publish' - AND post_date_gmt < '" . gmdate("Y-m-d H:i:59") . "' - ORDER BY comment_date_gmt ASC" ); - } else { // if no post id passed in, we'll just ue the last 10 comments. - $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_author_email, - comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, - $wpdb->posts.ID, $wpdb->posts.post_password FROM $wpdb->comments - LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE $wpdb->posts.post_status = 'publish' - AND $wpdb->comments.comment_approved = '1' AND post_date_gmt < '" . gmdate("Y-m-d H:i:s") . "' - ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss') ); - } - // this line is WordPress' motor, do not delete it. - if ($comments) { - foreach ($comments as $comment) { - $GLOBALS['comment'] =& $comment; - // Some plugins may need to know the metadata - // associated with this comment's post: - get_post_custom($comment->comment_post_ID); +if ( have_comments() ) : while ( have_comments() ) : the_comment(); + $comment_post = get_post($comment->comment_post_ID); + get_post_custom($comment_post->ID); ?> - <?php if ( ! (is_single() || is_page()) ) { - $title = get_the_title($comment->comment_post_ID); - $title = apply_filters('the_title', $title); - $title = apply_filters('the_title_rss', $title); - printf(__('Comment on %1$s by %2$s'), $title, get_comment_author_rss()); - } else { - printf(__('By: %s'), get_comment_author_rss()); - } ?> + <?php + if ( !is_singular() ) { + $title = get_the_title($comment_post->ID); + $title = apply_filters('the_title', $title); + $title = apply_filters('the_title_rss', $title); + printf(__('Comment on %1$s by %2$s'), $title, get_comment_author_rss()); + } else { + printf(__('By: %s'), get_comment_author_rss()); + } + ?> - post_password) && $_COOKIE['wp-postpass'] != $comment->post_password) { - ?> +post_password) && $_COOKIE['wp-postpass'] != $comment_post->post_password) : ?> ]]> - + ]]> - comment_ID, $comment->comment_post_ID); - ?> - -comment_ID, $comment_post->ID); ?> + + diff --git a/wp-includes/feed.php b/wp-includes/feed.php index d2efe7cf1..125ae7766 100644 --- a/wp-includes/feed.php +++ b/wp-includes/feed.php @@ -10,6 +10,11 @@ function bloginfo_rss($show = '') { echo get_bloginfo_rss($show); } +function get_wp_title_rss($sep = '»') { + $title = wp_title($sep, false); + $title = apply_filters('get_wp_title_rss', $title); + return $title; +} function get_the_title_rss() { $title = get_the_title(); diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 027c28c6b..8179b9359 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -939,29 +939,18 @@ function bool_from_yn($yn) { } function do_feed() { + global $wp_query; + $feed = get_query_var('feed'); // Remove the pad, if present. $feed = preg_replace('/^_+/', '', $feed); if ( $feed == '' || $feed == 'feed' ) - $feed = 'rss2'; - - $for_comments = false; - - if ( is_singular() || get_query_var('withcomments') == 1 ) - $for_comments = true; - - if ( false !== strpos($feed, 'comments-') ) { - $for_comments = true; - $feed = str_replace('comments-', '', $feed); - } - - if ( get_query_var('withoutcomments') == 1 ) - $for_comments = false; + $feed = 'rss2'; $hook = 'do_feed_' . $feed; - do_action($hook, $for_comments); + do_action($hook, $wp_query->is_comment_feed); } function do_feed_rdf() { diff --git a/wp-includes/query.php b/wp-includes/query.php index dbb028537..16eb543f9 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -10,6 +10,12 @@ function get_query_var($var) { return $wp_query->get($var); } +function set_query_var($var, $value) { + global $wp_query; + + return $wp_query->set($var, $value); +} + function &query_posts($query) { unset($GLOBALS['wp_query']); $GLOBALS['wp_query'] =& new WP_Query(); @@ -249,6 +255,20 @@ function the_post() { $wp_query->the_post(); } +/* + * Comments loop. + */ + +function have_comments() { + global $wp_query; + return $wp_query->have_comments(); +} + +function the_comment() { + global $wp_query; + return $wp_query->the_comment(); +} + /* * WP_Query */ @@ -265,6 +285,11 @@ class WP_Query { var $current_post = -1; var $in_the_loop = false; var $post; + + var $comments; + var $comment_count = 0; + var $current_comment = -1; + var $comment; var $found_posts = 0; var $max_num_pages = 0; @@ -282,6 +307,7 @@ class WP_Query { var $is_category = false; var $is_search = false; var $is_feed = false; + var $is_comment_feed = false; var $is_trackback = false; var $is_home = false; var $is_404 = false; @@ -305,6 +331,7 @@ class WP_Query { $this->is_category = false; $this->is_search = false; $this->is_feed = false; + $this->is_comment_feed = false; $this->is_trackback = false; $this->is_home = false; $this->is_404 = false; @@ -537,6 +564,14 @@ class WP_Query { if ( $this->is_single || $this->is_page || $this->is_attachment ) $this->is_singular = true; + if ( false !== strpos($qv['feed'], 'comments-') ) { + $this->query_vars['feed'] = $qv['feed'] = str_replace('comments-', '', $qv['feed']); + $qv['withcomments'] = 1; + } + + if ( $this->is_feed && (!empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) ) + $this->is_comment_feed = true; + if ( ! ($this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) { $this->is_home = true; } @@ -966,6 +1001,38 @@ class WP_Query { $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; } } + + // Comments feeds + if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) { + if ( $this->is_archive || $this->is_search ) { + $cjoin = "LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join "; + $cwhere = "WHERE comment_approved = '1' $where"; + $cgroupby = "GROUP BY $wpdb->comments.comment_id"; + } else { // Other non singular e.g. front + $cjoin = "LEFT JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )"; + $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'"; + $cgroupby = ''; + } + + $cjoin = apply_filters('comment_feed_join', $cjoin); + $cwhere = apply_filters('comment_feed_where', $cwhere); + $cgroupby = apply_filters('comment_feed_groupby', $cgroupby); + + $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby ORDER BY comment_date_gmt DESC LIMIT " . get_settings('posts_per_rss')); + $this->comment_count = count($this->comments); + + $post_ids = array(); + + foreach ($this->comments as $comment) + $post_ids[] = (int) $comment->comment_post_ID; + + $post_ids = join(',', $post_ids); + $join = ''; + if ( $post_ids ) + $where = "AND $wpdb->posts.ID IN ($post_ids) "; + else + $where = "AND 0"; + } // Apply post-paging filters on where and join. Only plugins that // manipulate paging queries should use these hooks. @@ -986,12 +1053,22 @@ class WP_Query { $this->request = apply_filters('posts_request', $request); $this->posts = $wpdb->get_results($this->request); + + if ( $this->is_comment_feed && $this->is_singular ) { + $cjoin = apply_filters('comment_feed_join', ''); + $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = {$this->posts[0]->ID} AND comment_approved = '1'"); + $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere ORDER BY comment_date_gmt DESC LIMIT " . get_settings('posts_per_rss'); + $this->comments = $wpdb->get_results($comments_request); + $this->comment_count = count($this->comments); + } + if ( !empty($limits) ) { $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' ); $this->found_posts = $wpdb->get_var( $found_posts_query ); $this->found_posts = apply_filters( 'found_posts', $this->found_posts ); $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); } + // Check post status to determine if post should be displayed. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { $status = get_post_status($this->posts[0]); @@ -1071,6 +1148,40 @@ class WP_Query { $this->post = $this->posts[0]; } } + + function next_comment() { + $this->current_comment++; + + $this->comment = $this->comments[$this->current_comment]; + return $this->comment; + } + + function the_comment() { + global $comment; + + $comment = $this->next_comment(); + + if ($this->current_comment == 0) { + do_action('comment_loop_start'); + } + } + + function have_comments() { + if ($this->current_comment + 1 < $this->comment_count) { + return true; + } elseif ($this->current_comment + 1 == $this->comment_count) { + $this->rewind_comments(); + } + + return false; + } + + function rewind_comments() { + $this->current_comment = -1; + if ($this->comment_count > 0) { + $this->comment = $this->comments[0]; + } + } function &query($query) { $this->parse_query($query);