Skip to:
Content

BuddyPress.org

Changeset 11558


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

Introduce groups_get_id_by_previous_slug().

Introduce groups_get_id_by_previous_slug() function to find groups
that once had the requested slug.

See #6014.

Location:
trunk
Files:
3 edited

Legend:

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

    r11555 r11558  
    476476function groups_get_id( $group_slug ) {
    477477    return BP_Groups_Group::group_exists( $group_slug );
     478}
     479
     480/**
     481 * Get a group ID by checking against old (not currently active) slugs.
     482 *
     483 * @since 2.9.0
     484 *
     485 * @param string $group_slug The group's slug.
     486 * @return int|null The group ID on success; null on failure.
     487 */
     488function groups_get_id_by_previous_slug( $group_slug ) {
     489    return BP_Groups_Group::get_id_by_previous_slug( $group_slug );
    478490}
    479491
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r11544 r11558  
    707707    public static function get_id_from_slug( $slug ) {
    708708        return BP_Groups_Group::group_exists( $slug );
     709    }
     710
     711    /**
     712     * Get whether a group exists for an old slug.
     713     *
     714     * @since 2.9.0
     715     *
     716     * @param string      $slug       Slug to check.
     717     *
     718     * @return int|null|false Group ID if found; null if not; false if missing parameters.
     719     */
     720    public static function get_id_by_previous_slug( $slug ) {
     721        global $wpdb;
     722
     723        if ( empty( $slug ) ) {
     724            return false;
     725        }
     726
     727        $args = array(
     728            'meta_query'         => array(
     729                array(
     730                    'key'   => 'previous_slug',
     731                    'value' => $slug
     732                ),
     733            ),
     734            'orderby'            => 'meta_id',
     735            'order'              => 'DESC',
     736            'per_page'           => 1,
     737            'page'               => 1,
     738            'update_meta_cache'  => false,
     739            'show_hidden'        => true,
     740        );
     741        $groups = BP_Groups_Group::get( $args );
     742
     743        $group_id = null;
     744        if ( $groups['groups'] ) {
     745            $group_id = current( $groups['groups'] )->id;
     746        }
     747
     748        return $group_id;
    709749    }
    710750
  • trunk/tests/phpunit/testcases/groups/functions.php

    r11555 r11558  
    826826    }
    827827
     828    /**
     829     * @group groups_get_id_by_previous_slug
     830     */
     831    public function test_groups_get_id_by_previous_slug() {
     832        $slug = 'circe';
     833        $g1 = $this->factory->group->create( array( 'slug' => $slug ) );
     834        $g2 = $this->factory->group->create( array( 'slug' => 'loom' ) );
     835
     836        groups_edit_base_group_details( array(
     837            'group_id'       => $g1,
     838            'slug'           => 'newslug',
     839        ) );
     840
     841        // Function should return the group ID as an integer.
     842        $this->assertSame( $g1, groups_get_id_by_previous_slug( $slug ) );
     843    }
     844
     845    /**
     846     * @group groups_get_id_by_previous_slug
     847     */
     848    public function test_groups_get_id_by_previous_slug_null_no_results() {
     849        $this->assertNull( groups_get_id_by_previous_slug( 'woohoo' ) );
     850    }
     851
    828852}
Note: See TracChangeset for help on using the changeset viewer.