/**
 * Create an activity item for a newly posted post type comment.
 *
 * @since 2.5.0
 *
 * @param  int  $comment_id  ID of the comment.
 * @param  bool $is_approved Whether the comment is approved or not.
 * @param  object $activity_post_object the post type tracking args object.
 *
 * @return int|bool The ID of the activity on success. False on error.
 */
function bp_activity_post_type_comment($comment_id = 0, $is_approved = true, $activity_post_object = null)
{
    // Get the users comment
    $post_type_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($post_type_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', $post_type_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();
    // Get the post
    $post_type_comment->post = get_post($post_type_comment->comment_post_ID);
    if (!is_a($post_type_comment->post, 'WP_Post')) {
        return false;
    }
    /**
     * Filters whether to publish activities about the comment regarding the post status
     *
     * @since 2.5.0
     *
     * @param bool true to bail, false otherwise.
     */
    $is_post_status_not_allowed = (bool) apply_filters('bp_activity_post_type_is_post_status_allowed', 'publish' !== $post_type_comment->post->post_status || !empty($post_type_comment->post->post_password));
    // If this is a password protected post, or not a public post don't record the comment
    if ($is_post_status_not_allowed) {
        return false;
    }
    // Set post type
    $post_type = $post_type_comment->post->post_type;
    if (empty($activity_post_object)) {
        // Get the post type tracking args.
        $activity_post_object = bp_activity_get_post_type_tracking_args($post_type);
        // Bail if the activity type does not exist
        if (empty($activity_post_object->comments_tracking->action_id)) {
            return false;
        }
    }
    // Set the $activity_comment_object
    $activity_comment_object = $activity_post_object->comments_tracking;
    /**
     * Filters whether or not to post the activity about the comment.
     *
     * This is a variable filter, dependent on the post type,
     * that lets components or plugins bail early if needed.
     *
     * @since 2.5.0
     *
     * @param bool $value      Whether or not to continue.
     * @param int  $blog_id    ID of the current site.
     * @param int  $post_id    ID of the current post being commented.
     * @param int  $user_id    ID of the current user.
     * @param int  $comment_id ID of the current comment being posted.
     */
    if (false === apply_filters("bp_activity_{$post_type}_pre_comment", true, $blog_id, $post_type_comment->post->ID, $user_id, $comment_id)) {
        return false;
    }
    // Is this an update ?
    $activity_id = bp_activity_get_activity_id(array('user_id' => $user_id, 'component' => $activity_comment_object->component_id, 'type' => $activity_comment_object->action_id, 'item_id' => $blog_id, 'secondary_item_id' => $comment_id));
    // Record this in activity streams.
    $comment_link = get_comment_link($post_type_comment->comment_ID);
    // Backward compatibility filters for the 'blogs' component.
    if ('blogs' == $activity_comment_object->component_id) {
        $activity_content = apply_filters_ref_array('bp_blogs_activity_new_comment_content', array($post_type_comment->comment_content, &$post_type_comment, $comment_link));
        $activity_primary_link = apply_filters_ref_array('bp_blogs_activity_new_comment_primary_link', array($comment_link, &$post_type_comment));
    } else {
        $activity_content = $post_type_comment->comment_content;
        $activity_primary_link = $comment_link;
    }
    $activity_args = array('id' => $activity_id, 'user_id' => $user_id, 'content' => $activity_content, 'primary_link' => $activity_primary_link, 'component' => $activity_comment_object->component_id, 'recorded_time' => $post_type_comment->comment_date_gmt);
    if (bp_disable_blogforum_comments()) {
        $blog_url = get_home_url($blog_id);
        $post_url = add_query_arg('p', $post_type_comment->post->ID, trailingslashit($blog_url));
        $activity_args['type'] = $activity_comment_object->action_id;
        $activity_args['item_id'] = $blog_id;
        $activity_args['secondary_item_id'] = $post_type_comment->comment_ID;
        if (!empty($activity_args['content'])) {
            // Create the excerpt.
            $activity_summary = bp_activity_create_summary($activity_args['content'], $activity_args);
            // Backward compatibility filter for blog comments.
            if ('blogs' == $activity_post_object->component_id) {
                $activity_args['content'] = apply_filters('bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post_type);
            } else {
                $activity_args['content'] = $activity_summary;
            }
        }
        // Set up the action by using the format functions.
        $action_args = array_merge($activity_args, array('post_title' => $post_type_comment->post->post_title, 'post_url' => $post_url, 'blog_url' => $blog_url, 'blog_name' => get_blog_option($blog_id, 'blogname')));
        $activity_args['action'] = call_user_func_array($activity_comment_object->format_callback, array('', (object) $action_args));
        // Make sure the action is set.
        if (empty($activity_args['action'])) {
            return;
        } else {
            // Backward compatibility filter for the blogs component.
            if ('blogs' === $activity_post_object->component_id) {
                $activity_args['action'] = apply_filters('bp_blogs_record_activity_action', $activity_args['action']);
            }
        }
        $activity_id = bp_activity_add($activity_args);
    }
    /**
     * Fires after the publishing of an activity item for a newly published post type post.
     *
     * @since 2.5.0
     *
     * @param int        $activity_id          ID of the newly published activity item.
     * @param WP_Comment $post_type_comment    Comment object.
     * @param array      $activity_args        Array of activity arguments.
     * @param object     $activity_post_object the post type tracking args object.
     */
    do_action_ref_array('bp_activity_post_type_comment', array(&$activity_id, $post_type_comment, $activity_args, $activity_post_object));
    return $activity_id;
}
/**
 * Record blog-related activity to the activity stream.
 *
 * @since 1.0.0
 *
 * @see bp_activity_add() for description of parameters.
 *
 * @param array|string $args {
 *     See {@link bp_activity_add()} for complete description of arguments.
 *     The arguments listed here have different default values from
 *     bp_activity_add().
 *     @type string $component Default: 'blogs'.
 * }
 * @return int|bool On success, returns the activity ID. False on failure.
 */
function bp_blogs_record_activity($args = '')
{
    $defaults = array('user_id' => bp_loggedin_user_id(), 'action' => '', 'content' => '', 'primary_link' => '', 'component' => buddypress()->blogs->id, 'type' => false, 'item_id' => false, 'secondary_item_id' => false, 'recorded_time' => bp_core_current_time(), 'hide_sitewide' => false);
    $r = wp_parse_args($args, $defaults);
    if (!empty($r['action'])) {
        /**
         * Filters the action associated with activity for activity stream.
         *
         * @since 1.2.0
         *
         * @param string $value Action for the activity stream.
         */
        $r['action'] = apply_filters('bp_blogs_record_activity_action', $r['action']);
    }
    if (!empty($r['content'])) {
        /**
         * Filters the content associated with activity for activity stream.
         *
         * @since 1.2.0
         *
         * @param string $value Generated summary from content for the activity stream.
         * @param string $value Content for the activity stream.
         * @param array  $r     Array of arguments used for the activity stream item.
         */
        $r['content'] = apply_filters('bp_blogs_record_activity_content', bp_activity_create_summary($r['content'], $r), $r['content'], $r);
    }
    // Check for an existing entry and update if one exists.
    $id = bp_activity_get_activity_id(array('user_id' => $r['user_id'], 'component' => $r['component'], 'type' => $r['type'], 'item_id' => $r['item_id'], 'secondary_item_id' => $r['secondary_item_id']));
    return bp_activity_add(array('id' => $id, 'user_id' => $r['user_id'], 'action' => $r['action'], 'content' => $r['content'], 'primary_link' => $r['primary_link'], 'component' => $r['component'], 'type' => $r['type'], 'item_id' => $r['item_id'], 'secondary_item_id' => $r['secondary_item_id'], 'recorded_time' => $r['recorded_time'], 'hide_sitewide' => $r['hide_sitewide']));
}
/**
 * Update the activity item for a custom post type entry.
 *
 * @since 2.2.0
 *
 * @param WP_Post $post Post item.
 * @return bool True on success, false on failure.
 */
function bp_activity_post_type_update($post = null)
{
    if (!is_a($post, 'WP_Post')) {
        return;
    }
    // Get the post type tracking args.
    $activity_post_object = bp_activity_get_post_type_tracking_args($post->post_type);
    if (empty($activity_post_object->action_id)) {
        return;
    }
    $activity_id = bp_activity_get_activity_id(array('component' => $activity_post_object->component_id, 'item_id' => get_current_blog_id(), 'secondary_item_id' => $post->ID, 'type' => $activity_post_object->action_id));
    // Activity ID doesn't exist, so stop!
    if (empty($activity_id)) {
        return;
    }
    // Delete the activity if the post was updated with a password.
    if (!empty($post->post_password)) {
        bp_activity_delete(array('id' => $activity_id));
    }
    // Update the activity entry.
    $activity = new BP_Activity_Activity($activity_id);
    if (!empty($post->post_content)) {
        $activity_summary = bp_activity_create_summary($post->post_content, (array) $activity);
        // Backward compatibility filter for the blogs component.
        if ('blogs' == $activity_post_object->component_id) {
            $activity->content = apply_filters('bp_blogs_record_activity_content', $activity_summary, $post->post_content, (array) $activity, $post->post_type);
        } else {
            $activity->content = $activity_summary;
        }
    }
    // Save the updated activity.
    $updated = $activity->save();
    /**
     * Fires after the updating of an activity item for a custom post type entry.
     *
     * @since 2.2.0
     *
     * @param WP_Post              $post     Post object.
     * @param BP_Activity_Activity $activity Activity object.
     */
    do_action('bp_activity_post_type_updated', $post, $activity);
    return $updated;
}