Skip to:
Content

BuddyPress.org

Changeset 2826


Ignore:
Timestamp:
03/10/2010 12:03:43 PM (15 years ago)
Author:
apeatling
Message:

Fixing errors in single WP setups with blog post recording by removing all no longer used blog and comment recording functions to the back compat plugin.

Location:
branches/1.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.2/bp-blogs.php

    r2794 r2826  
    2626                KEY user_id (user_id),
    2727                KEY blog_id (blog_id)
    28              ) {$charset_collate};";
    29 
    30     $sql[] = "CREATE TABLE {$bp->blogs->table_name_blog_posts} (
    31                 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    32                 user_id bigint(20) NOT NULL,
    33                 blog_id bigint(20) NOT NULL,
    34                 post_id bigint(20) NOT NULL,
    35                 date_created datetime NOT NULL,
    36                 KEY user_id (user_id),
    37                 KEY blog_id (blog_id),
    38                 KEY post_id (post_id)
    39              ) {$charset_collate};";
    40 
    41     $sql[] = "CREATE TABLE {$bp->blogs->table_name_blog_comments} (
    42                 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    43                 user_id bigint(20) NOT NULL,
    44                 blog_id bigint(20) NOT NULL,
    45                 comment_id bigint(20) NOT NULL,
    46                 comment_post_id bigint(20) NOT NULL,
    47                 date_created datetime NOT NULL,
    48                 KEY user_id (user_id),
    49                 KEY blog_id (blog_id),
    50                 KEY comment_id (comment_id),
    51                 KEY comment_post_id (comment_post_id)
    5228             ) {$charset_collate};";
    5329
     
    9470
    9571    $bp->blogs->table_name = $wpdb->base_prefix . 'bp_user_blogs';
    96     $bp->blogs->table_name_blog_posts = $wpdb->base_prefix . 'bp_user_blogs_posts';
    97     $bp->blogs->table_name_blog_comments = $wpdb->base_prefix . 'bp_user_blogs_comments';
    9872    $bp->blogs->table_name_blogmeta = $wpdb->base_prefix . 'bp_user_blogs_blogmeta';
    9973    $bp->blogs->format_notification_function = 'bp_blogs_format_notifications';
     
    181155    do_action( 'bp_blogs_screen_my_blogs' );
    182156    bp_core_load_template( apply_filters( 'bp_blogs_template_my_blogs', 'members/single/home' ) );
    183 }
    184 
    185 function bp_blogs_screen_recent_posts() {
    186     do_action( 'bp_blogs_screen_recent_posts' );
    187     bp_core_load_template( apply_filters( 'bp_blogs_template_recent_posts', 'members/single/home' ) );
    188 }
    189 
    190 function bp_blogs_screen_recent_comments() {
    191     do_action( 'bp_blogs_screen_recent_comments' );
    192     bp_core_load_template( apply_filters( 'bp_blogs_template_recent_comments', 'members/single/home' ) );
    193157}
    194158
     
    331295}
    332296
    333 
    334297function bp_blogs_record_existing_blogs() {
    335298    global $bp, $wpdb;
     
    403366        $user_id = (int)$post->post_author;
    404367
    405     /* This is to stop infinate loops with Donncha's sitewide tags plugin */
     368    /* This is to stop infinite loops with Donncha's sitewide tags plugin */
    406369    if ( (int)$bp->site_options['tags_blog_id'] == (int)$blog_id )
    407370        return false;
     
    411374        return false;
    412375
    413     if ( !$is_recorded = BP_Blogs_Post::is_recorded( $post_id, $blog_id, $user_id ) ) {
    414         if ( 'publish' == $post->post_status && '' == $post->post_password ) {
    415 
    416             /* If we're on a multiblog install, record this post */
    417             if ( bp_core_is_multisite() ) {
    418                 $recorded_post = new BP_Blogs_Post;
    419                 $recorded_post->user_id = $user_id;
    420                 $recorded_post->blog_id = $blog_id;
    421                 $recorded_post->post_id = $post_id;
    422                 $recorded_post->date_created = strtotime( $post->post_date );
    423 
    424                 $recorded_post_id = $recorded_post->save();
    425 
    426                 bp_blogs_update_blogmeta( $recorded_post->blog_id, 'last_activity', gmdate( "Y-m-d H:i:s" ) );
    427             }
    428 
    429             if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
    430                 /* Record this in activity streams */
    431                 $post_permalink = get_permalink( $post_id );
    432 
    433                 $activity_action = sprintf( __( '%s wrote a new blog post: %s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
    434                 $activity_content = $post->post_content;
    435 
    436                 bp_blogs_record_activity( array(
    437                     'user_id' => (int)$post->post_author,
    438                     'action' => apply_filters( 'bp_blogs_activity_new_post_action', $activity_action, &$post, $post_permalink ),
    439                     'content' => apply_filters( 'bp_blogs_activity_new_post_content', $activity_content, &$post, $post_permalink ),
    440                     'primary_link' => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink, $post_id ),
    441                     'type' => 'new_blog_post',
    442                     'item_id' => $blog_id,
    443                     'secondary_item_id' => $post_id,
    444                     'recorded_time' => $post->post_date_gmt
    445                 ));
    446             }
    447         }
    448     } else {
    449         $existing_post = new BP_Blogs_Post( null, $blog_id, $post_id );
    450 
    451         /* Delete the recorded post if the status is not published or it is password protected */
    452         if ( 'publish' != $post->post_status || '' != $post->post_password ) {
    453             return bp_blogs_remove_post( $post_id, $blog_id, $existing_post );
    454 
    455         /* If the post author has changed, delete the post and re-add it. */
    456         } else if ( (int)$existing_post->user_id != (int)$post->post_author ) {
    457             // Delete the existing recorded post
    458             bp_blogs_remove_post( $post_id, $blog_id, $existing_post );
    459 
    460             // Re-record the post with the new author.
    461             bp_blogs_record_post( $post_id );
    462         }
     376    if ( 'publish' == $post->post_status && '' == $post->post_password ) {
     377        bp_blogs_update_blogmeta( $recorded_post->blog_id, 'last_activity', gmdate( "Y-m-d H:i:s" ) );
    463378
    464379        if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
    465             /* Now re-record the post in the activity streams */
     380            /* Record this in activity streams */
    466381            $post_permalink = get_permalink( $post_id );
    467382
     
    478393                'secondary_item_id' => $post_id,
    479394                'recorded_time' => $post->post_date_gmt
    480             ) );
     395            ));
    481396        }
    482     }
    483 
    484     do_action( 'bp_blogs_new_blog_post', $existing_post, $is_private, $is_recorded );
     397    } else
     398        bp_blogs_remove_post( $post_id, $blog_id );
     399
     400    do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
    485401}
    486402add_action( 'save_post', 'bp_blogs_record_post', 10, 2 );
    487403
    488 function bp_blogs_record_comment( $comment_id, $is_approved ) {
     404function bp_blogs_record_comment( $comment_id, $is_approved = true ) {
    489405    global $wpdb, $bp;
    490 
    491     if ( !$is_approved )
    492         return false;
    493406
    494407    $comment = get_comment($comment_id);
     
    505418    if ( !empty( $post->post_password ) )
    506419        return false;
    507 
    508     /* If we're on a multiblog install, record this post */
    509     if ( bp_core_is_multisite() ) {
    510         $recorded_comment = new BP_Blogs_Comment;
    511         $recorded_comment->user_id = $user_id;
    512         $recorded_comment->blog_id = $wpdb->blogid;
    513         $recorded_comment->comment_id = $comment_id;
    514         $recorded_comment->comment_post_id = $comment->comment_post_ID;
    515         $recorded_comment->date_created = strtotime( $comment->comment_date_gmt );
    516 
    517         $recorded_commment_id = $recorded_comment->save();
    518 
    519         bp_blogs_update_blogmeta( $recorded_comment->blog_id, 'last_activity', gmdate( "Y-m-d H:i:s" ) );
    520     }
    521420
    522421    if ( (int)get_blog_option( $recorded_comment->blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
     
    542441}
    543442add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
     443add_action( 'edit_comment', 'bp_blogs_record_comment', 10 );
    544444
    545445function bp_blogs_manage_comment( $comment_id, $comment_status ) {
     
    601501add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 );
    602502
    603 function bp_blogs_remove_post( $post_id, $blog_id = false, $existing_post = false ) {
     503function bp_blogs_remove_post( $post_id, $blog_id = false ) {
    604504    global $current_blog, $bp;
    605505
     
    609509        $blog_id = (int)$current_blog->blog_id;
    610510
    611     if ( !$existing_post )
    612         $existing_post = new BP_Blogs_Post( null, $blog_id, $post_id );
    613 
    614     // Delete post from the bp_blogs table
    615     BP_Blogs_Post::delete( $post_id, $blog_id );
    616 
    617511    // Delete activity stream item
    618512    bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'secondary_item_id' => $post_id, 'component' => $bp->blogs->slug, 'type' => 'new_blog_post' ) );
     
    624518function bp_blogs_remove_comment( $comment_id ) {
    625519    global $wpdb, $bp;
    626 
    627     $recorded_comment = new BP_Blogs_Comment( false, $wpdb->blogid, $comment_id );
    628     BP_Blogs_Comment::delete( $comment_id, $wpdb->blogid );
    629520
    630521    // Delete activity stream item
     
    663554    /* If this is regular blog, delete all data for that blog. */
    664555    BP_Blogs_Blog::delete_blog_for_all( $blog_id );
    665     BP_Blogs_Post::delete_posts_for_blog( $blog_id );
    666     BP_Blogs_Comment::delete_comments_for_blog( $blog_id );
    667556
    668557    // Delete activity stream item
     
    677566}
    678567
    679 /* DEPRECATED - scheduled for removal. Please use the activity stream with a 'new_blog_post' filter. */
    680 function bp_blogs_get_posts_for_user( $user_id ) {
    681     return BP_Blogs_Post::get_posts_for_user( $user_id );
    682 }
    683 
    684 /* DEPRECATED - scheduled for removal. Please use the activity stream with a 'new_blog_comment' filter. */
    685 function bp_blogs_get_comments_for_user( $user_id ) {
    686     return BP_Blogs_Comment::get_comments_for_user( $user_id );
    687 }
    688 
    689 function bp_blogs_get_latest_posts( $blog_id = null, $limit = 5 ) {
    690     global $bp;
    691 
    692     if ( !is_numeric( $limit ) )
    693         $limit = 5;
    694 
    695     return BP_Blogs_Post::get_latest_posts( $blog_id, $limit );
    696 }
    697 
    698568function bp_blogs_get_all_blogs( $limit = null, $page = null ) {
    699569    return BP_Blogs_Blog::get_all( $limit, $page );
     
    702572function bp_blogs_get_random_blogs( $limit = null, $page = null ) {
    703573    return BP_Blogs_Blog::get( 'random', $limit, $page );
    704 }
    705 
    706 function bp_blogs_get_all_posts( $limit = null, $page = null ) {
    707     return BP_Blogs_Post::get_all( $limit, $page );
    708 }
    709 
    710 function bp_blogs_total_post_count( $blog_id ) {
    711     return BP_Blogs_Post::total_post_count( $blog_id );
    712 }
    713 
    714 function bp_blogs_total_comment_count( $blog_id, $post_id = false ) {
    715     return BP_Blogs_Post::total_comment_count( $blog_id, $post_id );
    716574}
    717575
     
    834692    /* If this is regular blog, delete all data for that blog. */
    835693    BP_Blogs_Blog::delete_blogs_for_user( $user_id );
    836     BP_Blogs_Post::delete_posts_for_user( $user_id );
    837     BP_Blogs_Comment::delete_comments_for_user( $user_id );
    838694
    839695    do_action( 'bp_blogs_remove_data', $user_id );
     
    867723}
    868724
    869 function bp_blogs_clear_post_object_cache( $blog_id, $post_id, $user_id ) {
    870     wp_cache_delete( 'bp_user_posts_' . $user_id, 'bp' );
    871 }
    872 
    873 function bp_blogs_format_clear_post_cache( $recorded_post_obj ) {
    874     bp_blogs_clear_post_object_cache( false, false, $recorded_post_obj->user_id );
    875 
    876     /* Clear the sitewide activity cache */
    877     wp_cache_delete( 'sitewide_activity', 'bp' );
    878 }
    879 
    880 function bp_blogs_clear_comment_object_cache( $blog_id, $comment_id, $user_id ) {
    881     wp_cache_delete( 'bp_user_comments_' . $user_id, 'bp' );
    882 }
    883 
    884 function bp_blogs_format_clear_comment_cache( $recorded_comment_obj ) {
    885     bp_blogs_clear_comment_object_cache( false, false, $recorded_comment_obj->user_id );
    886 
    887     /* Clear the sitewide activity cache */
    888     wp_cache_delete( 'sitewide_activity', 'bp' );
    889 }
    890 
    891725// List actions to clear object caches on
    892726add_action( 'bp_blogs_remove_blog_for_user', 'bp_blogs_clear_blog_object_cache', 10, 2 );
    893 add_action( 'bp_blogs_remove_post', 'bp_blogs_clear_post_object_cache', 10, 3 );
    894 add_action( 'bp_blogs_remove_comment', 'bp_blogs_clear_comment_object_cache', 10, 3 );
    895 
    896727add_action( 'bp_blogs_new_blog', 'bp_blogs_format_clear_blog_cache', 10, 2 );
    897 add_action( 'bp_blogs_new_blog_post', 'bp_blogs_format_clear_post_cache', 10, 2 );
    898 add_action( 'bp_blogs_new_blog_comment', 'bp_blogs_format_clear_comment_cache', 10, 2 );
    899728
    900729// List actions to clear super cached pages on, if super cache is installed
  • branches/1.2/bp-blogs/bp-blogs-classes.php

    r2772 r2826  
    279279            return $paged_blogs;
    280280
    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" );
    291283
    292284        /* Fetch the blog description for each blog (as it may be empty we can't fetch it in the main query). */
     
    316308}
    317309
    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         else
    343             $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             // Update
    364             $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             // Save
    367             $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         else
    378             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         else
    555             $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             // Update
    577             $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             // Save
    580             $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         else
    591             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 
    712310?>
Note: See TracChangeset for help on using the changeset viewer.