2 /** |
2 /** |
3 * WordPress Comment Administration API. |
3 * WordPress Comment Administration API. |
4 * |
4 * |
5 * @package WordPress |
5 * @package WordPress |
6 * @subpackage Administration |
6 * @subpackage Administration |
|
7 * @since 2.3.0 |
7 */ |
8 */ |
8 |
9 |
9 /** |
10 /** |
10 * Determine if a comment exists based on author and date. |
11 * Determine if a comment exists based on author and date. |
11 * |
12 * |
|
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. |
|
15 * |
12 * @since 2.0.0 |
16 * @since 2.0.0 |
|
17 * @since 4.4.0 Added the `$timezone` parameter. |
13 * |
18 * |
14 * @global wpdb $wpdb WordPress database abstraction object. |
19 * @global wpdb $wpdb WordPress database abstraction object. |
15 * |
20 * |
16 * @param string $comment_author Author of the comment |
21 * @param string $comment_author Author of the comment. |
17 * @param string $comment_date Date of the comment |
22 * @param string $comment_date Date of the comment. |
|
23 * @param string $timezone Timezone. Accepts 'blog' or 'gmt'. Default 'blog'. |
|
24 * |
18 * @return mixed Comment post ID on success. |
25 * @return mixed Comment post ID on success. |
19 */ |
26 */ |
20 function comment_exists($comment_author, $comment_date) { |
27 function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) { |
21 global $wpdb; |
28 global $wpdb; |
22 |
29 |
23 $comment_author = stripslashes($comment_author); |
30 $date_field = 'comment_date'; |
24 $comment_date = stripslashes($comment_date); |
31 if ( 'gmt' === $timezone ) { |
|
32 $date_field = 'comment_date_gmt'; |
|
33 } |
25 |
34 |
26 return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments |
35 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) ); |
36 WHERE comment_author = %s AND $date_field = %s", |
|
37 stripslashes( $comment_author ), |
|
38 stripslashes( $comment_date ) |
|
39 ) ); |
28 } |
40 } |
29 |
41 |
30 /** |
42 /** |
31 * Update a comment with values provided in $_POST. |
43 * Update a comment with values provided in $_POST. |
32 * |
44 * |
33 * @since 2.0.0 |
45 * @since 2.0.0 |
34 */ |
46 */ |
35 function edit_comment() { |
47 function edit_comment() { |
36 |
|
37 if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) ) |
48 if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) ) |
38 wp_die ( __( 'You are not allowed to edit comments on this post.' ) ); |
49 wp_die ( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); |
39 |
50 |
40 if ( isset( $_POST['newcomment_author'] ) ) |
51 if ( isset( $_POST['newcomment_author'] ) ) |
41 $_POST['comment_author'] = $_POST['newcomment_author']; |
52 $_POST['comment_author'] = $_POST['newcomment_author']; |
42 if ( isset( $_POST['newcomment_author_email'] ) ) |
53 if ( isset( $_POST['newcomment_author_email'] ) ) |
43 $_POST['comment_author_email'] = $_POST['newcomment_author_email']; |
54 $_POST['comment_author_email'] = $_POST['newcomment_author_email']; |
73 |
84 |
74 wp_update_comment( $_POST ); |
85 wp_update_comment( $_POST ); |
75 } |
86 } |
76 |
87 |
77 /** |
88 /** |
78 * Returns a comment object based on comment ID. |
89 * Returns a WP_Comment object based on comment ID. |
79 * |
90 * |
80 * @since 2.0.0 |
91 * @since 2.0.0 |
81 * |
92 * |
82 * @param int $id ID of comment to retrieve. |
93 * @param int $id ID of comment to retrieve. |
83 * @return bool|object Comment if found. False on failure. |
94 * @return WP_Comment|false Comment if found. False on failure. |
84 */ |
95 */ |
85 function get_comment_to_edit( $id ) { |
96 function get_comment_to_edit( $id ) { |
86 if ( !$comment = get_comment($id) ) |
97 if ( !$comment = get_comment($id) ) |
87 return false; |
98 return false; |
88 |
99 |
89 $comment->comment_ID = (int) $comment->comment_ID; |
100 $comment->comment_ID = (int) $comment->comment_ID; |
90 $comment->comment_post_ID = (int) $comment->comment_post_ID; |
101 $comment->comment_post_ID = (int) $comment->comment_post_ID; |
91 |
102 |
92 $comment->comment_content = format_to_edit( $comment->comment_content ); |
103 $comment->comment_content = format_to_edit( $comment->comment_content ); |
93 /** |
104 /** |
94 * Filter the comment content before editing. |
105 * Filters the comment content before editing. |
95 * |
106 * |
96 * @since 2.0.0 |
107 * @since 2.0.0 |
97 * |
108 * |
98 * @param string $comment->comment_content Comment content. |
109 * @param string $comment->comment_content Comment content. |
99 */ |
110 */ |
154 |
165 |
155 /** |
166 /** |
156 * Add avatars to relevant places in admin, or try to. |
167 * Add avatars to relevant places in admin, or try to. |
157 * |
168 * |
158 * @since 2.5.0 |
169 * @since 2.5.0 |
159 * @uses $comment |
|
160 * |
170 * |
161 * @param string $name User name. |
171 * @param string $name User name. |
162 * @return string Avatar with Admin name. |
172 * @return string Avatar with Admin name. |
163 */ |
173 */ |
164 function floated_admin_avatar( $name ) { |
174 function floated_admin_avatar( $name ) { |
165 global $comment; |
175 $avatar = get_avatar( get_comment(), 32, 'mystery' ); |
166 $avatar = get_avatar( $comment, 32, 'mystery' ); |
|
167 return "$avatar $name"; |
176 return "$avatar $name"; |
168 } |
177 } |
169 |
178 |
|
179 /** |
|
180 * @since 2.7.0 |
|
181 */ |
170 function enqueue_comment_hotkeys_js() { |
182 function enqueue_comment_hotkeys_js() { |
171 if ( 'true' == get_user_option( 'comment_shortcuts' ) ) |
183 if ( 'true' == get_user_option( 'comment_shortcuts' ) ) |
172 wp_enqueue_script( 'jquery-table-hotkeys' ); |
184 wp_enqueue_script( 'jquery-table-hotkeys' ); |
173 } |
185 } |
|
186 |
|
187 /** |
|
188 * Display error message at bottom of comments. |
|
189 * |
|
190 * @param string $msg Error Message. Assumed to contain HTML and be sanitized. |
|
191 */ |
|
192 function comment_footer_die( $msg ) { |
|
193 echo "<div class='wrap'><p>$msg</p></div>"; |
|
194 include( ABSPATH . 'wp-admin/admin-footer.php' ); |
|
195 die; |
|
196 } |