diff --git src/wp-includes/comment.php src/wp-includes/comment.php
index dd45821..4d56fd9 100644
|
|
class WP_Comment_Query { |
233 | 233 | } |
234 | 234 | |
235 | 235 | /** |
236 | | * Execute the query |
| 236 | * Query vars set by the user |
237 | 237 | * |
238 | 238 | * @since 3.1.0 |
| 239 | * @access public |
| 240 | * @var array |
| 241 | */ |
| 242 | public $query_vars; |
| 243 | |
| 244 | /** |
| 245 | * List of comments. |
239 | 246 | * |
240 | | * @param string|array $query_vars |
241 | | * @return int|array |
| 247 | * @since 4.0.0 |
| 248 | * @access public |
| 249 | * @var array |
242 | 250 | */ |
243 | | public function query( $query_vars ) { |
244 | | global $wpdb; |
| 251 | public $comments; |
245 | 252 | |
| 253 | /** |
| 254 | * Constructor. |
| 255 | * |
| 256 | * Sets up the comment query, if parameter is not empty. |
| 257 | * |
| 258 | * @since 4.0.0 |
| 259 | * @access public |
| 260 | * |
| 261 | * @param string $query URL query string. |
| 262 | * @return WP_Comment_Query |
| 263 | */ |
| 264 | function __construct($query = '') { |
| 265 | if ( ! empty($query) ) { |
| 266 | $this->query($query); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Parse a query string and set query type booleans. |
| 272 | * |
| 273 | * @since 1.5.0 |
| 274 | * @access public |
| 275 | * |
| 276 | * @param string|array $query Optional query. |
| 277 | */ |
| 278 | function parse_query( $query = '' ) { |
| 279 | if ( empty( $query ) ) { |
| 280 | $query = $this->query_vars; |
| 281 | } |
246 | 282 | $defaults = array( |
247 | 283 | 'author_email' => '', |
248 | 284 | 'ID' => '', |
… |
… |
class WP_Comment_Query { |
270 | 306 | 'date_query' => null, // See WP_Date_Query |
271 | 307 | ); |
272 | 308 | |
| 309 | $this->query_vars = wp_parse_args( $query, $defaults ); |
| 310 | do_action_ref_array('parse_comment_query', array(&$this)); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Sets up the WordPress query by parsing query string. |
| 315 | * |
| 316 | * @since 1.5.0 |
| 317 | * @access public |
| 318 | * |
| 319 | * @param string $query_vars URL query string. |
| 320 | * @return array List of posts. |
| 321 | */ |
| 322 | function query( $query_vars ) { |
| 323 | $this->query_vars = wp_parse_args( $query_vars ); |
| 324 | return $this->get_comments(); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Execute the query |
| 329 | * |
| 330 | * @since 4.0.0 |
| 331 | * |
| 332 | * @return int|array |
| 333 | */ |
| 334 | function get_comments() { |
| 335 | global $wpdb; |
| 336 | |
273 | 337 | $groupby = ''; |
274 | 338 | |
275 | | $this->query_vars = wp_parse_args( $query_vars, $defaults ); |
| 339 | $this->parse_query(); |
276 | 340 | |
277 | 341 | // Parse meta query |
278 | 342 | $this->meta_query = new WP_Meta_Query(); |
… |
… |
class WP_Comment_Query { |
287 | 351 | */ |
288 | 352 | do_action_ref_array( 'pre_get_comments', array( &$this ) ); |
289 | 353 | |
290 | | // $args can be whatever, only use the args defined in defaults to compute the key |
291 | | $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $defaults ) ) ) ); |
| 354 | // $args can be whatever, only use the args defined in the query_vars to compute the key |
| 355 | $key = md5( serialize( $this->query_vars) ); |
292 | 356 | $last_changed = wp_cache_get( 'last_changed', 'comment' ); |
293 | 357 | if ( ! $last_changed ) { |
294 | 358 | $last_changed = microtime(); |
… |
… |
class WP_Comment_Query { |
297 | 361 | $cache_key = "get_comments:$key:$last_changed"; |
298 | 362 | |
299 | 363 | if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { |
| 364 | $this->comments = $cache; |
300 | 365 | return $cache; |
301 | 366 | } |
302 | 367 | |
… |
… |
class WP_Comment_Query { |
472 | 537 | $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) ); |
473 | 538 | |
474 | 539 | wp_cache_add( $cache_key, $comments, 'comment' ); |
| 540 | $this->comments = $comments; |
475 | 541 | |
476 | 542 | return $comments; |
477 | 543 | } |
diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
index 2dc3623..0c1084d 100644
|
|
class Tests_Comment_Query extends WP_UnitTestCase { |
180 | 180 | $this->assertEquals( $users[1], $comments[2]->user_id ); |
181 | 181 | |
182 | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Ticket 24826 |
| 186 | */ |
| 187 | function test_comment_query_object() { |
| 188 | $comment_id = $this->factory->comment->create(); |
| 189 | |
| 190 | $query1 = new WP_Comment_Query(); |
| 191 | $this->assertNull( $query1->query_vars ); |
| 192 | $this->assertEmpty( $query1->comments ); |
| 193 | $comments = $query1->query( array( 'status' => 'all' ) ); |
| 194 | $this->assertInternalType( 'array', $query1->query_vars ); |
| 195 | $this->assertNotEmpty( $query1->comments ); |
| 196 | $this->assertInternalType( 'array', $query1->comments ); |
| 197 | |
| 198 | $query2 = new WP_Comment_Query( array( 'status' => 'all' ) ); |
| 199 | $this->assertNotEmpty( $query2->query_vars ); |
| 200 | $this->assertNotEmpty( $query2->comments ); |
| 201 | $this->assertEquals( $query2->comments, $query1->get_comments() ); |
| 202 | } |
183 | 203 | } |