source: recent-comments/trunk/recent-comments.php @ 339423

Revision 339423, 6.3 KB checked in by Viper007Bond, 16 months ago (diff)

Version 2.0.0!

Line 
1<?php
2/*
3Plugin Name: Recent Comments
4Plugin URI:  http://wordpress.org/extend/plugins/recent-comments/
5Description: Retrieves a list of the most recent comments.
6Version:     2.0
7Author:      Automattic
8Author URI:  http://automattic.com/
9*/
10
11
12// Fetch the latest comments. Works like get_comments(), but accepts some additional arguments relating to posts.
13function get_most_recent_comments( $args = null ) {
14        global $most_recent_comments_args;
15
16        // You can pass any of these arguments as well as any argument supported by get_comments()
17        $defaults = array(
18                'passworded_posts' => false, // Include password protected posts?
19                'showpings'        => false, // Include pingbacks and trackbacks?
20                'post_types'       => array( 'post', 'page' ),     // Array of post types to use
21                'post_statuses'    => array( 'publish', 'static' ), // Array of post statuses to include
22
23                // Standard get_comments() args
24                'number'           => 5,
25                'status'           => 'approve', // Hide comments in moderation
26        );
27
28        $most_recent_comments_args = wp_parse_args( $args, $defaults );
29
30        // Create the cache key
31        $key = md5( serialize( $most_recent_comments_args ) );
32        $last_changed = wp_cache_get( 'last_changed', 'comment' );
33        if ( ! $last_changed ) {
34                $last_changed = time();
35                wp_cache_set( 'last_changed', $last_changed, 'comment' );
36        }
37        $cache_key = "most_recent_comments:$key:$last_changed";
38
39        // Check to see if we already have results for this request
40        if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
41                return $cache;
42        }
43
44        // Modify the get_comments() SQL query
45        add_filter( 'comments_clauses', '_mrc_modify_comments_clauses' );
46
47        // Get the comments
48        // The custom arguments will be ignored by get_comments()
49        $comments = get_comments( $most_recent_comments_args );
50
51        // Remove the get_comments() SQL query filter
52        remove_filter( 'comments_clauses', '_mrc_modify_comments_clauses' );
53
54        // Cache these results
55        wp_cache_add( $cache_key, $comments, 'comment' );
56
57        return $comments;
58}
59
60// Output an unordered list (<ul>) of the most recent comments
61function list_most_recent_comments( $args = null ) {
62
63        // These are in addition to the args of get_most_recent_comments() and get_comments()
64        $defaults = array(
65                'excerpt_words'    => 0, // The Number of words to display from the comment
66                'excerpt_chars'    => 0, // Or the nmber of characters to display from the comment
67                'comment_format'   => 0, // Pick from some pre-defined formats
68        );
69
70        $r = wp_parse_args( $args, $defaults );
71
72        $comments = get_most_recent_comments( $r );
73
74        $output = "<ul>\n";
75
76        if ( $comments ) {
77                $idx = 0;
78                foreach ( $comments as $comment ) {
79                        $comment_author = stripslashes( $comment->comment_author );
80                        if ( empty( $comment_author ) )
81                                $comment_author = __( 'anonymous' );
82
83                        $comment_content = strip_tags( $comment->comment_content );
84                        $comment_content = stripslashes( $comment_content );
85
86                        if ( 0 != $r['excerpt_words'] ) {
87                                $words = explode( ' ', $comment_content );
88                                $comment_content = implode( ' ', array_slice( $words, 0, $r['excerpt_words'] ) );
89                        } elseif ( 0 != $r['excerpt_chars'] ) {
90                                $comment_content = substr( $comment_content, 0, $r['excerpt_chars'] );
91                        }
92
93                        $comment_permalink = get_comment_link( $comment->comment_ID );
94
95                        if ( 1 == $r['comment_format'] ) {
96                                $post_title = stripslashes( $comment->post_title );
97                                $post_id= stripslashes( $comment->post_id );
98                                $url = $comment->comment_author_url;
99
100                                $idx++;
101                                if ( 1 == $idx % 2 )
102                                        $before = '<li class="statsclass1">';
103                                else
104                                        $before = '<li class="statsclass2">';
105
106                                $output .= "$before<a href='$comment_permalink'>$comment_author</a> on <a href='" . get_permalink( $comment->ID ) . "'>$post_title</a>$after";
107                        } else {
108                                $idx++;
109                                if ( 1 == $idx % 2 )
110                                        $before = "<li class='statsclass1'>";
111                                else
112                                        $before = "<li class='statsclass2'>";
113       
114                                $output .= "$before<strong>$comment_author:</strong> <a href='$comment_permalink' title='" . sprintf( __( 'View the entire comment by %s' ), $comment_author ) . "'>$comment_content</a>$after";
115                        }
116                }
117
118                $output = convert_smilies( $output );
119        } else {
120                $output .= '<li>' . __( 'None Found' ) . '</li>';
121        }
122
123        $output .= "</ul>\n";
124
125        echo $output;
126}
127
128
129// Deprecated, use list_most_recent_comments() instead
130function most_recent_comments( $no_comments = 5, $comment_lenth = 5, $deprecated1 = null, $deprecated2 = null, $show_pass_post = false, $comment_style = 0, $hide_pingbacks_trackbacks = false ) {
131        $args = array(
132                'number'           => $no_comments,
133                'passworded_posts' => $show_pass_post,
134                'excerpt_words'    => $comment_lenth,
135                'comment_format'   => $comment_style,
136        );
137        $args['showpings'] = ! $hide_pingbacks_trackbacks; // Inverse
138
139        list_most_recent_comments( $args );
140}
141
142
143// Helper function for get_most_recent_comments(), you don't need to use this function
144function _mrc_modify_comments_clauses( $clauses ) {
145        global $wpdb, $most_recent_comments_args;
146
147        // Join in the posts table
148        $clauses['join'] .= " LEFT JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID ";
149
150        // Controls showing comments on password protected posts or not
151        if ( empty( $most_recent_comments_args['passworded_posts'] ) )
152                $clauses['where'] .= " AND $wpdb->posts.post_password = ''";
153
154        // Controls what post_types to use
155        if ( ! empty( $most_recent_comments_args['post_types'] ) && is_array( $most_recent_comments_args['post_types'] ) ) {
156                $post_types = array();
157
158                foreach ( $most_recent_comments_args['post_types'] as $post_type ) {
159                        if ( post_type_exists( $post_type ) ) {
160                                $post_types[] = $post_type;
161                        }
162                }
163
164                if ( ! empty( $post_types ) ) {
165                        $post_types = array_map( 'esc_sql', $post_types );
166                        $clauses['where'] .= " AND $wpdb->posts.post_type IN ( '" . implode( "', '", $post_types ) . "' )";
167                }
168        }
169
170        // Controls what post statuses to use
171        if ( ! empty( $most_recent_comments_args['post_statuses'] ) && is_array( $most_recent_comments_args['post_statuses'] ) ) {
172                $most_recent_comments_args['post_statuses'] = array_map( 'esc_sql', $most_recent_comments_args['post_statuses'] );
173                $clauses['where'] .= " AND $wpdb->posts.post_status IN ( '" . implode( "', '", $most_recent_comments_args['post_statuses'] ) . "' )";
174        }
175
176        return $clauses;
177}
178
179?>
Note: See TracBrowser for help on using the repository browser.