First cut of comment paging. Add paging and threading settings. see #7769 #7635

git-svn-id: http://svn.automattic.com/wordpress/trunk@8961 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-09-23 21:11:27 +00:00
parent ab861693c4
commit 04ce449e3c
8 changed files with 190 additions and 24 deletions

View File

@ -40,6 +40,14 @@ include('admin-header.php');
<input name="close_comments_for_old_posts" type="checkbox" id="close_comments_for_old_posts" value="1" <?php checked('1', get_option('close_comments_for_old_posts')); ?> /> <input name="close_comments_for_old_posts" type="checkbox" id="close_comments_for_old_posts" value="1" <?php checked('1', get_option('close_comments_for_old_posts')); ?> />
<?php _e('Close comments on articles older than') ?></label> <?php printf(__('%s days'), '<input name="close_comments_days_old" type="text" id="close_comments_days_old" value="' . attribute_escape(get_option('close_comments_days_old')) . '" size="3" />') ?> <?php _e('Close comments on articles older than') ?></label> <?php printf(__('%s days'), '<input name="close_comments_days_old" type="text" id="close_comments_days_old" value="' . attribute_escape(get_option('close_comments_days_old')) . '" size="3" />') ?>
<br /> <br />
<label for="thread_comments">
<input name="thread_comments" type="checkbox" id="thread_comments" value="1" <?php checked('1', get_option('thread_comments')); ?> />
<?php _e('Group replies into threads') ?></label> <?php printf(__('%s levels deep'), '<input name="thread_comments_depth" type="text" id="thread_comments_depth" value="' . attribute_escape(get_option('thread_comments_depth')) . '" size="3" />') ?>
<br />
<label for="page_comments">
<input name="page_comments" type="checkbox" id="page_comments" value="1" <?php checked('1', get_option('page_comments')); ?> />
<?php _e('Break comments into pages with') ?></label> <?php printf(__('%s comments per page'), '<input name="comments_per_page" type="text" id="comments_per_page" value="' . attribute_escape(get_option('comments_per_page')) . '" size="3" />') ?>
<br />
<small><em><?php echo '(' . __('These settings may be overridden for individual articles.') . ')'; ?></em></small> <small><em><?php echo '(' . __('These settings may be overridden for individual articles.') . ')'; ?></em></small>
</fieldset></td> </fieldset></td>
</tr> </tr>

View File

@ -23,7 +23,7 @@ wp_reset_vars(array('action'));
$whitelist_options = array( $whitelist_options = array(
'general' => array('blogname', 'blogdescription', 'admin_email', 'users_can_register', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'comment_registration', 'default_role' ), 'general' => array('blogname', 'blogdescription', 'admin_email', 'users_can_register', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'comment_registration', 'default_role' ),
'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'close_comments_for_old_posts', 'close_comments_days_old' ), 'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page' ),
'misc' => array( 'hack_file', 'use_linksupdate', 'uploads_use_yearmonth_folders', 'upload_path' ), 'misc' => array( 'hack_file', 'use_linksupdate', 'uploads_use_yearmonth_folders', 'upload_path' ),
'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type' ), 'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type' ),
'privacy' => array( 'blog_public' ), 'privacy' => array( 'blog_public' ),

View File

@ -15,9 +15,12 @@
<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3> <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>
<ol class="commentlist"> <ol class="commentlist">
<?php wp_list_comments($comments); ?> <?php wp_list_comments(); ?>
</ol> </ol>
<div class="navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>
<?php else : // this is displayed if there are no comments so far ?> <?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?> <?php if ('open' == $post->comment_status) : ?>

View File

