+ * @param array $comments Array of comments
+ * @return array Array of comments keyed by comment_type.
+ */
+function &separate_comments(&$comments) {
+ $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
+ $count = count($comments);
+ for ( $i = 0; $i < $count; $i++ ) {
+ $type = $comments[$i]->comment_type;
+ if ( empty($type) )
+ $type = 'comment';
+ $comments_by_type[$type][] = &$comments[$i];
+ if ( 'trackback' == $type || 'pingback' == $type )
+ $comments_by_type['pings'][] = &$comments[$i];
+ }
+
+ return $comments_by_type;
+}
+
+/**
+ * Calculate the total number of comment pages.
+ *
+ * @since 2.7.0
+ * @uses get_query_var() Used to fill in the default for $per_page parameter.
+ * @uses get_option() Used to fill in defaults for parameters.
+ * @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) )
+ $comments = $wp_query->comments;
+
+ if ( empty($comments) )
+ return 0;
+
+ 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;
+}
+
+/**
+ * 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'] );
+}
+
+/**
+ * Does comment contain blacklisted characters or words.