raw
mp-wp_genesis           1 <?php

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