Skip to:
Content

BuddyPress.org

Changeset 2077 for trunk/bp-messages.php


Ignore:
Timestamp:
11/02/2009 07:54:21 PM (16 years ago)
Author:
apeatling
Message:

Merging 1.1 branch changes and syncing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-messages.php

    r2020 r2077  
    1515if ( !defined( 'BP_IGNORE_DEPRECATED' ) )
    1616    require ( BP_PLUGIN_DIR . '/bp-messages/deprecated/bp-messages-deprecated.php' );
    17    
     17
    1818function messages_install() {
    1919    global $wpdb, $bp;
    20    
     20
    2121    if ( !empty($wpdb->charset) )
    2222        $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    23    
     23
    2424    $sql[] = "CREATE TABLE {$bp->messages->table_name_threads} (
    2525                id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     
    3333                KEY last_sender_id (last_sender_id)
    3434               ) {$charset_collate};";
    35    
     35
    3636    $sql[] = "CREATE TABLE {$bp->messages->table_name_recipients} (
    3737                id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     
    5656                KEY sender_id (sender_id)
    5757               ) {$charset_collate};";
    58    
     58
    5959    $sql[] = "CREATE TABLE {$bp->messages->table_name_notices} (
    6060                id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     
    6565                KEY is_active (is_active)
    6666               ) {$charset_collate};";
    67    
     67
    6868    require_once( ABSPATH . 'wp-admin/upgrade-functions.php' );
    6969    dbDelta($sql);
    70    
     70
    7171    add_site_option( 'bp-messages-db-version', BP_MESSAGES_DB_VERSION );
    7272}
     
    7777    /* For internal identification */
    7878    $bp->messages->id = 'messages';
    79    
     79
    8080    $bp->messages->table_name_threads = $wpdb->base_prefix . 'bp_messages_threads';
    8181    $bp->messages->table_name_messages = $wpdb->base_prefix . 'bp_messages_messages';
     
    9191    do_action( 'messages_setup_globals' );
    9292}
    93 add_action( 'plugins_loaded', 'messages_setup_globals', 5 );   
     93add_action( 'plugins_loaded', 'messages_setup_globals', 5 );
    9494add_action( 'admin_menu', 'messages_setup_globals', 2 );
    9595
    96 function messages_check_installed() {   
     96function messages_check_installed() {
    9797    global $wpdb, $bp;
    9898
    9999    if ( !is_site_admin() )
    100100        return false;
    101    
     101
    102102    /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
    103103    if ( get_site_option('bp-messages-db-version') < BP_MESSAGES_DB_VERSION )
     
    117117    /* Add 'Messages' to the main navigation */
    118118    bp_core_new_nav_item( array( 'name' => __('Messages', 'buddypress'), 'slug' => $bp->messages->slug, 'position' => 50, 'show_for_displayed_user' => false, 'screen_function' => 'messages_screen_inbox', 'default_subnav_slug' => 'inbox', 'item_css_id' => $bp->messages->id ) );
    119    
     119
    120120    $messages_link = $bp->loggedin_user->domain . $bp->messages->slug . '/';
    121    
     121
    122122    /* Add the subnav items to the profile */
    123123    bp_core_new_subnav_item( array( 'name' => __( 'Inbox', 'buddypress' ) . $count_indicator, 'slug' => 'inbox', 'parent_url' => $messages_link, 'parent_slug' => $bp->messages->slug, 'screen_function' => 'messages_screen_inbox', 'position' => 10, 'user_has_access' => bp_is_home() ) );
    124124    bp_core_new_subnav_item( array( 'name' => __( 'Sent Messages', 'buddypress' ), 'slug' => 'sentbox', 'parent_url' => $messages_link, 'parent_slug' => $bp->messages->slug, 'screen_function' => 'messages_screen_sentbox', 'position' => 20, 'user_has_access' => bp_is_home() ) );
    125125    bp_core_new_subnav_item( array( 'name' => __( 'Compose', 'buddypress' ), 'slug' => 'compose', 'parent_url' => $messages_link, 'parent_slug' => $bp->messages->slug, 'screen_function' => 'messages_screen_compose', 'position' => 30, 'user_has_access' => bp_is_home() ) );
    126    
     126
    127127    if ( is_site_admin() )
    128128        bp_core_new_subnav_item( array( 'name' => __( 'Notices', 'buddypress' ), 'slug' => 'notices', 'parent_url' => $messages_link, 'parent_slug' => $bp->messages->slug, 'screen_function' => 'messages_screen_notices', 'position' => 90, 'user_has_access' => is_site_admin() ) );
     
    130130    if ( $bp->current_component == $bp->messages->slug ) {
    131131        if ( bp_is_home() ) {
    132             $bp->bp_options_title = __( 'My Messages', 'buddypress' );         
     132            $bp->bp_options_title = __( 'My Messages', 'buddypress' );
    133133        } else {
    134134            $bp_options_avatar =  bp_core_fetch_avatar( array( 'item_id' => $bp->displayed_user->id, 'type' => 'thumb' ) );
     
    136136        }
    137137    }
    138    
     138
    139139    do_action( 'messages_setup_nav' );
    140140}
     
    153153function messages_screen_inbox() {
    154154    do_action( 'messages_screen_inbox' );
    155     bp_core_load_template( apply_filters( 'messages_template_inbox', 'messages/index' ) ); 
     155    bp_core_load_template( apply_filters( 'messages_template_inbox', 'messages/index' ) );
    156156}
    157157
     
    163163function messages_screen_compose() {
    164164    global $bp;
    165    
     165
    166166    // Remove any saved message data from a previous session.
    167167    messages_remove_callback_values();
     
    169169    /* Check if the message form has been submitted */
    170170    if ( isset( $_POST['send'] ) ) {
    171        
     171
    172172        /* Check the nonce */
    173173        check_admin_referer( 'messages_send_message' );
    174        
     174
    175175        /* Check we have what we need */
    176176        if ( empty( $_POST['subject'] ) || empty( $_POST['content'] ) ) {
     
    185185                $typed_recipients = explode( ' ', $_POST['send_to_usernames'] );
    186186                $recipients = array_merge( (array) $autocomplete_recipients, (array) $typed_recipients );
    187            
     187
    188188                /* Send the message */
    189189                if ( $thread_id = messages_new_message( array( 'recipients' => $recipients, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) ) {
     
    197197
    198198    }
    199    
     199
    200200    do_action( 'messages_screen_compose' );
    201    
     201
    202202    bp_core_load_template( apply_filters( 'messages_template_compose', 'messages/compose' ) );
    203203}
     
    205205function messages_screen_notices() {
    206206    global $bp, $notice_id;
    207        
     207
    208208    if ( !is_site_admin() )
    209209        return false;
    210        
     210
    211211    $notice_id = $bp->action_variables[1];
    212212
     
    216216        if ( 'deactivate' == $bp->action_variables[0] ) {
    217217            if ( !$notice->deactivate() ) {
    218                 bp_core_add_message( __('There was a problem deactivating that notice.', 'buddypress'), 'error' ); 
     218                bp_core_add_message( __('There was a problem deactivating that notice.', 'buddypress'), 'error' );
    219219            } else {
    220220                bp_core_add_message( __('Notice deactivated.', 'buddypress') );
     
    235235        bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/notices' );
    236236    }
    237    
     237
    238238    do_action( 'messages_screen_notices' );
    239    
    240     bp_core_load_template( apply_filters( 'messages_template_notices', 'messages/notices' ) ); 
    241 }
    242 
    243 function messages_screen_notification_settings() { 
     239
     240    bp_core_load_template( apply_filters( 'messages_template_notices', 'messages/notices' ) );
     241}
     242
     243function messages_screen_notification_settings() {
    244244    global $current_user; ?>
    245245    <table class="notification-settings" id="messages-notification-settings">
     
    262262            <td class="no"><input type="radio" name="notifications[notification_messages_new_notice]" value="no" <?php if ( 'no' == get_usermeta( $current_user->id, 'notification_messages_new_notice' ) ) { ?>checked="checked" <?php } ?>/></td>
    263263        </tr>
    264        
     264
    265265        <?php do_action( 'messages_screen_notification_settings' ) ?>
    266266    </table>
    267 <?php   
     267<?php
    268268}
    269269add_action( 'bp_notification_settings', 'messages_screen_notification_settings', 2 );
     
    280280function messages_action_view_message() {
    281281    global $bp, $thread_id;
    282    
     282
    283283    if ( $bp->current_component != $bp->messages->slug || $bp->current_action != 'view' )
    284284        return false;
    285        
     285
    286286    $thread_id = $bp->action_variables[0];
    287    
     287
    288288    if ( !$thread_id || !messages_is_valid_thread( $thread_id ) || ( !messages_check_thread_access($thread_id) && !is_site_admin() ) )
    289289        bp_core_redirect( $bp->displayed_user->domain . $bp->current_component );
    290        
     290
    291291    /* Check if a new reply has been submitted */
    292292    if ( isset( $_POST['send'] ) ) {
    293        
     293
    294294        /* Check the nonce */
    295295        check_admin_referer( 'messages_send_message', 'send_message_nonce' );
    296        
     296
    297297        /* Send the reply */
    298298        if ( messages_new_message( array( 'thread_id' => $thread_id, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) )
     
    300300        else
    301301            bp_core_add_message( __( 'There was a problem sending your reply, please try again', 'buddypress' ), 'error' );
    302        
    303         bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/view/' . $thread_id . '/' );     
    304     }
    305    
     302
     303        bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/view/' . $thread_id . '/' );
     304    }
     305
    306306    /* Mark message read */
    307307    messages_mark_thread_read( $thread_id );
    308    
     308
    309309    do_action( 'messages_action_view_message' );
    310    
     310
    311311    bp_core_new_subnav_item( array( 'name' => sprintf( __( 'From: %s', 'buddypress'), BP_Messages_Thread::get_last_sender($thread_id) ), 'slug' => 'view', 'parent_url' => $bp->loggedin_user->domain . $bp->messages->slug . '/', 'parent_slug' => $bp->messages->slug, 'screen_function' => true, 'position' => 40, 'user_has_access' => bp_is_home() ) );
    312312    bp_core_load_template( apply_filters( 'messages_template_view_message', 'messages/view' ) );
     
    316316function messages_action_delete_message() {
    317317    global $bp, $thread_id;
    318    
     318
    319319    if ( $bp->current_component != $bp->messages->slug || 'notices' == $bp->current_action || $bp->action_variables[0] != 'delete' )
    320320        return false;
    321    
     321
    322322    $thread_id = $bp->action_variables[1];
    323323
     
    341341function messages_action_bulk_delete() {
    342342    global $bp, $thread_ids;
    343    
     343
    344344    if ( $bp->current_component != $bp->messages->slug || $bp->action_variables[0] != 'bulk-delete' )
    345345        return false;
    346    
     346
    347347    $thread_ids = $_POST['thread_ids'];
    348348
    349349    if ( !$thread_ids || !messages_check_thread_access($thread_ids) ) {
    350         bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/' . $bp->current_action );           
     350        bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/' . $bp->current_action );
    351351    } else {
    352352        if ( !check_admin_referer( 'messages_delete_thread' ) )
    353353            return false;
    354            
     354
    355355        if ( !messages_delete_thread( $thread_ids ) ) {
    356356            bp_core_add_message( __('There was an error deleting messages.', 'buddypress'), 'error' );
     
    373373function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
    374374    global $bp;
    375    
     375
    376376    if ( 'new_message' == $action ) {
    377377        if ( (int)$total_items > 1 )
    378             return apply_filters( 'bp_messages_multiple_new_message_notification', '<a href="' . $bp->loggedin_user->domain . $bp->messages->slug . '/inbox" title="Inbox">' . sprintf( __('You have %d new messages', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );       
     378            return apply_filters( 'bp_messages_multiple_new_message_notification', '<a href="' . $bp->loggedin_user->domain . $bp->messages->slug . '/inbox" title="Inbox">' . sprintf( __('You have %d new messages', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );
    379379        else
    380380            return apply_filters( 'bp_messages_single_new_message_notification', '<a href="' . $bp->loggedin_user->domain . $bp->messages->slug . '/inbox" title="Inbox">' . sprintf( __('You have %d new message', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );
    381381    }
    382    
     382
    383383    do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
    384    
     384
    385385    return false;
    386386}
     
    398398function messages_new_message( $args = '' ) {
    399399    global $bp;
    400    
     400
    401401    $defaults = array(
    402402        'thread_id' => false, // false for a new message, thread id for a reply to a thread.
     
    409409
    410410    $r = wp_parse_args( $args, $defaults );
    411     extract( $r, EXTR_SKIP );   
    412    
     411    extract( $r, EXTR_SKIP );
     412
    413413    if ( !$sender_id || !$subject || !$content )
    414414        return false;
    415        
    416     /* Create a new message object */   
     415
     416    /* Create a new message object */
    417417    $message = new BP_Messages_Message;
    418418    $message->thread_id = $thread_id;
     
    425425    if ( $thread_id ) {
    426426        $thread = new BP_Messages_Thread($thread_id);
    427         $message->recipients = $thread->get_recipients();       
     427        $message->recipients = $thread->get_recipients();
    428428    } else {
    429429        if ( empty( $recipients ) )
    430430            return false;
    431        
     431
    432432        /* Loop the recipients and convert all usernames to user_ids where needed */
    433433        foreach( (array) $recipients as $recipient ) {
    434434            if ( is_numeric( trim( $recipient ) ) )
    435435                $recipient_ids[] = (int)trim( $recipient );
    436            
     436
    437437            if ( $recipient_id = bp_core_get_userid( trim( $recipient ) ) )
    438438                $recipient_ids[] = (int)$recipient_id;
     
    445445        /* Remove duplicates */
    446446        $recipient_ids = array_unique( (array)$recipient_ids );
    447        
     447
    448448        if ( empty( $recipient_ids ) )
    449449            return false;
    450        
    451         $message->recipients = $recipient_ids; 
    452     }
    453    
     450
     451        $message->recipients = $recipient_ids;
     452    }
     453
    454454    if ( $message->send() ) {
    455455        require_once( BP_PLUGIN_DIR . '/bp-messages/bp-messages-notifications.php' );
     
    457457        // Send screen notifications to the recipients
    458458        foreach ( (array)$message->recipients as $recipient ) {
    459             bp_core_add_notification( $message->id, $recipient, 'messages', 'new_message' );   
    460         }
    461        
     459            bp_core_add_notification( $message->id, $recipient, 'messages', 'new_message' );
     460        }
     461
    462462        // Send email notifications to the recipients
    463463        messages_notification_new_message( array( 'item_id' => $message->id, 'recipient_ids' => $message->recipients, 'thread_id' => $message->thread_id, 'component_name' => $bp->messages->slug, 'component_action' => 'message_sent', 'is_private' => 1 ) );
    464    
     464
    465465        do_action( 'messages_message_sent', &$message );
    466        
     466
    467467        return $message->thread_id;
    468468    }
    469    
     469
    470470    return false;
    471471}
     
    483483        $notice->is_active = 1;
    484484        $notice->save(); // send it.
    485        
     485
    486486        do_action( 'messages_send_notice', $subject, $message );
    487        
     487
    488488        return true;
    489489    }
     
    497497                $error = 1;
    498498        }
    499        
     499
    500500        if ( $error )
    501501            return false;
    502        
     502
    503503        do_action( 'messages_delete_thread', $thread_ids );
    504        
     504
    505505        return true;
    506506    } else {
    507507        if ( !BP_Messages_Thread::delete($thread_ids) )
    508508            return false;
    509        
     509
    510510        do_action( 'messages_delete_thread', $thread_ids );
    511        
     511
    512512        return true;
    513513    }
     
    516516function messages_check_thread_access( $thread_id, $user_id = false ) {
    517517    global $bp;
    518    
     518
    519519    if ( !$user_id )
    520520        $user_id = $bp->loggedin_user->id;
    521    
     521
    522522    return BP_Messages_Thread::check_access( $thread_id, $user_id );
    523523}
     
    528528
    529529function messages_mark_thread_unread( $thread_id ) {
    530     return BP_Messages_Thread::mark_as_unread( $thread_id );   
     530    return BP_Messages_Thread::mark_as_unread( $thread_id );
    531531}
    532532
     
    545545function messages_get_unread_count( $user_id = false ) {
    546546    global $bp;
    547    
    548     if ( !$user_id ) 
     547
     548    if ( !$user_id )
    549549        $user_id = $bp->loggedin_user->id;
    550        
     550
    551551    return BP_Messages_Thread::get_inbox_count( $user_id );
    552552}
Note: See TracChangeset for help on using the changeset viewer.