Ticket #1799: 1799.2.patch
File 1799.2.patch, 15.4 KB (added by , 12 years ago) |
---|
-
includes/common/functions.php
1202 1202 * @param int $parent_id Parent id 1203 1203 * @param string $post_type Post type. Defaults to 'post' 1204 1204 * @uses bbp_get_topic_post_type() To get the topic post type 1205 * @uses wp_cache_get() To check if there is a cache of the last child id 1206 * @uses wpdb::prepare() To prepare the query 1207 * @uses wpdb::get_var() To get the result of the query in a variable 1208 * @uses wp_cache_set() To set the cache for future use 1205 * @uses WP_Query To get get the posts 1209 1206 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child 1210 1207 * id, parent id and post type 1211 1208 * @return int The last active post_id 1212 1209 */ 1213 1210 function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) { 1214 global $wpdb;1215 1211 1216 1212 // Bail if nothing passed 1217 1213 if ( empty( $parent_id ) ) 1218 1214 return false; 1219 1215 1220 1216 // The ID of the cached query 1221 $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';1222 1217 $post_status = array( bbp_get_public_status_id() ); 1223 1218 1224 1219 // Add closed status if topic post type 1225 1220 if ( $post_type == bbp_get_topic_post_type() ) 1226 1221 $post_status[] = bbp_get_closed_status_id(); 1227 1222 1228 // Join post statuses together1229 $post_status = "'" . join( "', '", $post_status ) . "'";1230 1231 1223 // Check for cache and set if needed 1232 $child_id = wp_cache_get( $cache_id, 'bbpress' ); 1233 if ( empty( $child_id ) ) { 1234 $child_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type ) ); 1235 wp_cache_set( $cache_id, $child_id, 'bbpress' ); 1236 } 1224 $query = new WP_Query( array( 1225 'fields' => 'ids', 1226 'post_parent' => $parent_id, 1227 'post_status' => $post_status, 1228 'post_type' => $post_type, 1229 1230 // Maybe change these later 1231 'posts_per_page' => 1, 1232 'update_post_term_cache' => false, 1233 'update_post_meta_cache' => false, 1234 'ignore_sticky_posts' => true, 1235 ) ); 1236 $child_id = array_shift( $query->posts ); 1237 unset( $query ); 1237 1238 1238 1239 // Filter and return 1239 1240 return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, (int) $parent_id, $post_type ); … … 1245 1246 * @param int $parent_id Parent id 1246 1247 * @param string $post_type Post type. Defaults to 'post' 1247 1248 * @uses bbp_get_topic_post_type() To get the topic post type 1248 * @uses wp_cache_get() To check if there is a cache of the children count 1249 * @uses wpdb::prepare() To prepare the query 1250 * @uses wpdb::get_var() To get the result of the query in a variable 1251 * @uses wp_cache_set() To set the cache for future use 1249 * @uses WP_Query To get get the posts 1252 1250 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child 1253 1251 * count, parent id and post type 1254 1252 * @return int The number of children 1255 1253 */ 1256 1254 function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) { 1257 global $wpdb;1258 1255 1259 1256 // Bail if nothing passed 1260 1257 if ( empty( $parent_id ) ) 1261 1258 return false; 1262 1259 1263 1260 // The ID of the cached query 1264 $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';1265 1261 $post_status = array( bbp_get_public_status_id() ); 1266 1262 1267 1263 // Add closed status if topic post type 1268 if ( $post_type == bbp_get_topic_post_type())1264 if ( bbp_get_topic_post_type() == $post_type ) 1269 1265 $post_status[] = bbp_get_closed_status_id(); 1270 1266 1271 // Join post statuses together1272 $post_status = "'" . join( "', '", $post_status ) . "'";1273 1274 1267 // Check for cache and set if needed 1275 $child_count = wp_cache_get( $cache_id, 'bbpress' ); 1276 if ( empty( $child_count ) ) { 1277 $child_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type ) ); 1278 wp_cache_set( $cache_id, $child_count, 'bbpress' ); 1279 } 1268 $query = new WP_Query( array( 1269 'fields' => 'ids', 1270 'post_parent' => $parent_id, 1271 'post_status' => $post_status, 1272 'post_type' => $post_type, 1273 1274 // Maybe change these later 1275 'posts_per_page' => -1, 1276 'update_post_term_cache' => false, 1277 'update_post_meta_cache' => false, 1278 'ignore_sticky_posts' => true, 1279 ) ); 1280 $child_count = $query->post_count; 1281 unset( $query ); 1280 1282 1281 1283 // Filter and return 1282 1284 return apply_filters( 'bbp_get_public_child_count', (int) $child_count, (int) $parent_id, $post_type ); … … 1288 1290 * @param int $parent_id Parent id 1289 1291 * @param string $post_type Post type. Defaults to 'post' 1290 1292 * @uses bbp_get_topic_post_type() To get the topic post type 1291 * @uses wp_cache_get() To check if there is a cache of the children 1292 * @uses wpdb::prepare() To prepare the query 1293 * @uses wpdb::get_col() To get the result of the query in an array 1294 * @uses wp_cache_set() To set the cache for future use 1293 * @uses WP_Query To get get the posts 1295 1294 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, 1296 1295 * parent id and post type 1297 1296 * @return array The array of children 1298 1297 */ 1299 1298 function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) { 1300 global $wpdb;1301 1299 1302 1300 // Bail if nothing passed 1303 1301 if ( empty( $parent_id ) ) 1304 1302 return false; 1305 1303 1306 1304 // The ID of the cached query 1307 $cache_id = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids';1308 1305 $post_status = array( bbp_get_public_status_id() ); 1309 1306 1310 1307 // Add closed status if topic post type 1311 1308 if ( $post_type == bbp_get_topic_post_type() ) 1312 1309 $post_status[] = bbp_get_closed_status_id(); 1313 1310 1314 // Join post statuses together1315 $post_status = "'" . join( "', '", $post_status ) . "'";1316 1317 1311 // Check for cache and set if needed 1318 $child_ids = wp_cache_get( $cache_id, 'bbpress' ); 1319 if ( empty( $child_ids ) ) { 1320 $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) ); 1321 wp_cache_set( $cache_id, $child_ids, 'bbpress' ); 1322 } 1312 $query = new WP_Query( array( 1313 'fields' => 'ids', 1314 'post_parent' => $parent_id, 1315 'post_status' => $post_status, 1316 'post_type' => $post_type, 1317 1318 // Maybe change these later 1319 'posts_per_page' => -1, 1320 'update_post_term_cache' => false, 1321 'update_post_meta_cache' => false, 1322 'ignore_sticky_posts' => true, 1323 ) ); 1324 $child_ids = !empty( $query->posts ) ? $query->posts : array(); 1325 unset( $query ); 1323 1326 1324 1327 // Filter and return 1325 1328 return apply_filters( 'bbp_get_public_child_ids', $child_ids, (int) $parent_id, $post_type ); 1326 1329 } 1330 1327 1331 /** 1328 1332 * Query the DB and get a the child id's of all children 1329 1333 * 1330 1334 * @param int $parent_id Parent id 1331 1335 * @param string $post_type Post type. Defaults to 'post' 1332 1336 * @uses bbp_get_topic_post_type() To get the topic post type 1333 * @uses wp_cache_get() To check if there is a cache of the children 1334 * @uses wpdb::prepare() To prepare the query 1335 * @uses wpdb::get_col() To get the result of the query in an array 1336 * @uses wp_cache_set() To set the cache for future use 1337 * @uses WP_Query To get get the posts 1337 1338 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, 1338 1339 * parent id and post type 1339 1340 * @return array The array of children 1340 1341 */ 1341 1342 function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) { 1342 global $wpdb;1343 1343 1344 1344 // Bail if nothing passed 1345 1345 if ( empty( $parent_id ) ) 1346 1346 return false; 1347 1347 1348 1348 // The ID of the cached query 1349 $cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';1350 1349 $post_status = array( bbp_get_public_status_id() ); 1351 1350 1352 1351 // Extra post statuses based on post type … … 1372 1371 break; 1373 1372 } 1374 1373 1375 // Join post statuses together1376 $post_status = "'" . join( "', '", $post_status ) . "'";1377 1378 1374 // Check for cache and set if needed 1379 $child_ids = wp_cache_get( $cache_id, 'bbpress' ); 1380 if ( empty( $child_ids ) ) { 1381 $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) ); 1382 wp_cache_set( $cache_id, $child_ids, 'bbpress' ); 1383 } 1375 $query = new WP_Query( array( 1376 'fields' => 'ids', 1377 'post_parent' => $parent_id, 1378 'post_status' => 'any', 1379 'post_type' => $post_type, 1380 1381 // Maybe change these later 1382 'posts_per_page' => -1, 1383 'update_post_term_cache' => false, 1384 'update_post_meta_cache' => false, 1385 'ignore_sticky_posts' => true, 1386 ) ); 1387 $child_ids = !empty( $query->posts ) ? $query->posts : array(); 1388 unset( $query ); 1384 1389 1385 1390 // Filter and return 1386 1391 return apply_filters( 'bbp_get_all_child_ids', $child_ids, (int) $parent_id, $post_type ); -
includes/core/cache.php
145 145 146 146 do_action( 'bbp_clean_post_cache', $_post->ID, $_post ); 147 147 148 // Child query types to clean149 $post_types = array(150 bbp_get_topic_post_type(),151 bbp_get_forum_post_type(),152 bbp_get_reply_post_type()153 );154 155 // Loop through query types and clean caches156 foreach ( $post_types as $post_type ) {157 wp_cache_delete( 'bbp_get_forum_' . $_post->ID . '_reply_id', 'bbpress' );158 wp_cache_delete( 'bbp_parent_' . $_post->ID . '_type_' . $post_type . '_child_last_id', 'bbpress' );159 wp_cache_delete( 'bbp_parent_' . $_post->ID . '_type_' . $post_type . '_child_count', 'bbpress' );160 wp_cache_delete( 'bbp_parent_public_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress' );161 wp_cache_delete( 'bbp_parent_all_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress' );162 }163 164 148 // Invalidate parent caches 165 149 if ( ! empty( $_post->post_parent ) ) { 166 150 bbp_clean_post_cache( $_post->post_parent ); -
includes/forums/functions.php
1343 1343 * @return int Topic hidden topic count 1344 1344 */ 1345 1345 function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) { 1346 global $wpdb;1347 1346 1348 1347 // If topic_id was passed as $forum_id, then get its forum 1349 1348 if ( bbp_is_topic( $forum_id ) ) { … … 1359 1358 if ( !empty( $forum_id ) ) { 1360 1359 1361 1360 // Get topics of forum 1362 if ( empty( $topic_count ) ) 1363 $topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "') AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) ); 1361 if ( empty( $topic_count ) ) { 1362 $query = new WP_Query( array( 1363 'fields' => 'ids', 1364 'post_parent' => $forum_id, 1365 'post_status' => array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ), 1366 'post_type' => bbp_get_topic_post_type(), 1364 1367 1368 // Maybe change these later 1369 'posts_per_page' => -1, 1370 'update_post_term_cache' => false, 1371 'update_post_meta_cache' => false, 1372 'ignore_sticky_posts' => true, 1373 ) ); 1374 $topic_count = $query->post_count; 1375 unset( $query ); 1376 } 1377 1365 1378 // Update the count 1366 1379 update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count ); 1367 1380 } … … 1391 1404 * @return int Forum reply count 1392 1405 */ 1393 1406 function bbp_update_forum_reply_count( $forum_id = 0 ) { 1394 global $wpdb;1395 1407 1396 1408 $forum_id = bbp_get_forum_id( $forum_id ); 1397 1409 $children_reply_count = 0; … … 1407 1419 // Don't count replies if the forum is a category 1408 1420 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1409 1421 if ( !empty( $topic_ids ) ) { 1410 $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) ); 1422 $query = new WP_Query( array( 1423 'fields' => 'ids', 1424 'post_parent__in' => $topic_ids, 1425 'post_status' => bbp_get_public_status_id(), 1426 'post_type' => bbp_get_reply_post_type(), 1427 1428 // Maybe change these later 1429 'posts_per_page' => -1, 1430 'update_post_term_cache' => false, 1431 'update_post_meta_cache' => false, 1432 'ignore_sticky_posts' => true, 1433 ) ); 1434 $reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0; 1435 unset( $query ); 1411 1436 } else { 1412 $reply_count = 0;1437 $reply_count = 0; 1413 1438 } 1414 1439 1415 1440 // Calculate total replies in this forum … … 1726 1751 */ 1727 1752 function bbp_forum_query_subforum_ids( $forum_id ) { 1728 1753 $subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() ); 1729 //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );1730 1754 1731 1755 return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id ); 1732 1756 } 1733 1757 1734 1758 /** 1735 * Callback to sort forum ID's based on last active time1736 *1737 * @since bbPress (r3789)1738 * @param int $a First forum ID to compare1739 * @param int $b Second forum ID to compare1740 * @return Position change based on sort1741 */1742 function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {1743 $ta = get_post_meta( $a, '_bbp_last_active_time', true );1744 $tb = get_post_meta( $b, '_bbp_last_active_time', true );1745 return ( $ta < $tb ) ? -1 : 1;1746 }1747 1748 /**1749 1759 * Returns the forum's last reply id 1750 1760 * 1751 1761 * @since bbPress (r2908) 1752 1762 * 1753 1763 * @param int $forum_id Forum id 1754 1764 * @param int $topic_ids Optional. Topic ids 1755 * @uses wp_cache_get() To check for cache and retrieve it1756 1765 * @uses bbp_forum_query_topic_ids() To get the forum's topic ids 1757 * @uses wpdb::prepare() To prepare the query1758 * @uses wpdb::get_var() To execute the query and get the var back1759 1766 * @uses bbp_get_reply_post_type() To get the reply post type 1760 * @uses wp_cache_set() To set the cache for future use1761 1767 * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id 1762 1768 * and forum id 1763 1769 */ 1764 function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) { 1765 global $wpdb; 1770 function bbp_forum_query_last_reply_id( $forum_id = 0, $topic_ids = 0 ) { 1766 1771 1767 $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';1768 $ reply_id = (int) wp_cache_get( $cache_id, 'bbpress');1772 // Validate forum 1773 $forum_id = bbp_get_forum_id( $forum_id ); 1769 1774 1770 if ( empty( $reply_id ) ) { 1775 // Get topic ID's if none were passed 1776 if ( empty( $topic_ids ) ) 1777 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1771 1778 1772 if ( empty( $topic_ids ) ) { 1773 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1774 } 1779 // Check for cache and set if needed 1780 $query = new WP_Query( array( 1781 'fields' => 'ids', 1782 'post_parent__in' => $topic_ids, 1783 'post_status' => bbp_get_public_status_id(), 1784 'post_type' => bbp_get_reply_post_type(), 1775 1785 1776 if ( !empty( $topic_ids ) ) { 1777 $reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) ); 1778 wp_cache_set( $cache_id, $reply_id, 'bbpress' ); // May be (int) 0 1779 } else { 1780 wp_cache_set( $cache_id, '0', 'bbpress' ); 1781 } 1782 } 1786 // Maybe change these later 1787 'posts_per_page' => 1, 1788 'update_post_term_cache' => false, 1789 'update_post_meta_cache' => false, 1790 'ignore_sticky_posts' => true, 1791 ) ); 1792 $reply_id = array_shift( $query->posts ); 1793 unset( $query ); 1783 1794 1784 return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );1785 }1795 return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id ); 1796 } 1786 1797 1787 1798 /** Listeners *****************************************************************/ 1788 1799