Changeset 3917 for trunk/bp-friends/bp-friends-loader.php
- Timestamp:
- 01/25/2011 08:58:56 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-friends/bp-friends-loader.php
r3903 r3917 1 1 <?php 2 2 3 // Required Files 4 require ( BP_PLUGIN_DIR . '/bp-friends/bp-friends-classes.php' ); 5 require ( BP_PLUGIN_DIR . '/bp-friends/bp-friends-template.php' ); 3 /** 4 * BuddyPress Friends Streams Loader 5 * 6 * The friends component is for users to create relationships with each other 7 * 8 * @package BuddyPress 9 * @subpackage Friends Core 10 */ 6 11 7 define ( 'BP_FRIENDS_DB_VERSION', '1800' ); 12 class BP_Friends_Component extends BP_Component { 8 13 9 // Define the slug for the component 10 if ( !defined( 'BP_FRIENDS_SLUG' ) ) 11 define ( 'BP_FRIENDS_SLUG', 'friends' ); 12 13 function friends_setup_globals() { 14 global $bp; 15 16 // For internal identification 17 $bp->friends->id = 'friends'; 18 19 // Slug 20 $bp->friends->slug = BP_FRIENDS_SLUG; 21 22 // Table 23 $bp->friends->table_name = $bp->table_prefix . 'bp_friends'; 24 25 // Notifications 26 $bp->friends->notification_callback = 'friends_format_notifications'; 27 28 // Register this in the active components array 29 $bp->active_components[$bp->friends->slug] = $bp->friends->id; 30 31 do_action( 'friends_setup_globals' ); 32 } 33 add_action( 'bp_setup_globals', 'friends_setup_globals' ); 34 35 function friends_setup_nav() { 36 global $bp; 37 38 // Add 'Friends' to the main navigation 39 bp_core_new_nav_item( array( 'name' => sprintf( __( 'Friends <span>(%d)</span>', 'buddypress' ), friends_get_total_friend_count() ), 'slug' => $bp->friends->slug, 'position' => 60, 'screen_function' => 'friends_screen_my_friends', 'default_subnav_slug' => 'my-friends', 'item_css_id' => $bp->friends->id ) ); 40 41 $friends_link = $bp->loggedin_user->domain . $bp->friends->slug . '/'; 42 43 // Add the subnav items to the friends nav item 44 bp_core_new_subnav_item( array( 'name' => __( 'My Friends', 'buddypress' ), 'slug' => 'my-friends', 'parent_url' => $friends_link, 'parent_slug' => $bp->friends->slug, 'screen_function' => 'friends_screen_my_friends', 'position' => 10, 'item_css_id' => 'friends-my-friends' ) ); 45 bp_core_new_subnav_item( array( 'name' => __( 'Requests', 'buddypress' ), 'slug' => 'requests', 'parent_url' => $friends_link, 'parent_slug' => $bp->friends->slug, 'screen_function' => 'friends_screen_requests', 'position' => 20, 'user_has_access' => bp_is_my_profile() ) ); 46 47 if ( $bp->current_component == $bp->friends->slug ) { 48 if ( bp_is_my_profile() ) { 49 $bp->bp_options_title = __( 'My Friends', 'buddypress' ); 50 } else { 51 $bp->bp_options_avatar = bp_core_fetch_avatar( array( 'item_id' => $bp->displayed_user->id, 'type' => 'thumb' ) ); 52 $bp->bp_options_title = $bp->displayed_user->fullname; 53 } 14 /** 15 * Start the friends component creation process 16 * 17 * @since BuddyPress {unknown} 18 */ 19 function BP_Friends_Component() { 20 parent::start( 21 'friends', 22 __( 'Friend Connections', 'buddypress' ), 23 BP_PLUGIN_DIR 24 ); 54 25 } 55 26 56 do_action( 'friends_setup_nav' ); 57 } 58 add_action( 'bp_setup_nav', 'friends_setup_nav' ); 27 /** 28 * Include files 29 */ 30 function _includes() { 31 // Files to include 32 $includes = array( 33 'actions', 34 'screens', 35 'filters', 36 'classes', 37 'activity', 38 'template', 39 'functions', 40 'notifications', 41 ); 59 42 60 /******************************************************************************** 61 * Screen Functions 62 * 63 * Screen functions are the controllers of BuddyPress. They will execute when their 64 * specific URL is caught. They will first save or manipulate data using business 65 * functions, then pass on the user to a template file. 66 */ 67 68 function friends_screen_my_friends() { 69 global $bp; 70 71 // Delete any friendship acceptance notifications for the user when viewing a profile 72 bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->friends->id, 'friendship_accepted' ); 73 74 do_action( 'friends_screen_my_friends' ); 75 76 bp_core_load_template( apply_filters( 'friends_template_my_friends', 'members/single/home' ) ); 77 } 78 79 function friends_screen_requests() { 80 global $bp; 81 82 if ( isset( $bp->action_variables[0] ) && isset( $bp->action_variables[1] ) && 'accept' == $bp->action_variables[0] && is_numeric( $bp->action_variables[1] ) ) { 83 // Check the nonce 84 check_admin_referer( 'friends_accept_friendship' ); 85 86 if ( friends_accept_friendship( $bp->action_variables[1] ) ) 87 bp_core_add_message( __( 'Friendship accepted', 'buddypress' ) ); 88 else 89 bp_core_add_message( __( 'Friendship could not be accepted', 'buddypress' ), 'error' ); 90 91 bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action ); 92 93 } elseif ( isset( $bp->action_variables[0] ) && isset( $bp->action_variables[1] ) && 'reject' == $bp->action_variables[0] && is_numeric( $bp->action_variables[1] ) ) { 94 // Check the nonce 95 check_admin_referer( 'friends_reject_friendship' ); 96 97 if ( friends_reject_friendship( $bp->action_variables[1] ) ) 98 bp_core_add_message( __( 'Friendship rejected', 'buddypress' ) ); 99 else 100 bp_core_add_message( __( 'Friendship could not be rejected', 'buddypress' ), 'error' ); 101 102 bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action ); 43 parent::_includes( $includes ); 103 44 } 104 45 105 do_action( 'friends_screen_requests' ); 46 /** 47 * Setup globals 48 * 49 * The BP_FRIENDS_SLUG constant is deprecated, and only used here for 50 * backwards compatibility. 51 * 52 * @since BuddyPress {unknown} 53 * @global obj $bp 54 */ 55 function _setup_globals() { 56 global $bp; 106 57 107 if ( isset( $_GET['new'] ) ) 108 bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->friends->id, 'friendship_request' ); 58 define ( 'BP_FRIENDS_DB_VERSION', '1800' ); 109 59 110 bp_core_load_template( apply_filters( 'friends_template_requests', 'members/single/home' ) ); 111 } 60 // Define a slug, if necessary 61 if ( !defined( 'BP_FRIENDS_SLUG' ) ) 62 define( 'BP_FRIENDS_SLUG', $this->id ); 112 63 113 function friends_screen_notification_settings() { 114 global $bp; 64 // Global tables for messaging component 65 $global_tables = array( 66 'table_name' => $bp->table_prefix . 'bp_friends', 67 'table_name_meta' => $bp->table_prefix . 'bp_friends_meta', 68 ); 115 69 70 // All globals for messaging component. 71 // Note that global_tables is included in this array. 72 $globals = array( 73 'path' => BP_PLUGIN_DIR, 74 'slug' => BP_FRIENDS_SLUG, 75 'root_slug' => isset( $bp->pages->friends->slug ) ? $bp->pages->friends->slug : BP_FRIENDS_SLUG, 76 'search_string' => __( 'Search Friends...', 'buddypress' ), 77 'notification_callback' => 'friends_format_notifications', 78 'global_tables' => $global_tables, 79 ); 116 80 117 if ( !$send_requests = get_user_meta( $bp->displayed_user->id, 'notification_friends_friendship_request', true ) )118 $send_requests = 'yes';81 parent::_setup_globals( $globals ); 82 } 119 83 120 if ( !$accept_requests = get_user_meta( $bp->displayed_user->id, 'notification_friends_friendship_accepted', true ) ) 121 $accept_requests = 'yes'; 122 ?> 84 /** 85 * Setup BuddyBar navigation 86 * 87 * @global obj $bp 88 */ 89 function _setup_nav() { 90 global $bp; 123 91 124 <table class="notification-settings zebra" id="friends-notification-settings">125 <thead>126 <tr>127 <th class="icon"></th>128 <th class="title"><?php _e( 'Friends', 'buddypress' ) ?></th>129 <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>130 <th class="no"><?php _e( 'No', 'buddypress' )?></th>131 </tr>132 </thead>92 // Add 'Friends' to the main navigation 93 $main_nav = array( 94 'name' => sprintf( __( 'Friends <span>(%d)</span>', 'buddypress' ), friends_get_total_friend_count() ), 95 'slug' => $this->slug, 96 'position' => 60, 97 'screen_function' => 'friends_screen_my_friends', 98 'default_subnav_slug' => 'my-friends', 99 'item_css_id' => $bp->friends->id 100 ); 133 101 134 <tbody> 135 <tr id="friends-notification-settings-request"> 136 <td></td> 137 <td><?php _e( 'A member sends you a friendship request', 'buddypress' ) ?></td> 138 <td class="yes"><input type="radio" name="notifications[notification_friends_friendship_request]" value="yes" <?php checked( $send_requests, 'yes', true ) ?>/></td> 139 <td class="no"><input type="radio" name="notifications[notification_friends_friendship_request]" value="no" <?php checked( $send_requests, 'no', true ) ?>/></td> 140 </tr> 141 <tr id="friends-notification-settings-accepted"> 142 <td></td> 143 <td><?php _e( 'A member accepts your friendship request', 'buddypress' ) ?></td> 144 <td class="yes"><input type="radio" name="notifications[notification_friends_friendship_accepted]" value="yes" <?php checked( $accept_requests, 'yes', true ) ?>/></td> 145 <td class="no"><input type="radio" name="notifications[notification_friends_friendship_accepted]" value="no" <?php checked( $accept_requests, 'no', true ) ?>/></td> 146 </tr> 102 $friends_link = trailingslashit( $bp->loggedin_user->domain . $bp->friends->slug ); 147 103 148 <?php do_action( 'friends_screen_notification_settings' ); ?> 104 // Add the subnav items to the friends nav item 105 $sub_nav[] = array( 106 'name' => __( 'My Friends', 'buddypress' ), 107 'slug' => 'my-friends', 108 'parent_url' => $friends_link, 109 'parent_slug' => $bp->friends->slug, 110 'screen_function' => 'friends_screen_my_friends', 111 'position' => 10, 112 'item_css_id' => 'friends-my-friends' 113 ); 149 114 150 </tbody> 151 </table> 115 $sub_nav[] = array( 116 'name' => __( 'Requests', 'buddypress' ), 117 'slug' => 'requests', 118 'parent_url' => $friends_link, 119 'parent_slug' => $bp->friends->slug, 120 'screen_function' => 'friends_screen_requests', 121 'position' => 20, 122 'user_has_access' => bp_is_my_profile() 123 ); 152 124 153 <?php 154 } 155 add_action( 'bp_notification_settings', 'friends_screen_notification_settings' ); 125 parent::_setup_nav( $main_nav, $sub_nav ); 126 } 156 127 128 /** 129 * Sets up the title for pages and <title> 130 * 131 * @global obj $bp 132 */ 133 function _setup_title() { 134 global $bp; 157 135 158 /******************************************************************************** 159 * Action Functions 160 * 161 * Action functions are exactly the same as screen functions, however they do not 162 * have a template screen associated with them. Usually they will send the user 163 * back to the default screen after execution. 164 */ 165 166 function friends_action_add_friend() { 167 global $bp; 168 169 if ( $bp->current_component != $bp->friends->slug || $bp->current_action != 'add-friend' ) 170 return false; 171 172 $potential_friend_id = $bp->action_variables[0]; 173 174 if ( !is_numeric( $potential_friend_id ) || !isset( $potential_friend_id ) ) 175 return false; 176 177 if ( $potential_friend_id == $bp->loggedin_user->id ) 178 return false; 179 180 $friendship_status = BP_Friends_Friendship::check_is_friend( $bp->loggedin_user->id, $potential_friend_id ); 181 182 if ( 'not_friends' == $friendship_status ) { 183 184 if ( !check_admin_referer( 'friends_add_friend' ) ) 185 return false; 186 187 if ( !friends_add_friend( $bp->loggedin_user->id, $potential_friend_id ) ) { 188 bp_core_add_message( __( 'Friendship could not be requested.', 'buddypress' ), 'error' ); 189 } else { 190 bp_core_add_message( __( 'Friendship requested', 'buddypress' ) ); 136 // Adjust title 137 if ( bp_is_friends_component() ) { 138 if ( bp_is_my_profile() ) { 139 $bp->bp_options_title = __( 'My Friends', 'buddypress' ); 140 } else { 141 $bp->bp_options_avatar = bp_core_fetch_avatar( array( 142 'item_id' => $bp->displayed_user->id, 143 'type' => 'thumb' 144 ) ); 145 $bp->bp_options_title = $bp->displayed_user->fullname; 146 } 191 147 } 192 148 193 } else if ( 'is_friend' == $friendship_status ) { 194 bp_core_add_message( __( 'You are already friends with this user', 'buddypress' ), 'error' ); 195 } else { 196 bp_core_add_message( __( 'You already have a pending friendship request with this user', 'buddypress' ), 'error' ); 197 } 198 199 bp_core_redirect( wp_get_referer() ); 200 201 return false; 202 } 203 add_action( 'bp_init', 'friends_action_add_friend' ); 204 205 function friends_action_remove_friend() { 206 global $bp; 207 208 if ( $bp->current_component != $bp->friends->slug || $bp->current_action != 'remove-friend' ) 209 return false; 210 211 $potential_friend_id = $bp->action_variables[0]; 212 213 if ( !is_numeric( $potential_friend_id ) || !isset( $potential_friend_id ) ) 214 return false; 215 216 if ( $potential_friend_id == $bp->loggedin_user->id ) 217 return false; 218 219 $friendship_status = BP_Friends_Friendship::check_is_friend( $bp->loggedin_user->id, $potential_friend_id ); 220 221 if ( 'is_friend' == $friendship_status ) { 222 223 if ( !check_admin_referer( 'friends_remove_friend' ) ) 224 return false; 225 226 if ( !friends_remove_friend( $bp->loggedin_user->id, $potential_friend_id ) ) { 227 bp_core_add_message( __( 'Friendship could not be canceled.', 'buddypress' ), 'error' ); 228 } else { 229 bp_core_add_message( __( 'Friendship canceled', 'buddypress' ) ); 230 } 231 232 } else if ( 'is_friends' == $friendship_status ) { 233 bp_core_add_message( __( 'You are not yet friends with this user', 'buddypress' ), 'error' ); 234 } else { 235 bp_core_add_message( __( 'You have a pending friendship request with this user', 'buddypress' ), 'error' ); 236 } 237 238 bp_core_redirect( wp_get_referer() ); 239 240 return false; 241 } 242 add_action( 'bp_init', 'friends_action_remove_friend' ); 243 244 245 /******************************************************************************** 246 * Activity & Notification Functions 247 * 248 * These functions handle the recording, deleting and formatting of activity and 249 * notifications for the user and for this specific component. 250 */ 251 252 function friends_record_activity( $args = '' ) { 253 global $bp; 254 255 if ( !function_exists( 'bp_activity_add' ) ) 256 return false; 257 258 $defaults = array ( 259 'user_id' => $bp->loggedin_user->id, 260 'action' => '', 261 'content' => '', 262 'primary_link' => '', 263 'component' => $bp->friends->id, 264 'type' => false, 265 'item_id' => false, 266 'secondary_item_id' => false, 267 'recorded_time' => bp_core_current_time(), 268 'hide_sitewide' => false 269 ); 270 271 $r = wp_parse_args( $args, $defaults ); 272 extract( $r, EXTR_SKIP ); 273 274 return bp_activity_add( array( 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) ); 275 } 276 277 function friends_delete_activity( $args ) { 278 global $bp; 279 280 if ( function_exists('bp_activity_delete_by_item_id') ) { 281 extract( (array)$args ); 282 bp_activity_delete_by_item_id( array( 'item_id' => $item_id, 'component' => $bp->friends->id, 'type' => $type, 'user_id' => $user_id, 'secondary_item_id' => $secondary_item_id ) ); 149 parent::_setup_title(); 283 150 } 284 151 } 285 286 function friends_register_activity_actions() { 287 global $bp; 288 289 if ( !function_exists( 'bp_activity_set_action' ) ) 290 return false; 291 292 bp_activity_set_action( $bp->friends->id, 'friends_register_activity_action', __( 'New friendship created', 'buddypress' ) ); 293 294 do_action( 'friends_register_activity_actions' ); 295 } 296 add_action( 'bp_register_activity_actions', 'friends_register_activity_actions' ); 297 298 function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) { 299 global $bp; 300 301 switch ( $action ) { 302 case 'friendship_accepted': 303 if ( (int)$total_items > 1 ) { 304 return apply_filters( 'bp_friends_multiple_friendship_accepted_notification', '<a href="' . $bp->loggedin_user->domain . $bp->friends->slug . '/my-friends/newest" title="' . __( 'My Friends', 'buddypress' ) . '">' . sprintf( __('%d friends accepted your friendship requests', 'buddypress' ), (int)$total_items ) . '</a>', (int)$total_items ); 305 } else { 306 $user_fullname = bp_core_get_user_displayname( $item_id ); 307 $user_url = bp_core_get_user_domain( $item_id ); 308 return apply_filters( 'bp_friends_single_friendship_accepted_notification', '<a href="' . $user_url . '?new" title="' . $user_fullname .'\'s profile">' . sprintf( __( '%s accepted your friendship request', 'buddypress' ), $user_fullname ) . '</a>', $user_fullname ); 309 } 310 break; 311 312 case 'friendship_request': 313 if ( (int)$total_items > 1 ) { 314 return apply_filters( 'bp_friends_multiple_friendship_request_notification', '<a href="' . $bp->loggedin_user->domain . $bp->friends->slug . '/requests/?new" title="' . __( 'Friendship requests', 'buddypress' ) . '">' . sprintf( __('You have %d pending friendship requests', 'buddypress' ), (int)$total_items ) . '</a>', $total_items ); 315 } else { 316 $user_fullname = bp_core_get_user_displayname( $item_id ); 317 $user_url = bp_core_get_user_domain( $item_id ); 318 return apply_filters( 'bp_friends_single_friendship_request_notification', '<a href="' . $bp->loggedin_user->domain . $bp->friends->slug . '/requests/?new" title="' . __( 'Friendship requests', 'buddypress' ) . '">' . sprintf( __('You have a friendship request from %s', 'buddypress' ), $user_fullname ) . '</a>', $user_fullname ); 319 } 320 break; 321 } 322 323 do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); 324 325 return false; 326 } 327 328 329 /******************************************************************************** 330 * Business Functions 331 * 332 * Business functions are where all the magic happens in BuddyPress. They will 333 * handle the actual saving or manipulation of information. Usually they will 334 * hand off to a database class for data access, then return 335 * true or false on success or failure. 336 */ 337 338 function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) { 339 global $bp; 340 341 $friendship = new BP_Friends_Friendship; 342 343 if ( (int)$friendship->is_confirmed ) 344 return true; 345 346 $friendship->initiator_user_id = $initiator_userid; 347 $friendship->friend_user_id = $friend_userid; 348 $friendship->is_confirmed = 0; 349 $friendship->is_limited = 0; 350 $friendship->date_created = bp_core_current_time(); 351 352 if ( $force_accept ) 353 $friendship->is_confirmed = 1; 354 355 if ( $friendship->save() ) { 356 357 if ( !$force_accept ) { 358 // Add the on screen notification 359 bp_users_add_notification( $friendship->initiator_user_id, $friendship->friend_user_id, $bp->friends->id, 'friendship_request' ); 360 361 // Send the email notification 362 require_once( BP_PLUGIN_DIR . '/bp-friends/bp-friends-notifications.php' ); 363 friends_notification_new_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 364 365 do_action( 'friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 366 } else { 367 do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 368 } 369 370 return true; 371 } 372 373 return false; 374 } 375 376 function friends_remove_friend( $initiator_userid, $friend_userid ) { 377 global $bp; 378 379 $friendship_id = BP_Friends_Friendship::get_friendship_id( $initiator_userid, $friend_userid ); 380 $friendship = new BP_Friends_Friendship( $friendship_id ); 381 382 do_action( 'friends_before_friendship_delete', $friendship_id, $initiator_userid, $friend_userid ); 383 384 // Remove the activity stream item for the user who canceled the friendship 385 friends_delete_activity( array( 'item_id' => $friendship_id, 'type' => 'friendship_accepted', 'user_id' => $bp->displayed_user->id ) ); 386 387 do_action( 'friends_friendship_deleted', $friendship_id, $initiator_userid, $friend_userid ); 388 389 if ( $friendship->delete() ) { 390 friends_update_friend_totals( $initiator_userid, $friend_userid, 'remove' ); 391 392 return true; 393 } 394 395 return false; 396 } 397 398 function friends_accept_friendship( $friendship_id ) { 399 global $bp; 400 401 $friendship = new BP_Friends_Friendship( $friendship_id, true, false ); 402 403 if ( !$friendship->is_confirmed && BP_Friends_Friendship::accept( $friendship_id ) ) { 404 friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id ); 405 406 // Remove the friend request notice 407 bp_users_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' ); 408 409 // Add a friend accepted notice for the initiating user 410 bp_users_add_notification( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_accepted' ); 411 412 $initiator_link = bp_core_get_userlink( $friendship->initiator_user_id ); 413 $friend_link = bp_core_get_userlink( $friendship->friend_user_id ); 414 415 // Record in activity streams for the initiator 416 friends_record_activity( array( 417 'user_id' => $friendship->initiator_user_id, 418 'type' => 'friendship_created', 419 'action' => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ), $friendship ), 420 'item_id' => $friendship_id, 421 'secondary_item_id' => $friendship->friend_user_id 422 ) ); 423 424 // Record in activity streams for the friend 425 friends_record_activity( array( 426 'user_id' => $friendship->friend_user_id, 427 'type' => 'friendship_created', 428 'action' => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $friend_link, $initiator_link ), $friendship ), 429 'item_id' => $friendship_id, 430 'secondary_item_id' => $friendship->initiator_user_id, 431 'hide_sitewide' => true // We've already got the first entry site wide 432 ) ); 433 434 // Send the email notification 435 require_once( BP_PLUGIN_DIR . '/bp-friends/bp-friends-notifications.php' ); 436 friends_notification_accepted_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 437 438 do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id ); 439 440 return true; 441 } 442 443 return false; 444 } 445 446 function friends_reject_friendship( $friendship_id ) { 447 $friendship = new BP_Friends_Friendship( $friendship_id, true, false ); 448 449 if ( !$friendship->is_confirmed && BP_Friends_Friendship::reject( $friendship_id ) ) { 450 // Remove the friend request notice 451 bp_users_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' ); 452 453 do_action_ref_array( 'friends_friendship_rejected', array( $friendship_id, &$friendship ) ); 454 return true; 455 } 456 457 return false; 458 } 459 460 function friends_check_friendship( $user_id, $possible_friend_id ) { 461 global $bp; 462 463 if ( 'is_friend' == BP_Friends_Friendship::check_is_friend( $user_id, $possible_friend_id ) ) 464 return true; 465 466 return false; 467 } 468 469 // Returns - 'is_friend', 'not_friends', 'pending' 470 function friends_check_friendship_status( $user_id, $possible_friend_id ) { 471 return BP_Friends_Friendship::check_is_friend( $user_id, $possible_friend_id ); 472 } 473 474 function friends_get_total_friend_count( $user_id = 0 ) { 475 global $bp; 476 477 if ( !$user_id ) 478 $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id; 479 480 if ( !$count = wp_cache_get( 'bp_total_friend_count_' . $user_id, 'bp' ) ) { 481 $count = get_user_meta( $user_id, 'total_friend_count', true ); 482 if ( empty( $count ) ) $count = 0; 483 wp_cache_set( 'bp_total_friend_count_' . $user_id, $count, 'bp' ); 484 } 485 486 return apply_filters( 'friends_get_total_friend_count', $count ); 487 } 488 489 function friends_check_user_has_friends( $user_id ) { 490 $friend_count = friends_get_total_friend_count( $user_id ); 491 492 if ( empty( $friend_count ) ) 493 return false; 494 495 if ( !(int)$friend_count ) 496 return false; 497 498 return true; 499 } 500 501 function friends_get_friendship_id( $initiator_user_id, $friend_user_id ) { 502 return BP_Friends_Friendship::get_friendship_id( $initiator_user_id, $friend_user_id ); 503 } 504 505 function friends_get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false, $filter = false ) { 506 return BP_Friends_Friendship::get_friend_user_ids( $user_id, $friend_requests_only, $assoc_arr, $filter ); 507 } 508 509 function friends_search_friends( $search_terms, $user_id, $pag_num = 10, $pag_page = 1 ) { 510 return BP_Friends_Friendship::search_friends( $search_terms, $user_id, $pag_num, $pag_page ); 511 } 512 513 function friends_get_friendship_request_user_ids( $user_id ) { 514 return BP_Friends_Friendship::get_friendship_request_user_ids( $user_id ); 515 } 516 517 function friends_get_recently_active( $user_id, $per_page = 0, $page = 0, $filter = '' ) { 518 return apply_filters( 'friends_get_recently_active', BP_Core_User::get_users( 'active', $per_page, $page, $user_id, $filter ) ); 519 } 520 521 function friends_get_alphabetically( $user_id, $per_page = 0, $page = 0, $filter = '' ) { 522 return apply_filters( 'friends_get_alphabetically', BP_Core_User::get_users( 'alphabetical', $per_page, $page, $user_id, $filter ) ); 523 } 524 525 function friends_get_newest( $user_id, $per_page = 0, $page = 0, $filter = '' ) { 526 return apply_filters( 'friends_get_newest', BP_Core_User::get_users( 'newest', $per_page, $page, $user_id, $filter ) ); 527 } 528 529 function friends_get_bulk_last_active( $friend_ids ) { 530 return BP_Friends_Friendship::get_bulk_last_active( $friend_ids ); 531 } 532 533 function friends_get_friends_invite_list( $user_id = 0 ) { 534 global $bp; 535 536 if ( !$user_id ) 537 $user_id = $bp->loggedin_user->id; 538 539 if ( bp_has_members( 'user_id=' . $user_id . '&type=alphabetical&per_page=0' ) ) { 540 while ( bp_members() ) : bp_the_member(); 541 $friends[] = array( 542 'id' => bp_get_member_user_id(), 543 'full_name' => bp_get_member_name() 544 ); 545 endwhile; 546 } 547 548 if ( empty($friends) ) 549 return false; 550 551 return $friends; 552 } 553 554 function friends_count_invitable_friends( $user_id, $group_id ) { 555 return BP_Friends_Friendship::get_invitable_friend_count( $user_id, $group_id ); 556 } 557 558 function friends_get_friend_count_for_user( $user_id ) { 559 return BP_Friends_Friendship::total_friend_count( $user_id ); 560 } 561 562 function friends_search_users( $search_terms, $user_id, $pag_num = 0, $pag_page = 0 ) { 563 global $bp; 564 565 $user_ids = BP_Friends_Friendship::search_users( $search_terms, $user_id, $pag_num, $pag_page ); 566 567 if ( !$user_ids ) 568 return false; 569 570 for ( $i = 0; $i < count( $user_ids ); $i++ ) 571 $users[] = new BP_Core_User($user_ids[$i]); 572 573 return array( 'users' => $users, 'count' => BP_Friends_Friendship::search_users_count( $search_terms ) ); 574 } 575 576 function friends_is_friendship_confirmed( $friendship_id ) { 577 $friendship = new BP_Friends_Friendship( $friendship_id ); 578 return $friendship->is_confirmed; 579 } 580 581 function friends_update_friend_totals( $initiator_user_id, $friend_user_id, $status = 'add' ) { 582 if ( 'add' == $status ) { 583 update_user_meta( $initiator_user_id, 'total_friend_count', (int)get_user_meta( $initiator_user_id, 'total_friend_count', true ) + 1 ); 584 update_user_meta( $friend_user_id, 'total_friend_count', (int)get_user_meta( $friend_user_id, 'total_friend_count', true ) + 1 ); 585 } else { 586 update_user_meta( $initiator_user_id, 'total_friend_count', (int)get_user_meta( $initiator_user_id, 'total_friend_count', true ) - 1 ); 587 update_user_meta( $friend_user_id, 'total_friend_count', (int)get_user_meta( $friend_user_id, 'total_friend_count', true ) - 1 ); 588 } 589 } 590 591 function friends_remove_data( $user_id ) { 592 global $bp; 593 594 do_action( 'friends_before_remove_data', $user_id ); 595 596 BP_Friends_Friendship::delete_all_for_user($user_id); 597 598 // Remove usermeta 599 delete_user_meta( $user_id, 'total_friend_count' ); 600 601 // Remove friendship requests FROM user 602 bp_users_delete_notifications_from_user( $user_id, $bp->friends->id, 'friendship_request' ); 603 604 do_action( 'friends_remove_data', $user_id ); 605 } 606 add_action( 'wpmu_delete_user', 'friends_remove_data' ); 607 add_action( 'delete_user', 'friends_remove_data' ); 608 add_action( 'bp_make_spam_user', 'friends_remove_data' ); 609 610 611 /******************************************************************************** 612 * Caching 613 * 614 * Caching functions handle the clearing of cached objects and pages on specific 615 * actions throughout BuddyPress. 616 */ 617 618 function friends_clear_friend_object_cache( $friendship_id ) { 619 if ( !$friendship = new BP_Friends_Friendship( $friendship_id ) ) 620 return false; 621 622 wp_cache_delete( 'friends_friend_ids_' . $friendship->initiator_user_id, 'bp' ); 623 wp_cache_delete( 'friends_friend_ids_' . $friendship->friend_user_id, 'bp' ); 624 wp_cache_delete( 'bp_total_friend_count_' . $friendship->initiator_user_id, 'bp' ); 625 wp_cache_delete( 'bp_total_friend_count_' . $friendship->friend_user_id, 'bp' ); 626 } 627 628 function friends_clear_friend_notifications() { 629 global $bp; 630 631 if ( isset( $_GET['new'] ) ) 632 bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->friends->id, 'friendship_accepted' ); 633 } 634 add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' ); 635 636 // List actions to clear object caches on 637 add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' ); 638 add_action( 'friends_friendship_deleted', 'friends_clear_friend_object_cache' ); 639 640 // List actions to clear super cached pages on, if super cache is installed 641 add_action( 'friends_friendship_rejected', 'bp_core_clear_cache' ); 642 add_action( 'friends_friendship_accepted', 'bp_core_clear_cache' ); 643 add_action( 'friends_friendship_deleted', 'bp_core_clear_cache' ); 644 add_action( 'friends_friendship_requested', 'bp_core_clear_cache' ); 152 // Create the friends component 153 $bp->friends = new BP_Friends_Component(); 645 154 646 155 ?>
Note: See TracChangeset
for help on using the changeset viewer.