Changeset 2842 for trunk/bp-blogs/bp-blogs-classes.php
- Timestamp:
- 03/12/2010 01:03:42 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-blogs/bp-blogs-classes.php
r2784 r2842 279 279 return $paged_blogs; 280 280 281 /* Fetch lastest post for each blog. */ 282 $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_id, p.blog_id FROM {$bp->blogs->table_name_blog_posts} p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.blog_id IN ( {$blog_ids} ) GROUP BY p.blog_id ORDER BY p.date_created DESC" ) ); 283 284 for ( $i = 0; $i < count( $paged_blogs ); $i++ ) { 285 foreach ( (array)$post_ids as $post ) { 286 if ( $post->blog_id == $paged_blogs[$i]->blog_id ) { 287 $paged_blogs[$i]->latest_post = $wpdb->get_row( "SELECT post_title, guid FROM {$wpdb->base_prefix}" . $post->blog_id . "_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY id DESC LIMIT 1" ); 288 } 289 } 290 } 281 for ( $i = 0; $i < count( $paged_blogs ); $i++ ) 282 $paged_blogs[$i]->latest_post = $wpdb->get_row( "SELECT post_title, guid FROM {$wpdb->base_prefix}" . $paged_blogs[$i]->blog_id . "_posts WHERE post_status = 'publish' AND post_type = 'post' AND id != 1 ORDER BY id DESC LIMIT 1" ); 291 283 292 284 /* Fetch the blog description for each blog (as it may be empty we can't fetch it in the main query). */ … … 316 308 } 317 309 318 /* DEPRECATED - Post DB recording is scheduled for removal. Please use the activity stream to fetch a user's posts. */319 Class BP_Blogs_Post {320 var $id;321 var $user_id;322 var $blog_id;323 var $post_id;324 var $date_created;325 326 function bp_blogs_post( $id = null, $blog_id = null, $post_id = null ) {327 global $bp, $wpdb;328 329 if ( $id || ( !$id && $blog_id && $post_id ) ) {330 $this->id = $id;331 $this->blog_id = $blog_id;332 $this->post_id = $post_id;333 $this->populate();334 }335 }336 337 function populate() {338 global $wpdb, $bp;339 340 if ( $this->id )341 $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_posts} WHERE id = %d", $this->id ) );342 else343 $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d AND post_id = %d", $this->blog_id, $this->post_id ) );344 345 $this->id = $post->id;346 $this->user_id = $post->user_id;347 $this->blog_id = $post->blog_id;348 $this->post_id = $post->post_id;349 $this->date_created = $post->date_created;350 }351 352 function save() {353 global $wpdb, $bp;354 355 $this->post_id = apply_filters( 'bp_blogs_post_id_before_save', $this->post_id, $this->id );356 $this->blog_id = apply_filters( 'bp_blogs_post_blog_id_before_save', $this->blog_id, $this->id );357 $this->user_id = apply_filters( 'bp_blogs_post_user_id_before_save', $this->user_id, $this->id );358 $this->date_created = apply_filters( 'bp_blogs_post_date_created_before_save', $this->date_created, $this->id );359 360 do_action( 'bp_blogs_post_before_save', $this );361 362 if ( $this->id ) {363 // Update364 $sql = $wpdb->prepare( "UPDATE {$bp->blogs->table_name_blog_posts} SET post_id = %d, blog_id = %d, user_id = %d, date_created = FROM_UNIXTIME(%d) WHERE id = %d", $this->post_id, $this->blog_id, $this->user_id, $this->date_created, $this->id );365 } else {366 // Save367 $sql = $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name_blog_posts} ( post_id, blog_id, user_id, date_created ) VALUES ( %d, %d, %d, FROM_UNIXTIME(%d) )", $this->post_id, $this->blog_id, $this->user_id, $this->date_created );368 }369 370 if ( !$wpdb->query($sql) )371 return false;372 373 do_action( 'bp_blogs_post_after_save', $this );374 375 if ( $this->id )376 return $this->id;377 else378 return $wpdb->insert_id;379 }380 381 /* Static Functions */382 383 function delete( $post_id, $blog_id ) {384 global $wpdb, $bp, $current_user;385 386 if ( !$bp->blogs )387 bp_blogs_setup_globals();388 389 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d AND post_id = %d", $blog_id, $post_id ) );390 }391 392 function delete_oldest( $user_id = null ) {393 global $wpdb, $bp;394 395 if ( !$bp->blogs )396 bp_blogs_setup_globals();397 398 if ( !$user_id )399 $user_id = $current_user->ID;400 401 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE user_id = %d ORDER BY date_created ASC LIMIT 1", $user_id ) );402 }403 404 function delete_posts_for_user( $user_id = null ) {405 global $wpdb, $bp;406 407 if ( !$bp->blogs )408 bp_blogs_setup_globals();409 410 if ( !$user_id )411 $user_id = $bp->loggedin_user->id;412 413 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE user_id = %d", $user_id ) );414 }415 416 function delete_posts_for_blog( $blog_id ) {417 global $wpdb, $bp;418 419 if ( !$bp->blogs )420 bp_blogs_setup_globals();421 422 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d", $blog_id ) );423 }424 425 function get_latest_posts( $blog_id = null, $limit = 5 ) {426 global $wpdb, $bp;427 428 if ( !$bp->blogs )429 bp_blogs_setup_globals();430 431 if ( $blog_id )432 $blog_sql = $wpdb->prepare( " AND p.blog_id = %d", $blog_id );433 434 $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT p.post_id, p.blog_id FROM {$bp->blogs->table_name_blog_posts} p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 $blog_sql ORDER BY p.date_created DESC LIMIT $limit" ) );435 436 for ( $i = 0; $i < count($post_ids); $i++ ) {437 $posts[$i] = BP_Blogs_Post::fetch_post_content($post_ids[$i]);438 }439 440 return $posts;441 }442 443 function get_posts_for_user( $user_id = null ) {444 global $bp, $wpdb;445 446 if ( !$bp->blogs )447 bp_blogs_setup_globals();448 449 if ( !$user_id )450 $user_id = $bp->displayed_user->id;451 452 // Show a logged in user their posts on private blogs, but not anyone else.453 if ( !bp_is_my_profile() ) {454 $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_id, p.blog_id FROM {$bp->blogs->table_name_blog_posts} p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.user_id = %d ORDER BY p.date_created DESC", $user_id) );455 $total_post_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(p.post_id) FROM {$bp->blogs->table_name_blog_posts} p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.user_id = %d", $user_id) );456 } else {457 $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_id, p.blog_id FROM {$bp->blogs->table_name_blog_posts} p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.user_id = %d ORDER BY p.date_created DESC", $user_id) );458 459 $total_post_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(p.post_id) FROM {$bp->blogs->table_name_blog_posts} p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.user_id = %d", $user_id) );460 }461 462 for ( $i = 0; $i < count($post_ids); $i++ ) {463 $posts[$i] = BP_Blogs_Post::fetch_post_content($post_ids[$i]);464 }465 466 return array( 'posts' => $posts, 'count' => $total_post_count );467 }468 469 function fetch_post_content( $post_object ) {470 // TODO: switch_to_blog() calls are expensive and this needs to be changed.471 switch_to_blog( $post_object->blog_id );472 $post = get_post($post_object->post_id);473 $post->blog_id = $post_object->blog_id;474 restore_current_blog();475 476 return $post;477 }478 479 function get_total_recorded_for_user( $user_id = null ) {480 global $bp, $wpdb;481 482 if ( !$bp->blogs )483 bp_blogs_setup_globals();484 485 if ( !$user_id )486 $user_id = $current_user->ID;487 488 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(post_id) FROM {$bp->blogs->table_name_blog_posts} WHERE user_id = %d", $user_id ) );489 }490 491 function is_recorded( $post_id, $blog_id, $user_id = null ) {492 global $bp, $wpdb, $current_user;493 494 if ( !$bp->blogs )495 bp_blogs_setup_globals();496 497 if ( !$user_id )498 $user_id = $current_user->ID;499 500 return $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$bp->blogs->table_name_blog_posts} WHERE post_id = %d AND blog_id = %d AND user_id = %d", $post_id, $blog_id, $user_id ) );501 }502 503 function total_post_count( $blog_id ) {504 global $bp, $wpdb;505 506 if ( !$bp->blogs )507 bp_blogs_setup_globals();508 509 if ( !$blog_id )510 return false;511 512 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(post_id) FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d", $blog_id ) );513 }514 515 function get_all() {516 global $bp, $wpdb;517 518 if ( !$bp->blogs )519 bp_blogs_setup_globals();520 521 return $wpdb->get_col( $wpdb->prepare( "SELECT post_id, blog_id FROM " . $bp->blogs->table_name_blog_posts ) );522 }523 524 }525 526 /* DEPRECATED - Comment DB recording is scheduled for removal. Please use the activity stream to fetch a user's comments. */527 Class BP_Blogs_Comment {528 var $id;529 var $user_id;530 var $blog_id;531 var $comment_id;532 var $comment_post_id;533 var $date_created;534 535 function bp_blogs_comment( $id = false, $blog_id = false, $comment_id = false ) {536 global $bp, $wpdb;537 538 if ( !$user_id )539 $user_id = $bp->displayed_user->id;540 541 if ( $id || ( !$id && $blog_id && $comment_id ) ) {542 $this->id = $id;543 $this->blog_id = $blog_id;544 $this->comment_id = $comment_id;545 $this->populate();546 }547 }548 549 function populate() {550 global $wpdb, $bp;551 552 if ( $this->id )553 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_comments} WHERE id = %d", $this->id ) );554 else555 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_comments} WHERE blog_id = %d AND comment_id = %d", (int)$this->blog_id, (int)$this->comment_id ) );556 557 $this->comment_id = $comment->comment_id;558 $this->user_id = $comment->user_id;559 $this->blog_id = $comment->blog_id;560 $this->comment_post_id = $comment->comment_post_id;561 $this->date_created = $comment->date_created;562 }563 564 function save() {565 global $wpdb, $bp;566 567 $this->comment_id = apply_filters( 'bp_blogs_comment_id_before_save', $this->comment_id, $this->id );568 $this->comment_post_id = apply_filters( 'bp_blogs_comment_post_id_before_save', $this->comment_post_id, $this->id );569 $this->blog_id = apply_filters( 'bp_blogs_comment_blog_id_before_save', $this->blog_id, $this->id );570 $this->user_id = apply_filters( 'bp_blogs_comment_user_id_before_save', $this->user_id, $this->id );571 $this->date_created = apply_filters( 'bp_blogs_comment_date_created_before_save', $this->date_created, $this->id );572 573 do_action( 'bp_blogs_comment_before_save', $this );574 575 if ( $this->id ) {576 // Update577 $sql = $wpdb->prepare( "UPDATE {$bp->blogs->table_name_blog_comments} SET comment_id = %d, comment_post_id = %d, blog_id = %d, user_id = %d, date_created = FROM_UNIXTIME(%d) WHERE id = %d", $this->comment_id, $this->comment_post_id, $this->blog_id, $this->user_id, $this->date_created, $this->id );578 } else {579 // Save580 $sql = $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name_blog_comments} ( comment_id, comment_post_id, blog_id, user_id, date_created ) VALUES ( %d, %d, %d, %d, FROM_UNIXTIME(%d) )", $this->comment_id, $this->comment_post_id, $this->blog_id, $this->user_id, $this->date_created );581 }582 583 if ( !$wpdb->query($sql) )584 return false;585 586 do_action( 'bp_blogs_comment_after_save', $this );587 588 if ( $this->id )589 return $this->id;590 else591 return $wpdb->insert_id;592 }593 594 /* Static Functions */595 596 function delete( $comment_id, $blog_id ) {597 global $wpdb, $bp, $current_user;598 599 if ( !$bp->blogs )600 bp_blogs_setup_globals();601 602 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE comment_id = %d AND blog_id = %d", $comment_id, $blog_id ) );603 }604 605 function delete_oldest( $user_id = null ) {606 global $wpdb, $bp, $current_user;607 608 if ( !$bp->blogs )609 bp_blogs_setup_globals();610 611 if ( !$user_id )612 $user_id = $current_user->ID;613 614 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE user_id = %d ORDER BY date_created ASC LIMIT 1", $user_id ) );615 }616 617 function delete_comments_for_user( $user_id = null ) {618 global $wpdb, $bp;619 620 if ( !$bp->blogs )621 bp_blogs_setup_globals();622 623 if ( !$user_id )624 $user_id = $bp->loggedin_user->id;625 626 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE user_id = %d", $user_id ) );627 }628 629 function delete_comments_for_blog( $blog_id ) {630 global $wpdb, $bp;631 632 if ( !$bp->blogs )633 bp_blogs_setup_globals();634 635 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE blog_id = %d", $blog_id ) );636 }637 638 function get_comments_for_user( $user_id = null ) {639 global $bp, $wpdb;640 641 if ( !$bp->blogs )642 bp_blogs_setup_globals();643 644 if ( !$user_id )645 $user_id = $bp->displayed_user->id;646 647 // Show the logged in user their comments on hidden blogs, but not to anyone else.648 if ( !bp_is_my_profile() ) {649 $comment_ids = $wpdb->get_results( $wpdb->prepare( "SELECT c.comment_id, c.blog_id FROM {$bp->blogs->table_name_blog_comments} c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d ORDER BY c.date_created ASC", $user_id) );650 $total_comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(c.comment_id) FROM {$bp->blogs->table_name_blog_comments} c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d", $user_id) );651 } else {652 $comment_ids = $wpdb->get_results( $wpdb->prepare( "SELECT c.comment_id, c.blog_id FROM {$bp->blogs->table_name_blog_comments} c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d ORDER BY c.date_created ASC", $user_id) );653 654 $total_comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(c.comment_id) FROM {$bp->blogs->table_name_blog_comments} c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d", $user_id) );655 }656 657 for ( $i = 0; $i < count($comment_ids); $i++ )658 $comments[$i] = BP_Blogs_Comment::fetch_comment_content($comment_ids[$i]);659 660 return array( 'comments' => $comments, 'count' => $total_comment_count );661 }662 663 function fetch_comment_content( $comment_object ) {664 switch_to_blog($comment_object->blog_id);665 $comment = get_comment($comment_object->comment_id);666 $comment->blog_id = $comment_object->blog_id;667 $comment->post = &get_post( $comment->comment_post_ID );668 restore_current_blog();669 670 return $comment;671 }672 673 function get_total_recorded_for_user( $user_id = null ) {674 global $bp, $wpdb, $current_user;675 676 if ( !$bp->blogs )677 bp_blogs_setup_globals();678 679 if ( !$user_id )680 $user_id = $current_user->ID;681 682 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_id) FROM {$bp->blogs->table_name_blog_comments} WHERE user_id = %d", $user_id ) );683 }684 685 function total_comment_count( $blog_id, $post_id ) {686 global $bp, $wpdb;687 688 if ( !$bp->blogs )689 bp_blogs_setup_globals();690 691 if ( $post_id )692 $post_sql = $wpdb->prepare( " AND comment_post_id = %d", $post_id );693 694 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_id) WHERE blog_id = %d{$post_sql}", $blog_id ) );695 }696 697 698 function is_recorded( $comment_id, $comment_post_id, $blog_id, $user_id = null ) {699 global $bp, $wpdb, $current_user;700 701 if ( !$bp->blogs )702 bp_blogs_setup_globals();703 704 if ( !$user_id )705 $user_id = $current_user->ID;706 707 return $wpdb->get_var( $wpdb->prepare( "SELECT comment_id FROM {$bp->blogs->table_name_blog_comments} WHERE comment_id = %d AND blog_id = %d AND comment_post_id = %d AND user_id = %d", $comment_id, $blog_id, $comment_post_id, $user_id ) );708 }709 710 }711 712 310 ?>
Note: See TracChangeset
for help on using the changeset viewer.