Skip to:
Content

BuddyPress.org

Changeset 7087


Ignore:
Timestamp:
05/19/2013 12:09:30 AM (12 years ago)
Author:
boonebgorges
Message:

Introduces 'order' and 'orderby' parameters for the bp_has_groups() stack

'order' and 'orderby' are more specific and flexible versions of the existing
'type' parameter ('popular', 'newest', etc). Backward compatibility is
maintained by always obeying 'type' when it is passed, and by internally
converting the legacy 'type' values into the corresponding 'order' and
'orderby' values.

Adds unit tests to ensure that the adjusted default values for functions in
the stack are backward compatibile (in particular, the fact that the 'type'
parameter is now empty by default, replaced with the corresponding default
values for 'order' and 'orderby'). Unit tests are also added for the new
params, as well as some internal utility methods.

See #4483

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-groups/bp-groups-classes.php

    r7084 r7087  
    333333
    334334        $defaults = array(
    335             'type'            => 'newest',
     335            'type'            => null,
     336            'orderby'         => 'date_created',
     337            'order'           => 'DESC',
    336338            'per_page'        => null,
    337339            'page'            => null,
     
    342344            'populate_extras' => true,
    343345            'exclude'         => false,
    344             'show_hidden'     => false
     346            'show_hidden'     => false,
    345347        );
    346348
     
    401403        }
    402404
    403         switch ( $r['type'] ) {
    404             case 'newest':
    405             default:
    406                 $sql['order'] = " ORDER BY g.date_created DESC";
    407                 break;
    408             case 'active':
    409                 $sql[] = "ORDER BY last_activity DESC";
    410                 break;
    411             case 'popular':
    412                 $sql[] = "ORDER BY CONVERT(gm1.meta_value, SIGNED) DESC";
    413                 break;
    414             case 'alphabetical':
    415                 $sql[] = "ORDER BY g.name ASC";
    416                 break;
    417             case 'random':
    418                 $sql[] = "ORDER BY rand()";
    419                 break;
     405        /** Order/orderby ********************************************/
     406
     407        $order   = $r['order'];
     408        $orderby = $r['orderby'];
     409
     410        // If a 'type' parameter was passed, parse it and overwrite
     411        // 'order' and 'orderby' params passed to the function
     412        if (  ! empty( $r['type'] ) ) {
     413            $order_orderby = self::convert_type_to_order_orderby( $r['type'] );
     414
     415            // If an invalid type is passed, $order_orderby will be
     416            // an array with empty values. In this case, we stick
     417            // with the default values of $order and $orderby
     418            if ( ! empty( $order_orderby['order'] ) ) {
     419                $order = $order_orderby['order'];
     420            }
     421
     422            if ( ! empty( $order_orderby['orderby'] ) ) {
     423                $orderby = $order_orderby['orderby'];
     424            }
     425        }
     426
     427        // Sanitize 'order'
     428        $order = bp_esc_sql_order( $order );
     429
     430        // Convert 'orderby' into the proper ORDER BY term
     431        $orderby = self::convert_orderby_to_order_by_term( $orderby );
     432
     433        // Random order is a special case
     434        if ( 'rand()' === $orderby ) {
     435            $sql[] = "ORDER BY rand()";
     436        } else {
     437            $sql[] = "ORDER BY {$orderby} {$order}";
    420438        }
    421439
     
    541559    }
    542560
     561    /**
     562     * Convert the 'type' parameter to 'order' and 'orderby'
     563     *
     564     * @since BuddyPress (1.8)
     565     * @access protected
     566     * @param string $type The 'type' shorthand param
     567     * @return array 'order' and 'orderby'
     568     */
     569    protected function convert_type_to_order_orderby( $type = '' ) {
     570        $order = $orderby = '';
     571
     572        switch ( $type ) {
     573            case 'newest' :
     574                $order   = 'DESC';
     575                $orderby = 'date_created';
     576                break;
     577
     578            case 'active' :
     579                $order   = 'DESC';
     580                $orderby = 'last_activity';
     581                break;
     582
     583            case 'popular' :
     584                $order   = 'DESC';
     585                $orderby = 'total_member_count';
     586                break;
     587
     588            case 'alphabetical' :
     589                $order   = 'ASC';
     590                $orderby = 'name';
     591                break;
     592
     593            case 'random' :
     594                $order   = '';
     595                $orderby = 'random';
     596                break;
     597        }
     598
     599        return array( 'order' => $order, 'orderby' => $orderby );
     600    }
     601
     602    /**
     603     * Convert the 'orderby' param into a proper SQL term/column
     604     *
     605     * @since BuddyPress (1.8)
     606     * @access protected
     607     * @param string $orderby
     608     * @return string $order_by_term
     609     */
     610    protected function convert_orderby_to_order_by_term( $orderby ) {
     611        $order_by_term = '';
     612
     613        switch ( $orderby ) {
     614            case 'date_created' :
     615            default :
     616                $order_by_term = 'g.date_created';
     617                break;
     618
     619            case 'last_activity' :
     620                $order_by_term = 'last_activity';
     621                break;
     622
     623            case 'total_group_members' :
     624                $order_by_term = 'CONVERT(gm1.meta_value, SIGNED)';
     625                break;
     626
     627            case 'name' :
     628                $order_by_term = 'g.name';
     629                break;
     630
     631            case 'random' :
     632                $order_by_term = 'rand()';
     633                break;
     634        }
     635
     636        return $order_by_term;
     637    }
    543638
    544639    function get_by_most_forum_topics( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) {
  • trunk/bp-groups/bp-groups-functions.php

    r6950 r7087  
    414414
    415415    $defaults = array(
    416         'type'            => 'active', // active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts
     416        'type'            => false,    // active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts
     417        'order'           => 'DESC',   // 'ASC' or 'DESC'
     418        'orderby'         => 'date_created', // date_created, last_activity, total_member_count, name, random
    417419        'user_id'         => false,    // Pass a user_id to limit to only groups that this user is a member of
    418420        'include'         => false,    // Only include these specific groups (group_ids)
     
    438440        'per_page'        => $r['per_page'],
    439441        'page'            => $r['page'],
    440         'populate_extras' => $r['populate_extras']
     442        'populate_extras' => $r['populate_extras'],
     443        'order'           => $r['order'],
     444        'orderby'         => $r['orderby'],
    441445    ) );
    442446
  • trunk/bp-groups/bp-groups-template.php

    r6951 r7087  
    167167            $this->groups = groups_get_groups( array(
    168168                'type'            => $type,
     169                'order'           => $order,
     170                'orderby'         => $orderby,
    169171                'per_page'        => $this->pag_num,
    170172                'page'            => $this->pag_page,
     
    267269}
    268270
     271/**
     272 * Start the Groups Template Loop
     273 *
     274 * See the $defaults definition below for a description of parameters.
     275 *
     276 * Note that the 'type' parameter overrides 'order' and 'orderby'. See
     277 * BP_Groups_Group::get() for more details.
     278 *
     279 * @param array $args
     280 * @return bool True if there are groups to display that match the params
     281 */
    269282function bp_has_groups( $args = '' ) {
    270283    global $groups_template, $bp;
     
    276289     */
    277290    $slug    = false;
    278     $type    = 'active';
     291    $type    = '';
    279292    $user_id = 0;
    280293    $order   = '';
     
    285298
    286299    // Type
     300    // @todo What is $order? At some point it was removed incompletely?
    287301    if ( bp_is_current_action( 'my-groups' ) ) {
    288302        if ( 'most-popular' == $order ) {
     
    299313
    300314    $defaults = array(
    301         'type'            => $type,
     315        'type'            => $type, // 'type' is an override for 'order' and 'orderby'. See docblock.
     316        'order'           => 'DESC',
     317        'orderby'         => 'last_activity',
    302318        'page'            => 1,
    303319        'per_page'        => 20,
     
    314330        'exclude'         => false,    // Pass comma separated list or array of group ID's to exclude these groups
    315331
    316         'populate_extras' => true      // Get extra meta - is_member, is_banned
     332        'populate_extras' => true,     // Get extra meta - is_member, is_banned
    317333    );
    318334
     
    330346    $groups_template = new BP_Groups_Template( array(
    331347        'type'            => $r['type'],
     348        'order'           => $r['order'],
     349        'orderby'         => $r['orderby'],
    332350        'page'            => (int) $r['page'],
    333351        'per_page'        => (int) $r['per_page'],
  • trunk/tests/testcases/groups/class-bp-groups-group.php

    r7086 r7087  
    172172    }
    173173
     174    /**
     175     * BP 1.8 will change the default 'type' param in favor of default
     176     * 'order' and 'orderby'. This is to make sure that existing plugins
     177     * will work appropriately
     178     *
     179     * @group get
     180     */
     181    public function test_get_with_default_type_value_should_be_newest() {
     182        $g1 = $this->factory->group->create( array(
     183            'name' => 'A Group',
     184            'date_created' => bp_core_current_time(),
     185        ) );
     186        $g2 = $this->factory->group->create( array(
     187            'name' => 'D Group',
     188            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
     189        ) );
     190        $g3 = $this->factory->group->create( array(
     191            'name' => 'B Group',
     192            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100000 ),
     193        ) );
     194        $g4 = $this->factory->group->create( array(
     195            'name' => 'C Group',
     196            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 1000 ),
     197        ) );
     198
     199        $found = BP_Groups_Group::get();
     200
     201        $this->assertEquals( BP_Groups_Group::get( array( 'type' => 'newest' ) ), $found );
     202    }
     203
     204    /**
     205     * @group get
     206     */
     207    public function test_get_with_type_newest() {
     208        $g1 = $this->factory->group->create( array(
     209            'name' => 'A Group',
     210            'date_created' => bp_core_current_time(),
     211        ) );
     212        $g2 = $this->factory->group->create( array(
     213            'name' => 'D Group',
     214            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     215        ) );
     216        $g3 = $this->factory->group->create( array(
     217            'name' => 'B Group',
     218            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100000 ),
     219        ) );
     220        $g4 = $this->factory->group->create( array(
     221            'name' => 'C Group',
     222            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
     223        ) );
     224
     225        $groups = BP_Groups_Group::get( array( 'type' => 'newest' ) );
     226        $found = wp_parse_id_list( wp_list_pluck( $groups['groups'], 'id' ) );
     227        $this->assertEquals( array( $g1, $g2, $g4, $g3 ), $found );
     228    }
     229
     230    /** convert_type_to_order_orderby() **********************************/
     231
     232    /**
     233     * @group convert_type_to_order_orderby
     234     */
     235    public function test_convert_type_to_order_orderby_newest() {
     236        $expected = array(
     237            'order' => 'DESC',
     238            'orderby' => 'date_created',
     239        );
     240        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'newest' ) );
     241    }
     242
     243    /**
     244     * @group convert_type_to_order_orderby
     245     */
     246    public function test_convert_type_to_order_orderby_active() {
     247        $expected = array(
     248            'order' => 'DESC',
     249            'orderby' => 'last_activity',
     250        );
     251        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'active' ) );
     252    }
     253
     254    /**
     255     * @group convert_type_to_order_orderby
     256     */
     257    public function test_convert_type_to_order_orderby_popular() {
     258        $expected = array(
     259            'order' => 'DESC',
     260            'orderby' => 'total_member_count',
     261        );
     262        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'popular' ) );
     263    }
     264
     265    /**
     266     * @group convert_type_to_order_orderby
     267     */
     268    public function test_convert_type_to_order_orderby_alphabetical() {
     269        $expected = array(
     270            'order' => 'ASC',
     271            'orderby' => 'name',
     272        );
     273        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'alphabetical' ) );
     274    }
     275
     276    /**
     277     * @group convert_type_to_order_orderby
     278     */
     279    public function test_convert_type_to_order_orderby_random() {
     280        $expected = array(
     281            // order gets thrown out
     282            'order' => '',
     283            'orderby' => 'random',
     284        );
     285        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'random' ) );
     286    }
     287
     288    /**
     289     * @group convert_type_to_order_orderby
     290     */
     291    public function test_convert_type_to_order_orderby_invalid() {
     292        $expected = array(
     293            'order' => '',
     294            'orderby' => '',
     295        );
     296        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'foooooooooooooooobar' ) );
     297    }
     298
     299    /** convert_orderby_to_order_by_term() **********************************/
     300
     301    /**
     302     * @group convert_orderby_to_order_by_term
     303     */
     304    public function test_convert_orderby_to_order_by_term_date_created() {
     305        $this->assertEquals( 'g.date_created', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'date_created' ) );
     306    }
     307
     308    /**
     309     * @group convert_orderby_to_order_by_term
     310     */
     311    public function test_convert_orderby_to_order_by_term_last_activity() {
     312        $c = new _BP_Groups_Group();
     313        $this->assertEquals( 'last_activity', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'last_activity' ) );
     314    }
     315
     316    /**
     317     * @group convert_orderby_to_order_by_term
     318     */
     319    public function test_convert_orderby_to_order_by_term_total_group_members() {
     320        $c = new _BP_Groups_Group();
     321        $this->assertEquals( 'CONVERT(gm1.meta_value, SIGNED)', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'total_group_members' ) );
     322    }
     323
     324    /**
     325     * @group convert_orderby_to_order_by_term
     326     */
     327    public function test_convert_orderby_to_order_by_term_name() {
     328        $c = new _BP_Groups_Group();
     329        $this->assertEquals( 'g.name', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'name' ) );
     330    }
     331
     332    /**
     333     * @group convert_orderby_to_order_by_term
     334     */
     335    public function test_convert_orderby_to_order_by_term_random() {
     336        $c = new _BP_Groups_Group();
     337        $this->assertEquals( 'rand()', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'random' ) );
     338    }
     339
     340    /**
     341     * @group convert_orderby_to_order_by_term
     342     */
     343    public function test_convert_orderby_to_order_by_term_invalid_fallback_to_date_created() {
     344        $c = new _BP_Groups_Group();
     345        $this->assertEquals( _BP_Groups_Group::_convert_orderby_to_order_by_term( 'date_created' ), _BP_Groups_Group::_convert_orderby_to_order_by_term( 'I am a bad boy' ) );
     346    }
     347
    174348    public function test_filter_user_groups_normal_search() {
    175349        $g1 = $this->factory->group->create( array(
     
    358532    }
    359533}
     534
     535/**
     536 * Stub class for accessing protected methods
     537 */
     538class _BP_Groups_Group extends BP_Groups_Group {
     539    public function _convert_type_to_order_orderby( $type ) {
     540        return self::convert_type_to_order_orderby( $type );
     541    }
     542
     543    public function _convert_orderby_to_order_by_term( $term ) {
     544        return self::convert_orderby_to_order_by_term( $term );
     545    }
     546}
  • trunk/tests/testcases/groups/template.php

    r7086 r7087  
    3535        $this->assertEquals( $ids, array( $g1, ) );
    3636    }
     37
     38    /**
     39     * Integration test to make sure order and orderby are interpreted when
     40     * no 'type' value has been passed
     41     *
     42     * @group bp_has_groups
     43     */
     44    public function test_bp_has_groups_with_order_orderby_with_null_type() {
     45        $g1 = $this->factory->group->create( array(
     46            'name' => 'AAAAA',
     47            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
     48            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 1000000 ),
     49        ) );
     50        $g2 = $this->factory->group->create( array(
     51            'name' => 'BBBBB',
     52            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 1000000 ),
     53            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 10000 ),
     54        ) );
     55        $g3 = $this->factory->group->create( array(
     56            'name' => 'CCCCC',
     57            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 10000 ),
     58            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 10 ),
     59        ) );
     60
     61        global $groups_template;
     62        bp_has_groups( array(
     63            'order' => 'ASC',
     64            'orderby' => 'name',
     65        ) );
     66
     67        $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
     68        $this->assertEquals( array( $g1, $g2, $g3, ), $ids );
     69    }
     70
     71    /**
     72     * Integration test to make sure 'order' is set to 'DESC' and 'orderby'
     73     * to 'last_activity' when no type or order/orderby params are passed.
     74     * This ensures backpat with the old system, where 'active' was the
     75     * default type param, and there were no order/orderby params.
     76     *
     77     * @group bp_has_groups
     78     */
     79    public function test_bp_has_groups_defaults_to_DESC_last_activity_for_default_type_active_backpat() {
     80        $g1 = $this->factory->group->create( array(
     81            'name' => 'AAAAA',
     82            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
     83        ) );
     84        $g2 = $this->factory->group->create( array(
     85            'name' => 'BBBBB',
     86            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 1000000 ),
     87        ) );
     88        $g3 = $this->factory->group->create( array(
     89            'name' => 'CCCCC',
     90            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 10000 ),
     91        ) );
     92
     93        global $groups_template;
     94        bp_has_groups();
     95
     96        $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
     97        $this->assertEquals( array( $g1, $g3, $g2, ), $ids );
     98    }
     99
    37100}
Note: See TracChangeset for help on using the changeset viewer.