Ticket #1799: 1799.patch
File 1799.patch, 18.9 KB (added by , 12 years ago) |
---|
-
includes/common/functions.php
1184 1184 * @param int $parent_id Parent id 1185 1185 * @param string $post_type Post type. Defaults to 'post' 1186 1186 * @uses bbp_get_topic_post_type() To get the topic post type 1187 * @uses wp_cache_get() To check if there is a cache of the last child id 1188 * @uses wpdb::prepare() To prepare the query 1189 * @uses wpdb::get_var() To get the result of the query in a variable 1190 * @uses wp_cache_set() To set the cache for future use 1187 * @uses WP_Query To get get the posts 1191 1188 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child 1192 1189 * id, parent id and post type 1193 1190 * @return int The last active post_id 1194 1191 */ 1195 1192 function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) { 1196 global $wpdb;1197 1193 1198 1194 // Bail if nothing passed 1199 1195 if ( empty( $parent_id ) ) 1200 1196 return false; 1201 1197 1202 1198 // The ID of the cached query 1203 $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';1204 1199 $post_status = array( bbp_get_public_status_id() ); 1205 1200 1206 1201 // Add closed status if topic post type 1207 1202 if ( $post_type == bbp_get_topic_post_type() ) 1208 1203 $post_status[] = bbp_get_closed_status_id(); 1209 1204 1210 // Join post statuses together1211 $post_status = "'" . join( "', '", $post_status ) . "'";1212 1213 1205 // Check for cache and set if needed 1214 $child_id = wp_cache_get( $cache_id, 'bbpress' ); 1215 if ( empty( $child_id ) ) { 1216 $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 ) ); 1217 wp_cache_set( $cache_id, $child_id, 'bbpress' ); 1218 } 1206 $query = new WP_Query( array( 1207 'fields' => 'ids', 1208 'post_parent' => $parent_id, 1209 'post_status' => $post_status, 1210 'post_type' => $post_type, 1211 1212 // Maybe change these later 1213 'posts_per_page' => 1, 1214 'update_post_term_cache' => false, 1215 'update_post_meta_cache' => false, 1216 'ignore_sticky_posts' => true, 1217 ) ); 1218 $child_id = array_shift( $query->posts ); 1219 unset( $query ); 1219 1220 1220 1221 // Filter and return 1221 1222 return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, (int) $parent_id, $post_type ); … … 1227 1228 * @param int $parent_id Parent id 1228 1229 * @param string $post_type Post type. Defaults to 'post' 1229 1230 * @uses bbp_get_topic_post_type() To get the topic post type 1230 * @uses wp_cache_get() To check if there is a cache of the children count 1231 * @uses wpdb::prepare() To prepare the query 1232 * @uses wpdb::get_var() To get the result of the query in a variable 1233 * @uses wp_cache_set() To set the cache for future use 1231 * @uses WP_Query To get get the posts 1234 1232 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child 1235 1233 * count, parent id and post type 1236 1234 * @return int The number of children 1237 1235 */ 1238 1236 function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) { 1239 global $wpdb;1240 1237 1241 1238 // Bail if nothing passed 1242 1239 if ( empty( $parent_id ) ) 1243 1240 return false; 1244 1241 1245 1242 // The ID of the cached query 1246 $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';1247 1243 $post_status = array( bbp_get_public_status_id() ); 1248 1244 1249 1245 // Add closed status if topic post type 1250 if ( $post_type == bbp_get_topic_post_type())1246 if ( bbp_get_topic_post_type() == $post_type ) 1251 1247 $post_status[] = bbp_get_closed_status_id(); 1252 1248 1253 // Join post statuses together1254 $post_status = "'" . join( "', '", $post_status ) . "'";1255 1256 1249 // Check for cache and set if needed 1257 $child_count = wp_cache_get( $cache_id, 'bbpress' ); 1258 if ( empty( $child_count ) ) { 1259 $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 ) ); 1260 wp_cache_set( $cache_id, $child_count, 'bbpress' ); 1261 } 1250 $query = new WP_Query( array( 1251 'fields' => 'ids', 1252 'post_parent' => $parent_id, 1253 'post_status' => $post_status, 1254 'post_type' => $post_type, 1255 1256 // Maybe change these later 1257 'posts_per_page' => -1, 1258 'update_post_term_cache' => false, 1259 'update_post_meta_cache' => false, 1260 'ignore_sticky_posts' => true, 1261 ) ); 1262 $child_count = $query->post_count; 1263 unset( $query ); 1262 1264 1263 1265 // Filter and return 1264 1266 return apply_filters( 'bbp_get_public_child_count', (int) $child_count, (int) $parent_id, $post_type ); … … 1270 1272 * @param int $parent_id Parent id 1271 1273 * @param string $post_type Post type. Defaults to 'post' 1272 1274 * @uses bbp_get_topic_post_type() To get the topic post type 1273 * @uses wp_cache_get() To check if there is a cache of the children 1274 * @uses wpdb::prepare() To prepare the query 1275 * @uses wpdb::get_col() To get the result of the query in an array 1276 * @uses wp_cache_set() To set the cache for future use 1275 * @uses WP_Query To get get the posts 1277 1276 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, 1278 1277 * parent id and post type 1279 1278 * @return array The array of children 1280 1279 */ 1281 1280 function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) { 1282 global $wpdb;1283 1281 1284 1282 // Bail if nothing passed 1285 1283 if ( empty( $parent_id ) ) 1286 1284 return false; 1287 1285 1288 1286 // The ID of the cached query 1289 $cache_id = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids';1290 1287 $post_status = array( bbp_get_public_status_id() ); 1291 1288 1292 1289 // Add closed status if topic post type 1293 1290 if ( $post_type == bbp_get_topic_post_type() ) 1294 1291 $post_status[] = bbp_get_closed_status_id(); 1295 1292 1296 // Join post statuses together1297 $post_status = "'" . join( "', '", $post_status ) . "'";1298 1299 1293 // Check for cache and set if needed 1300 $child_ids = wp_cache_get( $cache_id, 'bbpress' ); 1301 if ( empty( $child_ids ) ) { 1302 $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 ) ); 1303 wp_cache_set( $cache_id, $child_ids, 'bbpress' ); 1304 } 1294 $query = new WP_Query( array( 1295 'fields' => 'ids', 1296 'post_parent' => $parent_id, 1297 'post_status' => $post_status, 1298 'post_type' => $post_type, 1299 1300 // Maybe change these later 1301 'posts_per_page' => -1, 1302 'update_post_term_cache' => false, 1303 'update_post_meta_cache' => false, 1304 'ignore_sticky_posts' => true, 1305 ) ); 1306 $child_ids = !empty( $query->posts ) ? $query->posts : array(); 1307 unset( $query ); 1305 1308 1306 1309 // Filter and return 1307 1310 return apply_filters( 'bbp_get_public_child_ids', $child_ids, (int) $parent_id, $post_type ); … … 1312 1315 * @param int $parent_id Parent id 1313 1316 * @param string $post_type Post type. Defaults to 'post' 1314 1317 * @uses bbp_get_topic_post_type() To get the topic post type 1315 * @uses wp_cache_get() To check if there is a cache of the children 1316 * @uses wpdb::prepare() To prepare the query 1317 * @uses wpdb::get_col() To get the result of the query in an array 1318 * @uses wp_cache_set() To set the cache for future use 1318 * @uses WP_Query To get get the posts 1319 1319 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, 1320 1320 * parent id and post type 1321 1321 * @return array The array of children 1322 1322 */ 1323 1323 function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) { 1324 global $wpdb;1325 1324 1326 1325 // Bail if nothing passed 1327 1326 if ( empty( $parent_id ) ) 1328 1327 return false; 1329 1328 1330 1329 // The ID of the cached query 1331 $cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';1332 1330 $post_status = array( bbp_get_public_status_id() ); 1333 1331 1334 1332 // Extra post statuses based on post type … … 1354 1352 break; 1355 1353 } 1356 1354 1357 // Join post statuses together1358 $post_status = "'" . join( "', '", $post_status ) . "'";1359 1360 1355 // Check for cache and set if needed 1361 $child_ids = wp_cache_get( $cache_id, 'bbpress' ); 1362 if ( empty( $child_ids ) ) { 1363 $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 ) ); 1364 wp_cache_set( $cache_id, $child_ids, 'bbpress' ); 1365 } 1356 $query = new WP_Query( array( 1357 'fields' => 'ids', 1358 'post_parent' => $parent_id, 1359 'post_status' => 'any', 1360 'post_type' => $post_type, 1361 1362 // Maybe change these later 1363 'posts_per_page' => -1, 1364 'update_post_term_cache' => false, 1365 'update_post_meta_cache' => false, 1366 'ignore_sticky_posts' => true, 1367 ) ); 1368 $child_ids = !empty( $query->posts ) ? $query->posts : array(); 1369 unset( $query ); 1366 1370 1367 1371 // Filter and return 1368 1372 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
1368 1368 * @return int Topic hidden topic count 1369 1369 */ 1370 1370 function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) { 1371 global $wpdb;1372 1371 1373 1372 // If topic_id was passed as $forum_id, then get its forum 1374 1373 if ( bbp_is_topic( $forum_id ) ) { … … 1384 1383 if ( !empty( $forum_id ) ) { 1385 1384 1386 1385 // Get topics of forum 1387 if ( empty( $topic_count ) ) 1388 $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() ) ); 1386 if ( empty( $topic_count ) ) { 1387 $query = new WP_Query( array( 1388 'fields' => 'ids', 1389 'post_parent' => $forum_id, 1390 'post_status' => array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ), 1391 'post_type' => bbp_get_topic_post_type(), 1389 1392 1393 // Maybe change these later 1394 'posts_per_page' => -1, 1395 'update_post_term_cache' => false, 1396 'update_post_meta_cache' => false, 1397 'ignore_sticky_posts' => true, 1398 ) ); 1399 $topic_count = $query->post_count; 1400 unset( $query ); 1401 } 1402 1390 1403 // Update the count 1391 1404 update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count ); 1392 1405 } … … 1416 1429 * @return int Forum reply count 1417 1430 */ 1418 1431 function bbp_update_forum_reply_count( $forum_id = 0 ) { 1419 global $wpdb;1420 1421 1432 $forum_id = bbp_get_forum_id( $forum_id ); 1422 1433 $children_reply_count = 0; 1423 1434 … … 1431 1442 1432 1443 // Don't count replies if the forum is a category 1433 1444 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1434 if ( !empty( $topic_ids ) ) 1435 $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() ) ); 1436 else 1445 if ( !empty( $topic_ids ) ) { 1446 $query = new WP_Query( array( 1447 'fields' => 'ids', 1448 'post_parent__in' => $topic_ids, 1449 'post_status' => bbp_get_public_status_id(), 1450 'post_type' => bbp_get_reply_post_type(), 1451 1452 // Maybe change these later 1453 'posts_per_page' => -1, 1454 'update_post_term_cache' => false, 1455 'update_post_meta_cache' => false, 1456 'ignore_sticky_posts' => true, 1457 ) ); 1458 $reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0; 1459 unset( $query ); 1460 } else { 1437 1461 $reply_count = 0; 1462 } 1438 1463 1439 1464 // Calculate total replies in this forum 1440 1465 $total_replies = (int) $reply_count + $children_reply_count; … … 1749 1774 */ 1750 1775 function bbp_forum_query_subforum_ids( $forum_id ) { 1751 1776 $subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() ); 1752 //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );1753 1777 1754 1778 return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id ); 1755 1779 } 1756 1780 1757 1781 /** 1758 * Callback to sort forum ID's based on last active time1759 *1760 * @since bbPress (r3789)1761 * @param int $a First forum ID to compare1762 * @param int $b Second forum ID to compare1763 * @return Position change based on sort1764 */1765 function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {1766 $ta = get_post_meta( $a, '_bbp_last_active_time', true );1767 $tb = get_post_meta( $b, '_bbp_last_active_time', true );1768 return ( $ta < $tb ) ? -1 : 1;1769 }1770 1771 /**1772 1782 * Returns the forum's last reply id 1773 1783 * 1774 1784 * @since bbPress (r2908) 1775 1785 * 1776 1786 * @param int $forum_id Forum id 1777 1787 * @param int $topic_ids Optional. Topic ids 1778 * @uses wp_cache_get() To check for cache and retrieve it1779 1788 * @uses bbp_forum_query_topic_ids() To get the forum's topic ids 1780 * @uses wpdb::prepare() To prepare the query1781 * @uses wpdb::get_var() To execute the query and get the var back1782 1789 * @uses bbp_get_reply_post_type() To get the reply post type 1783 * @uses wp_cache_set() To set the cache for future use1784 1790 * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id 1785 1791 * and forum id 1786 1792 */ 1787 function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) { 1788 global $wpdb; 1793 function bbp_forum_query_last_reply_id( $forum_id = 0, $topic_ids = 0 ) { 1789 1794 1790 $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';1791 $ reply_id = (int) wp_cache_get( $cache_id, 'bbpress');1795 // Validate forum 1796 $forum_id = bbp_get_forum_id( $forum_id ); 1792 1797 1793 if ( empty( $reply_id ) ) { 1798 // Get topic ID's if none were passed 1799 if ( empty( $topic_ids ) ) 1800 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1794 1801 1795 if ( empty( $topic_ids ) ) { 1796 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1797 } 1802 // Check for cache and set if needed 1803 $query = new WP_Query( array( 1804 'fields' => 'ids', 1805 'post_parent__in' => $topic_ids, 1806 'post_status' => bbp_get_public_status_id(), 1807 'post_type' => bbp_get_reply_post_type(), 1808 1809 // Maybe change these later 1810 'posts_per_page' => 1, 1811 'update_post_term_cache' => false, 1812 'update_post_meta_cache' => false, 1813 'ignore_sticky_posts' => true, 1814 ) ); 1815 $reply_id = array_shift( $query->posts ); 1816 unset( $query ); 1798 1817 1799 if ( !empty( $topic_ids ) ) {1800 $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() ) );1801 wp_cache_set( $cache_id, $reply_id, 'bbpress' ); // May be (int) 01802 } else {1803 wp_cache_set( $cache_id, '0', 'bbpress' );1804 }1805 }1806 1807 1818 return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id ); 1808 1819 } 1809 1820 -
includes/replies/template-tags.php
85 85 'post_status' => $default_post_status, // Of this status 86 86 'posts_per_page' => bbp_get_replies_per_page(), // This many 87 87 'paged' => bbp_get_paged(), // On this page 88 'orderby' => ' date', // Sorted by date88 'orderby' => 'ID', // Sorted by ID 89 89 'order' => 'ASC', // Oldest to newest 90 90 's' => $default_reply_search, // Maybe search 91 91 ); -
includes/topics/functions.php
2241 2241 function bbp_update_topic_forum_id( $topic_id = 0, $forum_id = 0 ) { 2242 2242 2243 2243 // If it's a reply, then get the parent (topic id) 2244 if ( bbp_is_reply( $topic_id ) ) 2244 if ( bbp_is_reply( $topic_id ) ) { 2245 2245 $topic_id = bbp_get_reply_topic_id( $topic_id ); 2246 else2246 } else { 2247 2247 $topic_id = bbp_get_topic_id( $topic_id ); 2248 } 2248 2249 2249 2250 if ( empty( $forum_id ) ) 2250 2251 $forum_id = get_post_field( 'post_parent', $topic_id ); … … 2293 2294 function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = 0 ) { 2294 2295 2295 2296 // If it's a reply, then get the parent (topic id) 2296 if ( bbp_is_reply( $topic_id ) ) 2297 if ( bbp_is_reply( $topic_id ) ) { 2297 2298 $topic_id = bbp_get_reply_topic_id( $topic_id ); 2298 else2299 } else { 2299 2300 $topic_id = bbp_get_topic_id( $topic_id ); 2301 } 2300 2302 2301 2303 // Get replies of topic if not passed 2302 2304 if ( empty( $reply_count ) ) … … 2329 2331 global $wpdb; 2330 2332 2331 2333 // If it's a reply, then get the parent (topic id) 2332 if ( bbp_is_reply( $topic_id ) ) 2334 if ( bbp_is_reply( $topic_id ) ) { 2333 2335 $topic_id = bbp_get_reply_topic_id( $topic_id ); 2334 else2336 } else { 2335 2337 $topic_id = bbp_get_topic_id( $topic_id ); 2338 } 2336 2339 2337 2340 // Get replies of topic 2338 2341 if ( empty( $reply_count ) ) … … 2364 2367 function bbp_update_topic_last_active_id( $topic_id = 0, $active_id = 0 ) { 2365 2368 2366 2369 // If it's a reply, then get the parent (topic id) 2367 if ( bbp_is_reply( $topic_id ) ) 2370 if ( bbp_is_reply( $topic_id ) ) { 2368 2371 $topic_id = bbp_get_reply_topic_id( $topic_id ); 2369 else2372 } else { 2370 2373 $topic_id = bbp_get_topic_id( $topic_id ); 2374 } 2371 2375 2372 2376 if ( empty( $active_id ) ) 2373 2377 $active_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ); … … 2399 2403 function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) { 2400 2404 2401 2405 // If it's a reply, then get the parent (topic id) 2402 if ( bbp_is_reply( $topic_id ) ) 2406 if ( bbp_is_reply( $topic_id ) ) { 2403 2407 $topic_id = bbp_get_reply_topic_id( $topic_id ); 2404 else2408 } else { 2405 2409 $topic_id = bbp_get_topic_id( $topic_id ); 2410 } 2406 2411 2407 2412 // Check time and use current if empty 2408 if ( empty( $new_time ) ) 2413 if ( empty( $new_time ) ) { 2409 2414 $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) ); 2415 } 2410 2416 2411 2417 // Update only if published 2412 if ( !empty( $new_time ) ) 2418 if ( !empty( $new_time ) ) { 2413 2419 update_post_meta( $topic_id, '_bbp_last_active_time', $new_time ); 2420 } 2414 2421 2415 2422 return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id ); 2416 2423 } -
includes/users/functions.php
1037 1037 * Get the total number of users on the forums 1038 1038 * 1039 1039 * @since bbPress (r2769) 1040 * @uses wp_cache_get() Check if query is in cache 1041 * @uses get_users() To execute our query and get the var back 1042 * @uses wp_cache_set() Set the query in the cache 1040 * @uses count_users() To execute our query and get the var back 1043 1041 * @uses apply_filters() Calls 'bbp_get_total_users' with number of users 1044 1042 * @return int Total number of users 1045 1043 */