wp/wp-includes/comment.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Comment
     6  * @subpackage Comment
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Check whether a comment passes internal checks to be allowed to add.
    10  * Checks whether a comment passes internal checks to be allowed to add.
    11  *
    11  *
    12  * If manual comment moderation is set in the administration, then all checks,
    12  * If manual comment moderation is set in the administration, then all checks,
    13  * regardless of their type and substance, will fail and the function will
    13  * regardless of their type and substance, will fail and the function will
    14  * return false.
    14  * return false.
    15  *
    15  *
   147 	}
   147 	}
   148 	return true;
   148 	return true;
   149 }
   149 }
   150 
   150 
   151 /**
   151 /**
   152  * Retrieve the approved comments for post $post_id.
   152  * Retrieves the approved comments for post $post_id.
   153  *
   153  *
   154  * @since 2.0.0
   154  * @since 2.0.0
   155  * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
   155  * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
   156  *
   156  *
   157  * @param int   $post_id The ID of the post.
   157  * @param int   $post_id The ID of the post.
   158  * @param array $args    Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
   158  * @param array $args    Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
   159  * @return int|array The approved comments, or number of comments if `$count`
   159  * @return WP_Comment[]|int[]|int The approved comments, or number of comments if `$count`
   160  *                   argument is true.
   160  *                                argument is true.
   161  */
   161  */
   162 function get_approved_comments( $post_id, $args = array() ) {
   162 function get_approved_comments( $post_id, $args = array() ) {
   163 	if ( ! $post_id ) {
   163 	if ( ! $post_id ) {
   164 		return array();
   164 		return array();
   165 	}
   165 	}
   227 	}
   227 	}
   228 	return $_comment;
   228 	return $_comment;
   229 }
   229 }
   230 
   230 
   231 /**
   231 /**
   232  * Retrieve a list of comments.
   232  * Retrieves a list of comments.
   233  *
   233  *
   234  * The comment list can be for the blog as a whole or for an individual post.
   234  * The comment list can be for the blog as a whole or for an individual post.
   235  *
   235  *
   236  * @since 2.7.0
   236  * @since 2.7.0
   237  *
   237  *
   238  * @param string|array $args Optional. Array or string of arguments. See WP_Comment_Query::__construct()
   238  * @param string|array $args Optional. Array or string of arguments. See WP_Comment_Query::__construct()
   239  *                           for information on accepted arguments. Default empty.
   239  *                           for information on accepted arguments. Default empty.
   240  * @return int|array List of comments or number of found comments if `$count` argument is true.
   240  * @return WP_Comment[]|int[]|int List of comments or number of found comments if `$count` argument is true.
   241  */
   241  */
   242 function get_comments( $args = '' ) {
   242 function get_comments( $args = '' ) {
   243 	$query = new WP_Comment_Query;
   243 	$query = new WP_Comment_Query;
   244 	return $query->query( $args );
   244 	return $query->query( $args );
   245 }
   245 }
   246 
   246 
   247 /**
   247 /**
   248  * Retrieve all of the WordPress supported comment statuses.
   248  * Retrieves all of the WordPress supported comment statuses.
   249  *
   249  *
   250  * Comments have a limited set of valid status values, this provides the comment
   250  * Comments have a limited set of valid status values, this provides the comment
   251  * status values and descriptions.
   251  * status values and descriptions.
   252  *
   252  *
   253  * @since 2.7.0
   253  * @since 2.7.0
   308 	 */
   308 	 */
   309 	return apply_filters( 'get_default_comment_status', $status, $post_type, $comment_type );
   309 	return apply_filters( 'get_default_comment_status', $status, $post_type, $comment_type );
   310 }
   310 }
   311 
   311 
   312 /**
   312 /**
   313  * The date the last comment was modified.
   313  * Retrieves the date the last comment was modified.
   314  *
   314  *
   315  * @since 1.5.0
   315  * @since 1.5.0
   316  * @since 4.7.0 Replaced caching the modified date in a local static variable
   316  * @since 4.7.0 Replaced caching the modified date in a local static variable
   317  *              with the Object Cache API.
   317  *              with the Object Cache API.
   318  *
   318  *
   356 }
   356 }
   357 
   357 
   358 /**
   358 /**
   359  * Retrieves the total comment counts for the whole site or a single post.
   359  * Retrieves the total comment counts for the whole site or a single post.
   360  *
   360  *
   361  * Unlike wp_count_comments(), this function always returns the live comment counts without caching.
       
   362  *
       
   363  * @since 2.0.0
   361  * @since 2.0.0
   364  *
       
   365  * @global wpdb $wpdb WordPress database abstraction object.
       
   366  *
   362  *
   367  * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that
   363  * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that
   368  *                     comment counts for the whole site will be retrieved.
   364  *                     comment counts for the whole site will be retrieved.
   369  * @return array() {
   365  * @return int[] {
   370  *     The number of comments keyed by their status.
   366  *     The number of comments keyed by their status.
   371  *
   367  *
   372  *     @type int $approved            The number of approved comments.
   368  *     @type int $approved            The number of approved comments.
   373  *     @type int $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending).
   369  *     @type int $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending).
   374  *     @type int $spam                The number of spam comments.
   370  *     @type int $spam                The number of spam comments.
   377  *     @type int $total_comments      The total number of non-trashed comments, including spam.
   373  *     @type int $total_comments      The total number of non-trashed comments, including spam.
   378  *     @type int $all                 The total number of pending or approved comments.
   374  *     @type int $all                 The total number of pending or approved comments.
   379  * }
   375  * }
   380  */
   376  */
   381 function get_comment_count( $post_id = 0 ) {
   377 function get_comment_count( $post_id = 0 ) {
   382 	global $wpdb;
       
   383 
       
   384 	$post_id = (int) $post_id;
   378 	$post_id = (int) $post_id;
   385 
       
   386 	$where = '';
       
   387 	if ( $post_id > 0 ) {
       
   388 		$where = $wpdb->prepare( 'WHERE comment_post_ID = %d', $post_id );
       
   389 	}
       
   390 
       
   391 	$totals = (array) $wpdb->get_results(
       
   392 		"
       
   393 		SELECT comment_approved, COUNT( * ) AS total
       
   394 		FROM {$wpdb->comments}
       
   395 		{$where}
       
   396 		GROUP BY comment_approved
       
   397 	",
       
   398 		ARRAY_A
       
   399 	);
       
   400 
   379 
   401 	$comment_count = array(
   380 	$comment_count = array(
   402 		'approved'            => 0,
   381 		'approved'            => 0,
   403 		'awaiting_moderation' => 0,
   382 		'awaiting_moderation' => 0,
   404 		'spam'                => 0,
   383 		'spam'                => 0,
   406 		'post-trashed'        => 0,
   385 		'post-trashed'        => 0,
   407 		'total_comments'      => 0,
   386 		'total_comments'      => 0,
   408 		'all'                 => 0,
   387 		'all'                 => 0,
   409 	);
   388 	);
   410 
   389 
   411 	foreach ( $totals as $row ) {
   390 	$args = array(
   412 		switch ( $row['comment_approved'] ) {
   391 		'count'                     => true,
   413 			case 'trash':
   392 		'update_comment_meta_cache' => false,
   414 				$comment_count['trash'] = $row['total'];
   393 	);
   415 				break;
   394 	if ( $post_id > 0 ) {
   416 			case 'post-trashed':
   395 		$args['post_id'] = $post_id;
   417 				$comment_count['post-trashed'] = $row['total'];
   396 	}
   418 				break;
   397 	$mapping       = array(
   419 			case 'spam':
   398 		'approved'            => 'approve',
   420 				$comment_count['spam']            = $row['total'];
   399 		'awaiting_moderation' => 'hold',
   421 				$comment_count['total_comments'] += $row['total'];
   400 		'spam'                => 'spam',
   422 				break;
   401 		'trash'               => 'trash',
   423 			case '1':
   402 		'post-trashed'        => 'post-trashed',
   424 				$comment_count['approved']        = $row['total'];
   403 	);
   425 				$comment_count['total_comments'] += $row['total'];
   404 	$comment_count = array();
   426 				$comment_count['all']            += $row['total'];
   405 	foreach ( $mapping as $key => $value ) {
   427 				break;
   406 		$comment_count[ $key ] = get_comments( array_merge( $args, array( 'status' => $value ) ) );
   428 			case '0':
   407 	}
   429 				$comment_count['awaiting_moderation'] = $row['total'];
   408 
   430 				$comment_count['total_comments']     += $row['total'];
   409 	$comment_count['all']            = $comment_count['approved'] + $comment_count['awaiting_moderation'];
   431 				$comment_count['all']                += $row['total'];
   410 	$comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam'];
   432 				break;
       
   433 			default:
       
   434 				break;
       
   435 		}
       
   436 	}
       
   437 
   411 
   438 	return array_map( 'intval', $comment_count );
   412 	return array_map( 'intval', $comment_count );
   439 }
   413 }
   440 
   414 
   441 //
   415 //
   442 // Comment meta functions.
   416 // Comment meta functions.
   443 //
   417 //
   444 
   418 
   445 /**
   419 /**
   446  * Add meta data field to a comment.
   420  * Adds meta data field to a comment.
   447  *
   421  *
   448  * @since 2.9.0
   422  * @since 2.9.0
   449  *
   423  *
   450  * @link https://developer.wordpress.org/reference/functions/add_comment_meta/
   424  * @link https://developer.wordpress.org/reference/functions/add_comment_meta/
   451  *
   425  *
   459 function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
   433 function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
   460 	return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
   434 	return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
   461 }
   435 }
   462 
   436 
   463 /**
   437 /**
   464  * Remove metadata matching criteria from a comment.
   438  * Removes metadata matching criteria from a comment.
   465  *
   439  *
   466  * You can match based on the key, or key and value. Removing based on key and
   440  * You can match based on the key, or key and value. Removing based on key and
   467  * value, will keep from removing duplicate metadata with the same key. It also
   441  * value, will keep from removing duplicate metadata with the same key. It also
   468  * allows removing all metadata matching key, if needed.
   442  * allows removing all metadata matching key, if needed.
   469  *
   443  *
   481 function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
   455 function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
   482 	return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
   456 	return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
   483 }
   457 }
   484 
   458 
   485 /**
   459 /**
   486  * Retrieve comment meta field for a comment.
   460  * Retrieves comment meta field for a comment.
   487  *
   461  *
   488  * @since 2.9.0
   462  * @since 2.9.0
   489  *
   463  *
   490  * @link https://developer.wordpress.org/reference/functions/get_comment_meta/
   464  * @link https://developer.wordpress.org/reference/functions/get_comment_meta/
   491  *
   465  *
   503 function get_comment_meta( $comment_id, $key = '', $single = false ) {
   477 function get_comment_meta( $comment_id, $key = '', $single = false ) {
   504 	return get_metadata( 'comment', $comment_id, $key, $single );
   478 	return get_metadata( 'comment', $comment_id, $key, $single );
   505 }
   479 }
   506 
   480 
   507 /**
   481 /**
   508  * Update comment meta field based on comment ID.
   482  * Updates comment meta field based on comment ID.
   509  *
   483  *
   510  * Use the $prev_value parameter to differentiate between meta fields with the
   484  * Use the $prev_value parameter to differentiate between meta fields with the
   511  * same key and comment ID.
   485  * same key and comment ID.
   512  *
   486  *
   513  * If the meta field for the comment does not exist, it will be added.
   487  * If the meta field for the comment does not exist, it will be added.
  1001 
   975 
  1002 	return $comments_by_type;
   976 	return $comments_by_type;
  1003 }
   977 }
  1004 
   978 
  1005 /**
   979 /**
  1006  * Calculate the total number of comment pages.
   980  * Calculates the total number of comment pages.
  1007  *
   981  *
  1008  * @since 2.7.0
   982  * @since 2.7.0
  1009  *
   983  *
  1010  * @uses Walker_Comment
   984  * @uses Walker_Comment
  1011  *
   985  *
  1058 
  1032 
  1059 	return $count;
  1033 	return $count;
  1060 }
  1034 }
  1061 
  1035 
  1062 /**
  1036 /**
  1063  * Calculate what page number a comment will appear on for comment paging.
  1037  * Calculates what page number a comment will appear on for comment paging.
  1064  *
  1038  *
  1065  * @since 2.7.0
  1039  * @since 2.7.0
  1066  *
  1040  *
  1067  * @global wpdb $wpdb WordPress database abstraction object.
  1041  * @global wpdb $wpdb WordPress database abstraction object.
  1068  *
  1042  *
  1486 	 * Fires immediately before a comment is deleted from the database.
  1460 	 * Fires immediately before a comment is deleted from the database.
  1487 	 *
  1461 	 *
  1488 	 * @since 1.2.0
  1462 	 * @since 1.2.0
  1489 	 * @since 4.9.0 Added the `$comment` parameter.
  1463 	 * @since 4.9.0 Added the `$comment` parameter.
  1490 	 *
  1464 	 *
  1491 	 * @param int        $comment_id The comment ID.
  1465 	 * @param string     $comment_id The comment ID as a numeric string.
  1492 	 * @param WP_Comment $comment    The comment to be deleted.
  1466 	 * @param WP_Comment $comment    The comment to be deleted.
  1493 	 */
  1467 	 */
  1494 	do_action( 'delete_comment', $comment->comment_ID, $comment );
  1468 	do_action( 'delete_comment', $comment->comment_ID, $comment );
  1495 
  1469 
  1496 	// Move children up a level.
  1470 	// Move children up a level.
  1514 	 * Fires immediately after a comment is deleted from the database.
  1488 	 * Fires immediately after a comment is deleted from the database.
  1515 	 *
  1489 	 *
  1516 	 * @since 2.9.0
  1490 	 * @since 2.9.0
  1517 	 * @since 4.9.0 Added the `$comment` parameter.
  1491 	 * @since 4.9.0 Added the `$comment` parameter.
  1518 	 *
  1492 	 *
  1519 	 * @param int        $comment_id The comment ID.
  1493 	 * @param string     $comment_id The comment ID as a numeric string.
  1520 	 * @param WP_Comment $comment    The deleted comment.
  1494 	 * @param WP_Comment $comment    The deleted comment.
  1521 	 */
  1495 	 */
  1522 	do_action( 'deleted_comment', $comment->comment_ID, $comment );
  1496 	do_action( 'deleted_comment', $comment->comment_ID, $comment );
  1523 
  1497 
  1524 	$post_id = $comment->comment_post_ID;
  1498 	$post_id = $comment->comment_post_ID;
  1560 	 * Fires immediately before a comment is sent to the Trash.
  1534 	 * Fires immediately before a comment is sent to the Trash.
  1561 	 *
  1535 	 *
  1562 	 * @since 2.9.0
  1536 	 * @since 2.9.0
  1563 	 * @since 4.9.0 Added the `$comment` parameter.
  1537 	 * @since 4.9.0 Added the `$comment` parameter.
  1564 	 *
  1538 	 *
  1565 	 * @param int        $comment_id The comment ID.
  1539 	 * @param string     $comment_id The comment ID as a numeric string.
  1566 	 * @param WP_Comment $comment    The comment to be trashed.
  1540 	 * @param WP_Comment $comment    The comment to be trashed.
  1567 	 */
  1541 	 */
  1568 	do_action( 'trash_comment', $comment->comment_ID, $comment );
  1542 	do_action( 'trash_comment', $comment->comment_ID, $comment );
  1569 
  1543 
  1570 	if ( wp_set_comment_status( $comment, 'trash' ) ) {
  1544 	if ( wp_set_comment_status( $comment, 'trash' ) ) {
  1577 		 * Fires immediately after a comment is sent to Trash.
  1551 		 * Fires immediately after a comment is sent to Trash.
  1578 		 *
  1552 		 *
  1579 		 * @since 2.9.0
  1553 		 * @since 2.9.0
  1580 		 * @since 4.9.0 Added the `$comment` parameter.
  1554 		 * @since 4.9.0 Added the `$comment` parameter.
  1581 		 *
  1555 		 *
  1582 		 * @param int        $comment_id The comment ID.
  1556 		 * @param string     $comment_id The comment ID as a numeric string.
  1583 		 * @param WP_Comment $comment    The trashed comment.
  1557 		 * @param WP_Comment $comment    The trashed comment.
  1584 		 */
  1558 		 */
  1585 		do_action( 'trashed_comment', $comment->comment_ID, $comment );
  1559 		do_action( 'trashed_comment', $comment->comment_ID, $comment );
  1586 
  1560 
  1587 		return true;
  1561 		return true;
  1608 	 * Fires immediately before a comment is restored from the Trash.
  1582 	 * Fires immediately before a comment is restored from the Trash.
  1609 	 *
  1583 	 *
  1610 	 * @since 2.9.0
  1584 	 * @since 2.9.0
  1611 	 * @since 4.9.0 Added the `$comment` parameter.
  1585 	 * @since 4.9.0 Added the `$comment` parameter.
  1612 	 *
  1586 	 *
  1613 	 * @param int        $comment_id The comment ID.
  1587 	 * @param string     $comment_id The comment ID as a numeric string.
  1614 	 * @param WP_Comment $comment    The comment to be untrashed.
  1588 	 * @param WP_Comment $comment    The comment to be untrashed.
  1615 	 */
  1589 	 */
  1616 	do_action( 'untrash_comment', $comment->comment_ID, $comment );
  1590 	do_action( 'untrash_comment', $comment->comment_ID, $comment );
  1617 
  1591 
  1618 	$status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
  1592 	$status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
  1628 		 * Fires immediately after a comment is restored from the Trash.
  1602 		 * Fires immediately after a comment is restored from the Trash.
  1629 		 *
  1603 		 *
  1630 		 * @since 2.9.0
  1604 		 * @since 2.9.0
  1631 		 * @since 4.9.0 Added the `$comment` parameter.
  1605 		 * @since 4.9.0 Added the `$comment` parameter.
  1632 		 *
  1606 		 *
  1633 		 * @param int        $comment_id The comment ID.
  1607 		 * @param string     $comment_id The comment ID as a numeric string.
  1634 		 * @param WP_Comment $comment    The untrashed comment.
  1608 		 * @param WP_Comment $comment    The untrashed comment.
  1635 		 */
  1609 		 */
  1636 		do_action( 'untrashed_comment', $comment->comment_ID, $comment );
  1610 		do_action( 'untrashed_comment', $comment->comment_ID, $comment );
  1637 
  1611 
  1638 		return true;
  1612 		return true;
  1640 
  1614 
  1641 	return false;
  1615 	return false;
  1642 }
  1616 }
  1643 
  1617 
  1644 /**
  1618 /**
  1645  * Marks a comment as Spam
  1619  * Marks a comment as Spam.
  1646  *
  1620  *
  1647  * @since 2.9.0
  1621  * @since 2.9.0
  1648  *
  1622  *
  1649  * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1623  * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1650  * @return bool True on success, false on failure.
  1624  * @return bool True on success, false on failure.
  1688 
  1662 
  1689 	return false;
  1663 	return false;
  1690 }
  1664 }
  1691 
  1665 
  1692 /**
  1666 /**
  1693  * Removes a comment from the Spam
  1667  * Removes a comment from the Spam.
  1694  *
  1668  *
  1695  * @since 2.9.0
  1669  * @since 2.9.0
  1696  *
  1670  *
  1697  * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1671  * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1698  * @return bool True on success, false on failure.
  1672  * @return bool True on success, false on failure.
  1707 	 * Fires immediately before a comment is unmarked as Spam.
  1681 	 * Fires immediately before a comment is unmarked as Spam.
  1708 	 *
  1682 	 *
  1709 	 * @since 2.9.0
  1683 	 * @since 2.9.0
  1710 	 * @since 4.9.0 Added the `$comment` parameter.
  1684 	 * @since 4.9.0 Added the `$comment` parameter.
  1711 	 *
  1685 	 *
  1712 	 * @param int        $comment_id The comment ID.
  1686 	 * @param string     $comment_id The comment ID as a numeric string.
  1713 	 * @param WP_Comment $comment    The comment to be unmarked as spam.
  1687 	 * @param WP_Comment $comment    The comment to be unmarked as spam.
  1714 	 */
  1688 	 */
  1715 	do_action( 'unspam_comment', $comment->comment_ID, $comment );
  1689 	do_action( 'unspam_comment', $comment->comment_ID, $comment );
  1716 
  1690 
  1717 	$status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
  1691 	$status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
  1727 		 * Fires immediately after a comment is unmarked as Spam.
  1701 		 * Fires immediately after a comment is unmarked as Spam.
  1728 		 *
  1702 		 *
  1729 		 * @since 2.9.0
  1703 		 * @since 2.9.0
  1730 		 * @since 4.9.0 Added the `$comment` parameter.
  1704 		 * @since 4.9.0 Added the `$comment` parameter.
  1731 		 *
  1705 		 *
  1732 		 * @param int        $comment_id The comment ID.
  1706 		 * @param string     $comment_id The comment ID as a numeric string.
  1733 		 * @param WP_Comment $comment    The comment unmarked as spam.
  1707 		 * @param WP_Comment $comment    The comment unmarked as spam.
  1734 		 */
  1708 		 */
  1735 		do_action( 'unspammed_comment', $comment->comment_ID, $comment );
  1709 		do_action( 'unspammed_comment', $comment->comment_ID, $comment );
  1736 
  1710 
  1737 		return true;
  1711 		return true;
  1739 
  1713 
  1740 	return false;
  1714 	return false;
  1741 }
  1715 }
  1742 
  1716 
  1743 /**
  1717 /**
  1744  * The status of a comment by ID.
  1718  * Retrieves the status of a comment by comment ID.
  1745  *
  1719  *
  1746  * @since 1.0.0
  1720  * @since 1.0.0
  1747  *
  1721  *
  1748  * @param int|WP_Comment $comment_id Comment ID or WP_Comment object
  1722  * @param int|WP_Comment $comment_id Comment ID or WP_Comment object
  1749  * @return string|false Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
  1723  * @return string|false Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
  1770 		return false;
  1744 		return false;
  1771 	}
  1745 	}
  1772 }
  1746 }
  1773 
  1747 
  1774 /**
  1748 /**
  1775  * Call hooks for when a comment status transition occurs.
  1749  * Calls hooks for when a comment status transition occurs.
  1776  *
  1750  *
  1777  * Calls hooks for comment status transitions. If the new comment status is not the same
  1751  * Calls hooks for comment status transitions. If the new comment status is not the same
  1778  * as the previous comment status, then two hooks will be ran, the first is
  1752  * as the previous comment status, then two hooks will be ran, the first is
  1779  * {@see 'transition_comment_status'} with new status, old status, and comment data.
  1753  * {@see 'transition_comment_status'} with new status, old status, and comment data.
  1780  * The next action called is {@see 'comment_$old_status_to_$new_status'}. It has
  1754  * The next action called is {@see 'comment_$old_status_to_$new_status'}. It has
  1824 		 * Fires when the comment status is in transition from one specific status to another.
  1798 		 * Fires when the comment status is in transition from one specific status to another.
  1825 		 *
  1799 		 *
  1826 		 * The dynamic portions of the hook name, `$old_status`, and `$new_status`,
  1800 		 * The dynamic portions of the hook name, `$old_status`, and `$new_status`,
  1827 		 * refer to the old and new comment statuses, respectively.
  1801 		 * refer to the old and new comment statuses, respectively.
  1828 		 *
  1802 		 *
       
  1803 		 * Possible hook names include:
       
  1804 		 *
       
  1805 		 *  - `comment_unapproved_to_approved`
       
  1806 		 *  - `comment_spam_to_approved`
       
  1807 		 *  - `comment_approved_to_unapproved`
       
  1808 		 *  - `comment_spam_to_unapproved`
       
  1809 		 *  - `comment_unapproved_to_spam`
       
  1810 		 *  - `comment_approved_to_spam`
       
  1811 		 *
  1829 		 * @since 2.7.0
  1812 		 * @since 2.7.0
  1830 		 *
  1813 		 *
  1831 		 * @param WP_Comment $comment Comment object.
  1814 		 * @param WP_Comment $comment Comment object.
  1832 		 */
  1815 		 */
  1833 		do_action( "comment_{$old_status}_to_{$new_status}", $comment );
  1816 		do_action( "comment_{$old_status}_to_{$new_status}", $comment );
  1836 	 * Fires when the status of a specific comment type is in transition.
  1819 	 * Fires when the status of a specific comment type is in transition.
  1837 	 *
  1820 	 *
  1838 	 * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
  1821 	 * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
  1839 	 * refer to the new comment status, and the type of comment, respectively.
  1822 	 * refer to the new comment status, and the type of comment, respectively.
  1840 	 *
  1823 	 *
  1841 	 * Typical comment types include an empty string (standard comment), 'pingback',
  1824 	 * Typical comment types include 'comment', 'pingback', or 'trackback'.
  1842 	 * or 'trackback'.
  1825 	 *
       
  1826 	 * Possible hook names include:
       
  1827 	 *
       
  1828 	 *  - `comment_approved_comment`
       
  1829 	 *  - `comment_approved_pingback`
       
  1830 	 *  - `comment_approved_trackback`
       
  1831 	 *  - `comment_unapproved_comment`
       
  1832 	 *  - `comment_unapproved_pingback`
       
  1833 	 *  - `comment_unapproved_trackback`
       
  1834 	 *  - `comment_spam_comment`
       
  1835 	 *  - `comment_spam_pingback`
       
  1836 	 *  - `comment_spam_trackback`
  1843 	 *
  1837 	 *
  1844 	 * @since 2.7.0
  1838 	 * @since 2.7.0
  1845 	 *
  1839 	 *
  1846 	 * @param int        $comment_ID The comment ID.
  1840 	 * @param string     $comment_ID The comment ID as a numeric string.
  1847 	 * @param WP_Comment $comment    Comment object.
  1841 	 * @param WP_Comment $comment    Comment object.
  1848 	 */
  1842 	 */
  1849 	do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
  1843 	do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
  1850 }
  1844 }
  1851 
  1845 
  1852 /**
  1846 /**
  1853  * Clear the lastcommentmodified cached value when a comment status is changed.
  1847  * Clears the lastcommentmodified cached value when a comment status is changed.
  1854  *
  1848  *
  1855  * Deletes the lastcommentmodified cache key when a comment enters or leaves
  1849  * Deletes the lastcommentmodified cache key when a comment enters or leaves
  1856  * 'approved' status.
  1850  * 'approved' status.
  1857  *
  1851  *
  1858  * @since 4.7.0
  1852  * @since 4.7.0
  1861  * @param string $new_status The new comment status.
  1855  * @param string $new_status The new comment status.
  1862  * @param string $old_status The old comment status.
  1856  * @param string $old_status The old comment status.
  1863  */
  1857  */
  1864 function _clear_modified_cache_on_transition_comment_status( $new_status, $old_status ) {
  1858 function _clear_modified_cache_on_transition_comment_status( $new_status, $old_status ) {
  1865 	if ( 'approved' === $new_status || 'approved' === $old_status ) {
  1859 	if ( 'approved' === $new_status || 'approved' === $old_status ) {
       
  1860 		$data = array();
  1866 		foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
  1861 		foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
  1867 			wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' );
  1862 			$data[] = "lastcommentmodified:$timezone";
  1868 		}
  1863 		}
  1869 	}
  1864 		wp_cache_delete_multiple( $data, 'timeinfo' );
  1870 }
  1865 	}
  1871 
  1866 }
  1872 /**
  1867 
  1873  * Get current commenter's name, email, and URL.
  1868 /**
       
  1869  * Gets current commenter's name, email, and URL.
  1874  *
  1870  *
  1875  * Expects cookies content to already be sanitized. User of this function might
  1871  * Expects cookies content to already be sanitized. User of this function might
  1876  * wish to recheck the returned array for validity.
  1872  * wish to recheck the returned array for validity.
  1877  *
  1873  *
  1878  * @see sanitize_comment_cookies() Use to sanitize cookies
  1874  * @see sanitize_comment_cookies() Use to sanitize cookies
  1920 	 */
  1916 	 */
  1921 	return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) );
  1917 	return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) );
  1922 }
  1918 }
  1923 
  1919 
  1924 /**
  1920 /**
  1925  * Get unapproved comment author's email.
  1921  * Gets unapproved comment author's email.
  1926  *
  1922  *
  1927  * Used to allow the commenter to see their pending comment.
  1923  * Used to allow the commenter to see their pending comment.
  1928  *
  1924  *
  1929  * @since 5.1.0
  1925  * @since 5.1.0
  1930  * @since 5.7.0 The window within which the author email for an unapproved comment
  1926  * @since 5.7.0 The window within which the author email for an unapproved comment
  2023 	$id = (int) $wpdb->insert_id;
  2019 	$id = (int) $wpdb->insert_id;
  2024 
  2020 
  2025 	if ( 1 == $comment_approved ) {
  2021 	if ( 1 == $comment_approved ) {
  2026 		wp_update_comment_count( $comment_post_ID );
  2022 		wp_update_comment_count( $comment_post_ID );
  2027 
  2023 
       
  2024 		$data = array();
  2028 		foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
  2025 		foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
  2029 			wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' );
  2026 			$data[] = "lastcommentmodified:$timezone";
  2030 		}
  2027 		}
       
  2028 		wp_cache_delete_multiple( $data, 'timeinfo' );
  2031 	}
  2029 	}
  2032 
  2030 
  2033 	clean_comment_cache( $id );
  2031 	clean_comment_cache( $id );
  2034 
  2032 
  2035 	$comment = get_comment( $id );
  2033 	$comment = get_comment( $id );
  2117 	$commentdata['filtered']             = true;
  2115 	$commentdata['filtered']             = true;
  2118 	return $commentdata;
  2116 	return $commentdata;
  2119 }
  2117 }
  2120 
  2118 
  2121 /**
  2119 /**
  2122  * Whether a comment should be blocked because of comment flood.
  2120  * Determines whether a comment should be blocked because of comment flood.
  2123  *
  2121  *
  2124  * @since 2.1.0
  2122  * @since 2.1.0
  2125  *
  2123  *
  2126  * @param bool $block            Whether plugin has already blocked comment.
  2124  * @param bool $block            Whether plugin has already blocked comment.
  2127  * @param int  $time_lastcomment Timestamp for last comment.
  2125  * @param int  $time_lastcomment Timestamp for last comment.
  2287 
  2285 
  2288 	return $comment_ID;
  2286 	return $comment_ID;
  2289 }
  2287 }
  2290 
  2288 
  2291 /**
  2289 /**
  2292  * Send a comment moderation notification to the comment moderator.
  2290  * Sends a comment moderation notification to the comment moderator.
  2293  *
  2291  *
  2294  * @since 4.4.0
  2292  * @since 4.4.0
  2295  *
  2293  *
  2296  * @param int $comment_ID ID of the comment.
  2294  * @param int $comment_ID ID of the comment.
  2297  * @return bool True on success, false on failure.
  2295  * @return bool True on success, false on failure.
  2311 
  2309 
  2312 	return wp_notify_moderator( $comment_ID );
  2310 	return wp_notify_moderator( $comment_ID );
  2313 }
  2311 }
  2314 
  2312 
  2315 /**
  2313 /**
  2316  * Send a notification of a new comment to the post author.
  2314  * Sends a notification of a new comment to the post author.
  2317  *
  2315  *
  2318  * @since 4.4.0
  2316  * @since 4.4.0
  2319  *
  2317  *
  2320  * Uses the {@see 'notify_post_author'} filter to determine whether the post author
  2318  * Uses the {@see 'notify_post_author'} filter to determine whether the post author
  2321  * should be notified when a new comment is added, overriding site setting.
  2319  * should be notified when a new comment is added, overriding site setting.
  2411 	 * Fires immediately after transitioning a comment's status from one to another in the database
  2409 	 * Fires immediately after transitioning a comment's status from one to another in the database
  2412 	 * and removing the comment from the object cache, but prior to all status transition hooks.
  2410 	 * and removing the comment from the object cache, but prior to all status transition hooks.
  2413 	 *
  2411 	 *
  2414 	 * @since 1.5.0
  2412 	 * @since 1.5.0
  2415 	 *
  2413 	 *
  2416 	 * @param int    $comment_id     Comment ID.
  2414 	 * @param string $comment_id     Comment ID as a numeric string.
  2417 	 * @param string $comment_status Current comment status. Possible values include
  2415 	 * @param string $comment_status Current comment status. Possible values include
  2418 	 *                               'hold', '0', 'approve', '1', 'spam', and 'trash'.
  2416 	 *                               'hold', '0', 'approve', '1', 'spam', and 'trash'.
  2419 	 */
  2417 	 */
  2420 	do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status );
  2418 	do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status );
  2421 
  2419 
  2567 
  2565 
  2568 	return $rval;
  2566 	return $rval;
  2569 }
  2567 }
  2570 
  2568 
  2571 /**
  2569 /**
  2572  * Whether to defer comment counting.
  2570  * Determines whether to defer comment counting.
  2573  *
  2571  *
  2574  * When setting $defer to true, all post comment counts will not be updated
  2572  * When setting $defer to true, all post comment counts will not be updated
  2575  * until $defer is set to false. When $defer is set to false, then all
  2573  * until $defer is set to false. When $defer is set to false, then all
  2576  * previously deferred updated post comment counts will then be automatically
  2574  * previously deferred updated post comment counts will then be automatically
  2577  * updated without having to call wp_update_comment_count() after.
  2575  * updated without having to call wp_update_comment_count() after.
  2802 
  2800 
  2803 	return false;
  2801 	return false;
  2804 }
  2802 }
  2805 
  2803 
  2806 /**
  2804 /**
  2807  * Perform all pingbacks, enclosures, trackbacks, and send to pingback services.
  2805  * Performs all pingbacks, enclosures, trackbacks, and sends to pingback services.
  2808  *
  2806  *
  2809  * @since 2.1.0
  2807  * @since 2.1.0
  2810  * @since 5.6.0 Introduced `do_all_pings` action hook for individual services.
  2808  * @since 5.6.0 Introduced `do_all_pings` action hook for individual services.
  2811  */
  2809  */
  2812 function do_all_pings() {
  2810 function do_all_pings() {
  2817 	 */
  2815 	 */
  2818 	do_action( 'do_all_pings' );
  2816 	do_action( 'do_all_pings' );
  2819 }
  2817 }
  2820 
  2818 
  2821 /**
  2819 /**
  2822  * Perform all pingbacks.
  2820  * Performs all pingbacks.
  2823  *
  2821  *
  2824  * @since 5.6.0
  2822  * @since 5.6.0
  2825  */
  2823  */
  2826 function do_all_pingbacks() {
  2824 function do_all_pingbacks() {
  2827 	$pings = get_posts(
  2825 	$pings = get_posts(
  2839 		pingback( null, $ping );
  2837 		pingback( null, $ping );
  2840 	}
  2838 	}
  2841 }
  2839 }
  2842 
  2840 
  2843 /**
  2841 /**
  2844  * Perform all enclosures.
  2842  * Performs all enclosures.
  2845  *
  2843  *
  2846  * @since 5.6.0
  2844  * @since 5.6.0
  2847  */
  2845  */
  2848 function do_all_enclosures() {
  2846 function do_all_enclosures() {
  2849 	$enclosures = get_posts(
  2847 	$enclosures = get_posts(
  2861 		do_enclose( null, $enclosure );
  2859 		do_enclose( null, $enclosure );
  2862 	}
  2860 	}
  2863 }
  2861 }
  2864 
  2862 
  2865 /**
  2863 /**
  2866  * Perform all trackbacks.
  2864  * Performs all trackbacks.
  2867  *
  2865  *
  2868  * @since 5.6.0
  2866  * @since 5.6.0
  2869  */
  2867  */
  2870 function do_all_trackbacks() {
  2868 function do_all_trackbacks() {
  2871 	$trackbacks = get_posts(
  2869 	$trackbacks = get_posts(
  2883 		do_trackbacks( $trackback );
  2881 		do_trackbacks( $trackback );
  2884 	}
  2882 	}
  2885 }
  2883 }
  2886 
  2884 
  2887 /**
  2885 /**
  2888  * Perform trackbacks.
  2886  * Performs trackbacks.
  2889  *
  2887  *
  2890  * @since 1.5.0
  2888  * @since 1.5.0
  2891  * @since 4.7.0 `$post_id` can be a WP_Post object.
  2889  * @since 4.7.0 `$post_id` can be a WP_Post object.
  2892  *
  2890  *
  2893  * @global wpdb $wpdb WordPress database abstraction object.
  2891  * @global wpdb $wpdb WordPress database abstraction object.
  3071 		}
  3069 		}
  3072 	}
  3070 	}
  3073 }
  3071 }
  3074 
  3072 
  3075 /**
  3073 /**
  3076  * Check whether blog is public before returning sites.
  3074  * Checks whether blog is public before returning sites.
  3077  *
  3075  *
  3078  * @since 2.1.0
  3076  * @since 2.1.0
  3079  *
  3077  *
  3080  * @param mixed $sites Will return if blog is public, will not return if not public.
  3078  * @param mixed $sites Will return if blog is public, will not return if not public.
  3081  * @return mixed Empty string if blog is not public, returns $sites, if site is public.
  3079  * @return mixed Empty string if blog is not public, returns $sites, if site is public.
  3087 		return '';
  3085 		return '';
  3088 	}
  3086 	}
  3089 }
  3087 }
  3090 
  3088 
  3091 /**
  3089 /**
  3092  * Send a Trackback.
  3090  * Sends a Trackback.
  3093  *
  3091  *
  3094  * Updates database when sending trackback to prevent duplicates.
  3092  * Updates database when sending trackback to prevent duplicates.
  3095  *
  3093  *
  3096  * @since 0.71
  3094  * @since 0.71
  3097  *
  3095  *
  3128 	$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID ) );
  3126 	$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID ) );
  3129 	return $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID ) );
  3127 	return $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID ) );
  3130 }
  3128 }
  3131 
  3129 
  3132 /**
  3130 /**
  3133  * Send a pingback.
  3131  * Sends a pingback.
  3134  *
  3132  *
  3135  * @since 1.2.0
  3133  * @since 1.2.0
  3136  *
  3134  *
  3137  * @param string $server Host of blog to connect to.
  3135  * @param string $server Host of blog to connect to.
  3138  * @param string $path Path to send the ping.
  3136  * @param string $path Path to send the ping.
  3153 		$client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home );
  3151 		$client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home );
  3154 	}
  3152 	}
  3155 }
  3153 }
  3156 
  3154 
  3157 /**
  3155 /**
  3158  * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI
  3156  * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI.
  3159  *
  3157  *
  3160  * @since 3.5.1
  3158  * @since 3.5.1
  3161  *
  3159  *
  3162  * @see wp_http_validate_url()
  3160  * @see wp_http_validate_url()
  3163  *
  3161  *
  3198  * @since 2.3.0
  3196  * @since 2.3.0
  3199  *
  3197  *
  3200  * @param int|array $ids Comment ID or an array of comment IDs to remove from cache.
  3198  * @param int|array $ids Comment ID or an array of comment IDs to remove from cache.
  3201  */
  3199  */
  3202 function clean_comment_cache( $ids ) {
  3200 function clean_comment_cache( $ids ) {
  3203 	foreach ( (array) $ids as $id ) {
  3201 	$comment_ids = (array) $ids;
  3204 		wp_cache_delete( $id, 'comment' );
  3202 	wp_cache_delete_multiple( $comment_ids, 'comment' );
  3205 
  3203 	foreach ( $comment_ids as $id ) {
  3206 		/**
  3204 		/**
  3207 		 * Fires immediately after a comment has been removed from the object cache.
  3205 		 * Fires immediately after a comment has been removed from the object cache.
  3208 		 *
  3206 		 *
  3209 		 * @since 4.5.0
  3207 		 * @since 4.5.0
  3210 		 *
  3208 		 *
  3228  *
  3226  *
  3229  * @param WP_Comment[] $comments          Array of comment objects
  3227  * @param WP_Comment[] $comments          Array of comment objects
  3230  * @param bool         $update_meta_cache Whether to update commentmeta cache. Default true.
  3228  * @param bool         $update_meta_cache Whether to update commentmeta cache. Default true.
  3231  */
  3229  */
  3232 function update_comment_cache( $comments, $update_meta_cache = true ) {
  3230 function update_comment_cache( $comments, $update_meta_cache = true ) {
       
  3231 	$data = array();
  3233 	foreach ( (array) $comments as $comment ) {
  3232 	foreach ( (array) $comments as $comment ) {
  3234 		wp_cache_add( $comment->comment_ID, $comment, 'comment' );
  3233 		$data[ $comment->comment_ID ] = $comment;
  3235 	}
  3234 	}
       
  3235 	wp_cache_add_multiple( $data, 'comment' );
  3236 
  3236 
  3237 	if ( $update_meta_cache ) {
  3237 	if ( $update_meta_cache ) {
  3238 		// Avoid `wp_list_pluck()` in case `$comments` is passed by reference.
  3238 		// Avoid `wp_list_pluck()` in case `$comments` is passed by reference.
  3239 		$comment_ids = array();
  3239 		$comment_ids = array();
  3240 		foreach ( $comments as $comment ) {
  3240 		foreach ( $comments as $comment ) {
  3270 //
  3270 //
  3271 // Internal.
  3271 // Internal.
  3272 //
  3272 //
  3273 
  3273 
  3274 /**
  3274 /**
  3275  * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
  3275  * Closes comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
  3276  *
  3276  *
  3277  * @since 2.7.0
  3277  * @since 2.7.0
  3278  * @access private
  3278  * @access private
  3279  *
  3279  *
  3280  * @param WP_Post  $posts Post data object.
  3280  * @param WP_Post  $posts Post data object.
  3310 
  3310 
  3311 	return $posts;
  3311 	return $posts;
  3312 }
  3312 }
  3313 
  3313 
  3314 /**
  3314 /**
  3315  * Close comments on an old post. Hooked to comments_open and pings_open.
  3315  * Closes comments on an old post. Hooked to comments_open and pings_open.
  3316  *
  3316  *
  3317  * @since 2.7.0
  3317  * @since 2.7.0
  3318  * @access private
  3318  * @access private
  3319  *
  3319  *
  3320  * @param bool $open    Comments open or closed.
  3320  * @param bool $open    Comments open or closed.
  3527 
  3527 
  3528 	$comment_type = 'comment';
  3528 	$comment_type = 'comment';
  3529 
  3529 
  3530 	if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
  3530 	if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
  3531 		if ( '' == $comment_author_email || '' == $comment_author ) {
  3531 		if ( '' == $comment_author_email || '' == $comment_author ) {
  3532 			return new WP_Error( 'require_name_email', __( '<strong>Error</strong>: Please fill the required fields (name, email).' ), 200 );
  3532 			return new WP_Error( 'require_name_email', __( '<strong>Error</strong>: Please fill the required fields.' ), 200 );
  3533 		} elseif ( ! is_email( $comment_author_email ) ) {
  3533 		} elseif ( ! is_email( $comment_author_email ) ) {
  3534 			return new WP_Error( 'require_valid_email', __( '<strong>Error</strong>: Please enter a valid email address.' ), 200 );
  3534 			return new WP_Error( 'require_valid_email', __( '<strong>Error</strong>: Please enter a valid email address.' ), 200 );
  3535 		}
  3535 		}
  3536 	}
  3536 	}
  3537 
  3537