3 * WordPress Comment Administration API.
6 * @subpackage Administration
11 * Determine if a comment exists based on author and date.
13 * For best performance, use `$timezone = 'gmt'`, which queries a field that is properly indexed. The default value
14 * for `$timezone` is 'blog' for legacy reasons.
17 * @since 4.4.0 Added the `$timezone` parameter.
19 * @global wpdb $wpdb WordPress database abstraction object.
21 * @param string $comment_author Author of the comment.
22 * @param string $comment_date Date of the comment.
23 * @param string $timezone Timezone. Accepts 'blog' or 'gmt'. Default 'blog'.
25 * @return mixed Comment post ID on success.
27 function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) {
30 $date_field = 'comment_date';
31 if ( 'gmt' === $timezone ) {
32 $date_field = 'comment_date_gmt';
35 return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
36 WHERE comment_author = %s AND $date_field = %s",
37 stripslashes( $comment_author ),
38 stripslashes( $comment_date )
43 * Update a comment with values provided in $_POST.
47 function edit_comment() {
48 if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) )
49 wp_die ( __( 'Sorry, you are not allowed to edit comments on this post.' ) );
51 if ( isset( $_POST['newcomment_author'] ) )
52 $_POST['comment_author'] = $_POST['newcomment_author'];
53 if ( isset( $_POST['newcomment_author_email'] ) )
54 $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
55 if ( isset( $_POST['newcomment_author_url'] ) )
56 $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
57 if ( isset( $_POST['comment_status'] ) )
58 $_POST['comment_approved'] = $_POST['comment_status'];
59 if ( isset( $_POST['content'] ) )
60 $_POST['comment_content'] = $_POST['content'];
61 if ( isset( $_POST['comment_ID'] ) )
62 $_POST['comment_ID'] = (int) $_POST['comment_ID'];
64 foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
65 if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
66 $_POST['edit_date'] = '1';
71 if ( !empty ( $_POST['edit_date'] ) ) {
78 $jj = ($jj > 31 ) ? 31 : $jj;
79 $hh = ($hh > 23 ) ? $hh -24 : $hh;
80 $mn = ($mn > 59 ) ? $mn -60 : $mn;
81 $ss = ($ss > 59 ) ? $ss -60 : $ss;
82 $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
85 wp_update_comment( $_POST );
89 * Returns a WP_Comment object based on comment ID.
93 * @param int $id ID of comment to retrieve.
94 * @return WP_Comment|false Comment if found. False on failure.
96 function get_comment_to_edit( $id ) {
97 if ( !$comment = get_comment($id) )
100 $comment->comment_ID = (int) $comment->comment_ID;
101 $comment->comment_post_ID = (int) $comment->comment_post_ID;
103 $comment->comment_content = format_to_edit( $comment->comment_content );
105 * Filters the comment content before editing.
109 * @param string $comment->comment_content Comment content.
111 $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
113 $comment->comment_author = format_to_edit( $comment->comment_author );
114 $comment->comment_author_email = format_to_edit( $comment->comment_author_email );
115 $comment->comment_author_url = format_to_edit( $comment->comment_author_url );
116 $comment->comment_author_url = esc_url($comment->comment_author_url);
122 * Get the number of pending comments on a post or posts
126 * @global wpdb $wpdb WordPress database abstraction object.
128 * @param int|array $post_id Either a single Post ID or an array of Post IDs
129 * @return int|array Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
131 function get_pending_comments_num( $post_id ) {
135 if ( !is_array($post_id) ) {
136 $post_id_array = (array) $post_id;
139 $post_id_array = $post_id;
141 $post_id_array = array_map('intval', $post_id_array);
142 $post_id_in = "'" . implode("', '", $post_id_array) . "'";
144 $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 );
147 if ( empty($pending) )
150 return absint($pending[0]['num_comments']);
153 $pending_keyed = array();
155 // Default to zero pending for all posts in request
156 foreach ( $post_id_array as $id )
157 $pending_keyed[$id] = 0;
159 if ( !empty($pending) )
160 foreach ( $pending as $pend )
161 $pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);
163 return $pending_keyed;
167 * Add avatars to relevant places in admin, or try to.
171 * @param string $name User name.
172 * @return string Avatar with Admin name.
174 function floated_admin_avatar( $name ) {
175 $avatar = get_avatar( get_comment(), 32, 'mystery' );
176 return "$avatar $name";
182 function enqueue_comment_hotkeys_js() {
183 if ( 'true' == get_user_option( 'comment_shortcuts' ) )
184 wp_enqueue_script( 'jquery-table-hotkeys' );
188 * Display error message at bottom of comments.
190 * @param string $msg Error Message. Assumed to contain HTML and be sanitized.
192 function comment_footer_die( $msg ) {
193 echo "<div class='wrap'><p>$msg</p></div>";
194 include( ABSPATH . 'wp-admin/admin-footer.php' );