Changeset 11383
- Timestamp:
- 01/14/2017 01:32:34 AM (8 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-groups/bp-groups-functions.php
r11363 r11383 710 710 'parent_id' => null, // Get groups that are children of the specified group(s). 711 711 'search_terms' => false, // Limit to groups that match these search terms. 712 'search_columns' => array(), // Select which columns to search. 712 713 'group_type' => '', // Array or comma-separated list of group types to limit results to. 713 714 'group_type__in' => '', // Array or comma-separated list of group types to limit results to. … … 730 731 'parent_id' => $r['parent_id'], 731 732 'search_terms' => $r['search_terms'], 733 'search_columns' => $r['search_columns'], 732 734 'group_type' => $r['group_type'], 733 735 'group_type__in' => $r['group_type__in'], -
trunk/src/bp-groups/classes/class-bp-groups-group.php
r11363 r11383 883 883 * @type int $user_id Optional. If provided, results will be limited to groups 884 884 * of which the specified user is a member. Default: null. 885 * @type string $search_terms Optional. If provided, only groups whose names or descriptions 886 * match the search terms will be returned. Default: false. 885 * @type string $search_terms Optional. If provided, only groups whose names or descriptions 886 * match the search terms will be returned. Allows specifying the 887 * wildcard position using a '*' character before or after the 888 * string or both. Works in concert with $search_columns. 889 * Default: false. 890 * @type string $search_columns Optional. If provided, only apply the search terms to the 891 * specified columns. Works in concert with $search_terms. 892 * Default: empty array. 887 893 * @type array|string $group_type Array or comma-separated list of group types to limit results to. 888 894 * @type array|string $group_type__in Array or comma-separated list of group types to limit results to. … … 940 946 'user_id' => 0, 941 947 'search_terms' => false, 948 'search_columns' => array(), 942 949 'group_type' => '', 943 950 'group_type__in' => '', … … 974 981 } 975 982 976 if ( ! empty( $r['search_terms'] ) ) { 977 $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%'; 978 $where_conditions['search'] = $wpdb->prepare( "( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like ); 983 $search = ''; 984 if ( isset( $r['search_terms'] ) ) { 985 $search = trim( $r['search_terms'] ); 986 } 987 988 if ( $search ) { 989 $leading_wild = ( ltrim( $search, '*' ) != $search ); 990 $trailing_wild = ( rtrim( $search, '*' ) != $search ); 991 if ( $leading_wild && $trailing_wild ) { 992 $wild = 'both'; 993 } elseif ( $leading_wild ) { 994 $wild = 'leading'; 995 } elseif ( $trailing_wild ) { 996 $wild = 'trailing'; 997 } else { 998 // Default is to wrap in wildcard characters. 999 $wild = 'both'; 1000 } 1001 $search = trim( $search, '*' ); 1002 1003 $searches = array(); 1004 $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; 1005 $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : ''; 1006 $wildcarded = $leading_wild . bp_esc_like( $search ) . $trailing_wild; 1007 1008 $search_columns = array( 'name', 'description' ); 1009 if ( $r['search_columns'] ) { 1010 $search_columns = array_intersect( $r['search_columns'], $search_columns ); 1011 } 1012 1013 foreach ( $search_columns as $search_column ) { 1014 $searches[] = $wpdb->prepare( "$search_column LIKE %s", $wildcarded ); 1015 } 1016 1017 $where_conditions['search'] = '(' . implode(' OR ', $searches) . ')'; 979 1018 } 980 1019 -
trunk/src/bp-groups/classes/class-bp-groups-template.php
r11363 r11383 167 167 'parent_id' => null, 168 168 'search_terms' => '', 169 'search_columns' => array(), 169 170 'group_type' => '', 170 171 'group_type__in' => '', … … 218 219 'user_id' => $user_id, 219 220 'search_terms' => $search_terms, 221 'search_columns' => $search_columns, 220 222 'meta_query' => $meta_query, 221 223 'group_type' => $group_type, -
trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php
r11104 r11383 307 307 308 308 /** 309 * @group get 310 */ 311 public function test_get_search_with_left_wildcard() { 312 $g1 = $this->factory->group->create( array( 313 'name' => 'Ye Lads', 314 'description' => "My Bonnie lies over the ocean", 315 ) ); 316 $g2 = $this->factory->group->create(); 317 318 $groups = BP_Groups_Group::get( array( 319 'search_terms' => "*ads", 320 ) ); 321 322 $found = wp_list_pluck( $groups['groups'], 'id' ); 323 324 $this->assertEquals( array( $g1 ), $found ); 325 } 326 327 /** 328 * @group get 329 */ 330 public function test_get_search_with_left_wildcard_should_miss() { 331 $g1 = $this->factory->group->create( array( 332 'name' => 'Ye Lads', 333 'description' => "My Bonnie lies over the ocean", 334 ) ); 335 $g2 = $this->factory->group->create(); 336 337 $groups = BP_Groups_Group::get( array( 338 'search_terms' => "*la", 339 ) ); 340 341 $found = wp_list_pluck( $groups['groups'], 'id' ); 342 343 $this->assertEquals( array(), $found ); 344 } 345 346 /** 347 * @group get 348 */ 349 public function test_get_search_with_right_wildcard() { 350 $g1 = $this->factory->group->create( array( 351 'name' => 'Ye Lads', 352 'description' => "My Bonnie lies over the ocean", 353 ) ); 354 $g2 = $this->factory->group->create(); 355 356 $groups = BP_Groups_Group::get( array( 357 'search_terms' => "Ye*", 358 ) ); 359 360 $found = wp_list_pluck( $groups['groups'], 'id' ); 361 362 $this->assertEquals( array( $g1 ), $found ); 363 } 364 365 /** 366 * @group get 367 */ 368 public function test_get_search_with_right_wildcard_should_miss() { 369 $g1 = $this->factory->group->create( array( 370 'name' => 'Ye Lads', 371 'description' => "My Bonnie lies over the ocean", 372 ) ); 373 $g2 = $this->factory->group->create(); 374 375 $groups = BP_Groups_Group::get( array( 376 'search_terms' => "la*", 377 ) ); 378 379 $found = wp_list_pluck( $groups['groups'], 'id' ); 380 381 $this->assertEquals( array(), $found ); 382 } 383 384 /** 385 * @group get 386 */ 387 public function test_get_search_with_both_wildcard() { 388 $g1 = $this->factory->group->create( array( 389 'name' => 'Ye Lads', 390 'description' => "My Bonnie lies over the ocean", 391 ) ); 392 $g2 = $this->factory->group->create(); 393 394 $groups = BP_Groups_Group::get( array( 395 'search_terms' => "*la*", 396 ) ); 397 398 $found = wp_list_pluck( $groups['groups'], 'id' ); 399 400 $this->assertEquals( array( $g1 ), $found ); 401 } 402 403 /** 404 * @group get 405 */ 406 public function test_get_search_limited_to_name_column() { 407 $g1 = $this->factory->group->create( array( 408 'name' => 'Ye Lads', 409 'description' => "My Bonnie lies over the ocean", 410 ) ); 411 $g2 = $this->factory->group->create(); 412 $g3 = $this->factory->group->create( array( 413 'name' => 'Bonnie Lasses', 414 'description' => "That lad is unknown to me", 415 ) ); 416 417 $groups = BP_Groups_Group::get( array( 418 'search_terms' => "lad", 419 'search_columns' => array( 'name' ), 420 ) ); 421 422 $found = wp_list_pluck( $groups['groups'], 'id' ); 423 424 $this->assertEquals( array( $g1 ), $found ); 425 } 426 427 /** 428 * @group get 429 */ 430 public function test_get_search_limited_to_description_column() { 431 $g1 = $this->factory->group->create( array( 432 'name' => 'Ye Lads', 433 'description' => "My Bonnie lies over the ocean", 434 ) ); 435 $g2 = $this->factory->group->create(); 436 $g3 = $this->factory->group->create( array( 437 'name' => 'Bonnie Lasses', 438 'description' => "That lad is unknown to me", 439 ) ); 440 441 $groups = BP_Groups_Group::get( array( 442 'search_terms' => "lad", 443 'search_columns' => array( 'description' ), 444 ) ); 445 446 $found = wp_list_pluck( $groups['groups'], 'id' ); 447 448 $this->assertEquals( array( $g3 ), $found ); 449 } 450 451 /** 309 452 * BP 1.8 will change the default 'type' param in favor of default 310 453 * 'order' and 'orderby'. This is to make sure that existing plugins
Note: See TracChangeset
for help on using the changeset viewer.