Ticket #3961: 3961.04.patch
File 3961.04.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..c16a324 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 } 516 525 } 517 526 518 527 /** -
src/bp-groups/bp-groups-actions.php
diff --git src/bp-groups/bp-groups-actions.php src/bp-groups/bp-groups-actions.php index 4a3c510..faf065d 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 cb0753e..7ac4623 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 f08fea5..fa1403e 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 bde4f1e..f16b8c3 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 = $group->enable_forum; 218 229 $this->date_created = $group->date_created; 219 230 … … class BP_Groups_Group { 282 293 $this->slug = apply_filters( 'groups_group_slug_before_save', $this->slug, $this->id ); 283 294 $this->description = apply_filters( 'groups_group_description_before_save', $this->description, $this->id ); 284 295 $this->status = apply_filters( 'groups_group_status_before_save', $this->status, $this->id ); 296 $this->parent_id = apply_filters( 'groups_group_parent_id_before_save', $this->parent_id, $this->id ); 285 297 $this->enable_forum = apply_filters( 'groups_group_enable_forum_before_save', $this->enable_forum, $this->id ); 286 298 $this->date_created = apply_filters( 'groups_group_date_created_before_save', $this->date_created, $this->id ); 287 299 … … class BP_Groups_Group { 324 336 slug = %s, 325 337 description = %s, 326 338 status = %s, 339 parent_id = %d, 327 340 enable_forum = %d, 328 341 date_created = %s 329 342 WHERE … … class BP_Groups_Group { 334 347 $this->slug, 335 348 $this->description, 336 349 $this->status, 350 $this->parent_id, 337 351 $this->enable_forum, 338 352 $this->date_created, 339 353 $this->id … … class BP_Groups_Group { 346 360 slug, 347 361 description, 348 362 status, 363 parent_id, 349 364 enable_forum, 350 365 date_created 351 366 ) VALUES ( 352 %d, %s, %s, %s, %s, %d, % s367 %d, %s, %s, %s, %s, %d, %d, %s 353 368 )", 354 369 $this->creator_id, 355 370 $this->name, 356 371 $this->slug, 357 372 $this->description, 358 373 $this->status, 374 $this->parent_id, 359 375 $this->enable_forum, 360 376 $this->date_created 361 377 ); … … class BP_Groups_Group { 710 726 * See {@link WP_Meta_Query::queries} for description. 711 727 * @type array|string $value Optional. Array or comma-separated list of group IDs. Results 712 728 * will be limited to groups within the list. Default: false. 729 * @type array|string $parent_id Optional. Array or comma-separated list of group IDs. Results 730 * will be limited to children of the specified groups. Default: false. 713 731 * @type bool $populate_extras Whether to fetch additional information 714 732 * (such as member count) about groups. Default: true. 715 733 * @type array|string $exclude Optional. Array or comma-separated list of group IDs. … … class BP_Groups_Group { 761 779 'group_type__not_in' => '', 762 780 'meta_query' => false, 763 781 'include' => false, 782 'parent_id' => false, 764 783 'populate_extras' => true, 765 784 'update_meta_cache' => true, 766 785 'exclude' => false, … … class BP_Groups_Group { 835 854 $sql['include'] = " AND g.id IN ({$include})"; 836 855 } 837 856 857 if ( ! empty( $r['parent_id'] ) ) { 858 $parent_id = implode( ',', wp_parse_id_list( $r['parent_id'] ) ); 859 $sql['parent_id'] = " AND g.parent_id IN ({$parent_id})"; 860 } 861 838 862 if ( ! empty( $r['exclude'] ) ) { 839 863 $exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) ); 840 864 $sql['exclude'] = " AND g.id NOT IN ({$exclude})"; … … class BP_Groups_Group { 948 972 } 949 973 950 974 // Already escaped in the paginated results block. 975 if ( ! empty( $parent_id ) ) { 976 $total_sql['where'][] = "g.parent_id IN ({$parent_id})"; 977 } 978 979 // Already escaped in the paginated results block. 951 980 if ( ! empty( $exclude ) ) { 952 981 $total_sql['where'][] = "g.id NOT IN ({$exclude})"; 953 982 } -
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 d42959c..67948b4 100644
class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 1383 1383 1384 1384 $this->assertEmpty( $groups['groups'] ); 1385 1385 } 1386 1387 /** 1388 * @group hierarchical_groups 1389 */ 1390 public function test_get_by_parent_id() { 1391 $g1 = $this->factory->group->create(); 1392 $g2 = $this->factory->group->create( array( 1393 'parent_id' => $g1, 1394 ) ); 1395 $g3 = $this->factory->group->create( array( 1396 'parent_id' => $g2, 1397 ) ); 1398 $g4 = $this->factory->group->create(); 1399 1400 $groups = BP_Groups_Group::get( array( 1401 'parent_id' => $g1, 1402 ) ); 1403 1404 $found = wp_list_pluck( $groups['groups'], 'id' ); 1405 $this->assertEquals( array( $g2 ), $found ); 1406 } 1407 1408 /** 1409 * @group hierarchical_groups 1410 */ 1411 public function test_get_by_parent_id_ignore_grandparent() { 1412 $g1 = $this->factory->group->create(); 1413 $g2 = $this->factory->group->create( array( 1414 'parent_id' => $g1, 1415 ) ); 1416 $g3 = $this->factory->group->create( array( 1417 'parent_id' => $g2, 1418 ) ); 1419 $g4 = $this->factory->group->create(); 1420 1421 $groups = BP_Groups_Group::get( array( 1422 'parent_id' => $g2, 1423 ) ); 1424 1425 $found = wp_list_pluck( $groups['groups'], 'id' ); 1426 $this->assertEquals( array( $g3 ), $found ); 1427 } 1428 1429 /** 1430 * @group hierarchical_groups 1431 */ 1432 public function test_get_by_parent_id_array() { 1433 $g1 = $this->factory->group->create(); 1434 $g2 = $this->factory->group->create( array( 1435 'parent_id' => $g1, 1436 ) ); 1437 $g3 = $this->factory->group->create( array( 1438 'parent_id' => $g2, 1439 ) ); 1440 $g4 = $this->factory->group->create(); 1441 1442 $groups = BP_Groups_Group::get( array( 1443 'parent_id' => array( $g1, $g2 ), 1444 ) ); 1445 1446 $found = wp_list_pluck( $groups['groups'], 'id' ); 1447 $this->assertEqualSets( array( $g2, $g3 ), $found ); 1448 } 1449 1450 /** 1451 * @group hierarchical_groups 1452 */ 1453 public function test_get_by_parent_id_comma_separated_string() { 1454 $g1 = $this->factory->group->create(); 1455 $g2 = $this->factory->group->create( array( 1456 'parent_id' => $g1, 1457 ) ); 1458 $g3 = $this->factory->group->create( array( 1459 'parent_id' => $g2, 1460 ) ); 1461 $g4 = $this->factory->group->create(); 1462 1463 $groups = BP_Groups_Group::get( array( 1464 'parent_id' => "$g1, $g2", 1465 ) ); 1466 1467 $found = wp_list_pluck( $groups['groups'], 'id' ); 1468 $this->assertEqualSets( array( $g2, $g3 ), $found ); 1469 } 1386 1470 } 1387 1471 1388 1472 /** -
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 }