Make WordPress Core

Changeset 12518


Ignore:
Timestamp:
12/23/2009 04:16:13 PM (17 years ago)
Author:
ryan
Message:

Add more orderby and select options to get_comments. Props filosofo. fixes #10668

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment.php

    r12470 r12518  
    186186        global $wpdb;
    187187
    188         $defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);
     188        $defaults = array(
     189                'author_email' => '',
     190                'ID' => '',
     191                'karma' => '',
     192                'number' => '',
     193                'offset' => '',
     194                'orderby' => '',
     195                'order' => 'DESC',
     196                'parent' => '',
     197                'post_ID' => '',
     198                'post_id' => 0,
     199                'status' => '',
     200                'type' => '',
     201                'user_id' => '',
     202        );
    189203
    190204        $args = wp_parse_args( $args, $defaults );
     
    219233        $order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
    220234
    221         $orderby = 'comment_date_gmt';  // Hard code for now
     235        if ( ! empty( $orderby ) ) {
     236                $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
     237                $ordersby = array_intersect(
     238                        $ordersby,
     239                        array(
     240                                'comment_agent',
     241                                'comment_approved',
     242                                'comment_author',
     243                                'comment_author_email',
     244                                'comment_author_IP',
     245                                'comment_author_url',
     246                                'comment_content',
     247                                'comment_date',
     248                                'comment_date_gmt',
     249                                'comment_ID',
     250                                'comment_karma',
     251                                'comment_parent',
     252                                'comment_post_ID',
     253                                'comment_type',
     254                                'user_id',
     255                        )
     256                );
     257                $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
     258        } else {
     259                $orderby = 'comment_date_gmt';
     260        }
    222261
    223262        $number = absint($number);
     
    234273        }
    235274
     275        $post_where = '';
     276
    236277        if ( ! empty($post_id) )
    237                 $post_where = $wpdb->prepare( 'comment_post_ID = %d AND', $post_id );
    238         else
    239                 $post_where = '';
     278                $post_where .= $wpdb->prepare( 'comment_post_ID = %d AND ', $post_id );
     279        if ( '' !== $author_email )
     280                $post_where .= $wpdb->prepare( 'comment_author_email = %s AND ', $author_email );
     281        if ( '' !== $karma )
     282                $post_where .= $wpdb->prepare( 'comment_karma = %d AND ', $karma );
     283        if ( 'comment' == $type )
     284                $post_where .= "comment_type = '' AND ";
     285        elseif ( ! empty( $type ) )
     286                $post_where .= $wpdb->prepare( 'comment_type = %s AND ', $type );
     287        if ( '' !== $parent )
     288                $post_where .= $wpdb->prepare( 'comment_parent = %d AND ', $parent );
     289        if ( '' !== $user_id )
     290                $post_where .= $wpdb->prepare( 'user_id = %d AND ', $user_id );
    240291
    241292        $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
Note: See TracChangeset for help on using the changeset viewer.