Skip to content

Commit 873a5b0

Browse files
committed
Comment paging and sorting from Viper007Bond. see WordPress#7927
git-svn-id: https://develop.svn.wordpress.org/trunk@9296 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 88e2b9b commit 873a5b0

9 files changed

Lines changed: 131 additions & 21 deletions

File tree

wp-admin/includes/schema.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ function populate_options() {
288288
add_option('image_default_align', '');
289289
add_option('close_comments_for_old_posts', 0);
290290
add_option('close_comments_days_old', 14);
291+
add_option('thread_comments', 0);
292+
add_option('thread_comments_depth', 5);
293+
add_option('page_comments', 1);
294+
add_option('comments_per_page', 10);
295+
add_option('default_comments_page', 'newest');
296+
add_option('comment_order', 'asc');
291297

292298
// Delete unused options
293299
$unusedoptions = array ('blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory', 'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping', 'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers', 'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference', 'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char', 'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1', 'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5', 'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9', 'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat', 'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog', '_wpnonce', '_wp_http_referer', 'Update', 'action', 'rich_editing', 'autosave_interval', 'deactivated_plugins');

wp-admin/options-discussion.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@
4343
<input name="default_comment_status" type="checkbox" id="default_comment_status" value="open" <?php checked('open', get_option('default_comment_status')); ?> />
4444
<?php _e('Allow people to post comments on the article') ?></label>
4545
<br />
46+
<small><em><?php echo '(' . __('These settings may be overridden for individual articles.') . ')'; ?></em></small>
47+
</fieldset></td>
48+
</tr>
49+
<tr valign="top">
50+
<th scope="row"><?php _e('Other comment settings') ?></th>
51+
<td><fieldset><legend class="hidden"><?php _e('Other comment settings') ?></legend>
4652
<label for="require_name_email"><input type="checkbox" name="require_name_email" id="require_name_email" value="1" <?php checked('1', get_option('require_name_email')); ?> /> <?php _e('Comment author must fill out name and e-mail') ?></label>
4753
<br />
4854
<label for="close_comments_for_old_posts">
4955
<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')); ?> />
50-
<?php printf( __('Close comments on articles older than %s days'), '</label><input name="close_comments_days_old" type="text" id="close_comments_days_old" value="' . attribute_escape(get_option('close_comments_days_old')) . '" size="3" />') ?>
56+
<?php printf( __('Automatically close comments on articles older than %s days'), '</label><input name="close_comments_days_old" type="text" id="close_comments_days_old" value="' . attribute_escape(get_option('close_comments_days_old')) . '" size="3" />') ?>
5157
<br />
5258
<label for="thread_comments">
5359
<input name="thread_comments" type="checkbox" id="thread_comments" value="1" <?php checked('1', get_option('thread_comments')); ?> />
@@ -68,9 +74,29 @@
6874
?><br />
6975
<label for="page_comments">
7076
<input name="page_comments" type="checkbox" id="page_comments" value="1" <?php checked('1', get_option('page_comments')); ?> />
71-
<?php printf( __('Break comments into pages with %s comments per page'), '</label><input name="comments_per_page" type="text" id="comments_per_page" value="' . attribute_escape(get_option('comments_per_page')) . '" size="3" />') ?>
77+
<?php
78+
79+
$default_comments_page = '</label><select name="default_comments_page" id="default_comments_page"><option value="newest"';
80+
if ( 'newest' == get_option('default_comments_page') ) $default_comments_page .= ' selected="selected"';
81+
$default_comments_page .= '>' . __('last') . '</option><option value="oldest"';
82+
if ( 'oldest' == get_option('default_comments_page') ) $default_comments_page .= ' selected="selected"';
83+
$default_comments_page .= '>' . __('first') . '</option></select>';
84+
85+
printf( __('Break comments into pages with %1$s comments per page and the %2$s page displayed by default'), '</label><input name="comments_per_page" type="text" id="comments_per_page" value="' . attribute_escape(get_option('comments_per_page')) . '" size="3" />', $default_comments_page );
86+
87+
?>
7288
<br />
73-
<small><em><?php echo '(' . __('These settings may be overridden for individual articles.') . ')'; ?></em></small>
89+
<label for="comment_order"><?php
90+
91+
$comment_order = '<select name="comment_order" id="comment_order"><option value="asc"';
92+
if ( 'asc' == get_option('comment_order') ) $comment_order .= ' selected="selected"';
93+
$comment_order .= '>' . __('older') . '</option><option value="desc"';
94+
if ( 'desc' == get_option('comment_order') ) $comment_order .= ' selected="selected"';
95+
$comment_order .= '>' . __('newer') . '</option></select>';
96+
97+
printf( __('Comments should be displayed with the %s comments at the top of each page'), $comment_order );
98+
99+
?></label>
74100
</fieldset></td>
75101
</tr>
76102
<tr valign="top">

wp-admin/options.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
$whitelist_options = array(
2525
'general' => array('blogname', 'blogdescription', 'admin_email', 'users_can_register', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'comment_registration', 'default_role' ),
26-
'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', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page' ),
26+
'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', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order' ),
2727
'misc' => array( 'hack_file', 'use_linksupdate', 'uploads_use_yearmonth_folders', 'upload_path' ),
2828
'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' ),
2929
'privacy' => array( 'blog_public' ),

wp-comments-post.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@
7979
setcookie('comment_author_url_' . COOKIEHASH, clean_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
8080
}
8181

82-
$location = ( empty($_POST['redirect_to']) ? get_permalink($comment_post_ID) : $_POST['redirect_to'] ) . '#comment-' . $comment_id;
82+
if ( empty($_POST['redirect_to']) )
83+
$postlink = ( 'newest' == get_option('default_comments_page') ) ? get_permalink($comment_post_ID) : add_query_arg( 'cpage', get_comment_pages_count( get_comments( array( 'post_id' => $comment_post_ID ) ) ), get_permalink($comment_post_ID) );
84+
85+
$location = ( empty($_POST['redirect_to']) ? $postlink : $_POST['redirect_to'] ) . '#comment-' . $comment_id;
8386
$location = apply_filters('comment_post_redirect', $location, $comment);
8487

8588
wp_redirect($location);

wp-includes/classes.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,13 @@ function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
985985

986986
// flat display
987987
if ( -1 == $max_depth ) {
988+
if ( !empty($args[0]['reverse_top_level']) ) {
989+
$elements = array_reverse( $elements );
990+
$oldstart = $start;
991+
$start = $total_top - $end;
992+
$end = $total_top - $oldstart;
993+
}
994+
988995
$empty_array = array();
989996
foreach ( $elements as $e ) {
990997
$count++;
@@ -1017,7 +1024,18 @@ function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
10171024
else
10181025
$end = $total_top;
10191026

1020-
foreach( $top_level_elements as $e ){
1027+
if ( !empty($args[0]['reverse_top_level']) ) {
1028+
$top_level_elements = array_reverse( $top_level_elements );
1029+
$oldstart = $start;
1030+
$start = $total_top - $end;
1031+
$end = $total_top - $oldstart;
1032+
}
1033+
if ( !empty($args[0]['reverse_children']) ) {
1034+
foreach ( $children_elements as $parent => $children )
1035+
$children_elements[$parent] = array_reverse( $children );
1036+
}
1037+
1038+
foreach ( $top_level_elements as $e ) {
10211039
$count++;
10221040

10231041
//for the last page, need to unset earlier children in order to keep track of orphans

wp-includes/comment-template.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,9 @@ function comments_template( $file = '/comments.php', $separate_comments = false
762762
$comments_by_type = &$wp_query->comments_by_type;
763763
}
764764

765+
if ( '' == get_query_var('cpage') && get_option('page_comments') && 'newest' == get_option('default_comments_page') )
766+
set_query_var( 'cpage', get_comment_pages_count() );
767+
765768
define('COMMENTS_TEMPLATE', true);
766769

767770
$include = apply_filters('comments_template', STYLESHEETPATH . $file );
@@ -1122,8 +1125,8 @@ function end_el(&$output, $comment, $depth, $args) {
11221125
* @since 2.7.0
11231126
* @uses Walker_Comment
11241127
*
1125-
* @param $args string|array Formatting options
1126-
* @param $comments array Optional array of comment objects. Defaults to $wp_query->comments
1128+
* @param string|array $args Formatting options
1129+
* @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
11271130
*/
11281131
function wp_list_comments($args = array(), $comments = null ) {
11291132
global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt;
@@ -1132,7 +1135,7 @@ function wp_list_comments($args = array(), $comments = null ) {
11321135
$comment_depth = 1;
11331136

11341137
$defaults = array('walker' => null, 'depth' => '', 'style' => 'ul', 'callback' => null, 'end-callback' => null, 'type' => 'all',
1135-
'page' => get_query_var('cpage'), 'per_page' => '', 'avatar_size' => 32);
1138+
'page' => '', 'per_page' => '', 'avatar_size' => 32, 'reverse_top_level' => '', 'reverse_children' => '');
11361139

11371140
$r = wp_parse_args( $args, $defaults );
11381141

@@ -1142,10 +1145,6 @@ function wp_list_comments($args = array(), $comments = null ) {
11421145
if ( empty($r['per_page']) ) {
11431146
$r['per_page'] = 0;
11441147
$r['page'] = 0;
1145-
} else {
1146-
$r['page'] = intval($r['page']);
1147-
if ( empty($r['page']) )
1148-
$r['page'] = 1;
11491148
}
11501149

11511150
if ( '' === $r['depth'] ) {
@@ -1155,6 +1154,23 @@ function wp_list_comments($args = array(), $comments = null ) {
11551154
$r['depth'] = -1;
11561155
}
11571156

1157+
if ( '' === $r['page'] ) {
1158+
if ( empty($comments) ) {
1159+
$r['page'] = get_query_var('cpage');
1160+
} else {
1161+
$threaded = ( -1 == $r['depth'] ) ? false : true;
1162+
$r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($comments, $r['per_page'], $threaded) : 1;
1163+
set_query_var( 'cpage', $r['page'] );
1164+
}
1165+
}
1166+
// Validation check
1167+
$r['page'] = intval($r['page']);
1168+
if ( 0 == $r['page'] && 0 != $r['per_page'] )
1169+
$r['page'] = 1;
1170+
1171+
if ( '' == $r['reverse_top_level'] )
1172+
$r['reverse_top_level'] = ( 'asc' == get_option('comment_order') ) ? FALSE : TRUE;
1173+
11581174
extract( $r, EXTR_SKIP );
11591175

11601176
if ( empty($walker) )

wp-includes/comment.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,45 @@ function &separate_comments(&$comments) {
480480
return $comments_by_type;
481481
}
482482

483+
/**
484+
* Calculate the total number of comment pages.
485+
*
486+
* @since 2.7.0
487+
*
488+
* @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
489+
* @param int $per_page Optional comments per page.
490+
* @param boolean $threaded Optional control over flat or threaded comments.
491+
* @return int Number of comment pages.
492+
*/
493+
function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
494+
global $wp_query;
495+
496+
if ( !$comments || !is_array($comments) )
497+
$comments = $wp_query->comments;
498+
499+
if ( empty($comments) )
500+
return 0;
501+
502+
if ( !isset($per_page) )
503+
$per_page = (int) get_query_var('comments_per_page');
504+
if ( 0 === $per_page )
505+
$per_page = (int) get_option('comments_per_page');
506+
if ( 0 === $per_page )
507+
return 1;
508+
509+
if ( !isset($threaded) )
510+
$threaded = get_option('thread_comments');
511+
512+
if ( $threaded ) {
513+
$walker = new Walker_Comment;
514+
$count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
515+
} else {
516+
$count = ceil( count( $comments ) / $per_page );
517+
}
518+
519+
return $count;
520+
}
521+
483522
/**
484523
* Does comment contain blacklisted characters or words.
485524
*

wp-includes/link-template.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ function posts_nav_link($sep=' &#8212; ', $prelabel='&laquo; Previous Page', $nx
11351135
* @param int $pagenum Optional. Page number.
11361136
* @return string
11371137
*/
1138-
function get_comments_pagenum_link($pagenum = 1) {
1138+
function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
11391139
global $wp_rewrite;
11401140

11411141
$pagenum = (int) $pagenum;
@@ -1151,11 +1151,13 @@ function get_comments_pagenum_link($pagenum = 1) {
11511151

11521152
$base = trailingslashit( get_bloginfo( 'home' ) );
11531153

1154-
if ( $pagenum > 1 ) {
1154+
$result = $base . $request;
1155+
1156+
if ( 'newest' == get_option('default_comments_page') ) {
1157+
if ( $pagenum != $max_page )
1158+
$result = add_query_arg( 'cpage', $pagenum, $base . $request );
1159+
} elseif ( $pagenum > 1 )
11551160
$result = add_query_arg( 'cpage', $pagenum, $base . $request );
1156-
} else {
1157-
$result = $base . $request;
1158-
}
11591161

11601162
$result .= '#comments';
11611163

@@ -1194,7 +1196,7 @@ function next_comments_link($label='', $max_page = 0) {
11941196
if ( empty($label) )
11951197
$label = __('&raquo; Newer Comments');
11961198

1197-
echo '<a href="' . clean_url(get_comments_pagenum_link($nextpage));
1199+
echo '<a href="' . clean_url( get_comments_pagenum_link( $nextpage, $max_page ) );
11981200
$attr = apply_filters( 'next_comments_link_attributes', '' );
11991201
echo "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
12001202
}

wp-includes/version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
*
99
* @global string $wp_version
1010
*/
11-
$wp_version = '2.7-almost-beta';
11+
$wp_version = '2.7-almost-beta-9296';
1212

1313
/**
1414
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
1515
*
1616
* @global int $wp_db_version
1717
*/
18-
$wp_db_version = 9290;
18+
$wp_db_version = 9296;
1919

2020
?>

0 commit comments

Comments
 (0)