Advertisement
Guest User

Untitled

a guest
Jan 6th, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.24 KB | None | 0 0
  1. <?php
  2.  
  3. define ( 'BP_MESSAGES_DB_VERSION', '2000' );
  4.  
  5. /* Define the slug for the component */
  6. if ( !defined( 'BP_MESSAGES_SLUG' ) )
  7.     define ( 'BP_MESSAGES_SLUG', 'messages' );
  8.  
  9. require ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-classes.php' );
  10. require ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-cssjs.php' );
  11. require ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-templatetags.php' );
  12. require ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-filters.php' );
  13.  
  14. function messages_install() {
  15.     global $wpdb, $bp;
  16.  
  17.     if ( !empty($wpdb->charset) )
  18.         $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  19.  
  20.     $sql[] = "CREATE TABLE {$bp->messages->table_name_recipients} (
  21.                 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  22.                 user_id bigint(20) NOT NULL,
  23.                 thread_id bigint(20) NOT NULL,
  24.                 unread_count int(10) NOT NULL DEFAULT '0',
  25.                 sender_only tinyint(1) NOT NULL DEFAULT '0',
  26.                 is_deleted tinyint(1) NOT NULL DEFAULT '0',
  27.                 KEY user_id (user_id),
  28.                 KEY thread_id (thread_id),
  29.                 KEY is_deleted (is_deleted),
  30.                 KEY sender_only (sender_only),
  31.                 KEY unread_count (unread_count)
  32.                ) {$charset_collate};";
  33.  
  34.     $sql[] = "CREATE TABLE {$bp->messages->table_name_messages} (
  35.                 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  36.                 thread_id bigint(20) NOT NULL,
  37.                 sender_id bigint(20) NOT NULL,
  38.                 subject varchar(200) NOT NULL,
  39.                 message longtext NOT NULL,
  40.                 date_sent datetime NOT NULL,
  41.                 KEY sender_id (sender_id),
  42.                 KEY thread_id (thread_id)
  43.                ) {$charset_collate};";
  44.  
  45.     $sql[] = "CREATE TABLE {$bp->messages->table_name_notices} (
  46.                 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  47.                 subject varchar(200) NOT NULL,
  48.                 message longtext NOT NULL,
  49.                 date_sent datetime NOT NULL,
  50.                 is_active tinyint(1) NOT NULL DEFAULT '0',
  51.                 KEY is_active (is_active)
  52.                ) {$charset_collate};";
  53.  
  54.     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  55.     dbDelta($sql);
  56.  
  57.     /* Upgrade and remove the message threads table if it exists */
  58.     if ( $wpdb->get_var( "SHOW TABLES LIKE '%{$wpdb->base_prefix}bp_messages_threads%'" ) ) {
  59.         $upgrade = BP_Messages_Thread::upgrade_tables();
  60.  
  61.         if ( $upgrade )
  62.             $wpdb->query( "DROP TABLE {$wpdb->base_prefix}bp_messages_threads" );
  63.     }
  64.  
  65.     add_site_option( 'bp-messages-db-version', BP_MESSAGES_DB_VERSION );
  66. }
  67.  
  68. function messages_setup_globals() {
  69.     global $bp;
  70.  
  71.     /* For internal identification */
  72.     $bp->messages->id = 'messages';
  73.  
  74.     $bp->messages->slug = BP_MESSAGES_SLUG;
  75.  
  76.     $bp->messages->table_name_notices    = $bp->table_prefix . 'bp_messages_notices';
  77.     $bp->messages->table_name_messages   = $bp->table_prefix . 'bp_messages_messages';
  78.     $bp->messages->table_name_recipients = $bp->table_prefix . 'bp_messages_recipients';
  79.  
  80.     $bp->messages->format_notification_function = 'messages_format_notifications';
  81.  
  82.     /* Register this in the active components array */
  83.     $bp->active_components[$bp->messages->slug] = $bp->messages->id;
  84.  
  85.     do_action( 'messages_setup_globals' );
  86. }
  87. add_action( 'bp_setup_globals', 'messages_setup_globals' );
  88.  
  89. function messages_check_installed() {
  90.     global $bp;
  91.  
  92.     if ( !is_super_admin() )
  93.         return false;
  94.  
  95.     /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
  96.     if ( get_site_option( 'bp-messages-db-version' ) < BP_MESSAGES_DB_VERSION )
  97.         messages_install();
  98. }
  99. add_action( 'admin_menu', 'messages_check_installed' );
  100.  
  101. function messages_setup_nav() {
  102.     global $bp;
  103.  
  104.     if ( $count = messages_get_unread_count() )
  105.         $name = sprintf( __('Messages <strong>(%s)</strong>', 'buddypress'), $count );
  106.     else
  107.         $name = __('Messages <strong></strong>', 'buddypress');
  108.  
  109.     /* Add 'Messages' to the main navigation */
  110.     bp_core_new_nav_item( array( 'name' => $name, '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 ) );
  111.  
  112.     $messages_link = $bp->loggedin_user->domain . $bp->messages->slug . '/';
  113.  
  114.     /* Add the subnav items to the profile */
  115.     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_my_profile() ) );
  116.     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_my_profile() ) );
  117.     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_my_profile() ) );
  118.  
  119.     if ( is_super_admin() )
  120.         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_super_admin() ) );
  121.  
  122.     if ( $bp->current_component == $bp->messages->slug ) {
  123.         if ( bp_is_my_profile() ) {
  124.             $bp->bp_options_title = __( 'My Messages', 'buddypress' );
  125.         } else {
  126.             $bp_options_avatar =  bp_core_fetch_avatar( array( 'item_id' => $bp->displayed_user->id, 'type' => 'thumb' ) );
  127.             $bp->bp_options_title = $bp->displayed_user->fullname;
  128.         }
  129.     }
  130.  
  131.     do_action( 'messages_setup_nav' );
  132. }
  133. add_action( 'bp_setup_nav', 'messages_setup_nav' );
  134.  
  135. /********************************************************************************
  136.  * Screen Functions
  137.  *
  138.  * Screen functions are the controllers of BuddyPress. They will execute when their
  139.  * specific URL is caught. They will first save or manipulate data using business
  140.  * functions, then pass on the user to a template file.
  141.  */
  142.  
  143. function messages_screen_inbox() {
  144.     do_action( 'messages_screen_inbox' );
  145.     bp_core_load_template( apply_filters( 'messages_template_inbox', 'members/single/home' ) );
  146. }
  147.  
  148. function messages_screen_sentbox() {
  149.     do_action( 'messages_screen_sentbox' );
  150.     bp_core_load_template( apply_filters( 'messages_template_sentbox', 'members/single/home' ) );
  151. }
  152.  
  153. function messages_screen_compose() {
  154.     global $bp;
  155.  
  156.     // Remove any saved message data from a previous session.
  157.     messages_remove_callback_values();
  158.  
  159.     // Check if the message form has been submitted
  160.     if ( isset( $_POST['send'] ) ) {
  161.  
  162.         // Check the nonce
  163.         check_admin_referer( 'messages_send_message' );
  164.  
  165.         // Check we have what we need
  166.         if ( empty( $_POST['subject'] ) || empty( $_POST['content'] ) ) {
  167.             bp_core_add_message( __( 'There was an error sending that message, please try again', 'buddypress' ), 'error' );
  168.         } else {
  169.             // If this is a notice, send it
  170.             if ( isset( $_POST['send-notice'] ) ) {
  171.                 if ( messages_send_notice( $_POST['subject'], $_POST['content'] ) ) {
  172.                     bp_core_add_message( __( 'Notice sent successfully!', 'buddypress' ) );
  173.                     bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/inbox/' );
  174.                 } else {
  175.                     bp_core_add_message( __( 'There was an error sending that notice, please try again', 'buddypress' ), 'error' );
  176.                 }
  177.             } else {
  178.                 // Filter recipients into the format we need - array( 'username/userid', 'username/userid' )
  179.                 $autocomplete_recipients = explode( ',', $_POST['send-to-input'] );
  180.                 $typed_recipients        = explode( ' ', $_POST['send_to_usernames'] );
  181.                 $recipients              = array_merge( (array) $autocomplete_recipients, (array) $typed_recipients );
  182.                 $recipients              = apply_filters( 'bp_messages_recipients', $recipients );
  183.  
  184.                 // Send the message
  185.                 if ( $thread_id = messages_new_message( array( 'recipients' => $recipients, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) ) {
  186.                     bp_core_add_message( __( 'Message sent successfully!', 'buddypress' ) );
  187.                     bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/view/' . $thread_id . '/' );
  188.                 } else {
  189.                     bp_core_add_message( __( 'There was an error sending that message, please try again', 'buddypress' ), 'error' );
  190.                 }
  191.             }
  192.         }
  193.     }
  194.  
  195.     do_action( 'messages_screen_compose' );
  196.  
  197.     bp_core_load_template( apply_filters( 'messages_template_compose', 'members/single/home' ) );
  198. }
  199.  
  200. function messages_screen_notices() {
  201.     global $bp, $notice_id;
  202.  
  203.     if ( !is_super_admin() )
  204.         return false;
  205.  
  206.     $notice_id = $bp->action_variables[1];
  207.  
  208.     if ( $notice_id && is_numeric($notice_id) ) {
  209.         $notice = new BP_Messages_Notice($notice_id);
  210.  
  211.         if ( 'deactivate' == $bp->action_variables[0] ) {
  212.             if ( !$notice->deactivate() ) {
  213.                 bp_core_add_message( __('There was a problem deactivating that notice.', 'buddypress'), 'error' );
  214.             } else {
  215.                 bp_core_add_message( __('Notice deactivated.', 'buddypress') );
  216.             }
  217.         } else if ( 'activate' == $bp->action_variables[0] ) {
  218.             if ( !$notice->activate() ) {
  219.                 bp_core_add_message( __('There was a problem activating that notice.', 'buddypress'), 'error' );
  220.             } else {
  221.                 bp_core_add_message( __('Notice activated.', 'buddypress') );
  222.             }
  223.         } else if ( 'delete' == $bp->action_variables[0] ) {
  224.             if ( !$notice->delete() ) {
  225.                 bp_core_add_message( __('There was a problem deleting that notice.', 'buddypress'), 'buddypress' );
  226.             } else {
  227.                 bp_core_add_message( __('Notice deleted.', 'buddypress') );
  228.             }
  229.         }
  230.         bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/notices' );
  231.     }
  232.  
  233.     do_action( 'messages_screen_notices' );
  234.  
  235.     bp_core_load_template( apply_filters( 'messages_template_notices', 'members/single/home' ) );
  236. }
  237.  
  238. function messages_screen_notification_settings() {
  239.     global $current_user; ?>
  240.     <table class="notification-settings zebra" id="messages-notification-settings">
  241.         <thead>
  242.             <tr>
  243.                 <th class="icon"></th>
  244.                 <th class="title"><?php _e( 'Messages', 'buddypress' ) ?></th>
  245.                 <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
  246.                 <th class="no"><?php _e( 'No', 'buddypress' )?></th>
  247.             </tr>
  248.         </thead>
  249.  
  250.         <tbody>
  251.             <tr>
  252.                 <td></td>
  253.                 <td><?php _e( 'A member sends you a new message', 'buddypress' ) ?></td>
  254.                 <td class="yes"><input type="radio" name="notifications[notification_messages_new_message]" value="yes" <?php if ( !get_user_meta( $current_user->id, 'notification_messages_new_message', true ) || 'yes' == get_user_meta( $current_user->id, 'notification_messages_new_message', true ) ) { ?>checked="checked" <?php } ?>/></td>
  255.                 <td class="no"><input type="radio" name="notifications[notification_messages_new_message]" value="no" <?php if ( 'no' == get_user_meta( $current_user->id, 'notification_messages_new_message', true ) ) { ?>checked="checked" <?php } ?>/></td>
  256.             </tr>
  257.             <tr>
  258.                 <td></td>
  259.                 <td><?php _e( 'A new site notice is posted', 'buddypress' ) ?></td>
  260.                 <td class="yes"><input type="radio" name="notifications[notification_messages_new_notice]" value="yes" <?php if ( !get_user_meta( $current_user->id, 'notification_messages_new_notice', true ) || 'yes' == get_user_meta( $current_user->id, 'notification_messages_new_notice', true ) ) { ?>checked="checked" <?php } ?>/></td>
  261.                 <td class="no"><input type="radio" name="notifications[notification_messages_new_notice]" value="no" <?php if ( 'no' == get_user_meta( $current_user->id, 'notification_messages_new_notice', true ) ) { ?>checked="checked" <?php } ?>/></td>
  262.             </tr>
  263.  
  264.             <?php do_action( 'messages_screen_notification_settings' ) ?>
  265.         </tbody>
  266.     </table>
  267. <?php
  268. }
  269. add_action( 'bp_notification_settings', 'messages_screen_notification_settings', 2 );
  270.  
  271.  
  272. /********************************************************************************
  273.  * Action Functions
  274.  *
  275.  * Action functions are exactly the same as screen functions, however they do not
  276.  * have a template screen associated with them. Usually they will send the user
  277.  * back to the default screen after execution.
  278.  */
  279.  
  280. function messages_action_view_message() {
  281.     global $bp, $thread_id;
  282.  
  283.     if ( $bp->current_component != $bp->messages->slug || $bp->current_action != 'view' )
  284.         return false;
  285.  
  286.     $thread_id = $bp->action_variables[0];
  287.  
  288.     if ( !$thread_id || !messages_is_valid_thread( $thread_id ) || ( !messages_check_thread_access($thread_id) && !is_super_admin() ) )
  289.         bp_core_redirect( $bp->displayed_user->domain . $bp->current_component );
  290.  
  291.     /* Check if a new reply has been submitted */
  292.     if ( isset( $_POST['send'] ) ) {
  293.  
  294.         /* Check the nonce */
  295.         check_admin_referer( 'messages_send_message', 'send_message_nonce' );
  296.  
  297.         /* Send the reply */
  298.         if ( messages_new_message( array( 'thread_id' => $thread_id, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) )
  299.             bp_core_add_message( __( 'Your reply was sent successfully', 'buddypress' ) );
  300.         else
  301.             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.  
  306.     /* Mark message read */
  307.     messages_mark_thread_read( $thread_id );
  308.  
  309.     do_action( 'messages_action_view_message' );
  310.  
  311.     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_my_profile() ) );
  312.     bp_core_load_template( apply_filters( 'messages_template_view_message', 'members/single/home' ) );
  313. }
  314. add_action( 'wp', 'messages_action_view_message', 3 );
  315.  
  316. function messages_action_delete_message() {
  317.     global $bp, $thread_id;
  318.  
  319.     if ( $bp->current_component != $bp->messages->slug || 'notices' == $bp->current_action || $bp->action_variables[0] != 'delete' )
  320.         return false;
  321.  
  322.     $thread_id = $bp->action_variables[1];
  323.  
  324.     if ( !$thread_id || !is_numeric($thread_id) || !messages_check_thread_access($thread_id) ) {
  325.         bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/' . $bp->current_action );
  326.     } else {
  327.         if ( !check_admin_referer( 'messages_delete_thread' ) )
  328.             return false;
  329.  
  330.         // delete message
  331.         if ( !messages_delete_thread($thread_id) ) {
  332.             bp_core_add_message( __('There was an error deleting that message.', 'buddypress'), 'error' );
  333.         } else {
  334.             bp_core_add_message( __('Message deleted.', 'buddypress') );
  335.         }
  336.         bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action );
  337.     }
  338. }
  339. add_action( 'wp', 'messages_action_delete_message', 3 );
  340.  
  341. function messages_action_bulk_delete() {
  342.     global $bp, $thread_ids;
  343.  
  344.     if ( $bp->current_component != $bp->messages->slug || $bp->action_variables[0] != 'bulk-delete' )
  345.         return false;
  346.  
  347.     $thread_ids = $_POST['thread_ids'];
  348.  
  349.     if ( !$thread_ids || !messages_check_thread_access($thread_ids) ) {
  350.         bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/' . $bp->current_action );
  351.     } else {
  352.         if ( !check_admin_referer( 'messages_delete_thread' ) )
  353.             return false;
  354.  
  355.         if ( !messages_delete_thread( $thread_ids ) ) {
  356.             bp_core_add_message( __('There was an error deleting messages.', 'buddypress'), 'error' );
  357.         } else {
  358.             bp_core_add_message( __('Messages deleted.', 'buddypress') );
  359.         }
  360.         bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action );
  361.     }
  362. }
  363. add_action( 'wp', 'messages_action_bulk_delete', 3 );
  364.  
  365.  
  366. /********************************************************************************
  367.  * Activity & Notification Functions
  368.  *
  369.  * These functions handle the recording, deleting and formatting of activity and
  370.  * notifications for the user and for this specific component.
  371.  */
  372.  
  373. function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
  374.     global $bp;
  375.  
  376.     if ( 'new_message' == $action ) {
  377.         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 );
  379.         else
  380.             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 );
  381.     }
  382.  
  383.     do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
  384.  
  385.     return false;
  386. }
  387.  
  388.  
  389. /********************************************************************************
  390.  * Business Functions
  391.  *
  392.  * Business functions are where all the magic happens in BuddyPress. They will
  393.  * handle the actual saving or manipulation of information. Usually they will
  394.  * hand off to a database class for data access, then return
  395.  * true or false on success or failure.
  396.  */
  397.  
  398. function messages_new_message( $args = '' ) {
  399.     global $bp;
  400.  
  401.     $defaults = array(
  402.         'thread_id' => false, // false for a new message, thread id for a reply to a thread.
  403.         'sender_id' => $bp->loggedin_user->id,
  404.         'recipients' => false, // Can be an array of usernames, user_ids or mixed.
  405.         'subject' => false,
  406.         'content' => false,
  407.         'date_sent' => bp_core_current_time()
  408.     );
  409.  
  410.     $r = wp_parse_args( $args, $defaults );
  411.     extract( $r, EXTR_SKIP );
  412.  
  413.     if ( !$sender_id || !$content )
  414.         return false;
  415.  
  416.     /* Create a new message object */
  417.     $message = new BP_Messages_Message;
  418.     $message->thread_id = $thread_id;
  419.     $message->sender_id = $sender_id;
  420.     $message->subject = $subject;
  421.     $message->message = $content;
  422.     $message->date_sent = $date_sent;
  423.  
  424.     // If we have a thread ID, use the existing recipients, otherwise use the recipients passed
  425.     if ( $thread_id ) {
  426.         $thread = new BP_Messages_Thread( $thread_id );
  427.         $message->recipients = $thread->get_recipients();
  428.  
  429.         // Strip the sender from the recipient list if they exist
  430.         if ( isset( $message->recipients[$sender_id] ) )
  431.             unset( $message->recipients[$sender_id] );
  432.  
  433.         if ( empty( $message->subject ) )
  434.             $message->subject = sprintf( __( 'Re: %s', 'buddypress' ), $thread->messages[0]->subject );
  435.  
  436.     // No thread ID, so make some adjustments
  437.     } else {
  438.         if ( empty( $recipients ) )
  439.             return false;
  440.  
  441.         if ( empty( $message->subject ) )
  442.             $message->subject = __( 'No Subject', 'buddypress' );
  443.  
  444.         /* Loop the recipients and convert all usernames to user_ids where needed */
  445.         foreach( (array) $recipients as $recipient ) {
  446.             if ( is_numeric( trim( $recipient ) ) )
  447.                 $recipient_ids[] = (int)trim( $recipient );
  448.  
  449.             if ( $recipient_id = bp_core_get_userid( trim( $recipient ) ) )
  450.                 $recipient_ids[] = (int)$recipient_id;
  451.         }
  452.  
  453.         /* Strip the sender from the recipient list if they exist */
  454.         if ( $key = array_search( $sender_id, (array)$recipient_ids ) )
  455.             unset( $recipient_ids[$key] );
  456.  
  457.         /* Remove duplicates */
  458.         $recipient_ids = array_unique( (array)$recipient_ids );
  459.  
  460.         if ( empty( $recipient_ids ) )
  461.             return false;
  462.  
  463.         /* Format this to match existing recipients */
  464.         foreach( (array)$recipient_ids as $i => $recipient_id ) {
  465.             $message->recipients[$i] = new stdClass;
  466.             $message->recipients[$i]->user_id = $recipient_id;
  467.         }
  468.     }
  469.  
  470.     if ( $message->send() ) {
  471.         require_once( BP_PLUGIN_DIR . '/bp-messages/bp-messages-notifications.php' );
  472.  
  473.         // Send screen notifications to the recipients
  474.         foreach ( (array)$message->recipients as $recipient )
  475.             bp_core_add_notification( $message->id, $recipient->user_id, 'messages', 'new_message' );
  476.  
  477.         // Send email notifications to the recipients
  478.         messages_notification_new_message( array( 'message_id' => $message->id, 'sender_id' => $message->sender_id, 'subject' => $message->subject, 'content' => $message->message, 'recipients' => $message->recipients, 'thread_id' => $message->thread_id) );
  479.  
  480.         do_action( 'messages_message_sent', &$message );
  481.  
  482.         return $message->thread_id;
  483.     }
  484.  
  485.     return false;
  486. }
  487.  
  488.  
  489. function messages_send_notice( $subject, $message ) {
  490.     if ( !is_super_admin() || empty( $subject ) || empty( $message ) ) {
  491.         return false;
  492.     } else {
  493.         // Has access to send notices, lets do it.
  494.         $notice = new BP_Messages_Notice;
  495.         $notice->subject = $subject;
  496.         $notice->message = $message;
  497.         $notice->date_sent = bp_core_current_time();
  498.         $notice->is_active = 1;
  499.         $notice->save(); // send it.
  500.  
  501.         do_action( 'messages_send_notice', $subject, $message );
  502.  
  503.         return true;
  504.     }
  505. }
  506.  
  507. function messages_delete_thread( $thread_ids ) {
  508.  
  509.     if ( is_array($thread_ids) ) {
  510.         $error = 0;
  511.         for ( $i = 0; $i < count($thread_ids); $i++ ) {
  512.             if ( !$status = BP_Messages_Thread::delete($thread_ids[$i]) )
  513.                 $error = 1;
  514.         }
  515.  
  516.         if ( $error )
  517.             return false;
  518.  
  519.         do_action( 'messages_delete_thread', $thread_ids );
  520.  
  521.         return true;
  522.     } else {
  523.         if ( !BP_Messages_Thread::delete($thread_ids) )
  524.             return false;
  525.  
  526.         do_action( 'messages_delete_thread', $thread_ids );
  527.  
  528.         return true;
  529.     }
  530. }
  531.  
  532. function messages_check_thread_access( $thread_id, $user_id = false ) {
  533.     global $bp;
  534.  
  535.     if ( !$user_id )
  536.         $user_id = $bp->loggedin_user->id;
  537.  
  538.     return BP_Messages_Thread::check_access( $thread_id, $user_id );
  539. }
  540.  
  541. function messages_mark_thread_read( $thread_id ) {
  542.     return BP_Messages_Thread::mark_as_read( $thread_id );
  543. }
  544.  
  545. function messages_mark_thread_unread( $thread_id ) {
  546.     return BP_Messages_Thread::mark_as_unread( $thread_id );
  547. }
  548.  
  549. function messages_add_callback_values( $recipients, $subject, $content ) {
  550.     setcookie( 'bp_messages_send_to', $recipients, time()+60*60*24, COOKIEPATH );
  551.     setcookie( 'bp_messages_subject', $subject, time()+60*60*24, COOKIEPATH );
  552.     setcookie( 'bp_messages_content', $content, time()+60*60*24, COOKIEPATH );
  553. }
  554.  
  555. function messages_remove_callback_values() {
  556.     setcookie( 'bp_messages_send_to', false, time()-1000, COOKIEPATH );
  557.     setcookie( 'bp_messages_subject', false, time()-1000, COOKIEPATH );
  558.     setcookie( 'bp_messages_content', false, time()-1000, COOKIEPATH );
  559. }
  560.  
  561. function messages_get_unread_count( $user_id = false ) {
  562.     global $bp;
  563.  
  564.     if ( !$user_id )
  565.         $user_id = $bp->loggedin_user->id;
  566.  
  567.     return BP_Messages_Thread::get_inbox_count( $user_id );
  568. }
  569.  
  570. function messages_is_user_sender( $user_id, $message_id ) {
  571.     return BP_Messages_Message::is_user_sender( $user_id, $message_id );
  572. }
  573.  
  574. function messages_get_message_sender( $message_id ) {
  575.     return BP_Messages_Message::get_message_sender( $message_id );
  576. }
  577.  
  578. function messages_is_valid_thread( $thread_id ) {
  579.     return BP_Messages_Thread::is_valid( $thread_id );
  580. }
  581.  
  582.  
  583. /********************************************************************************
  584.  * Caching
  585.  *
  586.  * Caching functions handle the clearing of cached objects and pages on specific
  587.  * actions throughout BuddyPress.
  588.  */
  589.  
  590. // List actions to clear super cached pages on, if super cache is installed
  591. add_action( 'messages_delete_thread', 'bp_core_clear_cache' );
  592. add_action( 'messages_send_notice', 'bp_core_clear_cache' );
  593. add_action( 'messages_message_sent', 'bp_core_clear_cache' );
  594.  
  595. // Don't cache message inbox/sentbox/compose as it's too problematic
  596. add_action( 'messages_screen_compose', 'bp_core_clear_cache' );
  597. add_action( 'messages_screen_sentbox', 'bp_core_clear_cache' );
  598. add_action( 'messages_screen_inbox', 'bp_core_clear_cache' );
  599.  
  600. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement