WordPress.org

Make WordPress Core

Ticket #14369: comment_sorting.diff

File comment_sorting.diff, 4.8 KB (added by mhitza, 8 years ago)

Patch in order to enable comment sorting

  • wp-admin/edit-comments.php

     
    126126        $comment_status = 'all';
    127127
    128128$comment_type = !empty($_GET['comment_type']) ? esc_attr($_GET['comment_type']) : '';
    129 
     129$sort_by    = isset($_GET['sort_by']) ? $_GET['sort_by'] : 'comment_date_gmt';
     130$sort_order = isset($_GET['sort_order']) ? $_GET['sort_order'] : 'ASC';
    130131$search_dirty = ( isset($_GET['s']) ) ? $_GET['s'] : '';
    131132$search = esc_attr( $search_dirty ); ?>
    132133
     
    271272
    272273$start = $offset = ( $page - 1 ) * $comments_per_page;
    273274
    274 list($_comments, $total) = _wp_get_comment_list( $comment_status, $search_dirty, $start, $comments_per_page + 8, $post_id, $comment_type ); // Grab a few extra
     275list($_comments, $total) = _wp_get_comment_list( $comment_status, $search_dirty, $start, $comments_per_page + 8, $post_id, $comment_type, $sort_by, $sort_order ); // Grab a few extra
    275276
    276277$_comment_post_ids = array();
    277278foreach ( $_comments as $_c ) {
     
    361362</select>
    362363<input type="submit" id="post-query-submit" value="<?php esc_attr_e('Filter'); ?>" class="button-secondary" />
    363364
     365<select name="sort_by">
     366        <option value="all"><?php _e('Sort comments by'); ?></option>
     367<?php
     368        $comment_sort_by = apply_filters( 'admin_comment_sort_by', array(
     369                'comment_post_id'      => __('Post'),
     370                'comment_author'       => __('Author'),
     371                'comment_author_email' => __('eMail'),
     372                'comment_author_url'   => __('URL'),
     373                'comment_author_IP'    => __('IP Address'),
     374                'comment_date_gmt'     => __('Date'),
     375                'comment_content'      => __('Content'),
     376                'comment_karma'        => __('Karma')
     377        ) );
     378
     379        foreach ( $comment_sort_by as $field_sort => $label ) {
     380                var_dump($comment_field_sort);
     381                echo "  <option value='" . esc_attr($field_sort) . "'";
     382                selected( $sort_by, $field_sort );
     383                echo ">$label</option>\n";
     384        }
     385?>
     386</select>
     387<select name="sort_order">
     388        <option value="all"><?php _e('Sorting order'); ?></option>
     389<?php
     390        $comment_sort_order = apply_filters( 'admin_comment_sort_order', array(
     391                'ASC'  => __('Ascending'),
     392                'DESC' => __('Descending'),
     393        ) );
     394
     395        foreach ( $comment_sort_order as $type => $label ) {
     396                echo "  <option value='" . esc_attr($type) . "'";
     397                selected( $sort_order, $type );
     398                echo ">$label</option>\n";
     399        }
     400?>
     401</select>
     402<input type="submit" id="post-query-submit" value="<?php esc_attr_e('Sort'); ?>" class="button-secondary" />
     403
    364404<?php if ( isset($_GET['apage']) ) { ?>
    365405        <input type="hidden" name="apage" value="<?php echo esc_attr( absint( $_GET['apage'] ) ); ?>" />
    366406<?php }
  • wp-admin/includes/template.php

     
    19121912 * @param int $num Maximum number of comments to return
    19131913 * @param int $post Post ID or 0 to return all comments
    19141914 * @param string $type Comment type (comment, trackback, pingback, etc)
     1915 * @param string $sort_by Column by which to execute sorting for the returned comments
     1916 * @param string $sort_order Comment sort order (ASC, DESC)
    19151917 * @return array [0] contains the comments and [1] contains the total number of comments that match (ignoring $start and $num)
    19161918 */
    1917 function _wp_get_comment_list( $status = '', $s = false, $start, $num, $post = 0, $type = '' ) {
     1919function _wp_get_comment_list( $status = '', $s = false, $start, $num, $post = 0, $type = '', $sort_by = null, $sort_order = null ) {
    19181920        global $wpdb;
    19191921
    19201922        $start = abs( (int) $start );
     
    19481950                $post = '';
    19491951        }
    19501952
    1951         $orderby = "ORDER BY c.comment_date_gmt DESC LIMIT $start, $num";
     1953               
     1954        // Comment sorting field
     1955        if( !is_null( $sort_by ) ) {
     1956                $sort_by = strtolower($sort_by);
     1957                $sortableColumns = array(
     1958                        'comment_post_id',
     1959                        'comment_author',
     1960                        'comment_author_email',
     1961                        'comment_author_url',
     1962                        'comment_author_IP',
     1963                        'comment_date_gmt',
     1964                        'comment_content',
     1965                        'comment_karma'
     1966                );
     1967                if( !in_array( $sort_by, $sortableColumns ) ) {
     1968                        $sort_by = "comment_date_gmt";
     1969                }
     1970        } else {
     1971                $sort_by = "comment_date_gmt";
     1972        }
     1973        // Comment sorting direction
     1974        if( !is_null( $sort_order ) ) {
     1975                $sort_order = strtoupper($sort_order);
     1976                if( !in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) {
     1977                        $sort_order = "ASC";
     1978                }
     1979        } else {
     1980                $sort_order = "ASC";
     1981        }
     1982       
     1983        $orderby = "ORDER BY c.$sort_by $sort_order";
     1984       
     1985        $limit = "LIMIT $start, $num";
    19521986
    19531987        if ( 'comment' == $type )
    19541988                $typesql = "AND c.comment_type = ''";
     
    19802014                $query .= "AND $approved $post $typesql";
    19812015        }
    19822016
    1983         $comments = $wpdb->get_results("SELECT * $query $orderby");
     2017        $comments = $wpdb->get_results("SELECT * $query $orderby $limit");
    19842018        if ( '' === $total )
    19852019                $total = $wpdb->get_var("SELECT COUNT(c.comment_ID) $query");
    19862020