Skip to:
Content

BuddyPress.org

Changeset 10766


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.

Location:
trunk
Files:
1 added
7 edited

Legend:

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

    r10454 r10766  
    212212}
    213213add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' );
     214
     215/**
     216 * Clear the group type cache for a group.
     217 *
     218 * Called when group is deleted.
     219 *
     220 * @since 2.6.0
     221 *
     222 * @param int $group_id The group ID.
     223 */
     224function groups_clear_group_type_cache( $group_id = 0 ) {
     225    wp_cache_delete( $group_id, 'bp_groups_group_type' );
     226}
     227add_action( 'groups_delete_group', 'groups_clear_group_type_cache' );
    214228
    215229/* List actions to clear super cached pages on, if super cache is installed */
  • 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' );
  • trunk/src/bp-groups/bp-groups-template.php

    r10721 r10766  
    103103 *
    104104 * @since 1.0.0
     105 * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
    105106 *
    106107 * @param array|string $args {
     
    140141 *                                           `$_REQUEST['groups_search']` or
    141142 *                                           `$_REQUEST['s']`, if present. Otherwise false.
     143 *     @type array|string $group_type         Array or comma-separated list of group types to limit results to.
     144 *     @type array|string $group_type__in     Array or comma-separated list of group types to limit results to.
     145 *     @type array|string $group_type__not_in Array or comma-separated list of group types that will be
     146 *                                            excluded from results.
    142147 *     @type array        $meta_query        An array of meta_query conditions.
    143148 *                                           See {@link WP_Meta_Query::queries} for description.
     
    207212        'slug'              => $slug,
    208213        'search_terms'      => $search_terms,
     214        'group_type'         => '',
     215        'group_type__in'     => '',
     216        'group_type__not_in' => '',
    209217        'meta_query'        => false,
    210218        'include'           => false,
     
    227235        'slug'              => $r['slug'],
    228236        'search_terms'      => $r['search_terms'],
     237        'group_type'         => $r['group_type'],
     238        'group_type__in'     => $r['group_type__in'],
     239        'group_type__not_in' => $r['group_type__not_in'],
    229240        'meta_query'        => $r['meta_query'],
    230241        'include'           => $r['include'],
  • trunk/src/bp-groups/classes/class-bp-groups-component.php

    r10745 r10766  
    841841        parent::setup_cache_groups();
    842842    }
     843
     844    /**
     845     * Set up taxonomies.
     846     *
     847     * @since 2.6.0
     848     */
     849    public function register_taxonomies() {
     850        // Group Type.
     851        register_taxonomy( 'bp_group_type', 'bp_group', array(
     852            'public' => false,
     853        ) );
     854    }
    843855}
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r10487 r10766  
    684684     *
    685685     * @since 1.6.0
     686     * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
    686687     *
    687688     * @param array $args {
     
    705706     *                                           or descriptions match the search terms will be
    706707     *                                           returned. Default: false.
     708     *     @type array|string $group_type         Array or comma-separated list of group types to limit results to.
     709     *     @type array|string $group_type__in     Array or comma-separated list of group types to limit results to.
     710     *     @type array|string $group_type__not_in Array or comma-separated list of group types that will be
     711     *                                            excluded from results.
    707712     *     @type array        $meta_query        Optional. An array of meta_query conditions.
    708713     *                                           See {@link WP_Meta_Query::queries} for description.
     
    756761            'user_id'           => 0,
    757762            'search_terms'      => false,
     763            'group_type'         => '',
     764            'group_type__in'     => '',
     765            'group_type__not_in' => '',
    758766            'meta_query'        => false,
    759767            'include'           => false,
     
    803811        if ( ! empty( $meta_query_sql['where'] ) ) {
    804812            $sql['meta'] = $meta_query_sql['where'];
     813        }
     814
     815        // Only use 'group_type__in', if 'group_type' is not set.
     816        if ( empty( $r['group_type'] ) && ! empty( $r['group_type__in']) ) {
     817            $r['group_type'] = $r['group_type__in'];
     818        }
     819
     820        // Group types to exclude. This has priority over inclusions.
     821        if ( ! empty( $r['group_type__not_in'] ) ) {
     822            $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type__not_in'], 'NOT IN' );
     823
     824        // Group types to include.
     825        } elseif ( ! empty( $r['group_type'] ) ) {
     826            $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type'], 'IN' );
     827        }
     828
     829        if ( ! empty( $group_type_clause ) ) {
     830            $sql['group_type'] = $group_type_clause;
    805831        }
    806832
     
    914940            $meta_query_clause = preg_replace( '/^\s*AND/', '', $meta_query_sql['where'] );
    915941            $total_sql['where'][] = $meta_query_clause;
     942        }
     943
     944        // Trim leading 'AND' to match `$total_sql` query style.
     945        if ( ! empty( $group_type_clause ) ) {
     946            $total_sql['where'][] = preg_replace( '/^\s*AND\s*/', '', $group_type_clause );
    916947        }
    917948
     
    16251656        return $ids;
    16261657    }
     1658
     1659    /**
     1660     * Get SQL clause for group type(s).
     1661     *
     1662     * @since 2.6.0
     1663     *
     1664     * @param  string|array $group_types Group type(s).
     1665     * @param  string       $operator    'IN' or 'NOT IN'.
     1666     * @return string       $clause      SQL clause.
     1667     */
     1668    protected static function get_sql_clause_for_group_types( $group_types, $operator ) {
     1669        global $wpdb;
     1670
     1671        // Sanitize operator.
     1672        if ( 'NOT IN' !== $operator ) {
     1673            $operator = 'IN';
     1674        }
     1675
     1676        // Parse and sanitize types.
     1677        if ( ! is_array( $group_types ) ) {
     1678            $group_types = preg_split( '/[,\s+]/', $group_types );
     1679        }
     1680
     1681        $types = array();
     1682        foreach ( $group_types as $gt ) {
     1683            if ( bp_groups_get_group_type_object( $gt ) ) {
     1684                $types[] = $gt;
     1685            }
     1686        }
     1687
     1688        $tax_query = new WP_Tax_Query( array(
     1689            array(
     1690                'taxonomy' => 'bp_group_type',
     1691                'field'    => 'name',
     1692                'operator' => $operator,
     1693                'terms'    => $types,
     1694            ),
     1695        ) );
     1696
     1697        $switched = false;
     1698        if ( ! bp_is_root_blog() ) {
     1699            switch_to_blog( bp_get_root_blog_id() );
     1700            $switched = true;
     1701        }
     1702
     1703        $sql_clauses = $tax_query->get_sql( 'g', 'id' );
     1704
     1705        if ( $switched ) {
     1706            restore_current_blog();
     1707        }
     1708
     1709        $clause = '';
     1710
     1711        // The no_results clauses are the same between IN and NOT IN.
     1712        if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) {
     1713            $clause = $sql_clauses['where'];
     1714
     1715        // The tax_query clause generated for NOT IN can be used almost as-is.
     1716        } elseif ( 'NOT IN' === $operator ) {
     1717            $clause = $sql_clauses['where'];
     1718
     1719        // IN clauses must be converted to a subquery.
     1720        } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) {
     1721            $clause = " AND g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
     1722        }
     1723
     1724        return $clause;
     1725    }
    16271726}
  • trunk/src/bp-groups/classes/class-bp-groups-template.php

    r10520 r10766  
    167167            'exclude'           => false,
    168168            'search_terms'      => '',
     169            'group_type'         => '',
     170            'group_type__in'     => '',
     171            'group_type__not_in' => '',
    169172            'meta_query'        => false,
    170173            'populate_extras'   => true,
     
    219222                'search_terms'      => $search_terms,
    220223                'meta_query'        => $meta_query,
     224                'group_type'         => $group_type,
     225                'group_type__in'     => $group_type__in,
     226                'group_type__not_in' => $group_type__not_in,
    221227                'include'           => $include,
    222228                'exclude'           => $exclude,
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r10488 r10766  
    10301030        $this->set_current_user( $old_user );
    10311031    }
     1032
     1033    /**
     1034     * @group group_types
     1035     */
     1036    public function test_group_type_single_value() {
     1037        $g1 = $this->factory->group->create();
     1038        $g2 = $this->factory->group->create();
     1039        bp_groups_register_group_type( 'foo' );
     1040        bp_groups_register_group_type( 'bar' );
     1041        bp_groups_set_group_type( $g1, 'foo' );
     1042        bp_groups_set_group_type( $g2, 'bar' );
     1043
     1044        $groups = BP_Groups_Group::get( array(
     1045            'group_type' => 'foo',
     1046        ) );
     1047
     1048        $found = wp_list_pluck( $groups['groups'], 'id' );
     1049        $this->assertEquals( array( $g1 ), $found );
     1050    }
     1051
     1052    /**
     1053     * @group group_types
     1054     */
     1055    public function test_group_type_array_with_single_value() {
     1056        $g1 = $this->factory->group->create();
     1057        $g2 = $this->factory->group->create();
     1058        bp_groups_register_group_type( 'foo' );
     1059        bp_groups_register_group_type( 'bar' );
     1060        bp_groups_set_group_type( $g1, 'foo' );
     1061        bp_groups_set_group_type( $g2, 'bar' );
     1062
     1063        $groups = BP_Groups_Group::get( array(
     1064            'group_type' => array( 'foo' ),
     1065        ) );
     1066
     1067        $found = wp_list_pluck( $groups['groups'], 'id' );
     1068        $this->assertEquals( array( $g1 ), $found );
     1069    }
     1070
     1071    /**
     1072     * @group group_types
     1073     */
     1074    public function test_group_type_with_comma_separated_list() {
     1075        $g1 = $this->factory->group->create();
     1076        $g2 = $this->factory->group->create();
     1077        bp_groups_register_group_type( 'foo' );
     1078        bp_groups_register_group_type( 'bar' );
     1079        bp_groups_set_group_type( $g1, 'foo' );
     1080        bp_groups_set_group_type( $g2, 'bar' );
     1081
     1082        $groups = BP_Groups_Group::get( array(
     1083            'group_type' => 'foo, bar',
     1084        ) );
     1085
     1086        $found = wp_list_pluck( $groups['groups'], 'id' );
     1087        $this->assertEqualSets( array( $g1, $g2 ), $found );
     1088    }
     1089
     1090    /**
     1091     * @group group_types
     1092     */
     1093    public function test_group_type_array_with_multiple_values() {
     1094        $g1 = $this->factory->group->create();
     1095        $g2 = $this->factory->group->create();
     1096        bp_groups_register_group_type( 'foo' );
     1097        bp_groups_register_group_type( 'bar' );
     1098        bp_groups_set_group_type( $g1, 'foo' );
     1099        bp_groups_set_group_type( $g2, 'bar' );
     1100
     1101        $groups = BP_Groups_Group::get( array(
     1102            'group_type' => array( 'foo', 'bar' ),
     1103        ) );
     1104
     1105        $found = wp_list_pluck( $groups['groups'], 'id' );
     1106        $this->assertEqualSets( array( $g1, $g2 ), $found );
     1107    }
     1108
     1109    /**
     1110     * @group group_types
     1111     */
     1112    public function test_group_type_should_discart_non_existing_types_in_comma_separated_value() {
     1113        $g1 = $this->factory->group->create();
     1114        $g2 = $this->factory->group->create();
     1115        bp_groups_register_group_type( 'foo' );
     1116        bp_groups_register_group_type( 'bar' );
     1117        bp_groups_set_group_type( $g1, 'foo' );
     1118        bp_groups_set_group_type( $g2, 'bar' );
     1119
     1120        $groups = BP_Groups_Group::get( array(
     1121            'group_type' => 'foo, baz',
     1122        ) );
     1123
     1124        $found = wp_list_pluck( $groups['groups'], 'id' );
     1125        $this->assertEquals( array( $g1 ), $found );
     1126    }
     1127
     1128    /**
     1129     * @group group_types
     1130     */
     1131    public function test_group_type_should_return_empty_when_no_groups_match_specified_types() {
     1132        $g1 = $this->factory->group->create();
     1133        $g2 = $this->factory->group->create();
     1134
     1135        $groups = BP_Groups_Group::get( array(
     1136            'group_type' => 'foo, baz',
     1137        ) );
     1138
     1139        $this->assertEmpty( $groups['groups'] );
     1140    }
     1141
     1142    /**
     1143     * @group group_types
     1144     */
     1145    public function test_group_type__in_single_value() {
     1146        $g1 = $this->factory->group->create();
     1147        $g2 = $this->factory->group->create();
     1148        bp_groups_register_group_type( 'foo' );
     1149        bp_groups_register_group_type( 'bar' );
     1150        bp_groups_set_group_type( $g1, 'foo' );
     1151        bp_groups_set_group_type( $g2, 'bar' );
     1152
     1153        $groups = BP_Groups_Group::get( array(
     1154            'group_type__in' => 'bar',
     1155        ) );
     1156
     1157        $found = wp_list_pluck( $groups['groups'], 'id' );
     1158        $this->assertEquals( array( $g2 ), $found );
     1159    }
     1160
     1161    /**
     1162     * @group group_types
     1163     */
     1164    public function test_group_type__in_comma_separated_values() {
     1165        $g1 = $this->factory->group->create();
     1166        $g2 = $this->factory->group->create();
     1167        bp_groups_register_group_type( 'foo' );
     1168        bp_groups_register_group_type( 'bar' );
     1169        bp_groups_set_group_type( $g1, 'foo' );
     1170        bp_groups_set_group_type( $g2, 'bar' );
     1171
     1172        $groups = BP_Groups_Group::get( array(
     1173            'group_type__in' => 'foo, bar',
     1174        ) );
     1175
     1176        $found = wp_list_pluck( $groups['groups'], 'id' );
     1177        $this->assertEqualSets( array( $g1, $g2 ), $found );
     1178    }
     1179
     1180    /**
     1181     * @group group_types
     1182     */
     1183    public function test_group_type__in_array_multiple_values() {
     1184        $g1 = $this->factory->group->create();
     1185        $g2 = $this->factory->group->create();
     1186        bp_groups_register_group_type( 'foo' );
     1187        bp_groups_register_group_type( 'bar' );
     1188        bp_groups_set_group_type( $g1, 'foo' );
     1189        bp_groups_set_group_type( $g2, 'bar' );
     1190
     1191        $groups = BP_Groups_Group::get( array(
     1192            'group_type__in' => array( 'foo', 'bar' ),
     1193        ) );
     1194
     1195        $found = wp_list_pluck( $groups['groups'], 'id' );
     1196        $this->assertEqualSets( array( $g1, $g2 ), $found );
     1197    }
     1198
     1199    /**
     1200     * @group group_types
     1201     */
     1202    public function test_group_type__in_array_with_single_value() {
     1203        $g1 = $this->factory->group->create();
     1204        $g2 = $this->factory->group->create();
     1205        bp_groups_register_group_type( 'foo' );
     1206        bp_groups_register_group_type( 'bar' );
     1207        bp_groups_set_group_type( $g1, 'foo' );
     1208        bp_groups_set_group_type( $g2, 'bar' );
     1209
     1210        $groups = BP_Groups_Group::get( array(
     1211            'group_type__in' => array( 'foo' ),
     1212        ) );
     1213
     1214        $found = wp_list_pluck( $groups['groups'], 'id' );
     1215        $this->assertEquals( array( $g1 ), $found );
     1216    }
     1217
     1218    /**
     1219     * @group group_types
     1220     */
     1221    public function test_group_type__in_should_discart_non_existing_types_in_comma_separated_value() {
     1222        $g1 = $this->factory->group->create();
     1223        $g2 = $this->factory->group->create();
     1224        bp_groups_register_group_type( 'foo' );
     1225        bp_groups_register_group_type( 'bar' );
     1226        bp_groups_set_group_type( $g1, 'foo' );
     1227        bp_groups_set_group_type( $g2, 'bar' );
     1228
     1229        $groups = BP_Groups_Group::get( array(
     1230            'group_type__in' => 'foo, baz',
     1231        ) );
     1232
     1233        $found = wp_list_pluck( $groups['groups'], 'id' );
     1234        $this->assertEquals( array( $g1 ), $found );
     1235    }
     1236
     1237    /**
     1238     * @group group_types
     1239     */
     1240    public function test_group_type__in_should_return_empty_when_no_groups_match_specified_types() {
     1241        $g1 = $this->factory->group->create();
     1242        $g2 = $this->factory->group->create();
     1243
     1244        $groups = BP_Groups_Group::get( array(
     1245            'group_type__in' => 'foo, baz',
     1246        ) );
     1247
     1248        $this->assertEmpty( $groups['groups'] );
     1249    }
     1250
     1251    /**
     1252     * @group group_types
     1253     */
     1254    public function test_group_type_should_take_precedence_over_group_type__in() {
     1255        $g1 = $this->factory->group->create();
     1256        $g2 = $this->factory->group->create();
     1257        bp_groups_register_group_type( 'foo' );
     1258        bp_groups_register_group_type( 'bar' );
     1259        bp_groups_set_group_type( $g1, 'foo' );
     1260        bp_groups_set_group_type( $g2, 'bar' );
     1261
     1262        $groups = BP_Groups_Group::get( array(
     1263            'group_type__in' => 'foo',
     1264            'group_type' => 'bar',
     1265        ) );
     1266
     1267        $found = wp_list_pluck( $groups['groups'], 'id' );
     1268        $this->assertEquals( array( $g2 ), $found );
     1269    }
     1270
     1271    /**
     1272     * @group group_types
     1273     */
     1274    public function test_group_type__not_in_should_return_groups_with_types_and_without_types() {
     1275        $g1 = $this->factory->group->create();
     1276        $g2 = $this->factory->group->create();
     1277        $g3 = $this->factory->group->create();
     1278        bp_groups_register_group_type( 'foo' );
     1279        bp_groups_register_group_type( 'bar' );
     1280        bp_groups_set_group_type( $g1, 'foo' );
     1281        bp_groups_set_group_type( $g2, 'bar' );
     1282
     1283        $groups = BP_Groups_Group::get( array(
     1284            'group_type__not_in' => 'foo',
     1285        ) );
     1286
     1287        $found = wp_list_pluck( $groups['groups'], 'id' );
     1288        $this->assertEquals( array( $g2, $g3 ), $found );
     1289    }
     1290
     1291    /**
     1292     * @group group_types
     1293     */
     1294    public function test_group_type__not_in_comma_separated_values() {
     1295        $g1 = $this->factory->group->create();
     1296        $g2 = $this->factory->group->create();
     1297        $g3 = $this->factory->group->create();
     1298        bp_groups_register_group_type( 'foo' );
     1299        bp_groups_register_group_type( 'bar' );
     1300        bp_groups_set_group_type( $g1, 'foo' );
     1301        bp_groups_set_group_type( $g2, 'bar' );
     1302        bp_groups_set_group_type( $g3, 'baz' );
     1303
     1304        $groups = BP_Groups_Group::get( array(
     1305            'group_type__not_in' => 'foo, bar',
     1306        ) );
     1307
     1308        $found = wp_list_pluck( $groups['groups'], 'id' );
     1309        $this->assertEquals( array( $g3 ), $found );
     1310    }
     1311
     1312    /**
     1313     * @group group_types
     1314     */
     1315    public function test_group_type__not_array_with_multiple_values() {
     1316        $g1 = $this->factory->group->create();
     1317        $g2 = $this->factory->group->create();
     1318        $g3 = $this->factory->group->create();
     1319        bp_groups_register_group_type( 'foo' );
     1320        bp_groups_register_group_type( 'bar' );
     1321        bp_groups_set_group_type( $g1, 'foo' );
     1322        bp_groups_set_group_type( $g2, 'bar' );
     1323        bp_groups_set_group_type( $g3, 'baz' );
     1324
     1325        $groups = BP_Groups_Group::get( array(
     1326            'group_type__not_in' => array( 'foo', 'bar' ),
     1327        ) );
     1328
     1329        $found = wp_list_pluck( $groups['groups'], 'id' );
     1330        $this->assertEquals( array( $g3 ), $found );
     1331    }
     1332
     1333    /**
     1334     * @group group_types
     1335     */
     1336    public function test_group_type__not_in_should_return_no_results_when_all_groups_mathc_sepecified_type() {
     1337        $g1 = $this->factory->group->create();
     1338        $g2 = $this->factory->group->create();
     1339        $g3 = $this->factory->group->create();
     1340        bp_groups_register_group_type( 'foo' );
     1341        bp_groups_set_group_type( $g1, 'foo' );
     1342        bp_groups_set_group_type( $g2, 'foo' );
     1343        bp_groups_set_group_type( $g3, 'foo' );
     1344
     1345        $groups = BP_Groups_Group::get( array(
     1346            'group_type__not_in' => 'foo',
     1347        ) );
     1348
     1349        $this->assertEmpty( $groups['groups'] );
     1350    }
     1351
     1352    /**
     1353     * @group group_types
     1354     */
     1355    public function test_group_type__not_in_takes_precedence_over_group_type() {
     1356        $g1 = $this->factory->group->create();
     1357        $g2 = $this->factory->group->create();
     1358        $g3 = $this->factory->group->create();
     1359        bp_groups_register_group_type( 'foo' );
     1360        bp_groups_set_group_type( $g1, 'foo' );
     1361        bp_groups_set_group_type( $g2, 'foo' );
     1362        bp_groups_set_group_type( $g3, 'foo' );
     1363
     1364        $groups = BP_Groups_Group::get( array(
     1365            'group_type' => 'foo',
     1366            'group_type__not_in' => 'foo',
     1367        ) );
     1368
     1369        $this->assertEmpty( $groups['groups'] );
     1370    }
    10321371}
    10331372
Note: See TracChangeset for help on using the changeset viewer.