Changeset 3753
- Timestamp:
- 01/20/2011 05:44:22 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-activity.php
r3751 r3753 1 1 <?php 2 3 /** 4 * BuddyPress Activity Streams 5 * 6 * An activity stream component, for users, groups, and blog tracking. 7 * 8 * @package BuddyPress 9 * @subpackage Activity Core 10 */ 2 11 3 12 class BP_Activity_Component extends BP_Component { … … 5 14 /** 6 15 * Start the activity component creation process 16 * 17 * @since BuddyPress {unknown} 7 18 */ 8 19 function BP_Activity_Component() { … … 13 24 * Setup globals 14 25 * 26 * The BP_ACTIVITY_SLUG constant is deprecated, and only used here for 27 * backwards compatibility. 28 * 29 * @since BuddyPress {unknown} 15 30 * @global obj $bp 16 31 */ … … 37 52 */ 38 53 function _includes() { 39 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-actions.php' 40 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-filters.php' 41 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-screens.php' 42 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-classes.php' 43 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity- functions.php');44 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity- template.php' );54 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-actions.php' ); 55 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-filters.php' ); 56 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-screens.php' ); 57 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-classes.php' ); 58 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-template.php' ); 59 require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-functions.php' ); 45 60 } 46 61 … … 248 263 /** Actions *******************************************************************/ 249 264 265 /** 266 * Sets the current action for a given activity stream location 267 * 268 * @global obj $bp 269 * @param str $component_id 270 * @param str $key 271 * @param str $value 272 * @return bool False on error, True on success 273 */ 250 274 function bp_activity_set_action( $component_id, $key, $value ) { 251 275 global $bp; 252 276 277 // Return false if any of the above values are not set 253 278 if ( empty( $component_id ) || empty( $key ) || empty( $value ) ) 254 279 return false; 255 280 281 // Set activity action 256 282 $bp->activity->actions->{$component_id}->{$key} = apply_filters( 'bp_activity_set_action', array( 257 283 'key' => $key, 258 284 'value' => $value 259 285 ), $component_id, $key, $value ); 260 } 261 286 287 return true; 288 } 289 290 /** 291 * Retreives the current action from a component and key 292 * 293 * @global obj $bp 294 * @param str $component_id 295 * @param str $key 296 * @return mixed False on error, action on success 297 */ 262 298 function bp_activity_get_action( $component_id, $key ) { 263 299 global $bp; 264 300 301 // Return false if any of the above values are not set 265 302 if ( empty( $component_id ) || empty( $key ) ) 266 303 return false; … … 271 308 /** Favorites *****************************************************************/ 272 309 273 function bp_activity_get_user_favorites( $user_id ) { 274 $my_favs = maybe_unserialize( get_user_meta( $user_id, 'bp_favorite_activities', true ) ); 275 $existing_favs = bp_activity_get_specific( array( 'activity_ids' => $my_favs ) ); 310 /** 311 * Get a users favorite activity stream items 312 * 313 * @global obj $bp 314 * @param int $user_id 315 * @return array Array of users favorite activity stream ID's 316 */ 317 function bp_activity_get_user_favorites( $user_id = 0 ) { 318 global $bp; 319 320 // Fallback to logged in user if no user_id is passed 321 if ( empty( $user_id ) ) 322 $user_id = $bp->displayed_user->id; 323 324 // Get favorites for user 325 $favs = get_user_meta( $user_id, 'bp_favorite_activities', true ); 326 $existing_favs = bp_activity_get_specific( array( 'activity_ids' => $favs ) ); 276 327 277 328 foreach( (array)$existing_favs['activities'] as $fav ) … … 284 335 } 285 336 286 function bp_activity_add_user_favorite( $activity_id, $user_id = false ) { 287 global $bp; 288 289 if ( !$user_id ) 337 /** 338 * Add an activity stream item as a favorite for a user 339 * 340 * @global obj $bp 341 * @param int $activity_id 342 * @param int $user_id 343 * @return bool 344 */ 345 function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 346 global $bp; 347 348 // Favorite activity stream items are for logged in users only 349 if ( !is_user_logged_in() ) 350 return false; 351 352 // Fallback to logged in user if no user_id is passed 353 if ( empty( $user_id ) ) 290 354 $user_id = $bp->loggedin_user->id; 291 355 292 356 // Update the user's personal favorites 293 $my_favs = maybe_unserialize( get_user_meta( $bp->loggedin_user->id, 'bp_favorite_activities', true ));357 $my_favs = get_user_meta( $bp->loggedin_user->id, 'bp_favorite_activities', true ); 294 358 $my_favs[] = $activity_id; 295 359 296 360 // Update the total number of users who have favorited this activity 297 361 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 298 299 if ( !empty( $fav_count ) ) 300 $fav_count = (int)$fav_count + 1; 301 else 302 $fav_count = 1; 303 362 $fav_count = !empty( $fav_count ) ? (int)$fav_count + 1 : 1; 363 364 // Update user meta 304 365 update_user_meta( $bp->loggedin_user->id, 'bp_favorite_activities', $my_favs ); 305 bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ); 306 307 do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id ); 308 309 return true; 310 } 311 312 function bp_activity_remove_user_favorite( $activity_id, $user_id = false ) { 313 global $bp; 314 315 if ( !$user_id ) 366 367 // Update activity meta counts 368 if ( true === bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) { 369 370 // Execute additional code 371 do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id ); 372 373 // Success 374 return true; 375 376 // Saving meta was unsuccessful for an unknown reason 377 } else { 378 // Execute additional code 379 do_action( 'bp_activity_add_user_favorite_fail', $activity_id, $user_id ); 380 381 return false; 382 } 383 } 384 385 function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) { 386 global $bp; 387 388 // Favorite activity stream items are for logged in users only 389 if ( !is_user_logged_in() ) 390 return false; 391 392 // Fallback to logged in user if no user_id is passed 393 if ( empty( $user_id ) ) 316 394 $user_id = $bp->loggedin_user->id; 317 395 318 396 // Remove the fav from the user's favs 319 $my_favs = maybe_unserialize( get_user_meta( $user_id, 'bp_favorite_activities', true ));397 $my_favs = get_user_meta( $user_id, 'bp_favorite_activities', true ); 320 398 $my_favs = array_flip( (array) $my_favs ); 321 399 unset( $my_favs[$activity_id] ); … … 323 401 324 402 // Update the total number of users who have favorited this activity 325 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 326 327 if ( !empty( $fav_count ) ) { 328 $fav_count = (int)$fav_count - 1; 329 bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ); 330 } 331 332 update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ); 333 334 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id ); 335 336 return true; 337 } 338 403 if ( $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ) ) { 404 405 // Deduct from total favorites 406 if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int)$fav_count - 1 ) ) { 407 408 // Update users favorites 409 if ( update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) { 410 411 // Execute additional code 412 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id ); 413 414 // Success 415 return true; 416 417 // Error updating 418 } else { 419 return false; 420 } 421 422 // Error updating favorite count 423 } else { 424 return false; 425 } 426 427 // Error getting favorite count 428 } else { 429 return false; 430 } 431 } 432 433 /** 434 * Check if activity exists by scanning content 435 * 436 * @param str $content 437 * @return bool 438 */ 339 439 function bp_activity_check_exists_by_content( $content ) { 340 440 return apply_filters( 'bp_activity_check_exists_by_content', BP_Activity_Activity::check_exists_by_content( $content ) ); 341 441 } 342 442 443 /** 444 * Retreive the last time activity was updated 445 * 446 * @return str 447 */ 343 448 function bp_activity_get_last_updated() { 344 449 return apply_filters( 'bp_activity_get_last_updated', BP_Activity_Activity::get_last_updated() ); 345 450 } 346 451 347 function bp_activity_total_favorites_for_user( $user_id = false ) { 348 global $bp; 349 350 if ( !$user_id ) 452 /** 453 * Retreive the number of favorite activity stream items a user has 454 * 455 * @global obj $bp 456 * @param int $user_id 457 * @return int 458 */ 459 function bp_activity_total_favorites_for_user( $user_id = 0 ) { 460 global $bp; 461 462 // Fallback on displayed user, and then logged in user 463 if ( empty( $user_id ) ) 351 464 $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id; 352 465 … … 356 469 /** Meta **********************************************************************/ 357 470 358 359 function bp_activity_delete_meta( $activity_id, $meta_key = false, $meta_value = false ) { 471 /** 472 * Delete a meta entry from the DB for an activity stream item 473 * 474 * @global DB $wpdb 475 * @global obj $bp 476 * @param int $activity_id 477 * @param str $meta_key 478 * @param str $meta_value 479 * @return bool 480 */ 481 function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' ) { 360 482 global $wpdb, $bp; 361 483 484 // Return false if any of the above values are not set 362 485 if ( !is_numeric( $activity_id ) ) 363 486 return false; 364 487 488 // Sanitize key 365 489 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 366 490 … … 368 492 $meta_value = serialize( $meta_value ); 369 493 494 // Trim off whitespace 370 495 $meta_value = trim( $meta_value ); 371 496 372 if ( !$meta_key ) 373 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) ); 497 // Delete all for activity_id 498 if ( empty( $meta_key ) ) 499 $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) ); 500 501 // Delete only when all match 374 502 else if ( $meta_value ) 375 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s AND meta_value = %s", $activity_id, $meta_key, $meta_value ) ); 503 $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s AND meta_value = %s", $activity_id, $meta_key, $meta_value ) ); 504 505 // Delete only when activity_id and meta_key match 376 506 else 377 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) ); 378 507 $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) ); 508 509 // Delete cache entry 379 510 wp_cache_delete( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, 'bp' ); 380 511 381 return true; 382 } 383 384 function bp_activity_get_meta( $activity_id, $meta_key = '' ) { 512 // Success 513 if ( !is_wp_error( $retval ) ) 514 return true; 515 516 // Fail 517 else 518 return false; 519 } 520 521 /** 522 * Get activity meta 523 * 524 * @global DB $wpdb 525 * @global obj $bp 526 * @param int $activity_id 527 * @param str $meta_key 528 * @return bool 529 */ 530 function bp_activity_get_meta( $activity_id = 0, $meta_key = '' ) { 385 531 global $wpdb, $bp; 386 532 387 $activity_id = (int)$activity_id; 388 389 if ( !$activity_id ) 390 return false; 391 392 if ( !empty($meta_key) ) { 533 // Make sure activity_id is valid 534 if ( empty( $activity_id ) || !is_numeric( $activity_id ) ) 535 return false; 536 537 // We have a key to look for 538 if ( !empty( $meta_key ) ) { 539 540 // Sanitize key 393 541 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 394 542 543 // Check cache 395 544 if ( !$metas = wp_cache_get( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, 'bp' ) ) { 545 546 // No cache so hit the DB 396 547 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) ); 548 549 // Set cache 397 550 wp_cache_set( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, $metas, 'bp' ); 398 551 } 399 } else 552 553 // No key so get all for activity_id 554 } else { 400 555 $metas = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) ); 401 402 if ( empty($metas) ) 403 return false; 404 556 } 557 558 // No result so return false 559 if ( empty( $metas ) ) 560 return false; 561 562 // Maybe, just maybe... unserialize 405 563 $metas = array_map( 'maybe_unserialize', (array)$metas ); 406 564 407 if ( 1 == count($metas) ) 408 return $metas[0]; 409 else 410 return $metas; 411 } 412 565 // Return first item in array if only 1, else return all metas found 566 $retval = ( 1 == count( $metas ) ? $metas[0] : $metas ); 567 568 // Filter result before returning 569 return apply_filters( 'bp_activity_get_meta', $retval, $activity_id, $meta_key ); 570 } 571 572 /** 573 * Update activity meta 574 * 575 * @global DB $wpdb 576 * @global obj $bp 577 * @param int $activity_id 578 * @param str $meta_key 579 * @param str $meta_value 580 * @return bool 581 */ 413 582 function bp_activity_update_meta( $activity_id, $meta_key, $meta_value ) { 414 583 global $wpdb, $bp; 415 584 585 // Make sure activity_id is valid 416 586 if ( !is_numeric( $activity_id ) ) 417 587 return false; 418 588 589 // Sanitize key 419 590 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 420 591 592 // Sanitize value 421 593 if ( is_string( $meta_value ) ) 422 594 $meta_value = stripslashes( $wpdb->escape( $meta_value ) ); 423 595 596 // Maybe, just maybe... serialize 424 597 $meta_value = maybe_serialize( $meta_value ); 425 598 599 // If value is empty, delete the meta key 426 600 if ( empty( $meta_value ) ) 427 601 return bp_activity_delete_meta( $activity_id, $meta_key ); 428 602 603 // See if meta key exists for activity_id 429 604 $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) ); 430 605 431 if ( !$cur ) 606 // Meta key does not exist so INSERT 607 if ( empty( $cur ) ) 432 608 $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_meta} ( activity_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $activity_id, $meta_key, $meta_value ) ); 609 610 // Meta key exists, so UPDATE 433 611 else if ( $cur->meta_value != $meta_value ) 434 612 $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name_meta} SET meta_value = %s WHERE activity_id = %d AND meta_key = %s", $meta_value, $activity_id, $meta_key ) ); 613 614 // Weirdness, so return false 435 615 else 436 616 return false; 437 617 618 // Set cache 438 619 wp_cache_set( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, $meta_value, 'bp' ); 439 620 621 // Victory is ours! 440 622 return true; 441 623 } … … 443 625 /** Clean up ******************************************************************/ 444 626 445 function bp_activity_remove_data( $user_id ) { 627 /** 628 * Completely remove 629 * @param int $user_id 630 */ 631 function bp_activity_remove_all_user_data( $user_id = 0 ) { 632 633 // Do not delete user data unless a logged in user says so 634 if ( empty( $user_id ) || !is_user_logged_in() ) 635 return false; 636 446 637 // Clear the user's activity from the sitewide stream and clear their activity tables 447 638 bp_activity_delete( array( 'user_id' => $user_id ) ); … … 451 642 delete_user_meta( $user_id, 'bp_favorite_activities' ); 452 643 453 do_action( 'bp_activity_remove_data', $user_id ); 454 } 455 add_action( 'wpmu_delete_user', 'bp_activity_remove_data' ); 456 add_action( 'delete_user', 'bp_activity_remove_data' ); 457 add_action( 'bp_make_spam_user', 'bp_activity_remove_data' ); 644 // Execute additional code 645 do_action( 'bp_activity_remove_data', $user_id ); // Deprecated! Do not use! 646 647 // Use this going forward 648 do_action( 'bp_activity_remove_all_user_data', $user_id ); 649 } 650 add_action( 'wpmu_delete_user', 'bp_activity_remove_all_user_data' ); 651 add_action( 'delete_user', 'bp_activity_remove_all_user_data' ); 652 add_action( 'bp_make_spam_user', 'bp_activity_remove_all_user_data' ); 458 653 459 654 /** 460 655 * Register the activity stream actions for updates 461 656 * 462 * @global array$bp657 * @global obj $bp 463 658 */ 464 659 function updates_register_activity_actions() {
Note: See TracChangeset
for help on using the changeset viewer.