Changeset 7468
- Timestamp:
- 09/10/2026 12:36:51 AM (2 months ago)
- Location:
- branches/2.6
- Files:
-
- 1 added
- 22 edited
-
CHANGELOG.md (modified) (1 diff)
-
src/bbpress.php (modified) (1 diff)
-
src/includes/admin/tools/repair.php (modified) (1 diff)
-
src/includes/common/engagements.php (modified) (2 diffs)
-
src/includes/common/functions.php (modified) (5 diffs)
-
src/includes/core/abstraction.php (modified) (1 diff)
-
src/includes/core/actions.php (modified) (1 diff)
-
src/includes/forums/functions.php (modified) (23 diffs)
-
src/includes/replies/functions.php (modified) (3 diffs)
-
src/includes/topics/functions.php (modified) (11 diffs)
-
src/includes/users/engagements.php (modified) (2 diffs)
-
src/includes/users/functions.php (modified) (2 diffs)
-
src/includes/users/options.php (modified) (6 diffs)
-
src/readme.txt (modified) (1 diff)
-
tests/phpunit/testcases/admin/tools.php (modified) (5 diffs)
-
tests/phpunit/testcases/common/engagements.php (added)
-
tests/phpunit/testcases/core/abstraction.php (modified) (1 diff)
-
tests/phpunit/testcases/forums/functions/counts.php (modified) (6 diffs)
-
tests/phpunit/testcases/forums/functions/query.php (modified) (1 diff)
-
tests/phpunit/testcases/replies/functions/reply.php (modified) (1 diff)
-
tests/phpunit/testcases/topics/functions/counts.php (modified) (3 diffs)
-
tests/phpunit/testcases/topics/functions/topic.php (modified) (2 diffs)
-
tests/phpunit/testcases/users/functions/counts.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/2.6/CHANGELOG.md
r7466 r7468 40 40 - Kept public, hidden, and author contribution counts accurate when topics and 41 41 replies change moderation status or are permanently deleted. 42 - Kept subforum counts accurate across public, private, hidden, trashed, moved, 43 restored, and permanently deleted forums. 42 - Prevented simultaneous topic and reply submissions from overwriting each 43 other's existing stored count changes. 44 - Kept forum, ancestor, contribution, engagement, and voice counts synchronized 45 when moderators move replies or merge and split topics. 46 - Reconciled replacement-user contribution, engagement, and voice counts when 47 a deleted user's posts are reassigned. 48 - Excluded replies beneath non-public topics from forum reply counts during 49 moderation and repair recounts. 50 - Kept subforum counts and recursive forum aggregates accurate across public, 51 private, hidden, trashed, moved, restored, and permanently deleted forums. 44 52 - Corrected count repair tools to clear stale metadata caches, exclude anonymous 45 53 authors from voice counts, honor reply-status filters, rebuild private and 46 54 hidden forums, and preserve similarly named metadata on other post types. 55 - Made topic engagement and voice recounts honor filtered public reply statuses. 56 - Preserved term-backed favorites and subscriptions during engagement recounts. 47 57 - Prevented BuddyPress integration from loading bbPress translations before 48 58 WordPress initialization. -
branches/2.6/src/bbpress.php
r7436 r7468 832 832 833 833 // Counts 834 register_meta( 'post', '_bbp_topic_count', $count ); 835 register_meta( 'post', '_bbp_reply_count', $count ); 836 register_meta( 'post', '_bbp_total_topic_count', $count ); 837 register_meta( 'post', '_bbp_total_reply_count', $count ); 838 register_meta( 'post', '_bbp_voice_count', $count ); 839 register_meta( 'post', '_bbp_anonymous_reply_count', $count ); 840 register_meta( 'post', '_bbp_topic_count_hidden', $count ); 841 register_meta( 'post', '_bbp_reply_count_hidden', $count ); 842 register_meta( 'post', '_bbp_forum_subforum_count', $count ); 834 register_meta( 'post', '_bbp_topic_count', $count ); 835 register_meta( 'post', '_bbp_reply_count', $count ); 836 register_meta( 'post', '_bbp_total_topic_count', $count ); 837 register_meta( 'post', '_bbp_total_reply_count', $count ); 838 register_meta( 'post', '_bbp_voice_count', $count ); 839 register_meta( 'post', '_bbp_anonymous_reply_count', $count ); 840 register_meta( 'post', '_bbp_topic_count_hidden', $count ); 841 register_meta( 'post', '_bbp_reply_count_hidden', $count ); 842 register_meta( 'post', '_bbp_total_topic_count_hidden', $count ); 843 register_meta( 'post', '_bbp_total_reply_count_hidden', $count ); 844 register_meta( 'post', '_bbp_forum_subforum_count', $count ); 843 845 844 846 /** User **************************************************************/ -
branches/2.6/src/includes/admin/tools/repair.php
r7454 r7468 427 427 LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id` 428 428 WHERE `posts`.`post_type` = '{$fpt}' 429 AND `postmeta`.`meta_key` IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )";429 AND `postmeta`.`meta_key` IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden', '_bbp_total_topic_count_hidden' )"; 430 430 if ( false === $bbp_db->query( $sql_delete ) ) { 431 431 return array( 1, sprintf( $statement, $result ) ); -
branches/2.6/src/includes/common/engagements.php
r7436 r7468 430 430 * 431 431 * @since 2.6.0 bbPress (r6737) 432 * @since 2.6.16 Limit removal to the requested relationship taxonomy. 432 433 * 433 434 * @param int $object_id The object id … … 439 440 */ 440 441 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) { 441 return wp_delete_object_term_relationships( $object_id, get_object_taxonomies( 'bbp_user' ) ); 442 $tax_key = "{$meta_key}_{$meta_type}"; 443 $this->jit_taxonomy( $tax_key ); 444 445 return wp_delete_object_term_relationships( $object_id, $tax_key ); 442 446 } 443 447 -
branches/2.6/src/includes/common/functions.php
r7451 r7468 244 244 $was_public = in_array( $old_status, bbp_get_public_topic_statuses(), true ); 245 245 $is_public = in_array( $new_status, bbp_get_public_topic_statuses(), true ); 246 $was_hidden = in_array( $old_status, bbp_get_non_public_topic_statuses(), true ); 247 $is_hidden = in_array( $new_status, bbp_get_non_public_topic_statuses(), true ); 246 248 $public_difference = (int) $is_public - (int) $was_public; 247 $hidden_difference = $is_new 248 ? (int) ! $is_public 249 : - $public_difference; 250 251 // A new topic or public boundary crossing changes at least one count 249 $hidden_difference = (int) $is_hidden - (int) $was_hidden; 250 251 // A new topic or count boundary crossing changes at least one count 252 252 if ( ! empty( $public_difference ) || ! empty( $hidden_difference ) ) { 253 253 $forum_id = $is_new … … 270 270 } 271 271 272 // Topic approval does not change its replies' statuses 273 if ( ! $is_new && in_array( bbp_get_pending_status_id(), array( $old_status, $new_status ), true ) ) { 274 $reply_count = bbp_get_public_child_count( $post->ID, bbp_get_reply_post_type() ); 275 bbp_bump_forum_reply_count( $forum_id, $reply_count * $public_difference ); 272 // Recount replies after their parent topic crosses the public boundary 273 if ( ! $is_new && ! empty( $forum_id ) && ! empty( $public_difference ) ) { 274 bbp_update_forum_reply_count( $forum_id, true ); 276 275 } 277 276 } … … 281 280 $was_public = in_array( $old_status, bbp_get_public_reply_statuses(), true ); 282 281 $is_public = in_array( $new_status, bbp_get_public_reply_statuses(), true ); 282 $was_hidden = in_array( $old_status, bbp_get_non_public_reply_statuses(), true ); 283 $is_hidden = in_array( $new_status, bbp_get_non_public_reply_statuses(), true ); 283 284 $public_difference = (int) $is_public - (int) $was_public; 284 $hidden_difference = $is_new 285 ? (int) ! $is_public 286 : - $public_difference; 287 288 // A new reply or public boundary crossing changes at least one count 285 $hidden_difference = (int) $is_hidden - (int) $was_hidden; 286 287 // A new reply or count boundary crossing changes at least one count 289 288 if ( ! empty( $public_difference ) || ! empty( $hidden_difference ) ) { 290 289 $topic_id = $is_new … … 294 293 ? bbp_get_topic_forum_id( $topic_id ) 295 294 : bbp_get_reply_forum_id( $post->ID ); 295 $forum_public_difference = bbp_is_topic_public( $topic_id ) 296 ? $public_difference 297 : 0; 296 298 297 299 // Update the topic's public reply count … … 306 308 307 309 // Update the forum's public reply count 308 if ( ! empty( $forum_id ) && ! empty( $ public_difference ) ) {309 bbp_bump_forum_reply_count( $forum_id, $ public_difference );310 if ( ! empty( $forum_id ) && ! empty( $forum_public_difference ) ) { 311 bbp_bump_forum_reply_count( $forum_id, $forum_public_difference ); 310 312 } 311 313 -
branches/2.6/src/includes/core/abstraction.php
r7436 r7468 147 147 function bbp_db() { 148 148 return bbp_get_global_object( 'wpdb', 'WPDB' ); 149 } 150 151 /** 152 * Atomically bump a numeric metadata value using compare-and-swap retries. 153 * 154 * Count updates normally require reading a value, changing it in PHP, and 155 * writing it back. Two requests can read the same value and overwrite one 156 * another's changes. This function avoids that lost update by making the write 157 * conditional on the value that was read. If another request changes the value 158 * first, the condition matches no rows, so the current value is read directly 159 * from the database and the calculation is retried. 160 * 161 * The first read uses the WordPress metadata API and its cache. Missing 162 * metadata is added through add_metadata() so its standard lifecycle continues 163 * to run. Existing metadata uses a conditional database update while preserving 164 * the standard update metadata short-circuit filter and before/after actions. 165 * Before actions run for every conditional attempt, while after actions run 166 * only after a successful write. Metadata caches are cleared between attempts 167 * and after successful writes. Values are sanitized through sanitize_meta(), 168 * cast to integers, and prevented from falling below zero. 169 * 170 * Retries are bounded and filterable. This function does not lock rows or hold 171 * a database transaction open, and returns false when there is no change, a 172 * database operation fails, or all attempts lose to concurrent writes. It is 173 * intended for uniquely keyed numeric count metadata; WordPress metadata tables 174 * do not enforce uniqueness during simultaneous first-time inserts. 175 * 176 * @since 2.6.16 177 * 178 * @see https://bbpress.trac.wordpress.org/ticket/3678 179 * 180 * @param string $meta_type Type of object metadata is for. 181 * @param int $object_id ID of the object metadata is for. 182 * @param string $meta_key Metadata key. 183 * @param int $difference Amount to add to the stored value. 184 * @param int $default Existing value to use when metadata is missing. 185 * @return bool True on success, false on failure or no change. 186 */ 187 function bbp_bump_count_meta( $meta_type = '', $object_id = 0, $meta_key = '', $difference = 1, $default = 0 ) { 188 189 $object_id = (int) $object_id; 190 $difference = (int) $difference; 191 $default = (int) $default; 192 193 // Bail if required values are missing 194 if ( empty( $object_id ) || empty( $meta_key ) || empty( $difference ) ) { 195 return false; 196 } 197 198 /** 199 * Short-circuits bumping numeric metadata. 200 * 201 * Returning a non-null value prevents the normal metadata update. 202 * 203 * @since 2.6.16 204 * 205 * @param null|bool $check Whether to short-circuit the metadata update. 206 * @param string $meta_type Type of object metadata is for. 207 * @param int $object_id ID of the object metadata is for. 208 * @param string $meta_key Metadata key. 209 * @param int $difference Amount to add to the stored value. 210 * @param int $default Existing value to use when metadata is missing. 211 */ 212 $check = apply_filters( 'bbp_pre_bump_count_meta', null, $meta_type, $object_id, $meta_key, $difference, $default ); 213 if ( null !== $check ) { 214 return (bool) $check; 215 } 216 217 /** 218 * Filters the metadata types that support atomic count updates. 219 * 220 * @since 2.6.16 221 * 222 * @param array $meta_types Supported metadata types. 223 * @param string $meta_type Requested metadata type. 224 * @param int $object_id ID of the object metadata is for. 225 * @param string $meta_key Metadata key. 226 */ 227 $meta_types = (array) apply_filters( 'bbp_bump_count_meta_types', array( 'post', 'user', 'term', 'comment' ), $meta_type, $object_id, $meta_key ); 228 229 // Bail if the metadata type is unsupported 230 if ( ! in_array( $meta_type, $meta_types, true ) ) { 231 return false; 232 } 233 234 $bbp_db = bbp_db(); 235 $table_name = sanitize_key( $meta_type . 'meta' ); 236 $table = isset( $bbp_db->{$table_name} ) ? $bbp_db->{$table_name} : ''; 237 $column = sanitize_key( $meta_type . '_id' ); 238 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 239 240 // Bail if the metadata table does not exist 241 if ( empty( $table ) ) { 242 return false; 243 } 244 245 /** 246 * Filters the maximum number of conditional metadata write attempts. 247 * 248 * @since 2.6.16 249 * 250 * @param int $max_attempts Maximum number of attempts. 251 * @param string $meta_type Type of object metadata is for. 252 * @param int $object_id ID of the object metadata is for. 253 * @param string $meta_key Metadata key. 254 * @param int $difference Amount to add to the stored value. 255 * @param int $default Existing value to use when metadata is missing. 256 */ 257 $max_attempts = (int) apply_filters( 'bbp_bump_count_meta_max_attempts', 5, $meta_type, $object_id, $meta_key, $difference, $default ); 258 $max_attempts = max( 1, $max_attempts ); 259 $checked = false; 260 $subtype = get_object_subtype( $meta_type, $object_id ); 261 $count_query = $bbp_db->prepare( "SELECT meta_value FROM {$table} WHERE meta_key = %s AND {$column} = %d LIMIT 1", $meta_key, $object_id ); 262 263 // Retry when another request updates the same value first 264 for ( $attempt = 0; $attempt < $max_attempts; $attempt++ ) { 265 if ( empty( $attempt ) ) { 266 $exists = metadata_exists( $meta_type, $object_id, $meta_key ); 267 $count = $exists 268 ? (int) get_metadata( $meta_type, $object_id, $meta_key, true ) 269 : $default; 270 } else { 271 $stored = $bbp_db->get_var( $count_query ); 272 273 // Bail on a database error 274 if ( ! empty( $bbp_db->last_error ) ) { 275 return false; 276 } 277 278 $exists = null !== $stored; 279 $count = $exists ? (int) $stored : $default; 280 } 281 282 $new_count = sanitize_meta( $meta_key, bbp_number_not_negative( $count + $difference ), $meta_type, $subtype ); 283 $new_count = (int) $new_count; 284 285 // Allow metadata updates to be short-circuited as usual 286 if ( ! $checked ) { 287 $checked = true; 288 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $new_count, '' ); 289 290 if ( null !== $check ) { 291 return (bool) $check; 292 } 293 } 294 295 // Bail if the count is already at its lower bound 296 if ( $new_count === $count ) { 297 if ( ! empty( $attempt ) ) { 298 return false; 299 } 300 301 $stored = $bbp_db->get_var( $count_query ); 302 303 // Bail on a database error 304 if ( ! empty( $bbp_db->last_error ) ) { 305 return false; 306 } 307 308 $current_exists = null !== $stored; 309 $current_count = $current_exists ? (int) $stored : $default; 310 311 if ( ( $current_exists === $exists ) && ( $current_count === $count ) ) { 312 return false; 313 } 314 315 wp_cache_delete( $object_id, $meta_type . '_meta' ); 316 continue; 317 } 318 319 // Add missing metadata using the standard WordPress lifecycle 320 if ( ! $exists ) { 321 if ( ! empty( add_metadata( $meta_type, $object_id, $meta_key, $new_count, true ) ) ) { 322 return true; 323 } 324 325 $stored = $bbp_db->get_var( $count_query ); 326 327 // Bail if the add failed without a concurrent insert or on a database error 328 if ( ! empty( $bbp_db->last_error ) || ( null === $stored ) ) { 329 return false; 330 } 331 332 wp_cache_delete( $object_id, $meta_type . '_meta' ); 333 continue; 334 } 335 336 $meta_ids = $bbp_db->get_col( $bbp_db->prepare( "SELECT {$id_column} FROM {$table} WHERE meta_key = %s AND {$column} = %d", $meta_key, $object_id ) ); 337 338 // Bail on a database error 339 if ( ! empty( $bbp_db->last_error ) ) { 340 return false; 341 } 342 343 // Retry if metadata was removed after the cached existence check 344 if ( empty( $meta_ids ) ) { 345 wp_cache_delete( $object_id, $meta_type . '_meta' ); 346 continue; 347 } 348 349 // Run the standard actions immediately before the conditional update 350 foreach ( $meta_ids as $meta_id ) { 351 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $new_count ); 352 353 if ( 'post' === $meta_type ) { 354 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $new_count ); 355 } 356 } 357 358 // Compare count metadata numerically to normalize stored numeric strings 359 $updated = $bbp_db->update( 360 $table, 361 array( 'meta_value' => $new_count ), 362 array( 363 $column => $object_id, 364 'meta_key' => $meta_key, 365 'meta_value' => $count 366 ), 367 array( '%d' ), 368 array( '%d', '%s', '%d' ) 369 ); 370 371 // Bail on a database error 372 if ( false === $updated ) { 373 return false; 374 } 375 376 wp_cache_delete( $object_id, $meta_type . '_meta' ); 377 378 // Retry when another request updated the count first 379 if ( empty( $updated ) ) { 380 continue; 381 } 382 383 // Run the standard actions immediately after the conditional update 384 foreach ( $meta_ids as $meta_id ) { 385 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $new_count ); 386 387 if ( 'post' === $meta_type ) { 388 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $new_count ); 389 } 390 } 391 392 return true; 393 } 394 395 return false; 149 396 } 150 397 -
branches/2.6/src/includes/core/actions.php
r7454 r7468 286 286 add_action( 'bbp_deleted_topic', 'bbp_decrease_user_topic_count' ); 287 287 add_action( 'bbp_deleted_reply', 'bbp_decrease_user_reply_count' ); 288 add_action( 'bbp_post_updated', 'bbp_update_counts_on_post_author_change', 10, 3 ); 289 add_action( 'bbp_post_updated', 'bbp_recalculate_engagements_on_post_author_change', 20, 3 ); 290 291 // Update counts and engagements after WordPress reassigns a deleted user's posts 292 add_action( 'delete_user', 'bbp_update_counts_on_user_reassignment', 10, 2 ); 293 add_action( 'deleted_user', 'bbp_update_counts_on_user_reassignment', 10, 2 ); 288 294 289 295 // Topic status transition helpers for replies -
branches/2.6/src/includes/forums/functions.php
r7456 r7468 1116 1116 * 1117 1117 * @since 2.1.0 bbPress (r3825) 1118 * @since 2.6.16 Use atomic metadata writes and non-negative counts. 1118 1119 * 1119 1120 * @param int $forum_id Optional. Forum id. … … 1137 1138 1138 1139 // Update this forum id 1139 update_post_meta( $forum_id, '_bbp_topic_count', (int) ( $topic_count + $difference ));1140 update_post_meta( $forum_id, '_bbp_total_topic_count', (int) ( $total_topic_count + $difference ));1140 bbp_bump_count_meta( 'post', $forum_id, '_bbp_topic_count', $difference, $topic_count ); 1141 bbp_bump_count_meta( 'post', $forum_id, '_bbp_total_topic_count', $difference, $total_topic_count ); 1141 1142 1142 1143 // Check for ancestors … … 1155 1156 1156 1157 $parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false, true ); 1157 update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) ( $parent_topic_count + $difference ));1158 bbp_bump_count_meta( 'post', $parent_forum_id, '_bbp_topic_count', $difference, $parent_topic_count ); 1158 1159 } 1159 1160 1160 1161 // Update the total topic count. 1161 1162 $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true, true ); 1162 update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) ( $parent_total_topic_count + $difference ));1163 bbp_bump_count_meta( 'post', $parent_forum_id, '_bbp_total_topic_count', $difference, $parent_total_topic_count ); 1163 1164 } 1164 1165 } 1165 1166 } 1166 1167 1167 $forum_topic_count = (int)( $total_topic_count + $difference );1168 $forum_topic_count = bbp_number_not_negative( $total_topic_count + $difference ); 1168 1169 1169 1170 // Filter & return … … 1238 1239 * 1239 1240 * @since 2.1.0 bbPress (r3825) 1241 * @since 2.6.16 Use atomic metadata writes and non-negative counts. 1240 1242 * 1241 1243 * @param int $forum_id Optional. Forum id. … … 1259 1261 1260 1262 // Update this forum id 1261 update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) ( $reply_count + $difference ));1262 update_post_meta( $forum_id, '_bbp_total_topic_count_hidden', (int) ( $total_topic_count + $difference ));1263 bbp_bump_count_meta( 'post', $forum_id, '_bbp_topic_count_hidden', $difference, $reply_count ); 1264 bbp_bump_count_meta( 'post', $forum_id, '_bbp_total_topic_count_hidden', $difference, $total_topic_count ); 1263 1265 1264 1266 // Check for ancestors … … 1277 1279 1278 1280 $parent_topic_count = bbp_get_forum_topic_count_hidden( $parent_forum_id, false, true ); 1279 update_post_meta( $parent_forum_id, '_bbp_topic_count_hidden', (int) ( $parent_topic_count + $difference ));1281 bbp_bump_count_meta( 'post', $parent_forum_id, '_bbp_topic_count_hidden', $difference, $parent_topic_count ); 1280 1282 } 1281 1283 1282 1284 // Update the total topic count. 1283 1285 $parent_total_topic_count = bbp_get_forum_topic_count_hidden( $parent_forum_id, true, true ); 1284 update_post_meta( $parent_forum_id, '_bbp_total_topic_count_hidden', (int) ( $parent_total_topic_count + $difference ));1286 bbp_bump_count_meta( 'post', $parent_forum_id, '_bbp_total_topic_count_hidden', $difference, $parent_total_topic_count ); 1285 1287 } 1286 1288 } 1287 1289 } 1288 1290 1289 $forum_topic_count = (int)( $total_topic_count + $difference );1291 $forum_topic_count = bbp_number_not_negative( $total_topic_count + $difference ); 1290 1292 1291 1293 // Filter & return … … 1361 1363 * 1362 1364 * @since 2.1.0 bbPress (r3825) 1365 * @since 2.6.16 Use atomic metadata writes and non-negative counts. 1363 1366 * 1364 1367 * @param int $forum_id Optional. Forum id. … … 1382 1385 1383 1386 // Update this forum id 1384 update_post_meta( $forum_id, '_bbp_reply_count', (int) ( $reply_count + $difference ));1385 update_post_meta( $forum_id, '_bbp_total_reply_count', (int) ( $total_reply_count + $difference ));1387 bbp_bump_count_meta( 'post', $forum_id, '_bbp_reply_count', $difference, $reply_count ); 1388 bbp_bump_count_meta( 'post', $forum_id, '_bbp_total_reply_count', $difference, $total_reply_count ); 1386 1389 1387 1390 // Check for ancestors … … 1400 1403 1401 1404 $parent_reply_count = bbp_get_forum_reply_count( $parent_forum_id, false, true ); 1402 update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) ( $parent_reply_count + $difference ));1405 bbp_bump_count_meta( 'post', $parent_forum_id, '_bbp_reply_count', $difference, $parent_reply_count ); 1403 1406 } 1404 1407 1405 1408 // Update the total reply count. 1406 1409 $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true, true ); 1407 update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) ( $parent_total_reply_count + $difference ));1410 bbp_bump_count_meta( 'post', $parent_forum_id, '_bbp_total_reply_count', $difference, $parent_total_reply_count ); 1408 1411 } 1409 1412 } 1410 1413 } 1411 1414 1412 $forum_reply_count = (int)( $total_reply_count + $difference );1415 $forum_reply_count = bbp_number_not_negative( $total_reply_count + $difference ); 1413 1416 1414 1417 // Filter & return … … 1420 1423 * 1421 1424 * @since 2.6.0 bbPress (r6922) 1425 * @since 2.6.16 Use atomic metadata writes and non-negative counts. 1422 1426 * 1423 1427 * @param int $forum_id Optional. Forum id. … … 1441 1445 1442 1446 // Update this forum id 1443 update_post_meta( $forum_id, '_bbp_reply_count_hidden', (int) ( $reply_count + $difference ));1444 update_post_meta( $forum_id, '_bbp_total_reply_count_hidden', (int) ( $total_reply_count + $difference ));1447 bbp_bump_count_meta( 'post', $forum_id, '_bbp_reply_count_hidden', $difference, $reply_count ); 1448 bbp_bump_count_meta( 'post', $forum_id, '_bbp_total_reply_count_hidden', $difference, $total_reply_count ); 1445 1449 1446 1450 // Check for ancestors … … 1459 1463 1460 1464 $parent_reply_count = bbp_get_forum_reply_count_hidden( $parent_forum_id, false, true ); 1461 update_post_meta( $parent_forum_id, '_bbp_reply_count_hidden', (int) ( $parent_reply_count + $difference ));1465 bbp_bump_count_meta( 'post', $parent_forum_id, '_bbp_reply_count_hidden', $difference, $parent_reply_count ); 1462 1466 } 1463 1467 1464 1468 // Update the total reply count. 1465 1469 $parent_total_reply_count = bbp_get_forum_reply_count_hidden( $parent_forum_id, true, true ); 1466 update_post_meta( $parent_forum_id, '_bbp_total_reply_count_hidden', (int) ( $parent_total_reply_count + $difference ));1470 bbp_bump_count_meta( 'post', $parent_forum_id, '_bbp_total_reply_count_hidden', $difference, $parent_total_reply_count ); 1467 1471 } 1468 1472 } 1469 1473 } 1470 1474 1471 $forum_reply_count = (int)( $total_reply_count + $difference );1475 $forum_reply_count = bbp_number_not_negative( $total_reply_count + $difference ); 1472 1476 1473 1477 // Filter & return 1474 1478 return (int) apply_filters( 'bbp_bump_forum_reply_count_hidden', $forum_reply_count, $forum_id, $difference, $update_ancestors ); 1479 } 1480 1481 /** 1482 * Bump one total count through a forum's ancestors. 1483 * 1484 * This is used after recounting a forum that may be nested as a subforum. The 1485 * starting forum's total has already been updated, so only its parent forums 1486 * receive the difference between the old and new totals. The supplied metadata 1487 * key identifies which total topic or reply count is propagated. 1488 * 1489 * Forum hierarchy is stored in `post_parent`, making get_post_ancestors() the 1490 * canonical, cache-aware way to walk from a subforum toward its root forum. 1491 * Only parent forums are traversed: topics and replies are never ancestors in 1492 * a valid forum hierarchy, even when the metadata key stores their totals. The 1493 * walk stops if a malformed parent relationship leaves the forum post type. 1494 * 1495 * @since 2.6.16 1496 * 1497 * @param int $forum_id Starting forum ID. Its own count is not changed. 1498 * @param string $meta_key Topic or reply total count metadata key. 1499 * @param int $difference Amount to add to the stored value. 1500 * @return bool True when all ancestor counts were updated, false otherwise. 1501 */ 1502 function bbp_bump_forum_ancestor_count( $forum_id = 0, $meta_key = '', $difference = 0 ) { 1503 $forum_id = bbp_get_forum_id( $forum_id ); 1504 $difference = (int) $difference; 1505 1506 // Bail if nothing can change 1507 if ( empty( $forum_id ) || empty( $meta_key ) || empty( $difference ) ) { 1508 return false; 1509 } 1510 1511 $updated = true; 1512 $ancestor_ids = get_post_ancestors( $forum_id ); 1513 1514 // Return if this forum has no ancestors 1515 if ( empty( $ancestor_ids ) ) { 1516 return $updated; 1517 } 1518 1519 // Update only total counts on ancestor forums 1520 foreach ( $ancestor_ids as $ancestor_id ) { 1521 1522 // Stop if malformed data leaves the subforum hierarchy 1523 if ( ! bbp_is_forum( $ancestor_id ) ) { 1524 break; 1525 } 1526 1527 $count = (int) get_post_meta( $ancestor_id, $meta_key, true ); 1528 1529 if ( ! bbp_bump_count_meta( 'post', $ancestor_id, $meta_key, $difference, $count ) ) { 1530 $updated = false; 1531 } 1532 } 1533 1534 return $updated; 1475 1535 } 1476 1536 … … 1998 2058 /** 1999 2059 * Adjust the total topic count of a forum. 2000 *2060 * 2001 2061 * @since 2.0.0 bbPress (r2464) 2062 * @since 2.6.16 Optionally update ancestor forum totals. 2002 2063 * 2003 2064 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it 2004 2065 * is a topic or a forum. If it's a topic, its parent, 2005 2066 * i.e. the forum is automatically retrieved. 2006 * @param bool $total_count Optional. To return the total count or normal count? 2007 * @return int Forum topic count 2008 */ 2009 function bbp_update_forum_topic_count( $forum_id = 0 ) { 2010 $forum_id = bbp_get_forum_id( $forum_id ); 2067 * @param bool $update_ancestors Optional. Whether to update ancestor totals. 2068 * @return int Forum topic count. 2069 */ 2070 function bbp_update_forum_topic_count( $forum_id = 0, $update_ancestors = false ) { 2071 $forum_id = bbp_get_forum_id( $forum_id ); 2072 $old_total_topics = ( true === $update_ancestors ) 2073 ? (int) get_post_meta( $forum_id, '_bbp_total_topic_count', true ) 2074 : 0; 2011 2075 $children_topic_count = 0; 2076 $total_topics = 0; 2012 2077 2013 2078 // Loop through subforums and add together forum topic counts … … 2029 2094 update_post_meta( $forum_id, '_bbp_total_topic_count', $total_topics ); 2030 2095 2096 // Update ancestor total counts by the persisted difference 2097 if ( true === $update_ancestors ) { 2098 bbp_bump_forum_ancestor_count( $forum_id, '_bbp_total_topic_count', $total_topics - $old_total_topics ); 2099 } 2100 2031 2101 // Filter & return 2032 2102 return (int) apply_filters( 'bbp_update_forum_topic_count', $total_topics, $forum_id ); … … 2035 2105 /** 2036 2106 * Adjust the total hidden topic count of a forum (hidden includes trashed, 2037 * spammed and pending topics) 2107 * spammed and pending topics). 2038 2108 * 2039 2109 * @since 2.0.0 bbPress (r2888) 2040 2110 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects 2111 * @since 2.6.16 Optionally update ancestor forum totals. 2041 2112 * 2042 2113 * @param int $forum_id Optional. Topic id to update. 2043 2114 * @param int $topic_count Optional. Set the topic count manually. 2044 * 2045 * @return int Topic hidden topic count 2046 */ 2047 function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = false ) { 2115 * @param bool $update_ancestors Optional. Whether to update ancestor totals. 2116 * 2117 * @return int Topic hidden topic count. 2118 */ 2119 function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = false, $update_ancestors = false ) { 2048 2120 2049 2121 // If topic_id was passed as $forum_id, then get its forum … … 2057 2129 } 2058 2130 2131 $children_topic_count = 0; 2132 $total_topics = 0; 2133 $old_total_topics = ( true === $update_ancestors ) 2134 ? (int) get_post_meta( $forum_id, '_bbp_total_topic_count_hidden', true ) 2135 : 0; 2136 2059 2137 // Can't update what isn't there 2060 2138 if ( ! empty( $forum_id ) ) { 2139 2140 // Loop through children and add together hidden topic counts 2141 $children = bbp_forum_query_subforum_ids( $forum_id ); 2142 if ( ! empty( $children ) ) { 2143 foreach ( (array) $children as $child ) { 2144 bbp_update_forum_topic_count_hidden( $child ); 2145 $children_topic_count += bbp_get_forum_topic_count_hidden( $child, true, true ); 2146 } 2147 } 2061 2148 2062 2149 // Get topics of forum … … 2085 2172 } 2086 2173 2087 $topic_count = (int) $topic_count; 2088 2089 // Update the count 2090 update_post_meta( $forum_id, '_bbp_topic_count_hidden', $topic_count ); 2174 $topic_count = (int) $topic_count; 2175 $total_topics = (int) ( $topic_count + $children_topic_count ); 2176 2177 // Update the counts 2178 update_post_meta( $forum_id, '_bbp_topic_count_hidden', $topic_count ); 2179 update_post_meta( $forum_id, '_bbp_total_topic_count_hidden', $total_topics ); 2180 2181 // Update ancestor total counts by the persisted difference 2182 if ( true === $update_ancestors ) { 2183 bbp_bump_forum_ancestor_count( $forum_id, '_bbp_total_topic_count_hidden', $total_topics - $old_total_topics ); 2184 } 2091 2185 } 2092 2186 … … 2096 2190 2097 2191 /** 2098 * Adjust the total reply count of a forum 2192 * Adjust the total reply count of a forum. 2099 2193 * 2100 2194 * @since 2.0.0 bbPress (r2464) 2101 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects 2195 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects. 2196 * @since 2.6.16 Count replies only when their parent topics are public. 2197 * @since 2.6.16 Optionally update ancestor forum totals. 2102 2198 * 2103 2199 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it 2104 2200 * is a topic or a forum. If it's a topic, its parent, 2105 2201 * i.e. the forum is automatically retrieved. 2106 * 2107 * @return int Forum reply count 2108 */ 2109 function bbp_update_forum_reply_count( $forum_id = 0 ) { 2110 2111 $forum_id = bbp_get_forum_id( $forum_id ); 2202 * @param bool $update_ancestors Optional. Whether to update ancestor totals. 2203 * 2204 * @return int Forum reply count. 2205 */ 2206 function bbp_update_forum_reply_count( $forum_id = 0, $update_ancestors = false ) { 2207 2208 $forum_id = bbp_get_forum_id( $forum_id ); 2209 $old_total_replies = ( true === $update_ancestors ) 2210 ? (int) get_post_meta( $forum_id, '_bbp_total_reply_count', true ) 2211 : 0; 2112 2212 $children_reply_count = 0; 2113 2213 … … 2121 2221 2122 2222 // Don't count replies if the forum is a category 2123 $reply_count = ! bbp_is_forum_category( $forum_id ) 2124 ? bbp_get_public_child_count( $forum_id, bbp_get_reply_post_type() ) 2125 : 0; 2223 if ( bbp_is_forum_category( $forum_id ) ) { 2224 $reply_count = 0; 2225 2226 // Count public replies whose parent topics are also public 2227 } else { 2228 $bbp_db = bbp_db(); 2229 $reply_statuses = bbp_get_public_reply_statuses(); 2230 $topic_statuses = bbp_get_public_topic_statuses(); 2231 2232 if ( empty( $reply_statuses ) || empty( $topic_statuses ) ) { 2233 $reply_count = 0; 2234 } else { 2235 $reply_placeholders = implode( ', ', array_fill( 0, count( $reply_statuses ), '%s' ) ); 2236 $topic_placeholders = implode( ', ', array_fill( 0, count( $topic_statuses ), '%s' ) ); 2237 $sql = "SELECT COUNT(*) FROM {$bbp_db->posts} AS replies 2238 INNER JOIN {$bbp_db->posts} AS topics ON replies.post_parent = topics.ID 2239 WHERE topics.post_parent = %d 2240 AND topics.post_type = %s 2241 AND topics.post_status IN ({$topic_placeholders}) 2242 AND replies.post_type = %s 2243 AND replies.post_status IN ({$reply_placeholders})"; 2244 $query = $bbp_db->prepare( 2245 $sql, 2246 array_merge( 2247 array( $forum_id, bbp_get_topic_post_type() ), 2248 $topic_statuses, 2249 array( bbp_get_reply_post_type() ), 2250 $reply_statuses 2251 ) 2252 ); 2253 $reply_count = bbp_number_not_negative( $bbp_db->get_var( $query ) ); 2254 } 2255 } 2126 2256 2127 2257 // Calculate total replies in this forum … … 2132 2262 update_post_meta( $forum_id, '_bbp_total_reply_count', $total_replies ); 2133 2263 2264 // Update ancestor total counts by the persisted difference 2265 if ( true === $update_ancestors ) { 2266 bbp_bump_forum_ancestor_count( $forum_id, '_bbp_total_reply_count', $total_replies - $old_total_replies ); 2267 } 2268 2134 2269 // Filter & return 2135 2270 return (int) apply_filters( 'bbp_update_forum_reply_count', $total_replies, $forum_id ); … … 2137 2272 2138 2273 /** 2139 * Adjust the total hidden reply count of a forum 2274 * Adjust the total hidden reply count of a forum. 2140 2275 * 2141 2276 * @since 2.6.0 bbPress (r6922) 2277 * @since 2.6.16 Optionally update ancestor forum totals. 2142 2278 * 2143 2279 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it 2144 2280 * is a topic or a forum. If it's a topic, its parent, 2145 2281 * i.e. the forum is automatically retrieved. 2146 * 2147 * @return int Forum reply count 2148 */ 2149 function bbp_update_forum_reply_count_hidden( $forum_id = 0 ) { 2150 2151 $forum_id = bbp_get_forum_id( $forum_id ); 2282 * @param bool $update_ancestors Optional. Whether to update ancestor totals. 2283 * 2284 * @return int Forum reply count. 2285 */ 2286 function bbp_update_forum_reply_count_hidden( $forum_id = 0, $update_ancestors = false ) { 2287 2288 $forum_id = bbp_get_forum_id( $forum_id ); 2289 $old_total_replies = ( true === $update_ancestors ) 2290 ? (int) get_post_meta( $forum_id, '_bbp_total_reply_count_hidden', true ) 2291 : 0; 2152 2292 $children_reply_count = 0; 2153 2293 … … 2171 2311 update_post_meta( $forum_id, '_bbp_reply_count_hidden', $reply_count ); 2172 2312 update_post_meta( $forum_id, '_bbp_total_reply_count_hidden', $total_replies ); 2313 2314 // Update ancestor total counts by the persisted difference 2315 if ( true === $update_ancestors ) { 2316 bbp_bump_forum_ancestor_count( $forum_id, '_bbp_total_reply_count_hidden', $total_replies - $old_total_replies ); 2317 } 2173 2318 2174 2319 // Filter & return … … 2766 2911 2767 2912 /** 2768 * Returns the forum's subforum ids 2769 * 2770 * Only forums with published status are returned2913 * Returns the forum's subforum ids. 2914 * 2915 * Only forums with countable statuses are returned. 2771 2916 * 2772 2917 * @since 2.0.0 bbPress (r2908) 2773 * 2774 * @param int $forum_id Forum id 2918 * @since 2.6.16 Exclude forums with uncountable statuses. 2919 * 2920 * @param int $forum_id Forum id. 2775 2921 */ 2776 2922 function bbp_forum_query_subforum_ids( $forum_id ) { 2777 $subforum_ids = bbp_get_all_child_ids( $forum_id, bbp_get_forum_post_type() ); 2923 $forum_id = bbp_get_forum_id( $forum_id ); 2924 $statuses = bbp_get_countable_forum_statuses(); 2925 $subforum_ids = array(); 2926 2927 // Query and cache countable subforums. The public child-ID helper excludes 2928 // private and hidden forums, while the all-child helper includes trash, so 2929 // neither existing helper represents the statuses counted here. 2930 if ( ! empty( $forum_id ) && ! empty( $statuses ) ) { 2931 $key = md5( 2932 serialize( 2933 array( 2934 'parent_id' => $forum_id, 2935 'post_type' => bbp_get_forum_post_type(), 2936 'post_status' => $statuses 2937 ) 2938 ) 2939 ); 2940 $cache_key = "bbp_child_ids:{$key}:" . wp_cache_get_last_changed( 'bbpress_posts' ); 2941 $subforum_ids = wp_cache_get( $cache_key, 'bbpress_posts' ); 2942 2943 if ( false === $subforum_ids ) { 2944 $bbp_db = bbp_db(); 2945 $placeholders = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) ); 2946 $query = $bbp_db->prepare( 2947 "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_type = %s AND post_status IN ({$placeholders}) ORDER BY ID DESC", 2948 array_merge( array( $forum_id, bbp_get_forum_post_type() ), $statuses ) 2949 ); 2950 $subforum_ids = (array) $bbp_db->get_col( $query ); 2951 2952 wp_cache_set( $cache_key, $subforum_ids, 'bbpress_posts' ); 2953 } 2954 } 2778 2955 2779 2956 // Filter & return -
branches/2.6/src/includes/replies/functions.php
r7458 r7468 1497 1497 * 1498 1498 * @since 2.3.0 bbPress (r4521) 1499 * @since 2.6.16 Recount both forums and topic engagements. 1499 1500 * 1500 1501 * @param int $move_reply_id Move reply id … … 1503 1504 */ 1504 1505 function bbp_move_reply_count( $move_reply_id, $source_topic_id, $destination_topic_id ) { 1505 1506 // Forum topic counts 1507 bbp_update_forum_topic_count( bbp_get_topic_forum_id( $destination_topic_id ) ); 1508 1509 // Forum reply counts 1510 bbp_update_forum_reply_count( bbp_get_topic_forum_id( $destination_topic_id ) ); 1506 $source_forum_id = bbp_get_topic_forum_id( $source_topic_id ); 1507 $destination_forum_id = bbp_get_topic_forum_id( $destination_topic_id ); 1508 1509 // A reply converted into a topic changes its forum's topic counts 1510 if ( bbp_is_topic( $move_reply_id ) ) { 1511 bbp_update_forum_topic_count( $destination_forum_id, true ); 1512 bbp_update_forum_topic_count_hidden( $destination_forum_id, false, true ); 1513 1514 // Transfer the public contribution between count types 1515 if ( bbp_is_topic_published( $move_reply_id ) ) { 1516 $user_id = bbp_get_topic_author_id( $move_reply_id ); 1517 bbp_bump_user_reply_count( $user_id, -1 ); 1518 bbp_bump_user_topic_count( $user_id, 1 ); 1519 } 1520 } 1521 1522 // Recount replies in both forums 1523 foreach ( bbp_get_unique_array_values( array( $source_forum_id, $destination_forum_id ) ) as $forum_id ) { 1524 bbp_update_forum_reply_count( $forum_id, true ); 1525 bbp_update_forum_reply_count_hidden( $forum_id, true ); 1526 } 1511 1527 1512 1528 // Topic reply counts … … 1518 1534 bbp_update_topic_reply_count_hidden( $destination_topic_id ); 1519 1535 1520 // Topic voice counts 1536 // Topic engagement and voice counts 1537 bbp_recalculate_topic_engagements( $source_topic_id ); 1538 bbp_recalculate_topic_engagements( $destination_topic_id ); 1521 1539 bbp_update_topic_voice_count( $source_topic_id ); 1522 1540 bbp_update_topic_voice_count( $destination_topic_id ); -
branches/2.6/src/includes/topics/functions.php
r7456 r7468 1326 1326 1327 1327 /** 1328 * Fix counts on topic merge 1328 * Fix counts on topic merge. 1329 1329 * 1330 1330 * When a topic is merged, update the counts of source and destination topic … … 1332 1332 * 1333 1333 * @since 2.0.0 bbPress (r2756) 1334 * 1335 * @param int $destination_topic_id Destination topic id 1336 * @param int $source_topic_id Source topic id 1337 * @param int $source_topic_forum_id Source topic's forum id 1334 * @since 2.6.16 Recount both forums and topic engagements. 1335 * 1336 * @param int $destination_topic_id Destination topic id. 1337 * @param int $source_topic_id Source topic id. 1338 * @param int $source_topic_forum_id Source topic's forum id. 1338 1339 */ 1339 1340 function bbp_merge_topic_count( $destination_topic_id, $source_topic_id, $source_topic_forum_id ) { 1341 $destination_forum_id = bbp_get_topic_forum_id( $destination_topic_id ); 1340 1342 1341 1343 /** Source Topic **********************************************************/ 1342 1344 1343 1345 // Forum Topic Counts 1344 bbp_update_forum_topic_count( $source_topic_forum_id ); 1346 bbp_update_forum_topic_count( $source_topic_forum_id, true ); 1347 bbp_update_forum_topic_count_hidden( $source_topic_forum_id, false, true ); 1345 1348 1346 1349 // Forum Reply Counts 1347 bbp_update_forum_reply_count( $source_topic_forum_id ); 1350 foreach ( bbp_get_unique_array_values( array( $source_topic_forum_id, $destination_forum_id ) ) as $forum_id ) { 1351 bbp_update_forum_reply_count( $forum_id, true ); 1352 bbp_update_forum_reply_count_hidden( $forum_id, true ); 1353 } 1348 1354 1349 1355 /** Destination Topic *****************************************************/ … … 1355 1361 bbp_update_topic_reply_count_hidden( $destination_topic_id ); 1356 1362 1357 // Topic Voice Counts 1363 // Topic Engagement and Voice Counts 1364 bbp_recalculate_topic_engagements( $destination_topic_id ); 1358 1365 bbp_update_topic_voice_count( $destination_topic_id ); 1359 1366 1367 // Transfer the converted source topic author's contribution to replies 1368 if ( bbp_is_reply_published( $source_topic_id ) ) { 1369 $user_id = bbp_get_reply_author_id( $source_topic_id ); 1370 bbp_bump_user_topic_count( $user_id, -1 ); 1371 bbp_bump_user_reply_count( $user_id, 1 ); 1372 } 1373 1360 1374 do_action( 'bbp_merge_topic_count', $destination_topic_id, $source_topic_id, $source_topic_forum_id ); 1361 1375 } … … 1367 1381 * 1368 1382 * @since 2.0.0 bbPress (r2756) 1383 * @since 2.6.16 Recount both forums and topic engagements. 1369 1384 * 1370 1385 * @param string $action The requested action to compare this function to … … 1682 1697 * 1683 1698 * @since 2.0.0 bbPress (r2756) 1699 * @since 2.6.16 Recount both forums and topic engagements. 1684 1700 * 1685 1701 * @param int $from_reply_id From reply id … … 1688 1704 */ 1689 1705 function bbp_split_topic_count( $from_reply_id, $source_topic_id, $destination_topic_id ) { 1690 1691 // Forum Topic Counts 1692 bbp_update_forum_topic_count( bbp_get_topic_forum_id( $destination_topic_id ) ); 1693 1694 // Forum Reply Counts 1695 bbp_update_forum_reply_count( bbp_get_topic_forum_id( $destination_topic_id ) ); 1706 $source_forum_id = bbp_get_topic_forum_id( $source_topic_id ); 1707 $destination_forum_id = bbp_get_topic_forum_id( $destination_topic_id ); 1708 1709 // A reply converted into a topic changes its forum's topic counts 1710 if ( bbp_is_topic( $from_reply_id ) ) { 1711 bbp_update_forum_topic_count( $destination_forum_id, true ); 1712 bbp_update_forum_topic_count_hidden( $destination_forum_id, false, true ); 1713 1714 // Transfer the public contribution between count types 1715 if ( bbp_is_topic_published( $from_reply_id ) ) { 1716 $user_id = bbp_get_topic_author_id( $from_reply_id ); 1717 bbp_bump_user_reply_count( $user_id, -1 ); 1718 bbp_bump_user_topic_count( $user_id, 1 ); 1719 } 1720 } 1721 1722 // Recount replies in both forums 1723 foreach ( bbp_get_unique_array_values( array( $source_forum_id, $destination_forum_id ) ) as $forum_id ) { 1724 bbp_update_forum_reply_count( $forum_id, true ); 1725 bbp_update_forum_reply_count_hidden( $forum_id, true ); 1726 } 1696 1727 1697 1728 // Topic Reply Counts … … 1703 1734 bbp_update_topic_reply_count_hidden( $destination_topic_id ); 1704 1735 1705 // Topic Voice Counts 1736 // Topic Engagement and Voice Counts 1737 bbp_recalculate_topic_engagements( $source_topic_id ); 1738 bbp_recalculate_topic_engagements( $destination_topic_id ); 1706 1739 bbp_update_topic_voice_count( $source_topic_id ); 1707 1740 bbp_update_topic_voice_count( $destination_topic_id ); … … 2367 2400 * 2368 2401 * @since 2.1.0 bbPress (r3825) 2402 * @since 2.6.16 Use atomic metadata writes and non-negative counts. 2369 2403 * 2370 2404 * @param int $topic_id Optional. Topic id. … … 2383 2417 $reply_count = bbp_get_topic_reply_count( $topic_id, true ); 2384 2418 $difference = (int) $difference; 2385 $new_count = (int)( $reply_count + $difference );2419 $new_count = bbp_number_not_negative( $reply_count + $difference ); 2386 2420 2387 2421 // Update this topic id's reply count 2388 update_post_meta( $topic_id, '_bbp_reply_count', $new_count );2422 bbp_bump_count_meta( 'post', $topic_id, '_bbp_reply_count', $difference, $reply_count ); 2389 2423 2390 2424 // Filter & return … … 2460 2494 * 2461 2495 * @since 2.1.0 bbPress (r3825) 2496 * @since 2.6.16 Use atomic metadata writes and non-negative counts. 2462 2497 * 2463 2498 * @param int $topic_id Optional. Topic id. … … 2476 2511 $reply_count = bbp_get_topic_reply_count_hidden( $topic_id, true ); 2477 2512 $difference = (int) $difference; 2478 $new_count = (int)( $reply_count + $difference );2513 $new_count = bbp_number_not_negative( $reply_count + $difference ); 2479 2514 2480 2515 // Update this topic id's hidden reply count 2481 update_post_meta( $topic_id, '_bbp_reply_count_hidden', $new_count );2516 bbp_bump_count_meta( 'post', $topic_id, '_bbp_reply_count_hidden', $difference, $reply_count ); 2482 2517 2483 2518 // Filter & return -
branches/2.6/src/includes/users/engagements.php
r7436 r7468 195 195 * 196 196 * @since 2.6.0 bbPress (r6522) 197 * @since 2.6.16 Honor filtered public reply statuses. 197 198 * 198 199 * @param int $topic_id … … 203 204 204 205 // Default variables 205 $topic_id = bbp_get_topic_id( $topic_id ); 206 $bbp_db = bbp_db(); 207 $statii = "'" . implode( "', '", bbp_get_public_topic_statuses() ) . "'"; 208 209 // A cool UNION query! 210 $sql = " 211 SELECT DISTINCT( post_author ) FROM ( 212 SELECT post_author FROM {$bbp_db->posts} 213 WHERE ( ID = %d AND post_status IN ({$statii}) AND post_type = %s ) 214 UNION 215 SELECT post_author FROM {$bbp_db->posts} 216 WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) 217 ) as u1"; 218 219 // Prepare & get results 220 $query = $bbp_db->prepare( $sql, $topic_id, bbp_get_topic_post_type(), $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type() ); 221 $results = $bbp_db->get_col( $query ); 206 $topic_id = bbp_get_topic_id( $topic_id ); 207 $bbp_db = bbp_db(); 208 $topic_type = bbp_get_topic_post_type(); 209 $reply_type = bbp_get_reply_post_type(); 210 $topic_statuses = array_unique( array_filter( bbp_get_public_topic_statuses() ) ); 211 $reply_statuses = array_unique( array_filter( bbp_get_public_reply_statuses() ) ); 212 $sql = array(); 213 $values = array(); 214 215 // Add the topic author for countable topic statuses 216 if ( ! empty( $topic_statuses ) ) { 217 $placeholders = implode( ', ', array_fill( 0, count( $topic_statuses ), '%s' ) ); 218 $sql[] = "SELECT post_author FROM {$bbp_db->posts} WHERE ID = %d AND post_status IN ( {$placeholders} ) AND post_type = %s"; 219 $values = array_merge( $values, array( $topic_id ), $topic_statuses, array( $topic_type ) ); 220 } 221 222 // Add reply authors for countable reply statuses 223 if ( ! empty( $reply_statuses ) ) { 224 $placeholders = implode( ', ', array_fill( 0, count( $reply_statuses ), '%s' ) ); 225 $sql[] = "SELECT post_author FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$placeholders} ) AND post_type = %s"; 226 $values = array_merge( $values, array( $topic_id ), $reply_statuses, array( $reply_type ) ); 227 } 228 229 // Query unique topic and reply authors 230 $results = ! empty( $sql ) 231 ? $bbp_db->get_col( $bbp_db->prepare( implode( ' UNION ', $sql ), $values ) ) 232 : array(); 222 233 223 234 // Parse results into voices 224 $engagements = ! is_wp_error( $results ) 225 ? wp_parse_id_list( array_filter( $results ) ) 226 : array(); 235 $engagements = wp_parse_id_list( array_filter( $results ) ); 227 236 228 237 // Filter & return -
branches/2.6/src/includes/users/functions.php
r7451 r7468 720 720 : bbp_get_user_topic_count( $user_id, true ); 721 721 722 $user_topic_count = bbp_number_not_negative( $count + $difference );722 $user_topic_count = (int) bbp_number_not_negative( $count + $difference ); 723 723 724 724 // Add them up and filter them 725 725 $new_count = (int) apply_filters( 'bbp_bump_user_topic_count', $user_topic_count, $user_id, $difference, $count ); 726 726 727 return bbp_update_user_topic_count( $user_id, $new_count ); 727 // Preserve absolute count filters before using the atomic difference 728 $difference = ( $new_count === $user_topic_count ) 729 ? $new_count - $count 730 : false; 731 732 return bbp_update_user_topic_count( $user_id, $new_count, $difference ); 728 733 } 729 734 … … 756 761 : bbp_get_user_reply_count( $user_id, true ); 757 762 758 $user_reply_count = bbp_number_not_negative( $count + $difference );763 $user_reply_count = (int) bbp_number_not_negative( $count + $difference ); 759 764 760 765 // Add them up and filter them 761 766 $new_count = (int) apply_filters( 'bbp_bump_user_reply_count', $user_reply_count, $user_id, $difference, $count ); 762 767 763 return bbp_update_user_reply_count( $user_id, $new_count ); 768 // Preserve absolute count filters before using the atomic difference 769 $difference = ( $new_count === $user_reply_count ) 770 ? $new_count - $count 771 : false; 772 773 return bbp_update_user_reply_count( $user_id, $new_count, $difference ); 774 } 775 776 /** 777 * Update user counts when a topic or reply changes authors. 778 * 779 * @since 2.6.16 780 * 781 * @param int $post_id Post ID. 782 * @param WP_Post $post_after Post object following the update. 783 * @param WP_Post $post_before Post object before the update. 784 */ 785 function bbp_update_counts_on_post_author_change( $post_id = 0, $post_after = false, $post_before = false ) { 786 787 // Bail if the author or post type did not change as expected 788 if ( ( $post_after->post_author === $post_before->post_author ) || ( $post_after->post_type !== $post_before->post_type ) ) { 789 return; 790 } 791 792 // Set topic public membership 793 if ( bbp_get_topic_post_type() === $post_after->post_type ) { 794 $public_statuses = bbp_get_public_topic_statuses(); 795 $was_public = in_array( $post_before->post_status, $public_statuses, true ); 796 $is_public = in_array( $post_after->post_status, $public_statuses, true ); 797 $is_topic = true; 798 799 // Set reply public membership 800 } elseif ( bbp_get_reply_post_type() === $post_after->post_type ) { 801 $public_statuses = bbp_get_public_reply_statuses(); 802 $was_public = in_array( $post_before->post_status, $public_statuses, true ); 803 $is_public = in_array( $post_after->post_status, $public_statuses, true ); 804 $is_topic = false; 805 806 // Bail if this is not a topic or reply 807 } else { 808 return; 809 } 810 811 // The transition callback already handles posts that were not public 812 if ( ! $was_public ) { 813 return; 814 } 815 816 // Transfer a public contribution between authors 817 if ( $is_public ) { 818 if ( $is_topic ) { 819 bbp_bump_user_topic_count( $post_before->post_author, -1 ); 820 bbp_bump_user_topic_count( $post_after->post_author, 1 ); 821 } else { 822 bbp_bump_user_reply_count( $post_before->post_author, -1 ); 823 bbp_bump_user_reply_count( $post_after->post_author, 1 ); 824 } 825 826 // Repair both authors after the transition callback targeted the new author 827 } else { 828 foreach ( bbp_get_unique_array_values( array( $post_before->post_author, $post_after->post_author ) ) as $user_id ) { 829 if ( $is_topic ) { 830 bbp_update_user_topic_count( $user_id, bbp_get_user_topic_count_raw( $user_id ) ); 831 } else { 832 bbp_update_user_reply_count( $user_id, bbp_get_user_reply_count_raw( $user_id ) ); 833 } 834 } 835 } 836 } 837 838 /** 839 * Update topic engagements when a topic or reply changes authors. 840 * 841 * @since 2.6.16 842 * 843 * @param int $post_id Post ID. 844 * @param WP_Post $post_after Post object following the update. 845 * @param WP_Post $post_before Post object before the update. 846 */ 847 function bbp_recalculate_engagements_on_post_author_change( $post_id = 0, $post_after = false, $post_before = false ) { 848 849 // Bail if the author did not change 850 if ( $post_after->post_author === $post_before->post_author ) { 851 return; 852 } 853 854 // Get the topic ID from a topic or reply 855 if ( bbp_get_topic_post_type() === $post_after->post_type ) { 856 $topic_id = $post_id; 857 } elseif ( bbp_get_reply_post_type() === $post_after->post_type ) { 858 $topic_id = bbp_get_reply_topic_id( $post_id ); 859 } else { 860 return; 861 } 862 863 // Recalculate engagements and their count 864 bbp_recalculate_topic_engagements( $topic_id ); 865 bbp_update_topic_voice_count( $topic_id ); 866 } 867 868 /** 869 * Update counts and engagements when a deleted user's posts are reassigned. 870 * 871 * WordPress reassigns post authors directly in the database, bypassing the 872 * normal post update actions. Record affected topics before that write, then 873 * repair the replacement user's counts and those topics after it completes. 874 * 875 * @since 2.6.16 876 * 877 * @param int $user_id ID of the user being deleted. 878 * @param int|null $reassign ID of the user receiving the posts. 879 */ 880 function bbp_update_counts_on_user_reassignment( $user_id = 0, $reassign = null ) { 881 static $topic_ids = array(); 882 883 $user_id = (int) $user_id; 884 $reassign = (int) $reassign; 885 886 // Bail if posts are not being reassigned to another user 887 if ( empty( $user_id ) || empty( $reassign ) || ( $user_id === $reassign ) ) { 888 return; 889 } 890 891 $key = get_current_blog_id() . ':' . $user_id . ':' . $reassign; 892 893 // Record affected topics before WordPress changes their authors directly 894 if ( 'delete_user' === current_filter() ) { 895 $bbp_db = bbp_db(); 896 $topic_type = bbp_get_topic_post_type(); 897 $reply_type = bbp_get_reply_post_type(); 898 $query = $bbp_db->prepare( 899 "SELECT DISTINCT CASE WHEN post_type = %s THEN ID ELSE post_parent END FROM {$bbp_db->posts} WHERE post_author = %d AND post_type IN ( %s, %s )", 900 $topic_type, 901 $user_id, 902 $topic_type, 903 $reply_type 904 ); 905 906 $topic_ids[ $key ] = wp_parse_id_list( array_filter( $bbp_db->get_col( $query ) ) ); 907 return; 908 } 909 910 // Bail unless WordPress completed the reassignment recorded above 911 if ( ( 'deleted_user' !== current_filter() ) || ! isset( $topic_ids[ $key ] ) ) { 912 return; 913 } 914 915 $affected_topic_ids = $topic_ids[ $key ]; 916 unset( $topic_ids[ $key ] ); 917 918 // Bail if the deleted user did not author any topics or replies 919 if ( empty( $affected_topic_ids ) ) { 920 return; 921 } 922 923 // Recount contributions for the replacement user 924 bbp_update_user_topic_count( $reassign, bbp_get_user_topic_count_raw( $reassign ) ); 925 bbp_update_user_reply_count( $reassign, bbp_get_user_reply_count_raw( $reassign ) ); 926 927 // Rebuild engagements and voices for each affected topic 928 foreach ( $affected_topic_ids as $topic_id ) { 929 bbp_recalculate_topic_engagements( $topic_id, true ); 930 bbp_update_topic_voice_count( $topic_id ); 931 } 764 932 } 765 933 -
branches/2.6/src/includes/users/options.php
r7436 r7468 124 124 * 125 125 * @since 2.6.0 bbPress (r5309) 126 * 127 * @param int $user_id 128 * @param mixed $count 126 * @since 2.6.16 Support atomic count differences. 127 * 128 * @param int $user_id User ID. 129 * @param mixed $count New topic count. 130 * @param int|false $difference Optional. Difference from the previous count. 129 131 * @return boolean 130 132 */ 131 function bbp_update_user_topic_count( $user_id = 0, $count = false ) {133 function bbp_update_user_topic_count( $user_id = 0, $count = false, $difference = false ) { 132 134 133 135 // Validate user id … … 138 140 139 141 // Just in time filtering of the user's topic count 142 $unfiltered_count = $count; 140 143 $count = apply_filters( 'bbp_update_user_topic_count', $count, $user_id ); 141 144 … … 145 148 } 146 149 150 // Atomically bump an unfiltered user option 151 if ( ( false !== $difference ) && ( $count === $unfiltered_count ) ) { 152 $default = (int) $unfiltered_count - (int) $difference; 153 $meta_key = bbp_db()->get_blog_prefix() . '_bbp_topic_count'; 154 $result = bbp_bump_count_meta( 'user', $user_id, $meta_key, $difference, $default ); 155 156 return $result; 157 } 158 147 159 // Return the updated user option 148 160 return update_user_option( $user_id, '_bbp_topic_count', $count ); … … 150 162 151 163 /** 152 * Update the reply count for a user 164 * Update the reply count for a user. 153 165 * 154 166 * @since 2.6.0 bbPress (r5309) 155 * 156 * @param int $user_id 157 * @param mixed $count 167 * @since 2.6.16 Support atomic count differences. 168 * 169 * @param int $user_id User ID. 170 * @param mixed $count New reply count. 171 * @param int|false $difference Optional. Difference from the previous count. 158 172 * @return boolean 159 173 */ 160 function bbp_update_user_reply_count( $user_id = 0, $count = false ) {174 function bbp_update_user_reply_count( $user_id = 0, $count = false, $difference = false ) { 161 175 162 176 // Validate user id … … 167 181 168 182 // Just in time filtering of the user's reply count 183 $unfiltered_count = $count; 169 184 $count = apply_filters( 'bbp_update_user_reply_count', $count, $user_id ); 170 185 … … 172 187 if ( false === $count ) { 173 188 return false; 189 } 190 191 // Atomically bump an unfiltered user option 192 if ( ( false !== $difference ) && ( $count === $unfiltered_count ) ) { 193 $default = (int) $unfiltered_count - (int) $difference; 194 $meta_key = bbp_db()->get_blog_prefix() . '_bbp_reply_count'; 195 $result = bbp_bump_count_meta( 'user', $user_id, $meta_key, $difference, $default ); 196 197 return $result; 174 198 } 175 199 -
branches/2.6/src/readme.txt
r7454 r7468 56 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 57 58 Count bump functions now use conditional metadata writes and bounded retries so simultaneous requests do not overwrite each other's existing count changes. Existing bbPress count filters and the standard WordPress metadata filters and actions continue to run. WordPress metadata tables do not enforce unique object-and-key pairs, so simultaneous first-time inserts retain the same limitation as the core metadata API. The `bbp_pre_bump_count_meta` filter can short-circuit an update, the `bbp_bump_count_meta_max_attempts` filter controls the default limit of five write attempts, and the `bbp_bump_count_meta_types` filter controls the post, user, term, and comment metadata types supported by default. `bbp_update_user_topic_count()` and `bbp_update_user_reply_count()` accept an optional third `$difference` argument for this internal bump lifecycle; existing calls remain compatible. 59 60 Post-author changes and user deletion with post reassignment now reconcile affected user contribution, topic engagement, and voice counts. Moderator move, merge, and split operations also reconcile source, destination, and ancestor forum counts. Forum count updater functions accept an optional final argument for propagating a recount's difference through ancestor totals; existing calls remain compatible. Forum reply recounts include public replies only when their parent topics are also public. A public reply beneath a non-public topic is excluded from the public forum total without being included in the pending, spammed, and trashed reply count. Topic engagement recounts honor filtered public topic and reply statuses, and preserve other term-backed relationships. 61 58 62 = Subforum counts in 2.6.16 = 59 63 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.64 bbPress now maintains subforum counts when forums are trashed, restored, permanently deleted, or moved between parents. Recursive forum counts include public, private, and hidden subforums while excluding subforums with uncountable statuses. 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 65 62 66 == Screenshots == -
branches/2.6/tests/phpunit/testcases/admin/tools.php
r7454 r7468 370 370 $this->assertSame( 0, $count ); 371 371 372 // Category total topic count hidden. 373 $count = bbp_get_forum_topic_count_hidden( $c, true, true ); 374 $this->assertSame( 2, $count ); 375 372 376 // Forum topic count. 373 377 $count = bbp_get_forum_topic_count( $f, false, true ); … … 382 386 $this->assertSame( 2, $count ); 383 387 384 // Delete the _bbp_to tal_topic_countmeta key.388 // Delete the _bbp_topic_count_hidden meta key. 385 389 $this->assertTrue( delete_post_meta_by_key( '_bbp_topic_count_hidden' ) ); 390 391 // Delete the _bbp_total_topic_count_hidden meta key. 392 $this->assertTrue( delete_post_meta_by_key( '_bbp_total_topic_count_hidden' ) ); 386 393 387 394 // Delete the _bbp_total_topic_count meta key. … … 403 410 $this->assertSame( 0, $count ); 404 411 412 // Category total topic count hidden. 413 $count = bbp_get_forum_topic_count_hidden( $c, true, true ); 414 $this->assertSame( 0, $count ); 415 405 416 // Forum topic count. 406 417 $count = bbp_get_forum_topic_count( $f, false, true ); … … 430 441 $this->assertSame( 0, $count ); 431 442 443 // Category total topic count hidden. 444 $count = bbp_get_forum_topic_count_hidden( $c, true, true ); 445 $this->assertSame( 2, $count ); 446 432 447 // Forum topic count. 433 448 $count = bbp_get_forum_topic_count( $f, false, true ); … … 441 456 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 442 457 $this->assertSame( 2, $count ); 458 } 459 460 /** 461 * @covers ::bbp_admin_repair_forum_hidden_reply_count 462 */ 463 public function test_bbp_admin_repair_forum_hidden_reply_count() { 464 $c = $this->factory->forum->create( array( 465 'forum_meta' => array( 'forum_type' => 'category' ), 466 ) ); 467 $f = $this->factory->forum->create( array( 468 'post_parent' => $c, 469 'forum_meta' => array( 'forum_id' => $c ), 470 ) ); 471 $t = $this->factory->topic->create( array( 472 'post_parent' => $f, 473 'topic_meta' => array( 'forum_id' => $f ), 474 ) ); 475 476 $this->factory->reply->create( array( 477 'post_parent' => $t, 478 'post_status' => bbp_get_pending_status_id(), 479 'reply_meta' => array( 480 'forum_id' => $f, 481 'topic_id' => $t, 482 ), 483 ) ); 484 485 $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $c, false, true ) ); 486 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $c, true, true ) ); 487 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $f, false, true ) ); 488 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $f, true, true ) ); 489 490 $this->assertTrue( delete_post_meta_by_key( '_bbp_reply_count_hidden' ) ); 491 $this->assertTrue( delete_post_meta_by_key( '_bbp_total_reply_count_hidden' ) ); 492 493 bbp_admin_repair_forum_hidden_reply_count(); 494 495 $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $c, false, true ) ); 496 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $c, true, true ) ); 497 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $f, false, true ) ); 498 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $f, true, true ) ); 443 499 } 444 500 -
branches/2.6/tests/phpunit/testcases/core/abstraction.php
r5898 r7468 32 32 33 33 /** 34 * @covers ::bbp_bump_count_meta 35 */ 36 public function test_bbp_bump_count_meta_handles_missing_and_non_negative_values() { 37 $post_id = self::factory()->post->create(); 38 39 $this->assertTrue( bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count', 2, 3 ) ); 40 $this->assertSame( 5, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 41 $this->assertTrue( bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count', -10 ) ); 42 $this->assertSame( 0, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 43 $this->assertFalse( bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count', -1 ) ); 44 } 45 46 /** 47 * @covers ::bbp_bump_count_meta 48 */ 49 public function test_bbp_bump_count_meta_validates_required_values_before_filters() { 50 $filtered = 0; 51 $callback = function( $check ) use ( &$filtered ) { 52 $filtered++; 53 return $check; 54 }; 55 56 add_filter( 'bbp_pre_bump_count_meta', $callback ); 57 $this->assertFalse( bbp_bump_count_meta( 'post', 0, '_bbp_test_count' ) ); 58 $this->assertFalse( bbp_bump_count_meta( 'post', 1, '' ) ); 59 $this->assertFalse( bbp_bump_count_meta( 'post', 1, '_bbp_test_count', 0 ) ); 60 remove_filter( 'bbp_pre_bump_count_meta', $callback, 10 ); 61 62 $this->assertSame( 0, $filtered ); 63 } 64 65 /** 66 * @covers ::bbp_bump_count_meta 67 */ 68 public function test_bbp_bump_count_meta_can_be_short_circuited_before_type_validation() { 69 $meta_types_filtered = 0; 70 $received = array(); 71 $pre_callback = function( $check, $meta_type, $object_id, $meta_key, $difference, $default ) use ( &$received ) { 72 $received = func_get_args(); 73 return false; 74 }; 75 $types_callback = function( $meta_types ) use ( &$meta_types_filtered ) { 76 $meta_types_filtered++; 77 return $meta_types; 78 }; 79 80 add_filter( 'bbp_pre_bump_count_meta', $pre_callback, 10, 6 ); 81 add_filter( 'bbp_bump_count_meta_types', $types_callback ); 82 $result = bbp_bump_count_meta( 'invalid', 1, '_bbp_test_count' ); 83 remove_filter( 'bbp_bump_count_meta_types', $types_callback, 10 ); 84 remove_filter( 'bbp_pre_bump_count_meta', $pre_callback, 10 ); 85 86 $this->assertFalse( $result ); 87 $this->assertSame( array( null, 'invalid', 1, '_bbp_test_count', 1, 0 ), $received ); 88 $this->assertSame( 0, $meta_types_filtered ); 89 90 $pre_callback = function() { 91 return true; 92 }; 93 94 add_filter( 'bbp_pre_bump_count_meta', $pre_callback ); 95 $result = bbp_bump_count_meta( 'invalid', 1, '_bbp_test_count' ); 96 remove_filter( 'bbp_pre_bump_count_meta', $pre_callback, 10 ); 97 98 $this->assertTrue( $result ); 99 } 100 101 /** 102 * @covers ::bbp_bump_count_meta 103 */ 104 public function test_bbp_bump_count_meta_supports_user_term_and_comment_metadata() { 105 $post_id = self::factory()->post->create(); 106 $user_id = self::factory()->user->create(); 107 $term_id = self::factory()->term->create(); 108 $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); 109 110 $this->assertTrue( bbp_bump_count_meta( 'user', $user_id, '_bbp_test_count', 1 ) ); 111 $this->assertSame( 1, (int) get_user_meta( $user_id, '_bbp_test_count', true ) ); 112 $this->assertTrue( bbp_bump_count_meta( 'term', $term_id, '_bbp_test_count', 2 ) ); 113 $this->assertSame( 2, (int) get_term_meta( $term_id, '_bbp_test_count', true ) ); 114 $this->assertTrue( bbp_bump_count_meta( 'comment', $comment_id, '_bbp_test_count', 3 ) ); 115 $this->assertSame( 3, (int) get_comment_meta( $comment_id, '_bbp_test_count', true ) ); 116 } 117 118 /** 119 * @covers ::bbp_bump_count_meta 120 */ 121 public function test_bbp_bump_count_meta_types_are_filterable() { 122 $term_id = self::factory()->term->create(); 123 $callback = function() { 124 return array( 'post' ); 125 }; 126 127 add_filter( 'bbp_bump_count_meta_types', $callback ); 128 $result = bbp_bump_count_meta( 'term', $term_id, '_bbp_test_count' ); 129 remove_filter( 'bbp_bump_count_meta_types', $callback, 10 ); 130 131 $this->assertFalse( $result ); 132 $this->assertSame( '', get_term_meta( $term_id, '_bbp_test_count', true ) ); 133 } 134 135 /** 136 * @covers ::bbp_bump_count_meta 137 */ 138 public function test_bbp_bump_count_meta_does_not_retry_a_filtered_update() { 139 $post_id = self::factory()->post->create(); 140 $updates = 0; 141 $callback = function() use ( &$updates ) { 142 $updates++; 143 return false; 144 }; 145 146 update_post_meta( $post_id, '_bbp_test_count', 5 ); 147 add_filter( 'update_post_metadata', $callback ); 148 $result = bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count' ); 149 remove_filter( 'update_post_metadata', $callback, 10 ); 150 151 $this->assertFalse( $result ); 152 $this->assertSame( 1, $updates ); 153 $this->assertSame( 5, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 154 } 155 156 /** 157 * @covers ::bbp_bump_count_meta 158 */ 159 public function test_bbp_bump_count_meta_filters_an_update_before_adding_metadata() { 160 $post_id = self::factory()->post->create(); 161 $previous = null; 162 $callback = function( $check, $filtered_post_id, $meta_key, $meta_value, $prev_value ) use ( &$previous ) { 163 $previous = $prev_value; 164 return false; 165 }; 166 167 add_filter( 'update_post_metadata', $callback, 10, 5 ); 168 $result = bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count' ); 169 remove_filter( 'update_post_metadata', $callback, 10 ); 170 171 $this->assertFalse( $result ); 172 $this->assertSame( '', $previous ); 173 $this->assertSame( '', get_post_meta( $post_id, '_bbp_test_count', true ) ); 174 } 175 176 /** 177 * @covers ::bbp_bump_count_meta 178 * @ticket BBP3678 179 */ 180 public function test_bbp_bump_count_meta_uses_the_database_after_a_filtered_read() { 181 $post_id = self::factory()->post->create(); 182 $callback = function( $value, $filtered_post_id, $meta_key ) use ( $post_id ) { 183 return ( ( $post_id === $filtered_post_id ) && ( '_bbp_test_count' === $meta_key ) ) 184 ? 100 185 : $value; 186 }; 187 188 update_post_meta( $post_id, '_bbp_test_count', 5 ); 189 add_filter( 'get_post_metadata', $callback, 10, 3 ); 190 $result = bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count' ); 191 remove_filter( 'get_post_metadata', $callback, 10 ); 192 193 $this->assertTrue( $result ); 194 $this->assertSame( 6, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 195 } 196 197 /** 198 * @covers ::bbp_bump_count_meta 199 */ 200 public function test_bbp_bump_count_meta_returns_false_when_adding_metadata_fails() { 201 $post_id = self::factory()->post->create(); 202 $callback = function() { 203 return false; 204 }; 205 206 add_filter( 'add_post_metadata', $callback ); 207 $result = bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count' ); 208 remove_filter( 'add_post_metadata', $callback, 10 ); 209 210 $this->assertFalse( $result ); 211 $this->assertSame( '', get_post_meta( $post_id, '_bbp_test_count', true ) ); 212 } 213 214 /** 215 * @covers ::bbp_bump_count_meta 216 * @ticket BBP3678 217 */ 218 public function test_bbp_bump_count_meta_preserves_an_interleaved_update_from_zero() { 219 $post_id = self::factory()->post->create(); 220 $interleaved = false; 221 $callback = function( $check, $filtered_post_id, $meta_key ) use ( $post_id, &$interleaved ) { 222 if ( ( $post_id === $filtered_post_id ) && ( '_bbp_test_count' === $meta_key ) && ! $interleaved ) { 223 $interleaved = true; 224 bbp_bump_count_meta( 'post', $post_id, $meta_key ); 225 } 226 227 return $check; 228 }; 229 230 update_post_meta( $post_id, '_bbp_test_count', 0 ); 231 add_filter( 'update_post_metadata', $callback, 10, 3 ); 232 bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count' ); 233 remove_filter( 'update_post_metadata', $callback, 10 ); 234 235 $this->assertSame( 2, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 236 } 237 238 /** 239 * @covers ::bbp_bump_count_meta 240 * @ticket BBP3678 241 */ 242 public function test_bbp_bump_count_meta_preserves_interleaved_decrements() { 243 $post_id = self::factory()->post->create(); 244 $interleaved = false; 245 $filter_calls = 0; 246 $callback = function( $check, $filtered_post_id, $meta_key ) use ( $post_id, &$interleaved, &$filter_calls ) { 247 $filter_calls++; 248 249 if ( ( $post_id === $filtered_post_id ) && ( '_bbp_test_count' === $meta_key ) && ! $interleaved ) { 250 $interleaved = true; 251 bbp_bump_count_meta( 'post', $post_id, $meta_key, -1 ); 252 } 253 254 return $check; 255 }; 256 257 update_post_meta( $post_id, '_bbp_test_count', 3 ); 258 add_filter( 'update_post_metadata', $callback, 10, 3 ); 259 bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count', -1 ); 260 remove_filter( 'update_post_metadata', $callback, 10 ); 261 262 $this->assertSame( 1, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 263 $this->assertSame( 2, $filter_calls ); 264 } 265 266 /** 267 * @covers ::bbp_bump_count_meta 268 * @ticket BBP3678 269 */ 270 public function test_bbp_bump_count_meta_refreshes_a_stale_zero_before_decrementing() { 271 global $wpdb; 272 273 $post_id = self::factory()->post->create(); 274 275 update_post_meta( $post_id, '_bbp_test_count', 0 ); 276 get_post_meta( $post_id, '_bbp_test_count', true ); 277 278 $wpdb->update( 279 $wpdb->postmeta, 280 array( 'meta_value' => 1 ), 281 array( 282 'post_id' => $post_id, 283 'meta_key' => '_bbp_test_count' 284 ), 285 array( '%d' ), 286 array( '%d', '%s' ) 287 ); 288 289 $this->assertTrue( bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count', -1 ) ); 290 $this->assertSame( 0, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 291 } 292 293 /** 294 * @covers ::bbp_bump_count_meta 295 * @ticket BBP3678 296 */ 297 public function test_bbp_bump_count_meta_max_attempts_is_filterable() { 298 $post_id = self::factory()->post->create(); 299 $interleaved = false; 300 $callback = function( $check, $filtered_post_id, $meta_key ) use ( $post_id, &$interleaved ) { 301 if ( ( $post_id === $filtered_post_id ) && ( '_bbp_test_count' === $meta_key ) && ! $interleaved ) { 302 $interleaved = true; 303 bbp_bump_count_meta( 'post', $post_id, $meta_key, -1 ); 304 } 305 306 return $check; 307 }; 308 $max_attempts = function() { 309 return 1; 310 }; 311 312 update_post_meta( $post_id, '_bbp_test_count', 3 ); 313 add_filter( 'update_post_metadata', $callback, 10, 3 ); 314 add_filter( 'bbp_bump_count_meta_max_attempts', $max_attempts ); 315 $result = bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count', -1 ); 316 remove_filter( 'bbp_bump_count_meta_max_attempts', $max_attempts, 10 ); 317 remove_filter( 'update_post_metadata', $callback, 10 ); 318 319 $this->assertFalse( $result ); 320 $this->assertSame( 2, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 321 } 322 323 /** 324 * @covers ::bbp_bump_count_meta 325 * @ticket BBP3678 326 */ 327 public function test_bbp_bump_count_meta_preserves_an_interleaved_add() { 328 $post_id = self::factory()->post->create(); 329 $interleaved = false; 330 $callback = function( $check, $filtered_post_id, $meta_key ) use ( $post_id, &$interleaved ) { 331 if ( ( $post_id === $filtered_post_id ) && ( '_bbp_test_count' === $meta_key ) && ! $interleaved ) { 332 $interleaved = true; 333 bbp_bump_count_meta( 'post', $post_id, $meta_key ); 334 } 335 336 return $check; 337 }; 338 339 add_filter( 'add_post_metadata', $callback, 10, 3 ); 340 bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count' ); 341 remove_filter( 'add_post_metadata', $callback, 10 ); 342 343 $this->assertSame( 2, (int) get_post_meta( $post_id, '_bbp_test_count', true ) ); 344 } 345 346 /** 347 * @covers ::bbp_bump_count_meta 348 */ 349 public function test_bbp_bump_count_meta_runs_standard_metadata_actions() { 350 $post_id = self::factory()->post->create(); 351 $updated = 0; 352 $legacy_updated = 0; 353 $callback = function( $meta_id, $updated_post_id, $meta_key, $meta_value ) use ( $post_id, &$updated ) { 354 if ( ( $post_id === $updated_post_id ) && ( '_bbp_test_count' === $meta_key ) && ( 2 === $meta_value ) ) { 355 $updated++; 356 } 357 }; 358 $legacy_callback = function( $meta_id, $updated_post_id, $meta_key, $meta_value ) use ( $post_id, &$legacy_updated ) { 359 if ( ( $post_id === $updated_post_id ) && ( '_bbp_test_count' === $meta_key ) && ( 2 === $meta_value ) ) { 360 $legacy_updated++; 361 } 362 }; 363 364 update_post_meta( $post_id, '_bbp_test_count', 1 ); 365 add_action( 'updated_post_meta', $callback, 10, 4 ); 366 add_action( 'updated_postmeta', $legacy_callback, 10, 4 ); 367 bbp_bump_count_meta( 'post', $post_id, '_bbp_test_count' ); 368 remove_action( 'updated_post_meta', $callback, 10 ); 369 remove_action( 'updated_postmeta', $legacy_callback, 10 ); 370 371 $this->assertSame( 1, $updated ); 372 $this->assertSame( 1, $legacy_updated ); 373 } 374 375 /** 34 376 * @covers ::bbp_rewrite 35 377 * @todo Implement test_bbp_rewrite(). -
branches/2.6/tests/phpunit/testcases/forums/functions/counts.php
r7454 r7468 11 11 12 12 /** 13 * @covers BBPress::register_meta 14 */ 15 public function test_hidden_total_count_meta_is_registered() { 16 bbpress()->register_meta(); 17 18 $registered = get_registered_meta_keys( 'post' ); 19 20 $this->assertArrayHasKey( '_bbp_total_topic_count_hidden', $registered ); 21 $this->assertArrayHasKey( '_bbp_total_reply_count_hidden', $registered ); 22 } 23 24 /** 13 25 * @covers ::bbp_get_countable_forum_statuses 14 26 */ … … 34 46 35 47 /** 48 * @covers ::bbp_update_forum_topic_count_hidden 49 * @covers ::bbp_forum_query_subforum_ids 50 * @ticket BBP3678 51 */ 52 public function test_hidden_topic_counts_exclude_trashed_subforums() { 53 $parent_id = $this->factory->forum->create( array( 54 'forum_meta' => array( 'forum_type' => 'category' ), 55 ) ); 56 $forum_id = $this->factory->forum->create( array( 57 'post_parent' => $parent_id, 58 'forum_meta' => array( 'forum_id' => $parent_id ), 59 ) ); 60 $this->factory->topic->create( array( 61 'post_parent' => $forum_id, 62 'post_status' => bbp_get_spam_status_id(), 63 'topic_meta' => array( 'forum_id' => $forum_id ), 64 ) ); 65 66 $this->assertSame( 0, bbp_update_forum_topic_count_hidden( $parent_id ) ); 67 $this->assertSame( 1, bbp_get_forum_topic_count_hidden( $parent_id, true, true ) ); 68 69 wp_trash_post( $forum_id ); 70 71 $this->assertSame( 0, bbp_update_forum_topic_count_hidden( $parent_id ) ); 72 } 73 74 /** 75 * @covers ::bbp_bump_forum_ancestor_count 76 * @ticket BBP3678 77 */ 78 public function test_bbp_bump_forum_ancestor_count_stops_outside_forum_hierarchy() { 79 $forum_id = $this->factory->forum->create(); 80 $topic_id = $this->factory->topic->create( array( 81 'post_parent' => $forum_id, 82 'topic_meta' => array( 'forum_id' => $forum_id ), 83 ) ); 84 $subforum_id = $this->factory->forum->create( array( 85 'post_parent' => $topic_id, 86 'forum_meta' => array( 'forum_id' => $topic_id ), 87 ) ); 88 89 update_post_meta( $forum_id, '_bbp_total_topic_count', 2 ); 90 update_post_meta( $topic_id, '_bbp_total_topic_count', 3 ); 91 92 bbp_bump_forum_ancestor_count( $subforum_id, '_bbp_total_topic_count', 1 ); 93 94 $this->assertSame( 2, (int) get_post_meta( $forum_id, '_bbp_total_topic_count', true ) ); 95 $this->assertSame( 3, (int) get_post_meta( $topic_id, '_bbp_total_topic_count', true ) ); 96 } 97 98 /** 36 99 * Generic function to test the forum counts with a new topic 37 100 * … … 90 153 91 154 /** 155 * @covers ::bbp_update_counts_on_transition_post_status 156 */ 157 public function test_bbp_forum_draft_topic_is_not_counted_as_hidden() { 158 $user_id = $this->factory->user->create(); 159 $forum_id = $this->factory->forum->create(); 160 $topic_id = $this->factory->topic->create( array( 161 'post_author' => $user_id, 162 'post_parent' => $forum_id, 163 'post_status' => 'draft', 164 'topic_meta' => array( 'forum_id' => $forum_id ), 165 ) ); 166 167 $this->assertSame( 0, bbp_get_forum_topic_count( $forum_id, false, true ) ); 168 $this->assertSame( 0, bbp_get_forum_topic_count_hidden( $forum_id, false, true ) ); 169 170 wp_update_post( array( 171 'ID' => $topic_id, 172 'post_status' => bbp_get_pending_status_id(), 173 ) ); 174 $this->assertSame( 1, bbp_get_forum_topic_count_hidden( $forum_id, false, true ) ); 175 176 wp_update_post( array( 177 'ID' => $topic_id, 178 'post_status' => 'draft', 179 ) ); 180 $this->assertSame( 0, bbp_get_forum_topic_count_hidden( $forum_id, false, true ) ); 181 } 182 183 /** 92 184 * Generic function to test the forum counts on a trashed/untrashed topic 93 185 */ … … 266 358 267 359 /** 360 * @covers ::bbp_update_counts_on_transition_post_status 361 */ 362 public function test_bbp_forum_reply_count_stays_excluded_when_pending_topic_is_trashed_and_restored() { 363 $forum_id = $this->factory->forum->create(); 364 $topic_id = $this->factory->topic->create( array( 365 'post_parent' => $forum_id, 366 'topic_meta' => array( 'forum_id' => $forum_id ), 367 ) ); 368 369 $this->factory->reply->create( array( 370 'post_parent' => $topic_id, 371 'post_status' => bbp_get_public_status_id(), 372 'reply_meta' => array( 373 'forum_id' => $forum_id, 374 'topic_id' => $topic_id, 375 ), 376 ) ); 377 378 bbp_unapprove_topic( $topic_id ); 379 $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, true, true ) ); 380 381 wp_trash_post( $topic_id ); 382 $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, true, true ) ); 383 384 wp_untrash_post( $topic_id ); 385 $this->assertSame( bbp_get_pending_status_id(), bbp_get_topic_status( $topic_id ) ); 386 $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, true, true ) ); 387 } 388 389 /** 390 * @covers ::bbp_update_counts_on_transition_post_status 391 * @ticket BBP3678 392 */ 393 public function test_topic_status_transition_updates_ancestor_forum_reply_counts() { 394 $parent_id = $this->factory->forum->create( array( 395 'forum_meta' => array( 'forum_type' => 'category' ), 396 ) ); 397 $forum_id = $this->factory->forum->create( array( 398 'post_parent' => $parent_id, 399 'forum_meta' => array( 'forum_id' => $parent_id ), 400 ) ); 401 $topic_id = $this->factory->topic->create( array( 402 'post_parent' => $forum_id, 403 'topic_meta' => array( 'forum_id' => $forum_id ), 404 ) ); 405 $this->factory->reply->create( array( 406 'post_parent' => $topic_id, 407 'reply_meta' => array( 408 'forum_id' => $forum_id, 409 'topic_id' => $topic_id, 410 ), 411 ) ); 412 413 $this->assertSame( 1, bbp_get_forum_reply_count( $parent_id, true, true ) ); 414 415 bbp_unapprove_topic( $topic_id ); 416 $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, true, true ) ); 417 $this->assertSame( 0, bbp_get_forum_reply_count( $parent_id, true, true ) ); 418 419 bbp_approve_topic( $topic_id ); 420 $this->assertSame( 1, bbp_get_forum_reply_count( $forum_id, true, true ) ); 421 $this->assertSame( 1, bbp_get_forum_reply_count( $parent_id, true, true ) ); 422 } 423 424 /** 425 * @covers ::bbp_update_counts_on_transition_post_status 426 */ 427 public function test_bbp_forum_reply_count_excludes_new_public_reply_in_pending_topic() { 428 $forum_id = $this->factory->forum->create(); 429 $topic_id = $this->factory->topic->create( array( 430 'post_parent' => $forum_id, 431 'post_status' => bbp_get_pending_status_id(), 432 'topic_meta' => array( 'forum_id' => $forum_id ), 433 ) ); 434 435 $this->factory->reply->create( array( 436 'post_parent' => $topic_id, 437 'post_status' => bbp_get_public_status_id(), 438 'reply_meta' => array( 439 'forum_id' => $forum_id, 440 'topic_id' => $topic_id, 441 ), 442 ) ); 443 444 $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, true, true ) ); 445 $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $forum_id, true, true ) ); 446 $this->assertSame( 1, bbp_get_topic_reply_count( $topic_id, true ) ); 447 $this->assertSame( 0, bbp_update_forum_reply_count( $forum_id ) ); 448 } 449 450 /** 268 451 * @covers ::bbp_bump_forum_topic_count 269 452 */ … … 278 461 $count = bbp_get_forum_topic_count( $f ); 279 462 $this->assertSame( '1', $count ); 463 } 464 465 /** 466 * @covers ::bbp_bump_forum_topic_count 467 * @ticket BBP3678 468 */ 469 public function test_bbp_bump_forum_topic_count_preserves_an_interleaved_update() { 470 $forum_id = $this->factory->forum->create(); 471 $interleaved = false; 472 $callback = function( $count, $filtered_forum_id ) use ( &$interleaved ) { 473 if ( ! $interleaved ) { 474 $interleaved = true; 475 bbp_bump_forum_topic_count( $filtered_forum_id ); 476 } 477 478 return $count; 479 }; 480 481 add_filter( 'bbp_get_forum_topic_count_int', $callback, 10, 2 ); 482 bbp_bump_forum_topic_count( $forum_id ); 483 remove_filter( 'bbp_get_forum_topic_count_int', $callback, 10 ); 484 485 $this->assertSame( 2, bbp_get_forum_topic_count( $forum_id, false, true ) ); 486 $this->assertSame( 2, bbp_get_forum_topic_count( $forum_id, true, true ) ); 280 487 } 281 488 … … 702 909 703 910 /** 911 * @covers ::bbp_update_forum_topic_count_hidden 912 */ 913 public function test_bbp_update_forum_topic_count_hidden_rebuilds_total_counts() { 914 $parent_id = $this->factory->forum->create(); 915 $child_id = $this->factory->forum->create( array( 'post_parent' => $parent_id ) ); 916 917 $this->factory->topic->create( array( 918 'post_parent' => $child_id, 919 'post_status' => bbp_get_pending_status_id(), 920 'topic_meta' => array( 'forum_id' => $child_id ), 921 ) ); 922 923 update_post_meta( $parent_id, '_bbp_total_topic_count_hidden', 99 ); 924 update_post_meta( $child_id, '_bbp_total_topic_count_hidden', 99 ); 925 926 $this->assertSame( 0, bbp_update_forum_topic_count_hidden( $parent_id ) ); 927 $this->assertSame( 0, bbp_get_forum_topic_count_hidden( $parent_id, false, true ) ); 928 $this->assertSame( 1, bbp_get_forum_topic_count_hidden( $parent_id, true, true ) ); 929 $this->assertSame( 1, bbp_get_forum_topic_count_hidden( $child_id, true, true ) ); 930 } 931 932 /** 704 933 * @covers ::bbp_update_forum_reply_count 705 934 */ -
branches/2.6/tests/phpunit/testcases/forums/functions/query.php
r6077 r7468 69 69 70 70 /** 71 * @covers ::bbp_forum_query_subforum_ids 72 * @ticket BBP3678 73 */ 74 public function test_bbp_forum_query_subforum_ids_excludes_uncountable_statuses() { 75 $this->assertSame( array(), bbp_forum_query_subforum_ids( 0 ) ); 76 77 $parent_id = $this->factory->forum->create(); 78 $public_id = $this->factory->forum->create( array( 79 'post_parent' => $parent_id, 80 ) ); 81 $hidden_id = $this->factory->forum->create( array( 82 'post_parent' => $parent_id, 83 'post_status' => bbp_get_hidden_status_id(), 84 ) ); 85 $private_id = $this->factory->forum->create( array( 86 'post_parent' => $parent_id, 87 'post_status' => bbp_get_private_status_id(), 88 ) ); 89 $trash_id = $this->factory->forum->create( array( 90 'post_parent' => $parent_id, 91 ) ); 92 wp_update_post( array( 93 'ID' => $trash_id, 94 'post_status' => bbp_get_trash_status_id(), 95 ) ); 96 97 $this->assertEqualSets( array( $public_id, $private_id, $hidden_id ), bbp_forum_query_subforum_ids( $parent_id ) ); 98 } 99 100 /** 71 101 * @covers ::bbp_forum_query_last_reply_id 72 102 */ -
branches/2.6/tests/phpunit/testcases/replies/functions/reply.php
r7456 r7468 250 250 /** 251 251 * @covers ::bbp_move_reply_count 252 * @todo Implement test_bbp_move_reply_count().253 252 */ 254 253 public function test_bbp_move_reply_count() { 255 // Remove the following lines when you implement this test. 256 $this->markTestIncomplete( 257 'This test has not been implemented yet.' 258 ); 254 $topic_author_id = $this->factory->user->create(); 255 $reply_author_id = $this->factory->user->create(); 256 $destination_author_id = $this->factory->user->create(); 257 $source_parent_id = $this->factory->forum->create( array( 'forum_meta' => array( 'forum_type' => 'category' ) ) ); 258 $destination_parent_id = $this->factory->forum->create( array( 'forum_meta' => array( 'forum_type' => 'category' ) ) ); 259 $source_forum_id = $this->factory->forum->create( array( 'post_parent' => $source_parent_id ) ); 260 $destination_forum_id = $this->factory->forum->create( array( 'post_parent' => $destination_parent_id ) ); 261 $source_topic_id = $this->factory->topic->create( array( 262 'post_author' => $topic_author_id, 263 'post_parent' => $source_forum_id, 264 'topic_meta' => array( 'forum_id' => $source_forum_id ), 265 ) ); 266 $destination_topic_id = $this->factory->topic->create( array( 267 'post_author' => $destination_author_id, 268 'post_parent' => $destination_forum_id, 269 'topic_meta' => array( 'forum_id' => $destination_forum_id ), 270 ) ); 271 $reply_id = $this->factory->reply->create( array( 272 'post_author' => $reply_author_id, 273 'post_parent' => $source_topic_id, 274 'reply_meta' => array( 275 'forum_id' => $source_forum_id, 276 'topic_id' => $source_topic_id, 277 ), 278 ) ); 279 280 wp_update_post( array( 281 'ID' => $reply_id, 282 'post_parent' => $destination_topic_id, 283 ) ); 284 bbp_update_reply_topic_id( $reply_id, $destination_topic_id ); 285 bbp_update_reply_forum_id( $reply_id, $destination_forum_id ); 286 bbp_move_reply_count( $reply_id, $source_topic_id, $destination_topic_id ); 287 288 $this->assertSame( 0, bbp_get_forum_reply_count( $source_forum_id, true, true ) ); 289 $this->assertSame( 1, bbp_get_forum_reply_count( $destination_forum_id, true, true ) ); 290 $this->assertSame( 0, bbp_get_forum_reply_count( $source_parent_id, true, true ) ); 291 $this->assertSame( 1, bbp_get_forum_reply_count( $destination_parent_id, true, true ) ); 292 $this->assertSame( 0, bbp_get_topic_reply_count( $source_topic_id, true ) ); 293 $this->assertSame( 1, bbp_get_topic_reply_count( $destination_topic_id, true ) ); 294 $this->assertEqualSets( array( $topic_author_id ), bbp_get_topic_engagements( $source_topic_id ) ); 295 $this->assertEqualSets( array( $reply_author_id, $destination_author_id ), bbp_get_topic_engagements( $destination_topic_id ) ); 296 $this->assertSame( 1, bbp_get_topic_voice_count( $source_topic_id, true ) ); 297 $this->assertSame( 2, bbp_get_topic_voice_count( $destination_topic_id, true ) ); 298 } 299 300 /** 301 * @covers ::bbp_move_reply_count 302 */ 303 public function test_bbp_move_reply_count_updates_hidden_counts() { 304 $source_parent_id = $this->factory->forum->create( array( 'forum_meta' => array( 'forum_type' => 'category' ) ) ); 305 $destination_parent_id = $this->factory->forum->create( array( 'forum_meta' => array( 'forum_type' => 'category' ) ) ); 306 $source_forum_id = $this->factory->forum->create( array( 'post_parent' => $source_parent_id ) ); 307 $destination_forum_id = $this->factory->forum->create( array( 'post_parent' => $destination_parent_id ) ); 308 $source_topic_id = $this->factory->topic->create( array( 309 'post_parent' => $source_forum_id, 310 'topic_meta' => array( 'forum_id' => $source_forum_id ), 311 ) ); 312 $destination_topic_id = $this->factory->topic->create( array( 313 'post_parent' => $destination_forum_id, 314 'topic_meta' => array( 'forum_id' => $destination_forum_id ), 315 ) ); 316 $reply_id = $this->factory->reply->create( array( 317 'post_parent' => $source_topic_id, 318 'post_status' => bbp_get_pending_status_id(), 319 'reply_meta' => array( 320 'forum_id' => $source_forum_id, 321 'topic_id' => $source_topic_id, 322 ), 323 ) ); 324 325 wp_update_post( array( 326 'ID' => $reply_id, 327 'post_parent' => $destination_topic_id, 328 ) ); 329 bbp_update_reply_topic_id( $reply_id, $destination_topic_id ); 330 bbp_update_reply_forum_id( $reply_id, $destination_forum_id ); 331 bbp_move_reply_count( $reply_id, $source_topic_id, $destination_topic_id ); 332 333 $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $source_forum_id, true, true ) ); 334 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $destination_forum_id, true, true ) ); 335 $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $source_parent_id, true, true ) ); 336 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $destination_parent_id, true, true ) ); 337 $this->assertSame( 0, bbp_get_topic_reply_count_hidden( $source_topic_id, true ) ); 338 $this->assertSame( 1, bbp_get_topic_reply_count_hidden( $destination_topic_id, true ) ); 339 } 340 341 /** 342 * @covers ::bbp_move_reply_count 343 */ 344 public function test_bbp_move_reply_count_transfers_contribution_when_converted_to_topic() { 345 $user_id = $this->factory->user->create(); 346 $forum_id = $this->factory->forum->create(); 347 $topic_id = $this->factory->topic->create( array( 348 'post_parent' => $forum_id, 349 'topic_meta' => array( 'forum_id' => $forum_id ), 350 ) ); 351 $reply_id = $this->factory->reply->create( array( 352 'post_author' => $user_id, 353 'post_parent' => $topic_id, 354 'reply_meta' => array( 355 'forum_id' => $forum_id, 356 'topic_id' => $topic_id, 357 ), 358 ) ); 359 360 wp_update_post( array( 361 'ID' => $reply_id, 362 'post_parent' => $forum_id, 363 'post_type' => bbp_get_topic_post_type(), 364 ) ); 365 bbp_update_topic_forum_id( $reply_id, $forum_id ); 366 bbp_update_topic_topic_id( $reply_id ); 367 bbp_move_reply_count( $reply_id, $topic_id, $reply_id ); 368 369 $this->assertSame( 0, bbp_get_user_reply_count( $user_id, true ) ); 370 $this->assertSame( 1, bbp_get_user_topic_count( $user_id, true ) ); 371 $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, true, true ) ); 372 $this->assertSame( 2, bbp_get_forum_topic_count( $forum_id, true, true ) ); 259 373 } 260 374 -
branches/2.6/tests/phpunit/testcases/topics/functions/counts.php
r7451 r7468 92 92 93 93 /** 94 * @covers ::bbp_update_counts_on_transition_post_status 95 */ 96 public function test_bbp_topic_draft_reply_is_not_counted_as_hidden() { 97 $forum_id = $this->factory->forum->create(); 98 $topic_id = $this->factory->topic->create( array( 99 'post_parent' => $forum_id, 100 'topic_meta' => array( 'forum_id' => $forum_id ), 101 ) ); 102 $reply_id = $this->factory->reply->create( array( 103 'post_parent' => $topic_id, 104 'post_status' => 'draft', 105 'reply_meta' => array( 106 'forum_id' => $forum_id, 107 'topic_id' => $topic_id, 108 ), 109 ) ); 110 111 $this->assertSame( 0, bbp_get_topic_reply_count_hidden( $topic_id, true ) ); 112 $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) ); 113 114 wp_update_post( array( 115 'ID' => $reply_id, 116 'post_status' => bbp_get_pending_status_id(), 117 ) ); 118 $this->assertSame( 1, bbp_get_topic_reply_count_hidden( $topic_id, true ) ); 119 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) ); 120 121 wp_update_post( array( 122 'ID' => $reply_id, 123 'post_status' => 'draft', 124 ) ); 125 $this->assertSame( 0, bbp_get_topic_reply_count_hidden( $topic_id, true ) ); 126 $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) ); 127 } 128 129 /** 94 130 * Generic function to test the topic counts on a deleted reply 95 131 */ … … 380 416 381 417 /** 418 * @covers ::bbp_bump_topic_reply_count 419 * @ticket BBP3678 420 */ 421 public function test_bbp_bump_topic_reply_count_preserves_an_interleaved_update() { 422 $topic_id = $this->factory->topic->create(); 423 $interleaved = false; 424 $callback = function( $count, $filtered_topic_id ) use ( &$interleaved ) { 425 if ( ! $interleaved ) { 426 $interleaved = true; 427 bbp_bump_topic_reply_count( $filtered_topic_id ); 428 } 429 430 return $count; 431 }; 432 433 add_filter( 'bbp_get_topic_reply_count_int', $callback, 10, 2 ); 434 bbp_bump_topic_reply_count( $topic_id ); 435 remove_filter( 'bbp_get_topic_reply_count_int', $callback, 10 ); 436 437 $this->assertSame( 2, bbp_get_topic_reply_count( $topic_id, true ) ); 438 } 439 440 /** 382 441 * @covers ::bbp_decrease_topic_reply_count 383 442 */ … … 589 648 $count = bbp_get_topic_voice_count( $t ); 590 649 $this->assertSame( '2', $count ); 650 } 651 652 /** 653 * @covers ::bbp_get_topic_engagements_raw 654 * @covers ::bbp_recalculate_topic_engagements 655 * @covers ::bbp_update_topic_voice_count 656 * @ticket BBP3678 657 */ 658 public function test_topic_voice_count_honors_filtered_public_reply_statuses() { 659 $topic_author_id = $this->factory->user->create(); 660 $reply_author_id = $this->factory->user->create(); 661 $forum_id = $this->factory->forum->create(); 662 $topic_id = $this->factory->topic->create( array( 663 'post_author' => $topic_author_id, 664 'post_parent' => $forum_id, 665 'topic_meta' => array( 'forum_id' => $forum_id ), 666 ) ); 667 $public_statuses = function( $statuses ) { 668 $statuses[] = 'private'; 669 return $statuses; 670 }; 671 672 add_filter( 'bbp_get_public_reply_statuses', $public_statuses ); 673 674 try { 675 $this->factory->reply->create( array( 676 'post_author' => $reply_author_id, 677 'post_parent' => $topic_id, 678 'post_status' => 'private', 679 'reply_meta' => array( 680 'forum_id' => $forum_id, 681 'topic_id' => $topic_id, 682 ), 683 ) ); 684 685 bbp_recalculate_topic_engagements( $topic_id, true ); 686 bbp_update_topic_voice_count( $topic_id ); 687 } finally { 688 remove_filter( 'bbp_get_public_reply_statuses', $public_statuses, 10 ); 689 } 690 691 $this->assertSame( array( $topic_author_id, $reply_author_id ), bbp_get_topic_engagements( $topic_id ) ); 692 $this->assertSame( 2, bbp_get_topic_voice_count( $topic_id, true ) ); 591 693 } 592 694 -
branches/2.6/tests/phpunit/testcases/topics/functions/topic.php
r7456 r7468 271 271 /** 272 272 * @covers ::bbp_merge_topic_count 273 * @todo Implement test_bbp_merge_topic_count().274 273 */ 275 274 public function test_bbp_merge_topic_count() { 276 // Remove the following lines when you implement this test. 277 $this->markTestIncomplete( 278 'This test has not been implemented yet.' 279 ); 275 $source_author_id = $this->factory->user->create(); 276 $reply_author_id = $this->factory->user->create(); 277 $destination_author_id = $this->factory->user->create(); 278 $source_parent_id = $this->factory->forum->create( array( 'forum_meta' => array( 'forum_type' => 'category' ) ) ); 279 $destination_parent_id = $this->factory->forum->create( array( 'forum_meta' => array( 'forum_type' => 'category' ) ) ); 280 $source_forum_id = $this->factory->forum->create( array( 'post_parent' => $source_parent_id ) ); 281 $destination_forum_id = $this->factory->forum->create( array( 'post_parent' => $destination_parent_id ) ); 282 $source_topic_id = $this->factory->topic->create( array( 283 'post_author' => $source_author_id, 284 'post_parent' => $source_forum_id, 285 'topic_meta' => array( 'forum_id' => $source_forum_id ), 286 ) ); 287 $destination_topic_id = $this->factory->topic->create( array( 288 'post_author' => $destination_author_id, 289 'post_parent' => $destination_forum_id, 290 'topic_meta' => array( 'forum_id' => $destination_forum_id ), 291 ) ); 292 $reply_id = $this->factory->reply->create( array( 293 'post_author' => $reply_author_id, 294 'post_parent' => $source_topic_id, 295 'reply_meta' => array( 296 'forum_id' => $source_forum_id, 297 'topic_id' => $source_topic_id, 298 ), 299 ) ); 300 301 wp_update_post( array( 302 'ID' => $reply_id, 303 'post_parent' => $destination_topic_id, 304 ) ); 305 bbp_update_reply_topic_id( $reply_id, $destination_topic_id ); 306 bbp_update_reply_forum_id( $reply_id, $destination_forum_id ); 307 wp_update_post( array( 308 'ID' => $source_topic_id, 309 'post_parent' => $destination_topic_id, 310 'post_type' => bbp_get_reply_post_type(), 311 ) ); 312 bbp_update_reply_topic_id( $source_topic_id, $destination_topic_id ); 313 bbp_update_reply_forum_id( $source_topic_id, $destination_forum_id ); 314 bbp_merge_topic_count( $destination_topic_id, $source_topic_id, $source_forum_id ); 315 316 $this->assertSame( 0, bbp_get_forum_topic_count( $source_forum_id, true, true ) ); 317 $this->assertSame( 0, bbp_get_forum_reply_count( $source_forum_id, true, true ) ); 318 $this->assertSame( 2, bbp_get_forum_reply_count( $destination_forum_id, true, true ) ); 319 $this->assertSame( 0, bbp_get_forum_topic_count( $source_parent_id, true, true ) ); 320 $this->assertSame( 0, bbp_get_forum_reply_count( $source_parent_id, true, true ) ); 321 $this->assertSame( 2, bbp_get_forum_reply_count( $destination_parent_id, true, true ) ); 322 $this->assertSame( 2, bbp_get_topic_reply_count( $destination_topic_id, true ) ); 323 $this->assertSame( 0, bbp_get_user_topic_count( $source_author_id, true ) ); 324 $this->assertSame( 1, bbp_get_user_reply_count( $source_author_id, true ) ); 325 $this->assertEqualSets( array( $source_author_id, $reply_author_id, $destination_author_id ), bbp_get_topic_engagements( $destination_topic_id ) ); 326 $this->assertSame( 3, bbp_get_topic_voice_count( $destination_topic_id, true ) ); 280 327 } 281 328 … … 293 340 /** 294 341 * @covers ::bbp_split_topic_count 295 * @todo Implement test_bbp_split_topic_count().296 342 */ 297 343 public function test_bbp_split_topic_count() { 298 // Remove the following lines when you implement this test. 299 $this->markTestIncomplete( 300 'This test has not been implemented yet.' 301 ); 344 $source_parent_id = $this->factory->forum->create( array( 'forum_meta' => array( 'forum_type' => 'category' ) ) ); 345 $destination_parent_id = $this->factory->forum->create( array( 'forum_meta' => array( 'forum_type' => 'category' ) ) ); 346 $source_forum_id = $this->factory->forum->create( array( 'post_parent' => $source_parent_id ) ); 347 $destination_forum_id = $this->factory->forum->create( array( 'post_parent' => $destination_parent_id ) ); 348 $source_topic_id = $this->factory->topic->create( array( 349 'post_parent' => $source_forum_id, 350 'topic_meta' => array( 'forum_id' => $source_forum_id ), 351 ) ); 352 $destination_topic_id = $this->factory->topic->create( array( 353 'post_parent' => $destination_forum_id, 354 'topic_meta' => array( 'forum_id' => $destination_forum_id ), 355 ) ); 356 $reply_id = $this->factory->reply->create( array( 357 'post_parent' => $source_topic_id, 358 'reply_meta' => array( 359 'forum_id' => $source_forum_id, 360 'topic_id' => $source_topic_id, 361 ), 362 ) ); 363 364 wp_update_post( array( 365 'ID' => $reply_id, 366 'post_parent' => $destination_topic_id, 367 ) ); 368 bbp_update_reply_topic_id( $reply_id, $destination_topic_id ); 369 bbp_update_reply_forum_id( $reply_id, $destination_forum_id ); 370 bbp_split_topic_count( $reply_id, $source_topic_id, $destination_topic_id ); 371 372 $this->assertSame( 0, bbp_get_forum_reply_count( $source_forum_id, true, true ) ); 373 $this->assertSame( 1, bbp_get_forum_reply_count( $destination_forum_id, true, true ) ); 374 $this->assertSame( 0, bbp_get_forum_reply_count( $source_parent_id, true, true ) ); 375 $this->assertSame( 1, bbp_get_forum_reply_count( $destination_parent_id, true, true ) ); 376 $this->assertSame( 0, bbp_get_topic_reply_count( $source_topic_id, true ) ); 377 $this->assertSame( 1, bbp_get_topic_reply_count( $destination_topic_id, true ) ); 378 } 379 380 /** 381 * @covers ::bbp_split_topic_count 382 * @ticket BBP3678 383 */ 384 public function test_bbp_split_topic_count_updates_converted_reply_counts_and_engagements() { 385 $source_author_id = $this->factory->user->create(); 386 $reply_author_id = $this->factory->user->create(); 387 $forum_id = $this->factory->forum->create(); 388 $source_topic_id = $this->factory->topic->create( array( 389 'post_author' => $source_author_id, 390 'post_parent' => $forum_id, 391 'topic_meta' => array( 'forum_id' => $forum_id ), 392 ) ); 393 $from_reply_id = $this->factory->reply->create( array( 394 'post_author' => $reply_author_id, 395 'post_parent' => $source_topic_id, 396 'reply_meta' => array( 397 'forum_id' => $forum_id, 398 'topic_id' => $source_topic_id, 399 ), 400 ) ); 401 402 wp_update_post( array( 403 'ID' => $from_reply_id, 404 'post_parent' => $forum_id, 405 'post_type' => bbp_get_topic_post_type(), 406 ) ); 407 bbp_update_topic_topic_id( $from_reply_id ); 408 bbp_update_topic_forum_id( $from_reply_id, $forum_id ); 409 410 bbp_split_topic_count( $from_reply_id, $source_topic_id, $from_reply_id ); 411 412 $this->assertSame( 2, bbp_get_forum_topic_count( $forum_id, true, true ) ); 413 $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, true, true ) ); 414 $this->assertSame( 1, bbp_get_user_topic_count( $source_author_id, true ) ); 415 $this->assertSame( 1, bbp_get_user_topic_count( $reply_author_id, true ) ); 416 $this->assertSame( 0, bbp_get_user_reply_count( $reply_author_id, true ) ); 417 $this->assertSame( array( $source_author_id ), bbp_get_topic_engagements( $source_topic_id ) ); 418 $this->assertSame( array( $reply_author_id ), bbp_get_topic_engagements( $from_reply_id ) ); 419 $this->assertSame( 1, bbp_get_topic_voice_count( $source_topic_id, true ) ); 420 $this->assertSame( 1, bbp_get_topic_voice_count( $from_reply_id, true ) ); 302 421 } 303 422 -
branches/2.6/tests/phpunit/testcases/users/functions/counts.php
r7451 r7468 246 246 $count = bbp_get_user_topic_count( $u, $integer ); 247 247 $this->assertSame( $int_value + 1, $count ); 248 } 249 250 /** 251 * @covers ::bbp_bump_user_topic_count 252 * @covers ::bbp_update_user_topic_count 253 * @ticket BBP3678 254 */ 255 public function test_bbp_bump_user_topic_count_preserves_an_interleaved_update() { 256 $user_id = $this->factory->user->create(); 257 $interleaved = false; 258 $callback = function( $count, $filtered_user_id ) use ( &$interleaved ) { 259 if ( ! $interleaved ) { 260 $interleaved = true; 261 bbp_bump_user_topic_count( $filtered_user_id ); 262 } 263 264 return $count; 265 }; 266 267 bbp_update_user_topic_count( $user_id, 0 ); 268 add_filter( 'bbp_get_user_topic_count_int', $callback, 10, 2 ); 269 bbp_bump_user_topic_count( $user_id ); 270 remove_filter( 'bbp_get_user_topic_count_int', $callback, 10 ); 271 272 $this->assertSame( 2, bbp_get_user_topic_count( $user_id, true ) ); 273 } 274 275 /** 276 * @covers ::bbp_bump_user_topic_count 277 * @covers ::bbp_update_user_topic_count 278 */ 279 public function test_bbp_bump_user_topic_count_preserves_count_filters() { 280 $user_id = $this->factory->user->create(); 281 $atomic_calls = 0; 282 $bump = function( $count ) { 283 return $count + 4; 284 }; 285 $update = function( $count ) { 286 return $count + 3; 287 }; 288 $atomic = function( $check ) use ( &$atomic_calls ) { 289 $atomic_calls++; 290 return $check; 291 }; 292 293 bbp_update_user_topic_count( $user_id, 5 ); 294 add_filter( 'bbp_bump_user_topic_count', $bump ); 295 add_filter( 'bbp_update_user_topic_count', $update ); 296 add_filter( 'bbp_pre_bump_count_meta', $atomic ); 297 bbp_bump_user_topic_count( $user_id ); 298 remove_filter( 'bbp_bump_user_topic_count', $bump, 10 ); 299 remove_filter( 'bbp_update_user_topic_count', $update, 10 ); 300 remove_filter( 'bbp_pre_bump_count_meta', $atomic, 10 ); 301 302 $this->assertSame( 13, bbp_get_user_topic_count( $user_id, true ) ); 303 $this->assertSame( 0, $atomic_calls ); 304 } 305 306 /** 307 * @covers ::bbp_bump_user_reply_count 308 * @covers ::bbp_update_user_reply_count 309 */ 310 public function test_bbp_bump_user_reply_count_preserves_an_absolute_update_filter() { 311 $user_id = $this->factory->user->create(); 312 $atomic_calls = 0; 313 $update = function() { 314 return 100; 315 }; 316 $atomic = function( $check ) use ( &$atomic_calls ) { 317 $atomic_calls++; 318 return $check; 319 }; 320 321 bbp_update_user_reply_count( $user_id, 5 ); 322 add_filter( 'bbp_update_user_reply_count', $update ); 323 add_filter( 'bbp_pre_bump_count_meta', $atomic ); 324 bbp_bump_user_reply_count( $user_id ); 325 remove_filter( 'bbp_update_user_reply_count', $update, 10 ); 326 remove_filter( 'bbp_pre_bump_count_meta', $atomic, 10 ); 327 328 $this->assertSame( 100, bbp_get_user_reply_count( $user_id, true ) ); 329 $this->assertSame( 0, $atomic_calls ); 330 } 331 332 /** 333 * @covers ::bbp_bump_user_topic_count 334 * @covers ::bbp_update_user_topic_count 335 * @ticket BBP3678 336 */ 337 public function test_bbp_bump_user_topic_count_uses_the_current_site_option() { 338 if ( ! is_multisite() ) { 339 $this->markTestSkipped( 'Requires multisite.' ); 340 } 341 342 $user_id = $this->factory->user->create(); 343 $site_id = $this->factory->blog->create(); 344 345 bbp_update_user_topic_count( $user_id, 5 ); 346 switch_to_blog( $site_id ); 347 348 try { 349 bbp_update_user_topic_count( $user_id, 7 ); 350 bbp_bump_user_topic_count( $user_id ); 351 $this->assertSame( 8, bbp_get_user_topic_count( $user_id, true ) ); 352 $this->assertSame( 8, (int) get_user_meta( $user_id, bbp_db()->get_blog_prefix() . '_bbp_topic_count', true ) ); 353 } finally { 354 restore_current_blog(); 355 } 356 357 $this->assertSame( 5, bbp_get_user_topic_count( $user_id, true ) ); 248 358 } 249 359 … … 401 511 402 512 /** 513 * @covers ::bbp_update_counts_on_user_reassignment 514 * @ticket BBP3678 515 */ 516 public function test_deleting_user_with_reassignment_updates_counts_and_engagements() { 517 $deleted_user_id = $this->factory->user->create(); 518 $reassign_user_id = $this->factory->user->create(); 519 $forum_id = $this->factory->forum->create(); 520 $topic_id = $this->factory->topic->create( array( 521 'post_author' => $deleted_user_id, 522 'post_parent' => $forum_id, 523 'topic_meta' => array( 'forum_id' => $forum_id ), 524 ) ); 525 $deleted_user_reply_id = $this->factory->reply->create( array( 526 'post_author' => $deleted_user_id, 527 'post_parent' => $topic_id, 528 'reply_meta' => array( 529 'forum_id' => $forum_id, 530 'topic_id' => $topic_id, 531 ), 532 ) ); 533 $this->factory->reply->create( array( 534 'post_author' => $reassign_user_id, 535 'post_parent' => $topic_id, 536 'reply_meta' => array( 537 'forum_id' => $forum_id, 538 'topic_id' => $topic_id, 539 ), 540 ) ); 541 $reply_only_topic_id = $this->factory->topic->create( array( 542 'post_author' => $reassign_user_id, 543 'post_parent' => $forum_id, 544 'topic_meta' => array( 'forum_id' => $forum_id ), 545 ) ); 546 $this->factory->reply->create( array( 547 'post_author' => $deleted_user_id, 548 'post_parent' => $reply_only_topic_id, 549 'reply_meta' => array( 550 'forum_id' => $forum_id, 551 'topic_id' => $reply_only_topic_id, 552 ), 553 ) ); 554 555 $this->assertSame( 1, bbp_get_user_topic_count( $deleted_user_id, true ) ); 556 $this->assertSame( 2, bbp_get_user_reply_count( $deleted_user_id, true ) ); 557 $this->assertSame( 1, bbp_get_user_topic_count( $reassign_user_id, true ) ); 558 $this->assertSame( 1, bbp_get_user_reply_count( $reassign_user_id, true ) ); 559 $this->assertSame( 2, bbp_get_topic_voice_count( $topic_id, true ) ); 560 $this->assertSame( 2, bbp_get_topic_voice_count( $reply_only_topic_id, true ) ); 561 562 wp_delete_user( $deleted_user_id, $reassign_user_id ); 563 564 $this->assertSame( $reassign_user_id, (int) get_post_field( 'post_author', $topic_id ) ); 565 $this->assertSame( $reassign_user_id, (int) get_post_field( 'post_author', $deleted_user_reply_id ) ); 566 $this->assertSame( 2, bbp_get_user_topic_count( $reassign_user_id, true ) ); 567 $this->assertSame( 3, bbp_get_user_reply_count( $reassign_user_id, true ) ); 568 $this->assertSame( array( $reassign_user_id ), bbp_get_topic_engagements( $topic_id ) ); 569 $this->assertSame( array( $reassign_user_id ), bbp_get_topic_engagements( $reply_only_topic_id ) ); 570 $this->assertSame( 1, bbp_get_topic_voice_count( $topic_id, true ) ); 571 $this->assertSame( 1, bbp_get_topic_voice_count( $reply_only_topic_id, true ) ); 572 } 573 574 /** 575 * @covers ::bbp_update_counts_on_user_reassignment 576 * @ticket BBP3678 577 */ 578 public function test_deleting_user_with_reassignment_updates_current_multisite_counts() { 579 if ( ! is_multisite() ) { 580 $this->markTestSkipped( 'Requires multisite.' ); 581 } 582 583 $deleted_user_id = $this->factory->user->create(); 584 $reassign_user_id = $this->factory->user->create(); 585 $site_id = $this->factory->blog->create(); 586 587 add_user_to_blog( $site_id, $deleted_user_id, 'subscriber' ); 588 add_user_to_blog( $site_id, $reassign_user_id, 'subscriber' ); 589 switch_to_blog( $site_id ); 590 591 try { 592 $forum_id = $this->factory->forum->create(); 593 $this->factory->topic->create( array( 594 'post_author' => $deleted_user_id, 595 'post_parent' => $forum_id, 596 'topic_meta' => array( 'forum_id' => $forum_id ), 597 ) ); 598 599 wp_delete_user( $deleted_user_id, $reassign_user_id ); 600 601 $meta_key = bbp_db()->get_blog_prefix() . '_bbp_topic_count'; 602 $this->assertSame( 1, bbp_get_user_topic_count( $reassign_user_id, true ) ); 603 $this->assertSame( 1, (int) get_user_meta( $reassign_user_id, $meta_key, true ) ); 604 } finally { 605 restore_current_blog(); 606 } 607 } 608 609 /** 610 * @covers ::bbp_make_spam_user 611 * @covers ::bbp_make_ham_user 612 * @covers ::bbp_update_counts_on_transition_post_status 613 * @ticket BBP3678 614 */ 615 public function test_bbp_make_spam_and_ham_user_updates_counts() { 616 $user_id = $this->factory->user->create(); 617 $forum_id = $this->factory->forum->create(); 618 $topic_id = $this->factory->topic->create( array( 619 'post_author' => $user_id, 620 'post_parent' => $forum_id, 621 'topic_meta' => array( 'forum_id' => $forum_id ), 622 ) ); 623 $this->factory->reply->create( array( 624 'post_author' => $user_id, 625 'post_parent' => $topic_id, 626 'reply_meta' => array( 627 'forum_id' => $forum_id, 628 'topic_id' => $topic_id, 629 ), 630 ) ); 631 632 $this->assertTrue( bbp_make_spam_user( $user_id ) ); 633 $this->assertSame( 0, bbp_get_user_topic_count( $user_id, true ) ); 634 $this->assertSame( 0, bbp_get_user_reply_count( $user_id, true ) ); 635 $this->assertSame( 0, bbp_get_forum_topic_count( $forum_id, false, true ) ); 636 $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, false, true ) ); 637 $this->assertSame( 1, bbp_get_forum_topic_count_hidden( $forum_id, false, true ) ); 638 $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) ); 639 640 $this->assertTrue( bbp_make_ham_user( $user_id ) ); 641 $this->assertSame( 1, bbp_get_user_topic_count( $user_id, true ) ); 642 $this->assertSame( 1, bbp_get_user_reply_count( $user_id, true ) ); 643 $this->assertSame( 1, bbp_get_forum_topic_count( $forum_id, false, true ) ); 644 $this->assertSame( 1, bbp_get_forum_reply_count( $forum_id, false, true ) ); 645 $this->assertSame( 0, bbp_get_forum_topic_count_hidden( $forum_id, false, true ) ); 646 $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) ); 647 } 648 649 /** 650 * @covers ::bbp_update_counts_on_post_author_change 651 */ 652 public function test_changing_post_authors_updates_user_counts() { 653 $old_user_id = $this->factory->user->create(); 654 $new_user_id = $this->factory->user->create(); 655 $forum_id = $this->factory->forum->create(); 656 $topic_id = $this->factory->topic->create( array( 657 'post_author' => $old_user_id, 658 'post_parent' => $forum_id, 659 'topic_meta' => array( 'forum_id' => $forum_id ), 660 ) ); 661 $reply_id = $this->factory->reply->create( array( 662 'post_author' => $old_user_id, 663 'post_parent' => $topic_id, 664 'reply_meta' => array( 665 'forum_id' => $forum_id, 666 'topic_id' => $topic_id, 667 ), 668 ) ); 669 670 wp_update_post( array( 'ID' => $topic_id, 'post_author' => $new_user_id ) ); 671 $this->assertSame( 2, bbp_get_topic_voice_count( $topic_id, true ) ); 672 673 wp_update_post( array( 'ID' => $reply_id, 'post_author' => $new_user_id ) ); 674 $this->assertSame( 1, bbp_get_topic_voice_count( $topic_id, true ) ); 675 676 $this->assertSame( 0, bbp_get_user_topic_count( $old_user_id, true ) ); 677 $this->assertSame( 0, bbp_get_user_reply_count( $old_user_id, true ) ); 678 $this->assertSame( 1, bbp_get_user_topic_count( $new_user_id, true ) ); 679 $this->assertSame( 1, bbp_get_user_reply_count( $new_user_id, true ) ); 680 } 681 682 /** 683 * @covers ::bbp_update_counts_on_post_author_change 684 */ 685 public function test_changing_post_author_and_status_rebuilds_user_counts() { 686 $old_user_id = $this->factory->user->create(); 687 $new_user_id = $this->factory->user->create(); 688 $forum_id = $this->factory->forum->create(); 689 $topic_id = $this->factory->topic->create( array( 690 'post_author' => $old_user_id, 691 'post_parent' => $forum_id, 692 'topic_meta' => array( 'forum_id' => $forum_id ), 693 ) ); 694 695 wp_update_post( array( 696 'ID' => $topic_id, 697 'post_author' => $new_user_id, 698 'post_status' => bbp_get_pending_status_id(), 699 ) ); 700 701 $this->assertSame( 0, bbp_get_user_topic_count( $old_user_id, true ) ); 702 $this->assertSame( 0, bbp_get_user_topic_count( $new_user_id, true ) ); 703 } 704 705 /** 403 706 * @covers ::bbp_increase_user_topic_count 404 707 */
Note:
See TracChangeset
for help on using the changeset viewer.