Changeset 2077 for trunk/bp-messages/bp-messages-classes.php
- Timestamp:
- 11/02/2009 07:54:21 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/bp-messages/bp-messages-classes.php (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-messages/bp-messages-classes.php
r2020 r2077 1 1 <?php 2 2 3 Class BP_Messages_Thread { 3 Class BP_Messages_Thread { 4 4 var $thread_id; 5 5 var $message_ids; 6 6 var $first_post_date; 7 7 8 8 var $last_post_date; 9 9 var $last_sender_id; … … 12 12 var $last_message_message; 13 13 var $last_message_date_sent; 14 14 15 15 var $messages = null; 16 16 var $has_access = false; 17 17 var $unread_count = 0; 18 18 var $recipients = null; 19 19 20 20 var $box; 21 21 var $get_all_messages; 22 22 23 23 function bp_messages_thread( $id = null, $get_all_messages = false, $box = 'inbox' ) { 24 24 $this->box = $box; 25 25 $this->get_all_messages = $get_all_messages; 26 26 27 27 if ( $id ) { 28 28 $this->populate( $id ); 29 29 } 30 30 31 31 if ( $this->get_all_messages ) { 32 32 $this->messages = $this->get_messages(); … … 34 34 } 35 35 } 36 37 function populate( $id ) { 36 37 function populate( $id ) { 38 38 global $wpdb, $bp; 39 39 40 40 $thread = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_threads} WHERE id = %d", $id ) ); 41 41 42 42 if ( $thread ) { 43 43 44 44 // If we're only viewing a thread in a list of threads, don't bother using 45 45 // up resources checking if we have access. Only check if we're viewing the full … … 57 57 // filter the users messages out 58 58 if ( !$this->get_all_messages ) { 59 59 60 60 // Flip the array to start from the newest message 61 61 $this->message_ids = array_reverse( $this->message_ids ); 62 62 63 63 foreach ( $this->message_ids as $key => $message_id ) { 64 64 if ( 'sentbox' == $this->box ) { … … 67 67 } else { 68 68 break; 69 } 69 } 70 70 } else { 71 71 if ( messages_is_user_sender( $bp->loggedin_user->id, $message_id ) ) { … … 73 73 } else { 74 74 break; 75 } 75 } 76 76 } 77 77 } … … 86 86 $this->first_post_date = $thread->first_post_date; 87 87 $this->last_post_date = $thread->last_post_date; 88 88 89 89 if ( !empty($this->message_ids) ) 90 90 $this->message_ids = implode( ',', $this->message_ids ); 91 91 else 92 92 $this->message_ids = false; 93 93 94 94 $this->unread_count = $this->get_unread(); 95 95 96 $last_message = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $this->last_message_id ) ); 97 96 $last_message = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $this->last_message_id ) ); 97 98 98 if ( $last_message ) { 99 99 $this->last_message_subject = $last_message->subject; … … 101 101 $this->last_message_date_sent = $last_message->date_sent; 102 102 } 103 103 104 104 $this->recipients = $this->get_recipients(); 105 105 } 106 106 } 107 107 } 108 108 109 109 function get_messages() { 110 110 global $wpdb, $bp; 111 111 112 112 if ( $this->message_ids) 113 113 return $wpdb->get_results( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id IN (" . $wpdb->escape($this->message_ids) . ")" ); … … 121 121 $sql = $wpdb->prepare( "SELECT unread_count FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND user_id = %d", $this->thread_id, $bp->loggedin_user->id ); 122 122 $unread_count = $wpdb->get_var($sql); 123 123 124 124 return $unread_count; 125 125 } 126 126 127 127 function mark_read() { 128 128 BP_Messages_Thread::mark_as_read($this->thread_id); 129 129 } 130 130 131 131 function mark_unread() { 132 132 BP_Messages_Thread::mark_as_unread($this->thread_id); 133 133 } 134 134 135 135 function get_recipients() { 136 136 global $wpdb, $bp; … … 144 144 $recipient_ids[] = $recipient; 145 145 } 146 146 147 147 return $recipient_ids; 148 148 } 149 149 150 150 /** Static Functions **/ 151 151 152 152 function delete( $thread_id ) { 153 153 global $wpdb, $bp; 154 154 155 155 $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 ) ); 156 156 157 157 // Check to see if any more recipients remain for this message 158 158 // if not, then delete the message from the database. … … 163 163 $message_ids = $wpdb->get_var( $wpdb->prepare( "SELECT message_ids FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) ); 164 164 $message_ids = unserialize($message_ids); 165 165 166 166 // delete thread: 167 167 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) ); 168 168 169 169 // delete messages: 170 170 for ( $i = 0; $i < count($message_ids); $i++ ) { 171 171 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_ids[$i] ) ); 172 172 } 173 173 174 174 // delete the recipients 175 175 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE id = %d", $thread_id ) ); … … 178 178 return true; 179 179 } 180 180 181 181 function get_current_threads_for_user( $user_id, $box = 'inbox', $limit = null, $page = null, $type = 'all' ) { 182 182 global $wpdb, $bp; … … 185 185 if ( $limit && $page ) 186 186 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 187 187 188 188 if ( $type == 'unread' ) 189 189 $type_sql = $wpdb->prepare( " AND r.unread_count != 0 " ); 190 190 else if ( $type == 'read' ) 191 191 $type_sql = $wpdb->prepare( " AND r.unread_count = 0 " ); 192 192 193 193 $sql = $wpdb->prepare( "SELECT r.thread_id FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_threads} t WHERE t.id = r.thread_id AND r.is_deleted = 0 AND r.user_id = %d$exclude_sender $type_sql ORDER BY t.last_post_date DESC$pag_sql", $bp->loggedin_user->id ); 194 194 195 195 if ( !$thread_ids = $wpdb->get_results($sql) ) 196 196 return false; 197 197 198 198 $threads = false; 199 199 200 200 for ( $i = 0; $i < count($thread_ids); $i++ ) { 201 201 $threads[$i] = new BP_Messages_Thread( $thread_ids[$i]->thread_id, false, $box ); … … 206 206 207 207 // reset keys 208 return array_reverse( array_reverse( $threads ) ); 209 } 210 208 return array_reverse( array_reverse( $threads ) ); 209 } 210 211 211 function mark_as_read( $thread_id ) { 212 212 global $wpdb, $bp; 213 213 214 214 $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 ); 215 215 $wpdb->query($sql); 216 216 } 217 217 218 218 function mark_as_unread( $thread_id ) { 219 219 global $wpdb, $bp; 220 220 221 221 $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 ); 222 222 $wpdb->query($sql); 223 223 } 224 224 225 225 function get_total_threads_for_user( $user_id, $box = 'inbox', $type = 'all' ) { 226 226 global $wpdb, $bp; … … 229 229 if ( $box != 'sentbox' ) 230 230 $exclude_sender = ' AND sender_only != 1'; 231 231 232 232 if ( $type == 'unread' ) 233 233 $type_sql = $wpdb->prepare( " AND unread_count != 0 " ); … … 237 237 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 ) ); 238 238 } 239 239 240 240 function user_is_sender($thread_id) { 241 241 global $wpdb, $bp; 242 243 $sender_ids = $wpdb->get_var( $wpdb->prepare( "SELECT sender_ids FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) ); 244 242 243 $sender_ids = $wpdb->get_var( $wpdb->prepare( "SELECT sender_ids FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) ); 244 245 245 if ( !$sender_ids ) 246 246 return false; 247 247 248 248 $sender_ids = unserialize($sender_ids); 249 249 250 250 return in_array( $bp->loggedin_user->id, $sender_ids ); 251 251 } … … 255 255 256 256 $sql = $wpdb->prepare("SELECT last_sender_id FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id); 257 257 258 258 if ( !$sender_id = $wpdb->get_var($sql) ) 259 259 return false; 260 260 261 261 return bp_core_get_userlink( $sender_id, true ); 262 262 } 263 263 264 264 function get_inbox_count() { 265 265 global $wpdb, $bp; … … 269 269 if ( !$unread_counts = $wpdb->get_results($sql) ) 270 270 return false; 271 271 272 272 $count = 0; 273 273 for ( $i = 0; $i < count($unread_counts); $i++ ) { 274 274 $count += $unread_counts[$i]->unread_count; 275 275 } 276 276 277 277 return $count; 278 278 } 279 279 280 280 function check_access( $thread_id, $user_id = false ) { 281 281 global $wpdb, $bp; 282 282 283 283 if ( !$user_id ) 284 284 $user_id = $bp->loggedin_user->id; 285 285 286 286 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 ) ); 287 287 } … … 289 289 function is_valid( $thread_id ) { 290 290 global $wpdb, $bp; 291 291 292 292 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) ); 293 293 } 294 294 295 295 function get_recipient_links($recipients) { 296 296 if ( count($recipients) >= 5 ) … … 311 311 var $message; 312 312 var $date_sent; 313 313 314 314 var $thread_id; 315 315 var $recipients = false; … … 325 325 } 326 326 } 327 327 328 328 function populate( $id ) { 329 329 global $wpdb, $bp; 330 330 331 331 $sql = $wpdb->prepare("SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $id); 332 332 … … 340 340 341 341 } 342 343 function send() { 344 global $wpdb, $bp; 345 342 343 function send() { 344 global $wpdb, $bp; 345 346 346 $this->sender_id = apply_filters( 'messages_message_sender_id_before_save', $this->sender_id, $this->id ); 347 347 $this->subject = apply_filters( 'messages_message_subject_before_save', $this->subject, $this->id ); 348 348 $this->message = apply_filters( 'messages_message_content_before_save', $this->message, $this->id ); 349 $this->date_sent = apply_filters( 'messages_message_date_sent_before_save', $this->date_sent, $this->id ); 349 $this->date_sent = apply_filters( 'messages_message_date_sent_before_save', $this->date_sent, $this->id ); 350 350 351 351 do_action( 'messages_message_before_save', $this ); 352 352 353 353 // First insert the message into the messages table 354 354 if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_messages} ( sender_id, subject, message, date_sent ) VALUES ( %d, %s, %s, FROM_UNIXTIME(%d) )", $this->sender_id, $this->subject, $this->message, $this->date_sent ) ) ) 355 355 return false; 356 356 357 357 // Next, if thread_id is set, we are adding to an existing thread, if not, start a new one. 358 358 if ( $this->thread_id ) { … … 362 362 $message_ids[] = $wpdb->insert_id; 363 363 $message_ids = serialize($message_ids); 364 364 365 365 // We need this so we can return the new message ID. 366 366 $message_id = $wpdb->insert_id; 367 367 368 368 // Update the sender ids for the thread 369 369 $sender_ids = unserialize($the_ids->sender_ids); … … 371 371 if ( !in_array( $this->sender_id, $sender_ids ) || !$sender_ids ) 372 372 $sender_ids[] = $this->sender_id; 373 374 $sender_ids = serialize($sender_ids); 375 373 374 $sender_ids = serialize($sender_ids); 375 376 376 // Update the thread the message belongs to. 377 377 $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_threads} SET message_ids = %s, sender_ids = %s, last_message_id = %d, last_sender_id = %d, last_post_date = FROM_UNIXTIME(%d) WHERE id = %d", $message_ids, $sender_ids, $wpdb->insert_id, $this->sender_id, $this->date_sent, $this->thread_id ) ); 378 378 379 379 // Find the recipients and update the unread counts for each 380 380 if ( !$this->recipients ) 381 381 $this->recipients = $this->get_recipients(); 382 382 383 383 for ( $i = 0; $i < count($this->recipients); $i++ ) { 384 384 if ( $this->recipients[$i]->user_id != $bp->loggedin_user->id ) … … 390 390 $serialized_message_id = serialize( array( (int)$message_id ) ); 391 391 $serialized_sender_id = serialize( array( (int)$bp->loggedin_user->id ) ); 392 393 $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_threads} ( message_ids, sender_ids, first_post_date, last_post_date, last_message_id, last_sender_id ) VALUES ( %s, %s, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d), %d, %d )", $serialized_message_id, $serialized_sender_id, $this->date_sent, $this->date_sent, $message_id, $this->sender_id ); 394 392 393 $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_threads} ( message_ids, sender_ids, first_post_date, last_post_date, last_message_id, last_sender_id ) VALUES ( %s, %s, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d), %d, %d )", $serialized_message_id, $serialized_sender_id, $this->date_sent, $this->date_sent, $message_id, $this->sender_id ); 394 395 395 if ( false === $wpdb->query($sql) ) 396 396 return false; 397 397 398 398 399 399 $this->thread_id = $wpdb->insert_id; 400 400 401 401 // Add a new entry for each recipient; 402 402 for ( $i = 0; $i < count($this->recipients); $i++ ) { 403 403 $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id, unread_count ) VALUES ( %d, %d, 1 )", $this->recipients[$i], $this->thread_id ) ); 404 404 } 405 405 406 406 if ( !in_array( $this->sender_id, (array)$this->recipients ) ) { 407 407 // Finally, add a recipient entry for the sender, as replies need to go to this person too. … … 409 409 } 410 410 } 411 411 412 412 $this->id = $message_id; 413 413 messages_remove_callback_values(); 414 414 415 415 do_action( 'messages_message_after_save', $this ); 416 416 417 417 return $this->thread_id; 418 418 } 419 419 420 420 function get_recipients() { 421 421 global $bp, $wpdb; 422 422 423 423 return $wpdb->get_results( $wpdb->prepare( "SELECT user_id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) ); 424 424 } 425 425 426 426 // Static Functions 427 427 428 428 function get_recipient_ids( $recipient_usernames ) { 429 429 if ( !$recipient_usernames ) 430 430 return false; 431 431 432 432 if ( is_array($recipient_usernames) ) { 433 433 for ( $i = 0; $i < count($recipient_usernames); $i++ ) { … … 436 436 } 437 437 } 438 438 439 439 return $recipient_ids; 440 440 } 441 441 442 442 function get_last_sent_for_user( $thread_id ) { 443 443 global $wpdb, $bp; 444 444 445 445 $message_ids = $wpdb->get_var( $wpdb->prepare( "SELECT message_ids FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) ); 446 446 $message_ids = implode( ',', unserialize($message_ids)); … … 448 448 $sql = $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE sender_id = %d AND id IN (" . $wpdb->escape($message_ids) . ") ORDER BY date_sent DESC LIMIT 1", $bp->loggedin_user->id ); 449 449 return $wpdb->get_var($sql); 450 } 451 450 } 451 452 452 function is_user_sender( $user_id, $message_id ) { 453 453 global $wpdb, $bp; 454 454 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 ) ); 455 455 } 456 456 457 457 function get_message_sender( $message_id ) { 458 458 global $wpdb, $bp; 459 return $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_id ) ); 459 return $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_id ) ); 460 460 } 461 461 } … … 467 467 var $date_sent; 468 468 var $is_active; 469 469 470 470 function bp_messages_notice($id = null) { 471 471 if ( $id ) { … … 474 474 } 475 475 } 476 476 477 477 function populate() { 478 478 global $wpdb, $bp; 479 479 480 480 $notice = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ) ); 481 481 482 482 if ( $notice ) { 483 483 $this->subject = $notice->subject; … … 487 487 } 488 488 } 489 489 490 490 function save() { 491 491 global $wpdb, $bp; 492 492 493 493 $this->subject = apply_filters( 'messages_notice_subject_before_save', $this->subject, $this->id ); 494 494 $this->message = apply_filters( 'messages_notice_message_before_save', $this->message, $this->id ); 495 495 496 496 do_action( 'messages_notice_before_save', $this ); 497 497 498 498 if ( !$this->id ) { 499 $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, FROM_UNIXTIME(%d), %d)", $this->subject, $this->message, $this->date_sent, $this->is_active ); 499 $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, FROM_UNIXTIME(%d), %d)", $this->subject, $this->message, $this->date_sent, $this->is_active ); 500 500 } else { 501 $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 ); 502 } 503 501 $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 ); 502 } 503 504 504 if ( !$wpdb->query($sql) ) 505 505 return false; 506 506 507 507 if ( !$id = $this->id ) 508 508 $id = $wpdb->insert_id; 509 509 510 510 // Now deactivate all notices apart from the new one. 511 511 $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET is_active = 0 WHERE id != %d", $id ) ); 512 513 update_usermeta( $bp->loggedin_user->id, 'last_activity', date( 'Y-m-d H:i:s' ) ); 512 513 update_usermeta( $bp->loggedin_user->id, 'last_activity', date( 'Y-m-d H:i:s' ) ); 514 514 515 515 do_action( 'messages_notice_after_save', $this ); 516 516 517 517 return true; 518 518 } 519 519 520 520 function activate() { 521 521 $this->is_active = 1; 522 522 if ( !$this->save() ) 523 523 return false; 524 524 525 525 return true; 526 526 } 527 527 528 528 function deactivate() { 529 529 $this->is_active = 0; 530 530 if ( !$this->save() ) 531 531 return false; 532 532 533 533 return true; 534 534 } 535 535 536 536 function delete() { 537 537 global $wpdb, $bp; 538 538 539 539 $sql = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ); 540 540 541 541 if ( !$wpdb->query($sql) ) 542 542 return false; 543 544 return true; 545 } 546 543 544 return true; 545 } 546 547 547 // Static Functions 548 548 549 549 function get_notices() { 550 550 global $wpdb, $bp; 551 551 552 552 $notices = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_notices} ORDER BY date_sent DESC" ) ); 553 553 return $notices; 554 554 } 555 555 556 556 function get_total_notice_count() { 557 557 global $wpdb, $bp; 558 558 559 559 $notice_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM " . $bp->messages->table_name_notices ) ); 560 560 return $notice_count; 561 561 } 562 562 563 563 function get_active() { 564 564 global $wpdb, $bp; 565 565 566 566 $notice_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1") ); 567 567 return new BP_Messages_Notice($notice_id);
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)