Skip to:
Content

BuddyPress.org

Changeset 3761


Ignore:
Timestamp:
01/20/2011 11:49:24 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Refactor blogs component to use BP_Component. Split code out of bp-blogs-loader into smaller files.

Location:
trunk/bp-blogs
Files:
5 added
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/bp-blogs/bp-blogs-loader.php

    r3757 r3761  
    11<?php
    22
    3 // Required Files
    4 require ( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-classes.php' );
    5 require ( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-templatetags.php' );
    6 
    7 // Include the sitewide blog posts widget if this is a multisite installation
    8 if ( is_multisite() )
    9     require ( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-widgets.php' );
     3/**
     4 * BuddyPress Blogs Streams Loader
     5 *
     6 * An blogs stream component, for users, groups, and blog tracking.
     7 *
     8 * @package BuddyPress
     9 * @subpackage Blogs Core
     10 */
     11
     12class BP_Blogs_Component extends BP_Component {
     13
     14    /**
     15     * Start the blogs component creation process
     16     *
     17     * @since BuddyPress {unknown}
     18     */
     19    function BP_Blogs_Component() {
     20        parent::start( 'blogs', __( 'Blogs Streams', 'buddypress' ) );
     21    }
     22
     23    /**
     24     * Setup globals
     25     *
     26     * The BP_BLOGS_SLUG constant is deprecated, and only used here for
     27     * backwards compatibility.
     28     *
     29     * @since BuddyPress {unknown}
     30     * @global obj $bp
     31     */
     32    function _setup_globals() {
     33        global $bp;
     34
     35        if ( !defined( 'BP_BLOGS_SLUG' ) )
     36            define ( 'BP_BLOGS_SLUG', $this->id );
     37
     38        // Do some slug checks
     39        $this->slug      = BP_BLOGS_SLUG;
     40        $this->root_slug = isset( $bp->pages->blogs->slug ) ? $bp->pages->blogs->slug : $this->slug;
     41
     42        // Tables
     43        $this->table_name      = $bp->table_prefix . 'bp_user_blogs';
     44        $this->table_name_meta = $bp->table_prefix . 'bp_user_blogs_blogmeta';
     45
     46        // Notifications
     47        $bp->blogs->notification_callback = 'bp_blogs_format_notifications';
     48
     49        // Register this in the active components array
     50        $bp->active_components[$this->slug] = $this->id;
     51
     52        // The default text for the blogs directory search box
     53        $bp->default_search_strings[$this->slug] = __( 'Search Blogs...', 'buddypress' );
     54    }
     55
     56    /**
     57     * Include files
     58     */
     59    function _includes() {
     60        require_once( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-cache.php'        );
     61        require_once( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-classes.php'      );
     62        require_once( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-screens.php'      );
     63        require_once( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-actions.php'      );
     64        require_once( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-activity.php'     );
     65        require_once( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-template.php'     );
     66        require_once( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-functions.php'    );
     67
     68
     69
     70        if ( is_multisite() )
     71            require_once( BP_PLUGIN_DIR . '/bp-blogs/bp-blogs-widgets.php' );
     72    }
     73
     74    /**
     75     * Setup BuddyBar navigation
     76     *
     77     * @global obj $bp
     78     */
     79    function _setup_nav() {
     80        global $bp;
     81
     82        // Add 'Blogs' to the main navigation
     83        bp_core_new_nav_item( array(
     84            'name'                => __( 'Blogs', 'buddypress' ),
     85            'slug'                => $bp->blogs->slug,
     86            'position'            => 10,
     87            'screen_function'     => 'bp_blogs_screen_my_blogs',
     88            'default_subnav_slug' => 'just-me',
     89            'item_css_id'         => $bp->blogs->id )
     90        );
     91
     92        // Stop if there is no user displayed or logged in
     93        if ( !is_user_logged_in() && !isset( $bp->displayed_user->id ) )
     94            return;
     95
     96        // User links
     97        $user_domain   = ( isset( $bp->displayed_user->domain ) )               ? $bp->displayed_user->domain               : $bp->loggedin_user->domain;
     98        $user_login    = ( isset( $bp->displayed_user->userdata->user_login ) ) ? $bp->displayed_user->userdata->user_login : $bp->loggedin_user->userdata->user_login;
     99        $blogs_link = $user_domain . $bp->blogs->slug . '/';
     100
     101        // Add the subnav items to the blogs nav item if we are using a theme that supports this
     102        bp_core_new_subnav_item( array(
     103            'name'            => __( 'Personal', 'buddypress' ),
     104            'slug'            => 'just-me',
     105            'parent_url'      => $blogs_link,
     106            'parent_slug'     => $bp->blogs->slug,
     107            'screen_function' => 'bp_blogs_screen_my_blogs',
     108            'position'        => 10
     109        ) );
     110
     111        // Additional menu if friends is active
     112        if ( bp_is_active( 'friends' ) ) {
     113            bp_core_new_subnav_item( array(
     114                'name'            => __( 'Friends', 'buddypress' ),
     115                'slug'            => BP_FRIENDS_SLUG,
     116                'parent_url'      => $blogs_link,
     117                'parent_slug'     => $bp->blogs->slug,
     118                'screen_function' => 'bp_blogs_screen_friends',
     119                'position'        => 20,
     120                'item_css_id'     => 'blogs-friends'
     121            ) );
     122        }
     123
     124        // Additional menu if groups is active
     125        if ( bp_is_active( 'groups' ) ) {
     126            bp_core_new_subnav_item( array(
     127                'name'            => __( 'Groups', 'buddypress' ),
     128                'slug'            => BP_GROUPS_SLUG,
     129                'parent_url'      => $blogs_link,
     130                'parent_slug'     => $bp->blogs->slug,
     131                'screen_function' => 'bp_blogs_screen_groups',
     132                'position'        => 30,
     133                'item_css_id'     => 'blogs-groups'
     134            ) );
     135        }
     136
     137        // Favorite blogs items
     138        bp_core_new_subnav_item( array(
     139            'name'            => __( 'Favorites', 'buddypress' ),
     140            'slug'            => 'favorites',
     141            'parent_url'      => $blogs_link,
     142            'parent_slug'     => $bp->blogs->slug,
     143            'screen_function' => 'bp_blogs_screen_favorites',
     144            'position'        => 40,
     145            'item_css_id'     => 'blogs-favs'
     146        ) );
     147
     148        // @ mentions
     149        bp_core_new_subnav_item( array(
     150            'name'            => sprintf( __( '@%s Mentions', 'buddypress' ), $user_login ),
     151            'slug'            => 'mentions',
     152            'parent_url'      => $blogs_link,
     153            'parent_slug'     => $bp->blogs->slug,
     154            'screen_function' => 'bp_blogs_screen_mentions',
     155            'position'        => 50,
     156            'item_css_id'     => 'blogs-mentions'
     157        ) );
     158
     159        // Adjust title based on view
     160        if ( bp_is_blogs_component() ) {
     161            if ( bp_is_my_profile() ) {
     162                $bp->bp_options_title = __( 'My Blogs', 'buddypress' );
     163            } else {
     164                $bp->bp_options_avatar = bp_core_fetch_avatar( array(
     165                    'item_id' => $bp->displayed_user->id,
     166                    'type'    => 'thumb'
     167                ) );
     168                $bp->bp_options_title  = $bp->displayed_user->fullname;
     169            }
     170        }
     171    }
     172}
     173// Create the blogs component
     174$bp->blogs = new BP_Blogs_Component();
     175
     176?>
     177
     178
     179
     180<?php
    10181
    11182function bp_blogs_setup_globals() {
     
    95266add_action( 'wp', 'bp_blogs_directory_blogs_setup', 2 );
    96267
    97 
    98 /*******************************************************************************
    99  * Screen Functions
    100  *
    101  * Screen functions are the controllers of BuddyPress. They will execute when their
    102  * specific URL is caught. They will first save or manipulate data using business
    103  * functions, then pass on the user to a template file.
    104  */
    105 
    106 function bp_blogs_screen_my_blogs() {
    107     global $bp;
    108 
    109     if ( !is_multisite() )
    110         return false;
    111 
    112     do_action( 'bp_blogs_screen_my_blogs' );
    113     bp_core_load_template( apply_filters( 'bp_blogs_template_my_blogs', 'members/single/home' ) );
    114 }
    115 
    116 function bp_blogs_screen_create_a_blog() {
    117     global $bp;
    118 
    119     if ( !is_multisite() || $bp->current_component != $bp->blogs->slug || 'create' != $bp->current_action )
    120         return false;
    121 
    122     if ( !is_user_logged_in() || !bp_blog_signup_enabled() )
    123         return false;
    124 
    125     do_action( 'bp_blogs_screen_create_a_blog' );
    126     bp_core_load_template( apply_filters( 'bp_blogs_template_create_a_blog', 'blogs/create' ) );
    127 }
    128 add_action( 'wp', 'bp_blogs_screen_create_a_blog', 3 );
    129 
    130 
    131 /*******************************************************************************
    132  * Activity & Notification Functions
    133  *
    134  * These functions handle the recording, deleting and formatting of activity and
    135  * notifications for the user and for this specific component.
    136  */
    137 
    138 function bp_blogs_register_activity_actions() {
    139     global $bp;
    140 
    141     if ( !function_exists( 'bp_activity_set_action' ) )
    142         return false;
    143 
    144     bp_activity_set_action( $bp->blogs->id, 'new_blog', __( 'New blog created', 'buddypress' ) );
    145     bp_activity_set_action( $bp->blogs->id, 'new_blog_post', __( 'New blog post published', 'buddypress' ) );
    146     bp_activity_set_action( $bp->blogs->id, 'new_blog_comment', __( 'New blog post comment posted', 'buddypress' ) );
    147 
    148     do_action( 'bp_blogs_register_activity_actions' );
    149 }
    150 add_action( 'bp_register_activity_actions', 'bp_blogs_register_activity_actions' );
    151 
    152 function bp_blogs_record_activity( $args = '' ) {
    153     global $bp;
    154 
    155     if ( !function_exists( 'bp_activity_add' ) )
    156         return false;
    157 
    158     /**
    159      * Because blog, comment, and blog post code execution happens before
    160      * anything else we may need to manually instantiate the activity
    161      * component globals.
    162      */
    163     if ( !$bp->activity && function_exists('bp_activity_setup_globals') )
    164         bp_activity_setup_globals();
    165 
    166     $defaults = array(
    167         'user_id'           => $bp->loggedin_user->id,
    168         'action'            => '',
    169         'content'           => '',
    170         'primary_link'      => '',
    171         'component'         => $bp->blogs->id,
    172         'type'              => false,
    173         'item_id'           => false,
    174         'secondary_item_id' => false,
    175         'recorded_time'     => bp_core_current_time(),
    176         'hide_sitewide'     => false
    177     );
    178 
    179     $r = wp_parse_args( $args, $defaults );
    180     extract( $r, EXTR_SKIP );
    181 
    182     // Remove large images and replace them with just one image thumbnail
    183     if ( function_exists( 'bp_activity_thumbnail_content_images' ) && !empty( $content ) )
    184         $content = bp_activity_thumbnail_content_images( $content, $primary_link );
    185 
    186     if ( !empty( $action ) )
    187         $action = apply_filters( 'bp_blogs_record_activity_action', $action );
    188 
    189     if ( !empty( $content ) )
    190         $content = apply_filters( 'bp_blogs_record_activity_content', bp_create_excerpt( $content ), $content );
    191 
    192     // Check for an existing entry and update if one exists.
    193     $id = bp_activity_get_activity_id( array(
    194         'user_id' => $user_id,
    195         'component' => $component,
    196         'type' => $type,
    197         'item_id' => $item_id,
    198         'secondary_item_id' => $secondary_item_id
    199     ) );
    200 
    201     return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) );
    202 }
    203 
    204 function bp_blogs_delete_activity( $args = true ) {
    205     global $bp;
    206 
    207     if ( function_exists( 'bp_activity_delete_by_item_id' ) ) {
    208         $defaults = array(
    209             'item_id'           => false,
    210             'component'         => $bp->blogs->id,
    211             'type'              => false,
    212             'user_id'           => false,
    213             'secondary_item_id' => false
    214         );
    215 
    216         $params = wp_parse_args( $args, $defaults );
    217         extract( $params, EXTR_SKIP );
    218 
    219         bp_activity_delete_by_item_id( array(
    220             'item_id' => $item_id,
    221             'component' => $component,
    222             'type' => $type,
    223             'user_id' => $user_id,
    224             'secondary_item_id' => $secondary_item_id
    225         ) );
    226     }
    227 }
    228 
    229 /********************************************************************************
    230  * Business Functions
    231  *
    232  * Business functions are where all the magic happens in BuddyPress. They will
    233  * handle the actual saving or manipulation of information. Usually they will
    234  * hand off to a database class for data access, then return
    235  * true or false on success or failure.
    236  */
    237 
    238 function bp_blogs_get_blogs( $args = '' ) {
    239     global $bp;
    240 
    241     $defaults = array(
    242         'type'         => 'active', // active, alphabetical, newest, or random
    243         'user_id'      => false,    // Pass a user_id to limit to only blogs that this user has privilages higher than subscriber on
    244         'search_terms' => false,    // Limit to blogs that match these search terms
    245         'per_page'     => 20,       // The number of results to return per page
    246         'page'         => 1,        // The page to return if limiting per page
    247     );
    248 
    249     $params = wp_parse_args( $args, $defaults );
    250     extract( $params, EXTR_SKIP );
    251 
    252     return apply_filters( 'bp_blogs_get_blogs', BP_Blogs_Blog::get( $type, $per_page, $page, $user_id, $search_terms ), $params );
    253 }
    254 
    255 function bp_blogs_record_existing_blogs() {
    256     global $bp, $wpdb;
    257 
    258     // Truncate user blogs table and re-record.
    259     $wpdb->query( "TRUNCATE TABLE {$bp->blogs->table_name}" );
    260 
    261     $blog_ids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0" ) );
    262 
    263     if ( $blog_ids ) {
    264         foreach( (array)$blog_ids as $blog_id ) {
    265             $users = get_users_of_blog( $blog_id );
    266 
    267             if ( $users ) {
    268                 foreach ( (array)$users as $user ) {
    269                     $role = unserialize( $user->meta_value );
    270 
    271                     if ( !isset( $role['subscriber'] ) )
    272                         bp_blogs_record_blog( $blog_id, $user->user_id, true );
    273                 }
    274             }
    275         }
    276     }
    277 }
    278 
    279 /**
    280  * Makes BuddyPress aware of a new site so that it can track its activity.
    281  *
    282  * @global object $bp BuddyPress global settings
    283  * @param int $blog_id
    284  * @param int $user_id
    285  * @param $bool $no_activity ; optional.
    286  * @since 1.0
    287  * @uses BP_Blogs_Blog
    288  */
    289 function bp_blogs_record_blog( $blog_id, $user_id, $no_activity = false ) {
    290     global $bp;
    291 
    292     if ( !$user_id )
    293         $user_id = $bp->loggedin_user->id;
    294 
    295     $name = get_blog_option( $blog_id, 'blogname' );
    296     $description = get_blog_option( $blog_id, 'blogdescription' );
    297 
    298     if ( empty( $name ) )
    299         return false;
    300 
    301     $recorded_blog = new BP_Blogs_Blog;
    302     $recorded_blog->user_id = $user_id;
    303     $recorded_blog->blog_id = $blog_id;
    304 
    305     $recorded_blog_id = $recorded_blog->save();
    306 
    307     $is_recorded = !empty( $recorded_blog_id ) ? true : false;
    308 
    309     bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'name', $name );
    310     bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'description', $description );
    311     bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'last_activity', bp_core_current_time() );
    312 
    313     $is_private = !empty( $_POST['blog_public'] ) && (int)$_POST['blog_public'] ? false : true;
    314 
    315     // Only record this activity if the blog is public
    316     if ( !$is_private && !$no_activity ) {
    317         // Record this in activity streams
    318         bp_blogs_record_activity( array(
    319             'user_id'      => $recorded_blog->user_id,
    320             'action'       => apply_filters( 'bp_blogs_activity_created_blog_action', sprintf( __( '%s created the blog %s', 'buddypress'), bp_core_get_userlink( $recorded_blog->user_id ), '<a href="' . get_site_url( $recorded_blog->blog_id ) . '">' . esc_attr( $name ) . '</a>' ), $recorded_blog, $name, $description ),
    321             'primary_link' => apply_filters( 'bp_blogs_activity_created_blog_primary_link', get_site_url( $recorded_blog->blog_id ), $recorded_blog->blog_id ),
    322             'type'         => 'new_blog',
    323             'item_id'      => $recorded_blog->blog_id
    324         ) );
    325     }
    326 
    327     do_action( 'bp_blogs_new_blog', &$recorded_blog, $is_private, $is_recorded );
    328 }
    329 add_action( 'wpmu_new_blog', 'bp_blogs_record_blog', 10, 2 );
    330 
    331 /**
    332  * Updates blogname in BuddyPress blogmeta table
    333  *
    334  * @global object $wpdb DB Layer
    335  * @param string $oldvalue Value before save (not used)
    336  * @param string $newvalue Value to change meta to
    337  */
    338 function bp_blogs_update_option_blogname( $oldvalue, $newvalue ) {
    339     global $wpdb;
    340 
    341     bp_blogs_update_blogmeta( $wpdb->blogid, 'name', $newvalue );
    342 }
    343 add_action( 'update_option_blogname', 'bp_blogs_update_option_blogname', 10, 2 );
    344 
    345 /**
    346  * Updates blogdescription in BuddyPress blogmeta table
    347  *
    348  * @global object $wpdb DB Layer
    349  * @param string $oldvalue Value before save (not used)
    350  * @param string $newvalue Value to change meta to
    351  */
    352 function bp_blogs_update_option_blogdescription( $oldvalue, $newvalue ) {
    353     global $wpdb;
    354 
    355     bp_blogs_update_blogmeta( $wpdb->blogid, 'description', $newvalue );
    356 }
    357 add_action( 'update_option_blogdescription', 'bp_blogs_update_option_blogdescription', 10, 2 );
    358 
    359 function bp_blogs_record_post( $post_id, $post, $user_id = false ) {
    360     global $bp, $wpdb;
    361 
    362     $post_id = (int)$post_id;
    363     $blog_id = (int)$wpdb->blogid;
    364 
    365     if ( !$user_id )
    366         $user_id = (int)$post->post_author;
    367 
    368     // This is to stop infinite loops with Donncha's sitewide tags plugin
    369     if ( !empty( $bp->site_options['tags_blog_id'] ) && (int)$blog_id == (int)$bp->site_options['tags_blog_id'] )
    370         return false;
    371 
    372     // Don't record this if it's not a post
    373     if ( 'post' != $post->post_type )
    374         return false;
    375 
    376     if ( 'publish' == $post->post_status && empty( $post->post_password ) ) {
    377         if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
    378             // Record this in activity streams
    379             $post_permalink   = get_permalink( $post_id );
    380 
    381             if ( is_multisite() ) {
    382                 $activity_action  = sprintf( __( '%1$s wrote a new blog post: %2$s on the blog %3$s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
    383             } else {
    384                 $activity_action  = sprintf( __( '%1$s wrote a new blog post: %2$s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
    385             }
    386 
    387             $activity_content = $post->post_content;
    388 
    389             bp_blogs_record_activity( array(
    390                 'user_id'           => (int)$post->post_author,
    391                 'action'            => apply_filters( 'bp_blogs_activity_new_post_action', $activity_action, &$post, $post_permalink ),
    392                 'content'           => apply_filters( 'bp_blogs_activity_new_post_content', $activity_content, &$post, $post_permalink ),
    393                 'primary_link'      => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink, $post_id ),
    394                 'type'              => 'new_blog_post',
    395                 'item_id'           => $blog_id,
    396                 'secondary_item_id' => $post_id,
    397                 'recorded_time'     => $post->post_date_gmt
    398             ));
    399         }
    400 
    401         // Update the blogs last activity
    402         bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    403     } else {
    404         bp_blogs_remove_post( $post_id, $blog_id, $user_id );
    405     }
    406 
    407     do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
    408 }
    409 add_action( 'save_post', 'bp_blogs_record_post', 10, 2 );
    410 
    411 /**
    412  * Record blog comment activity. Checks if blog is public and post is not
    413  * password protected.
    414  *
    415  * @global object $wpdb
    416  * @global $bp $bp
    417  * @param int $comment_id
    418  * @param bool $is_approved
    419  * @return mixed
    420  */
    421 function bp_blogs_record_comment( $comment_id, $is_approved = true ) {
    422     global $wpdb, $bp;
    423 
    424     // Get the users comment
    425     $recorded_comment = get_comment( $comment_id );
    426 
    427     // Don't record activity if the comment hasn't been approved
    428     if ( empty( $is_approved ) )
    429         return false;
    430 
    431     // Don't record activity if no email address has been included
    432     if ( empty( $recorded_comment->comment_author_email ) )
    433         return false;
    434 
    435     // Get the user_id from the comment author email.
    436     $user    = get_user_by_email( $recorded_comment->comment_author_email );
    437     $user_id = (int)$user->ID;
    438 
    439     // If there's no registered user id, don't record activity
    440     if ( empty( $user_id ) )
    441         return false;
    442 
    443     // Get blog and post data
    444     $blog_id                = (int)$wpdb->blogid;
    445     $recorded_comment->post = get_post( $recorded_comment->comment_post_ID );
    446 
    447     // If this is a password protected post, don't record the comment
    448     if ( !empty( $recorded_comment->post->post_password ) )
    449         return false;
    450 
    451     // If blog is public allow activity to be posted
    452     if ( get_blog_option( $blog_id, 'blog_public' ) ) {
    453 
    454         // Get activity related links
    455         $post_permalink = get_permalink( $recorded_comment->comment_post_ID );
    456         $comment_link   = htmlspecialchars( get_comment_link( $recorded_comment->comment_ID ) );
    457 
    458         // Prepare to record in activity streams
    459         if ( is_multisite() )
    460             $activity_action = sprintf( __( '%1$s commented on the blog post %2$s on the blog %3$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
    461         else
    462             $activity_action = sprintf( __( '%1$s commented on the blog post %2$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '</a>' );
    463 
    464         $activity_content   = $recorded_comment->comment_content;
    465 
    466         // Record in activity streams
    467         bp_blogs_record_activity( array(
    468             'user_id'           => $user_id,
    469             'action'            => apply_filters( 'bp_blogs_activity_new_comment_action', $activity_action, &$recorded_comment, $comment_link ),
    470             'content'           => apply_filters( 'bp_blogs_activity_new_comment_content', $activity_content, &$recorded_comment, $comment_link ),
    471             'primary_link'      => apply_filters( 'bp_blogs_activity_new_comment_primary_link', $comment_link, &$recorded_comment ),
    472             'type'              => 'new_blog_comment',
    473             'item_id'           => $blog_id,
    474             'secondary_item_id' => $comment_id,
    475             'recorded_time'     => $recorded_comment->comment_date_gmt
    476         ) );
    477 
    478         // Update the blogs last active date
    479         bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    480     }
    481 
    482     return $recorded_comment;
    483 }
    484 add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
    485 add_action( 'edit_comment', 'bp_blogs_record_comment', 10    );
    486 
    487 function bp_blogs_manage_comment( $comment_id, $comment_status ) {
    488     if ( 'spam' == $comment_status || 'hold' == $comment_status || 'delete' == $comment_status || 'trash' == $comment_status )
    489         return bp_blogs_remove_comment( $comment_id );
    490 
    491     return bp_blogs_record_comment( $comment_id, true );
    492 }
    493 add_action( 'wp_set_comment_status', 'bp_blogs_manage_comment', 10, 2 );
    494 
    495 function bp_blogs_add_user_to_blog( $user_id, $role = false, $blog_id = false ) {
    496     global $wpdb, $current_blog;
    497 
    498     if ( empty( $blog_id ) )
    499         $blog_id = $current_blog->blog_id;
    500 
    501     if ( empty( $role ) ) {
    502         $key = $wpdb->get_blog_prefix( $blog_id ). 'capabilities';
    503 
    504         $roles = get_user_meta( $user_id, $key, true );
    505 
    506         if ( is_array( $roles ) )
    507             $role = array_search( 1, $roles );
    508         else
    509             return false;
    510     }
    511 
    512     if ( $role != 'subscriber' )
    513         bp_blogs_record_blog( $blog_id, $user_id, true );
    514 }
    515 add_action( 'add_user_to_blog', 'bp_blogs_add_user_to_blog', 10, 3 );
    516 add_action( 'profile_update',   'bp_blogs_add_user_to_blog'        );
    517 add_action( 'user_register',    'bp_blogs_add_user_to_blog'        );
    518 
    519 function bp_blogs_remove_user_from_blog( $user_id, $blog_id = false ) {
    520     global $current_blog;
    521 
    522     if ( empty( $blog_id ) )
    523         $blog_id = $current_blog->blog_id;
    524 
    525     bp_blogs_remove_blog_for_user( $user_id, $blog_id );
    526 }
    527 add_action( 'remove_user_from_blog', 'bp_blogs_remove_user_from_blog', 10, 2 );
    528 
    529 function bp_blogs_remove_blog( $blog_id ) {
    530     global $bp;
    531 
    532     $blog_id = (int)$blog_id;
    533 
    534     BP_Blogs_Blog::delete_blog_for_all( $blog_id );
    535 
    536     // Delete activity stream item
    537     bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component' => $bp->blogs->slug, 'type' => 'new_blog' ) );
    538 
    539     do_action( 'bp_blogs_remove_blog', $blog_id );
    540 }
    541 add_action( 'delete_blog', 'bp_blogs_remove_blog' );
    542 
    543 function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) {
    544     global $current_user;
    545 
    546     $blog_id = (int)$blog_id;
    547     $user_id = (int)$user_id;
    548 
    549     BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id );
    550 
    551     // Delete activity stream item
    552     bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component' => $bp->blogs->slug, 'type' => 'new_blog' ) );
    553 
    554     do_action( 'bp_blogs_remove_blog_for_user', $blog_id, $user_id );
    555 }
    556 add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 );
    557 
    558 function bp_blogs_remove_post( $post_id, $blog_id = false, $user_id = false ) {
    559     global $current_blog, $bp;
    560 
    561     if ( empty( $current_blog->blog_id ) )
    562         return false;
    563 
    564     $post_id = (int)$post_id;
    565 
    566     if ( !$blog_id )
    567         $blog_id = (int)$current_blog->blog_id;
    568 
    569     if ( !$user_id )
    570         $user_id = $bp->loggedin_user->id;
    571 
    572     // Delete activity stream item
    573     bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'secondary_item_id' => $post_id, 'component' => $bp->blogs->slug, 'type' => 'new_blog_post' ) );
    574 
    575     do_action( 'bp_blogs_remove_post', $blog_id, $post_id, $user_id );
    576 }
    577 add_action( 'delete_post', 'bp_blogs_remove_post' );
    578 
    579 function bp_blogs_remove_comment( $comment_id ) {
    580     global $wpdb, $bp;
    581 
    582     // Delete activity stream item
    583     bp_blogs_delete_activity( array( 'item_id' => $wpdb->blogid , 'secondary_item_id' => $comment_id, 'type' => 'new_blog_comment' ) );
    584 
    585     do_action( 'bp_blogs_remove_comment', $blog_id, $comment_id, $bp->loggedin_user->id );
    586 }
    587 add_action( 'delete_comment', 'bp_blogs_remove_comment' );
    588 
    589 function bp_blogs_total_blogs() {
    590     if ( !$count = wp_cache_get( 'bp_total_blogs', 'bp' ) ) {
    591         $blogs = BP_Blogs_Blog::get_all();
    592         $count = $blogs['total'];
    593         wp_cache_set( 'bp_total_blogs', $count, 'bp' );
    594     }
    595     return $count;
    596 }
    597 
    598 function bp_blogs_total_blogs_for_user( $user_id = false ) {
    599     global $bp;
    600 
    601     if ( !$user_id )
    602         $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
    603 
    604     if ( !$count = wp_cache_get( 'bp_total_blogs_for_user_' . $user_id, 'bp' ) ) {
    605         $count = BP_Blogs_Blog::total_blog_count_for_user( $user_id );
    606         wp_cache_set( 'bp_total_blogs_for_user_' . $user_id, $count, 'bp' );
    607     }
    608 
    609     return $count;
    610 }
    611 
    612 function bp_blogs_remove_data_for_blog( $blog_id ) {
    613     global $bp;
    614 
    615     // If this is regular blog, delete all data for that blog.
    616     BP_Blogs_Blog::delete_blog_for_all( $blog_id );
    617 
    618     // Delete activity stream item
    619     bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component' => $bp->blogs->slug, 'type' => false ) );
    620 
    621     do_action( 'bp_blogs_remove_data_for_blog', $blog_id );
    622 }
    623 add_action( 'delete_blog', 'bp_blogs_remove_data_for_blog', 1 );
    624 
    625 function bp_blogs_get_blogs_for_user( $user_id, $show_hidden = false ) {
    626     return BP_Blogs_Blog::get_blogs_for_user( $user_id, $show_hidden );
    627 }
    628 
    629 function bp_blogs_get_all_blogs( $limit = null, $page = null ) {
    630     return BP_Blogs_Blog::get_all( $limit, $page );
    631 }
    632 
    633 function bp_blogs_get_random_blogs( $limit = null, $page = null ) {
    634     return BP_Blogs_Blog::get( 'random', $limit, $page );
    635 }
    636 
    637 function bp_blogs_is_blog_hidden( $blog_id ) {
    638     return BP_Blogs_Blog::is_hidden( $blog_id );
    639 }
    640 
    641 function bp_blogs_redirect_to_random_blog() {
    642     global $bp, $wpdb;
    643 
    644     if ( $bp->current_component == $bp->blogs->slug && isset( $_GET['random-blog'] ) ) {
    645         $blog = bp_blogs_get_random_blogs( 1, 1 );
    646 
    647         bp_core_redirect( get_site_url( $blog['blogs'][0]->blog_id ) );
    648     }
    649 }
    650 add_action( 'wp', 'bp_blogs_redirect_to_random_blog', 6 );
    651 
    652 
    653 /*******************************************************************************
    654  * Blog meta functions
    655  *
    656  * These functions are used to store specific blogmeta in one global table,
    657  * rather than in each blog's options table. Significantly speeds up global blog
    658  * queries. By default each blog's name, description and last updated time are
    659  * stored and synced here.
    660  */
    661 
    662 function bp_blogs_delete_blogmeta( $blog_id, $meta_key = false, $meta_value = false ) {
    663     global $wpdb, $bp;
    664 
    665     if ( !is_numeric( $blog_id ) || !is_multisite() )
    666         return false;
    667 
    668     $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    669 
    670     if ( is_array($meta_value) || is_object($meta_value) )
    671         $meta_value = serialize($meta_value);
    672 
    673     $meta_value = trim( $meta_value );
    674 
    675     if ( !$meta_key )
    676         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d", $blog_id ) );
    677     else if ( $meta_value )
    678         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s AND meta_value = %s", $blog_id, $meta_key, $meta_value ) );
    679     else
    680         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key ) );
    681 
    682     wp_cache_delete( 'bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, 'bp' );
    683 
    684     return true;
    685 }
    686 
    687 function bp_blogs_get_blogmeta( $blog_id, $meta_key = '') {
    688     global $wpdb, $bp;
    689 
    690     $blog_id = (int) $blog_id;
    691 
    692     if ( !$blog_id || !is_multisite() )
    693         return false;
    694 
    695     if ( !empty($meta_key) ) {
    696         $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    697 
    698         if ( !$metas = wp_cache_get( 'bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, 'bp' ) ) {
    699             $metas = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key ) );
    700             wp_cache_set( 'bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, $metas, 'bp' );
    701         }
    702     } else {
    703         $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d", $blog_id) );
    704     }
    705 
    706     if ( empty($metas) ) {
    707         if ( empty($meta_key) )
    708             return array();
    709         else
    710             return '';
    711     }
    712 
    713     $metas = array_map('maybe_unserialize', (array)$metas);
    714 
    715     if ( 1 == count($metas) )
    716         return $metas[0];
    717     else
    718         return $metas;
    719 }
    720 
    721 function bp_blogs_update_blogmeta( $blog_id, $meta_key, $meta_value ) {
    722     global $wpdb, $bp;
    723 
    724     if ( !is_numeric( $blog_id ) || !is_multisite() )
    725         return false;
    726 
    727     $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    728 
    729     if ( is_string($meta_value) )
    730         $meta_value = stripslashes($wpdb->escape($meta_value));
    731 
    732     $meta_value = maybe_serialize($meta_value);
    733 
    734     if (empty( $meta_value ) )
    735         return bp_blogs_delete_blogmeta( $blog_id, $meta_key );
    736 
    737     $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key ) );
    738 
    739     if ( !$cur )
    740         $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name_blogmeta} ( blog_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $blog_id, $meta_key, $meta_value ) );
    741     else if ( $cur->meta_value != $meta_value )
    742         $wpdb->query( $wpdb->prepare( "UPDATE {$bp->blogs->table_name_blogmeta} SET meta_value = %s WHERE blog_id = %d AND meta_key = %s", $meta_value, $blog_id, $meta_key ) );
    743     else
    744         return false;
    745 
    746     wp_cache_set( 'bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, $meta_value, 'bp' );
    747 
    748     return true;
    749 }
    750 
    751 function bp_blogs_remove_data( $user_id ) {
    752     if ( !is_multisite() )
    753         return false;
    754 
    755     // If this is regular blog, delete all data for that blog.
    756     BP_Blogs_Blog::delete_blogs_for_user( $user_id );
    757 
    758     do_action( 'bp_blogs_remove_data', $user_id );
    759 }
    760 add_action( 'wpmu_delete_user',  'bp_blogs_remove_data' );
    761 add_action( 'delete_user',       'bp_blogs_remove_data' );
    762 add_action( 'bp_make_spam_user', 'bp_blogs_remove_data' );
    763 
    764 
    765 /*******************************************************************************
    766  * Caching
    767  *
    768  * Caching functions handle the clearing of cached objects and pages on specific
    769  * actions throughout BuddyPress.
    770  */
    771 
    772 function bp_blogs_clear_blog_object_cache( $blog_id, $user_id ) {
    773     wp_cache_delete( 'bp_blogs_of_user_' . $user_id, 'bp' );
    774     wp_cache_delete( 'bp_total_blogs_for_user_' . $user_id, 'bp' );
    775 }
    776 
    777 function bp_blogs_format_clear_blog_cache( $recorded_blog_obj ) {
    778     bp_blogs_clear_blog_object_cache( false, $recorded_blog_obj->user_id );
    779     wp_cache_delete( 'bp_total_blogs', 'bp' );
    780 }
    781 
    782 // List actions to clear object caches on
    783 add_action( 'bp_blogs_remove_blog_for_user', 'bp_blogs_clear_blog_object_cache', 10, 2 );
    784 add_action( 'bp_blogs_new_blog',             'bp_blogs_format_clear_blog_cache', 10, 2 );
    785 
    786 // List actions to clear super cached pages on, if super cache is installed
    787 add_action( 'bp_blogs_remove_data_for_blog', 'bp_core_clear_cache' );
    788 add_action( 'bp_blogs_remove_comment',       'bp_core_clear_cache' );
    789 add_action( 'bp_blogs_remove_post',          'bp_core_clear_cache' );
    790 add_action( 'bp_blogs_remove_blog_for_user', 'bp_core_clear_cache' );
    791 add_action( 'bp_blogs_remove_blog',          'bp_core_clear_cache' );
    792 add_action( 'bp_blogs_new_blog_comment',     'bp_core_clear_cache' );
    793 add_action( 'bp_blogs_new_blog_post',        'bp_core_clear_cache' );
    794 add_action( 'bp_blogs_new_blog',             'bp_core_clear_cache' );
    795 add_action( 'bp_blogs_remove_data',          'bp_core_clear_cache' );
    796 
    797268?>
Note: See TracChangeset for help on using the changeset viewer.