Ticket #4483: 4483.patch
| File 4483.patch, 19.6 KB (added by , 13 years ago) |
|---|
-
bp-groups/bp-groups-classes.php
diff --git bp-groups/bp-groups-classes.php bp-groups/bp-groups-classes.php index 6744aba..121d21a 100644
class BP_Groups_Group { 332 332 } 333 333 334 334 $defaults = array( 335 'type' => 'newest',335 'type' => null, 336 336 'per_page' => null, 337 337 'page' => null, 338 338 'user_id' => 0, … … class BP_Groups_Group { 341 341 'include' => false, 342 342 'populate_extras' => true, 343 343 'exclude' => false, 344 'show_hidden' => false 344 'show_hidden' => false, 345 'orderby' => 'date_created', 346 'order' => 'DESC', 345 347 ); 346 348 347 349 $r = wp_parse_args( $args, $defaults ); … … class BP_Groups_Group { 400 402 $sql['exclude'] = " AND g.id NOT IN ({$exclude})"; 401 403 } 402 404 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, fall back on defaults 416 if ( ! empty( $order_orderby['order'] ) ) { 417 $order = $order_orderby['order']; 418 } 419 420 if ( ! empty( $order_orderby['orderby'] ) ) { 421 $orderby = $order_orderby['orderby']; 422 } 423 } 424 425 // Sanitize 'order' 426 $order = bp_esc_sql_order( $order ); 427 428 // Convert 'orderby' into the proper ORDER BY term 429 $orderby = self::convert_orderby_to_order_by_term( $orderby ); 430 431 // Random order is a special case 432 if ( 'rand()' === $orderby ) { 433 $sql[] = "ORDER BY rand()"; 434 } else { 435 $sql[] = "ORDER BY {$orderby} {$order}"; 420 436 } 421 437 422 438 if ( ! empty( $r['per_page'] ) && ! empty( $r['page'] ) ) { … … class BP_Groups_Group { 497 513 * WP_Query, we have to alter the return value (stripping the leading 498 514 * AND keyword from the 'where' clause). 499 515 * 500 * @since 1.8 516 * @since BuddyPress (1.8) 517 * @access protected 501 518 * 502 519 * @param array $meta_query An array of meta_query filters. See the 503 520 * documentation for WP_Meta_Query for details. 504 521 * @return array $sql_array 'join' and 'where' clauses 505 522 */ 506 p ublicstatic function get_meta_query_sql( $meta_query = array() ) {523 protected static function get_meta_query_sql( $meta_query = array() ) { 507 524 global $wpdb; 508 525 509 526 $sql_array = array( … … class BP_Groups_Group { 539 556 return $sql_array; 540 557 } 541 558 559 /** 560 * Convert the 'type' parameter to 'order' and 'orderby' 561 * 562 * @since BuddyPress (1.8) 563 * @access protected 564 * @param string $type The 'type' shorthand param 565 * @return array 'order' and 'orderby' 566 */ 567 protected function convert_type_to_order_orderby( $type = '' ) { 568 $order = $orderby = ''; 569 570 switch ( $type ) { 571 case 'newest' : 572 $order = 'DESC'; 573 $orderby = 'date_created'; 574 break; 575 576 case 'active' : 577 $order = 'DESC'; 578 $orderby = 'last_activity'; 579 break; 580 581 case 'popular' : 582 $order = 'DESC'; 583 $orderby = 'total_member_count'; 584 break; 585 586 case 'alphabetical' : 587 $order = 'ASC'; 588 $orderby = 'name'; 589 break; 590 591 case 'random' : 592 $order = 'DESC'; 593 $orderby = 'random'; 594 break; 595 } 596 597 return array( 'order' => $order, 'orderby' => $orderby ); 598 } 599 600 /** 601 * Convert the 'orderby' param to get() into a proper SQL term/column 602 * 603 * @since BuddyPress (1.8) 604 * @access protected 605 * @param string $orderby 606 * @return string $order_by_term 607 */ 608 protected function convert_orderby_to_order_by_term( $orderby ) { 609 $order_by_term = ''; 610 611 switch ( $orderby ) { 612 case 'date_created' : 613 default : 614 $order_by_term = 'g.date_created'; 615 break; 616 617 case 'last_activity' : 618 $order_by_term = 'last_activity'; 619 break; 620 621 case 'total_group_members' : 622 $order_by_term = 'CONVERT(gm1.meta_value, SIGNED)'; 623 break; 624 625 case 'name' : 626 $order_by_term = 'g.name'; 627 break; 628 629 case 'random' : 630 $order_by_term = 'rand()'; 631 break; 632 } 633 634 return $order_by_term; 635 } 542 636 543 637 function get_by_most_forum_topics( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) { 544 638 global $wpdb, $bp, $bbdb; -
bp-groups/bp-groups-functions.php
diff --git bp-groups/bp-groups-functions.php bp-groups/bp-groups-functions.php index f56567a..d53502b 100644
function groups_get_total_member_count( $group_id ) { 413 413 function groups_get_groups( $args = '' ) { 414 414 415 415 $defaults = array( 416 'type' => 'active', // active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts416 'type' => false, // active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts 417 417 'user_id' => false, // Pass a user_id to limit to only groups that this user is a member of 418 418 'include' => false, // Only include these specific groups (group_ids) 419 419 'exclude' => false, // Do not include these specific groups (group_ids) … … function groups_get_groups( $args = '' ) { 423 423 'per_page' => 20, // The number of results to return per page 424 424 'page' => 1, // The page to return if limiting per page 425 425 'populate_extras' => true, // Fetch meta such as is_banned and is_member 426 'order' => 'DESC', // 'ASC' or 'DESC' 427 'orderby' => 'date_created' // date_created, last_activity, total_member_count, name, random 426 428 ); 427 429 428 430 $r = wp_parse_args( $args, $defaults ); … … function groups_get_groups( $args = '' ) { 437 439 'show_hidden' => $r['show_hidden'], 438 440 'per_page' => $r['per_page'], 439 441 'page' => $r['page'], 440 'populate_extras' => $r['populate_extras'] 442 'populate_extras' => $r['populate_extras'], 443 'order' => $r['order'], 444 'orderby' => $r['orderby'], 441 445 ) ); 442 446 443 447 return apply_filters_ref_array( 'groups_get_groups', array( &$groups, &$r ) ); -
bp-groups/bp-groups-template.php
diff --git bp-groups/bp-groups-template.php bp-groups/bp-groups-template.php index 9be763f..656d91b 100644
class BP_Groups_Template { 166 166 } else { 167 167 $this->groups = groups_get_groups( array( 168 168 'type' => $type, 169 'order' => $order, 170 'orderby' => $orderby, 169 171 'per_page' => $this->pag_num, 170 172 'page' => $this->pag_page, 171 173 'user_id' => $user_id, … … class BP_Groups_Template { 266 268 } 267 269 } 268 270 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. To use 'order' and 'orderby' 278 * parameters, pass null for 'type'. 279 * 280 * @param array $args 281 * @return bool True if there are groups to display that match the params 282 */ 269 283 function bp_has_groups( $args = '' ) { 270 284 global $groups_template, $bp; 271 285 … … function bp_has_groups( $args = '' ) { 275 289 * pass their parameters directly to the loop. 276 290 */ 277 291 $slug = false; 278 $type = ' active';292 $type = ''; 279 293 $user_id = 0; 280 294 $order = ''; 281 295 … … function bp_has_groups( $args = '' ) { 284 298 $user_id = bp_displayed_user_id(); 285 299 286 300 // Type 301 // @todo What is $order? At some point it was removed incompletely? 287 302 if ( bp_is_current_action( 'my-groups' ) ) { 288 303 if ( 'most-popular' == $order ) { 289 304 $type = 'popular'; … … function bp_has_groups( $args = '' ) { 298 313 } 299 314 300 315 $defaults = array( 301 'type' => $type, 316 'type' => $type, // 'type' is an override for 'order' and 'orderby'. See docblock. 317 'order' => 'DESC', 318 'orderby' => 'last_activity', 302 319 'page' => 1, 303 320 'per_page' => 20, 304 321 'max' => false, … … function bp_has_groups( $args = '' ) { 313 330 'include' => false, // Pass comma separated list or array of group ID's to return only these groups 314 331 'exclude' => false, // Pass comma separated list or array of group ID's to exclude these groups 315 332 316 'populate_extras' => true // Get extra meta - is_member, is_banned333 'populate_extras' => true, // Get extra meta - is_member, is_banned 317 334 ); 318 335 319 336 $r = wp_parse_args( $args, $defaults ); … … function bp_has_groups( $args = '' ) { 329 346 330 347 $groups_template = new BP_Groups_Template( array( 331 348 'type' => $r['type'], 349 'order' => $r['order'], 350 'orderby' => $r['orderby'], 332 351 'page' => (int) $r['page'], 333 352 'per_page' => (int) $r['per_page'], 334 353 'max' => (int) $r['max'], -
tests/includes/factory.php
diff --git tests/includes/factory.php tests/includes/factory.php index baf5f47..98a6d19 100644
class BP_UnitTest_Factory_For_Group extends WP_UnitTest_Factory_For_Thing { 73 73 $group_id = groups_create_group( $args ); 74 74 75 75 groups_update_groupmeta( $group_id, 'total_member_count', 1 ); 76 groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() ); 76 77 $last_activity = isset( $args['last_activity'] ) ? $args['last_activity'] : bp_core_current_time(); 78 groups_update_groupmeta( $group_id, 'last_activity', $last_activity ); 77 79 78 80 return $group_id; 79 81 } -
tests/testcases/groups/class-bp-groups-group.php
diff --git tests/testcases/groups/class-bp-groups-group.php tests/testcases/groups/class-bp-groups-group.php index 2893e1b..33dc20c 100644
class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 83 83 $this->assertEquals( $ids, array( $g1 ) ); 84 84 } 85 85 86 /** 87 * @group get 88 */ 86 89 public function test_get_empty_meta_query() { 87 90 $g1 = $this->factory->group->create(); 88 91 $g2 = $this->factory->group->create(); … … class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 113 116 $this->assertEquals( array( $g1 ), $found ); 114 117 } 115 118 119 /** 120 * @group get 121 */ 116 122 public function test_get_search_with_underscores() { 117 123 $g1 = $this->factory->group->create( array( 118 124 'name' => 'Cool Group', … … class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 165 171 $this->assertEquals( array( $g1 ), $found ); 166 172 } 167 173 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' => 'DESC', 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 168 348 public function test_filter_user_groups_normal_search() { 169 349 $g1 = $this->factory->group->create( array( 170 350 'name' => 'Cool Group', … … class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 351 531 $this->assertEquals( array( $g1 ), $found ); 352 532 } 353 533 } 534 535 /** 536 * Stub class for accessing protected methods 537 */ 538 class _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 } -
tests/testcases/groups/template.php
diff --git tests/testcases/groups/template.php tests/testcases/groups/template.php index a3e3260..c7511c5 100644
class BP_Tests_Groups_Template extends BP_UnitTestCase { 11 11 parent::tearDown(); 12 12 } 13 13 14 /** 15 * Integration test to make sure meta_query is getting passed through 16 * 17 * @group bp_has_groups 18 */ 14 19 public function test_bp_has_groups_with_meta_query() { 15 20 $g1 = $this->factory->group->create(); 16 21 $g2 = $this->factory->group->create(); … … class BP_Tests_Groups_Template extends BP_UnitTestCase { 29 34 $ids = wp_list_pluck( $groups_template->groups, 'id' ); 30 35 $this->assertEquals( $ids, array( $g1, ) ); 31 36 } 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 32 100 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)