WordPressを読む 45-3 /blog/wp-includes/comment.php 3
2015/01/11
目次
/blog/wp-includes/comment.php 3
関数 get_comment_pages_count()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /** * Calculate the total number of comment pages. * * @since 2.7.0 * * @uses Walker_Comment * * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments * @param int $per_page Optional comments per page. * @param boolean $threaded Optional control over flat or threaded comments. * @return int Number of comment pages. */ function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) { global $wp_query; if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) ) return $wp_query->max_num_comment_pages; if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) $comments = $wp_query->comments; if ( empty($comments) ) return 0; if ( ! get_option( 'page_comments' ) ) return 1; if ( !isset($per_page) ) $per_page = (int) get_query_var('comments_per_page'); if ( 0 === $per_page ) $per_page = (int) get_option('comments_per_page'); if ( 0 === $per_page ) return 1; if ( !isset($threaded) ) $threaded = get_option('thread_comments'); if ( $threaded ) { $walker = new Walker_Comment; $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page ); } else { $count = ceil( count( $comments ) / $per_page ); } return $count; } |
関数 get_page_of_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /** * Calculate what page number a comment will appear on for comment paging. * * @since 2.7.0 * @uses get_comment() Gets the full comment of the $comment_ID parameter. * @uses get_option() Get various settings to control function and defaults. * @uses get_page_of_comment() Used to loop up to top level comment. * * @param int $comment_ID Comment ID. * @param array $args Optional args. * @return int|null Comment page number or null on error. */ function get_page_of_comment( $comment_ID, $args = array() ) { global $wpdb; if ( !$comment = get_comment( $comment_ID ) ) return; $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' ); $args = wp_parse_args( $args, $defaults ); if ( '' === $args['per_page'] && get_option('page_comments') ) $args['per_page'] = get_query_var('comments_per_page'); if ( empty($args['per_page']) ) { $args['per_page'] = 0; $args['page'] = 0; } if ( $args['per_page'] < 1 ) return 1; if ( '' === $args['max_depth'] ) { if ( get_option('thread_comments') ) $args['max_depth'] = get_option('thread_comments_depth'); else $args['max_depth'] = -1; } // Find this comment's top level parent if threading is enabled if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) return get_page_of_comment( $comment->comment_parent, $args ); $allowedtypes = array( 'comment' => '', 'pingback' => 'pingback', 'trackback' => 'trackback', ); $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : ''; // Count comments older than this one $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) ); // No older comments? Then it's page #1. if ( 0 == $oldercoms ) return 1; // Divide comments older than this one by comments per page to get this comment's page number return ceil( ( $oldercoms + 1 ) / $args['per_page'] ); } |
関数 wp_blacklist_check()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | /** * Does comment contain blacklisted characters or words. * * @since 1.5.0 * * @param string $author The author of the comment * @param string $email The email of the comment * @param string $url The url used in the comment * @param string $comment The comment content * @param string $user_ip The comment author IP address * @param string $user_agent The author's browser user agent * @return bool True if comment contains blacklisted content, false if comment does not */ function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) { /** * Fires before the comment is tested for blacklisted characters or words. * * @since 1.5.0 * * @param string $author Comment author. * @param string $email Comment author's email. * @param string $url Comment author's URL. * @param string $comment Comment content. * @param string $user_ip Comment author's IP address. * @param string $user_agent Comment author's browser user agent. */ do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent ); $mod_keys = trim( get_option('blacklist_keys') ); if ( '' == $mod_keys ) return false; // If moderation keys are empty $words = explode("\n", $mod_keys ); foreach ( (array) $words as $word ) { $word = trim($word); // Skip empty lines if ( empty($word) ) { continue; } // Do some escaping magic so that '#' chars in the // spam words don't break things: $word = preg_quote($word, '#'); $pattern = "#$word#i"; if ( preg_match($pattern, $author) || preg_match($pattern, $email) || preg_match($pattern, $url) || preg_match($pattern, $comment) || preg_match($pattern, $user_ip) || preg_match($pattern, $user_agent) ) return true; } return false; } |
関数 wp_count_comments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | /** * Retrieve total comments for blog or single post. * * The properties of the returned object contain the 'moderated', 'approved', * and spam comments for either the entire blog or single post. Those properties * contain the amount of comments that match the status. The 'total_comments' * property contains the integer of total comments. * * The comment stats are cached and then retrieved, if they already exist in the * cache. * * @since 2.5.0 * * @param int $post_id Optional. Post ID. * @return object Comment stats. */ function wp_count_comments( $post_id = 0 ) { global $wpdb; $post_id = (int) $post_id; /** * Filter the comments count for a given post. * * @since 2.7.0 * * @param array $count An empty array. * @param int $post_id The post ID. */ $stats = apply_filters( 'wp_count_comments', array(), $post_id ); if ( !empty($stats) ) return $stats; $count = wp_cache_get("comments-{$post_id}", 'counts'); if ( false !== $count ) return $count; $where = ''; if ( $post_id > 0 ) $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id ); $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A ); $total = 0; $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed'); foreach ( (array) $count as $row ) { // Don't count post-trashed toward totals if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) $total += $row['num_comments']; if ( isset( $approved[$row['comment_approved']] ) ) $stats[$approved[$row['comment_approved']]] = $row['num_comments']; } $stats['total_comments'] = $total; foreach ( $approved as $key ) { if ( empty($stats[$key]) ) $stats[$key] = 0; } $stats = (object) $stats; wp_cache_set("comments-{$post_id}", $stats, 'counts'); return $stats; } |
関数 wp_delete_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /** * Trashes or deletes a comment. * * The comment is moved to trash instead of permanently deleted unless trash is * disabled, item is already in the trash, or $force_delete is true. * * The post comment count will be updated if the comment was approved and has a * post ID available. * * @since 2.0.0 * @uses $wpdb * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object * * @param int $comment_id Comment ID * @param bool $force_delete Whether to bypass trash and force deletion. Default is false. * @return bool True on success, false on failure. */ function wp_delete_comment($comment_id, $force_delete = false) { global $wpdb; if (!$comment = get_comment($comment_id)) return false; if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) ) return wp_trash_comment($comment_id); /** * Fires immediately before a comment is deleted from the database. * * @since 1.2.0 * * @param int $comment_id The comment ID. */ do_action( 'delete_comment', $comment_id ); // Move children up a level. $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) ); if ( !empty($children) ) { $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id)); clean_comment_cache($children); } // Delete metadata $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) ); foreach ( $meta_ids as $mid ) delete_metadata_by_mid( 'comment', $mid ); if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) ) return false; /** * Fires immediately after a comment is deleted from the database. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'deleted_comment', $comment_id ); $post_id = $comment->comment_post_ID; if ( $post_id && $comment->comment_approved == 1 ) wp_update_comment_count($post_id); clean_comment_cache($comment_id); /** This action is documented in wp-includes/comment.php */ do_action( 'wp_set_comment_status', $comment_id, 'delete' ); wp_transition_comment_status('delete', $comment->comment_approved, $comment); return true; } |
関数 wp_trash_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | /** * Moves a comment to the Trash * * If trash is disabled, comment is permanently deleted. * * @since 2.9.0 * * @uses wp_delete_comment() if trash is disabled * * @param int $comment_id Comment ID. * @return bool True on success, false on failure. */ function wp_trash_comment($comment_id) { if ( !EMPTY_TRASH_DAYS ) return wp_delete_comment($comment_id, true); if ( !$comment = get_comment($comment_id) ) return false; /** * Fires immediately before a comment is sent to the Trash. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'trash_comment', $comment_id ); if ( wp_set_comment_status($comment_id, 'trash') ) { add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved); add_comment_meta($comment_id, '_wp_trash_meta_time', time() ); /** * Fires immediately after a comment is sent to Trash. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'trashed_comment', $comment_id ); return true; } return false; } |
関数 wp_untrash_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /** * Removes a comment from the Trash * * @since 2.9.0 * * @param int $comment_id Comment ID. * @return bool True on success, false on failure. */ function wp_untrash_comment($comment_id) { if ( ! (int)$comment_id ) return false; /** * Fires immediately before a comment is restored from the Trash. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'untrash_comment', $comment_id ); $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true); if ( empty($status) ) $status = '0'; if ( wp_set_comment_status($comment_id, $status) ) { delete_comment_meta($comment_id, '_wp_trash_meta_time'); delete_comment_meta($comment_id, '_wp_trash_meta_status'); /** * Fires immediately after a comment is restored from the Trash. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'untrashed_comment', $comment_id ); return true; } return false; } |
関数 wp_spam_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * Marks a comment as Spam * * @since 2.9.0 * * @param int $comment_id Comment ID. * @return bool True on success, false on failure. */ function wp_spam_comment($comment_id) { if ( !$comment = get_comment($comment_id) ) return false; /** * Fires immediately before a comment is marked as Spam. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'spam_comment', $comment_id ); if ( wp_set_comment_status($comment_id, 'spam') ) { add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved); /** * Fires immediately after a comment is marked as Spam. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'spammed_comment', $comment_id ); return true; } return false; } |
関数 wp_unspam_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /** * Removes a comment from the Spam * * @since 2.9.0 * * @param int $comment_id Comment ID. * @return bool True on success, false on failure. */ function wp_unspam_comment($comment_id) { if ( ! (int)$comment_id ) return false; /** * Fires immediately before a comment is unmarked as Spam. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'unspam_comment', $comment_id ); $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true); if ( empty($status) ) $status = '0'; if ( wp_set_comment_status($comment_id, $status) ) { delete_comment_meta($comment_id, '_wp_trash_meta_status'); /** * Fires immediately after a comment is unmarked as Spam. * * @since 2.9.0 * * @param int $comment_id The comment ID. */ do_action( 'unspammed_comment', $comment_id ); return true; } return false; } |
関数 wp_get_comment_status()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * The status of a comment by ID. * * @since 1.0.0 * * @param int $comment_id Comment ID * @return string|bool Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure. */ function wp_get_comment_status($comment_id) { $comment = get_comment($comment_id); if ( !$comment ) return false; $approved = $comment->comment_approved; if ( $approved == null ) return false; elseif ( $approved == '1' ) return 'approved'; elseif ( $approved == '0' ) return 'unapproved'; elseif ( $approved == 'spam' ) return 'spam'; elseif ( $approved == 'trash' ) return 'trash'; else return false; } |