Skip to:
Content

BuddyPress.org

Changeset 375 for trunk/bp-groups.php


Ignore:
Timestamp:
10/09/2008 04:37:49 AM (17 years ago)
Author:
apeatling
Message:
  • Moved all group pages to the root, rather than using a member URL
  • Introduced groupmeta support - groups_update_groupmeta / groups_delete_groupmeta / groups_get_groupmeta
  • Added widgets for site wide activity and who's online
  • Updated home theme to support display of new BuddyPress widgets
  • Added site wide activity feed support
  • Fixed bug where ajax functions would only work when logged in
  • Various other bug fixes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-groups.php

    r373 r375  
    33
    44define ( 'BP_GROUPS_IS_INSTALLED', 1 );
    5 define ( 'BP_GROUPS_VERSION', '0.1.6' );
     5define ( 'BP_GROUPS_VERSION', '0.1.9' );
    66
    77include_once( 'bp-groups/bp-groups-classes.php' );
     
    5858            KEY is_confirmed (is_confirmed)
    5959           );";
     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           );";
    6070   
    6171    if ( function_exists('bp_wire_install') ) {
     
    8696 **************************************************************************/
    8797
    88 function groups_setup_globals() {
    89     global $bp, $wpdb;
    90 
     98function groups_setup_globals( $global = true ) {
     99    global $wpdb;
     100   
     101    if ( $global )
     102        global $bp;
     103   
    91104    $bp['groups'] = array(
    92105        'table_name' => $wpdb->base_prefix . 'bp_groups',
    93106        'table_name_members' => $wpdb->base_prefix . 'bp_groups_members',
     107        'table_name_groupmeta' => $wpdb->base_prefix . 'bp_groups_groupmeta',
    94108        'image_base' => site_url() . '/wp-content/mu-plugins/bp-groups/images',
    95109        'format_activity_function' => 'groups_format_activity',
     
    101115   
    102116    $bp['groups']['forbidden_names'] = array( 'my-groups', 'group-finder', 'create', 'invites', 'delete', 'add' );
     117
     118    return $bp;
    103119}
    104120add_action( 'wp', 'groups_setup_globals', 1 ); 
     
    124140    if ( ( $wpdb->get_var("show tables like '%" . $bp['groups']['table_name'] . "%'") == false ) || ( get_site_option('bp-groups-version') < BP_GROUPS_VERSION )  )
    125141        groups_install(BP_GROUPS_VERSION);
    126        
     142   
    127143}
    128144add_action( 'admin_menu', 'groups_add_admin_menu' );
     
    135151
    136152function groups_setup_nav() {
    137     global $bp;
     153    global $bp, $current_blog;
    138154    global $group_obj, $is_single_group;
    139155   
     
    151167    bp_core_add_nav_default( $bp['groups']['slug'], 'groups_screen_my_groups', 'my-groups' );
    152168       
    153     $groups_link = $group_link = $bp['loggedin_domain'] . $bp['groups']['slug'] . '/';
     169    $groups_link = $bp['loggedin_domain'] . $bp['groups']['slug'] . '/';
    154170   
    155171    /* Add the subnav items to the groups nav item */
     
    170186            $bp['bp_options_title'] = $bp['current_fullname'];
    171187           
    172         } else if ( $is_single_group ) {
     188        } else if ( $is_single_group ) {       
    173189            // We are viewing a single group, so set up the
    174190            // group navigation menu using the $group_obj global.
     
    183199            $bp['bp_options_avatar'] = '<img src="' . $group_obj->avatar_thumb . '" alt="Group Avatar Thumbnail" />';
    184200           
    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);
    186204           
    187205            // Reset the existing subnav items
     
    202220           
    203221            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' );
    205222           
    206223            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' );
    207225                bp_core_add_subnav_item( $bp['groups']['slug'], 'leave-group', __('Leave Group'), $group_link, 'groups_screen_group_leave', 'group-leave' );
    208226            }
     
    211229}
    212230add_action( 'wp', 'groups_setup_nav', 2 );
     231
     232function 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}
     250add_filter( 'template', 'groups_get_group_theme' );
     251
     252function 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}
     260add_filter( 'stylesheet', 'groups_get_group_stylesheet' );
     261
    213262
    214263/***** Screens **********/
     
    334383                }
    335384            }
    336        
     385   
    337386        } 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'] ) ) {
    340390                bp_catch_uri( 'groups/group-home' );
    341391            } else {
     
    412462            } else {
    413463                $bp['message'] = __('You left the group successfully.');
    414                 $bp['message_type'] = 'success';                                   
     464                $bp['message_type'] = 'success';   
    415465            }
    416466            add_action( 'template_notices', 'bp_core_render_notice' );
    417467       
    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' );
    421470       
    422471        } else if ( isset($bp['action_variables']) && $bp['action_variables'][1] == 'no' ) {
     
    445494        } else {
    446495            $bp['message'] = __('You joined the group!');
    447             $bp['message_type'] = 'success';                       
     496            $bp['message_type'] = 'success';   
    448497        }
    449498
     
    469518    }
    470519}
    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' );
     520add_action( 'activity_groups_joined_group', 'groups_record_activity' );
     521add_action( 'activity_groups_created_group', 'groups_record_activity' );
     522add_action( 'activity_groups_new_wire_post', 'groups_record_activity' );
    474523
    475524/**************************************************************************
     
    518567
    519568/**************************************************************************
    520  groups_admin_setup()
     569 groups_update_last_activity()
    521570 
    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
     575function groups_update_last_activity( $args = true ) {
     576    extract($args);
     577
     578    groups_update_groupmeta( $group_id, 'last_activity', time() );
     579}
     580add_action( 'groups_deleted_wire_post', 'groups_update_last_activity' );
     581add_action( 'groups_new_wire_post', 'groups_update_last_activity' );
     582add_action( 'groups_joined_group', 'groups_update_last_activity' );
     583add_action( 'groups_leave_group', 'groups_update_last_activity' );
     584add_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 **************************************************************************/
    529592
    530593function groups_get_user_groups( $pag_page, $pag_num ) {
     
    655718                    if ( !$group->save() )
    656719                        return false;
    657                    
     720                                       
    658721                    // Save the creator as the group administrator
    659722                    $admin = new BP_Groups_Member( $bp['loggedin_userid'], $group->id );
     
    663726                    $admin->inviter_id = 0;
    664727                    $admin->is_confirmed = 1;
    665                                    
     728
    666729                    if ( !$admin->save() )
    667730                        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                   
    669737                    return $group->id;
    670738                }
     
    680748                $group->enable_photos = 1;
    681749                $group->photos_admin_only = 0;
    682                 $group->date_created = time();
    683750               
    684751                if ( !isset($_POST['group-show-wire']) )
     
    727794                groups_send_invites($group);
    728795               
    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 ) );
    730801               
    731802                header( "Location: " . $bp['loggedin_domain'] . $bp['groups']['slug'] . "/" . $group->slug );
     
    774845        return false;
    775846   
     847    do_action( 'groups_invite_user', array( 'group_id' => $group_id, 'user_id' => $user_id ) );
     848       
    776849    return true;
    777850}
     
    782855    if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
    783856        return false;
    784    
     857
     858    do_action( 'groups_uninvite_user', array( 'group_id' => $group_id, 'user_id' => $user_id ) );
     859
    785860    return true;
    786861}
     
    824899        wp_mail( $invited_user->email, __("New Group Invitation:") . $group_obj->name, $message, "From: noreply@" . $_SERVER[ 'HTTP_HOST' ]  );
    825900    }
     901
     902    do_action( 'groups_send_invites', array( 'group_id' => $group_obj->id, 'invited_users' => $invited_users ) );
    826903}
    827904
     
    832909    if ( !groups_uninvite_user( $bp['loggedin_userid'], $group_id ) )
    833910        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 );
    834916   
    835917    return true;
     
    850932    if ( !$new_member->save() )
    851933        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 );
    854943   
    855944    return true;
     
    858947function groups_new_wire_post( $group_id, $content ) {
    859948    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       
    861956        return true;
    862957    }
     
    867962function groups_delete_wire_post( $wire_post_id, $table_name ) {
    868963    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 ) );
    870965        return true;
    871966    }
     
    873968    return false;
    874969}
     970
     971function groups_get_newest( $limit = 5 ) {
     972    return BP_Groups_Group::get_newest($limit);
     973}
     974
     975function groups_get_active( $limit = 5 ) {
     976    return BP_Groups_Group::get_active($limit);
     977}
     978
     979function groups_get_popular( $limit = 5 ) {
     980    return BP_Groups_Group::get_popular($limit);
     981}
     982
     983
     984//
     985// Group meta functions
     986//
     987
     988function 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
     1012function 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
     1050function 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
    8751083
    8761084function groups_remove_data( $user_id ) {
Note: See TracChangeset for help on using the changeset viewer.