Projects : mp-wp : mp-wp_genesis
1 | <?php |
2 | /** |
3 | * Manages WordPress comments |
4 | * |
5 | * @package WordPress |
6 | * @subpackage Comment |
7 | */ |
8 | |
9 | /** |
10 | * Checks whether a comment passes internal checks to be allowed to add. |
11 | * |
12 | * If comment moderation is set in the administration, then all comments, |
13 | * regardless of their type and whitelist will be set to false. If the number of |
14 | * links exceeds the amount in the administration, then the check fails. If any |
15 | * of the parameter contents match the blacklist of words, then the check fails. |
16 | * |
17 | * If the number of links exceeds the amount in the administration, then the |
18 | * check fails. If any of the parameter contents match the blacklist of words, |
19 | * then the check fails. |
20 | * |
21 | * If the comment is a trackback and part of the blogroll, then the trackback is |
22 | * automatically whitelisted. If the comment author was approved before, then |
23 | * the comment is automatically whitelisted. |
24 | * |
25 | * If none of the checks fail, then the failback is to set the check to pass |
26 | * (return true). |
27 | * |
28 | * @since 1.2.0 |
29 | * @uses $wpdb |
30 | * |
31 | * @param string $author Comment Author's name |
32 | * @param string $email Comment Author's email |
33 | * @param string $url Comment Author's URL |
34 | * @param string $comment Comment contents |
35 | * @param string $user_ip Comment Author's IP address |
36 | * @param string $user_agent Comment Author's User Agent |
37 | * @param string $comment_type Comment type, either user submitted comment, |
38 | * trackback, or pingback |
39 | * @return bool Whether the checks passed (true) and the comments should be |
40 | * displayed or set to moderated |
41 | */ |
42 | function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) { |
43 | global $wpdb; |
44 | |
45 | if ( 1 == get_option('comment_moderation') ) return 0; // If moderation is set to manual |
46 | |
47 | $textchk = apply_filters('comment_text', $comment); |
48 | $linkcount = preg_match_all("|(href\t*?=\t*?['\"]?)?(https?:)?//|i", $textchk, $out); |
49 | $plinkcount = preg_match_all("|(href\t*?=\t*?['\"]?)?(https?:)?//trilema\.com/|i",$textchk , $out); |
50 | $linkcount_result = $linkcount - $plinkcount; |
51 | |
52 | $mod_keys = trim(get_option('moderation_keys')); |
53 | if ( !empty($mod_keys) ) { |
54 | $words = explode("\n", $mod_keys ); |
55 | |
56 | foreach ( (array) $words as $word) { |
57 | $word = trim($word); |
58 | |
59 | // Skip empty lines |
60 | if ( empty($word) ) |
61 | continue; |
62 | |
63 | // Do some escaping magic so that '#' chars in the |
64 | // spam words don't break things: |
65 | $word = preg_quote($word, '#'); |
66 | |
67 | $pattern = "#$word#i"; |
68 | if ( preg_match($pattern, $author) ) return 0; |
69 | if ( preg_match($pattern, $email) ) return 0; |
70 | if ( preg_match($pattern, $url) ) return 0; |
71 | if ( preg_match($pattern, $comment) ) return 0; |
72 | if ( preg_match($pattern, $user_ip) ) return 0; |
73 | if ( preg_match($pattern, $user_agent) ) return 0; |
74 | } |
75 | } |
76 | |
77 | // Comment whitelisting: |
78 | if ( 1 == get_option('comment_whitelist')) { |
79 | if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll |
80 | $uri = parse_url($url); |
81 | $domain = $uri['host']; |
82 | $uri = parse_url( get_option('home') ); |
83 | $home_domain = $uri['host']; |
84 | if ( $wpdb->get_var($wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_url LIKE (%s) LIMIT 1", '%'.$domain.'%')) || $domain == $home_domain ) |
85 | return 1; |
86 | else |
87 | return 0; |
88 | } elseif ( $author != '' && $email != '' ) { |
89 | // expected_slashed ($author, $email) |
90 | $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1"); |
91 | if ( ( 1 == $ok_to_comment ) && |
92 | ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) ) |
93 | if ($linkcount_result == 0) return 1; else return 0; |
94 | else if ($linkcount_result > 1) return "spam"; else return 0; |
95 | } else return 0; |
96 | } |
97 | return 1; |
98 | } |
99 | |
100 | /** |
101 | * Retrieve the approved comments for post $post_id. |
102 | * |
103 | * @since 2.0.0 |
104 | * @uses $wpdb |
105 | * |
106 | * @param int $post_id The ID of the post |
107 | * @return array $comments The approved comments |
108 | */ |
109 | function get_approved_comments($post_id) { |
110 | global $wpdb; |
111 | return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id)); |
112 | } |
113 | |
114 | /** |
115 | * Retrieves comment data given a comment ID or comment object. |
116 | * |
117 | * If an object is passed then the comment data will be cached and then returned |
118 | * after being passed through a filter. If the comment is empty, then the global |
119 | * comment variable will be used, if it is set. |
120 | * |
121 | * If the comment is empty, then the global comment variable will be used, if it |
122 | * is set. |
123 | * |
124 | * @since 2.0.0 |
125 | * @uses $wpdb |
126 | * |
127 | * @param object|string|int $comment Comment to retrieve. |
128 | * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants. |
129 | * @return object|array|null Depends on $output value. |
130 | */ |
131 | function &get_comment(&$comment, $output = OBJECT) { |
132 | global $wpdb; |
133 | |
134 | if ( empty($comment) ) { |
135 | if ( isset($GLOBALS['comment']) ) |
136 | $_comment = & $GLOBALS['comment']; |
137 | else |
138 | $_comment = null; |
139 | } elseif ( is_object($comment) ) { |
140 | wp_cache_add($comment->comment_ID, $comment, 'comment'); |
141 | $_comment = $comment; |
142 | } else { |
143 | if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) { |
144 | $_comment = & $GLOBALS['comment']; |
145 | } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) { |
146 | $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment)); |
147 | wp_cache_add($_comment->comment_ID, $_comment, 'comment'); |
148 | } |
149 | } |
150 | |
151 | $_comment = apply_filters('get_comment', $_comment); |
152 | |
153 | if ( $output == OBJECT ) { |
154 | return $_comment; |
155 | } elseif ( $output == ARRAY_A ) { |
156 | $__comment = get_object_vars($_comment); |
157 | return $__comment; |
158 | } elseif ( $output == ARRAY_N ) { |
159 | $__comment = array_values(get_object_vars($_comment)); |
160 | return $__comment; |
161 | } else { |
162 | return $_comment; |
163 | } |
164 | } |
165 | |
166 | /** |
167 | * Retrieve a list of comments. |
168 | * |
169 | * The list of comment arguments are 'status', 'orderby', 'comment_date_gmt', |
170 | * 'order', 'number', 'offset', and 'post_id'. |
171 | * |
172 | * @since 2.7.0 |
173 | * @uses $wpdb |
174 | * |
175 | * @param mixed $args Optional. Array or string of options to override defaults. |
176 | * @return array List of comments. |
177 | */ |
178 | function get_comments( $args = '' ) { |
179 | global $wpdb; |
180 | |
181 | $defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0); |
182 | |
183 | $args = wp_parse_args( $args, $defaults ); |
184 | extract( $args, EXTR_SKIP ); |
185 | |
186 | // $args can be whatever, only use the args defined in defaults to compute the key |
187 | $key = md5( serialize( compact(array_keys($defaults)) ) ); |
188 | $last_changed = wp_cache_get('last_changed', 'comment'); |
189 | if ( !$last_changed ) { |
190 | $last_changed = time(); |
191 | wp_cache_set('last_changed', $last_changed, 'comment'); |
192 | } |
193 | $cache_key = "get_comments:$key:$last_changed"; |
194 | |
195 | if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { |
196 | return $cache; |
197 | } |
198 | |
199 | $post_id = absint($post_id); |
200 | |
201 | if ( 'hold' == $status ) |
202 | $approved = "comment_approved = '0'"; |
203 | elseif ( 'approve' == $status ) |
204 | $approved = "comment_approved = '1'"; |
205 | elseif ( 'spam' == $status ) |
206 | $approved = "comment_approved = 'spam'"; |
207 | else |
208 | $approved = "( comment_approved = '0' OR comment_approved = '1' )"; |
209 | |
210 | $order = ( 'ASC' == $order ) ? 'ASC' : 'DESC'; |
211 | |
212 | $orderby = 'comment_date_gmt'; // Hard code for now |
213 | |
214 | $number = absint($number); |
215 | $offset = absint($offset); |
216 | |
217 | if ( !empty($number) ) { |
218 | if ( $offset ) |
219 | $number = 'LIMIT ' . $offset . ',' . $number; |
220 | else |
221 | $number = 'LIMIT ' . $number; |
222 | |
223 | } else { |
224 | $number = ''; |
225 | } |
226 | |
227 | if ( ! empty($post_id) ) |
228 | $post_where = $wpdb->prepare( 'comment_post_ID = %d AND', $post_id ); |
229 | else |
230 | $post_where = ''; |
231 | |
232 | $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" ); |
233 | wp_cache_add( $cache_key, $comments, 'comment' ); |
234 | |
235 | return $comments; |
236 | } |
237 | |
238 | /** |
239 | * Retrieve all of the WordPress supported comment statuses. |
240 | * |
241 | * Comments have a limited set of valid status values, this provides the comment |
242 | * status values and descriptions. |
243 | * |
244 | * @package WordPress |
245 | * @subpackage Post |
246 | * @since 2.7.0 |
247 | * |
248 | * @return array List of comment statuses. |
249 | */ |
250 | function get_comment_statuses( ) { |
251 | $status = array( |
252 | 'hold' => __('Unapproved'), |
253 | 'approve' => __('Approved'), |
254 | 'spam' => _c('Spam|adjective'), |
255 | ); |
256 | |
257 | return $status; |
258 | } |
259 | |
260 | |
261 | /** |
262 | * The date the last comment was modified. |
263 | * |
264 | * @since 1.5.0 |
265 | * @uses $wpdb |
266 | * @global array $cache_lastcommentmodified |
267 | * |
268 | * @param string $timezone Which timezone to use in reference to 'gmt', 'blog', |
269 | * or 'server' locations. |
270 | * @return string Last comment modified date. |
271 | */ |
272 | function get_lastcommentmodified($timezone = 'server') { |
273 | global $cache_lastcommentmodified, $wpdb; |
274 | |
275 | if ( isset($cache_lastcommentmodified[$timezone]) ) |
276 | return $cache_lastcommentmodified[$timezone]; |
277 | |
278 | $add_seconds_server = date('Z'); |
279 | |
280 | switch ( strtolower($timezone)) { |
281 | case 'gmt': |
282 | $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1"); |
283 | break; |
284 | case 'blog': |
285 | $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1"); |
286 | break; |
287 | case 'server': |
288 | $lastcommentmodified = $wpdb->get_var($wpdb->prepare("SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server)); |
289 | break; |
290 | } |
291 | |
292 | $cache_lastcommentmodified[$timezone] = $lastcommentmodified; |
293 | |
294 | return $lastcommentmodified; |
295 | } |
296 | |
297 | /** |
298 | * The amount of comments in a post or total comments. |
299 | * |
300 | * A lot like {@link wp_count_comments()}, in that they both return comment |
301 | * stats (albeit with different types). The {@link wp_count_comments()} actual |
302 | * caches, but this function does not. |
303 | * |
304 | * @since 2.0.0 |
305 | * @uses $wpdb |
306 | * |
307 | * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide. |
308 | * @return array The amount of spam, approved, awaiting moderation, and total comments. |
309 | */ |
310 | function get_comment_count( $post_id = 0 ) { |
311 | global $wpdb; |
312 | |
313 | $post_id = (int) $post_id; |
314 | |
315 | $where = ''; |
316 | if ( $post_id > 0 ) { |
317 | $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id); |
318 | } |
319 | |
320 | $totals = (array) $wpdb->get_results(" |
321 | SELECT comment_approved, COUNT( * ) AS total |
322 | FROM {$wpdb->comments} |
323 | {$where} |
324 | GROUP BY comment_approved |
325 | ", ARRAY_A); |
326 | |
327 | $comment_count = array( |
328 | "approved" => 0, |
329 | "awaiting_moderation" => 0, |
330 | "spam" => 0, |
331 | "total_comments" => 0 |
332 | ); |
333 | |
334 | foreach ( $totals as $row ) { |
335 | switch ( $row['comment_approved'] ) { |
336 | case 'spam': |
337 | $comment_count['spam'] = $row['total']; |
338 | $comment_count["total_comments"] += $row['total']; |
339 | break; |
340 | case 1: |
341 | $comment_count['approved'] = $row['total']; |
342 | $comment_count['total_comments'] += $row['total']; |
343 | break; |
344 | case 0: |
345 | $comment_count['awaiting_moderation'] = $row['total']; |
346 | $comment_count['total_comments'] += $row['total']; |
347 | break; |
348 | default: |
349 | break; |
350 | } |
351 | } |
352 | |
353 | return $comment_count; |
354 | } |
355 | |
356 | /** |
357 | * Sanitizes the cookies sent to the user already. |
358 | * |
359 | * Will only do anything if the cookies have already been created for the user. |
360 | * Mostly used after cookies had been sent to use elsewhere. |
361 | * |
362 | * @since 2.0.4 |
363 | */ |
364 | function sanitize_comment_cookies() { |
365 | if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) { |
366 | $comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]); |
367 | $comment_author = stripslashes($comment_author); |
368 | $comment_author = attribute_escape($comment_author); |
369 | $_COOKIE['comment_author_'.COOKIEHASH] = $comment_author; |
370 | } |
371 | |
372 | if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) { |
373 | $comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]); |
374 | $comment_author_email = stripslashes($comment_author_email); |
375 | $comment_author_email = attribute_escape($comment_author_email); |
376 | $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email; |
377 | } |
378 | |
379 | if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) { |
380 | $comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]); |
381 | $comment_author_url = stripslashes($comment_author_url); |
382 | $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url; |
383 | } |
384 | } |
385 | |
386 | /** |
387 | * Validates whether this comment is allowed to be made or not. |
388 | * |
389 | * @since 2.0.0 |
390 | * @uses $wpdb |
391 | * @uses apply_filters() Calls 'pre_comment_approved' hook on the type of comment |
392 | * @uses do_action() Calls 'check_comment_flood' hook on $comment_author_IP, $comment_author_email, and $comment_date_gmt |
393 | * |
394 | * @param array $commentdata Contains information on the comment |
395 | * @return mixed Signifies the approval status (0|1|'spam') |
396 | */ |
397 | function wp_allow_comment($commentdata) { |
398 | global $wpdb; |
399 | extract($commentdata, EXTR_SKIP); |
400 | |
401 | // Simple duplicate check |
402 | // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) |
403 | $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' "; |
404 | if ( $comment_author_email ) |
405 | $dupe .= "OR comment_author_email = '$comment_author_email' "; |
406 | $dupe .= ") AND comment_content = '$comment_content' LIMIT 1"; |
407 | if ( $wpdb->get_var($dupe) ) { |
408 | if ( defined('DOING_AJAX') ) |
409 | die( __('Duplicate comment detected; it looks as though you\'ve already said that!') ); |
410 | |
411 | wp_die( __('Duplicate comment detected; it looks as though you\'ve already said that!') ); |
412 | } |
413 | |
414 | do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt ); |
415 | |
416 | if ( $user_id ) { |
417 | $userdata = get_userdata($user_id); |
418 | $user = new WP_User($user_id); |
419 | $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID)); |
420 | } |
421 | |
422 | if ( isset($userdata) && ( $user_id == $post_author || $user->has_cap('moderate_comments') ) ) { |
423 | // The author and the admins get respect. |
424 | $approved = 1; |
425 | } else { |
426 | // Everyone else's comments will be checked. |
427 | |
428 | // Rewritten nov 2013 to make linky spam go straight to spam. |
429 | |
430 | $approved = check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type); |
431 | |
432 | if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) ) $approved = 'spam'; |
433 | |
434 | } |
435 | |
436 | $approved = apply_filters('pre_comment_approved', $approved); |
437 | return $approved; |
438 | } |
439 | |
440 | /** |
441 | * Check whether comment flooding is occurring. |
442 | * |
443 | * Won't run, if current user can manage options, so to not block |
444 | * administrators. |
445 | * |
446 | * @since 2.3.0 |
447 | * @uses $wpdb |
448 | * @uses apply_filters() Calls 'comment_flood_filter' filter with first |
449 | * parameter false, last comment timestamp, new comment timestamp. |
450 | * @uses do_action() Calls 'comment_flood_trigger' action with parameters with |
451 | * last comment timestamp and new comment timestamp. |
452 | * |
453 | * @param string $ip Comment IP. |
454 | * @param string $email Comment author email address. |
455 | * @param string $date MySQL time string. |
456 | */ |
457 | function check_comment_flood_db( $ip, $email, $date ) { |
458 | global $wpdb; |
459 | if ( current_user_can( 'manage_options' ) ) |
460 | return; // don't throttle admins |
461 | if ( $lasttime = $wpdb->get_var( $wpdb->prepare("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = %s OR comment_author_email = %s ORDER BY comment_date DESC LIMIT 1", $ip, $email) ) ) { |
462 | $time_lastcomment = mysql2date('U', $lasttime); |
463 | $time_newcomment = mysql2date('U', $date); |
464 | $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment); |
465 | if ( $flood_die ) { |
466 | do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment); |
467 | |
468 | if ( defined('DOING_AJAX') ) |
469 | die( __('You are posting comments too quickly. Slow down.') ); |
470 | |
471 | wp_die( __('You are posting comments too quickly. Slow down.'), '', array('response' => 403) ); |
472 | } |
473 | } |
474 | } |
475 | |
476 | /** |
477 | * Separates an array of comments into an array keyed by comment_type. |
478 | * |
479 | * @since 2.7.0 |
480 | * |
481 | * @param array $comments Array of comments |
482 | * @return array Array of comments keyed by comment_type. |
483 | */ |
484 | function &separate_comments(&$comments) { |
485 | $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array()); |
486 | $count = count($comments); |
487 | for ( $i = 0; $i < $count; $i++ ) { |
488 | $type = $comments[$i]->comment_type; |
489 | if ( empty($type) ) |
490 | $type = 'comment'; |
491 | $comments_by_type[$type][] = &$comments[$i]; |
492 | if ( 'trackback' == $type || 'pingback' == $type ) |
493 | $comments_by_type['pings'][] = &$comments[$i]; |
494 | } |
495 | |
496 | return $comments_by_type; |
497 | } |
498 | |
499 | /** |
500 | * Calculate the total number of comment pages. |
501 | * |
502 | * @since 2.7.0 |
503 | * @uses get_query_var() Used to fill in the default for $per_page parameter. |
504 | * @uses get_option() Used to fill in defaults for parameters. |
505 | * @uses Walker_Comment |
506 | * |
507 | * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments |
508 | * @param int $per_page Optional comments per page. |
509 | * @param boolean $threaded Optional control over flat or threaded comments. |
510 | * @return int Number of comment pages. |
511 | */ |
512 | function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) { |
513 | global $wp_query; |
514 | |
515 | if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) ) |
516 | return $wp_query->max_num_comment_pages; |
517 | |
518 | if ( !$comments || !is_array($comments) ) |
519 | $comments = $wp_query->comments; |
520 | |
521 | if ( empty($comments) ) |
522 | return 0; |
523 | |
524 | if ( !isset($per_page) ) |
525 | $per_page = (int) get_query_var('comments_per_page'); |
526 | if ( 0 === $per_page ) |
527 | $per_page = (int) get_option('comments_per_page'); |
528 | if ( 0 === $per_page ) |
529 | return 1; |
530 | |
531 | if ( !isset($threaded) ) |
532 | $threaded = get_option('thread_comments'); |
533 | |
534 | if ( $threaded ) { |
535 | $walker = new Walker_Comment; |
536 | $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page ); |
537 | } else { |
538 | $count = ceil( count( $comments ) / $per_page ); |
539 | } |
540 | |
541 | return $count; |
542 | } |
543 | |
544 | /** |
545 | * Calculate what page number a comment will appear on for comment paging. |
546 | * |
547 | * @since 2.7.0 |
548 | * @uses get_comment() Gets the full comment of the $comment_ID parameter. |
549 | * @uses get_option() Get various settings to control function and defaults. |
550 | * @uses get_page_of_comment() Used to loop up to top level comment. |
551 | * |
552 | * @param int $comment_ID Comment ID. |
553 | * @param array $args Optional args. |
554 | * @return int|null Comment page number or null on error. |
555 | */ |
556 | function get_page_of_comment( $comment_ID, $args = array() ) { |
557 | global $wpdb; |
558 | |
559 | if ( !$comment = get_comment( $comment_ID ) ) |
560 | return; |
561 | |
562 | $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' ); |
563 | $args = wp_parse_args( $args, $defaults ); |
564 | |
565 | if ( '' === $args['per_page'] && get_option('page_comments') ) |
566 | $args['per_page'] = get_query_var('comments_per_page'); |
567 | if ( empty($args['per_page']) ) { |
568 | $args['per_page'] = 0; |
569 | $args['page'] = 0; |
570 | } |
571 | if ( $args['per_page'] < 1 ) |
572 | return 1; |
573 | |
574 | if ( '' === $args['max_depth'] ) { |
575 | if ( get_option('thread_comments') ) |
576 | $args['max_depth'] = get_option('thread_comments_depth'); |
577 | else |
578 | $args['max_depth'] = -1; |
579 | } |
580 | |
581 | // Find this comment's top level parent if threading is enabled |
582 | if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) |
583 | return get_page_of_comment( $comment->comment_parent, $args ); |
584 | |
585 | $allowedtypes = array( |
586 | 'comment' => '', |
587 | 'pingback' => 'pingback', |
588 | 'trackback' => 'trackback', |
589 | ); |
590 | |
591 | $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : ''; |
592 | |
593 | // Count comments older than this one |
594 | $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) ); |
595 | |
596 | // No older comments? Then it's page #1. |
597 | if ( 0 == $oldercoms ) |
598 | return 1; |
599 | |
600 | // Divide comments older than this one by comments per page to get this comment's page number |
601 | return ceil( ( $oldercoms + 1 ) / $args['per_page'] ); |
602 | } |
603 | |
604 | /** |
605 | * Does comment contain blacklisted characters or words. |
606 | * |
607 | * @since 1.5.0 |
608 | * @uses do_action() Calls 'wp_blacklist_check' hook for all parameters. |
609 | * |
610 | * @param string $author The author of the comment |
611 | * @param string $email The email of the comment |
612 | * @param string $url The url used in the comment |
613 | * @param string $comment The comment content |
614 | * @param string $user_ip The comment author IP address |
615 | * @param string $user_agent The author's browser user agent |
616 | * @return bool True if comment contains blacklisted content, false if comment does not |
617 | */ |
618 | function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) { |
619 | do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent); |
620 | |
621 | if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) { |
622 | foreach ( (array) $chars[1] as $char ) { |
623 | // If it's an encoded char in the normal ASCII set, reject |
624 | if ( 38 == $char ) |
625 | continue; // Unless it's & |
626 | if ( $char < 128 ) |
627 | return true; |
628 | } |
629 | } |
630 | |
631 | $mod_keys = trim( get_option('blacklist_keys') ); |
632 | if ( '' == $mod_keys ) |
633 | return false; // If moderation keys are empty |
634 | $words = explode("\n", $mod_keys ); |
635 | |
636 | foreach ( (array) $words as $word ) { |
637 | $word = trim($word); |
638 | |
639 | // Skip empty lines |
640 | if ( empty($word) ) { continue; } |
641 | |
642 | // Do some escaping magic so that '#' chars in the |
643 | // spam words don't break things: |
644 | $word = preg_quote($word, '#'); |
645 | |
646 | $pattern = "#$word#i"; |
647 | if ( |
648 | preg_match($pattern, $author) |
649 | || preg_match($pattern, $email) |
650 | || preg_match($pattern, $url) |
651 | || preg_match($pattern, $comment) |
652 | || preg_match($pattern, $user_ip) |
653 | || preg_match($pattern, $user_agent) |
654 | ) |
655 | return true; |
656 | } |
657 | return false; |
658 | } |
659 | |
660 | /** |
661 | * Retrieve total comments for blog or single post. |
662 | * |
663 | * The properties of the returned object contain the 'moderated', 'approved', |
664 | * and spam comments for either the entire blog or single post. Those properties |
665 | * contain the amount of comments that match the status. The 'total_comments' |
666 | * property contains the integer of total comments. |
667 | * |
668 | * The comment stats are cached and then retrieved, if they already exist in the |
669 | * cache. |
670 | * |
671 | * @since 2.5.0 |
672 | * |
673 | * @param int $post_id Optional. Post ID. |
674 | * @return object Comment stats. |
675 | */ |
676 | function wp_count_comments( $post_id = 0 ) { |
677 | global $wpdb; |
678 | |
679 | $post_id = (int) $post_id; |
680 | |
681 | $stats = apply_filters('wp_count_comments', array(), $post_id); |
682 | if ( !empty($stats) ) |
683 | return $stats; |
684 | |
685 | $count = wp_cache_get("comments-{$post_id}", 'counts'); |
686 | |
687 | if ( false !== $count ) |
688 | return $count; |
689 | |
690 | $where = ''; |
691 | if( $post_id > 0 ) |
692 | $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id ); |
693 | |
694 | $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A ); |
695 | |
696 | $total = 0; |
697 | $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam'); |
698 | $known_types = array_keys( $approved ); |
699 | foreach( (array) $count as $row_num => $row ) { |
700 | $total += $row['num_comments']; |
701 | if ( in_array( $row['comment_approved'], $known_types ) ) |
702 | $stats[$approved[$row['comment_approved']]] = $row['num_comments']; |
703 | } |
704 | |
705 | $stats['total_comments'] = $total; |
706 | foreach ( $approved as $key ) { |
707 | if ( empty($stats[$key]) ) |
708 | $stats[$key] = 0; |
709 | } |
710 | |
711 | $stats = (object) $stats; |
712 | wp_cache_set("comments-{$post_id}", $stats, 'counts'); |
713 | |
714 | return $stats; |
715 | } |
716 | |
717 | /** |
718 | * Removes comment ID and maybe updates post comment count. |
719 | * |
720 | * The post comment count will be updated if the comment was approved and has a |
721 | * post ID available. |
722 | * |
723 | * @since 2.0.0 |
724 | * @uses $wpdb |
725 | * @uses do_action() Calls 'delete_comment' hook on comment ID |
726 | * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter |
727 | * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object |
728 | * |
729 | * @param int $comment_id Comment ID |
730 | * @return bool False if delete comment query failure, true on success. |
731 | */ |
732 | function wp_delete_comment($comment_id) { |
733 | global $wpdb; |
734 | do_action('delete_comment', $comment_id); |
735 | |
736 | $comment = get_comment($comment_id); |
737 | |
738 | if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) ) |
739 | return false; |
740 | |
741 | $post_id = $comment->comment_post_ID; |
742 | if ( $post_id && $comment->comment_approved == 1 ) |
743 | wp_update_comment_count($post_id); |
744 | |
745 | clean_comment_cache($comment_id); |
746 | |
747 | do_action('wp_set_comment_status', $comment_id, 'delete'); |
748 | wp_transition_comment_status('delete', $comment->comment_approved, $comment); |
749 | return true; |
750 | } |
751 | |
752 | /** |
753 | * The status of a comment by ID. |
754 | * |
755 | * @since 1.0.0 |
756 | * |
757 | * @param int $comment_id Comment ID |
758 | * @return string|bool Status might be 'deleted', 'approved', 'unapproved', 'spam'. False on failure. |
759 | */ |
760 | function wp_get_comment_status($comment_id) { |
761 | $comment = get_comment($comment_id); |
762 | if ( !$comment ) |
763 | return false; |
764 | |
765 | $approved = $comment->comment_approved; |
766 | |
767 | if ( $approved == NULL ) |
768 | return 'deleted'; |
769 | elseif ( $approved == '1' ) |
770 | return 'approved'; |
771 | elseif ( $approved == '0' ) |
772 | return 'unapproved'; |
773 | elseif ( $approved == 'spam' ) |
774 | return 'spam'; |
775 | else |
776 | return false; |
777 | } |
778 | |
779 | /** |
780 | * Call hooks for when a comment status transition occurs. |
781 | * |
782 | * Calls hooks for comment status transitions. If the new comment status is not the same |
783 | * as the previous comment status, then two hooks will be ran, the first is |
784 | * 'transition_comment_status' with new status, old status, and comment data. The |
785 | * next action called is 'comment_OLDSTATUS_to_NEWSTATUS' the NEWSTATUS is the |
786 | * $new_status parameter and the OLDSTATUS is $old_status parameter; it has the |
787 | * comment data. |
788 | * |
789 | * The final action will run whether or not the comment statuses are the same. The |
790 | * action is named 'comment_NEWSTATUS_COMMENTTYPE', NEWSTATUS is from the $new_status |
791 | * parameter and COMMENTTYPE is comment_type comment data. |
792 | * |
793 | * @since 2.7.0 |
794 | * |
795 | * @param string $new_status New comment status. |
796 | * @param string $old_status Previous comment status. |
797 | * @param object $comment Comment data. |
798 | */ |
799 | function wp_transition_comment_status($new_status, $old_status, $comment) { |
800 | // Translate raw statuses to human readable formats for the hooks |
801 | // This is not a complete list of comment status, it's only the ones that need to be renamed |
802 | $comment_statuses = array( |
803 | 0 => 'unapproved', |
804 | 'hold' => 'unapproved', // wp_set_comment_status() uses "hold" |
805 | 1 => 'approved', |
806 | 'approve' => 'approved', // wp_set_comment_status() uses "approve" |
807 | ); |
808 | if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status]; |
809 | if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status]; |
810 | |
811 | // Call the hooks |
812 | if ( $new_status != $old_status ) { |
813 | do_action('transition_comment_status', $new_status, $old_status, $comment); |
814 | do_action("comment_${old_status}_to_$new_status", $comment); |
815 | } |
816 | do_action("comment_${new_status}_$comment->comment_type", $comment->comment_ID, $comment); |
817 | } |
818 | |
819 | /** |
820 | * Get current commenter's name, email, and URL. |
821 | * |
822 | * Expects cookies content to already be sanitized. User of this function might |
823 | * wish to recheck the returned array for validity. |
824 | * |
825 | * @see sanitize_comment_cookies() Use to sanitize cookies |
826 | * |
827 | * @since 2.0.4 |
828 | * |
829 | * @return array Comment author, email, url respectively. |
830 | */ |
831 | function wp_get_current_commenter() { |
832 | // Cookies should already be sanitized. |
833 | |
834 | $comment_author = ''; |
835 | if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) |
836 | $comment_author = $_COOKIE['comment_author_'.COOKIEHASH]; |
837 | |
838 | $comment_author_email = ''; |
839 | if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) |
840 | $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH]; |
841 | |
842 | $comment_author_url = ''; |
843 | if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) |
844 | $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH]; |
845 | |
846 | return compact('comment_author', 'comment_author_email', 'comment_author_url'); |
847 | } |
848 | |
849 | /** |
850 | * Inserts a comment to the database. |
851 | * |
852 | * The available comment data key names are 'comment_author_IP', 'comment_date', |
853 | * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'. |
854 | * |
855 | * @since 2.0.0 |
856 | * @uses $wpdb |
857 | * |
858 | * @param array $commentdata Contains information on the comment. |
859 | * @return int The new comment's ID. |
860 | */ |
861 | function wp_insert_comment($commentdata) { |
862 | global $wpdb; |
863 | extract(stripslashes_deep($commentdata), EXTR_SKIP); |
864 | |
865 | if ( ! isset($comment_author_IP) ) |
866 | $comment_author_IP = ''; |
867 | if ( ! isset($comment_date) ) |
868 | $comment_date = current_time('mysql'); |
869 | if ( ! isset($comment_date_gmt) ) |
870 | $comment_date_gmt = get_gmt_from_date($comment_date); |
871 | if ( ! isset($comment_parent) ) |
872 | $comment_parent = 0; |
873 | if ( ! isset($comment_approved) ) |
874 | $comment_approved = 1; |
875 | if ( ! isset($user_id) ) |
876 | $user_id = 0; |
877 | if ( ! isset($comment_type) ) |
878 | $comment_type = ''; |
879 | |
880 | $result = $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->comments |
881 | (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id) |
882 | VALUES (%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d)", |
883 | $comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, $comment_date, $comment_date_gmt, $comment_content, $comment_approved, $comment_agent, $comment_type, $comment_parent, $user_id) ); |
884 | |
885 | $id = (int) $wpdb->insert_id; |
886 | |
887 | if ( $comment_approved == 1) |
888 | wp_update_comment_count($comment_post_ID); |
889 | |
890 | return $id; |
891 | } |
892 | |
893 | /** |
894 | * Filters and sanitizes comment data. |
895 | * |
896 | * Sets the comment data 'filtered' field to true when finished. This can be |
897 | * checked as to whether the comment should be filtered and to keep from |
898 | * filtering the same comment more than once. |
899 | * |
900 | * @since 2.0.0 |
901 | * @uses apply_filters() Calls 'pre_user_id' hook on comment author's user ID |
902 | * @uses apply_filters() Calls 'pre_comment_user_agent' hook on comment author's user agent |
903 | * @uses apply_filters() Calls 'pre_comment_author_name' hook on comment author's name |
904 | * @uses apply_filters() Calls 'pre_comment_content' hook on the comment's content |
905 | * @uses apply_filters() Calls 'pre_comment_user_ip' hook on comment author's IP |
906 | * @uses apply_filters() Calls 'pre_comment_author_url' hook on comment author's URL |
907 | * @uses apply_filters() Calls 'pre_comment_author_email' hook on comment author's email address |
908 | * |
909 | * @param array $commentdata Contains information on the comment. |
910 | * @return array Parsed comment information. |
911 | */ |
912 | function wp_filter_comment($commentdata) { |
913 | $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']); |
914 | $commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']); |
915 | $commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']); |
916 | $commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']); |
917 | $commentdata['comment_author_IP'] = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']); |
918 | $commentdata['comment_author_url'] = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']); |
919 | $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']); |
920 | $commentdata['filtered'] = true; |
921 | return $commentdata; |
922 | } |
923 | |
924 | /** |
925 | * Whether comment should be blocked because of comment flood. |
926 | * |
927 | * @since 2.1.0 |
928 | * |
929 | * @param bool $block Whether plugin has already blocked comment. |
930 | * @param int $time_lastcomment Timestamp for last comment. |
931 | * @param int $time_newcomment Timestamp for new comment. |
932 | * @return bool Whether comment should be blocked. |
933 | */ |
934 | function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) { |
935 | if ( $block ) // a plugin has already blocked... we'll let that decision stand |
936 | return $block; |
937 | if ( ($time_newcomment - $time_lastcomment) < 15 ) |
938 | return true; |
939 | return false; |
940 | } |
941 | |
942 | /** |
943 | * Adds a new comment to the database. |
944 | * |
945 | * Filters new comment to ensure that the fields are sanitized and valid before |
946 | * inserting comment into database. Calls 'comment_post' action with comment ID |
947 | * and whether comment is approved by WordPress. Also has 'preprocess_comment' |
948 | * filter for processing the comment data before the function handles it. |
949 | * |
950 | * @since 1.5.0 |
951 | * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing |
952 | * @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved. |
953 | * @uses wp_filter_comment() Used to filter comment before adding comment. |
954 | * @uses wp_allow_comment() checks to see if comment is approved. |
955 | * @uses wp_insert_comment() Does the actual comment insertion to the database. |
956 | * |
957 | * @param array $commentdata Contains information on the comment. |
958 | * @return int The ID of the comment after adding. |
959 | */ |
960 | function wp_new_comment( $commentdata ) { |
961 | $commentdata = apply_filters('preprocess_comment', $commentdata); |
962 | |
963 | $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; |
964 | $commentdata['user_ID'] = (int) $commentdata['user_ID']; |
965 | |
966 | $commentdata['comment_parent'] = absint($commentdata['comment_parent']); |
967 | $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : ''; |
968 | $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0; |
969 | |
970 | $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] ); |
971 | $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT']; |
972 | |
973 | $commentdata['comment_date'] = current_time('mysql'); |
974 | $commentdata['comment_date_gmt'] = current_time('mysql', 1); |
975 | |
976 | $commentdata = wp_filter_comment($commentdata); |
977 | |
978 | $commentdata['comment_approved'] = wp_allow_comment($commentdata); |
979 | |
980 | $comment_ID = wp_insert_comment($commentdata); |
981 | |
982 | do_action('comment_post', $comment_ID, $commentdata['comment_approved']); |
983 | |
984 | if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching |
985 | if ( '0' == $commentdata['comment_approved'] ) |
986 | wp_notify_moderator($comment_ID); |
987 | |
988 | $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment |
989 | |
990 | if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] ) |
991 | wp_notify_postauthor($comment_ID, $commentdata['comment_type']); |
992 | } |
993 | |
994 | return $comment_ID; |
995 | } |
996 | |
997 | /** |
998 | * Sets the status of a comment. |
999 | * |
1000 | * The 'wp_set_comment_status' action is called after the comment is handled and |
1001 | * will only be called, if the comment status is either 'hold', 'approve', or |
1002 | * 'spam'. If the comment status is not in the list, then false is returned and |
1003 | * if the status is 'delete', then the comment is deleted without calling the |
1004 | * action. |
1005 | * |
1006 | * @since 1.0.0 |
1007 | * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object |
1008 | * |
1009 | * @param int $comment_id Comment ID. |
1010 | * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'. |
1011 | * @return bool False on failure or deletion and true on success. |
1012 | */ |
1013 | function wp_set_comment_status($comment_id, $comment_status) { |
1014 | global $wpdb; |
1015 | |
1016 | switch ( $comment_status ) { |
1017 | case 'hold': |
1018 | $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID = %d LIMIT 1", $comment_id); |
1019 | break; |
1020 | case 'approve': |
1021 | $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID = %d LIMIT 1", $comment_id); |
1022 | if ( get_option('comments_notify') ) { |
1023 | $comment = get_comment($comment_id); |
1024 | wp_notify_postauthor($comment_id, $comment->comment_type); |
1025 | } |
1026 | break; |
1027 | case 'spam': |
1028 | $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID = %d LIMIT 1", $comment_id); |
1029 | break; |
1030 | case 'delete': |
1031 | return wp_delete_comment($comment_id); |
1032 | break; |
1033 | default: |
1034 | return false; |
1035 | } |
1036 | |
1037 | if ( !$wpdb->query($query) ) |
1038 | return false; |
1039 | |
1040 | clean_comment_cache($comment_id); |
1041 | |
1042 | $comment = get_comment($comment_id); |
1043 | |
1044 | do_action('wp_set_comment_status', $comment_id, $comment_status); |
1045 | wp_transition_comment_status($comment_status, $comment->comment_approved, $comment); |
1046 | |
1047 | wp_update_comment_count($comment->comment_post_ID); |
1048 | |
1049 | return true; |
1050 | } |
1051 | |
1052 | /** |
1053 | * Updates an existing comment in the database. |
1054 | * |
1055 | * Filters the comment and makes sure certain fields are valid before updating. |
1056 | * |
1057 | * @since 2.0.0 |
1058 | * @uses $wpdb |
1059 | * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object |
1060 | * |
1061 | * @param array $commentarr Contains information on the comment. |
1062 | * @return int Comment was updated if value is 1, or was not updated if value is 0. |
1063 | */ |
1064 | function wp_update_comment($commentarr) { |
1065 | global $wpdb; |
1066 | |
1067 | // First, get all of the original fields |
1068 | $comment = get_comment($commentarr['comment_ID'], ARRAY_A); |
1069 | |
1070 | // Escape data pulled from DB. |
1071 | foreach ( (array) $comment as $key => $value ) |
1072 | $comment[$key] = $wpdb->escape($value); |
1073 | |
1074 | // Merge old and new fields with new fields overwriting old ones. |
1075 | $commentarr = array_merge($comment, $commentarr); |
1076 | |
1077 | $commentarr = wp_filter_comment( $commentarr ); |
1078 | |
1079 | // Now extract the merged array. |
1080 | extract(stripslashes_deep($commentarr), EXTR_SKIP); |
1081 | |
1082 | $comment_content = apply_filters('comment_save_pre', $comment_content); |
1083 | |
1084 | $comment_date_gmt = get_gmt_from_date($comment_date); |
1085 | |
1086 | if ( !isset($comment_approved) ) |
1087 | $comment_approved = 1; |
1088 | else if ( 'hold' == $comment_approved ) |
1089 | $comment_approved = 0; |
1090 | else if ( 'approve' == $comment_approved ) |
1091 | $comment_approved = 1; |
1092 | |
1093 | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->comments SET |
1094 | comment_content = %s, |
1095 | comment_author = %s, |
1096 | comment_author_email = %s, |
1097 | comment_approved = %s, |
1098 | comment_author_url = %s, |
1099 | comment_date = %s, |
1100 | comment_date_gmt = %s |
1101 | WHERE comment_ID = %d", |
1102 | $comment_content, |
1103 | $comment_author, |
1104 | $comment_author_email, |
1105 | $comment_approved, |
1106 | $comment_author_url, |
1107 | $comment_date, |
1108 | $comment_date_gmt, |
1109 | $comment_ID) ); |
1110 | |
1111 | $rval = $wpdb->rows_affected; |
1112 | |
1113 | clean_comment_cache($comment_ID); |
1114 | wp_update_comment_count($comment_post_ID); |
1115 | do_action('edit_comment', $comment_ID); |
1116 | $comment = get_comment($comment_ID); |
1117 | wp_transition_comment_status($comment_approved, $comment->comment_approved, $comment); |
1118 | return $rval; |
1119 | } |
1120 | |
1121 | /** |
1122 | * Whether to defer comment counting. |
1123 | * |
1124 | * When setting $defer to true, all post comment counts will not be updated |
1125 | * until $defer is set to false. When $defer is set to false, then all |
1126 | * previously deferred updated post comment counts will then be automatically |
1127 | * updated without having to call wp_update_comment_count() after. |
1128 | * |
1129 | * @since 2.5.0 |
1130 | * @staticvar bool $_defer |
1131 | * |
1132 | * @param bool $defer |
1133 | * @return unknown |
1134 | */ |
1135 | function wp_defer_comment_counting($defer=null) { |
1136 | static $_defer = false; |
1137 | |
1138 | if ( is_bool($defer) ) { |
1139 | $_defer = $defer; |
1140 | // flush any deferred counts |
1141 | if ( !$defer ) |
1142 | wp_update_comment_count( null, true ); |
1143 | } |
1144 | |
1145 | return $_defer; |
1146 | } |
1147 | |
1148 | /** |
1149 | * Updates the comment count for post(s). |
1150 | * |
1151 | * When $do_deferred is false (is by default) and the comments have been set to |
1152 | * be deferred, the post_id will be added to a queue, which will be updated at a |
1153 | * later date and only updated once per post ID. |
1154 | * |
1155 | * If the comments have not be set up to be deferred, then the post will be |
1156 | * updated. When $do_deferred is set to true, then all previous deferred post |
1157 | * IDs will be updated along with the current $post_id. |
1158 | * |
1159 | * @since 2.1.0 |
1160 | * @see wp_update_comment_count_now() For what could cause a false return value |
1161 | * |
1162 | * @param int $post_id Post ID |
1163 | * @param bool $do_deferred Whether to process previously deferred post comment counts |
1164 | * @return bool True on success, false on failure |
1165 | */ |
1166 | function wp_update_comment_count($post_id, $do_deferred=false) { |
1167 | static $_deferred = array(); |
1168 | |
1169 | if ( $do_deferred ) { |
1170 | $_deferred = array_unique($_deferred); |
1171 | foreach ( $_deferred as $i => $_post_id ) { |
1172 | wp_update_comment_count_now($_post_id); |
1173 | unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */ |
1174 | } |
1175 | } |
1176 | |
1177 | if ( wp_defer_comment_counting() ) { |
1178 | $_deferred[] = $post_id; |
1179 | return true; |
1180 | } |
1181 | elseif ( $post_id ) { |
1182 | return wp_update_comment_count_now($post_id); |
1183 | } |
1184 | |
1185 | } |
1186 | |
1187 | /** |
1188 | * Updates the comment count for the post. |
1189 | * |
1190 | * @since 2.5.0 |
1191 | * @uses $wpdb |
1192 | * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old |
1193 | * @uses do_action() Calls 'edit_posts' hook on $post_id and $post |
1194 | * |
1195 | * @param int $post_id Post ID |
1196 | * @return bool False on '0' $post_id or if post with ID does not exist. True on success. |
1197 | */ |
1198 | function wp_update_comment_count_now($post_id) { |
1199 | global $wpdb; |
1200 | $post_id = (int) $post_id; |
1201 | if ( !$post_id ) |
1202 | return false; |
1203 | if ( !$post = get_post($post_id) ) |
1204 | return false; |
1205 | |
1206 | $old = (int) $post->comment_count; |
1207 | $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) ); |
1208 | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_count = %d WHERE ID = %d", $new, $post_id) ); |
1209 | |
1210 | if ( 'page' == $post->post_type ) |
1211 | clean_page_cache( $post_id ); |
1212 | else |
1213 | clean_post_cache( $post_id ); |
1214 | |
1215 | do_action('wp_update_comment_count', $post_id, $new, $old); |
1216 | do_action('edit_post', $post_id, $post); |
1217 | |
1218 | return true; |
1219 | } |
1220 | |
1221 | // |
1222 | // Ping and trackback functions. |
1223 | // |
1224 | |
1225 | /** |
1226 | * Finds a pingback server URI based on the given URL. |
1227 | * |
1228 | * Checks the HTML for the rel="pingback" link and x-pingback headers. It does |
1229 | * a check for the x-pingback headers first and returns that, if available. The |
1230 | * check for the rel="pingback" has more overhead than just the header. |
1231 | * |
1232 | * @since 1.5.0 |
1233 | * |
1234 | * @param string $url URL to ping. |
1235 | * @param int $deprecated Not Used. |
1236 | * @return bool|string False on failure, string containing URI on success. |
1237 | */ |
1238 | function discover_pingback_server_uri($url, $deprecated = 2048) { |
1239 | |
1240 | $pingback_str_dquote = 'rel="pingback"'; |
1241 | $pingback_str_squote = 'rel=\'pingback\''; |
1242 | |
1243 | /** @todo Should use Filter Extension or custom preg_match instead. */ |
1244 | $parsed_url = parse_url($url); |
1245 | |
1246 | if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen. |
1247 | return false; |
1248 | |
1249 | $response = wp_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.1' ) ); |
1250 | |
1251 | if ( is_wp_error( $response ) ) |
1252 | return false; |
1253 | |
1254 | if ( isset( $response['headers']['x-pingback'] ) ) |
1255 | return $response['headers']['x-pingback']; |
1256 | |
1257 | // Not an (x)html, sgml, or xml page, no use going further. |
1258 | if ( isset( $response['headers']['content-type'] ) && preg_match('#(image|audio|video|model)/#is', $response['headers']['content-type']) ) |
1259 | return false; |
1260 | |
1261 | $contents = $response['body']; |
1262 | |
1263 | $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote); |
1264 | $pingback_link_offset_squote = strpos($contents, $pingback_str_squote); |
1265 | if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) { |
1266 | $quote = ($pingback_link_offset_dquote) ? '"' : '\''; |
1267 | $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote; |
1268 | $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset); |
1269 | $pingback_href_start = $pingback_href_pos+6; |
1270 | $pingback_href_end = @strpos($contents, $quote, $pingback_href_start); |
1271 | $pingback_server_url_len = $pingback_href_end - $pingback_href_start; |
1272 | $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len); |
1273 | |
1274 | // We may find rel="pingback" but an incomplete pingback URL |
1275 | if ( $pingback_server_url_len > 0 ) { // We got it! |
1276 | return $pingback_server_url; |
1277 | } |
1278 | } |
1279 | |
1280 | return false; |
1281 | } |
1282 | |
1283 | /** |
1284 | * Perform all pingbacks, enclosures, trackbacks, and send to pingback services. |
1285 | * |
1286 | * @since 2.1.0 |
1287 | * @uses $wpdb |
1288 | */ |
1289 | function do_all_pings() { |
1290 | global $wpdb; |
1291 | |
1292 | // Do pingbacks |
1293 | while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) { |
1294 | $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';"); |
1295 | pingback($ping->post_content, $ping->ID); |
1296 | } |
1297 | |
1298 | // Do Enclosures |
1299 | while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) { |
1300 | $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme';", $enclosure->ID) ); |
1301 | do_enclose($enclosure->post_content, $enclosure->ID); |
1302 | } |
1303 | |
1304 | // Do Trackbacks |
1305 | $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'"); |
1306 | if ( is_array($trackbacks) ) |
1307 | foreach ( $trackbacks as $trackback ) |
1308 | do_trackbacks($trackback); |
1309 | |
1310 | //Do Update Services/Generic Pings |
1311 | generic_ping(); |
1312 | } |
1313 | |
1314 | /** |
1315 | * Perform trackbacks. |
1316 | * |
1317 | * @since 1.5.0 |
1318 | * @uses $wpdb |
1319 | * |
1320 | * @param int $post_id Post ID to do trackbacks on. |
1321 | */ |
1322 | function do_trackbacks($post_id) { |
1323 | global $wpdb; |
1324 | |
1325 | $post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ); |
1326 | $to_ping = get_to_ping($post_id); |
1327 | $pinged = get_pung($post_id); |
1328 | if ( empty($to_ping) ) { |
1329 | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = %d", $post_id) ); |
1330 | return; |
1331 | } |
1332 | |
1333 | if ( empty($post->post_excerpt) ) |
1334 | $excerpt = apply_filters('the_content', $post->post_content); |
1335 | else |
1336 | $excerpt = apply_filters('the_excerpt', $post->post_excerpt); |
1337 | $excerpt = str_replace(']]>', ']]>', $excerpt); |
1338 | $excerpt = wp_html_excerpt($excerpt, 252) . '...'; |
1339 | |
1340 | $post_title = apply_filters('the_title', $post->post_title); |
1341 | $post_title = strip_tags($post_title); |
1342 | |
1343 | if ( $to_ping ) { |
1344 | foreach ( (array) $to_ping as $tb_ping ) { |
1345 | $tb_ping = trim($tb_ping); |
1346 | if ( !in_array($tb_ping, $pinged) ) { |
1347 | trackback($tb_ping, $post_title, $excerpt, $post_id); |
1348 | $pinged[] = $tb_ping; |
1349 | } else { |
1350 | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = %d", $post_id) ); |
1351 | } |
1352 | } |
1353 | } |
1354 | } |
1355 | |
1356 | /** |
1357 | * Sends pings to all of the ping site services. |
1358 | * |
1359 | * @since 1.2.0 |
1360 | * |
1361 | * @param int $post_id Post ID. Not actually used. |
1362 | * @return int Same as Post ID from parameter |
1363 | */ |
1364 | function generic_ping($post_id = 0) { |
1365 | $services = get_option('ping_sites'); |
1366 | |
1367 | $services = explode("\n", $services); |
1368 | foreach ( (array) $services as $service ) { |
1369 | $service = trim($service); |
1370 | if ( '' != $service ) |
1371 | weblog_ping($service); |
1372 | } |
1373 | |
1374 | return $post_id; |
1375 | } |
1376 | |
1377 | /** |
1378 | * Pings back the links found in a post. |
1379 | * |
1380 | * @since 0.71 |
1381 | * @uses $wp_version |
1382 | * @uses IXR_Client |
1383 | * |
1384 | * @param string $content Post content to check for links. |
1385 | * @param int $post_ID Post ID. |
1386 | */ |
1387 | function pingback($content, $post_ID) { |
1388 | global $wp_version; |
1389 | include_once(ABSPATH . WPINC . '/class-IXR.php'); |
1390 | |
1391 | // original code by Mort (http://mort.mine.nu:8080) |
1392 | $post_links = array(); |
1393 | |
1394 | $pung = get_pung($post_ID); |
1395 | |
1396 | // Variables |
1397 | $ltrs = '\w'; |
1398 | $gunk = '/#~:.?+=&%@!\-'; |
1399 | $punc = '.:?\-'; |
1400 | $any = $ltrs . $gunk . $punc; |
1401 | |
1402 | // Step 1 |
1403 | // Parsing the post, external links (if any) are stored in the $post_links array |
1404 | // This regexp comes straight from phpfreaks.com |
1405 | // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php |
1406 | preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp); |
1407 | |
1408 | // Step 2. |
1409 | // Walking thru the links array |
1410 | // first we get rid of links pointing to sites, not to specific files |
1411 | // Example: |
1412 | // http://dummy-weblog.org |
1413 | // http://dummy-weblog.org/ |
1414 | // http://dummy-weblog.org/post.php |
1415 | // We don't wanna ping first and second types, even if they have a valid <link/> |
1416 | |
1417 | foreach ( (array) $post_links_temp[0] as $link_test ) : |
1418 | if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself |
1419 | && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments. |
1420 | if ( $test = @parse_url($link_test) ) { |
1421 | if ( isset($test['query']) ) |
1422 | $post_links[] = $link_test; |
1423 | elseif ( ($test['path'] != '/') && ($test['path'] != '') ) |
1424 | $post_links[] = $link_test; |
1425 | } |
1426 | endif; |
1427 | endforeach; |
1428 | |
1429 | do_action_ref_array('pre_ping', array(&$post_links, &$pung)); |
1430 | |
1431 | foreach ( (array) $post_links as $pagelinkedto ) { |
1432 | $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048); |
1433 | |
1434 | if ( $pingback_server_url ) { |
1435 | @ set_time_limit( 60 ); |
1436 | // Now, the RPC call |
1437 | $pagelinkedfrom = get_permalink($post_ID); |
1438 | |
1439 | // using a timeout of 3 seconds should be enough to cover slow servers |
1440 | $client = new IXR_Client($pingback_server_url); |
1441 | $client->timeout = 3; |
1442 | $client->useragent .= ' -- WordPress/' . $wp_version; |
1443 | |
1444 | // when set to true, this outputs debug messages by itself |
1445 | $client->debug = false; |
1446 | |
1447 | if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered |
1448 | add_ping( $post_ID, $pagelinkedto ); |
1449 | } |
1450 | } |
1451 | } |
1452 | |
1453 | /** |
1454 | * Check whether blog is public before returning sites. |
1455 | * |
1456 | * @since 2.1.0 |
1457 | * |
1458 | * @param mixed $sites Will return if blog is public, will not return if not public. |
1459 | * @return mixed Empty string if blog is not public, returns $sites, if site is public. |
1460 | */ |
1461 | function privacy_ping_filter($sites) { |
1462 | if ( '0' != get_option('blog_public') ) |
1463 | return $sites; |
1464 | else |
1465 | return ''; |
1466 | } |
1467 | |
1468 | /** |
1469 | * Send a Trackback. |
1470 | * |
1471 | * Updates database when sending trackback to prevent duplicates. |
1472 | * |
1473 | * @since 0.71 |
1474 | * @uses $wpdb |
1475 | * |
1476 | * @param string $trackback_url URL to send trackbacks. |
1477 | * @param string $title Title of post. |
1478 | * @param string $excerpt Excerpt of post. |
1479 | * @param int $ID Post ID. |
1480 | * @return mixed Database query from update. |
1481 | */ |
1482 | function trackback($trackback_url, $title, $excerpt, $ID) { |
1483 | global $wpdb; |
1484 | |
1485 | if ( empty($trackback_url) ) |
1486 | return; |
1487 | |
1488 | $options = array(); |
1489 | $options['timeout'] = 4; |
1490 | $options['body'] = array( |
1491 | 'title' => $title, |
1492 | 'url' => get_permalink($ID), |
1493 | 'blog_name' => get_option('blogname'), |
1494 | 'excerpt' => $excerpt |
1495 | ); |
1496 | |
1497 | $response = wp_remote_post($trackback_url, $options); |
1498 | |
1499 | if ( is_wp_error( $response ) ) |
1500 | return; |
1501 | |
1502 | $tb_url = addslashes( $trackback_url ); |
1503 | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = %d", $ID) ); |
1504 | return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = %d", $ID) ); |
1505 | } |
1506 | |
1507 | /** |
1508 | * Send a pingback. |
1509 | * |
1510 | * @since 1.2.0 |
1511 | * @uses $wp_version |
1512 | * @uses IXR_Client |
1513 | * |
1514 | * @param string $server Host of blog to connect to. |
1515 | * @param string $path Path to send the ping. |
1516 | */ |
1517 | function weblog_ping($server = '', $path = '') { |
1518 | global $wp_version; |
1519 | include_once(ABSPATH . WPINC . '/class-IXR.php'); |
1520 | |
1521 | // using a timeout of 3 seconds should be enough to cover slow servers |
1522 | $client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path)); |
1523 | $client->timeout = 3; |
1524 | $client->useragent .= ' -- WordPress/'.$wp_version; |
1525 | |
1526 | // when set to true, this outputs debug messages by itself |
1527 | $client->debug = false; |
1528 | $home = trailingslashit( get_option('home') ); |
1529 | if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping |
1530 | $client->query('weblogUpdates.ping', get_option('blogname'), $home); |
1531 | } |
1532 | |
1533 | // |
1534 | // Cache |
1535 | // |
1536 | |
1537 | /** |
1538 | * Removes comment ID from the comment cache. |
1539 | * |
1540 | * @since 2.3.0 |
1541 | * @package WordPress |
1542 | * @subpackage Cache |
1543 | * |
1544 | * @param int $id Comment ID to remove from cache |
1545 | */ |
1546 | function clean_comment_cache($id) { |
1547 | wp_cache_delete($id, 'comment'); |
1548 | } |
1549 | |
1550 | /** |
1551 | * Updates the comment cache of given comments. |
1552 | * |
1553 | * Will add the comments in $comments to the cache. If comment ID already exists |
1554 | * in the comment cache then it will not be updated. The comment is added to the |
1555 | * cache using the comment group with the key using the ID of the comments. |
1556 | * |
1557 | * @since 2.3.0 |
1558 | * @package WordPress |
1559 | * @subpackage Cache |
1560 | * |
1561 | * @param array $comments Array of comment row objects |
1562 | */ |
1563 | function update_comment_cache($comments) { |
1564 | foreach ( (array) $comments as $comment ) |
1565 | wp_cache_add($comment->comment_ID, $comment, 'comment'); |
1566 | } |
1567 | |
1568 | // |
1569 | // Internal |
1570 | // |
1571 | |
1572 | /** |
1573 | * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts. |
1574 | * |
1575 | * @access private |
1576 | * @since 2.7.0 |
1577 | * |
1578 | * @param object $posts Post data object. |
1579 | * @return object |
1580 | */ |
1581 | function _close_comments_for_old_posts( $posts ) { |
1582 | if ( empty($posts) || !is_single() || !get_option('close_comments_for_old_posts') ) |
1583 | return $posts; |
1584 | |
1585 | $days_old = (int) get_option('close_comments_days_old'); |
1586 | if ( !$days_old ) |
1587 | return $posts; |
1588 | |
1589 | if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) ) { |
1590 | $posts[0]->comment_status = 'closed'; |
1591 | $posts[0]->ping_status = 'closed'; |
1592 | } |
1593 | |
1594 | return $posts; |
1595 | } |
1596 | |
1597 | /** |
1598 | * Close comments on an old post. Hooked to comments_open and pings_open. |
1599 | * |
1600 | * @access private |
1601 | * @since 2.7.0 |
1602 | * |
1603 | * @param bool $open Comments open or closed |
1604 | * @param int $post_id Post ID |
1605 | * @return bool $open |
1606 | */ |
1607 | function _close_comments_for_old_post( $open, $post_id ) { |
1608 | if ( ! $open ) |
1609 | return $open; |
1610 | |
1611 | if ( !get_option('close_comments_for_old_posts') ) |
1612 | return $open; |
1613 | |
1614 | $days_old = (int) get_option('close_comments_days_old'); |
1615 | if ( !$days_old ) |
1616 | return $open; |
1617 | |
1618 | $post = get_post($post_id); |
1619 | |
1620 | if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) ) |
1621 | return false; |
1622 | |
1623 | return $open; |
1624 | } |
1625 | |
1626 | ?> |