/** * Record a new blog post in the BuddyPress activity stream. * * @deprecated BuddyPress (2.2.0) * * @param int $post_id ID of the post being recorded. * @param object $post The WP post object passed to the 'save_post' action. * @param int $user_id Optional. The user to whom the activity item will be * associated. Defaults to the post_author. * @return bool|null Returns false on failure. */ function bp_blogs_record_post($post_id, $post, $user_id = 0) { _deprecated_function(__FUNCTION__, '2.2', 'bp_activity_post_type_publish()'); bp_activity_post_type_publish($post_id, $post, $user_id); }
/** * Detect a change in post type status, and initiate an activity update if necessary. * * @since 2.2.0 * * @todo Support untrashing better. * * @param string $new_status New status for the post. * @param string $old_status Old status for the post. * @param object $post Post data. */ function bp_activity_catch_transition_post_type_status($new_status, $old_status, $post) { if (!post_type_supports($post->post_type, 'buddypress-activity')) { return; } // This is an edit. if ($new_status === $old_status) { // An edit of an existing post should update the existing activity item. if ($new_status == 'publish') { bp_activity_post_type_update($post); } return; } // Publishing a previously unpublished post. if ('publish' === $new_status) { // Untrashing the post type - nothing here yet. if ('trash' == $old_status) { /** * Fires if untrashing post in a post type. * * This is a variable filter that is dependent on the post type * being untrashed. * * @since 2.2.0 * * @param WP_Post $post Post data. */ do_action('bp_activity_post_type_untrash_' . $post->post_type, $post); } else { // Record the post. bp_activity_post_type_publish($post->ID, $post); } // Unpublishing a previously published post. } elseif ('publish' === $old_status) { // Some form of pending status - only remove the activity entry. bp_activity_post_type_unpublish($post->ID, $post); } }
/** * Delete or edit activities regarding the transition post status * * @package WP Idea Stream * @subpackage buddypress/activity * * @since 2.0.0 * * @param int $idea_id the idea ID * @param WP_Post $idea the idea object * @uses get_post() to get the idea object if not set * @uses WP_Idea_Stream_Activity->get_idea_and_comments() to get activities to manage * @uses bp_activity_delete() to delete activities * @uses self::bulk_edit_activity to mark activities as private * @uses apply_filters() call 'wp_idea_stream_buddypress_activity_edit' to override edit args */ public function maybe_delete_activity($idea_id = 0, $idea = null) { if (empty($idea)) { $idea = get_post($idea_id); } $activities = $this->get_idea_and_comments($idea_id, $idea->post_status, $idea->post_password); // If no activity stop. if (empty($activities)) { // Publish the activity (no more pending or the password was reset?) if ('publish' == $idea->post_status && empty($idea->post_password)) { bp_activity_post_type_publish($idea_id, $idea); } return; } // Published with a password / trashed / pending or drafted idea > delete activities if (!empty($idea->post_password) || in_array($idea->post_status, array('trash', 'draft', 'pending'))) { /** * Delete activities. */ foreach ($activities as $activity_id) { bp_activity_delete(array('id' => $activity_id)); } // Reset edited activities if (!empty($this->edit_activities[$idea_id])) { $this->edit_activities[$idea_id] = array(); } // Make sure these activities are public } else { /** * There can be a case where the idea was first privately published, * and then edited as publicly published. BuddyPress will create a new * activity, so we need to handle this case and eventually delete private * activities about the same idea, but keep comments. */ if (!empty($this->private_activities[$idea_id])) { // Keep comments only, visibility will be edited after if (count($this->private_activities[$idea_id]) > 1) { $activities = array_slice($this->private_activities[$idea_id], 1); } else { $activities = array_diff($activities, $this->private_activities[$idea_id]); } // Delete the private to avoid duplicates bp_activity_delete(array('id' => $this->private_activities[$idea_id][0])); } // Reset edited activities $this->edit_activities = array(); // The above case needs a new check. if (empty($activities)) { return; } /** * Used internally in case an idea is added or removed from a group * * @param array $edit_args the component and item id arguments * @param WP_Post $idea the idea object */ $edit_args = apply_filters('wp_idea_stream_buddypress_activity_edit', array(), $idea); // Update component, item_id + visibility if (!empty($edit_args['component']) && !empty($edit_args['item_id'])) { $component = sanitize_key($edit_args['component']); $item_id = absint($edit_args['item_id']); self::bulk_edit_activity($activities, 0, $component, $item_id); // Simply update visibility } else { self::bulk_edit_activity($activities, 0); } } }
/** * Detect a change in post type status, and initiate an activity update if necessary. * * @since 2.2.0 * * @todo Support untrashing better. * * @param string $new_status New status for the post. * @param string $old_status Old status for the post. * @param object $post Post data. */ function bp_activity_catch_transition_post_type_status($new_status, $old_status, $post) { if (!post_type_supports($post->post_type, 'buddypress-activity')) { return; } // This is an edit. if ($new_status === $old_status) { // An edit of an existing post should update the existing activity item. if ($new_status == 'publish') { $edit = bp_activity_post_type_update($post); // Post was never recorded into activity stream, so record it now! if (null === $edit) { bp_activity_post_type_publish($post->ID, $post); } // Allow plugins to eventually deal with other post statuses. } else { /** * Fires when editing the post and the new status is not 'publish'. * * This is a variable filter that is dependent on the post type * being untrashed. * * @since 2.5.0 * * @param WP_Post $post Post data. * @param string $new_status New status for the post. * @param string $old_status Old status for the post. */ do_action('bp_activity_post_type_edit_' . $post->post_type, $post, $new_status, $old_status); } return; } // Publishing a previously unpublished post. if ('publish' === $new_status) { // Untrashing the post type - nothing here yet. if ('trash' == $old_status) { /** * Fires if untrashing post in a post type. * * This is a variable filter that is dependent on the post type * being untrashed. * * @since 2.2.0 * * @param WP_Post $post Post data. */ do_action('bp_activity_post_type_untrash_' . $post->post_type, $post); } else { // Record the post. bp_activity_post_type_publish($post->ID, $post); } // Unpublishing a previously published post. } elseif ('publish' === $old_status) { // Some form of pending status - only remove the activity entry. bp_activity_post_type_unpublish($post->ID, $post); // For any other cases, allow plugins to eventually deal with it. } else { /** * Fires when the old and the new post status are not 'publish'. * * This is a variable filter that is dependent on the post type * being untrashed. * * @since 2.5.0 * * @param WP_Post $post Post data. * @param string $new_status New status for the post. * @param string $old_status Old status for the post. */ do_action('bp_activity_post_type_transition_status_' . $post->post_type, $post, $new_status, $old_status); } }
/** * 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)); // Try to create a new activity item for the parent blog post. if (empty($parent_activity_id)) { $parent_activity_id = bp_activity_post_type_publish($recorded_comment->comment_post_ID, $recorded_comment->post); } // 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; }
/** * Update Activity and blogs meta and eventually sync comment with activity comment * * @since 2.5.0 * * @param int|bool $activity_id ID of recorded activity, or false if sync is active. * @param WP_Comment|null $comment The comment object. * @param array $activity_args Array of activity arguments. * @param object|null $activity_post_object The post type tracking args object. * @return int|bool Returns false if no activity, the activity id otherwise. */ function bp_blogs_comment_sync_activity_comment(&$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null) { if (empty($activity_args) || empty($comment->post->ID) || empty($activity_post_object->comment_action_id)) { return false; } // Set the current blog id. $blog_id = get_current_blog_id(); // These activity metadatas are used to build the new_blog_comment action string if (!empty($activity_id) && !empty($activity_args['item_id']) && 'new_blog_comment' === $activity_post_object->comment_action_id) { // add some post info in activity meta bp_activity_update_meta($activity_id, 'post_title', $comment->post->post_title); bp_activity_update_meta($activity_id, 'post_url', esc_url_raw(add_query_arg('p', $comment->post->ID, home_url('/')))); } // Sync comment - activity comment if (!bp_disable_blogforum_comments()) { if (!empty($_REQUEST['action'])) { $existing_activity_id = get_comment_meta($comment->comment_ID, 'bp_activity_comment_id', true); if (!empty($existing_activity_id)) { $activity_args['id'] = $existing_activity_id; } } if (empty($activity_post_object)) { $activity_post_object = bp_activity_get_post_type_tracking_args($comment->post->post_type); } if (isset($activity_post_object->action_id) && isset($activity_post_object->component_id)) { // find the parent 'new_post_type' activity entry $parent_activity_id = bp_activity_get_activity_id(array('component' => $activity_post_object->component_id, 'type' => $activity_post_object->action_id, 'item_id' => $blog_id, 'secondary_item_id' => $comment->comment_post_ID)); // Try to create a new activity item for the parent blog post. if (empty($parent_activity_id)) { $parent_activity_id = bp_activity_post_type_publish($comment->post->ID, $comment->post); } } // we found the parent activity entry // so let's go ahead and reconfigure some activity args if (!empty($parent_activity_id)) { // set the parent activity entry ID $activity_args['activity_id'] = $parent_activity_id; // now see if the WP parent comment has a BP activity ID $comment_parent = 0; if (!empty($comment->comment_parent)) { $comment_parent = get_comment_meta($comment->comment_parent, 'bp_activity_comment_id', true); } // WP parent comment does not have a BP activity ID // so set to 'new_' . post_type activity ID if (empty($comment_parent)) { $comment_parent = $parent_activity_id; } $activity_args['parent_id'] = $comment_parent; $activity_args['skip_notification'] = true; // could not find corresponding parent activity entry // so wipe out $args array } else { $activity_args = array(); } // Record in activity streams if (!empty($activity_args)) { $activity_id = bp_activity_new_comment($activity_args); if (empty($activity_args['id'])) { // The activity metadata to inform about the corresponding comment ID bp_activity_update_meta($activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID); // The comment metadata to inform about the corresponding activity ID add_comment_meta($comment->comment_ID, 'bp_activity_comment_id', $activity_id); // These activity metadatas are used to build the new_blog_comment action string if ('new_blog_comment' === $activity_post_object->comment_action_id) { bp_activity_update_meta($activity_id, 'post_title', $comment->post->post_title); bp_activity_update_meta($activity_id, 'post_url', esc_url_raw(add_query_arg('p', $comment->post->ID, home_url('/')))); } } /** * Fires after an activity comment is added from a WP post comment. * * @since 2.6.0 * * @param int $activity_id The activity comment ID. * @param WP_Comment $post_type_comment WP Comment object. * @param array $activity_args Activity comment arguments. * @param object $activity_post_object The post type tracking args object. */ do_action('bp_blogs_comment_sync_activity_comment', $activity_id, $comment, $activity_args, $activity_post_object); } } // Update the blogs last active date bp_blogs_update_blogmeta($blog_id, 'last_activity', bp_core_current_time()); if ('new_blog_comment' === $activity_post_object->comment_action_id) { /** * Fires after BuddyPress has recorded metadata about a published blog post comment. * * @since 2.5.0 * * @param int $value Comment ID of the blog post comment being recorded. * @param WP_Post $post WP_Comment object for the current blog post. * @param string $value ID of the user associated with the current blog post comment. */ do_action('bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id()); } return $activity_id; }