Skip to:
Content

BuddyPress.org

Changeset 11533


Ignore:
Timestamp:
04/20/2017 12:56:53 AM (7 years ago)
Author:
dcavins
Message:

Add 'meta_id' orderby option to BP_Groups_Group::get().

Add meta_id orderby option to groups_get_groups() and its
underlying function BP_Groups_Group::get(). When searching for groups
by meta value, the found groups may now be ordered by the order in
which the found meta rows appear in the bp_groups_groupmeta table.

See #6014.

Location:
trunk
Files:
3 edited

Legend:

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

    r11532 r11533  
    705705        'type'               => false,          // Active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts.
    706706        'order'              => 'DESC',         // 'ASC' or 'DESC'
    707         'orderby'            => 'date_created', // date_created, last_activity, total_member_count, name, random.
     707        'orderby'            => 'date_created', // date_created, last_activity, total_member_count, name, random, meta_id.
    708708        'user_id'            => false,          // Pass a user_id to limit to only groups that this user is a member of.
    709709        'include'            => false,          // Only include these specific groups (group_ids).
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r11532 r11533  
    884884     *                                            Default: null.
    885885     *     @type string       $orderby            Optional. Property to sort by. 'date_created', 'last_activity',
    886      *                                            'total_member_count', 'name', 'random'. Default: 'date_created'.
     886     *                                            'total_member_count', 'name', 'random', 'meta_id'.
     887     *                                            Default: 'date_created'.
    887888     *     @type string       $order              Optional. Sort order. 'ASC' or 'DESC'. Default: 'DESC'.
    888889     *     @type int          $per_page           Optional. Number of items to return per page of results.
     
    994995        $where_conditions = array();
    995996
    996 
    997997        if ( ! empty( $r['status'] ) ) {
    998998            if ( ! is_array( $r['status'] ) ) {
     
    11391139            $sql['from'] .= " JOIN {$bp->groups->table_name_groupmeta} gm_last_activity on ( g.id = gm_last_activity.group_id )";
    11401140            $where_conditions['last_activity'] = "gm_last_activity.meta_key = 'last_activity'";
     1141        }
     1142
     1143        // If 'meta_id' is the requested order, and there's no meta query, fall back to the default.
     1144        if ( 'meta_id' === $orderby && empty( $meta_query_sql['join'] ) ) {
     1145            $orderby = 'date_created';
    11411146        }
    11421147
     
    14431448                $order_by_term = 'rand()';
    14441449                break;
     1450
     1451            case 'meta_id' :
     1452                $order_by_term = buddypress()->groups->table_name_groupmeta . '.id';
     1453                break;
    14451454        }
    14461455
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r11532 r11533  
    957957    }
    958958
     959    /**
     960     * @group groups_get_orderby_meta_id
     961     */
     962    public function test_get_orderby_meta_id() {
     963        $g1 = $this->factory->group->create();
     964        $g2 = $this->factory->group->create();
     965        $g3 = $this->factory->group->create();
     966
     967        groups_update_groupmeta( $g2, 'orderup', 'sammy' );
     968        groups_update_groupmeta( $g1, 'orderup', 'sammy' );
     969
     970        $args = array(
     971            'meta_query'         => array(
     972                array(
     973                    'key'   => 'orderup',
     974                    'value' => 'sammy'
     975                ),
     976            ),
     977            'orderby'           => 'meta_id',
     978            'order'             => 'ASC',
     979        );
     980        $groups = BP_Groups_Group::get( $args );
     981
     982        $found = wp_list_pluck( $groups['groups'], 'id' );
     983        $this->assertEquals( array( $g2, $g1 ), $found );
     984    }
     985
     986    /**
     987     * @group groups_get_orderby_meta_id
     988     */
     989    public function test_get_orderby_meta_id_invalid_fallback_to_date_created() {
     990        $time = time();
     991        $g1 = $this->factory->group->create( array(
     992            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 10000 ),
     993        ) );
     994        $g2 = $this->factory->group->create( array(
     995            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
     996        ) );
     997        $g3 = $this->factory->group->create( array(
     998            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     999        ) );
     1000
     1001        $args = array(
     1002            'orderby' => 'meta_id',
     1003        );
     1004        $groups = BP_Groups_Group::get( $args );
     1005
     1006        // Orderby meta_id should be ignored if no meta query is present.
     1007        $found = wp_list_pluck( $groups['groups'], 'id' );
     1008        $this->assertEquals( array( $g3, $g2, $g1 ), $found );
     1009    }
     1010
    9591011    public function test_filter_user_groups_normal_search() {
    9601012        $g1 = $this->factory->group->create( array(
Note: See TracChangeset for help on using the changeset viewer.