Skip to:
Content

bbPress.org

Changeset 3669


Ignore:
Timestamp:
01/17/2012 01:40:46 AM (10 years ago)
Author:
johnjamesjacoby
Message:

First pass at forum trash/untrash/delete actions and functions for BuddyPress group forums. See #1669.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-forum-functions.php

    r3654 r3669  
    17081708}
    17091709
     1710/**
     1711 * Trash all topics inside a forum
     1712 *
     1713 * @since bbPress (r3668)
     1714 *
     1715 * @param int $forum_id
     1716 * @uses bbp_get_forum_id() To validate the forum ID
     1717 * @uses bbp_is_forum() To make sure it's a forum
     1718 * @uses bbp_get_topic_post_type() To get the topic post type
     1719 * @uses bbp_has_topics() To get the topics
     1720 * @uses bbp_topics() To make sure there are topics to loop through
     1721 * @uses wp_trash_post() To trash the post
     1722 * @uses update_post_meta() To update the forum meta of trashed topics
     1723 * @return If forum is not valid
     1724 */
     1725function bbp_delete_forum_topics( $forum_id = 0 ) {
     1726
     1727    // Validate forum ID
     1728    $forum_id = bbp_get_forum_id( $forum_id );
     1729
     1730    if ( empty( $forum_id ) )
     1731        return;
     1732
     1733    global $bbp;
     1734
     1735    // Forum is being permanently deleted, so its topics gotta go too
     1736    if ( bbp_has_topics( array(
     1737        'post_type'      => bbp_get_topic_post_type(),
     1738        'post_status'    => 'any',
     1739        'posts_per_page' => -1,
     1740        'meta_query'     => array( array(
     1741            'key'        => '_bbp_forum_id',
     1742            'value'      => $forum_id,
     1743            'compare'    => '='
     1744        ) )
     1745    ) ) ) {
     1746        while ( bbp_topics() ) {
     1747            bbp_the_topic();
     1748            wp_delete_post( $bbp->topic_query->post->ID, true );
     1749        }
     1750    }
     1751}
     1752
     1753/**
     1754 * Trash all topics inside a forum
     1755 *
     1756 * @since bbPress (r3668)
     1757 *
     1758 * @global bbPress $bbp
     1759 * @param int $forum_id
     1760 * @uses bbp_get_forum_id() To validate the forum ID
     1761 * @uses bbp_is_forum() To make sure it's a forum
     1762 * @uses bbp_get_public_status_id() To return public post status
     1763 * @uses bbp_get_closed_status_id() To return closed post status
     1764 * @uses bbp_get_pending_status_id() To return pending post status
     1765 * @uses bbp_get_topic_post_type() To get the topic post type
     1766 * @uses bbp_has_topics() To get the topics
     1767 * @uses bbp_topics() To make sure there are topics to loop through
     1768 * @uses wp_trash_post() To trash the post
     1769 * @uses update_post_meta() To update the forum meta of trashed topics
     1770 * @return If forum is not valid
     1771 */
     1772function bbp_trash_forum_topics( $forum_id = 0 ) {
     1773
     1774    // Validate forum ID
     1775    $forum_id = bbp_get_forum_id( $forum_id );
     1776
     1777    if ( empty( $forum_id ) )
     1778        return;
     1779
     1780    global $bbp;
     1781
     1782    // Allowed post statuses to pre-trash
     1783    $post_stati = join( ',', array(
     1784        bbp_get_public_status_id(),
     1785        bbp_get_closed_status_id(),
     1786        bbp_get_pending_status_id()
     1787    ) );
     1788
     1789    // Forum is being trashed, so its topics are trashed too
     1790    if ( bbp_has_topics( array(
     1791        'post_type'      => bbp_get_topic_post_type(),
     1792        'post_status'    => $post_stati,
     1793        'posts_per_page' => -1,
     1794        'meta_query'     => array( array(
     1795            'key'        => '_bbp_forum_id',
     1796            'value'      => $forum_id,
     1797            'compare'    => '='
     1798        ) )
     1799    ) ) ) {
     1800
     1801        // Prevent debug notices
     1802        $pre_trashed_topics = array();
     1803
     1804        // Loop through topics, trash them, and add them to array
     1805        while ( bbp_topics() ) {
     1806            bbp_the_topic();
     1807            wp_trash_post( $bbp->topic_query->post->ID );
     1808            $pre_trashed_topics[] = $bbp->topic_query->post->ID;
     1809        }
     1810
     1811        // Set a post_meta entry of the topics that were trashed by this action.
     1812        // This is so we can possibly untrash them, without untrashing topics
     1813        // that were purposefully trashed before.
     1814        update_post_meta( $forum_id, '_bbp_pre_trashed_topics', $pre_trashed_topics );
     1815    }
     1816}
     1817
     1818/**
     1819 * Trash all topics inside a forum
     1820 *
     1821 * @since bbPress (r3668)
     1822 *
     1823 * @global bbPress $bbp
     1824 * @param int $forum_id
     1825 * @uses bbp_get_forum_id() To validate the forum ID
     1826 * @uses bbp_is_forum() To make sure it's a forum
     1827 * @uses get_post_meta() To update the forum meta of trashed topics
     1828 * @uses wp_untrash_post() To trash the post
     1829 * @return If forum is not valid
     1830 */
     1831function bbp_untrash_forum_topics( $forum_id = 0 ) {
     1832
     1833    // Validate forum ID
     1834    $forum_id = bbp_get_forum_id( $forum_id );
     1835
     1836    if ( empty( $forum_id ) )
     1837        return;
     1838
     1839    // Get the topics that were not previously trashed
     1840    $pre_trashed_topics = get_post_meta( $forum_id, '_bbp_pre_trashed_topics', true );
     1841
     1842    // There are topics to untrash
     1843    if ( !empty( $pre_trashed_topics ) ) {
     1844
     1845        // Maybe reverse the trashed topics array
     1846        if ( is_array( $pre_trashed_topics ) )
     1847            $pre_trashed_topics = array_reverse( $pre_trashed_topics );
     1848
     1849        // Loop through topics
     1850        foreach ( (array) $pre_trashed_topics as $topic ) {
     1851            wp_untrash_post( $topic );
     1852        }
     1853    }
     1854}
     1855
     1856/** Before Delete/Trash/Untrash ***********************************************/
     1857
     1858/**
     1859 * Called before deleting a forum.
     1860 *
     1861 * This function is supplemental to the actual forum deletion which is
     1862 * handled by WordPress core API functions. It is used to clean up after
     1863 * a forum that is being deleted.
     1864 *
     1865 * @since bbPress (r3668)
     1866 * @uses bbp_get_forum_id() To get the forum id
     1867 * @uses bbp_is_forum() To check if the passed id is a forum
     1868 * @uses do_action() Calls 'bbp_delete_forum' with the forum id
     1869 * @uses bbp_has_topics() To check if the forum has topics
     1870 * @uses bbp_topics() To loop through the topics
     1871 * @uses bbp_the_topic() To set a topic as the current topic in the loop
     1872 * @uses bbp_get_topic_id() To get the topic id
     1873 * @uses wp_delete_post() To delete the topic
     1874 */
     1875function bbp_delete_forum( $forum_id = 0 ) {
     1876    $forum_id = bbp_get_forum_id( $forum_id );
     1877
     1878    if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
     1879        return false;
     1880
     1881    do_action( 'bbp_delete_forum', $forum_id );
     1882}
     1883
     1884/**
     1885 * Called before trashing a forum
     1886 *
     1887 * This function is supplemental to the actual forum being trashed which is
     1888 * handled by WordPress core API functions. It is used to clean up after
     1889 * a forum that is being trashed.
     1890 *
     1891 * @since bbPress (r3668)
     1892 * @uses bbp_get_forum_id() To get the forum id
     1893 * @uses bbp_is_forum() To check if the passed id is a forum
     1894 * @uses do_action() Calls 'bbp_trash_forum' with the forum id
     1895 * @uses bbp_has_topics() To check if the forum has topics
     1896 * @uses bbp_topics() To loop through the topics
     1897 * @uses bbp_the_topic() To set a topic as the current topic in the loop
     1898 * @uses bbp_get_topic_id() To get the topic id
     1899 * @uses wp_trash_post() To trash the topic
     1900 * @uses update_post_meta() To save a list of just trashed topics for future use
     1901 */
     1902function bbp_trash_forum( $forum_id = 0 ) {
     1903    $forum_id = bbp_get_forum_id( $forum_id );
     1904
     1905    if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
     1906        return false;
     1907
     1908    do_action( 'bbp_trash_forum', $forum_id );
     1909}
     1910
     1911/**
     1912 * Called before untrashing a forum
     1913 *
     1914 * @since bbPress (r3668)
     1915 * @uses bbp_get_forum_id() To get the forum id
     1916 * @uses bbp_is_forum() To check if the passed id is a forum
     1917 * @uses do_action() Calls 'bbp_untrash_forum' with the forum id
     1918 * @uses get_post_meta() To get the list of topics which were trashed with the
     1919 *                        forum
     1920 * @uses wp_untrash_post() To untrash the topic
     1921 */
     1922function bbp_untrash_forum( $forum_id = 0 ) {
     1923    $forum_id = bbp_get_forum_id( $forum_id );
     1924
     1925    if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
     1926        return false;
     1927
     1928    do_action( 'bbp_untrash_forum', $forum_id );
     1929}
     1930
     1931/** After Delete/Trash/Untrash ************************************************/
     1932
     1933/**
     1934 * Called after deleting a forum
     1935 *
     1936 * @since bbPress (r3668)
     1937 * @uses bbp_get_forum_id() To get the forum id
     1938 * @uses bbp_is_forum() To check if the passed id is a forum
     1939 * @uses do_action() Calls 'bbp_deleted_forum' with the forum id
     1940 */
     1941function bbp_deleted_forum( $forum_id = 0 ) {
     1942    $forum_id = bbp_get_forum_id( $forum_id );
     1943
     1944    if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
     1945        return false;
     1946
     1947    do_action( 'bbp_deleted_forum', $forum_id );
     1948}
     1949
     1950/**
     1951 * Called after trashing a forum
     1952 *
     1953 * @since bbPress (r3668)
     1954 * @uses bbp_get_forum_id() To get the forum id
     1955 * @uses bbp_is_forum() To check if the passed id is a forum
     1956 * @uses do_action() Calls 'bbp_trashed_forum' with the forum id
     1957 */
     1958function bbp_trashed_forum( $forum_id = 0 ) {
     1959    $forum_id = bbp_get_forum_id( $forum_id );
     1960
     1961    if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
     1962        return false;
     1963
     1964    do_action( 'bbp_trashed_forum', $forum_id );
     1965}
     1966
     1967/**
     1968 * Called after untrashing a forum
     1969 *
     1970 * @since bbPress (r3668)
     1971 * @uses bbp_get_forum_id() To get the forum id
     1972 * @uses bbp_is_forum() To check if the passed id is a forum
     1973 * @uses do_action() Calls 'bbp_untrashed_forum' with the forum id
     1974 */
     1975function bbp_untrashed_forum( $forum_id = 0 ) {
     1976    $forum_id = bbp_get_forum_id( $forum_id );
     1977
     1978    if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
     1979        return false;
     1980
     1981    do_action( 'bbp_untrashed_forum', $forum_id );
     1982}
     1983
    17101984?>
Note: See TracChangeset for help on using the changeset viewer.