Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/20/2011 10:53:49 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Second pass at huge component refactor.

Move component files out of root directory and append '-loader' to the file names. Split those files up into smaller pieces.

Abstract 'settings' component out of 'core' and into its own component for future extension, including new template files in bp-default.

Append '-sa' to bbPress bridge file for 'Stand Alone' compatibility, to make room for bbPress plugin compatibility layer.

Various bug fixes through-out all components. Various code clean-up and documentation through-out all components.

Still more refactoring to do.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-functions.php

    r3751 r3757  
    11<?php
     2
     3/**
     4 * BuddyPress Activity Functions
     5 *
     6 * Functions for the Activity Streams component
     7 *
     8 * @package BuddyPress
     9 * @subpackage Activity Core
     10 */
     11
     12/**
     13 * @todo Figure out if this is still needed
     14 *
     15 * @global obj $bp
     16 */
     17function bp_activity_directory_activity_setup() {
     18    global $bp;
     19
     20    if ( bp_is_activity_component() && empty( $bp->current_action ) ) {
     21        $bp->is_directory = true;
     22
     23        do_action( 'bp_activity_directory_activity_setup' );
     24
     25        bp_core_load_template( apply_filters( 'bp_activity_directory_activity_setup', 'activity/index' ) );
     26    }
     27}
     28add_action( 'wp', 'bp_activity_directory_activity_setup', 2 );
     29
     30/**
     31 * Searches through the content of an activity item to locate usernames, designated by an @ sign
     32 *
     33 * @package BuddyPress Activity
     34 * @since 1.3
     35 *
     36 * @param $content The content of the activity, usually found in $activity->content
     37 * @return array $usernames Array of the found usernames that match existing users
     38 */
     39function bp_activity_find_mentions( $content ) {
     40    $pattern = '/[@]+([A-Za-z0-9-_\.]+)/';
     41    preg_match_all( $pattern, $content, $usernames );
     42
     43    // Make sure there's only one instance of each username
     44    if ( !$usernames = array_unique( $usernames[1] ) )
     45        return false;
     46
     47    return $usernames;
     48}
     49
     50/**
     51 * Reduces new mention count for mentioned users when activity items are deleted
     52 *
     53 * @package BuddyPress Activity
     54 * @since 1.3
     55 *
     56 * @param $activity_id The unique id for the activity item
     57 */
     58function bp_activity_reduce_mention_count( $activity_id ) {
     59    $activity = new BP_Activity_Activity( $activity_id );
     60
     61    if ( $usernames = bp_activity_find_mentions( strip_tags( $activity->content ) ) ) {
     62        if ( ! function_exists( 'username_exists' ) )
     63            require_once( ABSPATH . WPINC . '/registration.php' );
     64
     65        foreach( (array)$usernames as $username ) {
     66            if ( !$user_id = username_exists( $username ) )
     67                continue;
     68
     69            // Decrease the number of new @ mentions for the user
     70            $new_mention_count = (int)get_user_meta( $user_id, 'bp_new_mention_count', true );
     71            update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count - 1 );
     72        }
     73    }
     74}
     75add_action( 'bp_activity_action_delete_activity', 'bp_activity_reduce_mention_count' );
     76
     77/**
     78 * Formats notifications related to activity
     79 *
     80 * @package BuddyPress Activity
     81 * @param $action The type of activity item. Just 'new_at_mention' for now
     82 * @param $item_id The activity id
     83 * @param $secondary_item_id In the case of at-mentions, this is the mentioner's id
     84 * @param $total_items The total number of notifications to format
     85 */
     86function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
     87    global $bp;
     88
     89    switch ( $action ) {
     90        case 'new_at_mention':
     91            $activity_id      = $item_id;
     92            $poster_user_id   = $secondary_item_id;
     93            $at_mention_link  = $bp->loggedin_user->domain . $bp->activity->slug . '/mentions/';
     94            $at_mention_title = sprintf( __( '@%s Mentions', 'buddypress' ), $bp->loggedin_user->userdata->user_nicename );
     95
     96            if ( (int)$total_items > 1 ) {
     97                return apply_filters( 'bp_activity_multiple_at_mentions_notification', '<a href="' . $at_mention_link . '" title="' . $at_mention_title . '">' . sprintf( __( 'You have %1$d new activity mentions', 'buddypress' ), (int)$total_items ) . '</a>', $at_mention_link, $total_items, $activity_id, $poster_user_id );
     98            } else {
     99                $user_fullname = bp_core_get_user_displayname( $poster_user_id );
     100
     101                return apply_filters( 'bp_activity_single_at_mentions_notification', '<a href="' . $at_mention_link . '" title="' . $at_mention_title . '">' . sprintf( __( '%1$s mentioned you in an activity update', 'buddypress' ), $user_fullname ) . '</a>', $at_mention_link, $total_items, $activity_id, $poster_user_id );
     102            }
     103        break;
     104    }
     105
     106    do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
     107
     108    return false;
     109}
     110
     111/** Actions *******************************************************************/
     112
     113/**
     114 * Sets the current action for a given activity stream location
     115 *
     116 * @global obj $bp
     117 * @param str $component_id
     118 * @param str $key
     119 * @param str $value
     120 * @return bool False on error, True on success
     121 */
     122function bp_activity_set_action( $component_id, $key, $value ) {
     123    global $bp;
     124
     125    // Return false if any of the above values are not set
     126    if ( empty( $component_id ) || empty( $key ) || empty( $value ) )
     127        return false;
     128
     129    // Set activity action
     130    $bp->activity->actions->{$component_id}->{$key} = apply_filters( 'bp_activity_set_action', array(
     131        'key'   => $key,
     132        'value' => $value
     133    ), $component_id, $key, $value );
     134
     135    return true;
     136}
     137
     138/**
     139 * Retreives the current action from a component and key
     140 *
     141 * @global obj $bp
     142 * @param str $component_id
     143 * @param str $key
     144 * @return mixed False on error, action on success
     145 */
     146function bp_activity_get_action( $component_id, $key ) {
     147    global $bp;
     148
     149    // Return false if any of the above values are not set
     150    if ( empty( $component_id ) || empty( $key ) )
     151        return false;
     152
     153    return apply_filters( 'bp_activity_get_action', $bp->activity->actions->{$component_id}->{$key}, $component_id, $key );
     154}
     155
     156/** Favorites *****************************************************************/
     157
     158/**
     159 * Get a users favorite activity stream items
     160 *
     161 * @global obj $bp
     162 * @param int $user_id
     163 * @return array Array of users favorite activity stream ID's
     164 */
     165function bp_activity_get_user_favorites( $user_id = 0 ) {
     166    global $bp;
     167
     168    // Fallback to logged in user if no user_id is passed
     169    if ( empty( $user_id ) )
     170        $user_id = $bp->displayed_user->id;
     171
     172    // Get favorites for user
     173    $favs          = get_user_meta( $user_id, 'bp_favorite_activities', true );
     174    $existing_favs = bp_activity_get_specific( array( 'activity_ids' => $favs ) );
     175
     176    foreach( (array)$existing_favs['activities'] as $fav )
     177        $new_favs[] = $fav->id;
     178
     179    $new_favs = array_unique( (array)$new_favs );
     180    update_user_meta( $user_id, 'bp_favorite_activities', $new_favs );
     181
     182    return apply_filters( 'bp_activity_get_user_favorites', $new_favs );
     183}
     184
     185/**
     186 * Add an activity stream item as a favorite for a user
     187 *
     188 * @global obj $bp
     189 * @param int $activity_id
     190 * @param int $user_id
     191 * @return bool
     192 */
     193function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) {
     194    global $bp;
     195
     196    // Favorite activity stream items are for logged in users only
     197    if ( !is_user_logged_in() )
     198        return false;
     199
     200    // Fallback to logged in user if no user_id is passed
     201    if ( empty( $user_id ) )
     202        $user_id = $bp->loggedin_user->id;
     203
     204    // Update the user's personal favorites
     205    $my_favs   = get_user_meta( $bp->loggedin_user->id, 'bp_favorite_activities', true );
     206    $my_favs[] = $activity_id;
     207
     208    // Update the total number of users who have favorited this activity
     209    $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
     210    $fav_count = !empty( $fav_count ) ? (int)$fav_count + 1 : 1;
     211
     212    // Update user meta
     213    update_user_meta( $bp->loggedin_user->id, 'bp_favorite_activities', $my_favs );
     214
     215    // Update activity meta counts
     216    if ( true === bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {
     217
     218        // Execute additional code
     219        do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id );
     220
     221        // Success
     222        return true;
     223
     224    // Saving meta was unsuccessful for an unknown reason
     225    } else {
     226        // Execute additional code
     227        do_action( 'bp_activity_add_user_favorite_fail', $activity_id, $user_id );
     228
     229        return false;
     230    }
     231}
     232
     233function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) {
     234    global $bp;
     235
     236    // Favorite activity stream items are for logged in users only
     237    if ( !is_user_logged_in() )
     238        return false;
     239
     240    // Fallback to logged in user if no user_id is passed
     241    if ( empty( $user_id ) )
     242        $user_id = $bp->loggedin_user->id;
     243
     244    // Remove the fav from the user's favs
     245    $my_favs = get_user_meta( $user_id, 'bp_favorite_activities', true );
     246    $my_favs = array_flip( (array) $my_favs );
     247    unset( $my_favs[$activity_id] );
     248    $my_favs = array_unique( array_flip( $my_favs ) );
     249
     250    // Update the total number of users who have favorited this activity
     251    if ( $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ) ) {
     252
     253        // Deduct from total favorites
     254        if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int)$fav_count - 1 ) ) {
     255
     256            // Update users favorites
     257            if ( update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {
     258
     259                // Execute additional code
     260                do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
     261
     262                // Success
     263                return true;
     264
     265            // Error updating
     266            } else {
     267                return false;
     268            }
     269
     270        // Error updating favorite count
     271        } else {
     272            return false;
     273        }
     274
     275    // Error getting favorite count
     276    } else {
     277        return false;
     278    }
     279}
     280
     281/**
     282 * Check if activity exists by scanning content
     283 *
     284 * @param str $content
     285 * @return bool
     286 */
     287function bp_activity_check_exists_by_content( $content ) {
     288    return apply_filters( 'bp_activity_check_exists_by_content', BP_Activity_Activity::check_exists_by_content( $content ) );
     289}
     290
     291/**
     292 * Retreive the last time activity was updated
     293 *
     294 * @return str
     295 */
     296function bp_activity_get_last_updated() {
     297    return apply_filters( 'bp_activity_get_last_updated', BP_Activity_Activity::get_last_updated() );
     298}
     299
     300/**
     301 * Retreive the number of favorite activity stream items a user has
     302 *
     303 * @global obj $bp
     304 * @param int $user_id
     305 * @return int
     306 */
     307function bp_activity_total_favorites_for_user( $user_id = 0 ) {
     308    global $bp;
     309
     310    // Fallback on displayed user, and then logged in user
     311    if ( empty( $user_id ) )
     312        $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
     313
     314    return BP_Activity_Activity::total_favorite_count( $user_id );
     315}
     316
     317/** Meta **********************************************************************/
     318
     319/**
     320 * Delete a meta entry from the DB for an activity stream item
     321 *
     322 * @global DB $wpdb
     323 * @global obj $bp
     324 * @param int $activity_id
     325 * @param str $meta_key
     326 * @param str $meta_value
     327 * @return bool
     328 */
     329function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' ) {
     330    global $wpdb, $bp;
     331
     332    // Return false if any of the above values are not set
     333    if ( !is_numeric( $activity_id ) )
     334        return false;
     335
     336    // Sanitize key
     337    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     338
     339    if ( is_array( $meta_value ) || is_object( $meta_value ) )
     340        $meta_value = serialize( $meta_value );
     341
     342    // Trim off whitespace
     343    $meta_value = trim( $meta_value );
     344
     345    // Delete all for activity_id
     346    if ( empty( $meta_key ) )
     347        $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );
     348
     349    // Delete only when all match
     350    else if ( $meta_value )
     351        $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 ) );
     352
     353    // Delete only when activity_id and meta_key match
     354    else
     355        $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) );
     356
     357    // Delete cache entry
     358    wp_cache_delete( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, 'bp' );
     359
     360    // Success
     361    if ( !is_wp_error( $retval ) )
     362        return true;
     363
     364    // Fail
     365    else
     366        return false;
     367}
     368
     369/**
     370 * Get activity meta
     371 *
     372 * @global DB $wpdb
     373 * @global obj $bp
     374 * @param int $activity_id
     375 * @param str $meta_key
     376 * @return bool
     377 */
     378function bp_activity_get_meta( $activity_id = 0, $meta_key = '' ) {
     379    global $wpdb, $bp;
     380
     381    // Make sure activity_id is valid
     382    if ( empty( $activity_id ) || !is_numeric( $activity_id ) )
     383        return false;
     384
     385    // We have a key to look for
     386    if ( !empty( $meta_key ) ) {
     387
     388        // Sanitize key
     389        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     390
     391        // Check cache
     392        if ( !$metas = wp_cache_get( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, 'bp' ) ) {
     393
     394            // No cache so hit the DB
     395            $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 ) );
     396
     397            // Set cache
     398            wp_cache_set( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, $metas, 'bp' );
     399        }
     400
     401    // No key so get all for activity_id
     402    } else {
     403        $metas = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );
     404    }
     405
     406    // No result so return false
     407    if ( empty( $metas ) )
     408        return false;
     409
     410    // Maybe, just maybe... unserialize
     411    $metas = array_map( 'maybe_unserialize', (array)$metas );
     412
     413    // Return first item in array if only 1, else return all metas found
     414    $retval = ( 1 == count( $metas ) ? $metas[0] : $metas );
     415
     416    // Filter result before returning
     417    return apply_filters( 'bp_activity_get_meta', $retval, $activity_id, $meta_key );
     418}
     419
     420/**
     421 * Update activity meta
     422 *
     423 * @global DB $wpdb
     424 * @global obj $bp
     425 * @param int $activity_id
     426 * @param str $meta_key
     427 * @param str $meta_value
     428 * @return bool
     429 */
     430function bp_activity_update_meta( $activity_id, $meta_key, $meta_value ) {
     431    global $wpdb, $bp;
     432
     433    // Make sure activity_id is valid
     434    if ( !is_numeric( $activity_id ) )
     435        return false;
     436
     437    // Sanitize key
     438    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     439
     440    // Sanitize value
     441    if ( is_string( $meta_value ) )
     442        $meta_value = stripslashes( $wpdb->escape( $meta_value ) );
     443
     444    // Maybe, just maybe... serialize
     445    $meta_value = maybe_serialize( $meta_value );
     446
     447    // If value is empty, delete the meta key
     448    if ( empty( $meta_value ) )
     449        return bp_activity_delete_meta( $activity_id, $meta_key );
     450
     451    // See if meta key exists for activity_id
     452    $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 ) );
     453
     454    // Meta key does not exist so INSERT
     455    if ( empty( $cur ) )
     456        $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 ) );
     457
     458    // Meta key exists, so UPDATE
     459    else if ( $cur->meta_value != $meta_value )
     460        $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 ) );
     461
     462    // Weirdness, so return false
     463    else
     464        return false;
     465
     466    // Set cache
     467    wp_cache_set( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, $meta_value, 'bp' );
     468
     469    // Victory is ours!
     470    return true;
     471}
     472
     473/** Clean up ******************************************************************/
     474
     475/**
     476 * Completely remove
     477 * @param int $user_id
     478 */
     479function bp_activity_remove_all_user_data( $user_id = 0 ) {
     480
     481    // Do not delete user data unless a logged in user says so
     482    if ( empty( $user_id ) || !is_user_logged_in() )
     483        return false;
     484
     485    // Clear the user's activity from the sitewide stream and clear their activity tables
     486    bp_activity_delete( array( 'user_id' => $user_id ) );
     487
     488    // Remove any usermeta
     489    delete_user_meta( $user_id, 'bp_latest_update' );
     490    delete_user_meta( $user_id, 'bp_favorite_activities' );
     491
     492    // Execute additional code
     493    do_action( 'bp_activity_remove_data', $user_id ); // Deprecated! Do not use!
     494
     495    // Use this going forward
     496    do_action( 'bp_activity_remove_all_user_data', $user_id );
     497}
     498add_action( 'wpmu_delete_user',  'bp_activity_remove_all_user_data' );
     499add_action( 'delete_user',       'bp_activity_remove_all_user_data' );
     500add_action( 'bp_make_spam_user', 'bp_activity_remove_all_user_data' );
     501
     502/**
     503 * Register the activity stream actions for updates
     504 *
     505 * @global obj $bp
     506 */
     507function updates_register_activity_actions() {
     508    global $bp;
     509
     510    bp_activity_set_action( $bp->activity->id, 'activity_update', __( 'Posted an update', 'buddypress' ) );
     511
     512    do_action( 'updates_register_activity_actions' );
     513}
     514add_action( 'bp_register_activity_actions', 'updates_register_activity_actions' );
    2515
    3516/*******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.