@ -26,7 +26,7 @@ class WP {
* @access public * @access public
* @var array * @var array
*/ */
var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term'); var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage');
/** /**
* Private query variables. * Private query variables.
@ -36,7 +36,7 @@ class WP {
* @since 2.0.0 * @since 2.0.0
* @var array * @var array
*/ */
var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging', 'post_type', 'post_status', 'category__in', 'category__not_in', 'category__and', 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'tag_id', 'post_mime_type', 'perm'); var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging', 'post_type', 'post_status', 'category__in', 'category__not_in', 'category__and', 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'tag_id', 'post_mime_type', 'perm', 'comments_per_page');
/** /**
* Extra query variables set by the user. * Extra query variables set by the user.
@ -730,6 +730,15 @@ class Walker {
*/ */
var $db_fields; var $db_fields;
/**
* Max number of pages walked by the paged walker
*
* @since 2.7.0
* @var int
* @access protected
*/
var $max_pages = 1;
/** /**
* Starts the list before the elements are added. * Starts the list before the elements are added.
* *
@ -947,7 +956,7 @@ class Walker {
function paged_walk( $elements, $max_depth, $page_num, $per_page ) { function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
/* sanity check */ /* sanity check */
if ( empty($elements) || $max_depth < 0 ) if ( empty($elements) || $max_depth < -1 )
return ''; return '';
$args = array_slice( func_get_args(), 4 ); $args = array_slice( func_get_args(), 4 );
@ -956,6 +965,38 @@ class Walker {
$id_field = $this->db_fields['id']; $id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent']; $parent_field = $this->db_fields['parent'];
$count = -1;
if ( -1 == $max_depth )
$total_top = count( $elements );
if ( $page_num < 1 || $per_page < 0 ) {
// No paging
$paging = false;
$start = 0;
if ( -1 == $max_depth )
$end = $total_top;
$this->max_pages = 1;
} else {
$paging = true;
$start = ( (int)$page_num - 1 ) * (int)$per_page;
$end = $start + $per_page;
if ( -1 == $max_depth )
$this->max_pages = ceil($total_top / $per_page);
}
// flat display
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e ) {
$count++;
if ( $count < $start )
continue;
if ( $count >= $end )
break;
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
}
return $output;
}
/* /*
* seperate elements into two buckets: top level and children elements * seperate elements into two buckets: top level and children elements
* children_elements is two dimensional array, eg. * children_elements is two dimensional array, eg.
@ -970,15 +1011,10 @@ class Walker {
$children_elements[ $e->$parent_field ][] = $e; $children_elements[ $e->$parent_field ][] = $e;
} }
$count = -1;
$total_top = count( $top_level_elements ); $total_top = count( $top_level_elements );
if ( $page_num < 1 || $per_page < 0 ) {
$start = 0; if ( $paging )
$end = $total_top; $this->max_pages = ceil($total_top / $per_page);
} else {
$start = ( (int)$page_num - 1 ) * (int)$per_page;
$end = $start + $per_page;
}
foreach( $top_level_elements as $e ){ foreach( $top_level_elements as $e ){
$count++; $count++;
@ -996,7 +1032,7 @@ class Walker {
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
} }
if ( $end >= $total_top && count( $children_elements ) > 0 ){ if ( $end >= $total_top && count( $children_elements ) > 0 ) {
$empty_array = array(); $empty_array = array();
foreach ( $children_elements as $orphans ) foreach ( $children_elements as $orphans )
foreach( $orphans as $op ) foreach( $orphans as $op )
@ -1030,6 +1066,7 @@ class Walker {
foreach ( (array)$children_elements[$id] as $child ) foreach ( (array)$children_elements[$id] as $child )
$this->unset_children( $child, $children_elements ); $this->unset_children( $child, $children_elements );
if ( isset($children_elements[$id]) )
unset( $children_elements[$id] ); unset( $children_elements[$id] );
} }

View File

@ -850,7 +850,7 @@ function comment_reply_link($args = array(), $comment = null, $post = null) {
$args = wp_parse_args($args, $defaults); $args = wp_parse_args($args, $defaults);
if ( 0 == $args['depth'] || $args['max_depth'] < $args['depth'] ) if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )
return; return;
extract($args, EXTR_SKIP); extract($args, EXTR_SKIP);
@ -978,10 +978,29 @@ class Walker_Comment extends Walker {
function wp_list_comments($args = array(), $comments = null ) { function wp_list_comments($args = array(), $comments = null ) {
global $wp_query; global $wp_query;
$defaults = array('walker' => null, 'depth' => 3, 'style' => 'ul', 'callback' => null, 'end-callback' => null, 'type' => 'all'); $defaults = array('walker' => null, 'depth' => '', 'style' => 'ul', 'callback' => null, 'end-callback' => null, 'type' => 'all',
'page' => get_query_var('cpage'), 'per_page' => '');
$r = wp_parse_args( $args, $defaults ); $r = wp_parse_args( $args, $defaults );
if ( '' === $r['per_page'] && get_option('page_comments') )
$r['per_page'] = get_query_var('comments_per_page');
if ( empty($r['per_page']) ) {
$r['page'] = 0;
} else {
$r['page'] = intval($r['page']);
if ( empty($r['page']) )
$r['page'] = 1;
}
if ( '' === $r['depth'] ) {
if ( get_option('thread_comments') )
$r['depth'] = get_option('thread_comments_depth');
else
$r['depth'] = -1;
}
extract( $r, EXTR_SKIP ); extract( $r, EXTR_SKIP );
if ( empty($walker) ) if ( empty($walker) )
@ -995,19 +1014,25 @@ function wp_list_comments($args = array(), $comments = null ) {
$wp_query->comments_by_type = &separate_comments($wp_query->comments); $wp_query->comments_by_type = &separate_comments($wp_query->comments);
if ( empty($wp_query->comments_by_type[$type]) ) if ( empty($wp_query->comments_by_type[$type]) )
return; return;
return $walker->walk($wp_query->comments_by_type[$type], $depth, $r); $walker->paged_walk($wp_query->comments_by_type[$type], $depth, $page, $per_page, $r);
$wp_query->max_num_comment_pages = $walker->max_pages;
return;
} }
$walker->walk($wp_query->comments, $depth, $r); $walker->paged_walk($wp_query->comments, $depth, $page, $per_page, $r);
$wp_query->max_num_comment_pages = $walker->max_pages;
} else { } else {
if ( empty($comments) ) if ( empty($comments) )
return; return;
if ( 'all' != $type ) { if ( 'all' != $type ) {
$comments_by_type = separate_comments($comments); $comments_by_type = &separate_comments($comments);
if ( empty($comments_by_type[$type]) ) if ( empty($comments_by_type[$type]) )
return; return;
return $walker->walk($comments_by_type[$type], $depth, $r); $walker->paged_walk($comments_by_type[$type], $depth, $page, $per_page, $r);
$wp_query->max_num_comment_pages = $walker->max_pages;
return;
} }
$walker->walk($comments, $depth, $r); $walker->paged_walk($comments, $depth, $page, $per_page, $r);
$wp_query->max_num_comment_pages = $walker->max_pages;
} }
} }

View File

@ -1820,6 +1820,9 @@ function sanitize_option($option, $value) {
case 'default_category': case 'default_category':
case 'default_email_category': case 'default_email_category':
case 'default_link_category': case 'default_link_category':
case 'close_comments_days_old':
case 'comments_per_page':
case 'thread_comments_depth':
$value = abs((int) $value); $value = abs((int) $value);
break; break;

View File

@ -793,6 +793,84 @@ function posts_nav_link($sep=' &#8212; ', $prelabel='&laquo; Previous Page', $nx
} }
} }
function get_comments_pagenum_link($pagenum = 1) {
global $wp_rewrite;
$pagenum = (int) $pagenum;
$request = remove_query_arg( 'cpage' );
$home_root = parse_url(get_option('home'));
$home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
$home_root = preg_quote( trailingslashit( $home_root ), '|' );
$request = preg_replace('|^'. $home_root . '|', '', $request);
$request = preg_replace('|^/+|', '', $request);
$base = trailingslashit( get_bloginfo( 'home' ) );
if ( $pagenum > 1 ) {
$result = add_query_arg( 'cpage', $pagenum, $base . $request );
} else {
$result = $base . $request;
}
$result = apply_filters('get_comments_pagenum_link', $result);
return $result;
}
function next_comments_link($label='', $max_page = 0) {
global $wp_query;
if ( !is_singular() )
return;
$page = get_query_var('cpage');
if ( !$page )
$page = 1;
if ( !$page )
$page = 1;
$nextpage = intval($page) + 1;
if ( empty($max_page) )
$max_page = $wp_query->max_num_comment_pages;
if ( $nextpage > $max_page )
return;
if ( empty($label) )
$label = __('&raquo; Newer Comments');
echo '<a href="' . clean_url(get_comments_pagenum_link($nextpage));
$attr = apply_filters( 'next_comments_link_attributes', '' );
echo "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
}
function previous_comments_link($label='') {
global $wp_query;
if ( !is_singular() )
return;
$page = get_query_var('cpage');
if ( $page <= 1 )
return;
$nextpage = intval($page) - 1;
if ( empty($label) )
$label = __('&laquo; Older Comments');
echo '<a href="' . clean_url(get_comments_pagenum_link($nextpage));
$attr = apply_filters( 'previous_comments_link_attributes', '' );
echo "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
}
function get_shortcut_link() { function get_shortcut_link() {
$link = "javascript: $link = "javascript:
var d=document, var d=document,

View File

@ -843,6 +843,15 @@ class WP_Query {
*/ */
var $max_num_pages = 0; var $max_num_pages = 0;
/**
* The amount of comment pages.
*
* @since 2.7.0
* @access public
* @var int
*/
var $max_num_comment_pages = 0;
/** /**
* Set if query is single post. * Set if query is single post.
* *
@ -1612,6 +1621,9 @@ class WP_Query {
else if ( $q['posts_per_page'] == 0 ) else if ( $q['posts_per_page'] == 0 )
$q['posts_per_page'] = 1; $q['posts_per_page'] = 1;
if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
$q['comments_per_page'] = get_option('comments_per_page');
if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) { if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
$this->is_page = true; $this->is_page = true;
$this->is_home = false; $this->is_home = false;