Ticket #3961: 3961.05.patch
File 3961.05.patch, 17.6 KB (added by , 8 years ago) |
---|
-
src/bp-core/admin/bp-core-admin-schema.php
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
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
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-activity.php
244 244 // rather than manually. 245 245 $uncached_ids = array(); 246 246 foreach ( $group_ids as $group_id ) { 247 if ( false === wp_cache_get( $group_id, 'bp_groups' ) ) { 247 $g = wp_cache_get( $group_id, 'bp_groups' ); 248 if ( false === $g || ! isset( $g->parent_id ) ) { 248 249 $uncached_ids[] = $group_id; 249 250 } 250 251 } -
src/bp-groups/bp-groups-functions.php
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, … … 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 ); … … 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 … … 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; … … 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 … … 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' => '', … … 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
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 … … 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' ) ) { … … 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' ); … … 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
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 … … 184 194 * Set up data about the current group. 185 195 * 186 196 * @since 1.6.0 197 * @since 2.7.0 Handle cache invalidation for new 'parent_id' column. 187 198 */ 188 199 public function populate() { 189 200 global $wpdb; … … 199 210 $group = $wpdb->get_row( $wpdb->prepare( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id = %d", $this->id ) ); 200 211 201 212 wp_cache_set( $this->id, $group, 'bp_groups' ); 213 214 // BP 2.7 - Older cache exists, so invalidate cache and refetch. 215 } elseif ( false !== $group && ! isset( $group->parent_id ) ) { 216 wp_cache_delete( $this->id, 'bp_groups' ); 217 $this->populate(); 218 return; 202 219 } 203 220 204 221 // No group found so set the ID and bail. … … 214 231 $this->slug = $group->slug; 215 232 $this->description = stripslashes( $group->description ); 216 233 $this->status = $group->status; 234 $this->parent_id = $group->parent_id; 217 235 $this->enable_forum = $group->enable_forum; 218 236 $this->date_created = $group->date_created; 219 237 … … 282 300 $this->slug = apply_filters( 'groups_group_slug_before_save', $this->slug, $this->id ); 283 301 $this->description = apply_filters( 'groups_group_description_before_save', $this->description, $this->id ); 284 302 $this->status = apply_filters( 'groups_group_status_before_save', $this->status, $this->id ); 303 $this->parent_id = apply_filters( 'groups_group_parent_id_before_save', $this->parent_id, $this->id ); 285 304 $this->enable_forum = apply_filters( 'groups_group_enable_forum_before_save', $this->enable_forum, $this->id ); 286 305 $this->date_created = apply_filters( 'groups_group_date_created_before_save', $this->date_created, $this->id ); 287 306 … … 324 343 slug = %s, 325 344 description = %s, 326 345 status = %s, 346 parent_id = %d, 327 347 enable_forum = %d, 328 348 date_created = %s 329 349 WHERE … … 334 354 $this->slug, 335 355 $this->description, 336 356 $this->status, 357 $this->parent_id, 337 358 $this->enable_forum, 338 359 $this->date_created, 339 360 $this->id … … 346 367 slug, 347 368 description, 348 369 status, 370 parent_id, 349 371 enable_forum, 350 372 date_created 351 373 ) VALUES ( 352 %d, %s, %s, %s, %s, %d, % s374 %d, %s, %s, %s, %s, %d, %d, %s 353 375 )", 354 376 $this->creator_id, 355 377 $this->name, 356 378 $this->slug, 357 379 $this->description, 358 380 $this->status, 381 $this->parent_id, 359 382 $this->enable_forum, 360 383 $this->date_created 361 384 ); … … 710 733 * See {@link WP_Meta_Query::queries} for description. 711 734 * @type array|string $value Optional. Array or comma-separated list of group IDs. Results 712 735 * will be limited to groups within the list. Default: false. 736 * @type array|string $parent_id Optional. Array or comma-separated list of group IDs. Results 737 * will be limited to children of the specified groups. Default: false. 713 738 * @type bool $populate_extras Whether to fetch additional information 714 739 * (such as member count) about groups. Default: true. 715 740 * @type array|string $exclude Optional. Array or comma-separated list of group IDs. … … 761 786 'group_type__not_in' => '', 762 787 'meta_query' => false, 763 788 'include' => false, 789 'parent_id' => false, 764 790 'populate_extras' => true, 765 791 'update_meta_cache' => true, 766 792 'exclude' => false, … … 835 861 $sql['include'] = " AND g.id IN ({$include})"; 836 862 } 837 863 864 if ( ! empty( $r['parent_id'] ) ) { 865 $parent_id = implode( ',', wp_parse_id_list( $r['parent_id'] ) ); 866 $sql['parent_id'] = " AND g.parent_id IN ({$parent_id})"; 867 } 868 838 869 if ( ! empty( $r['exclude'] ) ) { 839 870 $exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) ); 840 871 $sql['exclude'] = " AND g.id NOT IN ({$exclude})"; … … 948 979 } 949 980 950 981 // Already escaped in the paginated results block. 982 if ( ! empty( $parent_id ) ) { 983 $total_sql['where'][] = "g.parent_id IN ({$parent_id})"; 984 } 985 986 // Already escaped in the paginated results block. 951 987 if ( ! empty( $exclude ) ) { 952 988 $total_sql['where'][] = "g.id NOT IN ({$exclude})"; 953 989 } -
src/bp-groups/classes/class-bp-groups-template.php
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' => '', … … 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/cache.php
219 219 // check if function references cache or hits the DB by comparing query count 220 220 $this->assertEquals( $first_query_count, $wpdb->num_queries ); 221 221 } 222 223 /** 224 * @group schema 225 */ 226 public function test_invalidate_old_group_cache_after_db_schema_change() { 227 $g1 = $this->factory->group->create(); 228 229 // Fetch group object to set the cache. 230 new BP_Groups_Group( $g1 ); 231 232 // Set older cached version by unsetting 'parent_id' prop and resaving. 233 $data = wp_cache_get( $g1, 'bp_groups' ); 234 unset( $data->parent_id ); 235 wp_cache_set( $g1, $data, 'bp_groups' ); 236 237 // Now refetch group object to see if cached version is updated. 238 $g = new BP_Groups_Group( $g1 ); 239 240 $this->assertTrue( isset( $g->parent_id ) ); 241 } 222 242 } -
tests/phpunit/testcases/groups/class-bp-groups-group.php
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
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 }