Skip to:
Content

BuddyPress.org

Changeset 11142


Ignore:
Timestamp:
09/21/2016 06:00:05 PM (9 years ago)
Author:
r-a-y
Message:

Groups: In bp_groups_set_group_type(), allow an array of group types to be set.

See #7210.

Location:
trunk
Files:
2 edited

Legend:

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

    r11100 r11142  
    22482248 *
    22492249 * @since 2.6.0
    2250  *
    2251  * @param int    $group      ID of the group.
    2252  * @param string $group_type Group type.
    2253  * @param bool   $append     Optional. True to append this to existing types for group,
    2254  *                           false to replace. Default: false.
     2250 * @since 2.7.0 $group_type parameter also accepts an array of group types now.
     2251 *
     2252 * @param int          $group      ID of the group.
     2253 * @param string|array $group_type Group type or array of group types to set.
     2254 * @param bool         $append     Optional. True to append this to existing types for group,
     2255 *                                 false to replace. Default: false.
    22552256 * @return array $retval See bp_set_object_terms().
    22562257 */
    22572258function bp_groups_set_group_type( $group_id, $group_type, $append = false ) {
    22582259    // Pass an empty group type to remove group's type.
    2259     if ( ! empty( $group_type ) && ! bp_groups_get_group_type_object( $group_type ) ) {
    2260         return false;
     2260    if ( ! empty( $group_type ) && is_string( $group_type ) && ! bp_groups_get_group_type_object( $group_type ) ) {
     2261        return false;
     2262    }
     2263
     2264    // Cast as array.
     2265    $group_type = (array) $group_type;
     2266
     2267    // Validate group types.
     2268    foreach ( $group_type as $type ) {
     2269        // Remove any invalid group types.
     2270        if ( is_null( bp_groups_get_group_type_object( $type ) ) ) {
     2271            unset( $group_type[ $type ] );
     2272        }
    22612273    }
    22622274
     
    22722284         * @since 2.6.0
    22732285         *
    2274          * @param int    $group_id   ID of the group whose group type has been updated.
    2275          * @param string $group_type Group type.
    2276          * @param bool   $append     Whether the type is being appended to existing types.
     2286         * @param int          $group_id   ID of the group whose group type has been updated.
     2287         * @param string|array $group_type Group type or array of group types.
     2288         * @param bool         $append     Whether the type is being appended to existing types.
    22772289         */
    22782290        do_action( 'bp_groups_set_group_type', $group_id, $group_type, $append );
  • trunk/tests/phpunit/testcases/groups/types.php

    r11046 r11142  
    264264        $this->assertEquals( array( 'foo' ), $type );
    265265    }
     266
     267    public function test_bp_groups_set_group_type_should_set_multiple_types_when_passing_array_of_types() {
     268        $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) );
     269        bp_groups_register_group_type( 'foo' );
     270        bp_groups_register_group_type( 'bar' );
     271
     272        // Set multiple group types.
     273        $types = array( 'foo', 'bar' );
     274        bp_groups_set_group_type( $g, $types );
     275
     276        // Assert!
     277        $this->assertEqualSets( $types, bp_groups_get_group_type( $g, false ) );
     278    }
    266279}
Note: See TracChangeset for help on using the changeset viewer.