Make WordPress Core

Ticket #32619: get_comment.diff

File get_comment.diff, 5.9 KB (added by nacin, 10 years ago)

The patch I haven't looked at in a year.

  • src/wp-includes/comment-template.php

     
    11021102        /** @todo Use API instead of SELECTs. */
    11031103        if ( $user_ID) {
    11041104                $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
     1105                $comments = array_map( 'get_comment', $comments );
    11051106        } else if ( empty($comment_author) ) {
    11061107                $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
    11071108        } else {
    11081109                $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));
     1110                $comments = array_map( 'get_comment', $comments );
    11091111        }
    11101112
    11111113        /**
  • src/wp-includes/comment.php

     
    114114 * @param int $post_id The ID of the post
    115115 * @return array $comments The approved comments
    116116 */
    117 function get_approved_comments($post_id) {
    118         global $wpdb;
    119         return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
     117function get_approved_comments( $post_id ) {
     118        return get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
    120119}
    121120
    122121/**
     
    133132 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
    134133 * @return object|array|null Depends on $output value.
    135134 */
    136 function get_comment(&$comment, $output = OBJECT) {
    137         global $wpdb;
     135function get_comment( $comment = null, $output = OBJECT ) {
     136        if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
     137                $comment = $GLOBALS['comment'];
     138        }
    138139
    139         if ( empty($comment) ) {
    140                 if ( isset($GLOBALS['comment']) )
    141                         $_comment = & $GLOBALS['comment'];
    142                 else
    143                         $_comment = null;
    144         } elseif ( is_object($comment) ) {
    145                 wp_cache_add($comment->comment_ID, $comment, 'comment');
     140        if ( is_a( $comment, 'WP_Comment' ) ) {
    146141                $_comment = $comment;
     142        } elseif ( is_object( $comment ) ) {
     143                $_comment = new WP_Comment( $comment );
    147144        } else {
    148                 if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
    149                         $_comment = & $GLOBALS['comment'];
    150                 } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
    151                         $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
    152                         if ( ! $_comment )
    153                                 return null;
    154                         wp_cache_add($_comment->comment_ID, $_comment, 'comment');
    155                 }
     145                $_comment = WP_Comment::get_instance( $comment );
    156146        }
    157147
     148        if ( ! $_comment ) {
     149                return null;
     150        }
     151
    158152        /**
    159153         * Fires after a comment is retrieved.
    160154         *
     
    164158         */
    165159        $_comment = apply_filters( 'get_comment', $_comment );
    166160
    167         if ( $output == OBJECT ) {
    168                 return $_comment;
    169         } elseif ( $output == ARRAY_A ) {
    170                 $__comment = get_object_vars($_comment);
    171                 return $__comment;
     161        if ( $output == ARRAY_A ) {
     162                return $_comment->to_array();
    172163        } elseif ( $output == ARRAY_N ) {
    173                 $__comment = array_values(get_object_vars($_comment));
    174                 return $__comment;
    175         } else {
    176                 return $_comment;
     164                return array_values( $_comment->to_array() );
    177165        }
     166
     167        return $_comment;
    178168}
    179169
     170
    180171/**
     172 * WordPress Comment class.
     173 *
     174 * @since 4.0.0
     175 *
     176 */
     177final class WP_Comment {
     178
     179        public static function get_instance( $id ) {
     180                global $wpdb;
     181
     182                $id = (int) $id;
     183                if ( ! $id ) {
     184                        return false;
     185                }
     186
     187                $_comment = wp_cache_get( $id, 'comment' );
     188
     189                if ( ! $_comment ) {
     190                        $_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $id ) );
     191
     192                        if ( ! $_comment ) {
     193                                return false;
     194                        }
     195
     196                        wp_cache_add( $_comment->comment_ID, $_comment, 'comment' );
     197                }
     198
     199                return new WP_Comment( $_comment );
     200        }
     201
     202        public function __construct( $comment ) {
     203                foreach ( get_object_vars( $comment ) as $key => $value )
     204                        $this->$key = $value;
     205        }
     206
     207        public function to_array() {
     208                $comment = get_object_vars( $this );
     209                return $comment;
     210        }
     211}
     212
     213/**
    181214 * Retrieve a list of comments.
    182215 *
    183216 * The comment list can be for the blog as a whole or for an individual post.
     
    455488                        return $wpdb->get_var( $query );
    456489                }
    457490                $results = $wpdb->get_results( $query );
     491
    458492                /**
    459493                 * Filter the comment query results.
    460494                 *
     
    465499                 */
    466500                $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
    467501
     502                // Convert to WP_Comment instances
     503                $comments = array_map( 'get_comment', $comments );
     504
    468505                wp_cache_add( $cache_key, $comments, 'comment' );
    469506
    470507                return $comments;
  • src/wp-includes/query.php

     
    29712971                        $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
    29722972
    29732973                        $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
     2974                        $this->comments = array_map( 'get_comment', $this->comments ); // Convert to WP_Comment
    29742975                        $this->comment_count = count($this->comments);
    29752976
    29762977                        $post_ids = array();
     
    33273328
    33283329                        $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
    33293330                        $this->comments = $wpdb->get_results($comments_request);
     3331                        $this->comments = array_map( 'get_comment', $this->comments ); // Convert to WP_Comment
    33303332                        $this->comment_count = count($this->comments);
    33313333                }
    33323334