Example #1
0
/**
 * Check whether the current post can be published.
 *
 * Abstracted from the deprecated `bp_blogs_record_post()`.
 *
 * @since BuddyPress (2.2.0)
 *
 * @param  bool $return  Whether the post should be published.
 * @param  int  $blog_id ID of the blog.
 * @param  int  $post_id ID of the post.
 * @param  int  $user_id ID of the post author.
 * @return bool True to authorize the post to be published, otherwise false.
 */
function bp_blogs_post_pre_publish($return = true, $blog_id = 0, $post_id = 0, $user_id = 0)
{
    $bp = buddypress();
    // If blog is not trackable, do not record the activity.
    if (!bp_blogs_is_blog_trackable($blog_id, $user_id)) {
        return false;
    }
    /*
     * Stop infinite loops with WordPress MU Sitewide Tags.
     * That plugin changed the way its settings were stored at some point. Thus the dual check.
     */
    $sitewide_tags_blog_settings = bp_core_get_root_option('sitewide_tags_blog');
    if (!empty($sitewide_tags_blog_settings)) {
        $st_options = maybe_unserialize($sitewide_tags_blog_settings);
        $tags_blog_id = isset($st_options['tags_blog_id']) ? $st_options['tags_blog_id'] : 0;
    } else {
        $tags_blog_id = bp_core_get_root_option('sitewide_tags_blog');
        $tags_blog_id = intval($tags_blog_id);
    }
    /**
     * Filters whether or not BuddyPress should block sitewide tags activity.
     *
     * @since BuddyPress (2.2.0)
     *
     * @param bool $value Current status of the sitewide tags activity.
     */
    if ((int) $blog_id == $tags_blog_id && apply_filters('bp_blogs_block_sitewide_tags_activity', true)) {
        return false;
    }
    /**
     * Filters whether or not the current blog is public.
     *
     * @since BuddyPress (2.2.0)
     *
     * @param int $value Value from the blog_public option for the current blog.
     */
    $is_blog_public = apply_filters('bp_is_blog_public', (int) get_blog_option($blog_id, 'blog_public'));
    if (0 === $is_blog_public && is_multisite()) {
        return false;
    }
    return $return;
}
/**
 * Record blog comment activity. Checks if blog is public and post is not
 * password protected.
 *
 * @param int $comment_id
 * @param mixed $is_approved
 * @return mixed
 */
