Changeset 5954 for trunk/src/includes/common/functions.php
- Timestamp:
- 11/18/2015 04:39:54 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/common/functions.php
r5951 r5954 1500 1500 * Query the DB and get the last public post_id that has parent_id as post_parent 1501 1501 * 1502 * @param int $parent_id Parent id 1503 * @param string $post_type Post type. Defaults to 'post' 1502 * @since 2.0.0 bbPress (r2868) 1503 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects 1504 * 1505 * @param int $parent_id Parent id. 1506 * @param string $post_type Post type. Defaults to 'post'. 1507 * @uses bbp_get_public_status_id() To get the public status id 1504 1508 * @uses bbp_get_topic_post_type() To get the topic post type 1505 * @uses wp_cache_get() To check if there is a cache of the last child id 1506 * @uses wpdb::prepare() To prepare the query 1507 * @uses wpdb::get_var() To get the result of the query in a variable 1508 * @uses wp_cache_set() To set the cache for future use 1509 * @uses bbp_get_closed_status_id() To get the closed status id 1510 * @uses WP_Query To get get the posts 1509 1511 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child 1510 1512 * id, parent id and post type … … 1518 1520 } 1519 1521 1520 // The ID of the cached query 1521 $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id'; 1522 1523 // Check for cache and set if needed 1524 $child_id = wp_cache_get( $cache_id, 'bbpress_posts' ); 1525 if ( false === $child_id ) { 1526 $post_status = array( bbp_get_public_status_id() ); 1527 1528 // Add closed status if topic post type 1529 if ( $post_type === bbp_get_topic_post_type() ) { 1530 $post_status[] = bbp_get_closed_status_id(); 1531 } 1532 1533 // Join post statuses together 1534 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1535 $bbp_db = bbp_db(); 1536 $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->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 ); 1537 $child_id = (int) $bbp_db->get_var( $query ); 1538 1539 wp_cache_set( $cache_id, $child_id, 'bbpress_posts' ); 1540 } else { 1541 $child_id = (int) $child_id; 1542 } 1522 // Get the public posts status 1523 $post_status = array( bbp_get_public_status_id() ); 1524 1525 // Add closed status if topic post type 1526 if ( bbp_get_topic_post_type() === $post_type ) { 1527 $post_status[] = bbp_get_closed_status_id(); 1528 } 1529 1530 $query = new WP_Query( array( 1531 'fields' => 'ids', 1532 'post_parent' => $parent_id, 1533 'post_status' => $post_status, 1534 'post_type' => $post_type, 1535 1536 // Maybe change these later 1537 'posts_per_page' => 1, 1538 'update_post_term_cache' => false, 1539 'update_post_meta_cache' => false, 1540 'ignore_sticky_posts' => true, 1541 ) ); 1542 $child_id = array_shift( $query->posts ); 1543 unset( $query ); 1543 1544 1544 1545 // Filter and return … … 1550 1551 * 1551 1552 * @since 2.0.0 bbPress (r2868) 1552 * 1553 * @param int $parent_id Parent id 1554 * @param string $post_type Post type. Defaults to 'post' 1553 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects 1554 * 1555 * @param int $parent_id Parent id. 1556 * @param string $post_type Post type. Defaults to 'post'. 1557 * @uses bbp_get_public_status_id() To get the public status id 1555 1558 * @uses bbp_get_topic_post_type() To get the topic post type 1556 * @uses wp_cache_get() To check if there is a cache of the children count 1557 * @uses wpdb::prepare() To prepare the query 1558 * @uses wpdb::get_var() To get the result of the query in a variable 1559 * @uses wp_cache_set() To set the cache for future use 1559 * @uses bbp_get_closed_status_id() To get the closed status id 1560 * @uses WP_Query To get get the posts 1560 1561 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child 1561 1562 * count, parent id and post type … … 1569 1570 } 1570 1571 1571 // The ID of the cached query 1572 $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count'; 1573 1574 // Check for cache and set if needed 1575 $child_count = wp_cache_get( $cache_id, 'bbpress_posts' ); 1576 if ( false === $child_count ) { 1577 $post_status = array( bbp_get_public_status_id() ); 1578 1579 // Add closed status if topic post type 1580 if ( $post_type === bbp_get_topic_post_type() ) { 1581 $post_status[] = bbp_get_closed_status_id(); 1582 } 1583 1584 // Join post statuses together 1585 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1586 $bbp_db = bbp_db(); 1587 $query = $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type ); 1588 $child_count = (int) $bbp_db->get_var( $query ); 1589 1590 wp_cache_set( $cache_id, $child_count, 'bbpress_posts' ); 1591 } else { 1592 $child_count = (int) $child_count; 1593 } 1572 // Check the public post status 1573 $post_status = array( bbp_get_public_status_id() ); 1574 1575 // Add closed status if topic post type 1576 if ( bbp_get_topic_post_type() === $post_type ) { 1577 $post_status[] = bbp_get_closed_status_id(); 1578 } 1579 1580 $query = new WP_Query( array( 1581 'fields' => 'ids', 1582 'post_parent' => $parent_id, 1583 'post_status' => $post_status, 1584 'post_type' => $post_type, 1585 1586 // Maybe change these later 1587 'posts_per_page' => -1, 1588 'update_post_term_cache' => false, 1589 'update_post_meta_cache' => false, 1590 'ignore_sticky_posts' => true, 1591 ) ); 1592 $child_count = $query->post_count; 1593 unset( $query ); 1594 1594 1595 1595 // Filter and return … … 1598 1598 1599 1599 /** 1600 * Query the DB and get athe child id's of public children1600 * Query the DB and get the child id's of public children 1601 1601 * 1602 1602 * @since 2.0.0 bbPress (r2868) 1603 * 1604 * @param int $parent_id Parent id 1605 * @param string $post_type Post type. Defaults to 'post' 1603 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects 1604 * 1605 * @param int $parent_id Parent id. 1606 * @param string $post_type Post type. Defaults to 'post'. 1607 * @uses bbp_get_public_status_id() To get the public status id 1606 1608 * @uses bbp_get_topic_post_type() To get the topic post type 1607 * @uses wp_cache_get() To check if there is a cache of the children1608 * @uses bbp_get_public_status_id() To get the public status id1609 1609 * @uses bbp_get_closed_status_id() To get the closed status id 1610 * @uses wpdb::prepare() To prepare the query 1611 * @uses wpdb::get_col() To get the result of the query in an array 1612 * @uses wp_cache_set() To set the cache for future use 1610 * @uses WP_Query To get get the posts 1613 1611 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, 1614 1612 * parent id and post type … … 1622 1620 } 1623 1621 1624 // The ID of the cached query 1625 $cache_id = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids'; 1626 1627 // Check for cache and set if needed 1628 $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' ); 1629 if ( false === $child_ids ) { 1630 $post_status = array( bbp_get_public_status_id() ); 1631 1632 // Add closed status if topic post type 1633 if ( $post_type === bbp_get_topic_post_type() ) { 1634 $post_status[] = bbp_get_closed_status_id(); 1635 } 1636 1637 // Join post statuses together 1638 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1639 $bbp_db = bbp_db(); 1640 $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ); 1641 $child_ids = (array) $bbp_db->get_col( $query ); 1642 1643 wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' ); 1644 } else { 1645 $child_ids = (array) $child_ids; 1646 } 1622 // Get the public post status 1623 $post_status = array( bbp_get_public_status_id() ); 1624 1625 // Add closed status if topic post type 1626 if ( bbp_get_topic_post_type() === $post_type ) { 1627 $post_status[] = bbp_get_closed_status_id(); 1628 } 1629 1630 $query = new WP_Query( array( 1631 'fields' => 'ids', 1632 'post_parent' => $parent_id, 1633 'post_status' => $post_status, 1634 'post_type' => $post_type, 1635 1636 // Maybe change these later 1637 'posts_per_page' => -1, 1638 'update_post_term_cache' => false, 1639 'update_post_meta_cache' => false, 1640 'ignore_sticky_posts' => true, 1641 ) ); 1642 $child_ids = ! empty( $query->posts ) ? $query->posts : array(); 1643 unset( $query ); 1647 1644 1648 1645 // Filter and return
Note: See TracChangeset
for help on using the changeset viewer.