Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/15/2016 02:21:54 AM (9 years ago)
Author:
boonebgorges
Message:

Introduce Group Types API.

Modeled on the Member Types API, Group Types allow developers to register
arbitrary group categorizations, and assign one or more of these types to a
given group. BuddyPress stores this information in a custom taxonomy on the
root blog.

Group queries can be filtered by group type, by using the group_type,
group_type__in, and group_type__not_in parameters in the bp_has_groups()
template loop function stack.

Props Mamaduka, boonebgorges, dcavins.
See #6784.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-functions.php

    r10687 r10766  
    684684 *
    685685 * @since 1.2.0
     686 * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
    686687 *
    687688 * @param array|string $args {
     
    704705        'exclude'           => false,          // Do not include these specific groups (group_ids).
    705706        'search_terms'      => false,          // Limit to groups that match these search terms.
     707        'group_type'         => '',
     708        'group_type__in'     => '',
     709        'group_type__not_in' => '',
    706710        'meta_query'        => false,          // Filter by groupmeta. See WP_Meta_Query for syntax.
    707711        'show_hidden'       => false,          // Show hidden groups to non-admins.
     
    720724        'exclude'           => $r['exclude'],
    721725        'search_terms'      => $r['search_terms'],
     726        'group_type'         => $r['group_type'],
     727        'group_type__in'     => $r['group_type__in'],
     728        'group_type__not_in' => $r['group_type__not_in'],
    722729        'meta_query'        => $r['meta_query'],
    723730        'show_hidden'       => $r['show_hidden'],
     
    18511858add_action( 'delete_user',       'groups_remove_data_for_user' );
    18521859add_action( 'bp_make_spam_user', 'groups_remove_data_for_user' );
     1860
     1861/** Group Types ***************************************************************/
     1862
     1863/**
     1864 * Register a group type.
     1865 *
     1866 * @since 2.6.0
     1867 *
     1868 * @param string $group_type Unique string identifier for the group type.
     1869 * @param array  $args {
     1870 *     Array of arguments describing the group type.
     1871 *
     1872 *     @type array $labels {
     1873 *         Array of labels to use in various parts of the interface.
     1874 *
     1875 *         @type string $name          Default name. Should typically be plural.
     1876 *         @type string $singular_name Singular name.
     1877 *     }
     1878 * }
     1879 * @return object|WP_Error Group type object on success, WP_Error object on failure.
     1880 */
     1881function bp_groups_register_group_type( $group_type, $args = array() ) {
     1882    $bp = buddypress();
     1883
     1884    if ( isset( $bp->groups->types[ $group_type ] ) ) {
     1885        return new WP_Error( 'bp_group_type_exists', __( 'Group type already exists.', 'buddypress' ), $group_type );
     1886    }
     1887
     1888    $r = bp_parse_args( $args, array(
     1889        'labels'        => array(),
     1890    ), 'register_group_type' );
     1891
     1892    $group_type = sanitize_key( $group_type );
     1893
     1894    /**
     1895     * Filters the list of illegal group type names.
     1896     *
     1897     * - 'any' is a special pseudo-type, representing items unassociated with any group type.
     1898     * - 'null' is a special pseudo-type, representing users without any type.
     1899     * - '_none' is used internally to denote an item that should not apply to any group types.
     1900     *
     1901     * @since 2.6.0
     1902     *
     1903     * @param array $illegal_names Array of illegal names.
     1904     */
     1905    $illegal_names = apply_filters( 'bp_group_type_illegal_names', array( 'any', 'null', '_none' ) );
     1906    if ( in_array( $group_type, $illegal_names, true ) ) {
     1907        return new WP_Error( 'bp_group_type_illegal_name', __( 'You may not register a group type with this name.', 'buddypress' ), $group_type );
     1908    }
     1909
     1910    // Store the group type name as data in the object (not just as the array key).
     1911    $r['name'] = $group_type;
     1912
     1913    // Make sure the relevant labels have been filled in.
     1914    $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] );
     1915    $r['labels'] = array_merge( array(
     1916        'name'          => $default_name,
     1917        'singular_name' => $default_name,
     1918    ), $r['labels'] );
     1919
     1920    $bp->groups->types[ $group_type ] = $type = (object) $r;
     1921
     1922    /**
     1923     * Fires after a group type is registered.
     1924     *
     1925     * @since 2.6.0
     1926     *
     1927     * @param string $group_type Group type identifier.
     1928     * @param object $type       Group type object.
     1929     */
     1930    do_action( 'bp_groups_register_group_type', $group_type, $type );
     1931
     1932    return $type;
     1933}
     1934
     1935/**
     1936 * Get a list of all registered group type objects.
     1937 *
     1938 * @since 2.6.0
     1939 *
     1940 * @see bp_groups_register_group_type() for accepted arguments.
     1941 *
     1942 * @param array|string $args     Optional. An array of key => value arguments to match against
     1943 *                               the group type objects. Default empty array.
     1944 * @param string       $output   Optional. The type of output to return. Accepts 'names'
     1945 *                               or 'objects'. Default 'names'.
     1946 * @param string       $operator Optional. The logical operation to perform. 'or' means only one
     1947 *                               element from the array needs to match; 'and' means all elements
     1948 *                               must match. Accepts 'or' or 'and'. Default 'and'.
     1949 * @return array       $types    A list of groups type names or objects.
     1950 */
     1951function bp_groups_get_group_types( $args = array(), $output = 'names', $operator = 'and' ) {
     1952    $types = buddypress()->groups->types;
     1953
     1954    $types = wp_filter_object_list( $types, $args, $operator );
     1955
     1956    /**
     1957     * Filters the array of group type objects.
     1958     *
     1959     * This filter is run before the $output filter has been applied, so that
     1960     * filtering functions have access to the entire group type objects.
     1961     *
     1962     * @since 2.6.0
     1963     *
     1964     * @param array  $types     group type objects, keyed by name.
     1965     * @param array  $args      Array of key=>value arguments for filtering.
     1966     * @param string $operator  'or' to match any of $args, 'and' to require all.
     1967     */
     1968    $types = apply_filters( 'bp_groups_get_group_types', $types, $args, $operator );
     1969
     1970    if ( 'names' === $output ) {
     1971        $types = wp_list_pluck( $types, 'name' );
     1972    }
     1973
     1974    return $types;
     1975}
     1976
     1977/**
     1978 * Retrieve a group type object by name.
     1979 *
     1980 * @since 2.6.0
     1981 *
     1982 * @param string $group_type The name of the group type.
     1983 * @return object A group type object.
     1984 */
     1985function bp_groups_get_group_type_object( $group_type ) {
     1986    $types = bp_groups_get_group_types( array(), 'objects' );
     1987
     1988    if ( empty( $types[ $group_type ] ) ) {
     1989        return null;
     1990    }
     1991
     1992    return $types[ $group_type ];
     1993}
     1994
     1995/**
     1996 * Set type for a group.
     1997 *
     1998 * @since 2.6.0
     1999 *
     2000 * @param int    $group      ID of the group.
     2001 * @param string $group_type Group type.
     2002 * @param bool   $append     Optional. True to append this to existing types for group,
     2003 *                           false to replace. Default: false.
     2004 * @return array $retval See bp_set_object_terms().
     2005 */
     2006function bp_groups_set_group_type( $group_id, $group_type, $append = false ) {
     2007    // Pass an empty group type to remove group's type.
     2008    if ( ! empty( $group_type ) && ! bp_groups_get_group_type_object( $group_type ) ) {
     2009        return false;
     2010    }
     2011
     2012    $retval = bp_set_object_terms( $group_id, $group_type, 'bp_group_type', $append );
     2013
     2014    // Bust the cache if the type has been updated.
     2015    if ( ! is_wp_error( $retval ) ) {
     2016        wp_cache_delete( $group_id, 'bp_groups_group_type' );
     2017
     2018        /**
     2019         * Fires just after a group type has been changed.
     2020         *
     2021         * @since 2.6.0
     2022         *
     2023         * @param int    $group_id   ID of the group whose group type has been updated.
     2024         * @param string $group_type Group type.
     2025         * @param bool   $append     Whether the type is being appended to existing types.
     2026         */
     2027        do_action( 'bp_groups_set_group_type', $group_id, $group_type, $append );
     2028    }
     2029
     2030    return $retval;
     2031}
     2032
     2033/**
     2034 * Get type for a group.
     2035 *
     2036 * @since 2.6.0
     2037 *
     2038 * @param int  $group_id ID of the group.
     2039 * @param bool $single   Optional. Whether to return a single type string. If multiple types are found
     2040 *                       for the group, the oldest one will be returned. Default: true.
     2041 * @return string|array|bool On success, returns a single group type (if `$single` is true) or an array of group
     2042 *                           types (if `$single` is false). Returns false on failure.
     2043 */
     2044function bp_groups_get_group_type( $group_id, $single = true ) {
     2045    $types = wp_cache_get( $group_id, 'bp_groups_group_type' );
     2046
     2047    if ( false === $types ) {
     2048        $types = bp_get_object_terms( $group_id, 'bp_group_type' );
     2049
     2050        if ( ! is_wp_error( $types ) ) {
     2051            $types = wp_list_pluck( $types, 'name' );
     2052            wp_cache_set( $group_id, $types, 'bp_groups_group_type' );
     2053        }
     2054    }
     2055
     2056    $type = false;
     2057    if ( ! empty( $types ) ) {
     2058        if ( $single ) {
     2059            $type = end( $types );
     2060        } else {
     2061            $type = $types;
     2062        }
     2063    }
     2064
     2065    /**
     2066     * Filters a groups's group type(s).
     2067     *
     2068     * @since 2.6.0
     2069     *
     2070     * @param string|array $type     Group type.
     2071     * @param int          $group_id ID of the group.
     2072     * @param bool         $single   Whether to return a single type srting, or an array.
     2073     */
     2074    return apply_filters( 'bp_groups_get_group_type', $type, $group_id, $single );
     2075}
     2076
     2077/**
     2078 * Remove type for a group.
     2079 *
     2080 * @since 2.6.0
     2081 *
     2082 * @param int            $group      ID of the user.
     2083 * @param string         $group_type Group type.
     2084 * @return bool|WP_Error $deleted    True on success. False or WP_Error on failure.
     2085 */
     2086function bp_groups_remove_group_type( $group_id, $group_type ) {
     2087    if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) {
     2088        return false;
     2089    }
     2090
     2091    $deleted = bp_remove_object_terms( $group_id, $group_type, 'bp_group_type' );
     2092
     2093    // Bust the case, if the type has been removed.
     2094    if ( ! is_wp_error( $deleted ) ) {
     2095        wp_cache_delete( $group_id, 'bp_groups_group_type' );
     2096
     2097        /**
     2098         * Fires just after a group's group type has been removed.
     2099         *
     2100         * @since 2.6.0
     2101         *
     2102         * @param int    $group      ID of the group whose group type has been removed.
     2103         * @param string $group_type Group type.
     2104         */
     2105        do_action( 'bp_groups_remove_group_type', $group_id, $group_type );
     2106    }
     2107
     2108    return $deleted;
     2109}
     2110
     2111/**
     2112 * Check whether the given group has a certain group type.
     2113 *
     2114 * @since 2.6.0
     2115 *
     2116 * @param  int    $group_id   ID of the group.
     2117 * @param  srting $group_type Group type.
     2118 * @return bool   Whether the group has the give group type.
     2119 */
     2120function bp_groups_has_group_type( $group_id, $group_type ) {
     2121    if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) {
     2122        return false;
     2123    }
     2124
     2125    // Get all group's group types.
     2126    $types = bp_groups_get_group_type( $group_id, false );
     2127
     2128    if ( ! is_array( $types ) ) {
     2129        return false;
     2130    }
     2131
     2132    return in_array( $group_type, $types );
     2133}
     2134
     2135/**
     2136 * Delete a group's type when the group is deleted.
     2137 *
     2138 * @since 2.6.0
     2139 *
     2140 * @param  int   $group_id ID of the group.
     2141 * @return array $value    See {@see bp_groups_set_group_type()}.
     2142 */
     2143function bp_remove_group_type_on_group_delete( $group_id = 0 ) {
     2144    bp_groups_set_group_type( $group_id, '' );
     2145}
     2146add_action( 'groups_delete_group', 'bp_remove_group_type_on_group_delete' );
Note: See TracChangeset for help on using the changeset viewer.