Ticket #2894: 2894.diff
File 2894.diff, 11.1 KB (added by , 9 years ago) |
---|
-
src/includes/common/functions.php
1647 1647 } 1648 1648 1649 1649 /** 1650 * Query the DB and get athe child id's of all children1650 * Query the DB and get the child id's of all children 1651 1651 * 1652 1652 * @since 2.0.0 bbPress (r3325) 1653 * @since 2.6.0 bbPress (rXXXX) Replace direct queries with WP_Query() objects 1653 1654 * 1654 * @param int $ parent_id Parent id 1655 * @param string $post_type Post type. Defaults to 'post' 1656 * @uses wp_cache_get() To check if there is a cache of the children 1655 * @param int $parent_id Parent id. 1656 * @param string $post_type Post type. Defaults to 'post'. 1657 1657 * @uses bbp_get_public_status_id() To get the public status id 1658 1658 * @uses bbp_get_private_status_id() To get the private status id 1659 1659 * @uses bbp_get_hidden_status_id() To get the hidden status id … … 1664 1664 * @uses bbp_get_forum_post_type() To get the forum post type 1665 1665 * @uses bbp_get_topic_post_type() To get the topic post type 1666 1666 * @uses bbp_get_reply_post_type() To get the reply post type 1667 * @uses wpdb::prepare() To prepare the query 1668 * @uses wpdb::get_col() To get the result of the query in an array 1669 * @uses wp_cache_set() To set the cache for future use 1667 * @uses WP_Query To get get the posts 1670 1668 * @uses apply_filters() Calls 'bbp_get_all_child_ids' with the child ids, 1671 1669 * parent id and post type 1672 1670 * @return array The array of children … … 1673 1671 */ 1674 1672 function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) { 1675 1673 1676 // Bail if nothing passed 1674 // Bail if nothing passed. 1677 1675 if ( empty( $parent_id ) ) { 1678 1676 return false; 1679 1677 } 1680 1678 1681 // The ID of the cached query1682 $ cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';1679 // Get the public post status. 1680 $post_status = array( bbp_get_public_status_id() ); 1683 1681 1684 // Check for cache and set if needed 1685 $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' ); 1686 if ( false === $child_ids ) { 1687 $post_status = array( bbp_get_public_status_id() ); 1682 // Extra post statuses based on post type. 1683 switch ( $post_type ) { 1688 1684 1689 // Extra post statuses based on post type 1690 switch ( $post_type ) { 1685 // Forum 1686 case bbp_get_forum_post_type() : 1687 $post_status[] = bbp_get_private_status_id(); 1688 $post_status[] = bbp_get_hidden_status_id(); 1689 break; 1691 1690 1692 // Forum 1693 case bbp_get_forum_post_type() : 1694 $post_status[] = bbp_get_private_status_id(); 1695 $post_status[] = bbp_get_hidden_status_id(); 1696 break; 1691 // Topic 1692 case bbp_get_topic_post_type() : 1693 $post_status[] = bbp_get_pending_status_id(); 1694 $post_status[] = bbp_get_closed_status_id(); 1695 $post_status[] = bbp_get_trash_status_id(); 1696 $post_status[] = bbp_get_spam_status_id(); 1697 break; 1697 1698 1698 // Topic1699 case bbp_get_topic_post_type() :1700 1701 $post_status[] = bbp_get_closed_status_id();1702 $post_status[] = bbp_get_trash_status_id();1703 $post_status[] = bbp_get_spam_status_id();1704 break;1699 // Reply 1700 case bbp_get_reply_post_type() : 1701 $post_status[] = bbp_get_pending_status_id(); 1702 $post_status[] = bbp_get_trash_status_id(); 1703 $post_status[] = bbp_get_spam_status_id(); 1704 break; 1705 } 1705 1706 1706 // Reply 1707 case bbp_get_reply_post_type() : 1708 $post_status[] = bbp_get_pending_status_id(); 1709 $post_status[] = bbp_get_trash_status_id(); 1710 $post_status[] = bbp_get_spam_status_id(); 1711 break; 1712 } 1707 $query = new WP_Query( array( 1708 'fields' => 'ids', 1709 'post_parent' => $parent_id, 1710 'post_status' => 'any', 1711 'post_type' => $post_type, 1713 1712 1714 // Join post statuses together 1715 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1716 $bbp_db = bbp_db(); 1717 $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ); 1718 $child_ids = (array) $bbp_db->get_col( $query ); 1713 // Maybe change these later 1714 'posts_per_page' => -1, 1715 'update_post_term_cache' => false, 1716 'update_post_meta_cache' => false, 1717 'ignore_sticky_posts' => true, 1718 ) ); 1719 $child_ids = ! empty( $query->posts ) ? $query->posts : array(); 1720 unset( $query ); 1719 1721 1720 wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );1721 } else {1722 $child_ids = (array) $child_ids;1723 }1724 1725 1722 // Filter and return 1726 1723 return (array) apply_filters( 'bbp_get_all_child_ids', $child_ids, $parent_id, $post_type ); 1727 1724 } -
src/includes/core/cache.php
161 161 162 162 do_action( 'bbp_clean_post_cache', $_post->ID, $_post ); 163 163 164 // Loop through query types and clean caches165 foreach ( $post_types as $post_type ) {166 wp_cache_delete( 'bbp_parent_all_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress_posts' );167 }168 169 164 // Invalidate parent caches 170 165 if ( ! empty( $_post->post_parent ) ) { 171 166 bbp_clean_post_cache( $_post->post_parent ); -
tests/phpunit/testcases/common/query.php
251 251 public function test_bbp_get_all_child_ids() { 252 252 $f = $this->factory->forum->create(); 253 253 254 // Test initial forum public child counts254 // Test initial forum public child forum counts. 255 255 $count = count( bbp_get_all_child_ids( $f, bbp_get_forum_post_type() ) ); 256 256 $this->assertSame( 0, $count ); 257 257 258 // Test initial forum public child topic counts. 258 259 $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) ); 259 260 $this->assertSame( 0, $count ); 260 261 261 262 /* Sub-Forums *********************************************************/ 262 263 263 $this->factory->forum->create_many( 3, array( 264 // Create two child forums of the above parent forum. 265 $this->factory->forum->create_many( 2, array( 264 266 'post_parent' => $f, 265 267 ) ); 266 268 269 // Forums should now be 2. 270 $count = count( bbp_get_all_child_ids( $f, bbp_get_forum_post_type() ) ); 271 $this->assertSame( 2, $count ); 272 273 // Create a private forum. 267 274 $this->factory->forum->create( array( 268 275 'post_parent' => $f, 269 276 'post_status' => bbp_get_private_status_id(), 270 277 ) ); 271 278 279 // Forums should now be 3. 272 280 $count = count( bbp_get_all_child_ids( $f, bbp_get_forum_post_type() ) ); 281 $this->assertSame( 3, $count ); 282 283 // Create a hidden forum. 284 $this->factory->forum->create( array( 285 'post_parent' => $f, 286 'post_status' => bbp_get_hidden_status_id(), 287 ) ); 288 289 // Forums should now be 4. 290 $count = count( bbp_get_all_child_ids( $f, bbp_get_forum_post_type() ) ); 273 291 $this->assertSame( 4, $count ); 274 292 293 // Create two more child forums. 275 294 $this->factory->forum->create_many( 2, array( 276 295 'post_parent' => $f, 277 296 ) ); … … 281 300 282 301 /* Topics *************************************************************/ 283 302 284 $t1 = $this->factory->topic->create_many( 3, array( 303 // Create two topics of the above parent forum. 304 $t = $this->factory->topic->create_many( 2, array( 285 305 'post_parent' => $f, 286 306 'topic_meta' => array( 287 307 'forum_id' => $f, … … 288 308 ), 289 309 ) ); 290 310 311 // Topics should now be 2. 312 $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) ); 313 $this->assertSame( 2, $count ); 314 315 // Create a pending status topic. 291 316 $this->factory->topic->create( array( 292 317 'post_parent' => $f, 293 'post_status' => bbp_get_ spam_status_id(),318 'post_status' => bbp_get_pending_status_id(), 294 319 'topic_meta' => array( 295 320 'forum_id' => $f, 296 321 ), 297 322 ) ); 298 323 324 // Topics should now be 3. 299 325 $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) ); 300 $this->assertSame( 4, $count );326 $this->assertSame( 3, $count ); 301 327 302 $this->factory->topic->create_many( 2, array( 328 // Create a spam status topic. 329 $this->factory->topic->create( array( 303 330 'post_parent' => $f, 331 'post_status' => bbp_get_spam_status_id(), 304 332 'topic_meta' => array( 305 333 'forum_id' => $f, 306 334 ), 307 335 ) ); 308 336 337 // Topics should now be 4. 309 338 $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) ); 310 $this->assertSame( 6, $count );339 $this->assertSame( 4, $count ); 311 340 312 $this->factory->topic->create( array( 341 // Create two more child topics. 342 $this->factory->topic->create_many( 2, array( 313 343 'post_parent' => $f, 314 'post_status' => bbp_get_pending_status_id(),315 344 'topic_meta' => array( 316 345 'forum_id' => $f, 317 346 ), 318 347 ) ); 319 348 349 // Topics should now be 6. 320 350 $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) ); 321 $this->assertSame( 7, $count );351 $this->assertSame( 6, $count ); 322 352 323 353 /* Replies ************************************************************/ 324 354 325 $this->factory->reply->create_many( 3, array( 326 'post_parent' => $t1[0], 355 // Create two replies of the above parent topic. 356 $this->factory->reply->create_many( 2, array( 357 'post_parent' => $t[0], 327 358 'reply_meta' => array( 328 359 'forum_id' => $f, 329 'topic_id' => $t 1[0],360 'topic_id' => $t[0], 330 361 ), 331 362 ) ); 332 363 364 // Replies should now be 2. 365 $count = count( bbp_get_all_child_ids( $t[0], bbp_get_reply_post_type() ) ); 366 $this->assertSame( 2, $count ); 367 368 // Create a pending status reply. 333 369 $this->factory->reply->create( array( 334 'post_parent' => $t 1[0],335 'post_status' => bbp_get_ spam_status_id(),370 'post_parent' => $t[0], 371 'post_status' => bbp_get_pending_status_id(), 336 372 'reply_meta' => array( 337 373 'forum_id' => $f, 338 'topic_id' => $t 1[0],374 'topic_id' => $t[0], 339 375 ), 340 376 ) ); 341 377 342 $count = count( bbp_get_all_child_ids( $t1[0], bbp_get_reply_post_type() ) ); 343 $this->assertSame( 4, $count ); 378 // Replies should now be 3. 379 $count = count( bbp_get_all_child_ids( $t[0], bbp_get_reply_post_type() ) ); 380 $this->assertSame( 3, $count ); 344 381 345 $this->factory->reply->create_many( 2, array( 346 'post_parent' => $t1[0], 382 // Create a spam status reply. 383 $this->factory->reply->create( array( 384 'post_parent' => $t[0], 385 'post_status' => bbp_get_spam_status_id(), 347 386 'reply_meta' => array( 348 387 'forum_id' => $f, 349 'topic_id' => $t 1[0],388 'topic_id' => $t[0], 350 389 ), 351 390 ) ); 352 391 353 $count = count( bbp_get_all_child_ids( $t1[0], bbp_get_reply_post_type() ) ); 354 $this->assertSame( 6, $count ); 392 // Replies should now be 4. 393 $count = count( bbp_get_all_child_ids( $t[0], bbp_get_reply_post_type() ) ); 394 $this->assertSame( 4, $count ); 355 395 356 $this->factory->reply->create( array(357 'post_parent' => $t1[0],358 'post_ status' => bbp_get_pending_status_id(),396 // Create two more child replies. 397 $this->factory->reply->create_many( 2, array( 398 'post_parent' => $t[0], 359 399 'reply_meta' => array( 360 400 'forum_id' => $f, 361 'topic_id' => $t 1[0],401 'topic_id' => $t[0], 362 402 ), 363 403 ) ); 364 404 365 $count = count( bbp_get_all_child_ids( $t1[0], bbp_get_reply_post_type() ) ); 366 $this->assertSame( 7, $count ); 405 // Replies should now be 6. 406 $count = count( bbp_get_all_child_ids( $t[0], bbp_get_reply_post_type() ) ); 407 $this->assertSame( 6, $count ); 367 408 } 368 409 }