Changeset 375 for trunk/bp-groups.php
- Timestamp:
- 10/09/2008 04:37:49 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-groups.php
r373 r375 3 3 4 4 define ( 'BP_GROUPS_IS_INSTALLED', 1 ); 5 define ( 'BP_GROUPS_VERSION', '0.1. 6' );5 define ( 'BP_GROUPS_VERSION', '0.1.9' ); 6 6 7 7 include_once( 'bp-groups/bp-groups-classes.php' ); … … 58 58 KEY is_confirmed (is_confirmed) 59 59 );"; 60 61 $sql[] = "CREATE TABLE ". $bp['groups']['table_name_groupmeta'] ." ( 62 id int(11) NOT NULL AUTO_INCREMENT, 63 group_id int(11) NOT NULL, 64 meta_key varchar(255) DEFAULT NULL, 65 meta_value longtext DEFAULT NULL, 66 PRIMARY KEY (id), 67 KEY group_id (group_id), 68 KEY meta_key (meta_key) 69 );"; 60 70 61 71 if ( function_exists('bp_wire_install') ) { … … 86 96 **************************************************************************/ 87 97 88 function groups_setup_globals() { 89 global $bp, $wpdb; 90 98 function groups_setup_globals( $global = true ) { 99 global $wpdb; 100 101 if ( $global ) 102 global $bp; 103 91 104 $bp['groups'] = array( 92 105 'table_name' => $wpdb->base_prefix . 'bp_groups', 93 106 'table_name_members' => $wpdb->base_prefix . 'bp_groups_members', 107 'table_name_groupmeta' => $wpdb->base_prefix . 'bp_groups_groupmeta', 94 108 'image_base' => site_url() . '/wp-content/mu-plugins/bp-groups/images', 95 109 'format_activity_function' => 'groups_format_activity', … … 101 115 102 116 $bp['groups']['forbidden_names'] = array( 'my-groups', 'group-finder', 'create', 'invites', 'delete', 'add' ); 117 118 return $bp; 103 119 } 104 120 add_action( 'wp', 'groups_setup_globals', 1 ); … … 124 140 if ( ( $wpdb->get_var("show tables like '%" . $bp['groups']['table_name'] . "%'") == false ) || ( get_site_option('bp-groups-version') < BP_GROUPS_VERSION ) ) 125 141 groups_install(BP_GROUPS_VERSION); 126 142 127 143 } 128 144 add_action( 'admin_menu', 'groups_add_admin_menu' ); … … 135 151 136 152 function groups_setup_nav() { 137 global $bp ;153 global $bp, $current_blog; 138 154 global $group_obj, $is_single_group; 139 155 … … 151 167 bp_core_add_nav_default( $bp['groups']['slug'], 'groups_screen_my_groups', 'my-groups' ); 152 168 153 $groups_link = $ group_link = $bp['loggedin_domain'] . $bp['groups']['slug'] . '/';169 $groups_link = $bp['loggedin_domain'] . $bp['groups']['slug'] . '/'; 154 170 155 171 /* Add the subnav items to the groups nav item */ … … 170 186 $bp['bp_options_title'] = $bp['current_fullname']; 171 187 172 } else if ( $is_single_group ) { 188 } else if ( $is_single_group ) { 173 189 // We are viewing a single group, so set up the 174 190 // group navigation menu using the $group_obj global. … … 183 199 $bp['bp_options_avatar'] = '<img src="' . $group_obj->avatar_thumb . '" alt="Group Avatar Thumbnail" />'; 184 200 185 $group_link = $bp['loggedin_domain'] . $bp['groups']['slug'] . '/' . $group_obj->slug . '/'; 201 switch_to_blog(1); 202 $group_link = site_url() . '/' . $bp['groups']['slug'] . '/' . $group_obj->slug . '/'; 203 switch_to_blog($current_blog->blog_id); 186 204 187 205 // Reset the existing subnav items … … 202 220 203 221 bp_core_add_subnav_item( $bp['groups']['slug'], 'members', __('Members'), $group_link, 'groups_screen_group_members', 'group-members' ); 204 bp_core_add_subnav_item( $bp['groups']['slug'], 'send-invites', __('Send Invites'), $group_link, 'groups_screen_group_invite', 'group-invite' );205 222 206 223 if ( is_user_logged_in() && groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) ) { 224 bp_core_add_subnav_item( $bp['groups']['slug'], 'send-invites', __('Send Invites'), $group_link, 'groups_screen_group_invite', 'group-invite' ); 207 225 bp_core_add_subnav_item( $bp['groups']['slug'], 'leave-group', __('Leave Group'), $group_link, 'groups_screen_group_leave', 'group-leave' ); 208 226 } … … 211 229 } 212 230 add_action( 'wp', 'groups_setup_nav', 2 ); 231 232 function groups_get_group_theme() { 233 global $current_component, $current_action, $is_single_group; 234 235 // The theme filter does not recognize any globals, where as the stylesheet filter does. 236 // We have to set up the globals to use manually. 237 bp_core_set_uri_globals(); 238 $groups_bp = groups_setup_globals(false); 239 240 if ( $current_component == $groups_bp['groups']['slug'] ) 241 $is_single_group = BP_Groups_Group::group_exists( $current_action, $groups_bp['groups']['table_name'] ); 242 243 if ( $current_component == $groups_bp['groups']['slug'] && $is_single_group ) 244 $theme = 'buddypress'; 245 else 246 $theme = get_option('template'); 247 248 return $theme; 249 } 250 add_filter( 'template', 'groups_get_group_theme' ); 251 252 function groups_get_group_stylesheet() { 253 global $bp, $is_single_group; 254 255 if ( $bp['current_component'] == $bp['groups']['slug'] && $is_single_group ) 256 return 'buddypress'; 257 else 258 return get_option('stylesheet'); 259 } 260 add_filter( 'stylesheet', 'groups_get_group_stylesheet' ); 261 213 262 214 263 /***** Screens **********/ … … 334 383 } 335 384 } 336 385 337 386 } else if ( $bp['action_variables'][1] == 'delete' && BP_Groups_Member::check_is_member( $bp['loggedin_userid'], $group_obj->id ) ) { 338 339 if ( !groups_delete_wire_post( $bp['action_variables'][2], $bp['groups']['table_name_wire'] ) ) { 387 $wire_message_id = $bp['action_variables'][2]; 388 389 if ( !groups_delete_wire_post( $wire_message_id, $bp['groups']['table_name_wire'] ) ) { 340 390 bp_catch_uri( 'groups/group-home' ); 341 391 } else { … … 412 462 } else { 413 463 $bp['message'] = __('You left the group successfully.'); 414 $bp['message_type'] = 'success'; 464 $bp['message_type'] = 'success'; 415 465 } 416 466 add_action( 'template_notices', 'bp_core_render_notice' ); 417 467 418 $is_single_group = false; 419 $bp['current_action'] = 'group-finder'; 420 bp_catch_uri( 'groups/group-finder' ); 468 $bp['current_action'] = 'group-home'; 469 bp_catch_uri( 'groups/group-home' ); 421 470 422 471 } else if ( isset($bp['action_variables']) && $bp['action_variables'][1] == 'no' ) { … … 445 494 } else { 446 495 $bp['message'] = __('You joined the group!'); 447 $bp['message_type'] = 'success'; 496 $bp['message_type'] = 'success'; 448 497 } 449 498 … … 469 518 } 470 519 } 471 add_action( ' bp_groups_joined_group', 'groups_record_activity' );472 add_action( ' bp_groups_created_group', 'groups_record_activity' );473 add_action( ' bp_groups_new_wire_post', 'groups_record_activity' );520 add_action( 'activity_groups_joined_group', 'groups_record_activity' ); 521 add_action( 'activity_groups_created_group', 'groups_record_activity' ); 522 add_action( 'activity_groups_new_wire_post', 'groups_record_activity' ); 474 523 475 524 /************************************************************************** … … 518 567 519 568 /************************************************************************** 520 groups_ admin_setup()569 groups_update_last_activity() 521 570 522 Setup CSS, JS and other things needed for the xprofile component. 523 **************************************************************************/ 524 525 function groups_admin_setup() { 526 } 527 add_action( 'admin_menu', 'groups_admin_setup' ); 528 571 Sets groupmeta for the group with the last activity date for the group based 572 on specific group activities. 573 **************************************************************************/ 574 575 function groups_update_last_activity( $args = true ) { 576 extract($args); 577 578 groups_update_groupmeta( $group_id, 'last_activity', time() ); 579 } 580 add_action( 'groups_deleted_wire_post', 'groups_update_last_activity' ); 581 add_action( 'groups_new_wire_post', 'groups_update_last_activity' ); 582 add_action( 'groups_joined_group', 'groups_update_last_activity' ); 583 add_action( 'groups_leave_group', 'groups_update_last_activity' ); 584 add_action( 'groups_created_group', 'groups_update_last_activity' ); 585 586 587 /************************************************************************** 588 groups_get_user_groups() 589 590 Fetch the groups the current user is a member of. 591 **************************************************************************/ 529 592 530 593 function groups_get_user_groups( $pag_page, $pag_num ) { … … 655 718 if ( !$group->save() ) 656 719 return false; 657 720 658 721 // Save the creator as the group administrator 659 722 $admin = new BP_Groups_Member( $bp['loggedin_userid'], $group->id ); … … 663 726 $admin->inviter_id = 0; 664 727 $admin->is_confirmed = 1; 665 728 666 729 if ( !$admin->save() ) 667 730 return false; 668 731 732 /* Set groupmeta */ 733 groups_update_groupmeta( $group->id, 'total_member_count', 1 ); 734 groups_update_groupmeta( $group->id, 'theme', 'buddypress' ); 735 groups_update_groupmeta( $group->id, 'stylesheet', 'buddypress' ); 736 669 737 return $group->id; 670 738 } … … 680 748 $group->enable_photos = 1; 681 749 $group->photos_admin_only = 0; 682 $group->date_created = time();683 750 684 751 if ( !isset($_POST['group-show-wire']) ) … … 727 794 groups_send_invites($group); 728 795 729 do_action( 'bp_groups_created_group', array( 'item_id' => $group->id, 'component_name' => 'groups', 'component_action' => 'created_group', 'is_private' => 0 ) ); 796 /* activity stream recording action */ 797 do_action( 'activity_groups_created_group', array( 'item_id' => $group->id, 'component_name' => 'groups', 'component_action' => 'created_group', 'is_private' => 0 ) ); 798 799 /* regular action */ 800 do_action( 'groups_created_group', array( 'group_id' => $group->id ) ); 730 801 731 802 header( "Location: " . $bp['loggedin_domain'] . $bp['groups']['slug'] . "/" . $group->slug ); … … 774 845 return false; 775 846 847 do_action( 'groups_invite_user', array( 'group_id' => $group_id, 'user_id' => $user_id ) ); 848 776 849 return true; 777 850 } … … 782 855 if ( !BP_Groups_Member::delete( $user_id, $group_id ) ) 783 856 return false; 784 857 858 do_action( 'groups_uninvite_user', array( 'group_id' => $group_id, 'user_id' => $user_id ) ); 859 785 860 return true; 786 861 } … … 824 899 wp_mail( $invited_user->email, __("New Group Invitation:") . $group_obj->name, $message, "From: noreply@" . $_SERVER[ 'HTTP_HOST' ] ); 825 900 } 901 902 do_action( 'groups_send_invites', array( 'group_id' => $group_obj->id, 'invited_users' => $invited_users ) ); 826 903 } 827 904 … … 832 909 if ( !groups_uninvite_user( $bp['loggedin_userid'], $group_id ) ) 833 910 return false; 911 912 do_action( 'groups_leave_group', array( 'group_id' => $group_id, 'user_id' => $bp['loggedin_userid'] ) ); 913 914 /* Modify group member count */ 915 groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') - 1 ); 834 916 835 917 return true; … … 850 932 if ( !$new_member->save() ) 851 933 return false; 852 853 do_action( 'bp_groups_joined_group', array( 'item_id' => $new_member->group_id, 'component_name' => 'groups', 'component_action' => 'joined_group', 'is_private' => 0 ) ); 934 935 /* activity stream recording action */ 936 do_action( 'activity_groups_joined_group', array( 'item_id' => $new_member->group_id, 'component_name' => 'groups', 'component_action' => 'joined_group', 'is_private' => 0 ) ); 937 938 /* regular action */ 939 do_action( 'groups_joined_group', array( 'group_id' => $group_id, 'user_id' => $bp['loggedin_userid'] ) ); 940 941 /* Modify group member count */ 942 groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 ); 854 943 855 944 return true; … … 858 947 function groups_new_wire_post( $group_id, $content ) { 859 948 if ( $wire_post_id = bp_wire_new_post( $group_id, $content ) ) { 860 do_action( 'bp_groups_new_wire_post', array( 'item_id' => $wire_post_id, 'component_name' => 'groups', 'component_action' => 'new_wire_post', 'is_private' => 0 ) ); 949 950 /* activity stream recording action */ 951 do_action( 'activity_groups_new_wire_post', array( 'item_id' => $wire_post_id, 'component_name' => 'groups', 'component_action' => 'new_wire_post', 'is_private' => 0 ) ); 952 953 /* regular action */ 954 do_action( 'groups_new_wire_post', array( 'group_id' => $group_id, 'wire_post_id' => $wire_post_id ) ); 955 861 956 return true; 862 957 } … … 867 962 function groups_delete_wire_post( $wire_post_id, $table_name ) { 868 963 if ( bp_wire_delete_post( $wire_post_id, $table_name ) ) { 869 do_action( ' bp_groups_deleted_wire_post', array( 'wire_post_id' => $wire_post_id ) );964 do_action( 'groups_deleted_wire_post', array( 'wire_post_id' => $wire_post_id ) ); 870 965 return true; 871 966 } … … 873 968 return false; 874 969 } 970 971 function groups_get_newest( $limit = 5 ) { 972 return BP_Groups_Group::get_newest($limit); 973 } 974 975 function groups_get_active( $limit = 5 ) { 976 return BP_Groups_Group::get_active($limit); 977 } 978 979 function groups_get_popular( $limit = 5 ) { 980 return BP_Groups_Group::get_popular($limit); 981 } 982 983 984 // 985 // Group meta functions 986 // 987 988 function groups_delete_groupmeta( $group_id, $meta_key, $meta_value = '' ) { 989 global $wpdb, $bp; 990 991 if ( !is_numeric( $group_id ) ) 992 return false; 993 994 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 995 996 if ( is_array($meta_value) || is_object($meta_value) ) 997 $meta_value = serialize($meta_value); 998 999 $meta_value = trim( $meta_value ); 1000 1001 if ( !empty($meta_value) ) 1002 $wpdb->query( $wpdb->prepare("DELETE FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE groups_id = %d AND meta_key = %s AND meta_value = %s", $group_id, $meta_key, $meta_value) ); 1003 else 1004 $wpdb->query( $wpdb->prepare("DELETE FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key) ); 1005 1006 // TODO need to look into using this. 1007 // wp_cache_delete($group_id, 'groups'); 1008 1009 return true; 1010 } 1011 1012 function groups_get_groupmeta( $group_id, $meta_key = '') { 1013 global $wpdb, $bp; 1014 1015 $group_id = (int) $group_id; 1016 1017 if ( !$group_id ) 1018 return false; 1019 1020 if ( !empty($meta_key) ) { 1021 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 1022 1023 // TODO need to look into using this. 1024 //$user = wp_cache_get($user_id, 'users'); 1025 1026 // Check the cached user object 1027 //if ( false !== $user && isset($user->$meta_key) ) 1028 // $metas = array($user->$meta_key); 1029 //else 1030 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key) ); 1031 } else { 1032 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d", $group_id) ); 1033 } 1034 1035 if ( empty($metas) ) { 1036 if ( empty($meta_key) ) 1037 return array(); 1038 else 1039 return ''; 1040 } 1041 1042 $metas = array_map('maybe_unserialize', $metas); 1043 1044 if ( count($metas) == 1 ) 1045 return $metas[0]; 1046 else 1047 return $metas; 1048 } 1049 1050 function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) { 1051 global $wpdb, $bp; 1052 1053 if ( !is_numeric( $group_id ) ) 1054 return false; 1055 1056 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 1057 1058 if ( is_string($meta_value) ) 1059 $meta_value = stripslashes($wpdb->escape($meta_value)); 1060 1061 $meta_value = maybe_serialize($meta_value); 1062 1063 if (empty($meta_value)) { 1064 return groups_delete_groupmeta( $group_id, $meta_key ); 1065 } 1066 1067 $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) ); 1068 1069 if ( !$cur ) { 1070 $wpdb->query( $wpdb->prepare( "INSERT INTO " . $bp['groups']['table_name_groupmeta'] . " ( group_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $group_id, $meta_key, $meta_value ) ); 1071 } else if ( $cur->meta_value != $meta_value ) { 1072 $wpdb->query( $wpdb->prepare( "UPDATE " . $bp['groups']['table_name_groupmeta'] . " SET meta_value = %s WHERE group_id = %d AND meta_key = %s", $meta_value, $group_id, $meta_key ) ); 1073 } else { 1074 return false; 1075 } 1076 1077 // TODO need to look into using this. 1078 // wp_cache_delete($user_id, 'users'); 1079 1080 return true; 1081 } 1082 875 1083 876 1084 function groups_remove_data( $user_id ) {
Note: See TracChangeset
for help on using the changeset viewer.