author | ymh <ymh.work@gmail.com> |
Tue, 15 Oct 2019 15:48:13 +0200 | |
changeset 13 | d255fe9cd479 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Comment Administration API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* @since 2.3.0 |
0 | 8 |
*/ |
9 |
||
10 |
/** |
|
11 |
* Determine if a comment exists based on author and date. |
|
12 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* For best performance, use `$timezone = 'gmt'`, which queries a field that is properly indexed. The default value |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* for `$timezone` is 'blog' for legacy reasons. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* |
0 | 16 |
* @since 2.0.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* @since 4.4.0 Added the `$timezone` parameter. |
5 | 18 |
* |
19 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 20 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* @param string $comment_author Author of the comment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* @param string $comment_date Date of the comment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* @param string $timezone Timezone. Accepts 'blog' or 'gmt'. Default 'blog'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
* |
0 | 25 |
* @return mixed Comment post ID on success. |
26 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) { |
0 | 28 |
global $wpdb; |
29 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
$date_field = 'comment_date'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
if ( 'gmt' === $timezone ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
$date_field = 'comment_date_gmt'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
} |
0 | 34 |
|
9 | 35 |
return $wpdb->get_var( |
36 |
$wpdb->prepare( |
|
37 |
"SELECT comment_post_ID FROM $wpdb->comments |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
WHERE comment_author = %s AND $date_field = %s", |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
stripslashes( $comment_author ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
stripslashes( $comment_date ) |
9 | 41 |
) |
42 |
); |
|
0 | 43 |
} |
44 |
||
45 |
/** |
|
46 |
* Update a comment with values provided in $_POST. |
|
47 |
* |
|
48 |
* @since 2.0.0 |
|
49 |
*/ |
|
50 |
function edit_comment() { |
|
9 | 51 |
if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) ) { |
52 |
wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); |
|
53 |
} |
|
0 | 54 |
|
9 | 55 |
if ( isset( $_POST['newcomment_author'] ) ) { |
0 | 56 |
$_POST['comment_author'] = $_POST['newcomment_author']; |
9 | 57 |
} |
58 |
if ( isset( $_POST['newcomment_author_email'] ) ) { |
|
0 | 59 |
$_POST['comment_author_email'] = $_POST['newcomment_author_email']; |
9 | 60 |
} |
61 |
if ( isset( $_POST['newcomment_author_url'] ) ) { |
|
0 | 62 |
$_POST['comment_author_url'] = $_POST['newcomment_author_url']; |
9 | 63 |
} |
64 |
if ( isset( $_POST['comment_status'] ) ) { |
|
0 | 65 |
$_POST['comment_approved'] = $_POST['comment_status']; |
9 | 66 |
} |
67 |
if ( isset( $_POST['content'] ) ) { |
|
0 | 68 |
$_POST['comment_content'] = $_POST['content']; |
9 | 69 |
} |
70 |
if ( isset( $_POST['comment_ID'] ) ) { |
|
0 | 71 |
$_POST['comment_ID'] = (int) $_POST['comment_ID']; |
9 | 72 |
} |
0 | 73 |
|
9 | 74 |
foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) { |
75 |
if ( ! empty( $_POST[ 'hidden_' . $timeunit ] ) && $_POST[ 'hidden_' . $timeunit ] != $_POST[ $timeunit ] ) { |
|
0 | 76 |
$_POST['edit_date'] = '1'; |
77 |
break; |
|
78 |
} |
|
79 |
} |
|
80 |
||
9 | 81 |
if ( ! empty( $_POST['edit_date'] ) ) { |
82 |
$aa = $_POST['aa']; |
|
83 |
$mm = $_POST['mm']; |
|
84 |
$jj = $_POST['jj']; |
|
85 |
$hh = $_POST['hh']; |
|
86 |
$mn = $_POST['mn']; |
|
87 |
$ss = $_POST['ss']; |
|
88 |
$jj = ( $jj > 31 ) ? 31 : $jj; |
|
89 |
$hh = ( $hh > 23 ) ? $hh - 24 : $hh; |
|
90 |
$mn = ( $mn > 59 ) ? $mn - 60 : $mn; |
|
91 |
$ss = ( $ss > 59 ) ? $ss - 60 : $ss; |
|
0 | 92 |
$_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; |
93 |
} |
|
94 |
||
95 |
wp_update_comment( $_POST ); |
|
96 |
} |
|
97 |
||
98 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
* Returns a WP_Comment object based on comment ID. |
0 | 100 |
* |
101 |
* @since 2.0.0 |
|
102 |
* |
|
103 |
* @param int $id ID of comment to retrieve. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
* @return WP_Comment|false Comment if found. False on failure. |
0 | 105 |
*/ |
106 |
function get_comment_to_edit( $id ) { |
|
9 | 107 |
if ( ! $comment = get_comment( $id ) ) { |
0 | 108 |
return false; |
9 | 109 |
} |
0 | 110 |
|
9 | 111 |
$comment->comment_ID = (int) $comment->comment_ID; |
0 | 112 |
$comment->comment_post_ID = (int) $comment->comment_post_ID; |
113 |
||
114 |
$comment->comment_content = format_to_edit( $comment->comment_content ); |
|
115 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
* Filters the comment content before editing. |
0 | 117 |
* |
118 |
* @since 2.0.0 |
|
119 |
* |
|
120 |
* @param string $comment->comment_content Comment content. |
|
121 |
*/ |
|
122 |
$comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content ); |
|
123 |
||
9 | 124 |
$comment->comment_author = format_to_edit( $comment->comment_author ); |
0 | 125 |
$comment->comment_author_email = format_to_edit( $comment->comment_author_email ); |
9 | 126 |
$comment->comment_author_url = format_to_edit( $comment->comment_author_url ); |
127 |
$comment->comment_author_url = esc_url( $comment->comment_author_url ); |
|
0 | 128 |
|
129 |
return $comment; |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Get the number of pending comments on a post or posts |
|
134 |
* |
|
135 |
* @since 2.3.0 |
|
5 | 136 |
* |
137 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 138 |
* |
139 |
* @param int|array $post_id Either a single Post ID or an array of Post IDs |
|
140 |
* @return int|array Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs |
|
141 |
*/ |
|
142 |
function get_pending_comments_num( $post_id ) { |
|
143 |
global $wpdb; |
|
144 |
||
145 |
$single = false; |
|
9 | 146 |
if ( ! is_array( $post_id ) ) { |
0 | 147 |
$post_id_array = (array) $post_id; |
9 | 148 |
$single = true; |
0 | 149 |
} else { |
150 |
$post_id_array = $post_id; |
|
151 |
} |
|
9 | 152 |
$post_id_array = array_map( 'intval', $post_id_array ); |
153 |
$post_id_in = "'" . implode( "', '", $post_id_array ) . "'"; |
|
0 | 154 |
|
155 |
$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 ); |
|
156 |
||
157 |
if ( $single ) { |
|
9 | 158 |
if ( empty( $pending ) ) { |
0 | 159 |
return 0; |
9 | 160 |
} else { |
161 |
return absint( $pending[0]['num_comments'] ); |
|
162 |
} |
|
0 | 163 |
} |
164 |
||
165 |
$pending_keyed = array(); |
|
166 |
||
167 |
// Default to zero pending for all posts in request |
|
9 | 168 |
foreach ( $post_id_array as $id ) { |
169 |
$pending_keyed[ $id ] = 0; |
|
170 |
} |
|
0 | 171 |
|
9 | 172 |
if ( ! empty( $pending ) ) { |
173 |
foreach ( $pending as $pend ) { |
|
174 |
$pending_keyed[ $pend['comment_post_ID'] ] = absint( $pend['num_comments'] ); |
|
175 |
} |
|
176 |
} |
|
0 | 177 |
|
178 |
return $pending_keyed; |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Add avatars to relevant places in admin, or try to. |
|
183 |
* |
|
184 |
* @since 2.5.0 |
|
185 |
* |
|
186 |
* @param string $name User name. |
|
187 |
* @return string Avatar with Admin name. |
|
188 |
*/ |
|
189 |
function floated_admin_avatar( $name ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
$avatar = get_avatar( get_comment(), 32, 'mystery' ); |
0 | 191 |
return "$avatar $name"; |
192 |
} |
|
193 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
*/ |
0 | 197 |
function enqueue_comment_hotkeys_js() { |
9 | 198 |
if ( 'true' == get_user_option( 'comment_shortcuts' ) ) { |
0 | 199 |
wp_enqueue_script( 'jquery-table-hotkeys' ); |
9 | 200 |
} |
0 | 201 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
* Display error message at bottom of comments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
* @param string $msg Error Message. Assumed to contain HTML and be sanitized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
function comment_footer_die( $msg ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
echo "<div class='wrap'><p>$msg</p></div>"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
210 |
include( ABSPATH . 'wp-admin/admin-footer.php' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
die; |
9 | 212 |
} |