Comment paging and sorting from Viper007Bond. see #7927

git-svn-id: https://develop.svn.wordpress.org/trunk@9296 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2008-10-23 16:08:47 +00:00
parent 88e2b9b5c3
commit 873a5b075b
9 changed files with 131 additions and 21 deletions

View File

@ -288,6 +288,12 @@ function populate_options() {
add_option('image_default_align', '');
add_option('close_comments_for_old_posts', 0);
add_option('close_comments_days_old', 14);
add_option('thread_comments', 0);
add_option('thread_comments_depth', 5);
add_option('page_comments', 1);
add_option('comments_per_page', 10);
add_option('default_comments_page', 'newest');
add_option('comment_order', 'asc');
// Delete unused options
$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');

View File

@ -43,11 +43,17 @@ include('admin-header.php');
<input name="default_comment_status" type="checkbox" id="default_comment_status" value="open" <?php checked('open', get_option('default_comment_status')); ?> />
<?php _e('Allow people to post comments on the article') ?></label>
<br />
<small><em><?php echo '(' . __('These settings may be overridden for individual articles.') . ')'; ?></em></small>
</fieldset></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Other comment settings') ?></th>
<td><fieldset><legend class="hidden"><?php _e('Other comment settings') ?></legend>
<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>
<br />
<label for="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 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" />') ?>
<?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" />') ?>
<br />
<label for="thread_comments">
<input name="thread_comments" type="checkbox" id="thread_comments" value="1" <?php checked('1', get_option('thread_comments')); ?> />
@ -68,9 +74,29 @@ printf( __('Enable threaded (nested) comments %s levels deep'), $thread_comments
?><br />
<label for="page_comments">
<input name="page_comments" type="checkbox" id="page_comments" value="1" <?php checked('1', get_option('page_comments')); ?> />
<?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" />') ?>
<?php
$default_comments_page = '</label><select name="default_comments_page" id="default_comments_page"><option value="newest"';
if ( 'newest' == get_option('default_comments_page') ) $default_comments_page .= ' selected="selected"';
$default_comments_page .= '>' . __('last') . '</option><option value="oldest"';
if ( 'oldest' == get_option('default_comments_page') ) $default_comments_page .= ' selected="selected"';
$default_comments_page .= '>' . __('first') . '</option></select>';
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 );
?>
<br />
<small><em><?php echo '(' . __('These settings may be overridden for individual articles.') . ')'; ?></em></small>
<label for="comment_order"><?php
$comment_order = '<select name="comment_order" id="comment_order"><option value="asc"';
if ( 'asc' == get_option('comment_order') ) $comment_order .= ' selected="selected"';
$comment_order .= '>' . __('older') . '</option><option value="desc"';
if ( 'desc' == get_option('comment_order') ) $comment_order .= ' selected="selected"';
$comment_order .= '>' . __('newer') . '</option></select>';
printf( __('Comments should be displayed with the %s comments at the top of each page'), $comment_order );
?></label>
</fieldset></td>
</tr>
<tr valign="top">

View File

@ -23,7 +23,7 @@ wp_reset_vars(array('action'));
$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' ),
'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' ),
'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' ),
'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' ),
'privacy' => array( 'blog_public' ),

View File

@ -79,7 +79,10 @@ if ( !$user->ID ) {
setcookie('comment_author_url_' . COOKIEHASH, clean_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
}
$location = ( empty($_POST['redirect_to']) ? get_permalink($comment_post_ID) : $_POST['redirect_to'] ) . '#comment-' . $comment_id;
if ( empty($_POST['redirect_to']) )
$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) );
$location = ( empty($_POST['redirect_to']) ? $postlink : $_POST['redirect_to'] ) . '#comment-' . $comment_id;
$location = apply_filters('comment_post_redirect', $location, $comment);
wp_redirect($location);

View File

