]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/comment.php
WordPress 4.3.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         return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
24                         WHERE comment_author = %s AND comment_date = %s",
25                         stripslashes( $comment_author ),
26                         stripslashes( $comment_date )
27         ) );
28 }
29
30 /**
31  * Update a comment with values provided in $_POST.
32  *
33  * @since 2.0.0
34  */
35 function edit_comment() {
36         if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) )
37                 wp_die ( __( 'You are not allowed to edit comments on this post.' ) );
38
39         if ( isset( $_POST['newcomment_author'] ) )
40                 $_POST['comment_author'] = $_POST['newcomment_author'];
41         if ( isset( $_POST['newcomment_author_email'] ) )
42                 $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
43         if ( isset( $_POST['newcomment_author_url'] ) )
44                 $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
45         if ( isset( $_POST['comment_status'] ) )
46                 $_POST['comment_approved'] = $_POST['comment_status'];
47         if ( isset( $_POST['content'] ) )
48                 $_POST['comment_content'] = $_POST['content'];
49         if ( isset( $_POST['comment_ID'] ) )
50                 $_POST['comment_ID'] = (int) $_POST['comment_ID'];
51
52         foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
53                 if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
54                         $_POST['edit_date'] = '1';
55                         break;
56                 }
57         }
58
59         if ( !empty ( $_POST['edit_date'] ) ) {
60                 $aa = $_POST['aa'];
61                 $mm = $_POST['mm'];
62                 $jj = $_POST['jj'];
63                 $hh = $_POST['hh'];
64                 $mn = $_POST['mn'];
65                 $ss = $_POST['ss'];
66                 $jj = ($jj > 31 ) ? 31 : $jj;
67                 $hh = ($hh > 23 ) ? $hh -24 : $hh;
68                 $mn = ($mn > 59 ) ? $mn -60 : $mn;
69                 $ss = ($ss > 59 ) ? $ss -60 : $ss;
70                 $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
71         }
72
73         wp_update_comment( $_POST );
74 }
75
76 /**
77  * Returns a comment object based on comment ID.
78  *
79  * @since 2.0.0
80  *
81  * @param int $id ID of comment to retrieve.
82  * @return object|false Comment if found. False on failure.
83  */
84 function get_comment_to_edit( $id ) {
85         if ( !$comment = get_comment($id) )
86                 return false;
87
88         $comment->comment_ID = (int) $comment->comment_ID;
89         $comment->comment_post_ID = (int) $comment->comment_post_ID;
90
91         $comment->comment_content = format_to_edit( $comment->comment_content );
92         /**
93          * Filter the comment content before editing.
94          *
95          * @since 2.0.0
96          *
97          * @param string $comment->comment_content Comment content.
98          */
99         $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
100
101         $comment->comment_author = format_to_edit( $comment->comment_author );
102         $comment->comment_author_email = format_to_edit( $comment->comment_author_email );
103         $comment->comment_author_url = format_to_edit( $comment->comment_author_url );
104         $comment->comment_author_url = esc_url($comment->comment_author_url);
105
106         return $comment;
107 }
108
109 /**
110  * Get the number of pending comments on a post or posts
111  *
112  * @since 2.3.0
113  *
114  * @global wpdb $wpdb WordPress database abstraction object.
115  *
116  * @param int|array $post_id Either a single Post ID or an array of Post IDs
117  * @return int|array Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
118  */
119 function get_pending_comments_num( $post_id ) {
120         global $wpdb;
121
122         $single = false;
123         if ( !is_array($post_id) ) {
124                 $post_id_array = (array) $post_id;
125                 $single = true;
126         } else {
127                 $post_id_array = $post_id;
128         }
129         $post_id_array = array_map('intval', $post_id_array);
130         $post_id_in = "'" . implode("', '", $post_id_array) . "'";
131
132         $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 );
133
134         if ( $single ) {
135                 if ( empty($pending) )
136                         return 0;
137                 else
138                         return absint($pending[0]['num_comments']);
139         }
140
141         $pending_keyed = array();
142
143         // Default to zero pending for all posts in request
144         foreach ( $post_id_array as $id )
145                 $pending_keyed[$id] = 0;
146
147         if ( !empty($pending) )
148                 foreach ( $pending as $pend )
149                         $pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);
150
151         return $pending_keyed;
152 }
153
154 /**
155  * Add avatars to relevant places in admin, or try to.
156  *
157  * @since 2.5.0
158  *
159  * @global object $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 /**
171  * @since 2.7.0
172  */
173 function enqueue_comment_hotkeys_js() {
174         if ( 'true' == get_user_option( 'comment_shortcuts' ) )
175                 wp_enqueue_script( 'jquery-table-hotkeys' );
176 }