| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | Class BP_Messages_Thread { |
|---|
| 4 | var $thread_id; |
|---|
| 5 | var $messages; |
|---|
| 6 | var $recipients; |
|---|
| 7 | var $sender_ids; |
|---|
| 8 | |
|---|
| 9 | var $unread_count; |
|---|
| 10 | |
|---|
| 11 | function bp_messages_thread ( $thread_id = false ) { |
|---|
| 12 | if ( $thread_id ) |
|---|
| 13 | $this->populate( $thread_id ); |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | function populate( $thread_id ) { |
|---|
| 17 | global $wpdb, $bp; |
|---|
| 18 | |
|---|
| 19 | $this->thread_id = $thread_id; |
|---|
| 20 | |
|---|
| 21 | if ( !$this->messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC", $this->thread_id ) ) ) |
|---|
| 22 | return false; |
|---|
| 23 | |
|---|
| 24 | foreach ( (array)$this->messages as $key => $message ) |
|---|
| 25 | $this->sender_ids[$message->sender_id] = $message->sender_id; |
|---|
| 26 | |
|---|
| 27 | /* Fetch the recipients */ |
|---|
| 28 | $this->recipients = $this->get_recipients(); |
|---|
| 29 | |
|---|
| 30 | /* Get the unread count for the logged in user */ |
|---|
| 31 | if ( isset( $this->recipients[$bp->loggedin_user->id] ) ) |
|---|
| 32 | $this->unread_count = $this->recipients[$bp->loggedin_user->id]->unread_count; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | function mark_read() { |
|---|
| 36 | BP_Messages_Thread::mark_as_read( $this->thread_id ); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | function mark_unread() { |
|---|
| 40 | BP_Messages_Thread::mark_as_unread( $this->thread_id ); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | function get_recipients() { |
|---|
| 44 | global $wpdb, $bp; |
|---|
| 45 | |
|---|
| 46 | $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) ); |
|---|
| 47 | |
|---|
| 48 | foreach ( (array)$results as $recipient ) |
|---|
| 49 | $recipients[$recipient->user_id] = $recipient; |
|---|
| 50 | |
|---|
| 51 | return $recipients; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** Static Functions **/ |
|---|
| 55 | |
|---|
| 56 | function delete( $thread_id ) { |
|---|
| 57 | global $wpdb, $bp; |
|---|
| 58 | |
|---|
| 59 | $delete_for_user = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET is_deleted = 1 WHERE thread_id = %d AND user_id = %d", $thread_id, $bp->loggedin_user->id ) ); |
|---|
| 60 | |
|---|
| 61 | // Check to see if any more recipients remain for this message |
|---|
| 62 | // if not, then delete the message from the database. |
|---|
| 63 | $recipients = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND is_deleted = 0", $thread_id ) ); |
|---|
| 64 | |
|---|
| 65 | if ( empty( $recipients ) ) { |
|---|
| 66 | /* Delete all the messages */ |
|---|
| 67 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) ); |
|---|
| 68 | |
|---|
| 69 | /* Delete all the recipients */ |
|---|
| 70 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ) ); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | return true; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | function get_current_threads_for_user( $user_id, $box = 'inbox', $type = 'all', $limit = null, $page = null ) { |
|---|
| 77 | global $wpdb, $bp; |
|---|
| 78 | |
|---|
| 79 | if ( $limit && $page ) |
|---|
| 80 | $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); |
|---|
| 81 | |
|---|
| 82 | if ( $type == 'unread' ) |
|---|
| 83 | $type_sql = $wpdb->prepare( " AND r.unread_count != 0 " ); |
|---|
| 84 | else if ( $type == 'read' ) |
|---|
| 85 | $type_sql = $wpdb->prepare( " AND r.unread_count = 0 " ); |
|---|
| 86 | |
|---|
| 87 | if ( 'sentbox' == $box ) { |
|---|
| 88 | $thread_ids = $wpdb->get_results( $wpdb->prepare( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND m.sender_id = %d AND r.is_deleted = 0 GROUP BY m.thread_id ORDER BY m.date_sent DESC {$pag_sql}", $bp->loggedin_user->id ) ); |
|---|
| 89 | $total_threads = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT m.thread_id ) FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND m.sender_id = %d AND r.is_deleted = 0 ", $bp->loggedin_user->id ) ); |
|---|
| 90 | } else { |
|---|
| 91 | $thread_ids = $wpdb->get_results( $wpdb->prepare( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND r.user_id = %d AND r.sender_only = 0 {$type_sql} GROUP BY m.thread_id ORDER BY m.date_sent DESC {$pag_sql}", $bp->loggedin_user->id ) ); |
|---|
| 92 | $total_threads = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT m.thread_id ) FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND r.user_id = %d AND r.sender_only = 0 {$type_sql}", $bp->loggedin_user->id ) ); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | if ( empty( $thread_ids ) ) |
|---|
| 96 | return false; |
|---|
| 97 | |
|---|
| 98 | /* Sort threads by date_sent */ |
|---|
| 99 | foreach( (array)$thread_ids as $thread ) { |
|---|
| 100 | $sorted_threads[$thread->thread_id] = strtotime($thread->date_sent); |
|---|
| 101 | } |
|---|
| 102 | arsort($sorted_threads); |
|---|
| 103 | |
|---|
| 104 | $threads = false; |
|---|
| 105 | foreach ( (array)$sorted_threads as $thread_id => $date_sent ) |
|---|
| 106 | $threads[] = new BP_Messages_Thread( $thread_id ); |
|---|
| 107 | |
|---|
| 108 | return array( 'threads' => &$threads, 'total' => (int)$total_threads ); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | function mark_as_read( $thread_id ) { |
|---|
| 112 | global $wpdb, $bp; |
|---|
| 113 | |
|---|
| 114 | $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", $bp->loggedin_user->id, $thread_id ); |
|---|
| 115 | $wpdb->query($sql); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | function mark_as_unread( $thread_id ) { |
|---|
| 119 | global $wpdb, $bp; |
|---|
| 120 | |
|---|
| 121 | $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", $bp->loggedin_user->id, $thread_id ); |
|---|
| 122 | $wpdb->query($sql); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | function get_total_threads_for_user( $user_id, $box = 'inbox', $type = 'all' ) { |
|---|
| 126 | global $wpdb, $bp; |
|---|
| 127 | |
|---|
| 128 | $exclude_sender = ''; |
|---|
| 129 | if ( $box != 'sentbox' ) |
|---|
| 130 | $exclude_sender = ' AND sender_only != 1'; |
|---|
| 131 | |
|---|
| 132 | if ( $type == 'unread' ) |
|---|
| 133 | $type_sql = $wpdb->prepare( " AND unread_count != 0 " ); |
|---|
| 134 | else if ( $type == 'read' ) |
|---|
| 135 | $type_sql = $wpdb->prepare( " AND unread_count = 0 " ); |
|---|
| 136 | |
|---|
| 137 | return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(thread_id) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0$exclude_sender $type_sql", $user_id ) ); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | function user_is_sender( $thread_id ) { |
|---|
| 141 | global $wpdb, $bp; |
|---|
| 142 | |
|---|
| 143 | $sender_ids = $wpdb->get_col( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) ); |
|---|
| 144 | |
|---|
| 145 | if ( !$sender_ids ) |
|---|
| 146 | return false; |
|---|
| 147 | |
|---|
| 148 | return in_array( $bp->loggedin_user->id, $sender_ids ); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | function get_last_sender( $thread_id ) { |
|---|
| 152 | global $wpdb, $bp; |
|---|
| 153 | |
|---|
| 154 | if ( !$sender_id = $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d GROUP BY sender_id ORDER BY date_sent LIMIT 1", $thread_id ) ) ) |
|---|
| 155 | return false; |
|---|
| 156 | |
|---|
| 157 | return bp_core_get_userlink( $sender_id, true ); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | function get_inbox_count() { |
|---|
| 161 | global $wpdb, $bp; |
|---|
| 162 | |
|---|
| 163 | $sql = $wpdb->prepare( "SELECT unread_count FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0 AND sender_only = 0", $bp->loggedin_user->id ); |
|---|
| 164 | |
|---|
| 165 | if ( !$unread_counts = $wpdb->get_results($sql) ) |
|---|
| 166 | return false; |
|---|
| 167 | |
|---|
| 168 | $count = 0; |
|---|
| 169 | for ( $i = 0; $i < count($unread_counts); $i++ ) { |
|---|
| 170 | $count += $unread_counts[$i]->unread_count; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | return $count; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | function check_access( $thread_id, $user_id = false ) { |
|---|
| 177 | global $wpdb, $bp; |
|---|
| 178 | |
|---|
| 179 | if ( !$user_id ) |
|---|
| 180 | $user_id = $bp->loggedin_user->id; |
|---|
| 181 | |
|---|
| 182 | return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND user_id = %d", $thread_id, $user_id ) ); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | function is_valid( $thread_id ) { |
|---|
| 186 | global $wpdb, $bp; |
|---|
| 187 | |
|---|
| 188 | return $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d LIMIT 1", $thread_id ) ); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | function get_recipient_links($recipients) { |
|---|
| 192 | if ( count($recipients) >= 5 ) |
|---|
| 193 | return count( $recipients ) . __(' Recipients', 'buddypress'); |
|---|
| 194 | |
|---|
| 195 | foreach ( (array)$recipients as $recipient ) { |
|---|
| 196 | $recipient_links[] = bp_core_get_userlink( $recipient->user_id ); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | return implode( ', ', (array) $recipient_links ); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /* Upgrade Functions */ |
|---|
| 203 | |
|---|
| 204 | function upgrade_tables() { |
|---|
| 205 | global $wpdb, $bp; |
|---|
| 206 | |
|---|
| 207 | $errors = false; |
|---|
| 208 | $threads = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}bp_messages_threads" ) ); |
|---|
| 209 | |
|---|
| 210 | /* Nothing to upgrade, just return true to remove the table */ |
|---|
| 211 | if ( empty( $threads ) ) |
|---|
| 212 | return true; |
|---|
| 213 | |
|---|
| 214 | foreach( (array)$threads as $thread ) { |
|---|
| 215 | $message_ids = maybe_unserialize( $thread->message_ids ); |
|---|
| 216 | |
|---|
| 217 | if ( !empty( $message_ids ) ) { |
|---|
| 218 | $message_ids = implode( ',', $message_ids ); |
|---|
| 219 | |
|---|
| 220 | /* Add the thread_id to the messages table */ |
|---|
| 221 | if ( !$wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) ) |
|---|
| 222 | $errors = true; |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | if ( $errors ) |
|---|
| 227 | return false; |
|---|
| 228 | |
|---|
| 229 | return true; |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | Class BP_Messages_Message { |
|---|
| 234 | var $id; |
|---|
| 235 | var $thread_id; |
|---|
| 236 | var $sender_id; |
|---|
| 237 | var $subject; |
|---|
| 238 | var $message; |
|---|
| 239 | var $date_sent; |
|---|
| 240 | |
|---|
| 241 | var $recipients = false; |
|---|
| 242 | |
|---|
| 243 | function bp_messages_message( $id = null ) { |
|---|
| 244 | global $bp; |
|---|
| 245 | |
|---|
| 246 | $this->date_sent = bp_core_current_time(); |
|---|
| 247 | $this->sender_id = $bp->loggedin_user->id; |
|---|
| 248 | |
|---|
| 249 | if ( $id ) { |
|---|
| 250 | $this->populate($id); |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | function populate( $id ) { |
|---|
| 255 | global $wpdb, $bp; |
|---|
| 256 | |
|---|
| 257 | if ( $message = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $id ) ) ) { |
|---|
| 258 | $this->id = $message->id; |
|---|
| 259 | $this->thread_id = $message->thread_id; |
|---|
| 260 | $this->sender_id = $message->sender_id; |
|---|
| 261 | $this->subject = $message->subject; |
|---|
| 262 | $this->message = $message->message; |
|---|
| 263 | $this->date_sent = $message->date_sent; |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | function send() { |
|---|
| 268 | global $wpdb, $bp; |
|---|
| 269 | |
|---|
| 270 | $this->sender_id = apply_filters( 'messages_message_sender_id_before_save', $this->sender_id, $this->id ); |
|---|
| 271 | $this->thread_id = apply_filters( 'messages_message_thread_id_before_save', $this->thread_id, $this->id ); |
|---|
| 272 | $this->subject = apply_filters( 'messages_message_subject_before_save', $this->subject, $this->id ); |
|---|
| 273 | $this->message = apply_filters( 'messages_message_content_before_save', $this->message, $this->id ); |
|---|
| 274 | $this->date_sent = apply_filters( 'messages_message_date_sent_before_save', $this->date_sent, $this->id ); |
|---|
| 275 | |
|---|
| 276 | do_action( 'messages_message_before_save', $this ); |
|---|
| 277 | |
|---|
| 278 | /* Make sure we have at least one recipient before sending. */ |
|---|
| 279 | if ( empty( $this->recipients ) ) |
|---|
| 280 | return false; |
|---|
| 281 | |
|---|
| 282 | $new_thread = false; |
|---|
| 283 | |
|---|
| 284 | /* If we have no thread_id then this is the first message of a new thread. */ |
|---|
| 285 | if ( empty( $this->thread_id ) ) { |
|---|
| 286 | $this->thread_id = (int)$wpdb->get_var( $wpdb->prepare( "SELECT MAX(thread_id) FROM {$bp->messages->table_name_messages}" ) ) + 1; |
|---|
| 287 | $new_thread = true; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | // First insert the message into the messages table |
|---|
| 291 | if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_messages} ( thread_id, sender_id, subject, message, date_sent ) VALUES ( %d, %d, %s, %s, %s )", $this->thread_id, $this->sender_id, $this->subject, $this->message, $this->date_sent ) ) ) |
|---|
| 292 | return false; |
|---|
| 293 | |
|---|
| 294 | $recipient_ids = array(); |
|---|
| 295 | |
|---|
| 296 | if ( $new_thread ) { |
|---|
| 297 | /* Add an recipient entry for all recipients */ |
|---|
| 298 | foreach ( (array)$this->recipients as $recipient ) { |
|---|
| 299 | $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id, unread_count ) VALUES ( %d, %d, 1 )", $recipient->user_id, $this->thread_id ) ); |
|---|
| 300 | |
|---|
| 301 | $recipient_ids[] = $recipient->user_id; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | /* Add a sender recipient entry if the sender is not in the list of recipients */ |
|---|
| 305 | if ( !in_array( $this->sender_id, $recipient_ids ) ) |
|---|
| 306 | $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id, sender_only ) VALUES ( %d, %d, 1 )", $this->sender_id, $this->thread_id ) ); |
|---|
| 307 | } else { |
|---|
| 308 | /* Update the unread count for all recipients */ |
|---|
| 309 | $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = unread_count + 1, sender_only = 0, is_deleted = 0 WHERE thread_id = %d AND user_id != %d", $this->thread_id, $this->sender_id ) ); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | $this->id = $wpdb->insert_id; |
|---|
| 313 | messages_remove_callback_values(); |
|---|
| 314 | |
|---|
| 315 | do_action( 'messages_message_after_save', $this ); |
|---|
| 316 | |
|---|
| 317 | return $this->id; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | function get_recipients() { |
|---|
| 321 | global $bp, $wpdb; |
|---|
| 322 | |
|---|
| 323 | return $wpdb->get_results( $wpdb->prepare( "SELECT user_id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) ); |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | // Static Functions |
|---|
| 327 | |
|---|
| 328 | function get_recipient_ids( $recipient_usernames ) { |
|---|
| 329 | if ( !$recipient_usernames ) |
|---|
| 330 | return false; |
|---|
| 331 | |
|---|
| 332 | if ( is_array( $recipient_usernames ) ) { |
|---|
| 333 | for ( $i = 0; $i < count($recipient_usernames); $i++ ) { |
|---|
| 334 | if ( $rid = bp_core_get_userid( trim($recipient_usernames[$i]) ) ) |
|---|
| 335 | $recipient_ids[] = $rid; |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | return $recipient_ids; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | function get_last_sent_for_user( $thread_id ) { |
|---|
| 343 | global $wpdb, $bp; |
|---|
| 344 | |
|---|
| 345 | return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE sender_id = %d AND thread_id = %d ORDER BY date_sent DESC LIMIT 1", $bp->loggedin_user->id, $thread_id ) ); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | function is_user_sender( $user_id, $message_id ) { |
|---|
| 349 | global $wpdb, $bp; |
|---|
| 350 | return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE sender_id = %d AND id = %d", $user_id, $message_id ) ); |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | function get_message_sender( $message_id ) { |
|---|
| 354 | global $wpdb, $bp; |
|---|
| 355 | return $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_id ) ); |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | Class BP_Messages_Notice { |
|---|
| 360 | var $id = null; |
|---|
| 361 | var $subject; |
|---|
| 362 | var $message; |
|---|
| 363 | var $date_sent; |
|---|
| 364 | var $is_active; |
|---|
| 365 | |
|---|
| 366 | function bp_messages_notice($id = null) { |
|---|
| 367 | if ( $id ) { |
|---|
| 368 | $this->id = $id; |
|---|
| 369 | $this->populate($id); |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | function populate() { |
|---|
| 374 | global $wpdb, $bp; |
|---|
| 375 | |
|---|
| 376 | $notice = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ) ); |
|---|
| 377 | |
|---|
| 378 | if ( $notice ) { |
|---|
| 379 | $this->subject = $notice->subject; |
|---|
| 380 | $this->message = $notice->message; |
|---|
| 381 | $this->date_sent = $notice->date_sent; |
|---|
| 382 | $this->is_active = $notice->is_active; |
|---|
| 383 | } |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | function save() { |
|---|
| 387 | global $wpdb, $bp; |
|---|
| 388 | |
|---|
| 389 | $this->subject = apply_filters( 'messages_notice_subject_before_save', $this->subject, $this->id ); |
|---|
| 390 | $this->message = apply_filters( 'messages_notice_message_before_save', $this->message, $this->id ); |
|---|
| 391 | |
|---|
| 392 | do_action( 'messages_notice_before_save', $this ); |
|---|
| 393 | |
|---|
| 394 | if ( !$this->id ) { |
|---|
| 395 | $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, %s, %d)", $this->subject, $this->message, $this->date_sent, $this->is_active ); |
|---|
| 396 | } else { |
|---|
| 397 | $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET subject = %s, message = %s, is_active = %d WHERE id = %d", $this->subject, $this->message, $this->is_active, $this->id ); |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | if ( !$wpdb->query($sql) ) |
|---|
| 401 | return false; |
|---|
| 402 | |
|---|
| 403 | if ( !$id = $this->id ) |
|---|
| 404 | $id = $wpdb->insert_id; |
|---|
| 405 | |
|---|
| 406 | // Now deactivate all notices apart from the new one. |
|---|
| 407 | $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET is_active = 0 WHERE id != %d", $id ) ); |
|---|
| 408 | |
|---|
| 409 | update_user_meta( $bp->loggedin_user->id, 'last_activity', date( 'Y-m-d H:i:s' ) ); |
|---|
| 410 | |
|---|
| 411 | do_action( 'messages_notice_after_save', $this ); |
|---|
| 412 | |
|---|
| 413 | return true; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | function activate() { |
|---|
| 417 | $this->is_active = 1; |
|---|
| 418 | if ( !$this->save() ) |
|---|
| 419 | return false; |
|---|
| 420 | |
|---|
| 421 | return true; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | function deactivate() { |
|---|
| 425 | $this->is_active = 0; |
|---|
| 426 | if ( !$this->save() ) |
|---|
| 427 | return false; |
|---|
| 428 | |
|---|
| 429 | return true; |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | function delete() { |
|---|
| 433 | global $wpdb, $bp; |
|---|
| 434 | |
|---|
| 435 | $sql = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ); |
|---|
| 436 | |
|---|
| 437 | if ( !$wpdb->query($sql) ) |
|---|
| 438 | return false; |
|---|
| 439 | |
|---|
| 440 | return true; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | // Static Functions |
|---|
| 444 | |
|---|
| 445 | function get_notices() { |
|---|
| 446 | global $wpdb, $bp; |
|---|
| 447 | |
|---|
| 448 | $notices = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_notices} ORDER BY date_sent DESC" ) ); |
|---|
| 449 | return $notices; |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | function get_total_notice_count() { |
|---|
| 453 | global $wpdb, $bp; |
|---|
| 454 | |
|---|
| 455 | $notice_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM " . $bp->messages->table_name_notices ) ); |
|---|
| 456 | |
|---|
| 457 | return $notice_count; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | function get_active() { |
|---|
| 461 | global $wpdb, $bp; |
|---|
| 462 | |
|---|
| 463 | $notice_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1") ); |
|---|
| 464 | return new BP_Messages_Notice($notice_id); |
|---|
| 465 | } |
|---|
| 466 | } |
|---|
| 467 | ?> |
|---|