Skip to:
Content

BuddyPress.org

Changeset 11555


Ignore:
Timestamp:
05/17/2017 06:57:15 PM (7 years ago)
Author:
dcavins
Message:

Change signature of groups_edit_base_group_details().

  • Changes the signature of groups_edit_base_group_details() to accept

a single array instead of several arguments.

  • Adds the capability to update the group slug, with the old slug being

stored as a group meta item.

See #6014.

Location:
trunk
Files:
3 edited

Legend:

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

    r11533 r11555  
    219219 * @since 1.0.0
    220220 *
    221  * @param int    $group_id       ID of the group.
    222  * @param string $group_name     Name of the group.
    223  * @param string $group_desc     Description of the group.
    224  * @param bool   $notify_members Whether to send an email notification to group
    225  *                               members about changes in these details.
     221 * @param array $args {
     222 *     An array of optional arguments.
     223 *     @type int    $group_id       ID of the group.
     224 *     @type string $name           Name of the group.
     225 *     @type string $slug           Slug of the group.
     226 *     @type string $description    Description of the group.
     227 *     @type bool   $notify_members Whether to send an email notification to group
     228 *                                  members about changes in these details.
     229 * }
    226230 * @return bool True on success, false on failure.
    227231 */
    228 function groups_edit_base_group_details( $group_id, $group_name, $group_desc, $notify_members ) {
    229 
    230     if ( empty( $group_name ) || empty( $group_desc ) )
    231         return false;
    232 
    233     $group     = groups_get_group( $group_id );
     232function groups_edit_base_group_details( $args = array() ) {
     233
     234    // Backward compatibility with old method of passing arguments.
     235    if ( ! is_array( $args ) || func_num_args() > 1 ) {
     236        _deprecated_argument( __METHOD__, '2.9.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
     237
     238        $old_args_keys = array(
     239            0 => 'group_id',
     240            1 => 'name',
     241            2 => 'description',
     242            3 => 'notify_members',
     243        );
     244
     245        $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
     246    }
     247
     248    $r = wp_parse_args( $args, array(
     249        'group_id'       => bp_get_current_group_id(),
     250        'name'           => null,
     251        'slug'           => null,
     252        'description'    => null,
     253        'notify_members' => false,
     254    ) );
     255
     256    if ( ! $r['group_id'] ) {
     257        return false;
     258    }
     259
     260    $group     = groups_get_group( $r['group_id'] );
    234261    $old_group = clone $group;
    235262
    236     $group->name        = $group_name;
    237     $group->description = $group_desc;
    238 
    239     if ( !$group->save() )
    240         return false;
    241 
    242     if ( $notify_members ) {
     263    // Group name, slug and description can never be empty. Update only if provided.
     264    if ( $r['name'] ) {
     265        $group->name = $r['name'];
     266    }
     267    if ( $r['slug'] && $r['slug'] != $group->slug ) {
     268        $group->slug = groups_check_slug( $r['slug'] );
     269    }
     270    if ( $r['description'] ) {
     271        $group->description = $r['description'];
     272    }
     273
     274    if ( ! $group->save() ) {
     275        return false;
     276    }
     277
     278    // Maybe update the "previous_slug" groupmeta.
     279    if ( $group->slug != $old_group->slug ) {
     280        /*
     281         * If the old slug exists in this group's past, delete that entry.
     282         * Recent previous_slugs are preferred when selecting the current group
     283         * from an old group slug, so we want the previous slug to be
     284         * saved "now" in the groupmeta table and don't need the old record.
     285         */
     286        groups_delete_groupmeta( $group->id, 'previous_slug', $old_group->slug );
     287        groups_add_groupmeta( $group->id, 'previous_slug', $old_group->slug );
     288    }
     289
     290    if ( $r['notify_members'] ) {
    243291        groups_notification_group_updated( $group->id, $old_group );
    244292    }
     
    253301     * @param bool            $notify_members Whether to send an email notification to members about the change.
    254302     */
    255     do_action( 'groups_details_updated', $group->id, $old_group, $notify_members );
     303    do_action( 'groups_details_updated', $group->id, $old_group, $r['notify_members'] );
    256304
    257305    return true;
  • trunk/tests/phpunit/testcases/groups/activity.php

    r11091 r11555  
    5656    public function test_bp_groups_format_activity_action_group_details_updated_with_no_change() {
    5757        $group = $this->factory->group->create_and_get();
    58         groups_edit_base_group_details( $group->id, $group->name, $group->description, true );
     58        groups_edit_base_group_details( array(
     59                'group_id'       => $group->id,
     60                'name'           => $group->name,
     61                'slug'           => $group->slug,
     62                'description'    => $group->description,
     63                'notify_members' => true,
     64        ) );
    5965
    6066        $a = bp_activity_get( array(
     
    7379    public function test_bp_groups_format_activity_action_group_details_updated_with_notify_members_false() {
    7480        $group = $this->factory->group->create_and_get();
    75         groups_edit_base_group_details( $group->id, 'Foo', $group->description, false );
     81        groups_edit_base_group_details( array(
     82            'group_id'       => $group->id,
     83            'name'           => 'Foo',
     84            'slug'           => $group->slug,
     85            'description'    => $group->description,
     86            'notify_members' => false,
     87        ) );
    7688
    7789        $a = bp_activity_get( array(
     
    94106
    95107        $group = $this->factory->group->create_and_get();
    96         groups_edit_base_group_details( $group->id, 'Foo', $group->description, true );
     108        groups_edit_base_group_details( array(
     109            'group_id'       => $group->id,
     110            'name'           => 'Foo',
     111            'slug'           => $group->slug,
     112            'description'    => $group->description,
     113            'notify_members' => true,
     114        ) );
    97115
    98116        $a = bp_activity_get( array(
     
    120138
    121139        $group = $this->factory->group->create_and_get();
    122         groups_edit_base_group_details( $group->id, $group->name, 'Bar', true );
     140        groups_edit_base_group_details( array(
     141            'group_id'       => $group->id,
     142            'name'           => $group->name,
     143            'slug'           => $group->slug,
     144            'description'    => 'Bar',
     145            'notify_members' => true,
     146        ) );
    123147
    124148        $a = bp_activity_get( array(
     
    146170
    147171        $group = $this->factory->group->create_and_get();
    148         groups_edit_base_group_details( $group->id, 'Foo', 'Bar', true );
     172        groups_edit_base_group_details( array(
     173            'group_id'       => $group->id,
     174            'name'           => 'Foo',
     175            'slug'           => $group->slug,
     176            'description'    => 'Bar',
     177            'notify_members' => true,
     178        ) );
    149179
    150180        $a = bp_activity_get( array(
  • trunk/tests/phpunit/testcases/groups/functions.php

    r11213 r11555  
    712712        $this->assertEquals( $g1, $group->id );
    713713    }
     714
     715    /**
     716     * @expectedDeprecated groups_edit_base_group_details
     717     * @group groups_edit_base_group_details
     718     */
     719    public function test_groups_edit_base_group_details_test_backcompat_arguments() {
     720        $g1 = $this->factory->group->create();
     721        $name = 'Great Scott';
     722        $description = 'A must-see in time for the holidays!';
     723        groups_edit_base_group_details( $g1, $name, $description, false );
     724
     725        $expected = array(
     726            'id'          => $g1,
     727            'name'        => $name,
     728            'description' => $description
     729        );
     730        $updated_group_object = groups_get_group( $g1 );
     731        $updated = array(
     732            'id'          => $updated_group_object->id,
     733            'name'        => $updated_group_object->name,
     734            'description' => $updated_group_object->description
     735        );
     736
     737        $this->assertEqualSets( $expected, $updated );
     738    }
     739
     740    /**
     741     * @group groups_edit_base_group_details
     742     */
     743    public function test_groups_edit_base_group_details_test_new_arguments() {
     744        $g1 = $this->factory->group->create();
     745        $name = 'Great Scott';
     746        $slug = 'what-about-it';
     747        $description = 'A must-see in time for the holidays!';
     748        groups_edit_base_group_details( array(
     749                'group_id'       => $g1,
     750                'name'           => $name,
     751                'slug'           => $slug,
     752                'description'    => $description,
     753                'notify_members' => false,
     754        ) );
     755
     756        $expected = array(
     757            'id'          => $g1,
     758            'slug'        => $slug,
     759            'name'        => $name,
     760            'description' => $description
     761        );
     762        $updated_group_object = groups_get_group( $g1 );
     763        $updated = array(
     764            'id'          => $updated_group_object->id,
     765            'slug'        => $updated_group_object->slug,
     766            'name'        => $updated_group_object->name,
     767            'description' => $updated_group_object->description
     768        );
     769
     770        $this->assertEqualSets( $expected, $updated );
     771    }
     772
     773    /**
     774     * @group groups_edit_base_group_details
     775     */
     776    public function test_groups_edit_base_group_details_avoid_slug_collisions() {
     777        $slug = 'circe';
     778        $g1 = $this->factory->group->create( array( 'slug' => $slug ) );
     779        $g2 = $this->factory->group->create( array( 'slug' => 'loom' ) );
     780
     781        // Attempt to use a duplicate slug.
     782        groups_edit_base_group_details( array(
     783                'group_id'       => $g2,
     784                'slug'           => $slug,
     785        ) );
     786
     787        $updated_group_object = groups_get_group( $g2 );
     788
     789        $this->assertNotEquals( $slug, $updated_group_object->slug );
     790    }
     791
     792    /**
     793     * @group groups_edit_base_group_details
     794     */
     795    public function test_groups_edit_base_group_details_slug_no_change() {
     796        $slug = 'circe';
     797        $g1 = $this->factory->group->create( array( 'slug' => $slug ) );
     798
     799        // Make sure the slug doesn't get incremented when there's no change.
     800        groups_edit_base_group_details( array(
     801                'group_id'       => $g1,
     802                'slug'           => $slug,
     803        ) );
     804
     805        $updated_group_object = groups_get_group( $g1 );
     806
     807        $this->assertEquals( $slug, $updated_group_object->slug );
     808    }
     809
     810    /**
     811     * @group groups_edit_base_group_details
     812     */
     813    public function test_groups_edit_base_group_details_slug_null_value() {
     814        $slug = 'circe';
     815        $g1 = $this->factory->group->create( array( 'slug' => $slug ) );
     816
     817        // Make sure the slug doesn't get changed when null is passed.
     818        groups_edit_base_group_details( array(
     819                'group_id'       => $g1,
     820                'slug'           => null,
     821        ) );
     822
     823        $updated_group_object = groups_get_group( $g1 );
     824
     825        $this->assertEquals( $slug, $updated_group_object->slug );
     826    }
     827
    714828}
Note: See TracChangeset for help on using the changeset viewer.