diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php
index 46d6562..b8ef8ce 100644
|
|
|
class BP_Groups_Group { |
| 619 | 619 | public static function group_exists( $slug, $table_name = false ) { |
| 620 | 620 | global $wpdb; |
| 621 | 621 | |
| 622 | | if ( empty( $table_name ) ) |
| 623 | | $table_name = buddypress()->groups->table_name; |
| 624 | | |
| 625 | | if ( empty( $slug ) ) |
| | 622 | if ( empty( $slug ) ) { |
| 626 | 623 | return false; |
| | 624 | } |
| 627 | 625 | |
| 628 | | $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE slug = %s", strtolower( $slug ) ) ); |
| | 626 | if ( ! $table_name ) { |
| | 627 | $table_name = buddypress()->groups->table_name; |
| | 628 | } |
| | 629 | |
| | 630 | $group_exists_sql = $wpdb->prepare( "SELECT id FROM {$table_name} WHERE slug = %s", strtolower( $slug ) ); |
| | 631 | $cached = bp_core_get_incremented_cache( $group_exists_sql, 'bp_groups' ); |
| | 632 | if ( false === $cached ) { |
| | 633 | $group_id = $wpdb->get_var( $group_exists_sql ); |
| | 634 | bp_core_set_incremented_cache( $group_exists_sql, 'bp_groups', $group_id ); |
| | 635 | } else { |
| | 636 | $group_id = $cached; |
| | 637 | } |
| 629 | 638 | |
| 630 | | return is_numeric( $query ) ? (int) $query : $query; |
| | 639 | return is_numeric( $group_id ) ? (int) $group_id : $group_id; |
| 631 | 640 | } |
| 632 | 641 | |
| 633 | 642 | /** |
diff --git tests/phpunit/testcases/groups/cache.php tests/phpunit/testcases/groups/cache.php
index 1653b7a..42ee19d 100644
|
|
|
class BP_Tests_Group_Cache extends BP_UnitTestCase { |
| 259 | 259 | // check if function references cache or hits the DB by comparing query count |
| 260 | 260 | $this->assertEquals( $first_query_count, $wpdb->num_queries ); |
| 261 | 261 | } |
| | 262 | |
| | 263 | /** |
| | 264 | * @group group_exists |
| | 265 | */ |
| | 266 | public function test_group_exists_uses_cache() { |
| | 267 | global $wpdb; |
| | 268 | |
| | 269 | $g1 = $this->factory->group->create( array( |
| | 270 | 'slug' => 'sam', |
| | 271 | ) ); |
| | 272 | |
| | 273 | $group_id = BP_Groups_Group::group_exists( 'sam' ); |
| | 274 | $first_query_count = $wpdb->num_queries; |
| | 275 | $group_id_again = BP_Groups_Group::group_exists( 'sam' ); |
| | 276 | |
| | 277 | $this->assertEquals( $first_query_count, $wpdb->num_queries ); |
| | 278 | } |
| | 279 | |
| | 280 | /** |
| | 281 | * @group group_exists |
| | 282 | */ |
| | 283 | public function test_group_exists_uses_cache_no_match() { |
| | 284 | global $wpdb; |
| | 285 | |
| | 286 | $group_id = BP_Groups_Group::group_exists( 'brandywine-river' ); |
| | 287 | $first_query_count = $wpdb->num_queries; |
| | 288 | $group_id_again = BP_Groups_Group::group_exists( 'brandywine-river' ); |
| | 289 | |
| | 290 | $this->assertEquals( $first_query_count, $wpdb->num_queries ); |
| | 291 | } |
| 262 | 292 | } |
diff --git tests/phpunit/testcases/groups/class-bp-groups-group.php tests/phpunit/testcases/groups/class-bp-groups-group.php
index 6f6d625..d5c41bd 100644
|
|
|
class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { |
| 2011 | 2011 | $found = wp_list_pluck( $groups['groups'], 'id' ); |
| 2012 | 2012 | $this->assertEqualSets( array( $g1, $g4 ), $found ); |
| 2013 | 2013 | } |
| | 2014 | |
| | 2015 | /** |
| | 2016 | * @group group_exists |
| | 2017 | */ |
| | 2018 | public function test_group_exists_expected_behavior() { |
| | 2019 | $g1 = $this->factory->group->create( array( |
| | 2020 | 'slug' => 'sam', |
| | 2021 | ) ); |
| | 2022 | |
| | 2023 | $group_id = BP_Groups_Group::group_exists( 'sam' ); |
| | 2024 | |
| | 2025 | $this->assertEquals( $g1, $group_id ); |
| | 2026 | } |
| | 2027 | |
| | 2028 | /** |
| | 2029 | * @group group_exists |
| | 2030 | */ |
| | 2031 | public function test_group_exists_expected_behavior_no_match() { |
| | 2032 | $group_id = BP_Groups_Group::group_exists( 'brandywine-river' ); |
| | 2033 | |
| | 2034 | $this->assertNull( $group_id ); |
| | 2035 | } |
| 2014 | 2036 | } |
| 2015 | 2037 | |
| 2016 | 2038 | /** |