3 * WordPress Comment Administration API.
6 * @subpackage Administration
10 * Determine if a comment exists based on author and date.
14 * @global wpdb $wpdb WordPress database abstraction object.
16 * @param string $comment_author Author of the comment
17 * @param string $comment_date Date of the comment
18 * @return mixed Comment post ID on success.
20 function comment_exists($comment_author, $comment_date) {
23 $comment_author = stripslashes($comment_author);
24 $comment_date = stripslashes($comment_date);
26 return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
27 WHERE comment_author = %s AND comment_date = %s", $comment_author, $comment_date) );
31 * Update a comment with values provided in $_POST.
35 function edit_comment() {
37 if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) )
38 wp_die ( __( 'You are not allowed to edit comments on this post.' ) );
40 if ( isset( $_POST['newcomment_author'] ) )
41 $_POST['comment_author'] = $_POST['newcomment_author'];
42 if ( isset( $_POST['newcomment_author_email'] ) )
43 $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
44 if ( isset( $_POST['newcomment_author_url'] ) )
45 $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
46 if ( isset( $_POST['comment_status'] ) )
47 $_POST['comment_approved'] = $_POST['comment_status'];
48 if ( isset( $_POST['content'] ) )
49 $_POST['comment_content'] = $_POST['content'];
50 if ( isset( $_POST['comment_ID'] ) )
51 $_POST['comment_ID'] = (int) $_POST['comment_ID'];
53 foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
54 if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
55 $_POST['edit_date'] = '1';
60 if ( !empty ( $_POST['edit_date'] ) ) {
67 $jj = ($jj > 31 ) ? 31 : $jj;
68 $hh = ($hh > 23 ) ? $hh -24 : $hh;
69 $mn = ($mn > 59 ) ? $mn -60 : $mn;
70 $ss = ($ss > 59 ) ? $ss -60 : $ss;
71 $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
74 wp_update_comment( $_POST );
78 * Returns a comment object based on comment ID.
82 * @param int $id ID of comment to retrieve.
83 * @return bool|object Comment if found. False on failure.
85 function get_comment_to_edit( $id ) {
86 if ( !$comment = get_comment($id) )
89 $comment->comment_ID = (int) $comment->comment_ID;
90 $comment->comment_post_ID = (int) $comment->comment_post_ID;
92 $comment->comment_content = format_to_edit( $comment->comment_content );
94 * Filter the comment content before editing.
98 * @param string $comment->comment_content Comment content.
100 $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
102 $comment->comment_author = format_to_edit( $comment->comment_author );
103 $comment->comment_author_email = format_to_edit( $comment->comment_author_email );
104 $comment->comment_author_url = format_to_edit( $comment->comment_author_url );
105 $comment->comment_author_url = esc_url($comment->comment_author_url);
111 * Get the number of pending comments on a post or posts
115 * @global wpdb $wpdb WordPress database abstraction object.
117 * @param int|array $post_id Either a single Post ID or an array of Post IDs
118 * @return int|array Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
120 function get_pending_comments_num( $post_id ) {
124 if ( !is_array($post_id) ) {
125 $post_id_array = (array) $post_id;
128 $post_id_array = $post_id;
130 $post_id_array = array_map('intval', $post_id_array);
131 $post_id_in = "'" . implode("', '", $post_id_array) . "'";
133 $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_A );
136 if ( empty($pending) )
139 return absint($pending[0]['num_comments']);
142 $pending_keyed = array();
144 // Default to zero pending for all posts in request
145 foreach ( $post_id_array as $id )
146 $pending_keyed[$id] = 0;
148 if ( !empty($pending) )
149 foreach ( $pending as $pend )
150 $pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);
152 return $pending_keyed;
156 * Add avatars to relevant places in admin, or try to.
161 * @param string $name User name.
162 * @return string Avatar with Admin name.
164 function floated_admin_avatar( $name ) {
166 $avatar = get_avatar( $comment, 32, 'mystery' );
167 return "$avatar $name";
170 function enqueue_comment_hotkeys_js() {
171 if ( 'true' == get_user_option( 'comment_shortcuts' ) )
172 wp_enqueue_script( 'jquery-table-hotkeys' );