Ticket #1799: 1799.3.patch
File 1799.3.patch, 18.2 KB (added by , 11 years ago) |
---|
-
src/includes/common/functions.php
1400 1400 * @param int $parent_id Parent id 1401 1401 * @param string $post_type Post type. Defaults to 'post' 1402 1402 * @uses bbp_get_topic_post_type() To get the topic post type 1403 * @uses wp_cache_get() To check if there is a cache of the last child id 1404 * @uses wpdb::prepare() To prepare the query 1405 * @uses wpdb::get_var() To get the result of the query in a variable 1406 * @uses wp_cache_set() To set the cache for future use 1403 * @uses WP_Query To get get the posts 1407 1404 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child 1408 1405 * id, parent id and post type 1409 1406 * @return int The last active post_id 1410 1407 */ 1411 1408 function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) { 1412 global $wpdb;1413 1409 1414 1410 // Bail if nothing passed 1415 if ( empty( $parent_id ) ) 1411 if ( empty( $parent_id ) ) { 1416 1412 return false; 1413 } 1417 1414 1418 // The ID of the cached query1419 $ cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';1415 // Get the public posts status 1416 $post_status = array( bbp_get_public_status_id() ); 1420 1417 1418 // Add closed status if topic post type 1419 if ( $post_type === bbp_get_topic_post_type() ) { 1420 $post_status[] = bbp_get_closed_status_id(); 1421 } 1422 1421 1423 // Check for cache and set if needed 1422 $child_id = wp_cache_get( $cache_id, 'bbpress_posts' ); 1423 if ( false === $child_id ) { 1424 $post_status = array( bbp_get_public_status_id() ); 1424 $query = new WP_Query( array( 1425 'fields' => 'ids', 1426 'post_parent' => $parent_id, 1427 'post_status' => $post_status, 1428 'post_type' => $post_type, 1425 1429 1426 // Add closed status if topic post type 1427 if ( $post_type === bbp_get_topic_post_type() ) { 1428 $post_status[] = bbp_get_closed_status_id(); 1429 } 1430 // Maybe change these later 1431 'posts_per_page' => 1, 1432 'update_post_term_cache' => false, 1433 'update_post_meta_cache' => false, 1434 'ignore_sticky_posts' => true, 1435 ) ); 1436 $child_id = array_shift( $query->posts ); 1437 unset( $query ); 1430 1438 1431 // Join post statuses together1432 $post_status = "'" . implode( "', '", $post_status ) . "'";1433 1434 $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 ) );1435 wp_cache_set( $cache_id, $child_id, 'bbpress_posts' );1436 }1437 1438 1439 // Filter and return 1439 1440 return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, $parent_id, $post_type ); 1440 1441 } … … 1445 1446 * @param int $parent_id Parent id 1446 1447 * @param string $post_type Post type. Defaults to 'post' 1447 1448 * @uses bbp_get_topic_post_type() To get the topic post type 1448 * @uses wp_cache_get() To check if there is a cache of the children count 1449 * @uses wpdb::prepare() To prepare the query 1450 * @uses wpdb::get_var() To get the result of the query in a variable 1451 * @uses wp_cache_set() To set the cache for future use 1449 * @uses WP_Query To get get the posts 1452 1450 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child 1453 1451 * count, parent id and post type 1454 1452 * @return int The number of children 1455 1453 */ 1456 1454 function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) { 1457 global $wpdb;1458 1455 1459 1456 // Bail if nothing passed 1460 if ( empty( $parent_id ) ) 1457 if ( empty( $parent_id ) ) { 1461 1458 return false; 1459 } 1462 1460 1463 // The ID of the cached query1464 $ cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';1461 // Check the public post status 1462 $post_status = array( bbp_get_public_status_id() ); 1465 1463 1466 // Check for cache and set if needed1467 $child_count = wp_cache_get( $cache_id, 'bbpress_posts' );1468 if ( false === $child_count ) {1469 $post_status = array( bbp_get_public_status_id() );1464 // Add closed status if topic post type 1465 if ( $post_type === bbp_get_topic_post_type() ) { 1466 $post_status[] = bbp_get_closed_status_id(); 1467 } 1470 1468 1471 // Add closed status if topic post type 1472 if ( $post_type === bbp_get_topic_post_type() ) { 1473 $post_status[] = bbp_get_closed_status_id(); 1474 } 1469 $query = new WP_Query( array( 1470 'fields' => 'ids', 1471 'post_parent' => $parent_id, 1472 'post_status' => $post_status, 1473 'post_type' => $post_type, 1475 1474 1476 // Join post statuses together 1477 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1475 // Maybe change these later 1476 'posts_per_page' => -1, 1477 'update_post_term_cache' => false, 1478 'update_post_meta_cache' => false, 1479 'ignore_sticky_posts' => true, 1480 ) ); 1481 $child_count = $query->post_count; 1482 unset( $query ); 1478 1483 1479 $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 ) );1480 wp_cache_set( $cache_id, $child_count, 'bbpress_posts' );1481 }1482 1483 1484 // Filter and return 1484 1485 return apply_filters( 'bbp_get_public_child_count', (int) $child_count, $parent_id, $post_type ); 1485 1486 } … … 1490 1491 * @param int $parent_id Parent id 1491 1492 * @param string $post_type Post type. Defaults to 'post' 1492 1493 * @uses bbp_get_topic_post_type() To get the topic post type 1493 * @uses wp_cache_get() To check if there is a cache of the children 1494 * @uses wpdb::prepare() To prepare the query 1495 * @uses wpdb::get_col() To get the result of the query in an array 1496 * @uses wp_cache_set() To set the cache for future use 1494 * @uses WP_Query To get get the posts 1497 1495 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, 1498 1496 * parent id and post type 1499 1497 * @return array The array of children 1500 1498 */ 1501 1499 function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) { 1502 global $wpdb;1503 1500 1504 1501 // Bail if nothing passed 1505 if ( empty( $parent_id ) ) 1502 if ( empty( $parent_id ) ) { 1506 1503 return false; 1504 } 1507 1505 1508 // The ID of the cached query1509 $ cache_id = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids';1506 // Get the public post status 1507 $post_status = array( bbp_get_public_status_id() ); 1510 1508 1511 // Check for cache and set if needed1512 $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' );1513 if ( false === $child_ids ) {1514 $post_status = array( bbp_get_public_status_id() );1509 // Add closed status if topic post type 1510 if ( $post_type === bbp_get_topic_post_type() ) { 1511 $post_status[] = bbp_get_closed_status_id(); 1512 } 1515 1513 1516 // Add closed status if topic post type 1517 if ( $post_type === bbp_get_topic_post_type() ) { 1518 $post_status[] = bbp_get_closed_status_id(); 1519 } 1514 $query = new WP_Query( array( 1515 'fields' => 'ids', 1516 'post_parent' => $parent_id, 1517 'post_status' => $post_status, 1518 'post_type' => $post_type, 1520 1519 1521 // Join post statuses together 1522 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1520 // Maybe change these later 1521 'posts_per_page' => -1, 1522 'update_post_term_cache' => false, 1523 'update_post_meta_cache' => false, 1524 'ignore_sticky_posts' => true, 1525 ) ); 1526 $child_ids = !empty( $query->posts ) ? $query->posts : array(); 1527 unset( $query ); 1523 1528 1524 $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 ) );1525 wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );1526 }1527 1528 1529 // Filter and return 1529 1530 return apply_filters( 'bbp_get_public_child_ids', $child_ids, $parent_id, $post_type ); 1530 1531 } … … 1535 1536 * @param int $parent_id Parent id 1536 1537 * @param string $post_type Post type. Defaults to 'post' 1537 1538 * @uses bbp_get_topic_post_type() To get the topic post type 1538 * @uses wp_cache_get() To check if there is a cache of the children 1539 * @uses wpdb::prepare() To prepare the query 1540 * @uses wpdb::get_col() To get the result of the query in an array 1541 * @uses wp_cache_set() To set the cache for future use 1539 * @uses WP_Query To get get the posts 1542 1540 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, 1543 1541 * parent id and post type 1544 1542 * @return array The array of children 1545 1543 */ 1546 1544 function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) { 1547 global $wpdb;1548 1545 1549 1546 // Bail if nothing passed 1550 if ( empty( $parent_id ) ) 1547 if ( empty( $parent_id ) ) { 1551 1548 return false; 1549 } 1552 1550 1553 // The ID of the cached query1554 $ cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';1551 // Get the public post status 1552 $post_status = array( bbp_get_public_status_id() ); 1555 1553 1556 // Check for cache and set if needed 1557 $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' ); 1558 if ( false === $child_ids ) { 1559 $post_status = array( bbp_get_public_status_id() ); 1554 // Extra post statuses based on post type 1555 switch ( $post_type ) { 1560 1556 1561 // Extra post statuses based on post type 1562 switch ( $post_type ) { 1557 // Forum 1558 case bbp_get_forum_post_type() : 1559 $post_status[] = bbp_get_private_status_id(); 1560 $post_status[] = bbp_get_hidden_status_id(); 1561 break; 1563 1562 1564 // Forum 1565 case bbp_get_forum_post_type() : 1566 $post_status[] = bbp_get_private_status_id(); 1567 $post_status[] = bbp_get_hidden_status_id(); 1568 break; 1563 // Topic 1564 case bbp_get_topic_post_type() : 1565 $post_status[] = bbp_get_closed_status_id(); 1566 $post_status[] = bbp_get_trash_status_id(); 1567 $post_status[] = bbp_get_spam_status_id(); 1568 break; 1569 1569 1570 // Topic1571 case bbp_get_topic_post_type() :1572 $post_status[] = bbp_get_closed_status_id();1573 $post_status[] = bbp_get_trash_status_id();1574 $post_status[] = bbp_get_spam_status_id();1575 break;1570 // Reply 1571 case bbp_get_reply_post_type() : 1572 $post_status[] = bbp_get_trash_status_id(); 1573 $post_status[] = bbp_get_spam_status_id(); 1574 break; 1575 } 1576 1576 1577 // Reply 1578 case bbp_get_reply_post_type() : 1579 $post_status[] = bbp_get_trash_status_id(); 1580 $post_status[] = bbp_get_spam_status_id(); 1581 break; 1582 } 1577 $query = new WP_Query( array( 1578 'fields' => 'ids', 1579 'post_parent' => $parent_id, 1580 'post_status' => 'any', 1581 'post_type' => $post_type, 1583 1582 1584 // Join post statuses together 1585 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1583 // Maybe change these later 1584 'posts_per_page' => -1, 1585 'update_post_term_cache' => false, 1586 'update_post_meta_cache' => false, 1587 'ignore_sticky_posts' => true, 1588 ) ); 1589 $child_ids = !empty( $query->posts ) ? $query->posts : array(); 1590 unset( $query ); 1586 1591 1587 $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 ) );1588 wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );1589 }1590 1591 1592 // Filter and return 1592 1593 return apply_filters( 'bbp_get_all_child_ids', $child_ids, (int) $parent_id, $post_type ); 1593 1594 } -
src/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_posts' );158 wp_cache_delete( 'bbp_parent_' . $_post->ID . '_type_' . $post_type . '_child_last_id', 'bbpress_posts' );159 wp_cache_delete( 'bbp_parent_' . $_post->ID . '_type_' . $post_type . '_child_count', 'bbpress_posts' );160 wp_cache_delete( 'bbp_parent_public_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress_posts' );161 wp_cache_delete( 'bbp_parent_all_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress_posts' );162 }163 164 148 // Invalidate parent caches 165 149 if ( ! empty( $_post->post_parent ) ) { 166 150 bbp_clean_post_cache( $_post->post_parent ); -
src/includes/forums/functions.php
929 929 // First, delete everything. 930 930 delete_option( '_bbp_private_forums' ); 931 931 delete_option( '_bbp_hidden_forums' ); 932 932 933 933 /** 934 934 * Don't search for both private/hidden statuses. Since 'pre_get_posts' is an 935 * action, it's not removed by suppress_filters. We need to make sure that 935 * action, it's not removed by suppress_filters. We need to make sure that 936 936 * we're only searching for the supplied post_status. 937 937 * 938 938 * @see https://bbpress.trac.wordpress.org/ticket/2512 … … 954 954 'post_status' => bbp_get_hidden_status_id(), 955 955 'fields' => 'ids' 956 956 ) ); 957 957 958 958 // Enable forum visibilty normalization 959 959 add_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility', 4 ); 960 960 … … 1449 1449 * @return int Topic hidden topic count 1450 1450 */ 1451 1451 function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) { 1452 global $wpdb;1453 1452 1454 1453 // If topic_id was passed as $forum_id, then get its forum 1455 1454 if ( bbp_is_topic( $forum_id ) ) { … … 1466 1465 1467 1466 // Get topics of forum 1468 1467 if ( empty( $topic_count ) ) { 1469 $post_status = "'" . implode( "','", array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "'"; 1470 $topic_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';", $forum_id, bbp_get_topic_post_type() ) ); 1468 $query = new WP_Query( array( 1469 'fields' => 'ids', 1470 'post_parent' => $forum_id, 1471 'post_status' => array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ), 1472 'post_type' => bbp_get_topic_post_type(), 1473 1474 // Maybe change these later 1475 'posts_per_page' => -1, 1476 'update_post_term_cache' => false, 1477 'update_post_meta_cache' => false, 1478 'ignore_sticky_posts' => true, 1479 ) ); 1480 $topic_count = $query->post_count; 1481 unset( $query ); 1471 1482 } 1472 1483 1473 1484 // Update the count … … 1499 1510 * @return int Forum reply count 1500 1511 */ 1501 1512 function bbp_update_forum_reply_count( $forum_id = 0 ) { 1502 global $wpdb;1503 1513 1504 1514 $forum_id = bbp_get_forum_id( $forum_id ); 1505 1515 $children_reply_count = 0; … … 1515 1525 // Don't count replies if the forum is a category 1516 1526 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1517 1527 if ( !empty( $topic_ids ) ) { 1518 $topic_ids = implode( ',', wp_parse_id_list( $topic_ids ) ); 1519 $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) ); 1528 $query = new WP_Query( array( 1529 'fields' => 'ids', 1530 'post_parent__in' => $topic_ids, 1531 'post_status' => bbp_get_public_status_id(), 1532 'post_type' => bbp_get_reply_post_type(), 1533 1534 // Maybe change these later 1535 'posts_per_page' => -1, 1536 'update_post_term_cache' => false, 1537 'update_post_meta_cache' => false, 1538 'ignore_sticky_posts' => true, 1539 ) ); 1540 $reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0; 1541 unset( $query ); 1520 1542 } else { 1521 1543 $reply_count = 0; 1522 1544 } … … 1904 1926 */ 1905 1927 function bbp_forum_query_subforum_ids( $forum_id ) { 1906 1928 $subforum_ids = bbp_get_all_child_ids( $forum_id, bbp_get_forum_post_type() ); 1907 //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );1908 1929 1909 1930 return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id ); 1910 1931 } 1911 1932 1912 1933 /** 1913 * Callback to sort forum ID's based on last active time1914 *1915 * @since bbPress (r3789)1916 * @param int $a First forum ID to compare1917 * @param int $b Second forum ID to compare1918 * @return Position change based on sort1919 */1920 function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {1921 $ta = get_post_meta( $a, '_bbp_last_active_time', true );1922 $tb = get_post_meta( $b, '_bbp_last_active_time', true );1923 return ( $ta < $tb ) ? -1 : 1;1924 }1925 1926 /**1927 1934 * Returns the forum's last reply id 1928 1935 * 1929 1936 * @since bbPress (r2908) … … 1930 1937 * 1931 1938 * @param int $forum_id Forum id 1932 1939 * @param int $topic_ids Optional. Topic ids 1933 * @uses wp_cache_get() To check for cache and retrieve it1934 1940 * @uses bbp_forum_query_topic_ids() To get the forum's topic ids 1935 * @uses wpdb::prepare() To prepare the query1936 * @uses wpdb::get_var() To execute the query and get the var back1937 1941 * @uses bbp_get_reply_post_type() To get the reply post type 1938 * @uses wp_cache_set() To set the cache for future use1939 1942 * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id 1940 1943 * and forum id 1941 1944 */ 1942 function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) { 1943 global $wpdb; 1945 function bbp_forum_query_last_reply_id( $forum_id = 0, $topic_ids = 0 ) { 1944 1946 1945 $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';1946 $ reply_id = (int) wp_cache_get( $cache_id, 'bbpress_posts');1947 // Validate forum 1948 $forum_id = bbp_get_forum_id( $forum_id ); 1947 1949 1948 if ( false === $reply_id ) { 1950 // Get topic ID's if none were passed 1951 if ( empty( $topic_ids ) ) { 1952 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1953 } 1949 1954 1950 if ( empty( $topic_ids ) ) { 1951 $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 1952 } 1955 $query = new WP_Query( array( 1956 'fields' => 'ids', 1957 'post_parent__in' => $topic_ids, 1958 'post_status' => bbp_get_public_status_id(), 1959 'post_type' => bbp_get_reply_post_type(), 1953 1960 1954 if ( !empty( $topic_ids ) ) {1955 $topic_ids = implode( ',', wp_parse_id_list( $topic_ids ) );1956 $reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( {$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() ) );1957 wp_cache_set( $cache_id, $reply_id, 'bbpress_posts' ); // May be (int) 01958 } else {1959 wp_cache_set( $cache_id, '0', 'bbpress_posts');1960 }1961 }1961 // Maybe change these later 1962 'posts_per_page' => 1, 1963 'update_post_term_cache' => false, 1964 'update_post_meta_cache' => false, 1965 'ignore_sticky_posts' => true, 1966 ) ); 1967 $reply_id = array_shift( $query->posts ); 1968 unset( $query ); 1962 1969 1963 1970 return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id ); 1964 1971 }