From 91841f2e3ca0f94151fa5661c4ab77f838d8e8df Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 23 Oct 2008 16:08:47 +0000 Subject: [PATCH] Comment paging and sorting from Viper007Bond. see #7927 git-svn-id: http://svn.automattic.com/wordpress/trunk@9296 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/schema.php | 6 +++++ wp-admin/options-discussion.php | 32 +++++++++++++++++++++++--- wp-admin/options.php | 2 +- wp-comments-post.php | 5 +++- wp-includes/classes.php | 20 +++++++++++++++- wp-includes/comment-template.php | 30 ++++++++++++++++++------ wp-includes/comment.php | 39 ++++++++++++++++++++++++++++++++ wp-includes/link-template.php | 14 +++++++----- wp-includes/version.php | 4 ++-- 9 files changed, 131 insertions(+), 21 deletions(-) diff --git a/wp-admin/includes/schema.php b/wp-admin/includes/schema.php index dcb0b8ff7..bac94107c 100644 --- a/wp-admin/includes/schema.php +++ b/wp-admin/includes/schema.php @@ -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'); diff --git a/wp-admin/options-discussion.php b/wp-admin/options-discussion.php index 2b97a1ffa..8ab6883d7 100644 --- a/wp-admin/options-discussion.php +++ b/wp-admin/options-discussion.php @@ -43,11 +43,17 @@ include('admin-header.php'); />
+ + + + + +

diff --git a/wp-admin/options.php b/wp-admin/options.php index 62eeb3dd1..10a5bac4c 100644 --- a/wp-admin/options.php +++ b/wp-admin/options.php @@ -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' ), diff --git a/wp-comments-post.php b/wp-comments-post.php index 3bcbe388a..d026a6db1 100644 --- a/wp-comments-post.php +++ b/wp-comments-post.php @@ -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); diff --git a/wp-includes/classes.php b/wp-includes/classes.php index d7636e3c1..7e717fcd0 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -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 diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index 045f78790..0e74a93e8 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -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) ) diff --git a/wp-includes/comment.php b/wp-includes/comment.php index f9b0db92c..d76fd7dff 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -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. * diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index c527ae2d0..9894db123 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -1135,7 +1135,7 @@ function posts_nav_link($sep=' — ', $prelabel='« 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 = __('» Newer Comments'); - echo '". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .''; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 90ae81b74..f732bf96c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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; ?>