Changeset 7453
- Timestamp:
- 09/07/2026 05:01:34 PM (2 months ago)
- Location:
- trunk
- Files:
-
- 8 edited
-
CHANGELOG.md (modified) (3 diffs)
-
src/includes/admin/tools/repair.php (modified) (17 diffs)
-
src/includes/core/actions.php (modified) (3 diffs)
-
src/includes/core/sub-actions.php (modified) (1 diff)
-
src/includes/forums/functions.php (modified) (7 diffs)
-
src/readme.txt (modified) (2 diffs)
-
tests/phpunit/testcases/admin/tools.php (modified) (5 diffs)
-
tests/phpunit/testcases/forums/functions/counts.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/CHANGELOG.md
r7449 r7453 26 26 - Added hooks after bbPress administration classes load and dedicated hooks 27 27 for canonical redirects. 28 - Added `bbp_post_updated` with the post ID and before/after post objects. 28 29 - Added contributor, security, support, governance, and repository guidance. 29 30 … … 31 32 32 33 - Raised the minimum requirements to WordPress 6.0 and PHP 7.2. 34 - Moved built-in topic, reply, forum, and user count synchronization to 35 `bbp_transition_post_status`. Existing creation and moderation actions still 36 fire, but no longer host bbPress's core count callbacks. Extensions that need 37 finalized counts should use the transition action at priority 11 or later; 38 permanent deletion continues through `bbp_deleted_topic` and 39 `bbp_deleted_reply`. 33 40 - Kept the classic editor for forums, topics, and replies while making bbPress 34 41 blocks available in Posts and Pages. … … 53 60 - Kept public, hidden, and author contribution counts accurate when topics and 54 61 replies change moderation status or are permanently deleted. 62 - Kept subforum counts accurate across public, private, hidden, trashed, moved, 63 restored, and permanently deleted forums. 64 - Corrected count repair tools to clear stale metadata caches, exclude anonymous 65 authors from voice counts, honor reply-status filters, rebuild private and 66 hidden forums, and preserve similarly named metadata on other post types. 55 67 56 68 - Cache a distinct count of current-site forum-role holders for forum statistics, -
trunk/src/includes/admin/tools/repair.php
r7380 r7453 10 10 // Exit if accessed directly 11 11 defined( 'ABSPATH' ) || exit; 12 13 /** Helpers *******************************************************************/ 14 15 /** 16 * Clear cached metadata for a repaired bbPress post type. 17 * 18 * Repair tools update metadata directly in the database, bypassing the cache 19 * invalidation performed by the WordPress metadata API. 20 * 21 * @since 2.6.16 22 * 23 * @param string $post_type Post type name. 24 */ 25 function bbp_admin_repair_clean_post_meta_cache( $post_type = '' ) { 26 $bbp_db = bbp_db(); 27 $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_type = %s", $post_type ); 28 $post_ids = wp_parse_id_list( $bbp_db->get_col( $query ) ); 29 30 if ( ! empty( $post_ids ) ) { 31 wp_cache_delete_multiple( $post_ids, 'post_meta' ); 32 } 33 } 12 34 13 35 /** … … 242 264 243 265 // Post types and status 244 $tpt = bbp_get_topic_post_type();245 $rpt = bbp_get_reply_post_type();246 $ pps = bbp_get_public_status_id();247 $ cps = bbp_get_closed_status_id();266 $tpt = bbp_get_topic_post_type(); 267 $rpt = bbp_get_reply_post_type(); 268 $sql_topic_statuses = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'"; 269 $sql_reply_statuses = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'"; 248 270 249 271 // Delete the meta key _bbp_reply_count for each topic … … 253 275 AND `postmeta`.`meta_key` = '_bbp_reply_count'"; 254 276 255 if ( is_wp_error( $bbp_db->query( $sql_delete )) ) {277 if ( false === $bbp_db->query( $sql_delete ) ) { 256 278 return array( 1, sprintf( $statement, $result ) ); 257 279 } … … 263 285 LEFT JOIN `{$bbp_db->posts}` as `replies` 264 286 ON `replies`.`post_parent` = `topics`.`ID` 265 AND `replies`.`post_status` = '{$pps}'287 AND `replies`.`post_status` IN ( {$sql_reply_statuses} ) 266 288 AND `replies`.`post_type` = '{$rpt}' 267 289 WHERE `topics`.`post_type` = '{$tpt}' 268 AND `topics`.`post_status` IN ( '{$pps}', '{$cps}')290 AND `topics`.`post_status` IN ( {$sql_topic_statuses} ) 269 291 GROUP BY `topics`.`ID`)"; 270 292 271 if ( is_wp_error( $bbp_db->query( $sql )) ) {293 if ( false === $bbp_db->query( $sql ) ) { 272 294 return array( 2, sprintf( $statement, $result ) ); 273 295 } 296 297 bbp_admin_repair_clean_post_meta_cache( $tpt ); 274 298 275 299 return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) ); … … 291 315 $result = esc_html__( 'Failed!', 'bbpress' ); 292 316 293 $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ('_bbp_voice_count', '_bbp_engagement')"; 294 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 317 $tpt = bbp_get_topic_post_type(); 318 319 $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta` 320 LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id` 321 WHERE `posts`.`post_type` = '{$tpt}' 322 AND `postmeta`.`meta_key` IN ( '_bbp_voice_count', '_bbp_engagement' )"; 323 if ( false === $bbp_db->query( $sql_delete ) ) { 295 324 return array( 1, sprintf( $statement, $result ) ); 296 325 } 297 326 298 327 // Post types and status 299 $tpt = bbp_get_topic_post_type(); 300 $rpt = bbp_get_reply_post_type(); 301 $pps = bbp_get_public_status_id(); 302 $cps = bbp_get_closed_status_id(); 328 $rpt = bbp_get_reply_post_type(); 329 $sql_topic_statuses = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'"; 330 $sql_reply_statuses = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'"; 303 331 304 332 $engagements_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) ( 305 SELECT postmeta.meta_value, '_bbp_engagement', posts.post_author333 SELECT topics.ID, '_bbp_engagement', posts.post_author 306 334 FROM {$bbp_db->posts} AS posts 307 LEFTJOIN {$bbp_db->postmeta} AS postmeta335 INNER JOIN {$bbp_db->postmeta} AS postmeta 308 336 ON posts.ID = postmeta.post_id 309 337 AND postmeta.meta_key = '_bbp_topic_id' 310 WHERE posts.post_type IN (%s, %s) 311 AND posts.post_status IN (%s, %s) 312 GROUP BY postmeta.meta_value, posts.post_author)", 338 INNER JOIN {$bbp_db->posts} AS topics 339 ON topics.ID = postmeta.meta_value 340 AND topics.post_type = %s 341 WHERE ( 342 ( posts.post_type = %s AND posts.post_status IN ({$sql_topic_statuses}) ) 343 OR ( posts.post_type = %s AND posts.post_status IN ({$sql_reply_statuses}) ) 344 ) 345 AND posts.post_author > 0 346 GROUP BY topics.ID, posts.post_author)", 313 347 $tpt, 314 $rpt, 315 $pps, 316 $cps 348 $tpt, 349 $rpt 317 350 ); 318 351 319 if ( is_wp_error( $bbp_db->query( $engagements_sql )) ) {352 if ( false === $bbp_db->query( $engagements_sql ) ) { 320 353 return array( 2, sprintf( $statement, $result ) ); 321 354 } 322 355 323 $voice_count_sql = "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) ( 324 SELECT post_id, '_bbp_voice_count', COUNT(DISTINCT meta_value) 325 FROM {$bbp_db->postmeta} 326 WHERE meta_key = '_bbp_engagement' 327 GROUP BY post_id)"; 328 329 if ( is_wp_error( $bbp_db->query( $voice_count_sql ) ) ) { 356 $voice_count_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) ( 357 SELECT engagements.post_id, '_bbp_voice_count', COUNT(DISTINCT engagements.meta_value) 358 FROM {$bbp_db->postmeta} AS engagements 359 INNER JOIN {$bbp_db->posts} AS topics 360 ON topics.ID = engagements.post_id 361 AND topics.post_type = %s 362 WHERE engagements.meta_key = '_bbp_engagement' 363 GROUP BY engagements.post_id)", 364 $tpt 365 ); 366 367 if ( false === $bbp_db->query( $voice_count_sql ) ) { 330 368 return array( 3, sprintf( $statement, $result ) ); 331 369 } 370 371 bbp_admin_repair_clean_post_meta_cache( $tpt ); 332 372 333 373 return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) ); … … 349 389 $result = esc_html__( 'Failed!', 'bbpress' ); 350 390 351 $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_reply_count_hidden'"; 352 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 353 return array( 1, sprintf( $statement, $result ) ); 354 } 355 356 // Post types and status 391 $tpt = bbp_get_topic_post_type(); 357 392 $rpt = bbp_get_reply_post_type(); 358 $sta = bbp_get_non_public_topic_statuses(); 393 394 $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta` 395 LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id` 396 WHERE `posts`.`post_type` = '{$tpt}' 397 AND `postmeta`.`meta_key` = '_bbp_reply_count_hidden'"; 398 if ( false === $bbp_db->query( $sql_delete ) ) { 399 return array( 1, sprintf( $statement, $result ) ); 400 } 401 402 // Reply statuses 403 $sta = bbp_get_non_public_reply_statuses(); 359 404 360 405 // SQL 361 406 $sql_status = "'" . implode( "','", $sta ) . "'"; 362 407 363 $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_reply_count_hidden', COUNT(`post_status`) as `meta_value` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$rpt}' AND `post_status` IN ({$sql_status}) GROUP BY `post_parent`)"; 364 if ( is_wp_error( $bbp_db->query( $sql ) ) ) { 408 $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) ( 409 SELECT `topics`.`ID`, '_bbp_reply_count_hidden', COUNT(`replies`.`ID`) AS `meta_value` 410 FROM `{$bbp_db->posts}` AS `replies` 411 INNER JOIN `{$bbp_db->posts}` AS `topics` 412 ON `topics`.`ID` = `replies`.`post_parent` 413 AND `topics`.`post_type` = '{$tpt}' 414 WHERE `replies`.`post_type` = '{$rpt}' 415 AND `replies`.`post_status` IN ({$sql_status}) 416 GROUP BY `topics`.`ID`)"; 417 if ( false === $bbp_db->query( $sql ) ) { 365 418 return array( 2, sprintf( $statement, $result ) ); 366 419 } 420 421 bbp_admin_repair_clean_post_meta_cache( $tpt ); 367 422 368 423 return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) ); … … 384 439 $result = esc_html__( 'Failed!', 'bbpress' ); 385 440 386 $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )"; 387 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 441 $fpt = bbp_get_forum_post_type(); 442 443 $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta` 444 LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id` 445 WHERE `posts`.`post_type` = '{$fpt}' 446 AND `postmeta`.`meta_key` IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )"; 447 if ( false === $bbp_db->query( $sql_delete ) ) { 388 448 return array( 1, sprintf( $statement, $result ) ); 389 449 } … … 391 451 $forums = get_posts( 392 452 array( 393 'post_type' => bbp_get_forum_post_type(), 453 'fields' => 'ids', 454 'post_type' => $fpt, 455 'post_status' => bbp_get_countable_forum_statuses(), 394 456 'numberposts' => -1 395 457 ) … … 397 459 398 460 if ( ! empty( $forums ) ) { 399 foreach ( $forums as $forum ) {400 bbp_update_forum_topic_count( $forum ->ID);401 bbp_update_forum_topic_count_hidden( $forum ->ID);461 foreach ( $forums as $forum_id ) { 462 bbp_update_forum_topic_count( $forum_id ); 463 bbp_update_forum_topic_count_hidden( $forum_id ); 402 464 } 403 465 } else { … … 482 544 LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id` 483 545 WHERE `posts`.`post_type` = '{$fpt}' 484 AND `postmeta`.`meta_key` = '_bbp_reply_count' 485 OR `postmeta`.`meta_key` = '_bbp_total_reply_count'"; 486 487 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 546 AND `postmeta`.`meta_key` IN ( '_bbp_reply_count', '_bbp_total_reply_count' )"; 547 548 if ( false === $bbp_db->query( $sql_delete ) ) { 488 549 return array( 1, sprintf( $statement, $result ) ); 489 550 } … … 492 553 $forums = get_posts( 493 554 array( 494 'post_type' => bbp_get_forum_post_type(), 555 'fields' => 'ids', 556 'post_type' => $fpt, 557 'post_status' => bbp_get_countable_forum_statuses(), 495 558 'numberposts' => -1 496 559 ) … … 498 561 499 562 if ( ! empty( $forums ) ) { 500 foreach ( $forums as $forum ) {501 bbp_update_forum_reply_count( $forum ->ID);563 foreach ( $forums as $forum_id ) { 564 bbp_update_forum_reply_count( $forum_id ); 502 565 } 503 566 } else { … … 531 594 LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id` 532 595 WHERE `posts`.`post_type` = '{$fpt}' 533 AND `postmeta`.`meta_key` = '_bbp_reply_count_hidden' 534 OR `postmeta`.`meta_key` = '_bbp_total_reply_count_hidden'"; 535 536 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 596 AND `postmeta`.`meta_key` IN ( '_bbp_reply_count_hidden', '_bbp_total_reply_count_hidden' )"; 597 598 if ( false === $bbp_db->query( $sql_delete ) ) { 537 599 return array( 1, sprintf( $statement, $result ) ); 538 600 } … … 541 603 $forums = get_posts( 542 604 array( 543 'post_type' => bbp_get_forum_post_type(), 605 'fields' => 'ids', 606 'post_type' => $fpt, 607 'post_status' => bbp_get_countable_forum_statuses(), 544 608 'numberposts' => -1 545 609 ) … … 547 611 548 612 if ( ! empty( $forums ) ) { 549 foreach ( $forums as $forum ) {550 bbp_update_forum_reply_count_hidden( $forum ->ID);613 foreach ( $forums as $forum_id ) { 614 bbp_update_forum_reply_count_hidden( $forum_id ); 551 615 } 552 616 } else { … … 574 638 $sql_type = bbp_get_topic_post_type(); 575 639 $sql_status = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'"; 576 $sql_select = "SELECT `post_author`, COUNT( DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$sql_type}' AND `post_status` IN ({$sql_status})GROUP BY `post_author`";640 $sql_select = "SELECT `post_author`, COUNT(*) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$sql_type}' AND `post_status` IN ({$sql_status}) AND `post_author` > 0 GROUP BY `post_author`"; 577 641 $insert_rows = $bbp_db->get_results( $sql_select ); 578 642 579 if ( is_wp_error( $insert_rows) ) {643 if ( ! empty( $bbp_db->last_error ) ) { 580 644 return array( 1, sprintf( $statement, $result ) ); 581 645 } 582 646 583 647 $key = $bbp_db->prefix . '_bbp_topic_count'; 648 $user_ids = (array) $bbp_db->get_col( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" ); 584 649 $insert_values = array(); 650 651 if ( ! empty( $bbp_db->last_error ) ) { 652 return array( 2, sprintf( $statement, $result ) ); 653 } 654 585 655 foreach ( $insert_rows as $insert_row ) { 586 $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')"; 587 } 588 589 if ( ! count( $insert_values ) ) { 590 return array( 2, sprintf( $statement, $result ) ); 656 $user_id = (int) $insert_row->post_author; 657 $user_ids[] = $user_id; 658 $insert_values[] = $bbp_db->prepare( '(%d, %s, %d)', $user_id, $key, (int) $insert_row->_count ); 591 659 } 592 660 593 661 $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'"; 594 if ( is_wp_error( $bbp_db->query( $sql_delete )) ) {662 if ( false === $bbp_db->query( $sql_delete ) ) { 595 663 return array( 3, sprintf( $statement, $result ) ); 596 664 } 597 665 666 $insert_error = false; 598 667 foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) { 599 668 $chunk = "\n" . implode( ",\n", $chunk ); 600 669 $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}"; 601 670 602 if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) { 603 return array( 4, sprintf( $statement, $result ) ); 604 } 671 if ( false === $bbp_db->query( $sql_insert ) ) { 672 $insert_error = true; 673 break; 674 } 675 } 676 677 // Clear cached counts for users affected by the direct database update 678 $user_ids = wp_parse_id_list( $user_ids ); 679 if ( ! empty( $user_ids ) ) { 680 wp_cache_delete_multiple( $user_ids, 'user_meta' ); 681 } 682 683 if ( $insert_error ) { 684 return array( 4, sprintf( $statement, $result ) ); 605 685 } 606 686 … … 625 705 $sql_type = bbp_get_reply_post_type(); 626 706 $sql_status = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'"; 627 $sql_select = "SELECT `post_author`, COUNT( DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$sql_type}' AND `post_status` IN ({$sql_status})GROUP BY `post_author`";707 $sql_select = "SELECT `post_author`, COUNT(*) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$sql_type}' AND `post_status` IN ({$sql_status}) AND `post_author` > 0 GROUP BY `post_author`"; 628 708 $insert_rows = $bbp_db->get_results( $sql_select ); 629 709 630 if ( is_wp_error( $insert_rows) ) {710 if ( ! empty( $bbp_db->last_error ) ) { 631 711 return array( 1, sprintf( $statement, $result ) ); 632 712 } 633 713 634 714 $key = $bbp_db->prefix . '_bbp_reply_count'; 715 $user_ids = (array) $bbp_db->get_col( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" ); 635 716 $insert_values = array(); 717 718 if ( ! empty( $bbp_db->last_error ) ) { 719 return array( 2, sprintf( $statement, $result ) ); 720 } 721 636 722 foreach ( $insert_rows as $insert_row ) { 637 $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')"; 638 } 639 640 if ( ! count( $insert_values ) ) { 641 return array( 2, sprintf( $statement, $result ) ); 723 $user_id = (int) $insert_row->post_author; 724 $user_ids[] = $user_id; 725 $insert_values[] = $bbp_db->prepare( '(%d, %s, %d)', $user_id, $key, (int) $insert_row->_count ); 642 726 } 643 727 644 728 $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'"; 645 if ( is_wp_error( $bbp_db->query( $sql_delete )) ) {729 if ( false === $bbp_db->query( $sql_delete ) ) { 646 730 return array( 3, sprintf( $statement, $result ) ); 647 731 } 648 732 733 $insert_error = false; 649 734 foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) { 650 735 $chunk = "\n" . implode( ",\n", $chunk ); 651 736 $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}"; 652 737 653 if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) { 654 return array( 4, sprintf( $statement, $result ) ); 655 } 738 if ( false === $bbp_db->query( $sql_insert ) ) { 739 $insert_error = true; 740 break; 741 } 742 } 743 744 // Clear cached counts for users affected by the direct database update 745 $user_ids = wp_parse_id_list( $user_ids ); 746 if ( ! empty( $user_ids ) ) { 747 wp_cache_delete_multiple( $user_ids, 'user_meta' ); 748 } 749 750 if ( $insert_error ) { 751 return array( 4, sprintf( $statement, $result ) ); 656 752 } 657 753 -
trunk/src/includes/core/actions.php
r7449 r7453 55 55 add_action( 'wp_footer', 'bbp_footer', 10 ); 56 56 add_action( 'transition_post_status', 'bbp_transition_post_status', 10, 3 ); 57 add_action( 'post_updated', 'bbp_post_updated', 10, 3 ); 57 58 58 59 /** … … 181 182 add_action( 'trashed_post', 'bbp_trashed_forum' ); 182 183 add_action( 'untrashed_post', 'bbp_untrashed_forum' ); 183 add_action( 'deleted_post', 'bbp_deleted_forum' );184 add_action( 'deleted_post', 'bbp_deleted_forum', 10, 2 ); 184 185 185 186 // Auto trash/untrash/delete a forums topics … … 275 276 add_action( 'bbp_unapproved_reply', 'bbp_update_reply_walker' ); 276 277 277 // Update counts when topics and replies are created or change status278 // Update counts from persisted post status changes 278 279 add_action( 'bbp_transition_post_status', 'bbp_update_counts_on_transition_post_status', 10, 3 ); 280 add_action( 'bbp_transition_post_status', 'bbp_update_forum_subforum_count_on_transition_post_status', 10, 3 ); 281 282 // Update parent subforum counts after updates and permanent deletion 283 add_action( 'bbp_deleted_forum', 'bbp_reparent_forum_subforums', 9, 2 ); 284 add_action( 'bbp_deleted_forum', 'bbp_update_parent_forum_subforum_count', 10, 2 ); 285 add_action( 'bbp_post_updated', 'bbp_update_forum_subforum_counts_on_post_updated', 10, 3 ); 279 286 280 287 // Update user topic & reply counts -
trunk/src/includes/core/sub-actions.php
r7383 r7453 297 297 // Do the action 298 298 do_action( 'bbp_transition_post_status', $new_status, $old_status, $post ); 299 } 300 301 /** 302 * Add the bbPress-specific post updated action. 303 * 304 * @since 2.6.16 305 * 306 * @param int $post_id Post ID. 307 * @param WP_Post $post_after Post object following the update. 308 * @param WP_Post $post_before Post object before the update. 309 */ 310 function bbp_post_updated( $post_id = 0, $post_after = false, $post_before = false ) { 311 $post_types = bbp_get_post_types(); 312 313 // Bail if neither version is a bbPress post type 314 if ( ! in_array( $post_after->post_type, $post_types, true ) && ! in_array( $post_before->post_type, $post_types, true ) ) { 315 return; 316 } 317 318 // Do the action 319 do_action( 'bbp_post_updated', $post_id, $post_after, $post_before ); 299 320 } 300 321 -
trunk/src/includes/forums/functions.php
r7423 r7453 1815 1815 * 1816 1816 * @since 2.0.0 bbPress (r2625) 1817 * 1818 * @param int $forum_id Optional. Forum id. 1819 * @param int $subforums Optional. Number of subforums. 1820 * @return bool True on success, false on failure. 1817 * @since 2.6.16 Count supported forum visibilities from the post hierarchy. 1818 * 1819 * @param int $forum_id Optional. Forum ID. 1820 * @param int|bool $subforums Optional. Number of subforums, or false to query. 1821 * @return int|false Number of subforums, or false on query failure. 1821 1822 */ 1822 1823 function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = false ) { … … 1824 1825 1825 1826 // Maybe query for counts 1826 $subforums = ! is_int( $subforums ) 1827 ? bbp_get_public_child_count( $forum_id, bbp_get_forum_post_type() ) 1828 : (int) $subforums; 1827 if ( ! is_int( $subforums ) ) { 1828 $bbp_db = bbp_db(); 1829 $post_type = bbp_get_forum_post_type(); 1830 $statuses = bbp_get_countable_forum_statuses(); 1831 1832 if ( ! empty( $statuses ) ) { 1833 $placeholders = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) ); 1834 $sql = "SELECT COUNT(*) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_type = %s AND post_status IN ({$placeholders})"; 1835 $query = $bbp_db->prepare( $sql, array_merge( array( $forum_id, $post_type ), $statuses ) ); 1836 $subforums = $bbp_db->get_var( $query ); 1837 1838 // Bail if the count query failed 1839 if ( ! empty( $bbp_db->last_error ) ) { 1840 return false; 1841 } 1842 1843 $subforums = bbp_number_not_negative( $subforums ); 1844 } else { 1845 $subforums = 0; 1846 } 1847 } 1829 1848 1830 1849 update_post_meta( $forum_id, '_bbp_forum_subforum_count', $subforums ); … … 1832 1851 // Filter & return 1833 1852 return (int) apply_filters( 'bbp_update_forum_subforum_count', $subforums, $forum_id ); 1853 } 1854 1855 /** 1856 * Synchronize child-forum metadata after WordPress reparents deleted children. 1857 * 1858 * @since 2.6.16 1859 * 1860 * @param int $forum_id Deleted forum ID. 1861 * @param WP_Post $forum Deleted forum post object. 1862 */ 1863 function bbp_reparent_forum_subforums( $forum_id = 0, $forum = false ) { 1864 $bbp_db = bbp_db(); 1865 $post_type = bbp_get_forum_post_type(); 1866 $meta_key = '_bbp_forum_id'; 1867 $sql = "SELECT posts.ID 1868 FROM {$bbp_db->posts} AS posts 1869 INNER JOIN {$bbp_db->postmeta} AS postmeta 1870 ON posts.ID = postmeta.post_id 1871 AND postmeta.meta_key = %s 1872 WHERE posts.post_type = %s 1873 AND posts.post_parent = %d 1874 AND postmeta.meta_value = %d"; 1875 $query = $bbp_db->prepare( $sql, $meta_key, $post_type, $forum->post_parent, $forum_id ); 1876 1877 foreach ( wp_parse_id_list( $bbp_db->get_col( $query ) ) as $subforum_id ) { 1878 bbp_update_forum_id( $subforum_id, $forum->post_parent ); 1879 } 1880 } 1881 1882 /** 1883 * Update a parent forum's subforum count after a child is permanently deleted. 1884 * 1885 * @since 2.6.16 1886 * 1887 * @param int $forum_id Forum ID. 1888 * @param WP_Post|bool $forum Optional. Forum post object. 1889 * @return int|false Updated subforum count, or false if there is no parent. 1890 */ 1891 function bbp_update_parent_forum_subforum_count( $forum_id = 0, $forum = false ) { 1892 $forum = ( $forum instanceof WP_Post ) 1893 ? $forum 1894 : get_post( $forum_id ); 1895 1896 // Bail if the forum has no parent 1897 if ( empty( $forum ) || empty( $forum->post_parent ) ) { 1898 return false; 1899 } 1900 1901 return bbp_update_forum_subforum_count( $forum->post_parent ); 1902 } 1903 1904 /** 1905 * Update a parent forum's subforum count after a child changes count status. 1906 * 1907 * @since 2.6.16 1908 * 1909 * @param string $new_status New post status. 1910 * @param string $old_status Old post status. 1911 * @param WP_Post $forum Forum post object. 1912 * @return int|false Updated subforum count, or false when no update is needed. 1913 */ 1914 function bbp_update_forum_subforum_count_on_transition_post_status( $new_status = '', $old_status = '', $forum = false ) { 1915 1916 // Bail if this is not a child forum 1917 if ( ( bbp_get_forum_post_type() !== $forum->post_type ) || empty( $forum->post_parent ) ) { 1918 return false; 1919 } 1920 1921 $statuses = bbp_get_countable_forum_statuses(); 1922 $was_counted = in_array( $old_status, $statuses, true ); 1923 $is_counted = in_array( $new_status, $statuses, true ); 1924 1925 // Bail if subforum count membership did not change 1926 if ( $was_counted === $is_counted ) { 1927 return false; 1928 } 1929 1930 return bbp_update_forum_subforum_count( $forum->post_parent ); 1931 } 1932 1933 /** 1934 * Update subforum counts after a forum changes parent or post type. 1935 * 1936 * @since 2.6.16 1937 * 1938 * @param int $forum_id Forum ID. 1939 * @param WP_Post $forum_after Forum object following the update. 1940 * @param WP_Post $forum_before Forum object before the update. 1941 */ 1942 function bbp_update_forum_subforum_counts_on_post_updated( $forum_id = 0, $forum_after = false, $forum_before = false ) { 1943 $post_type = bbp_get_forum_post_type(); 1944 $parent_moved = ( $forum_after->post_parent !== $forum_before->post_parent ); 1945 1946 // Bail if forum hierarchy membership did not change 1947 if ( ! $parent_moved && ( $forum_after->post_type === $forum_before->post_type ) ) { 1948 return false; 1949 } 1950 1951 // Synchronize parent metadata when the updated post remains a forum 1952 if ( ( $post_type === $forum_after->post_type ) && ( $parent_moved || ( $post_type !== $forum_before->post_type ) ) ) { 1953 bbp_update_forum_id( $forum_id, $forum_after->post_parent ); 1954 } 1955 1956 $statuses = bbp_get_countable_forum_statuses(); 1957 $was_counted = ( $post_type === $forum_before->post_type ) && in_array( $forum_before->post_status, $statuses, true ); 1958 $is_counted = ( $post_type === $forum_after->post_type ) && in_array( $forum_after->post_status, $statuses, true ); 1959 1960 // Include the previous parent when it formerly contained this subforum 1961 $parent_ids = $was_counted 1962 ? array( $forum_before->post_parent ) 1963 : array(); 1964 1965 // Include the new parent when it now contains this subforum 1966 if ( $is_counted ) { 1967 $parent_ids[] = $forum_after->post_parent; 1968 } 1969 1970 // Recount each affected parent once 1971 foreach ( array_filter( array_unique( $parent_ids ) ) as $parent_id ) { 1972 bbp_update_forum_subforum_count( $parent_id ); 1973 } 1834 1974 } 1835 1975 … … 2197 2337 // Filter & return 2198 2338 return (array) apply_filters( 'bbp_get_non_public_forum_statuses', $statuses ); 2339 } 2340 2341 /** 2342 * Return forum statuses included in forum and subforum counts. 2343 * 2344 * @since 2.6.16 2345 * 2346 * @return array 2347 */ 2348 function bbp_get_countable_forum_statuses() { 2349 $statuses = array_values( array_unique( array_merge( bbp_get_public_forum_statuses(), bbp_get_non_public_forum_statuses() ) ) ); 2350 2351 // Filter & return 2352 return (array) apply_filters( 'bbp_get_countable_forum_statuses', $statuses ); 2199 2353 } 2200 2354 … … 2957 3111 * @since 2.1.0 bbPress (r3668) 2958 3112 * @since 2.6.0 bbPress (r6526) Not recommend for usage 2959 */ 2960 function bbp_deleted_forum( $forum_id = 0 ) { 3113 * @since 2.6.16 Added the `$forum` parameter and passed it to the action. 3114 * 3115 * @param int $forum_id Forum ID. 3116 * @param WP_Post|bool $forum Optional. Deleted forum post object. 3117 * @return false|null False if the post is not a forum, otherwise null. 3118 */ 3119 function bbp_deleted_forum( $forum_id = 0, $forum = false ) { 3120 $forum_id = bbp_get_forum_id( $forum_id ); 3121 $forum = ( $forum instanceof WP_Post ) 3122 ? $forum 3123 : get_post( $forum_id ); 3124 3125 if ( empty( $forum_id ) || ! $forum || ( bbp_get_forum_post_type() !== $forum->post_type ) ) { 3126 return false; 3127 } 3128 3129 do_action( 'bbp_deleted_forum', $forum_id, $forum ); 3130 } 3131 3132 /** 3133 * Called after trashing a forum. 3134 * 3135 * @since 2.1.0 bbPress (r3668) 3136 */ 3137 function bbp_trashed_forum( $forum_id = 0 ) { 2961 3138 $forum_id = bbp_get_forum_id( $forum_id ); 2962 3139 … … 2965 3142 } 2966 3143 2967 do_action( 'bbp_ deleted_forum', $forum_id );2968 } 2969 2970 /** 2971 * Called after trashing a forum.3144 do_action( 'bbp_trashed_forum', $forum_id ); 3145 } 3146 3147 /** 3148 * Called after untrashing a forum. 2972 3149 * 2973 3150 * @since 2.1.0 bbPress (r3668) 2974 3151 */ 2975 function bbp_ trashed_forum( $forum_id = 0 ) {3152 function bbp_untrashed_forum( $forum_id = 0 ) { 2976 3153 $forum_id = bbp_get_forum_id( $forum_id ); 2977 3154 … … 2980 3157 } 2981 3158 2982 do_action( 'bbp_trashed_forum', $forum_id );2983 }2984 2985 /**2986 * Called after untrashing a forum.2987 *2988 * @since 2.1.0 bbPress (r3668)2989 */2990 function bbp_untrashed_forum( $forum_id = 0 ) {2991 $forum_id = bbp_get_forum_id( $forum_id );2992 2993 if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) {2994 return false;2995 }2996 2997 3159 do_action( 'bbp_untrashed_forum', $forum_id ); 2998 3160 } -
trunk/src/readme.txt
r7418 r7453 44 44 * bbPress also comes with built-in support for Akismet and BuddyPress, two very popular and very powerful WordPress plugins. If you're using either, visit your Forum Settings page and ensure that integration appears correct. 45 45 46 == Developer Notes == 47 48 = Count updates in 2.6.16 = 49 50 bbPress now synchronizes its built-in public and hidden topic and reply counts, aggregate forum counts, and user contribution counts on `bbp_transition_post_status` at priority 10, after WordPress persists the new post status. This action receives the new status, old status, and `WP_Post` object. 51 52 The existing `bbp_new_*`, `bbp_insert_*`, `bbp_trash_*`, `bbp_untrash_*`, `bbp_spam_*`, `bbp_unspam_*`, `bbp_approve_*`, and `bbp_unapprove_*` topic and reply actions continue to fire with their existing arguments and timing. Their corresponding completed actions, such as `bbp_trashed_*`, also remain available. The public count helper functions also remain callable. 53 54 bbPress no longer attaches its built-in increase, decrease, and insertion count callbacks to those creation and moderation actions. Manually firing one of those actions without changing the post status therefore no longer updates counts. Extensions that need finalized counts after creation or moderation should use `bbp_transition_post_status` at priority 11 or later. 55 56 Permanent deletion does not produce a post-status transition. Its count maintenance continues through the existing `bbp_deleted_topic` and `bbp_deleted_reply` actions. 57 58 = Subforum counts in 2.6.16 = 59 60 bbPress now maintains subforum counts when forums are trashed, restored, permanently deleted, or moved between parents. The new `bbp_post_updated` action receives the post ID, the updated `WP_Post` object, and the previous `WP_Post` object after any bbPress post type is updated. 61 46 62 == Screenshots == 47 63 … … 57 73 58 74 Check out the [releases page](https://codex.bbpress.org/releases/) 75 76 == Upgrade Notice == 77 78 = 2.6.16 = 79 80 Count maintenance now uses the bbPress post-status transition action. Extensions that customize topic or reply counts should review the Developer Notes. -
trunk/tests/phpunit/testcases/admin/tools.php
r7365 r7453 108 108 /** 109 109 * @covers ::bbp_admin_repair_topic_reply_count 110 * @t odo Implement test_bbp_admin_repair_topic_reply_count().110 * @ticket BBP2664 111 111 */ 112 112 public function test_bbp_admin_repair_topic_reply_count() { 113 // Remove the following lines when you implement this test. 114 $this->markTestIncomplete( 115 'This test has not been implemented yet.' 116 ); 113 $topic_id = $this->factory->topic->create(); 114 115 $this->factory->reply->create_many( 2, array( 'post_parent' => $topic_id ) ); 116 $this->factory->reply->create( array( 117 'post_parent' => $topic_id, 118 'post_status' => bbp_get_pending_status_id(), 119 ) ); 120 bbp_update_topic_reply_count( $topic_id, 9 ); 121 122 $public_statuses = function ( $statuses ) { 123 $statuses[] = bbp_get_pending_status_id(); 124 return $statuses; 125 }; 126 add_filter( 'bbp_get_public_reply_statuses', $public_statuses ); 127 $result = bbp_admin_repair_topic_reply_count(); 128 remove_filter( 'bbp_get_public_reply_statuses', $public_statuses ); 129 130 $this->assertSame( 0, $result[0] ); 131 $this->assertSame( 3, bbp_get_topic_reply_count( $topic_id, true ) ); 117 132 } 118 133 119 134 /** 120 135 * @covers ::bbp_admin_repair_topic_voice_count 136 * @ticket BBP2664 121 137 */ 122 138 public function test_bbp_admin_repair_topic_voice_count() { … … 153 169 $this->assertSame( '3', $count ); 154 170 171 $pending_user_id = $this->factory->user->create(); 172 $this->factory->reply->create( array( 173 'post_author' => $pending_user_id, 174 'post_parent' => $t, 175 'post_status' => bbp_get_pending_status_id(), 176 'reply_meta' => array( 177 'forum_id' => $f, 178 'topic_id' => $t, 179 ), 180 ) ); 181 155 182 // Delete the topic _bbp_voice_count meta key. 156 183 $this->assertTrue( delete_post_meta_by_key( '_bbp_voice_count' ) ); … … 160 187 161 188 // Repair the topic voice count meta. 189 $public_statuses = function ( $statuses ) { 190 $statuses[] = bbp_get_pending_status_id(); 191 return $statuses; 192 }; 193 add_filter( 'bbp_get_public_reply_statuses', $public_statuses ); 162 194 bbp_admin_repair_topic_voice_count(); 163 164 clean_post_cache( $t ); 195 remove_filter( 'bbp_get_public_reply_statuses', $public_statuses ); 165 196 166 197 $count = bbp_get_topic_voice_count( $t ); 167 $this->assertSame( '3', $count ); 198 $this->assertSame( '4', $count ); 199 } 200 201 /** 202 * @covers ::bbp_admin_repair_topic_voice_count 203 */ 204 public function test_bbp_admin_repair_topic_voice_count_excludes_anonymous_authors() { 205 $forum_id = $this->factory->forum->create(); 206 $topic_id = $this->factory->topic->create( array( 207 'post_author' => 0, 208 'post_parent' => $forum_id, 209 'topic_meta' => array( 210 'forum_id' => $forum_id, 211 ), 212 ) ); 213 214 $this->factory->reply->create( array( 215 'post_author' => 0, 216 'post_parent' => $topic_id, 217 'reply_meta' => array( 218 'forum_id' => $forum_id, 219 'topic_id' => $topic_id, 220 ), 221 ) ); 222 223 bbp_admin_repair_topic_voice_count(); 224 225 $this->assertSame( 0, bbp_get_topic_voice_count( $topic_id, true ) ); 168 226 } 169 227 170 228 /** 171 229 * @covers ::bbp_admin_repair_topic_hidden_reply_count 230 * @ticket BBP2664 172 231 */ 173 232 public function test_bbp_admin_repair_topic_hidden_reply_count() { … … 214 273 215 274 // Repair the topic hidden reply count meta. 275 add_filter( 'bbp_get_non_public_topic_statuses', '__return_empty_array' ); 216 276 bbp_admin_repair_topic_hidden_reply_count(); 217 218 clean_post_cache( $t ); 277 remove_filter( 'bbp_get_non_public_topic_statuses', '__return_empty_array' ); 219 278 220 279 $count = bbp_get_topic_reply_count_hidden( $t, true ); … … 512 571 513 572 /** 573 * @covers ::bbp_admin_repair_forum_topic_count 574 * @covers ::bbp_admin_repair_forum_reply_count 575 * @covers ::bbp_admin_repair_forum_hidden_reply_count 576 */ 577 public function test_forum_count_repairs_include_private_and_hidden_forums() { 578 $forum_ids = array(); 579 580 foreach ( array( bbp_get_private_status_id(), bbp_get_hidden_status_id() ) as $status ) { 581 $forum_id = $this->factory->forum->create( array( 'post_status' => $status ) ); 582 $forum_ids[] = $forum_id; 583 $topic_id = $this->factory->topic->create( array( 584 'post_parent' => $forum_id, 585 'topic_meta' => array( 'forum_id' => $forum_id ), 586 ) ); 587 588 $this->factory->reply->create( array( 589 'post_parent' => $topic_id, 590 'reply_meta' => array( 591 'forum_id' => $forum_id, 592 'topic_id' => $topic_id, 593 ), 594 ) ); 595 $this->factory->reply->create( array( 596 'post_parent' => $topic_id, 597 'post_status' => bbp_get_spam_status_id(), 598 'reply_meta' => array( 599 'forum_id' => $forum_id, 600 'topic_id' => $topic_id, 601 ), 602 ) ); 603 } 604 605 bbp_admin_repair_forum_topic_count(); 606 bbp_admin_repair_forum_reply_count(); 607 bbp_admin_repair_forum_hidden_reply_count(); 608 609 foreach ( $forum_ids as $forum_id ) { 610 $this->assertSame( 1, bbp_get_forum_topic_count( $forum_id, true, true ) ); 611 $this->assertSame( 1, bbp_get_forum_reply_count( $forum_id, true, true ) ); 612 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $forum_id, true, true ) ); 613 } 614 } 615 616 /** 617 * @covers ::bbp_admin_repair_topic_voice_count 618 * @covers ::bbp_admin_repair_topic_hidden_reply_count 619 * @covers ::bbp_admin_repair_forum_topic_count 620 * @covers ::bbp_admin_repair_forum_reply_count 621 * @covers ::bbp_admin_repair_forum_hidden_reply_count 622 * @ticket BBP2586 623 */ 624 public function test_count_repairs_preserve_non_bbp_post_meta() { 625 $this->factory->forum->create(); 626 $post_id = $this->factory->post->create(); 627 $reply_id = $this->factory->post->create( array( 628 'post_author' => $this->factory->user->create(), 629 'post_parent' => $post_id, 630 'post_status' => bbp_get_public_status_id(), 631 'post_type' => bbp_get_reply_post_type(), 632 ) ); 633 634 update_post_meta( $reply_id, '_bbp_topic_id', $post_id ); 635 $this->factory->post->create( array( 636 'post_parent' => $post_id, 637 'post_status' => bbp_get_spam_status_id(), 638 'post_type' => bbp_get_reply_post_type(), 639 ) ); 640 641 $metadata = array( 642 '_bbp_reply_count_hidden' => 4, 643 '_bbp_voice_count' => 5, 644 '_bbp_engagement' => 6, 645 '_bbp_total_reply_count' => 7, 646 '_bbp_total_reply_count_hidden' => 8, 647 '_bbp_topic_count' => 9, 648 '_bbp_total_topic_count' => 10, 649 '_bbp_topic_count_hidden' => 11, 650 ); 651 652 foreach ( $metadata as $key => $value ) { 653 update_post_meta( $post_id, $key, $value ); 654 } 655 656 bbp_admin_repair_topic_voice_count(); 657 bbp_admin_repair_topic_hidden_reply_count(); 658 bbp_admin_repair_forum_topic_count(); 659 bbp_admin_repair_forum_reply_count(); 660 bbp_admin_repair_forum_hidden_reply_count(); 661 662 foreach ( $metadata as $key => $value ) { 663 $this->assertSame( array( (string) $value ), get_post_meta( $post_id, $key, false ) ); 664 } 665 } 666 667 /** 514 668 * @covers ::bbp_admin_repair_user_topic_count 515 * @t odo Implement test_bbp_admin_repair_user_topic_count().669 * @ticket BBP2664 516 670 */ 517 671 public function test_bbp_admin_repair_user_topic_count() { 518 // Remove the following lines when you implement this test. 519 $this->markTestIncomplete( 520 'This test has not been implemented yet.' 521 ); 672 $user_id = $this->factory->user->create(); 673 674 $this->factory->topic->create_many( 2, array( 'post_author' => $user_id ) ); 675 $this->factory->topic->create( array( 676 'post_author' => $user_id, 677 'post_status' => bbp_get_spam_status_id(), 678 ) ); 679 bbp_update_user_topic_count( $user_id, 9 ); 680 $this->assertSame( 9, bbp_get_user_topic_count( $user_id, true ) ); 681 682 $result = bbp_admin_repair_user_topic_count(); 683 684 $this->assertSame( 0, $result[0] ); 685 $this->assertSame( 2, bbp_get_user_topic_count( $user_id, true ) ); 522 686 } 523 687 524 688 /** 525 689 * @covers ::bbp_admin_repair_user_reply_count 526 * @t odo Implement test_bbp_admin_repair_user_reply_count().690 * @ticket BBP2664 527 691 */ 528 692 public function test_bbp_admin_repair_user_reply_count() { 529 // Remove the following lines when you implement this test. 530 $this->markTestIncomplete( 531 'This test has not been implemented yet.' 532 ); 693 $user_id = $this->factory->user->create(); 694 $topic_id = $this->factory->topic->create(); 695 696 $this->factory->reply->create_many( 2, array( 697 'post_author' => $user_id, 698 'post_parent' => $topic_id, 699 ) ); 700 $this->factory->reply->create( array( 701 'post_author' => $user_id, 702 'post_parent' => $topic_id, 703 'post_status' => bbp_get_spam_status_id(), 704 ) ); 705 bbp_update_user_reply_count( $user_id, 9 ); 706 $this->assertSame( 9, bbp_get_user_reply_count( $user_id, true ) ); 707 708 $result = bbp_admin_repair_user_reply_count(); 709 710 $this->assertSame( 0, $result[0] ); 711 $this->assertSame( 2, bbp_get_user_reply_count( $user_id, true ) ); 712 } 713 714 /** 715 * @covers ::bbp_admin_repair_user_topic_count 716 * @covers ::bbp_admin_repair_user_reply_count 717 * @ticket BBP2664 718 */ 719 public function test_user_count_repairs_clear_stale_counts_without_public_posts() { 720 $user_id = $this->factory->user->create(); 721 722 bbp_update_user_topic_count( $user_id, 7 ); 723 bbp_update_user_reply_count( $user_id, 8 ); 724 $this->assertSame( 7, bbp_get_user_topic_count( $user_id, true ) ); 725 $this->assertSame( 8, bbp_get_user_reply_count( $user_id, true ) ); 726 727 $topic_result = bbp_admin_repair_user_topic_count(); 728 $reply_result = bbp_admin_repair_user_reply_count(); 729 730 $this->assertSame( 0, $topic_result[0] ); 731 $this->assertSame( 0, $reply_result[0] ); 732 $this->assertSame( 0, bbp_get_user_topic_count( $user_id, true ) ); 733 $this->assertSame( 0, bbp_get_user_reply_count( $user_id, true ) ); 533 734 } 534 735 -
trunk/tests/phpunit/testcases/forums/functions/counts.php
r7449 r7453 11 11 12 12 /** 13 * @covers ::bbp_get_countable_forum_statuses 14 */ 15 public function test_bbp_get_countable_forum_statuses() { 16 $include_private = function ( $statuses ) { 17 $statuses[] = bbp_get_private_status_id(); 18 return $statuses; 19 }; 20 21 add_filter( 'bbp_get_public_forum_statuses', $include_private ); 22 $statuses = bbp_get_countable_forum_statuses(); 23 remove_filter( 'bbp_get_public_forum_statuses', $include_private ); 24 25 $this->assertSame( 26 array( 27 bbp_get_public_status_id(), 28 bbp_get_private_status_id(), 29 bbp_get_hidden_status_id() 30 ), 31 $statuses 32 ); 33 } 34 35 /** 13 36 * Generic function to test the forum counts with a new topic 14 37 * … … 417 440 /** 418 441 * @covers ::bbp_update_forum_subforum_count 442 * @ticket BBP2649 419 443 */ 420 444 public function test_bbp_update_forum_subforum_count() { … … 440 464 $count = bbp_get_forum_subforum_count( $f1, true ); 441 465 $this->assertSame( 3, $count ); 466 } 467 468 /** 469 * @covers ::bbp_update_forum_subforum_count 470 * @ticket BBP2649 471 */ 472 public function test_bbp_update_forum_subforum_count_includes_supported_visibilities() { 473 $forum_id = $this->factory->forum->create(); 474 475 foreach ( array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() ) as $status ) { 476 $subforum_id = $this->factory->forum->create( array( 477 'post_parent' => $forum_id, 478 'post_status' => $status, 479 ) ); 480 } 481 482 // Count the canonical hierarchy once, even if relationship meta is duplicated 483 add_post_meta( $subforum_id, '_bbp_forum_id', $forum_id ); 484 485 $trashed_id = $this->factory->forum->create( array( 'post_parent' => $forum_id ) ); 486 wp_trash_post( $trashed_id ); 487 488 bbp_update_forum_subforum_count( $forum_id ); 489 490 $this->assertSame( 3, bbp_get_forum_subforum_count( $forum_id, true ) ); 491 } 492 493 /** 494 * @covers ::bbp_update_parent_forum_subforum_count 495 * @covers ::bbp_update_forum_subforum_count_on_transition_post_status 496 * @ticket BBP2649 497 */ 498 public function test_subforum_count_tracks_trash_restore_and_deletion() { 499 $forum_id = $this->factory->forum->create(); 500 $subforum_id = $this->factory->forum->create( array( 'post_parent' => $forum_id ) ); 501 502 $this->assertSame( 1, bbp_get_forum_subforum_count( $forum_id, true ) ); 503 504 wp_trash_post( $subforum_id ); 505 $this->assertSame( 0, bbp_get_forum_subforum_count( $forum_id, true ) ); 506 507 wp_untrash_post( $subforum_id ); 508 $this->assertSame( 1, bbp_get_forum_subforum_count( $forum_id, true ) ); 509 510 wp_update_post( array( 511 'ID' => $subforum_id, 512 'post_status' => 'draft', 513 ) ); 514 $this->assertSame( 0, bbp_get_forum_subforum_count( $forum_id, true ) ); 515 516 wp_publish_post( $subforum_id ); 517 $this->assertSame( 1, bbp_get_forum_subforum_count( $forum_id, true ) ); 518 519 wp_delete_post( $subforum_id, true ); 520 $this->assertSame( 0, bbp_get_forum_subforum_count( $forum_id, true ) ); 521 } 522 523 /** 524 * @covers ::bbp_reparent_forum_subforums 525 * @covers ::bbp_update_parent_forum_subforum_count 526 * @ticket BBP2649 527 */ 528 public function test_deleting_forum_reparents_child_forum_metadata_and_count() { 529 $grandparent_id = $this->factory->forum->create(); 530 $parent_id = $this->factory->forum->create( array( 'post_parent' => $grandparent_id ) ); 531 $child_id = $this->factory->forum->create( array( 'post_parent' => $parent_id ) ); 532 533 wp_delete_post( $parent_id, true ); 534 535 $this->assertSame( $grandparent_id, (int) get_post_field( 'post_parent', $child_id ) ); 536 $this->assertSame( $grandparent_id, bbp_get_forum_parent_id( $child_id ) ); 537 $this->assertSame( 1, bbp_get_forum_subforum_count( $grandparent_id, true ) ); 538 } 539 540 /** 541 * @covers ::bbp_update_forum_subforum_counts_on_post_updated 542 * @ticket BBP2649 543 */ 544 public function test_subforum_count_tracks_parent_changes() { 545 $old_parent_id = $this->factory->forum->create(); 546 $new_parent_id = $this->factory->forum->create(); 547 $subforum_id = $this->factory->forum->create( array( 'post_parent' => $old_parent_id ) ); 548 549 $this->assertSame( 1, bbp_get_forum_subforum_count( $old_parent_id, true ) ); 550 $this->assertSame( 0, bbp_get_forum_subforum_count( $new_parent_id, true ) ); 551 552 wp_update_post( array( 553 'ID' => $subforum_id, 554 'post_parent' => $new_parent_id, 555 ) ); 556 557 $this->assertSame( 0, bbp_get_forum_subforum_count( $old_parent_id, true ) ); 558 $this->assertSame( 1, bbp_get_forum_subforum_count( $new_parent_id, true ) ); 559 } 560 561 /** 562 * @covers ::bbp_update_forum_subforum_counts_on_post_updated 563 * @ticket BBP2649 564 */ 565 public function test_subforum_count_tracks_post_type_changes() { 566 $parent_id = $this->factory->forum->create(); 567 $subforum_id = $this->factory->forum->create( array( 'post_parent' => $parent_id ) ); 568 569 $this->assertSame( 1, bbp_get_forum_subforum_count( $parent_id, true ) ); 570 571 wp_update_post( array( 572 'ID' => $subforum_id, 573 'post_type' => 'post', 574 ) ); 575 $this->assertSame( 0, bbp_get_forum_subforum_count( $parent_id, true ) ); 576 577 wp_update_post( array( 578 'ID' => $subforum_id, 579 'post_type' => bbp_get_forum_post_type(), 580 ) ); 581 $this->assertSame( $parent_id, bbp_get_forum_parent_id( $subforum_id ) ); 582 $this->assertSame( 1, bbp_get_forum_subforum_count( $parent_id, true ) ); 442 583 } 443 584
Note:
See TracChangeset
for help on using the changeset viewer.