function bp_blogs_record_comment($comment_id, $is_approved = true)
{
    // Get the users comment
    $recorded_comment = get_comment($comment_id);
    // Don't record activity if the comment hasn't been approved
    if (empty($is_approved)) {
        return false;
    }
    // Don't record activity if no email address has been included
    if (empty($recorded_comment->comment_author_email)) {
        return false;
    }
    // Don't record activity if the comment has already been marked as spam
    if ('spam' === $is_approved) {
        return false;
    }
    // Get the user by the comment author email.
    $user = get_user_by('email', $recorded_comment->comment_author_email);
    // If user isn't registered, don't record activity
    if (empty($user)) {
        return false;
    }
    // Get the user_id
    $user_id = (int) $user->ID;
    // Get blog and post data
    $blog_id = get_current_blog_id();
    // If blog is not trackable, do not record the activity.
    if (!bp_blogs_is_blog_trackable($blog_id, $user_id)) {
        return false;
    }
    $recorded_comment->post = get_post($recorded_comment->comment_post_ID);
    if (empty($recorded_comment->post) || is_wp_error($recorded_comment->post)) {
        return false;
    }
    // If this is a password protected post, don't record the comment
    if (!empty($recorded_comment->post->post_password)) {
        return false;
    }
    // Don't record activity if the comment's associated post isn't a WordPress Post
    if (!in_array($recorded_comment->post->post_type, apply_filters('bp_blogs_record_comment_post_types', array('post')))) {
        return false;
    }
    $is_blog_public = apply_filters('bp_is_blog_public', (int) get_blog_option($blog_id, 'blog_public'));
    // If blog is public allow activity to be posted
    if ($is_blog_public) {
        // Get activity related links
        $post_permalink = get_permalink($recorded_comment->comment_post_ID);
        $comment_link = htmlspecialchars(get_comment_link($recorded_comment->comment_ID));
        // Prepare to record in activity streams
        if (is_multisite()) {
            $activity_action = sprintf(__('%1$s commented on the post, %2$s, on the site %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>');
        } else {
            $activity_action = sprintf(__('%1$s commented on the post, %2$s', 'buddypress'), bp_core_get_userlink($user_id), '<a href="' . $post_permalink . '">' . apply_filters('the_title', $recorded_comment->post->post_title) . '</a>');
        }
        $activity_content = $recorded_comment->comment_content;
        // Record in activity streams
        bp_blogs_record_activity(array('user_id' => $user_id, 'action' => apply_filters_ref_array('bp_blogs_activity_new_comment_action', array($activity_action, &$recorded_comment, $comment_link)), 'content' => apply_filters_ref_array('bp_blogs_activity_new_comment_content', array($activity_content, &$recorded_comment, $comment_link)), 'primary_link' => apply_filters_ref_array('bp_blogs_activity_new_comment_primary_link', array($comment_link, &$recorded_comment)), 'type' => 'new_blog_comment', 'item_id' => $blog_id, 'secondary_item_id' => $comment_id, 'recorded_time' => $recorded_comment->comment_date_gmt));
        // Update the blogs last active date
        bp_blogs_update_blogmeta($blog_id, 'last_activity', bp_core_current_time());
    }
    return $recorded_comment;
}
/**
 * Add an activity entry for a newly-created site.
 *
 * Hooked to the 'bp_blogs_new_blog' action.
 *
 * @since 2.6.0
 *
 * @param BP_Blogs_Blog $recorded_blog Current site being recorded. Passed by reference.
 * @param bool          $is_private    Whether the current site being recorded is private.
 * @param bool          $is_recorded   Whether the current site was recorded.
 */
function bp_blogs_record_activity_on_site_creation($recorded_blog, $is_private, $is_recorded, $no_activity)
{
    // Only record this activity if the blog is public.
    if (!$is_private && !$no_activity && bp_blogs_is_blog_trackable($recorded_blog->blog_id, $recorded_blog->user_id)) {
        bp_blogs_record_activity(array('user_id' => $recorded_blog->user_id, 'primary_link' => apply_filters('bp_blogs_activity_created_blog_primary_link', bp_blogs_get_blogmeta($recorded_blog->blog_id, 'url'), $recorded_blog->blog_id), 'type' => 'new_blog', 'item_id' => $recorded_blog->blog_id));
    }
}
/**
 * Record a new blog comment in the BuddyPress activity stream.
 *
 * Only posts the item if blog is public and post is not password-protected.
 *
 * @param int $comment_id ID of the comment being recorded.
 * @param bool|string $is_approved Optional. The $is_approved value passed to
 *        the 'comment_post' action. Default: true.
 * @return bool|object Returns false on failure, the comment object on success.
 */
function bp_blogs_record_comment($comment_id, $is_approved = true)
{
    // bail if activity component is not active
    if (!bp_is_active('activity')) {
        return;
    }
    // Get the users comment
    $recorded_comment = get_comment($comment_id);
    // Don't record activity if the comment hasn't been approved
    if (empty($is_approved)) {
        return false;
    }
    // Don't record activity if no email address has been included
    if (empty($recorded_comment->comment_author_email)) {
        return false;
    }
    // Don't record activity if the comment has already been marked as spam
    if ('spam' === $is_approved) {
        return false;
    }
    // Get the user by the comment author email.
    $user = get_user_by('email', $recorded_comment->comment_author_email);
    // If user isn't registered, don't record activity
    if (empty($user)) {
        return false;
    }
    // Get the user_id
    $user_id = (int) $user->ID;
    // Get blog and post data
    $blog_id = get_current_blog_id();
    // If blog is not trackable, do not record the activity.
    if (!bp_blogs_is_blog_trackable($blog_id, $user_id)) {
        return false;
    }
    $recorded_comment->post = get_post($recorded_comment->comment_post_ID);
    if (empty($recorded_comment->post) || is_wp_error($recorded_comment->post)) {
        return false;
    }
    // If this is a password protected post, don't record the comment
    if (!empty($recorded_comment->post->post_password)) {
        return false;
    }
    // Don't record activity if the comment's associated post isn't a WordPress Post
    if (!in_array($recorded_comment->post->post_type, apply_filters('bp_blogs_record_comment_post_types', array('post')))) {
        return false;
    }
    $is_blog_public = apply_filters('bp_is_blog_public', (int) get_blog_option($blog_id, 'blog_public'));
    // If blog is public allow activity to be posted
    if ($is_blog_public) {
        // Get activity related links
        $post_permalink = get_permalink($recorded_comment->comment_post_ID);
        $comment_link = get_comment_link($recorded_comment->comment_ID);
        // Setup activity args
        $args = array();
        $args['user_id'] = $user_id;
        $args['content'] = apply_filters_ref_array('bp_blogs_activity_new_comment_content', array($recorded_comment->comment_content, &$recorded_comment, $comment_link));
        $args['primary_link'] = apply_filters_ref_array('bp_blogs_activity_new_comment_primary_link', array($comment_link, &$recorded_comment));
        $args['recorded_time'] = $recorded_comment->comment_date_gmt;
        // Setup some different activity args depending if activity commenting is
        // enabled or not
        // if cannot comment, record separate activity entry
        // this is the old way of doing things
        if (bp_disable_blogforum_comments()) {
            $args['type'] = 'new_blog_comment';
            $args['item_id'] = $blog_id;
            $args['secondary_item_id'] = $comment_id;
            // record the activity entry
            $activity_id = bp_blogs_record_activity($args);
            // add some post info in activity meta
            bp_activity_update_meta($activity_id, 'post_title', $recorded_comment->post->post_title);
            bp_activity_update_meta($activity_id, 'post_url', add_query_arg('p', $recorded_comment->post->ID, home_url('/')));
            // record comment as BP activity comment under the parent 'new_blog_post'
            // activity item
        } else {
            // this is a comment edit
            // check to see if corresponding activity entry already exists
            if (!empty($_REQUEST['action'])) {
                $existing_activity_id = get_comment_meta($comment_id, 'bp_activity_comment_id', true);
                if (!empty($existing_activity_id)) {
                    $args['id'] = $existing_activity_id;
                }
            }
            // find the parent 'new_blog_post' activity entry
            $parent_activity_id = bp_activity_get_activity_id(array('component' => 'blogs', 'type' => 'new_blog_post', 'item_id' => $blog_id, 'secondary_item_id' => $recorded_comment->comment_post_ID));
            // we found the parent activity entry
            // so let's go ahead and reconfigure some activity args
            if (!empty($parent_activity_id)) {
                // set the 'item_id' with the parent activity entry ID
                $args['item_id'] = $parent_activity_id;
                // now see if the WP parent comment has a BP activity ID
                $comment_parent = 0;
                if (!empty($recorded_comment->comment_parent)) {
                    $comment_parent = get_comment_meta($recorded_comment->comment_parent, 'bp_activity_comment_id', true);
                }
                // WP parent comment does not have a BP activity ID
                // so set to 'new_blog_post' activity ID
                if (empty($comment_parent)) {
                    $comment_parent = $parent_activity_id;
                }
                $args['secondary_item_id'] = $comment_parent;
                $args['component'] = 'activity';
                $args['type'] = 'activity_comment';
                // could not find corresponding parent activity entry
                // so wipe out $args array
            } else {
                $args = array();
            }
            // Record in activity streams
            if (!empty($args)) {
                // @todo should we use bp_activity_new_comment()? that function will also send
                // an email to people in the activity comment thread
                //
                // what if a site already has some comment email notification plugin setup?
                // this is why I decided to go with bp_activity_add() to avoid any conflict
                // with existing comment email notification plugins
                $comment_activity_id = bp_activity_add($args);
                if (empty($args['id'])) {
                    // add meta to activity comment
                    bp_activity_update_meta($comment_activity_id, 'bp_blogs_post_comment_id', $comment_id);
                    bp_activity_update_meta($comment_activity_id, 'post_title', $recorded_comment->post->post_title);
                    bp_activity_update_meta($comment_activity_id, 'post_url', add_query_arg('p', $recorded_comment->post->ID, home_url('/')));
                    // add meta to comment
                    add_comment_meta($comment_id, 'bp_activity_comment_id', $comment_activity_id);
                }
            }
        }
        // Update the blogs last active date
        bp_blogs_update_blogmeta($blog_id, 'last_activity', bp_core_current_time());
    }
    return $recorded_comment;
}
/**
 * Make BuddyPress aware of a new site so that it can track its activity.
 *
 * @since 1.0.0
 *
 * @uses BP_Blogs_Blog
 *
 * @param int  $blog_id     ID of the blog being recorded.
 * @param int  $user_id     ID of the user for whom the blog is being recorded.
 * @param bool $no_activity Optional. Whether to skip recording an activity
 *                          item about this blog creation. Default: false.
 * @return bool|null Returns false on failure.
 */
function bp_blogs_record_blog($blog_id, $user_id, $no_activity = false)
{
    if (empty($user_id)) {
        $user_id = bp_loggedin_user_id();
    }
    // If blog is not recordable, do not record the activity.
    if (!bp_blogs_is_blog_recordable($blog_id, $user_id)) {
        return false;
    }
    $name = get_blog_option($blog_id, 'blogname');
    $url = get_home_url($blog_id);
    if (empty($name)) {
        $name = $url;
    }
    $description = get_blog_option($blog_id, 'blogdescription');
    $close_old_posts = get_blog_option($blog_id, 'close_comments_for_old_posts');
    $close_days_old = get_blog_option($blog_id, 'close_comments_days_old');
    $thread_depth = get_blog_option($blog_id, 'thread_comments');
    if (!empty($thread_depth)) {
        $thread_depth = get_blog_option($blog_id, 'thread_comments_depth');
    } else {
        // Perhaps filter this?
        $thread_depth = 1;
    }
    $recorded_blog = new BP_Blogs_Blog();
    $recorded_blog->user_id = $user_id;
    $recorded_blog->blog_id = $blog_id;
    $recorded_blog_id = $recorded_blog->save();
    $is_recorded = !empty($recorded_blog_id) ? true : false;
    bp_blogs_update_blogmeta($recorded_blog->blog_id, 'url', $url);
    bp_blogs_update_blogmeta($recorded_blog->blog_id, 'name', $name);
    bp_blogs_update_blogmeta($recorded_blog->blog_id, 'description', $description);
    bp_blogs_update_blogmeta($recorded_blog->blog_id, 'last_activity', bp_core_current_time());
    bp_blogs_update_blogmeta($recorded_blog->blog_id, 'close_comments_for_old_posts', $close_old_posts);
    bp_blogs_update_blogmeta($recorded_blog->blog_id, 'close_comments_days_old', $close_days_old);
    bp_blogs_update_blogmeta($recorded_blog->blog_id, 'thread_comments_depth', $thread_depth);
    $is_private = !empty($_POST['blog_public']) && (int) $_POST['blog_public'] ? false : true;
    $is_private = !apply_filters('bp_is_new_blog_public', !$is_private);
    // Only record this activity if the activity component is active and the blog is public.
    if (bp_is_active('activity') && !$is_private && !$no_activity && bp_blogs_is_blog_trackable($blog_id, $user_id)) {
        // Record this in activity streams.
        bp_blogs_record_activity(array('user_id' => $recorded_blog->user_id, 'primary_link' => apply_filters('bp_blogs_activity_created_blog_primary_link', $url, $recorded_blog->blog_id), 'type' => 'new_blog', 'item_id' => $recorded_blog->blog_id));
    }
    /**
     * Fires after BuddyPress has been made aware of a new site for activity tracking.
     *
     * @since 1.0.0
     *
     * @param BP_Blogs_Blog $recorded_blog Current blog being recorded. Passed by reference.
     * @param bool          $is_private    Whether or not the current blog being recorded is private.
     * @param bool          $is_recorded   Whether or not the current blog was recorded.
     */
    do_action_ref_array('bp_blogs_new_blog', array(&$recorded_blog, $is_private, $is_recorded));
}