Ticket #32619: get_comment.diff
File get_comment.diff, 5.9 KB (added by , 10 years ago) |
---|
-
src/wp-includes/comment-template.php
1102 1102 /** @todo Use API instead of SELECTs. */ 1103 1103 if ( $user_ID) { 1104 1104 $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 ); 1105 1106 } else if ( empty($comment_author) ) { 1106 1107 $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') ); 1107 1108 } else { 1108 1109 $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 ); 1109 1111 } 1110 1112 1111 1113 /** -
src/wp-includes/comment.php
114 114 * @param int $post_id The ID of the post 115 115 * @return array $comments The approved comments 116 116 */ 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)); 117 function get_approved_comments( $post_id ) { 118 return get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) ); 120 119 } 121 120 122 121 /** … … 133 132 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants. 134 133 * @return object|array|null Depends on $output value. 135 134 */ 136 function get_comment(&$comment, $output = OBJECT) { 137 global $wpdb; 135 function get_comment( $comment = null, $output = OBJECT ) { 136 if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) { 137 $comment = $GLOBALS['comment']; 138 } 138 139 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' ) ) { 146 141 $_comment = $comment; 142 } elseif ( is_object( $comment ) ) { 143 $_comment = new WP_Comment( $comment ); 147 144 } 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 ); 156 146 } 157 147 148 if ( ! $_comment ) { 149 return null; 150 } 151 158 152 /** 159 153 * Fires after a comment is retrieved. 160 154 * … … 164 158 */ 165 159 $_comment = apply_filters( 'get_comment', $_comment ); 166 160 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(); 172 163 } 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() ); 177 165 } 166 167 return $_comment; 178 168 } 179 169 170 180 171 /** 172 * WordPress Comment class. 173 * 174 * @since 4.0.0 175 * 176 */ 177 final 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 /** 181 214 * Retrieve a list of comments. 182 215 * 183 216 * The comment list can be for the blog as a whole or for an individual post. … … 455 488 return $wpdb->get_var( $query ); 456 489 } 457 490 $results = $wpdb->get_results( $query ); 491 458 492 /** 459 493 * Filter the comment query results. 460 494 * … … 465 499 */ 466 500 $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) ); 467 501 502 // Convert to WP_Comment instances 503 $comments = array_map( 'get_comment', $comments ); 504 468 505 wp_cache_add( $cache_key, $comments, 'comment' ); 469 506 470 507 return $comments; -
src/wp-includes/query.php
2971 2971 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 2972 2972 2973 2973 $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 2974 2975 $this->comment_count = count($this->comments); 2975 2976 2976 2977 $post_ids = array(); … … 3327 3328 3328 3329 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits"; 3329 3330 $this->comments = $wpdb->get_results($comments_request); 3331 $this->comments = array_map( 'get_comment', $this->comments ); // Convert to WP_Comment 3330 3332 $this->comment_count = count($this->comments); 3331 3333 } 3332 3334