Ticket #1799: 1799.8.patch
File 1799.8.patch, 35.5 KB (added by , 9 years ago) |
---|
-
src/includes/core/actions.php
diff --git src/includes/core/actions.php src/includes/core/actions.php index 18fa0bb..263e9f7 100644
add_action( 'bbp_unspammed_reply', 'bbp_update_reply_walker' ); 255 255 add_action( 'bbp_approved_reply', 'bbp_update_reply_walker' ); 256 256 add_action( 'bbp_unapproved_reply', 'bbp_update_reply_walker' ); 257 257 258 // Update forum topic/reply counts 259 add_action( 'bbp_new_reply', 'bbp_increase_forum_reply_count' ); 260 add_action( 'bbp_new_topic', 'bbp_increase_forum_topic_count' ); 261 add_action( 'bbp_trashed_reply', 'bbp_decrease_forum_reply_count' ); 262 add_action( 'bbp_trashed_topic', 'bbp_decrease_forum_topic_count' ); 263 add_action( 'bbp_trashed_topic', 'bbp_increase_forum_topic_count_hidden' ); 264 add_action( 'bbp_untrashed_reply', 'bbp_increase_forum_reply_count' ); 265 add_action( 'bbp_untrashed_topic', 'bbp_increase_forum_topic_count' ); 266 add_action( 'bbp_untrashed_topic', 'bbp_decrease_forum_topic_count_hidden' ); 267 add_action( 'bbp_spammed_reply', 'bbp_decrease_forum_reply_count' ); 268 add_action( 'bbp_spammed_topic', 'bbp_decrease_forum_topic_count' ); 269 add_action( 'bbp_spammed_topic', 'bbp_increase_forum_topic_count_hidden' ); 270 add_action( 'bbp_unspammed_reply', 'bbp_increase_forum_reply_count' ); 271 add_action( 'bbp_unspammed_topic', 'bbp_increase_forum_topic_count' ); 272 add_action( 'bbp_unspammed_topic', 'bbp_decrease_forum_topic_count_hidden' ); 273 add_action( 'bbp_approved_reply', 'bbp_increase_forum_reply_count' ); 274 add_action( 'bbp_approved_topic', 'bbp_increase_forum_topic_count' ); 275 add_action( 'bbp_approved_topic', 'bbp_decrease_forum_topic_count_hidden' ); 276 add_action( 'bbp_unapproved_reply', 'bbp_decrease_forum_reply_count' ); 277 add_action( 'bbp_unapproved_topic', 'bbp_decrease_forum_topic_count' ); 278 add_action( 'bbp_unapproved_topic', 'bbp_increase_forum_topic_count_hidden' ); 279 280 // Update forum reply counts for approved/unapproved topics. 281 add_action( 'bbp_approved_topic', 'bbp_approved_unapproved_topic_update_forum_reply_count' ); 282 add_action( 'bbp_unapproved_topic', 'bbp_approved_unapproved_topic_update_forum_reply_count' ); 283 284 // Update topic reply counts 285 add_action( 'bbp_new_reply', 'bbp_increase_topic_reply_count' ); 286 add_action( 'bbp_trashed_reply', 'bbp_decrease_topic_reply_count' ); 287 add_action( 'bbp_trashed_reply', 'bbp_increase_topic_reply_count_hidden' ); 288 add_action( 'bbp_untrashed_reply', 'bbp_increase_topic_reply_count' ); 289 add_action( 'bbp_untrashed_reply', 'bbp_decrease_topic_reply_count_hidden' ); 290 add_action( 'bbp_spammed_reply', 'bbp_decrease_topic_reply_count' ); 291 add_action( 'bbp_spammed_reply', 'bbp_increase_topic_reply_count_hidden' ); 292 add_action( 'bbp_unspammed_reply', 'bbp_increase_topic_reply_count' ); 293 add_action( 'bbp_unspammed_reply', 'bbp_decrease_topic_reply_count_hidden' ); 294 add_action( 'bbp_approved_reply', 'bbp_increase_topic_reply_count' ); 295 add_action( 'bbp_approved_reply', 'bbp_decrease_topic_reply_count_hidden' ); 296 add_action( 'bbp_unapproved_reply', 'bbp_decrease_topic_reply_count' ); 297 add_action( 'bbp_unapproved_reply', 'bbp_increase_topic_reply_count_hidden' ); 298 258 299 // Users topic & reply counts 259 300 add_action( 'bbp_new_topic', 'bbp_increase_user_topic_count' ); 260 301 add_action( 'bbp_new_reply', 'bbp_increase_user_reply_count' ); … … add_action( 'bbp_trash_reply', 'bbp_decrease_user_reply_count' ); 267 308 add_action( 'bbp_spam_topic', 'bbp_decrease_user_topic_count' ); 268 309 add_action( 'bbp_spam_reply', 'bbp_decrease_user_reply_count' ); 269 310 311 // Insert forum/topic/reply counts 312 add_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10, 2 ); 313 add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 3 ); 314 270 315 // Topic status transition helpers for replies 271 316 add_action( 'bbp_trash_topic', 'bbp_trash_topic_replies' ); 272 317 add_action( 'bbp_untrash_topic', 'bbp_untrash_topic_replies' ); -
src/includes/forums/functions.php
diff --git src/includes/forums/functions.php src/includes/forums/functions.php index a821d79..615c143 100644
function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) { 73 73 'forum_id' => $forum_id, 74 74 ) ); 75 75 76 /** 77 * Fires after forum has been inserted via `bbp_insert_forum`. 78 * 79 * @since 2.6.0 bbPress (rXXXX) 80 * 81 * @param int $forum_id The forum id. 82 */ 83 do_action( 'bbp_insert_forum', (int) $forum_id ); 84 76 85 // Return new forum ID 77 86 return $forum_id; 78 87 } … … function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_anc 1141 1150 if ( ! empty( $ancestors ) ) { 1142 1151 foreach ( (array) $ancestors as $parent_forum_id ) { 1143 1152 1144 // Get forum counts 1145 $parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false, true ); 1146 $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true, true ); 1153 // Only update topic count when an ancestor is not a category. 1154 if ( ! bbp_is_forum_category( $parent_forum_id ) ) { 1155 1156 $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 } 1147 1159 1148 // Update counts1149 update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) ( $parent_topic_count + $difference ));1160 // Update the total topic count. 1161 $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true, true ); 1150 1162 update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) ( $parent_total_topic_count + $difference ) ); 1151 1163 } 1152 1164 } … … function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_anc 1158 1170 } 1159 1171 1160 1172 /** 1173 * Increase the total topic count of a forum by one. 1174 * 1175 * @since 2.6.0 bbPress (rXXXX) 1176 * 1177 * @param int $forum_id Optional. Forum id. 1178 * 1179 * @uses bbp_get_forum_id() To get the forum id. 1180 * @uses bbp_bump_forum_topic_count() To bump forum topic count. 1181 * 1182 * @return void 1183 */ 1184 function bbp_increase_forum_topic_count( $forum_id = 0 ) { 1185 1186 // Bail early if no id is passed. 1187 if ( empty( $forum_id ) ) { 1188 return; 1189 } 1190 1191 // If it's a topic, get the forum id. 1192 if ( bbp_is_topic( $forum_id ) ) { 1193 $topic_id = $forum_id; 1194 $forum_id = bbp_get_topic_forum_id( $topic_id ); 1195 1196 // If this is a new, unpublished, topic, increase hidden count and bail. 1197 if ( 'bbp_new_topic' === current_filter() && ( ! bbp_is_topic_published( $topic_id ) && ! bbp_is_topic_closed( $topic_id ) ) ) { 1198 bbp_increase_forum_topic_count_hidden( $forum_id ); 1199 return; 1200 } 1201 } 1202 1203 bbp_bump_forum_topic_count( $forum_id ); 1204 } 1205 1206 /** 1207 * Decrease the total topic count of a forum by one. 1208 * 1209 * @since 2.6.0 bbPress (rXXXX) 1210 * 1211 * @param int $forum_id Optional. Forum id. 1212 * @uses bbp_get_forum_id() To get the forum id. 1213 * @uses bbp_bump_forum_topic_count() To bump forum topic count. 1214 * 1215 * @return void 1216 */ 1217 function bbp_decrease_forum_topic_count( $forum_id = 0 ) { 1218 1219 // Bail early if no id is passed. 1220 if ( empty( $forum_id ) ) { 1221 return; 1222 } 1223 1224 // If it's a topic, get the forum id. 1225 if ( bbp_is_topic( $forum_id ) ) { 1226 $forum_id = bbp_get_topic_forum_id( $forum_id ); 1227 } 1228 1229 bbp_bump_forum_topic_count( $forum_id, -1 ); 1230 } 1231 1232 /** 1161 1233 * Bump the total hidden topic count of a forum 1162 1234 * 1163 1235 * @since 2.1.0 bbPress (r3825) … … function bbp_bump_forum_topic_count_hidden( $forum_id = 0, $difference = 1 ) { 1191 1263 } 1192 1264 1193 1265 /** 1266 * Increase the total hidden topic count of a forum by one. 1267 * 1268 * @since 2.6.0 bbPress (rXXXX) 1269 * 1270 * @param int $forum_id Optional. Forum id. 1271 * 1272 * @uses bbp_get_forum_id() To get the forum id. 1273 * @uses bbp_bump_forum_topic_count_hidden() To bump forum hidden topic count. 1274 * 1275 * @return void 1276 */ 1277 function bbp_increase_forum_topic_count_hidden( $forum_id = 0 ) { 1278 1279 // Bail early if no id is passed. 1280 if ( empty( $forum_id ) ) { 1281 return; 1282 } 1283 1284 // If it's a topic, get the forum id. 1285 if ( bbp_is_topic( $forum_id ) ) { 1286 $forum_id = bbp_get_topic_forum_id( $forum_id ); 1287 } 1288 1289 bbp_bump_forum_topic_count_hidden( $forum_id ); 1290 } 1291 1292 /** 1293 * Decrease the total hidden topic count of a forum by one. 1294 * 1295 * @since 2.6.0 bbPress (rXXXX) 1296 * 1297 * @param int $forum_id Optional. Forum id. 1298 * 1299 * @uses bbp_get_forum_id() To get the forum id. 1300 * @uses bbp_bump_forum_topic_count_hidden() To bump forum hidden topic count. 1301 * 1302 * @return void 1303 */ 1304 function bbp_decrease_forum_topic_count_hidden( $forum_id = 0 ) { 1305 1306 // Bail early if no id is passed. 1307 if ( empty( $forum_id ) ) { 1308 return; 1309 } 1310 1311 // If it's a topic, get the forum id. 1312 if ( bbp_is_topic( $forum_id ) ) { 1313 $forum_id = bbp_get_topic_forum_id( $forum_id ); 1314 } 1315 1316 bbp_bump_forum_topic_count_hidden( $forum_id, -1 ); 1317 } 1318 1319 /** 1194 1320 * Bump the total topic count of a forum 1195 1321 * 1196 1322 * @since 2.1.0 bbPress (r3825) … … function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_anc 1232 1358 if ( ! empty( $ancestors ) ) { 1233 1359 foreach ( (array) $ancestors as $parent_forum_id ) { 1234 1360 1235 // Get forum counts 1236 $parent_topic_count = bbp_get_forum_reply_count( $parent_forum_id, false, true ); 1237 $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true, true ); 1361 // Only update reply count when an ancestor is not a category. 1362 if ( ! bbp_is_forum_category( $parent_forum_id ) ) { 1363 1364 $parent_reply_count = bbp_get_forum_reply_count( $parent_forum_id, false, true ); 1365 update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) ( $parent_reply_count + $difference ) ); 1366 } 1238 1367 1239 // Update counts1240 update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) ( $parent_topic_count + $difference ));1368 // Update the total reply count. 1369 $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true, true ); 1241 1370 update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) ( $parent_total_reply_count + $difference ) ); 1242 1371 } 1243 1372 } … … function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_anc 1248 1377 return (int) apply_filters( 'bbp_bump_forum_reply_count', $forum_reply_count, $forum_id, $difference, $update_ancestors ); 1249 1378 } 1250 1379 1380 /** 1381 * Increase the total reply count of a forum by one. 1382 * 1383 * @since 2.6.0 bbPress (rXXXX) 1384 * 1385 * @param int $forum_id Optional. Forum id. 1386 * 1387 * @uses bbp_get_forum_id() To get the forum id. 1388 * @uses bbp_bump_forum_reply_count() To bump forum topic count. 1389 * 1390 * @return void 1391 */ 1392 function bbp_increase_forum_reply_count( $forum_id = 0 ) { 1393 1394 // Bail early if no id is passed. 1395 if ( empty( $forum_id ) ) { 1396 return; 1397 } 1398 1399 // If it's a reply, get the forum id. 1400 if ( bbp_is_reply( $forum_id ) ) { 1401 $reply_id = $forum_id; 1402 $forum_id = bbp_get_reply_forum_id( $reply_id ); 1403 1404 // Don't update if this is a new, unpublished, reply. 1405 if ( 'bbp_new_reply' === current_filter() && ! bbp_is_reply_published( $reply_id ) ) { 1406 return; 1407 } 1408 } 1409 1410 bbp_bump_forum_reply_count( $forum_id ); 1411 } 1412 1413 /** 1414 * Decrease the total reply count of a forum by one. 1415 * 1416 * @since 2.6.0 bbPress (rXXXX) 1417 * 1418 * @param int $forum_id Optional. Forum id. 1419 * 1420 * @uses bbp_get_forum_id() To get the forum id. 1421 * @uses bbp_bump_forum_reply_count() To bump forum topic count. 1422 * 1423 * @return void 1424 */ 1425 function bbp_decrease_forum_reply_count( $forum_id = 0 ) { 1426 1427 // Bail early if no id is passed. 1428 if ( empty( $forum_id ) ) { 1429 return; 1430 } 1431 1432 // If it's a reply, get the forum id. 1433 if ( bbp_is_reply( $forum_id ) ) { 1434 $forum_id = bbp_get_reply_forum_id( $forum_id ); 1435 } 1436 1437 bbp_bump_forum_reply_count( $forum_id, -1 ); 1438 } 1439 1440 /** 1441 * Update forum reply counts when a topic is approved or unapproved. 1442 * 1443 * @since 2.6.0 bbPress (rXXXX) 1444 * 1445 * @param int $topic_id 1446 * 1447 * @uses bbp_get_reply_post_type() To get the reply post type. 1448 * @uses bbp_get_public_child_ids() To get the topic's public child ids. 1449 * @uses bbp_get_topic_forum_id() To get the topic's forum id. 1450 * @uses bbp_bump_forum_reply_count() To bump the forum reply count. 1451 * 1452 * @return void 1453 */ 1454 function bbp_approved_unapproved_topic_update_forum_reply_count( $topic_id = 0 ) { 1455 1456 // Bail early if we don't have a topic id. 1457 if ( empty( $topic_id ) ) { 1458 return; 1459 } 1460 1461 // Get the topic's replies. 1462 $replies = bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() ); 1463 $count = count( $replies ); 1464 1465 // If we're unapproving, set count to negative. 1466 if ( 'bbp_unapproved_topic' === current_filter() ) { 1467 $count = -$count; 1468 } 1469 1470 // Update counts. 1471 bbp_bump_forum_reply_count( bbp_get_topic_forum_id( $topic_id ), $count ); 1472 } 1473 1251 1474 /** Forum Updaters ************************************************************/ 1252 1475 1253 1476 /** … … function bbp_update_forum( $args = array() ) { 1732 1955 } 1733 1956 1734 1957 // Counts 1735 bbp_update_forum_subforum_count ( $r['forum_id'] ); 1736 bbp_update_forum_reply_count ( $r['forum_id'] ); 1737 bbp_update_forum_topic_count ( $r['forum_id'] ); 1738 bbp_update_forum_topic_count_hidden( $r['forum_id'] ); 1958 bbp_update_forum_subforum_count( $r['forum_id'] ); 1959 1960 // Only update topic count if we're deleting a topic, or in the dashboard. 1961 if ( in_array( current_filter(), array( 'bbp_deleted_topic', 'save_post' ) ) ) { 1962 bbp_update_forum_reply_count( $r['forum_id'] ); 1963 bbp_update_forum_topic_count( $r['forum_id'] ); 1964 bbp_update_forum_topic_count_hidden( $r['forum_id'] ); 1965 } 1739 1966 1740 1967 // Update the parent forum if one was passed 1741 1968 if ( ! empty( $r['post_parent'] ) && is_numeric( $r['post_parent'] ) ) { -
src/includes/replies/functions.php
diff --git src/includes/replies/functions.php src/includes/replies/functions.php index caabba0..6b1c7b1 100644
function bbp_insert_reply( $reply_data = array(), $reply_meta = array() ) { 65 65 // Update the reply and hierarchy 66 66 bbp_update_reply( $reply_id, $reply_meta['topic_id'], $reply_meta['forum_id'], array(), $reply_data['post_author'], false, $reply_meta['reply_to'] ); 67 67 68 /** 69 * Fires after reply has been inserted via `bbp_insert_reply`. 70 * 71 * @since 2.6.0 bbPress (rXXXX) 72 * 73 * @param int $reply_id The reply id. 74 * @param int $topic_id The topic id. 75 * @param int $forum_id The forum id. 76 */ 77 do_action( 'bbp_insert_reply', (int) $reply_id, (int) $reply_meta['topic_id'], (int) $reply_meta['forum_id'] ); 78 68 79 // Return new reply ID 69 80 return $reply_id; 70 81 } 71 82 83 /** 84 * Update counts after a reply is inserted via `bbp_insert_reply`. 85 * 86 * @since 2.6.0 bbPress (rXXXX) 87 * 88 * @param int $reply_id 89 * @param int $topic_id 90 * @param int $forum_id 91 * 92 * @uses get_post_field() To get the post status. 93 * @uses bbp_get_public_status_id() To get the public status id. 94 * @uses bbp_increase_topic_reply_count() To bump the reply's topic reply count by 1. 95 * @uses bbp_increase_forum_reply_count() To bump the reply's forum reply count by 1. 96 * @uses bbp_increase_topic_reply_count_hidden() To bump the reply's topic reply 97 * hidden count by 1. 98 * 99 * @return void 100 */ 101 function bbp_insert_reply_update_counts( $reply_id = 0, $topic_id = 0, $forum_id = 0 ) { 102 103 // Get the reply status. 104 $status = get_post_field( 'post_status', $reply_id ); 105 106 // If the reply is public, update the forum/topic reply counts. 107 if ( $status === bbp_get_public_status_id() ) { 108 bbp_increase_topic_reply_count( $topic_id ); 109 bbp_increase_forum_reply_count( $forum_id ); 110 111 // If the reply isn't public only update the topic reply hidden count. 112 } else { 113 bbp_increase_topic_reply_count_hidden( $topic_id ); 114 } 115 } 116 72 117 /** Post Form Handlers ********************************************************/ 73 118 74 119 /** … … function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 977 1022 bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time ); 978 1023 979 1024 // Counts 980 bbp_update_topic_voice_count ( $ancestor ); 981 bbp_update_topic_reply_count ( $ancestor ); 982 bbp_update_topic_reply_count_hidden( $ancestor ); 1025 bbp_update_topic_voice_count( $ancestor ); 1026 1027 // Only update reply count if we're deleting a reply, or in the dashboard. 1028 if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ) ) ) { 1029 bbp_update_topic_reply_count( $ancestor ); 1030 bbp_update_topic_reply_count_hidden( $ancestor ); 1031 } 983 1032 984 1033 // Forum meta relating to most recent topic 985 1034 } elseif ( bbp_is_forum( $ancestor ) ) { … … function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 1003 1052 } 1004 1053 1005 1054 // Counts 1006 bbp_update_forum_reply_count( $ancestor ); 1055 // Only update reply count if we're deleting a reply, or in the dashboard. 1056 if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ) ) ) { 1057 bbp_update_forum_reply_count( $ancestor ); 1058 } 1007 1059 } 1008 1060 } 1009 1061 } -
src/includes/topics/functions.php
diff --git src/includes/topics/functions.php src/includes/topics/functions.php index 5a762e5..5e75cf4 100644
function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) { 70 70 // Update the topic and hierarchy 71 71 bbp_update_topic( $topic_id, $topic_meta['forum_id'], array(), $topic_data['post_author'], false ); 72 72 73 /** 74 * Fires after topic has been inserted via `bbp_insert_topic`. 75 * 76 * @since 2.6.0 bbPress (rXXXX) 77 * 78 * @param int $topic_id The topic id. 79 * @param int $forum_id The forum id. 80 */ 81 do_action( 'bbp_insert_topic', (int) $topic_id, (int) $topic_meta['forum_id'] ); 82 73 83 // Return new topic ID 74 84 return $topic_id; 75 85 } … … function bbp_update_topic_walker( $topic_id, $last_active_time = '', $forum_id = 1022 1032 * @uses update_post_meta() To update the old forum sticky meta 1023 1033 * @uses bbp_stick_topic() To stick the topic in the new forum 1024 1034 * @uses bbp_get_reply_post_type() To get the reply post type 1025 * @uses bbp_get_all_child_ids() To get the public child ids 1035 * @uses bbp_get_all_child_ids() To get the all child ids. 1036 * @uses bbp_get_public_child_ids() To get the all child ids. 1037 * @uses get_post_field() To get the topic's post status. 1038 * @uses bbp_get_public_status_id() To get the public status id. 1039 * @uses bbp_decrease_forum_topic_count() To bump the forum topic count by -1. 1040 * @uses bbp_bump_forum_reply_count() To bump the forum reply count. 1041 * @uses bbp_increase_forum_topic_count() To bump the forum topic count by 1. 1042 * @uses bbp_decrease_forum_topic_count_hidden() To bump the forum topic hidden count by -1. 1043 * @uses bbp_increase_forum_topic_count_hidden() To bump the forum topic hidden count by 1. 1026 1044 * @uses bbp_update_reply_forum_id() To update the reply forum id 1027 1045 * @uses bbp_update_topic_forum_id() To update the topic forum id 1028 1046 * @uses get_post_ancestors() To get the topic's ancestors … … function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) { 1100 1118 // Get topic ancestors 1101 1119 $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) ); 1102 1120 1121 // Get reply count. 1122 $public_reply_count = count( bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() ) ); 1123 1124 // Topic status. 1125 $topic_status = get_post_field( 'post_status', $topic_id ); 1126 1127 // Update old/new forum counts. 1128 if ( $topic_status === bbp_get_public_status_id() ) { 1129 1130 // Update old forum counts. 1131 bbp_decrease_forum_topic_count( $old_forum_id ); 1132 bbp_bump_forum_reply_count( $old_forum_id, -$public_reply_count ); 1133 1134 // Update new forum counts. 1135 bbp_increase_forum_topic_count( $new_forum_id ); 1136 bbp_bump_forum_reply_count( $new_forum_id, $public_reply_count ); 1137 } else { 1138 1139 // Update old forum counts. 1140 bbp_decrease_forum_topic_count_hidden( $old_forum_id ); 1141 1142 // Update new forum counts. 1143 bbp_increase_forum_topic_count_hidden( $new_forum_id ); 1144 } 1145 1103 1146 // Loop through ancestors and update them 1104 1147 if ( ! empty( $old_forum_ancestors ) ) { 1105 1148 foreach ( $old_forum_ancestors as $ancestor ) { … … function bbp_bump_topic_reply_count( $topic_id = 0, $difference = 1 ) { 2364 2407 } 2365 2408 2366 2409 /** 2410 * Increase the total reply count of a topic by one. 2411 * 2412 * @since 2.6.0 bbPress (rXXXX) 2413 * 2414 * @param int $topic_id Optional. Forum id. 2415 * 2416 * @uses bbp_is_reply() To check if the passed topic id is a reply. 2417 * @uses bbp_get_reply_topic_id() To get the topic id. 2418 * @uses bbp_bump_topic_reply_count() To bump topic reply count. 2419 * 2420 * @return void 2421 */ 2422 function bbp_increase_topic_reply_count( $topic_id = 0 ) { 2423 2424 // Bail early if no id is passed. 2425 if ( empty( $topic_id ) ) { 2426 return; 2427 } 2428 2429 // If it's a reply, get the topic id. 2430 if ( bbp_is_reply( $topic_id ) ) { 2431 $reply_id = $topic_id; 2432 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2433 2434 // If this is a new, unpublished, reply, update hidden count and bail. 2435 if ( 'bbp_new_reply' === current_filter() && ! bbp_is_reply_published( $reply_id ) ) { 2436 bbp_increase_topic_reply_count_hidden( $topic_id ); 2437 return; 2438 } 2439 } 2440 2441 bbp_bump_topic_reply_count( $topic_id ); 2442 } 2443 2444 /** 2445 * Decrease the total reply count of a topic by one. 2446 * 2447 * @since 2.6.0 bbPress (rXXXX) 2448 * 2449 * @param int $topic_id Optional. Topic id. 2450 * 2451 * @uses bbp_is_reply() To check if the passed topic id is a reply. 2452 * @uses bbp_get_reply_topic_id() To get the topic id. 2453 * @uses bbp_bump_topic_reply_count() To bump topic reply count. 2454 * 2455 * @return void 2456 */ 2457 function bbp_decrease_topic_reply_count( $topic_id = 0 ) { 2458 2459 // Bail early if no id is passed. 2460 if ( empty( $topic_id ) ) { 2461 return; 2462 } 2463 2464 // If it's a reply, get the topic id. 2465 if ( bbp_is_reply( $topic_id ) ) { 2466 $topic_id = bbp_get_reply_topic_id( $topic_id ); 2467 } 2468 2469 bbp_bump_topic_reply_count( $topic_id, -1 ); 2470 } 2471 2472 /** 2367 2473 * Bump the total hidden reply count of a topic 2368 2474 * 2369 2475 * @since 2.1.0 bbPress (r3825) … … function bbp_bump_topic_reply_count_hidden( $topic_id = 0, $difference = 1 ) { 2396 2502 return (int) apply_filters( 'bbp_bump_topic_reply_count_hidden', $new_count, $topic_id, $difference ); 2397 2503 } 2398 2504 2505 /** 2506 * Increase the total hidden reply count of a topic by one. 2507 * 2508 * @since 2.6.0 bbPress (rXXXX) 2509 * 2510 * @param int $topic_id Optional. Topic id. 2511 * 2512 * @uses bbp_is_reply() To check if the passed topic id is a reply. 2513 * @uses bbp_get_reply_topic_id() To get the topic id. 2514 * @uses bbp_bump_topic_reply_count_hidden() To bump topic hidden reply count. 2515 * 2516 * @return void 2517 */ 2518 function bbp_increase_topic_reply_count_hidden( $topic_id = 0 ) { 2519 2520 // Bail early if no id is passed. 2521 if ( empty( $topic_id ) ) { 2522 return; 2523 } 2524 2525 // If it's a reply, get the topic id. 2526 if ( bbp_is_reply( $topic_id ) ) { 2527 $topic_id = bbp_get_reply_topic_id( $topic_id ); 2528 } 2529 2530 bbp_bump_topic_reply_count_hidden( $topic_id ); 2531 } 2532 2533 /** 2534 * Decrease the total hidden reply count of a topic by one. 2535 * 2536 * @since 2.6.0 bbPress (rXXXX) 2537 * 2538 * @param int $topic_id Optional. Topic id. 2539 * 2540 * @uses bbp_is_reply() To check if the passed topic id is a reply. 2541 * @uses bbp_get_reply_topic_id() To get the topic id. 2542 * @uses bbp_bump_topic_reply_count_hidden() To bump topic hidden reply count. 2543 * 2544 * @return void 2545 */ 2546 function bbp_decrease_topic_reply_count_hidden( $topic_id = 0 ) { 2547 2548 // Bail early if no id is passed. 2549 if ( empty( $topic_id ) ) { 2550 return; 2551 } 2552 2553 // If it's a reply, get the topic id. 2554 if ( bbp_is_reply( $topic_id ) ) { 2555 $topic_id = bbp_get_reply_topic_id( $topic_id ); 2556 } 2557 2558 bbp_bump_topic_reply_count_hidden( $topic_id, -1 ); 2559 } 2560 2561 /** 2562 * Update counts after a topic is inserted via `bbp_insert_topic`. 2563 * 2564 * @since 2.6.0 bbPress (rXXXX) 2565 * 2566 * @param int $reply_id 2567 * @param int $topic_id 2568 * 2569 * @uses get_post_field() To get the post status. 2570 * @uses bbp_get_public_status_id() To get the public status id. 2571 * @uses bbp_increase_forum_topic_count() To bump the topic's forum topic count by 1. 2572 * @uses bbp_increase_forum_topic_count_hidden() To bump the topic's forum topic 2573 * hidden count by 1. 2574 * 2575 * @return void 2576 */ 2577 function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) { 2578 2579 // Get the topic status. 2580 $status = get_post_field( 'post_status', $topic_id ); 2581 2582 // If the topic is public, update the forum topic counts. 2583 if ( $status === bbp_get_public_status_id() ) { 2584 bbp_increase_forum_topic_count( $forum_id ); 2585 2586 // If the topic isn't public only update the forum topic hidden count. 2587 } else { 2588 bbp_increase_forum_topic_count_hidden( $forum_id ); 2589 } 2590 } 2591 2399 2592 /** Topic Updaters ************************************************************/ 2400 2593 2401 2594 /** -
tests/phpunit/includes/testcase.php
diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php index 2cfe03a..9ac767a 100644
3 3 class BBP_UnitTestCase extends WP_UnitTestCase { 4 4 5 5 protected static $cached_SERVER_NAME = null; 6 protected static $bbp_hooks_saved = array(); 6 7 7 8 /** 8 9 * Fake WP mail globals, to avoid errors … … class BBP_UnitTestCase extends WP_UnitTestCase { 28 29 } 29 30 } 30 31 32 if ( ! self::$bbp_hooks_saved ) { 33 $this->_bbp_maybe_backup_hooks(); 34 } 35 31 36 $this->factory = new BBP_UnitTest_Factory; 32 37 33 38 if ( class_exists( 'BP_UnitTest_Factory' ) ) { … … class BBP_UnitTestCase extends WP_UnitTestCase { 56 61 } 57 62 } 58 63 } 64 65 $this->_bbp_maybe_restore_hooks(); 66 } 67 68 /** 69 * Saves the action and filter-related globals so they can be restored later 70 * 71 * Stores $merged_filters, $wp_actions, $wp_current_filter, and $wp_filter 72 * on a class variable so they can be restored on tearDown() using _restore_hooks() 73 * 74 * This is a backport from the WP_UnitTestCase in WP 4.0+, so that we can 75 * get tests to pass in 3.9 and below. Some tests were failing for no other 76 * reason than the below globals were a mess. 77 * 78 * @global array $merged_filters 79 * @global array $wp_actions 80 * @global array $wp_current_filter 81 * @global array $wp_filter 82 * @return void 83 */ 84 protected function _bbp_maybe_backup_hooks() { 85 if ( version_compare( $GLOBALS['wp_version'], '4.0', '>=' ) ) { 86 return; 87 } 88 89 $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' ); 90 foreach ( $globals as $key ) { 91 self::$bbp_hooks_saved[ $key ] = $GLOBALS[ $key ]; 92 } 93 } 94 95 /** 96 * Restores the hook-related globals to their state at setUp() 97 * 98 * so that future tests aren't affected by hooks set during this last test 99 * 100 * This is a backport from the WP_UnitTestCase in WP 4.0+, so that we can 101 * get tests to pass in 3.9 and below. Some tests were failing for no other 102 * reason than the below globals were a mess. 103 * 104 * @global array $merged_filters 105 * @global array $wp_actions 106 * @global array $wp_current_filter 107 * @global array $wp_filter 108 * @return void 109 */ 110 protected function _bbp_maybe_restore_hooks(){ 111 if ( version_compare( $GLOBALS['wp_version'], '4.0', '>=' ) ) { 112 return; 113 } 114 115 $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' ); 116 foreach ( $globals as $key ) { 117 $GLOBALS[ $key ] = self::$bbp_hooks_saved[ $key ]; 118 } 59 119 } 60 120 61 121 function assertPreConditions() { -
tests/phpunit/testcases/forums/functions/counts.php
diff --git tests/phpunit/testcases/forums/functions/counts.php tests/phpunit/testcases/forums/functions/counts.php index dcc8bc3..633b05a 100644
class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 13 13 * Generic function to test the forum counts with a new topic 14 14 */ 15 15 public function test_bbp_forum_new_topic_counts() { 16 remove_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10 ); 17 16 18 $f = $this->factory->forum->create(); 17 19 $t1 = $this->factory->topic->create( array( 18 20 'post_parent' => $f, … … class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 60 62 61 63 $count = bbp_get_forum_topic_count_hidden( $f, true, true ); 62 64 $this->assertSame( 0, $count ); 65 66 remove_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10, 2 ); 63 67 } 64 68 65 69 /** … … class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 253 257 $this->assertSame( '1', $count ); 254 258 } 255 259 260 /** 261 * @covers ::bbp_increase_forum_topic_count 262 */ 263 public function test_bbp_increase_forum_topic_count() { 264 $f = $this->factory->forum->create(); 265 266 $count = bbp_get_forum_topic_count( $f ); 267 $this->assertSame( '0', $count ); 268 269 bbp_increase_forum_topic_count( $f ); 270 271 $count = bbp_get_forum_topic_count( $f ); 272 $this->assertSame( '1', $count ); 273 } 274 275 /** 276 * @covers ::bbp_decrease_forum_topic_count 277 */ 278 public function test_bbp_decrease_forum_topic_count() { 279 $f = $this->factory->forum->create(); 280 281 $count = bbp_get_forum_topic_count( $f ); 282 $this->assertSame( '0', $count ); 283 284 $t = $this->factory->topic->create_many( 9, array( 285 'post_parent' => $f, 286 ) ); 287 288 bbp_update_forum_topic_count( $f ); 289 290 $count = bbp_get_forum_topic_count( $f ); 291 $this->assertSame( '9', $count ); 292 293 bbp_update_forum_topic_count( $f ); 294 295 bbp_decrease_forum_topic_count( $f ); 296 297 $count = bbp_get_forum_topic_count( $f ); 298 $this->assertSame( '8', $count ); 299 } 300 256 301 /** 257 302 * @covers ::bbp_bump_forum_topic_count_hidden 258 303 */ … … class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 268 313 $this->assertSame( '1', $count ); 269 314 } 270 315 316 /** 317 * @covers ::bbp_increase_forum_topic_count_hidden 318 */ 319 public function test_bbp_increase_forum_topic_count_hidden() { 320 $f = $this->factory->forum->create(); 321 322 $count = bbp_get_forum_topic_count_hidden( $f ); 323 $this->assertSame( '0', $count ); 324 325 bbp_increase_forum_topic_count_hidden( $f ); 326 327 $count = bbp_get_forum_topic_count_hidden( $f ); 328 $this->assertSame( '1', $count ); 329 } 330 331 /** 332 * @covers ::bbp_decrease_forum_topic_count_hidden 333 */ 334 public function test_bbp_decrease_forum_topic_count_hidden() { 335 $f = $this->factory->forum->create(); 336 337 $count = bbp_get_forum_topic_count_hidden( $f ); 338 $this->assertSame( '0', $count ); 339 340 $t = $this->factory->topic->create_many( 9, array( 341 'post_parent' => $f, 342 'post_status' => bbp_get_spam_status_id(), 343 'topic_meta' => array( 344 'forum_id' => $f, 345 'spam_meta_status' => 'publish', 346 ) 347 ) ); 348 349 bbp_update_forum_topic_count_hidden( $f ); 350 351 $count = bbp_get_forum_topic_count_hidden( $f ); 352 $this->assertSame( '9', $count ); 353 354 bbp_decrease_forum_topic_count_hidden( $f ); 355 356 $count = bbp_get_forum_topic_count_hidden( $f ); 357 $this->assertSame( '8', $count ); 358 } 359 271 360 /** 272 361 * @covers ::bbp_bump_forum_reply_count 273 362 */ … … class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 283 372 $this->assertSame( '1', $count ); 284 373 } 285 374 375 /** 376 * @covers ::bbp_increase_forum_reply_count 377 */ 378 public function test_bbp_increase_forum_reply_count() { 379 $f = $this->factory->forum->create(); 380 381 $count = bbp_get_forum_reply_count( $f ); 382 $this->assertSame( '0', $count ); 383 384 bbp_increase_forum_reply_count( $f ); 385 386 $count = bbp_get_forum_reply_count( $f ); 387 $this->assertSame( '1', $count ); 388 } 389 390 /** 391 * @covers ::bbp_decrease_forum_reply_count 392 */ 393 public function test_bbp_decrease_forum_reply_count() { 394 $f = $this->factory->forum->create(); 395 396 $count = bbp_get_forum_reply_count( $f ); 397 $this->assertSame( '0', $count ); 398 399 $t = $this->factory->topic->create( array( 400 'post_parent' => $f, 401 ) ); 402 403 $r = $this->factory->reply->create_many( 9, array( 404 'post_parent' => $t, 405 ) ); 406 407 bbp_update_forum_reply_count( $f ); 408 409 $count = bbp_get_forum_reply_count( $f ); 410 $this->assertSame( '9', $count ); 411 412 bbp_decrease_forum_reply_count( $f ); 413 414 $count = bbp_get_forum_reply_count( $f ); 415 $this->assertSame( '8', $count ); 416 } 417 286 418 /** 287 419 * @covers ::bbp_update_forum_subforum_count 288 420 */ -
tests/phpunit/testcases/topics/functions/counts.php
diff --git tests/phpunit/testcases/topics/functions/counts.php tests/phpunit/testcases/topics/functions/counts.php index c040874..ffd1bc7 100644
class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 13 13 * Generic function to test the topics counts with a new reply 14 14 */ 15 15 public function test_bbp_topic_new_reply_counts() { 16 remove_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10 ); 17 16 18 $f = $this->factory->forum->create(); 17 19 $t = $this->factory->topic->create( array( 18 20 'post_parent' => $f, … … class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 75 77 76 78 $count = bbp_get_topic_voice_count( $t, true ); 77 79 $this->assertSame( 2, $count ); 80 81 add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 2 ); 78 82 } 79 83 80 84 /** … … class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 340 344 $this->assertSame( '4', $count ); 341 345 } 342 346 347 /** 348 * @covers ::bbp_increase_topic_reply_count 349 */ 350 public function test_bbp_increase_topic_reply_count() { 351 $t = $this->factory->topic->create(); 352 353 $count = bbp_get_topic_reply_count( $t ); 354 $this->assertSame( '0', $count ); 355 356 bbp_increase_topic_reply_count( $t ); 357 358 $count = bbp_get_topic_reply_count( $t ); 359 $this->assertSame( '1', $count ); 360 } 361 362 /** 363 * @covers ::bbp_decrease_topic_reply_count 364 */ 365 public function test_bbp_decrease_topic_reply_count() { 366 $t = $this->factory->topic->create(); 367 368 $count = bbp_get_topic_reply_count( $t ); 369 $this->assertSame( '0', $count ); 370 371 // Set the count manually to 9 372 bbp_update_topic_reply_count( $t, 9 ); 373 374 $count = bbp_get_topic_reply_count( $t ); 375 $this->assertSame( '9', $count ); 376 377 bbp_decrease_topic_reply_count( $t ); 378 379 $count = bbp_get_topic_reply_count( $t ); 380 $this->assertSame( '8', $count ); 381 } 382 343 383 /** 344 384 * @covers ::bbp_bump_topic_reply_count_hidden 345 385 */ … … class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 358 398 $this->assertSame( '4', $count ); 359 399 } 360 400 401 /** 402 * @covers ::bbp_increase_topic_reply_count_hidden 403 */ 404 public function test_bbp_increase_topic_reply_count_hidden() { 405 $t = $this->factory->topic->create(); 406 407 $count = bbp_get_topic_reply_count_hidden( $t ); 408 $this->assertSame( '0', $count ); 409 410 bbp_increase_topic_reply_count_hidden( $t ); 411 412 $count = bbp_get_topic_reply_count_hidden( $t ); 413 $this->assertSame( '1', $count ); 414 } 415 416 /** 417 * @covers ::bbp_decrease_topic_reply_count_hidden 418 */ 419 public function test_bbp_decrease_topic_reply_count_hidden() { 420 $t = $this->factory->topic->create(); 421 422 $count = bbp_get_topic_reply_count_hidden( $t ); 423 $this->assertSame( '0', $count ); 424 425 // Set the count manually to 9 426 bbp_update_topic_reply_count_hidden( $t, 9 ); 427 428 $count = bbp_get_topic_reply_count_hidden( $t ); 429 $this->assertSame( '9', $count ); 430 431 bbp_decrease_topic_reply_count_hidden( $t ); 432 433 $count = bbp_get_topic_reply_count_hidden( $t ); 434 $this->assertSame( '8', $count ); 435 } 436 361 437 /** 362 438 * @covers ::bbp_update_topic_reply_count 363 439 */ -
tests/phpunit/testcases/topics/functions/topic.php
diff --git tests/phpunit/testcases/topics/functions/topic.php tests/phpunit/testcases/topics/functions/topic.php index 5b191fb..f26840d 100644
class BBP_Tests_Topics_Functions_Topic extends BBP_UnitTestCase { 181 181 $this->assertSame( $new_forum_id, bbp_get_reply_forum_id( $reply_id ) ); 182 182 $this->assertSame( $topic_id, bbp_get_reply_topic_id( $reply_id ) ); 183 183 184 // Old Topic/Reply Counts 185 $this->assertSame( 0, bbp_get_forum_topic_count( $old_forum_id, true, true ) ); 186 $this->assertSame( 0, bbp_get_forum_reply_count( $old_forum_id, true, true ) ); 187 188 184 189 // Retore the user 185 190 $this->set_current_user( $this->old_current_user ); 186 191 }