Skip to:
Content

bbPress.org

Changeset 5499


Ignore:
Timestamp:
09/11/2014 02:19:19 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Enable forum status and visibility functions to check ancestry by extracting existing logic into new helper functions. Props alex-ye, netweb. Fixes #2303.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/forums/template.php

    r5474 r5499  
    1717 *
    1818 * @since bbPress (r2857)
     19 *
    1920 * @uses bbp_get_forum_post_type() To get the forum post type
    2021 */
     
    455456 *
    456457 * @since bbPress (r3653)
     458 *
    457459 * @uses do_action()
    458460 * @todo Links and filter
     
    15751577 *
    15761578 * @since bbPress (r2746)
    1577  * @param int $forum_id Optional. Forum id
    1578  *
    1579  * @param int $forum_id Optional. Forum id
    1580  * @uses bbp_is_forum_closed() To check if the forum is closed or not
     1579 *
     1580 * @param int $forum_id Optional. Forum id
     1581 * @param bool $check_ancestors Check if the ancestors are open (only
     1582 *                               if they're a category)
     1583 * @uses bbp_is_forum_closed() To check if the forum is closed
    15811584 * @return bool Whether the forum is open or not
    15821585 */
    1583 function bbp_is_forum_open( $forum_id = 0 ) {
    1584     return !bbp_is_forum_closed( $forum_id );
    1585 }
    1586 
    1587     /**
    1588      * Is the forum closed?
    1589      *
    1590      * @since bbPress (r2746)
    1591      *
    1592      * @param int $forum_id Optional. Forum id
    1593      * @param bool $check_ancestors Check if the ancestors are closed (only
    1594      *                               if they're a category)
    1595      * @uses bbp_get_forum_status() To get the forum status
    1596      * @uses bbp_get_forum_ancestors() To get the forum ancestors
    1597      * @uses bbp_is_forum_category() To check if the forum is a category
    1598      * @uses bbp_is_forum_closed() To check if the forum is closed
    1599      * @return bool True if closed, false if not
    1600      */
    1601     function bbp_is_forum_closed( $forum_id = 0, $check_ancestors = true ) {
    1602 
    1603         $forum_id = bbp_get_forum_id( $forum_id );
    1604         $retval    = ( bbp_get_closed_status_id() === bbp_get_forum_status( $forum_id ) );
    1605 
    1606         if ( !empty( $check_ancestors ) ) {
    1607             $ancestors = bbp_get_forum_ancestors( $forum_id );
    1608 
    1609             foreach ( (array) $ancestors as $ancestor ) {
    1610                 if ( bbp_is_forum_category( $ancestor, false ) && bbp_is_forum_closed( $ancestor, false ) ) {
    1611                     $retval = true;
     1586function bbp_is_forum_open( $forum_id = 0, $check_ancestors = true ) {
     1587    return !bbp_is_forum_closed( $forum_id, $check_ancestors );
     1588}
     1589
     1590/**
     1591* Is the forum closed?
     1592 *
     1593 * @since bbPress (r2746)
     1594 *
     1595 * @param int $forum_id Optional. Forum id
     1596 * @param bool $check_ancestors Check if the ancestors are closed (only
     1597 *                               if they're a category)
     1598 * @uses bbp_get_forum_id() To get the forum ID
     1599 * @uses bbp_is_forum_status() To check the forum status
     1600 * @return bool True if closed, false if not
     1601 */
     1602function bbp_is_forum_closed( $forum_id = 0, $check_ancestors = true ) {
     1603
     1604    // Get the forum ID
     1605    $forum_id = bbp_get_forum_id( $forum_id );
     1606
     1607    // Check if the forum or one of it's ancestors is closed
     1608    $retval   = bbp_is_forum_status( $forum_id, bbp_get_closed_status_id(), $check_ancestors, 'OR' );
     1609
     1610    return (bool) apply_filters( 'bbp_is_forum_closed', (bool) $retval, $forum_id, $check_ancestors );
     1611}
     1612
     1613/**
     1614 * Check if the forum status is a specific one, also maybe checking ancestors
     1615 *
     1616 * @since bbPress (r5499)
     1617 *
     1618 * @param bool $status_name The forum status name to check
     1619 * @param bool $check_ancestors Check the forum ancestors
     1620 * @param string $operator The logical operation to perform.
     1621 *      'OR' means only one forum from the tree needs to match;
     1622 *      'AND' means all forums must match. The default is 'AND'.
     1623 * @uses bbp_get_forum_id() To get the forum ID
     1624 * @uses bbp_get_forum_status() To get the forum status
     1625 * @uses bbp_get_forum_ancestors() To get the forum ancestors
     1626 * @uses bbp_is_forum_category() To check the forum type
     1627 * @return bool True if match, false if not
     1628 */
     1629function bbp_is_forum_status( $forum_id, $status_name, $check_ancestors = true, $operator = 'AND' ) {
     1630
     1631    // Setup some default variables
     1632    $count        = 0;
     1633    $retval       = false;
     1634    $operator     = strtoupper( $operator );
     1635    $forum_id     = bbp_get_forum_id( $forum_id );
     1636    $forum_status = bbp_get_forum_status( $forum_id );
     1637
     1638    // Quickly compare statuses of first forum ID
     1639    if ( $status_name === $forum_status ) {
     1640        $retval = true;
     1641        $count++;
     1642    }
     1643
     1644    // Let's check the forum's ancestors too
     1645    if ( ! empty( $check_ancestors ) ) {
     1646
     1647        // Adjust the ancestor check based on the count
     1648        switch( $operator ) {
     1649            default:
     1650            case 'AND':
     1651                $check_ancestors = ( $count > 0 );
     1652                break;
     1653
     1654            case 'OR':
     1655                $check_ancestors = ( $count < 1 );
     1656                break;
     1657        }
     1658
     1659        // Ancestor check passed, so continue looping through them
     1660        if ( ! empty( $check_ancestors ) ) {
     1661
     1662            // Loop through the forum ancestors
     1663            foreach ( (array) bbp_get_forum_ancestors( $forum_id ) as $ancestor ) {
     1664
     1665                // Check if the forum is a category
     1666                if ( bbp_is_forum_category( $ancestor ) ) {
     1667
     1668                    // Check the ancestor forum status
     1669                    $retval = bbp_is_forum_status( $ancestor, $status_name, false );
     1670                    if ( true === $retval ) {
     1671                        $count++;
     1672                    }
     1673                }
     1674
     1675                // Break when it reach the max count
     1676                if ( ( $operator === 'OR' ) && ( $count >= 1 ) ) {
     1677                    break;
    16121678                }
    16131679            }
    16141680        }
    1615 
    1616         return (bool) apply_filters( 'bbp_is_forum_closed', (bool) $retval, $forum_id, $check_ancestors );
    1617     }
     1681    }
     1682
     1683    // Filter and return
     1684    return (bool) apply_filters( 'bbp_is_forum_status', $retval, $count, $forum_id, $status_name, $check_ancestors, $operator );
     1685}
    16181686
    16191687/**
     
    16231691 *
    16241692 * @param int $forum_id Optional. Forum id
    1625  * @param bool $check_ancestors Check if the ancestors are public (only if
     1693 * @param bool $check_ancestors Check if the ancestors are public
     1694 * @uses bbp_get_forum_id() To get the forum ID
     1695 * @uses bbp_is_forum_visibility() To check the forum visibility ID
     1696 * @return bool True if closed, false if not
     1697 */
     1698function bbp_is_forum_public( $forum_id = 0, $check_ancestors = true ) {
     1699
     1700    // Get the forum ID
     1701    $forum_id = bbp_get_forum_id( $forum_id );
     1702
     1703    // Check if the forum and all of it's ancestors are public
     1704    $retval   = bbp_is_forum_visibility( $forum_id, bbp_get_public_status_id(), $check_ancestors );
     1705
     1706    return (bool) apply_filters( 'bbp_is_forum_public', (bool) $retval, $forum_id, $check_ancestors );
     1707}
     1708
     1709/**
     1710 * Is the forum private?
     1711 *
     1712 * @since bbPress (r2746)
     1713 *
     1714 * @param int $forum_id Optional. Forum id
     1715 * @param bool $check_ancestors Check if the ancestors are private
     1716 * @uses bbp_get_forum_id() To get the forum ID
     1717 * @uses bbp_is_forum_visibility() To check the forum visibility ID
     1718 * @return bool True if private, false if not
     1719 */
     1720function bbp_is_forum_private( $forum_id = 0, $check_ancestors = true ) {
     1721
     1722    // Get the forum ID
     1723    $forum_id = bbp_get_forum_id( $forum_id );
     1724
     1725    // Check if the forum or one of it's ancestors is private
     1726    $retval   = bbp_is_forum_visibility( $forum_id, bbp_get_private_status_id(), $check_ancestors, 'OR' );
     1727
     1728    return (bool) apply_filters( 'bbp_is_forum_private', (bool) $retval, $forum_id, $check_ancestors );
     1729}
     1730
     1731/**
     1732 * Is the forum hidden?
     1733 *
     1734 * @since bbPress (r2997)
     1735 *
     1736 * @param int $forum_id Optional. Forum id
     1737 * @param bool $check_ancestors Check if the ancestors are private (only if
    16261738 *                               they're a category)
    1627  * @uses get_post_meta() To get the forum public meta
     1739 * @uses bbp_get_forum_id() To get the forum ID
     1740 * @uses bbp_is_forum_visibility() To check the forum visibility ID
     1741 * @return bool True if hidden, false if not
     1742 */
     1743function bbp_is_forum_hidden( $forum_id = 0, $check_ancestors = true ) {
     1744
     1745    // Get the forum ID
     1746    $forum_id = bbp_get_forum_id( $forum_id );
     1747
     1748    // Check if the forum or one of it's ancestors is hidden
     1749    $retval   = bbp_is_forum_visibility( $forum_id, bbp_get_hidden_status_id(), $check_ancestors, 'OR' );
     1750
     1751    return (bool) apply_filters( 'bbp_is_forum_hidden', (bool) $retval, $forum_id, $check_ancestors );
     1752}
     1753
     1754/**
     1755 * Check the forum visibility ID
     1756 *
     1757 * @since bbPress (rX)
     1758 *
     1759 * @param int $forum_id Optional. Forum id
     1760 * @param bool $status_name The post status name to check
     1761 * @param bool $check_ancestors Check the forum ancestors
     1762 * @param string $operator The logical operation to perform.
     1763 *      'OR' means only one forum from the tree needs to match;
     1764 *      'AND' means all forums must match. The default is 'AND'.
     1765 * @uses bbp_get_forum_id() To get the forum ID
     1766 * @uses bbp_get_forum_visibility() To get the forum visibility
    16281767 * @uses bbp_get_forum_ancestors() To get the forum ancestors
    1629  * @uses bbp_is_forum_category() To check if the forum is a category
    1630  * @uses bbp_is_forum_closed() To check if the forum is closed
    1631  * @return bool True if closed, false if not
    1632  */
    1633 function bbp_is_forum_public( $forum_id = 0, $check_ancestors = true ) {
    1634 
     1768 * @uses bbp_is_forum() To check the post type
     1769 * @return bool True if match, false if not
     1770 */
     1771function bbp_is_forum_visibility( $forum_id, $status_name, $check_ancestors = true, $operator = 'AND' ) {
     1772
     1773    // Setup some default variables
     1774    $count      = 0;
     1775    $retval     = false;
     1776    $operator   = strtoupper( $operator );
    16351777    $forum_id   = bbp_get_forum_id( $forum_id );
    16361778    $visibility = bbp_get_forum_visibility( $forum_id );
    16371779
    1638     // If post status is public, return true
    1639     $retval = ( bbp_get_public_status_id() === $visibility );
    1640 
    1641     // Check ancestors and inherit their privacy setting for display
    1642     if ( !empty( $check_ancestors ) ) {
    1643         $ancestors = bbp_get_forum_ancestors( $forum_id );
    1644 
    1645         foreach ( (array) $ancestors as $ancestor ) {
    1646             if ( bbp_is_forum( $ancestor ) && bbp_is_forum_public( $ancestor, false ) ) {
    1647                 $retval = true;
     1780    // Quickly compare visibility of first forum ID
     1781    if ( $status_name === $visibility ){
     1782        $retval = true;
     1783        $count++;
     1784    }
     1785
     1786    // Let's check the forum's ancestors too
     1787    if ( ! empty( $check_ancestors ) ) {
     1788
     1789        // Adjust the ancestor check based on the count
     1790        switch( $operator ) {
     1791
     1792            // Adjust the ancestor check based on the count
     1793            default:
     1794            case 'AND':
     1795                $check_ancestors = ( $count > 0 );
     1796                break;
     1797
     1798            case 'OR':
     1799                $check_ancestors = ( $count < 1 );
     1800                break;
     1801        }
     1802
     1803        // Ancestor check passed, so continue looping through them
     1804        if ( ! empty( $check_ancestors ) ) {
     1805
     1806            // Loop through the forum ancestors
     1807            foreach ( (array) bbp_get_forum_ancestors( $forum_id ) as $ancestor ) {
     1808
     1809                // Check if the forum is not a category
     1810                if ( bbp_is_forum( $ancestor ) ) {
     1811
     1812                    // Check the forum visibility
     1813                    $retval = bbp_is_forum_visibility( $ancestor, $status_name, false );
     1814                    if ( true === $retval ) {
     1815                        $count++;
     1816                    }
     1817                }
     1818
     1819                // Break when it reach the max count
     1820                if ( ( $operator === 'OR' ) && ( $count >= 1 ) ) {
     1821                    break;
     1822                }
    16481823            }
    16491824        }
    16501825    }
    16511826
    1652     return (bool) apply_filters( 'bbp_is_forum_public', (bool) $retval, $forum_id, $check_ancestors );
    1653 }
    1654 
    1655 /**
    1656  * Is the forum private?
    1657  *
    1658  * @since bbPress (r2746)
    1659  *
    1660  * @param int $forum_id Optional. Forum id
    1661  * @param bool $check_ancestors Check if the ancestors are private (only if
    1662  *                               they're a category)
    1663  * @uses get_post_meta() To get the forum private meta
    1664  * @uses bbp_get_forum_ancestors() To get the forum ancestors
    1665  * @uses bbp_is_forum_category() To check if the forum is a category
    1666  * @uses bbp_is_forum_closed() To check if the forum is closed
    1667  * @return bool True if closed, false if not
    1668  */
    1669 function bbp_is_forum_private( $forum_id = 0, $check_ancestors = true ) {
    1670 
    1671     $forum_id   = bbp_get_forum_id( $forum_id );
    1672     $visibility = bbp_get_forum_visibility( $forum_id );
    1673 
    1674     // If post status is private, return true
    1675     $retval = ( bbp_get_private_status_id() === $visibility );
    1676 
    1677     // Check ancestors and inherit their privacy setting for display
    1678     if ( !empty( $check_ancestors ) ) {
    1679         $ancestors = bbp_get_forum_ancestors( $forum_id );
    1680 
    1681         foreach ( (array) $ancestors as $ancestor ) {
    1682             if ( bbp_is_forum( $ancestor ) && bbp_is_forum_private( $ancestor, false ) ) {
    1683                 $retval = true;
    1684             }
    1685         }
    1686     }
    1687 
    1688     return (bool) apply_filters( 'bbp_is_forum_private', (bool) $retval, $forum_id, $check_ancestors );
    1689 }
    1690 
    1691 /**
    1692  * Is the forum hidden?
    1693  *
    1694  * @since bbPress (r2997)
    1695  *
    1696  * @param int $forum_id Optional. Forum id
    1697  * @param bool $check_ancestors Check if the ancestors are private (only if
    1698  *                               they're a category)
    1699  * @uses get_post_meta() To get the forum private meta
    1700  * @uses bbp_get_forum_ancestors() To get the forum ancestors
    1701  * @uses bbp_is_forum_category() To check if the forum is a category
    1702  * @uses bbp_is_forum_closed() To check if the forum is closed
    1703  * @return bool True if closed, false if not
    1704  */
    1705 function bbp_is_forum_hidden( $forum_id = 0, $check_ancestors = true ) {
    1706 
    1707     $forum_id   = bbp_get_forum_id( $forum_id );
    1708     $visibility = bbp_get_forum_visibility( $forum_id );
    1709 
    1710     // If post status is private, return true
    1711     $retval = ( bbp_get_hidden_status_id() === $visibility );
    1712 
    1713     // Check ancestors and inherit their privacy setting for display
    1714     if ( !empty( $check_ancestors ) ) {
    1715         $ancestors = bbp_get_forum_ancestors( $forum_id );
    1716 
    1717         foreach ( (array) $ancestors as $ancestor ) {
    1718             if ( bbp_is_forum( $ancestor ) && bbp_is_forum_hidden( $ancestor, false ) ) {
    1719                 $retval = true;
    1720             }
    1721         }
    1722     }
    1723 
    1724     return (bool) apply_filters( 'bbp_is_forum_hidden', (bool) $retval, $forum_id, $check_ancestors );
     1827    // Filter and return
     1828    return (bool) apply_filters( 'bbp_is_forum_visibility', $retval, $count, $forum_id, $status_name, $check_ancestors, $operator );
    17251829}
    17261830
     
    22462350        return apply_filters( 'bbp_get_form_forum_visibility', esc_attr( $forum_visibility ) );
    22472351    }
    2248    
     2352
    22492353/**
    22502354 * Output checked value of forum subscription
Note: See TracChangeset for help on using the changeset viewer.