Ticket #3961: 3961.06.patch
File 3961.06.patch, 16.5 KB (added by , 8 years ago) |
---|
-
src/bp-core/admin/bp-core-admin-schema.php
diff --git src/bp-core/admin/bp-core-admin-schema.php src/bp-core/admin/bp-core-admin-schema.php index 6f9043a..44506bf 100644
function bp_core_install_groups() { 202 202 slug varchar(200) NOT NULL, 203 203 description longtext NOT NULL, 204 204 status varchar(10) NOT NULL DEFAULT 'public', 205 parent_id bigint(20) NOT NULL DEFAULT 0, 205 206 enable_forum tinyint(1) NOT NULL DEFAULT '1', 206 207 date_created datetime NOT NULL, 207 208 KEY creator_id (creator_id), 208 KEY status (status) 209 KEY status (status), 210 KEY parent_id (parent_id) 209 211 ) {$charset_collate};"; 210 212 211 213 $sql[] = "CREATE TABLE {$bp_prefix}bp_groups_members ( -
src/bp-core/bp-core-update.php
diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php index 16608a4..ce872aa 100644
function bp_update_to_2_5() { 508 508 * 2.7.0 update routine. 509 509 * 510 510 * - Add email unsubscribe salt. 511 * - Add `parent_id` column to groups table. 511 512 * 512 513 * @since 2.7.0 513 514 */ 514 515 function bp_update_to_2_7() { 515 516 bp_add_option( 'bp-emails-unsubscribe-salt', base64_encode( wp_generate_password( 64, true, true ) ) ); 517 518 /* 519 * Also handled by `bp_core_install()`. 520 * Add `parent_id` column to groups table. 521 */ 522 if ( bp_is_active( 'groups' ) ) { 523 bp_core_install_groups(); 524 525 // Invalidate all cached group objects. 526 global $wpdb; 527 $bp = buddypress(); 528 529 $group_ids = $wpdb->get_col( "SELECT id FROM {$bp->groups->table_name}" ); 530 foreach ( $group_ids as $group_id ) { 531 wp_cache_delete( $group_id, 'bp_groups' ); 532 } 533 } 516 534 } 517 535 518 536 /** -
src/bp-groups/bp-groups-actions.php
diff --git src/bp-groups/bp-groups-actions.php src/bp-groups/bp-groups-actions.php index d8152a2..f3eab83 100644
function groups_action_group_feed() { 566 566 ) ); 567 567 } 568 568 add_action( 'bp_actions', 'groups_action_group_feed' ); 569 570 /** 571 * Update orphaned child groups when the parent is deleted. 572 * 573 * @since 2.7.0 574 * 575 * @param BP_Groups_Group $group Instance of the group item being deleted. 576 */ 577 function bp_groups_update_orphaned_groups_on_group_delete( $group ) { 578 // Get child groups and set the parent to the deleted parent's parent. 579 $grandparent_group_id = ! empty( $group->parent_id ) ? $group->parent_id : 0; 580 $child_args = array( 581 'parent_id' => $group->id, 582 'show_hidden' => true, 583 'per_page' => false, 584 'populate_extras' => false, 585 'update_meta_cache' => false, 586 ); 587 $children = groups_get_groups( $child_args ); 588 $children = $children['groups']; 589 590 foreach ( $children as $cgroup ) { 591 // Create the BP_Groups_Group object. 592 $child_group = groups_get_group( array( 'group_id' => $cgroup->id ) ); 593 $child_group->parent_id = $grandparent_group_id; 594 $child_group->save(); 595 } 596 } 597 add_action( 'bp_groups_delete_group', 'bp_groups_update_orphaned_groups_on_group_delete', 10, 2 ); 598 No newline at end of file -
src/bp-groups/bp-groups-functions.php
diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php index e8337e1..b673faa 100644
function groups_get_group( $args = '' ) { 87 87 * @type string $slug The group slug. 88 88 * @type string $status The group's status. Accepts 'public', 'private' or 89 89 * 'hidden'. Defaults to 'public'. 90 * @type int $parent_id The ID of the parent group. Default: 0. 90 91 * @type int $enable_forum Optional. Whether the group has a forum enabled. 91 92 * If the legacy forums are enabled for this group 92 93 * or if a bbPress forum is enabled for the group, … … function groups_create_group( $args = '' ) { 105 106 'description' => '', 106 107 'slug' => '', 107 108 'status' => 'public', 109 'parent_id' => 0, 108 110 'enable_forum' => 0, 109 111 'date_created' => bp_core_current_time() 110 112 ); … … function groups_create_group( $args = '' ) { 151 153 $group->description = $description; 152 154 $group->slug = $slug; 153 155 $group->status = $status; 156 $group->parent_id = $parent_id; 154 157 $group->enable_forum = (int) $enable_forum; 155 158 $group->date_created = $date_created; 156 159 … … function groups_edit_base_group_details( $group_id, $group_name, $group_desc, $n 270 273 * to the group. 'members', 'mods', or 'admins'. 271 274 * @return bool True on success, false on failure. 272 275 */ 273 function groups_edit_group_settings( $group_id, $enable_forum, $status, $invite_status = false ) {276 function groups_edit_group_settings( $group_id, $enable_forum, $status, $invite_status = false, $parent_id = false ) { 274 277 275 278 $group = groups_get_group( array( 'group_id' => $group_id ) ); 276 279 $group->enable_forum = $enable_forum; … … function groups_edit_group_settings( $group_id, $enable_forum, $status, $invite_ 285 288 // Now update the status. 286 289 $group->status = $status; 287 290 291 // Update the parent ID if necessary. 292 if ( false !== $parent_id ) { 293 $group->parent_id = $parent_id; 294 } 295 288 296 if ( !$group->save() ) 289 297 return false; 290 298 … … function groups_get_groups( $args = '' ) { 703 711 'user_id' => false, // Pass a user_id to limit to only groups that this user is a member of. 704 712 'include' => false, // Only include these specific groups (group_ids). 705 713 'exclude' => false, // Do not include these specific groups (group_ids). 714 'parent_id' => false, // Get groups that are children of the specified group(s). 706 715 'search_terms' => false, // Limit to groups that match these search terms. 707 716 'group_type' => '', 708 717 'group_type__in' => '', … … function groups_get_groups( $args = '' ) { 722 731 'user_id' => $r['user_id'], 723 732 'include' => $r['include'], 724 733 'exclude' => $r['exclude'], 734 'parent_id' => $r['parent_id'], 725 735 'search_terms' => $r['search_terms'], 726 736 'group_type' => $r['group_type'], 727 737 'group_type__in' => $r['group_type__in'], -
src/bp-groups/bp-groups-template.php
diff --git src/bp-groups/bp-groups-template.php src/bp-groups/bp-groups-template.php index 4fe3a61..64502f8 100644
function bp_groups_directory_permalink() { 137 137 * about groups. Default: true. 138 138 * @type array|string $exclude Array or comma-separated list of group IDs. Results will exclude 139 139 * the listed groups. Default: false. 140 * @type array|string $parent_id Array or comma-separated list of group IDs. Results will include only 141 * child groups of the listed groups. Default: false. 140 142 * @type bool $update_meta_cache Whether to fetch groupmeta for queried groups. Default: true. 141 143 * } 142 144 * @return bool True if there are groups to display that match the params … … function bp_has_groups( $args = '' ) { 150 152 $slug = false; 151 153 $type = ''; 152 154 $search_terms = false; 155 $parent_id = false; 153 156 154 157 // When looking your own groups, check for two action variables. 155 158 if ( bp_is_current_action( 'my-groups' ) ) { … … function bp_has_groups( $args = '' ) { 198 201 'meta_query' => false, 199 202 'include' => false, 200 203 'exclude' => false, 204 'parent_id' => $parent_id, 201 205 'populate_extras' => true, 202 206 'update_meta_cache' => true, 203 207 ), 'has_groups' ); … … function bp_has_groups( $args = '' ) { 221 225 'meta_query' => $r['meta_query'], 222 226 'include' => $r['include'], 223 227 'exclude' => $r['exclude'], 228 'parent_id' => $r['parent_id'], 224 229 'populate_extras' => (bool) $r['populate_extras'], 225 230 'update_meta_cache' => (bool) $r['update_meta_cache'], 226 231 ) ); -
src/bp-groups/classes/class-bp-groups-group.php
diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php index c763f6e..6cadc4a 100644
class BP_Groups_Group { 68 68 public $status; 69 69 70 70 /** 71 * Parent ID. 72 * 73 * ID of parent group, if applicable. 74 * 75 * @since 2.7.0 76 * @var int 77 */ 78 public $parent_id; 79 80 /** 71 81 * Should (legacy) bbPress forums be enabled for this group? 72 82 * 73 83 * @since 1.6.0 … … class BP_Groups_Group { 214 224 $this->slug = $group->slug; 215 225 $this->description = stripslashes( $group->description ); 216 226 $this->status = $group->status; 227 $this->parent_id = $group->parent_id; 217 228 $this->enable_forum = (int) $group->enable_forum; 218 229 $this->date_created = $group->date_created; 219 230 … … class BP_Groups_Group { 281 292 $this->slug = apply_filters( 'groups_group_slug_before_save', $this->slug, $this->id ); 282 293 $this->description = apply_filters( 'groups_group_description_before_save', $this->description, $this->id ); 283 294 $this->status = apply_filters( 'groups_group_status_before_save', $this->status, $this->id ); 295 $this->parent_id = apply_filters( 'groups_group_parent_id_before_save', $this->parent_id, $this->id ); 284 296 $this->enable_forum = apply_filters( 'groups_group_enable_forum_before_save', $this->enable_forum, $this->id ); 285 297 $this->date_created = apply_filters( 'groups_group_date_created_before_save', $this->date_created, $this->id ); 286 298 … … class BP_Groups_Group { 323 335 slug = %s, 324 336 description = %s, 325 337 status = %s, 338 parent_id = %d, 326 339 enable_forum = %d, 327 340 date_created = %s 328 341 WHERE … … class BP_Groups_Group { 333 346 $this->slug, 334 347 $this->description, 335 348 $this->status, 349 $this->parent_id, 336 350 $this->enable_forum, 337 351 $this->date_created, 338 352 $this->id … … class BP_Groups_Group { 345 359 slug, 346 360 description, 347 361 status, 362 parent_id, 348 363 enable_forum, 349 364 date_created 350 365 ) VALUES ( 351 %d, %s, %s, %s, %s, %d, % s366 %d, %s, %s, %s, %s, %d, %d, %s 352 367 )", 353 368 $this->creator_id, 354 369 $this->name, 355 370 $this->slug, 356 371 $this->description, 357 372 $this->status, 373 $this->parent_id, 358 374 $this->enable_forum, 359 375 $this->date_created 360 376 ); … … class BP_Groups_Group { 754 770 * See {@link WP_Meta_Query::queries} for description. 755 771 * @type array|string $value Optional. Array or comma-separated list of group IDs. Results 756 772 * will be limited to groups within the list. Default: false. 773 * @type array|string $parent_id Optional. Array or comma-separated list of group IDs. Results 774 * will be limited to children of the specified groups. Default: false. 757 775 * @type bool $populate_extras Whether to fetch additional information 758 776 * (such as member count) about groups. Default: true. 759 777 * @type array|string $exclude Optional. Array or comma-separated list of group IDs. … … class BP_Groups_Group { 805 823 'group_type__not_in' => '', 806 824 'meta_query' => false, 807 825 'include' => false, 826 'parent_id' => false, 808 827 'populate_extras' => true, 809 828 'update_meta_cache' => true, 810 829 'exclude' => false, … … class BP_Groups_Group { 875 894 $where_conditions['include'] = "g.id IN ({$include})"; 876 895 } 877 896 897 if ( ! empty( $r['parent_id'] ) ) { 898 $parent_id = implode( ',', wp_parse_id_list( $r['parent_id'] ) ); 899 $where_conditions['parent_id'] = "g.parent_id IN ({$parent_id})"; 900 } 901 878 902 if ( ! empty( $r['exclude'] ) ) { 879 903 $exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) ); 880 904 $where_conditions['exclude'] = "g.id NOT IN ({$exclude})"; -
src/bp-groups/classes/class-bp-groups-template.php
diff --git src/bp-groups/classes/class-bp-groups-template.php src/bp-groups/classes/class-bp-groups-template.php index 043d8f4..49e5eb6 100644
class BP_Groups_Template { 165 165 'slug' => false, 166 166 'include' => false, 167 167 'exclude' => false, 168 'parent_id' => false, 168 169 'search_terms' => '', 169 170 'group_type' => '', 170 171 'group_type__in' => '', … … class BP_Groups_Template { 226 227 'group_type__not_in' => $group_type__not_in, 227 228 'include' => $include, 228 229 'exclude' => $exclude, 230 'parent_id' => $parent_id, 229 231 'populate_extras' => $populate_extras, 230 232 'update_meta_cache' => $update_meta_cache, 231 233 'show_hidden' => $show_hidden, -
tests/phpunit/testcases/groups/class-bp-groups-group.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-group.php tests/phpunit/testcases/groups/class-bp-groups-group.php index 9c5e245..26cf14b 100644
class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 1518 1518 1519 1519 $this->assertEmpty( $groups['groups'] ); 1520 1520 } 1521 1522 /** 1523 * @group hierarchical_groups 1524 */ 1525 public function test_get_by_parent_id() { 1526 $g1 = $this->factory->group->create(); 1527 $g2 = $this->factory->group->create( array( 1528 'parent_id' => $g1, 1529 ) ); 1530 $g3 = $this->factory->group->create( array( 1531 'parent_id' => $g2, 1532 ) ); 1533 $g4 = $this->factory->group->create(); 1534 1535 $groups = BP_Groups_Group::get( array( 1536 'parent_id' => $g1, 1537 ) ); 1538 1539 $found = wp_list_pluck( $groups['groups'], 'id' ); 1540 $this->assertEquals( array( $g2 ), $found ); 1541 } 1542 1543 /** 1544 * @group hierarchical_groups 1545 */ 1546 public function test_get_by_parent_id_ignore_grandparent() { 1547 $g1 = $this->factory->group->create(); 1548 $g2 = $this->factory->group->create( array( 1549 'parent_id' => $g1, 1550 ) ); 1551 $g3 = $this->factory->group->create( array( 1552 'parent_id' => $g2, 1553 ) ); 1554 $g4 = $this->factory->group->create(); 1555 1556 $groups = BP_Groups_Group::get( array( 1557 'parent_id' => $g2, 1558 ) ); 1559 1560 $found = wp_list_pluck( $groups['groups'], 'id' ); 1561 $this->assertEquals( array( $g3 ), $found ); 1562 } 1563 1564 /** 1565 * @group hierarchical_groups 1566 */ 1567 public function test_get_by_parent_id_array() { 1568 $g1 = $this->factory->group->create(); 1569 $g2 = $this->factory->group->create( array( 1570 'parent_id' => $g1, 1571 ) ); 1572 $g3 = $this->factory->group->create( array( 1573 'parent_id' => $g2, 1574 ) ); 1575 $g4 = $this->factory->group->create(); 1576 1577 $groups = BP_Groups_Group::get( array( 1578 'parent_id' => array( $g1, $g2 ), 1579 ) ); 1580 1581 $found = wp_list_pluck( $groups['groups'], 'id' ); 1582 $this->assertEqualSets( array( $g2, $g3 ), $found ); 1583 } 1584 1585 /** 1586 * @group hierarchical_groups 1587 */ 1588 public function test_get_by_parent_id_comma_separated_string() { 1589 $g1 = $this->factory->group->create(); 1590 $g2 = $this->factory->group->create( array( 1591 'parent_id' => $g1, 1592 ) ); 1593 $g3 = $this->factory->group->create( array( 1594 'parent_id' => $g2, 1595 ) ); 1596 $g4 = $this->factory->group->create(); 1597 1598 $groups = BP_Groups_Group::get( array( 1599 'parent_id' => "$g1, $g2", 1600 ) ); 1601 1602 $found = wp_list_pluck( $groups['groups'], 'id' ); 1603 $this->assertEqualSets( array( $g2, $g3 ), $found ); 1604 } 1521 1605 } 1522 1606 1523 1607 /** -
tests/phpunit/testcases/groups/functions.php
diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php index 22f4c53..b97840f 100644
Bar!'; 664 664 groups_accept_invite( $u2, $g ); 665 665 $this->assertEquals( 0, groups_get_invite_count_for_user( $u2 ) ); 666 666 } 667 668 /** 669 * @group hierarchical_groups 670 */ 671 public function test_update_orphaned_groups_on_group_delete_top_level() { 672 $g1 = $this->factory->group->create(); 673 $g2 = $this->factory->group->create( array( 674 'parent_id' => $g1, 675 ) ); 676 677 groups_delete_group( $g1 ); 678 679 $child = groups_get_group( array( 'group_id' => $g2 ) ); 680 $this->assertEquals( 0, $child->parent_id ); 681 } 682 683 /** 684 * @group hierarchical_groups 685 */ 686 public function test_update_orphaned_groups_on_group_delete_two_levels() { 687 $g1 = $this->factory->group->create(); 688 $g2 = $this->factory->group->create( array( 689 'parent_id' => $g1, 690 ) ); 691 $g3 = $this->factory->group->create( array( 692 'parent_id' => $g2, 693 ) ); 694 695 groups_delete_group( $g2 ); 696 697 $child = groups_get_group( array( 'group_id' => $g3 ) ); 698 $this->assertEquals( $g1, $child->parent_id ); 699 } 667 700 }