]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/comment.php
WordPress 4.1
[autoinstalls/wordpress.git] / wp-admin / includes / comment.php
1 <?php
2 /**
3  * WordPress Comment Administration API.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * Determine if a comment exists based on author and date.
11  *
12  * @since 2.0.0
13  *
14  * @global wpdb $wpdb WordPress database abstraction object.
15  *
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.
19  */
20 function comment_exists($comment_author, $comment_date) {
21         global $wpdb;
22
23         $comment_author = stripslashes($comment_author);
24         $comment_date = stripslashes($comment_date);
25
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) );
28 }
29
30 /**
31  * Update a comment with values provided in $_POST.
32  *
33  * @since 2.0.0
34  */
35 function edit_comment() {
36
37         if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) )
38                 wp_die ( __( 'You are not allowed to edit comments on this post.' ) );
39
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'];
52
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';
56                         break;
57                 }
58         }
59
60         if ( !empty ( $_POST['edit_date'] ) ) {
61                 $aa = $_POST['aa'];
62                 $mm = $_POST['mm'];
63                 $jj = $_POST['jj'];
64                 $hh = $_POST['hh'];
65                 $mn = $_POST['mn'];
66                 $ss = $_POST['ss'];
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";
72         }
73
74         wp_update_comment( $_POST );
75 }
76
77 /**
78  * Returns a comment object based on comment ID.
79  *
80  * @since 2.0.0
81  *
82  * @param int $id ID of comment to retrieve.
83  * @return bool|object Comment if found. False on failure.
84  */
85 function get_comment_to_edit( $id ) {
86         if ( !$comment = get_comment($id) )
87                 return false;
88
89         $comment->comment_ID = (int) $comment->comment_ID;
90         $comment->comment_post_ID = (int) $comment->comment_post_ID;
91
92         $comment->comment_content = format_to_edit( $comment->comment_content );
93         /**
94          * Filter the comment content before editing.
95          *
96          * @since 2.0.0
97          *
98          * @param string $comment->comment_content Comment content.
99          */
100         $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
101
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);
106
107         return $comment;
108 }
109
110 /**
111  * Get the number of pending comments on a post or posts
112  *
113  * @since 2.3.0
114  *
115  * @global wpdb $wpdb WordPress database abstraction object.
116  *
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
119  */
120 function get_pending_comments_num( $post_id ) {
121         global $wpdb;
122
123         $single = false;
124         if ( !is_array($post_id) ) {
125                 $post_id_array = (array) $post_id;
126                 $single = true;
127         } else {
128                 $post_id_array = $post_id;
129         }
130         $post_id_array = array_map('intval', $post_id_array);
131         $post_id_in = "'" . implode("', '", $post_id_array) . "'";
132
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 );
134
135         if ( $single ) {
136                 if ( empty($pending) )
137                         return 0;
138                 else
139                         return absint($pending[0]['num_comments']);
140         }
141
142         $pending_keyed = array();
143
144         // Default to zero pending for all posts in request
145         foreach ( $post_id_array as $id )
146                 $pending_keyed[$id] = 0;
147
148         if ( !empty($pending) )
149                 foreach ( $pending as $pend )
150                         $pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);
151
152         return $pending_keyed;
153 }
154
155 /**
156  * Add avatars to relevant places in admin, or try to.
157  *
158  * @since 2.5.0
159  * @uses $comment
160  *
161  * @param string $name User name.
162  * @return string Avatar with Admin name.
163  */
164 function floated_admin_avatar( $name ) {
165         global $comment;
166         $avatar = get_avatar( $comment, 32, 'mystery' );
167         return "$avatar $name";
168 }
169
170 function enqueue_comment_hotkeys_js() {
171         if ( 'true' == get_user_option( 'comment_shortcuts' ) )
172                 wp_enqueue_script( 'jquery-table-hotkeys' );
173 }