@ -985,6 +985,13 @@ class Walker {
// flat display
if ( -1 == $max_depth ) {
if ( !empty($args[0]['reverse_top_level']) ) {
$elements = array_reverse( $elements );
$oldstart = $start;
$start = $total_top - $end;
$end = $total_top - $oldstart;
}
$empty_array = array();
foreach ( $elements as $e ) {
$count++;
@ -1017,7 +1024,18 @@ class Walker {
else
$end = $total_top;
foreach( $top_level_elements as $e ){
if ( !empty($args[0]['reverse_top_level']) ) {
$top_level_elements = array_reverse( $top_level_elements );
$oldstart = $start;
$start = $total_top - $end;
$end = $total_top - $oldstart;
}
if ( !empty($args[0]['reverse_children']) ) {
foreach ( $children_elements as $parent => $children )
$children_elements[$parent] = array_reverse( $children );
}
foreach ( $top_level_elements as $e ) {
$count++;
//for the last page, need to unset earlier children in order to keep track of orphans

View File

@ -762,6 +762,9 @@ function comments_template( $file = '/comments.php', $separate_comments = false
$comments_by_type = &$wp_query->comments_by_type;
}
if ( '' == get_query_var('cpage') && get_option('page_comments') && 'newest' == get_option('default_comments_page') )
set_query_var( 'cpage', get_comment_pages_count() );
define('COMMENTS_TEMPLATE', true);
$include = apply_filters('comments_template', STYLESHEETPATH . $file );
@ -1122,8 +1125,8 @@ class Walker_Comment extends Walker {
* @since 2.7.0
* @uses Walker_Comment
*
* @param $args string|array Formatting options
* @param $comments array Optional array of comment objects. Defaults to $wp_query->comments
* @param string|array $args Formatting options
* @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
*/
function wp_list_comments($args = array(), $comments = null ) {
global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt;
@ -1132,7 +1135,7 @@ function wp_list_comments($args = array(), $comments = null ) {
$comment_depth = 1;
$defaults = array('walker' => null, 'depth' => '', 'style' => 'ul', 'callback' => null, 'end-callback' => null, 'type' => 'all',
'page' => get_query_var('cpage'), 'per_page' => '', 'avatar_size' => 32);
'page' => '', 'per_page' => '', 'avatar_size' => 32, 'reverse_top_level' => '', 'reverse_children' => '');
$r = wp_parse_args( $args, $defaults );
@ -1142,10 +1145,6 @@ function wp_list_comments($args = array(), $comments = null ) {
if ( empty($r['per_page']) ) {
$r['per_page'] = 0;
$r['page'] = 0;
} else {
$r['page'] = intval($r['page']);
if ( empty($r['page']) )
$r['page'] = 1;
}
if ( '' === $r['depth'] ) {
@ -1155,6 +1154,23 @@ function wp_list_comments($args = array(), $comments = null ) {
$r['depth'] = -1;
}
if ( '' === $r['page'] ) {
if ( empty($comments) ) {
$r['page'] = get_query_var('cpage');
} else {
$threaded = ( -1 == $r['depth'] ) ? false : true;
$r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($comments, $r['per_page'], $threaded) : 1;
set_query_var( 'cpage', $r['page'] );
}
}
// Validation check
$r['page'] = intval($r['page']);
if ( 0 == $r['page'] && 0 != $r['per_page'] )
$r['page'] = 1;
if ( '' == $r['reverse_top_level'] )
$r['reverse_top_level'] = ( 'asc' == get_option('comment_order') ) ? FALSE : TRUE;
extract( $r, EXTR_SKIP );
if ( empty($walker) )

View File

@ -480,6 +480,45 @@ function &separate_comments(&$comments) {
return $comments_by_type;
}
/**
* Calculate the total number of comment pages.
*
* @since 2.7.0
*
* @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 ( !$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;
}
/**
* Does comment contain blacklisted characters or words.
*

View File

@ -1135,7 +1135,7 @@ function posts_nav_link($sep=' &#8212; ', $prelabel='&laquo; Previous Page', $nx
* @param int $pagenum Optional. Page number.
* @return string
*/
function get_comments_pagenum_link($pagenum = 1) {
function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
global $wp_rewrite;
$pagenum = (int) $pagenum;
@ -1151,11 +1151,13 @@ function get_comments_pagenum_link($pagenum = 1) {
$base = trailingslashit( get_bloginfo( 'home' ) );
if ( $pagenum > 1 ) {
$result = $base . $request;
if ( 'newest' == get_option('default_comments_page') ) {
if ( $pagenum != $max_page )
$result = add_query_arg( 'cpage', $pagenum, $base . $request );
} elseif ( $pagenum > 1 )
$result = add_query_arg( 'cpage', $pagenum, $base . $request );
} else {
$result = $base . $request;
}
$result .= '#comments';
@ -1194,7 +1196,7 @@ function next_comments_link($label='', $max_page = 0) {
if ( empty($label) )
$label = __('&raquo; Newer Comments');
echo '<a href="' . clean_url(get_comments_pagenum_link($nextpage));
echo '<a href="' . clean_url( get_comments_pagenum_link( $nextpage, $max_page ) );
$attr = apply_filters( 'next_comments_link_attributes', '' );
echo "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
}

View File

@ -8,13 +8,13 @@
*
* @global string $wp_version
*/
$wp_version = '2.7-almost-beta';
$wp_version = '2.7-almost-beta-9296';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
*
* @global int $wp_db_version
*/
$wp_db_version = 9290;
$wp_db_version = 9296;
?>