/** * Detect a change in post status, and initiate an activity update if necessary. * * Posts get new activity updates when (a) they are being published, and (b) * they have not already been published. This enables proper posting for * regular posts as well as scheduled posts, while preventing post bumping. * * See #4090, #3746, #2546 for background. * * @since BuddyPress (2.0.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_blogs_catch_transition_post_status($new_status, $old_status, $post) { // This is an edit if ($new_status === $old_status) { if ($new_status == 'publish') { bp_blogs_update_post($post); return; } } // Publishing a previously unpublished post if ('publish' === $new_status) { // Untrashing the post // Nothing here yet if ('trash' == $old_status) { } // Record the post bp_blogs_record_post($post->ID, $post); // Unpublishing a previously published post } else { if ('publish' === $old_status) { // Some form of pending status // Only remove the activity entry bp_blogs_delete_activity(array('item_id' => get_current_blog_id(), 'secondary_item_id' => $post->ID, 'component' => buddypress()->blogs->id, 'type' => 'new_blog_post')); } } }
function bp_blogs_record_post($post_id, $blog_id = false, $user_id = false) { global $bp, $wpdb; $post_id = (int) $post_id; $post = get_post($post_id); if (!$user_id) { $user_id = (int) $post->post_author; } if (!$blog_id) { $blog_id = (int) $wpdb->blogid; } /* This is to stop infinate loops with Donncha's sitewide tags plugin */ if ((int) get_site_option('tags_blog_id') == (int) $blog_id) { return false; } /* Don't record this if it's not a post */ if ($post->post_type != 'post') { return false; } if (!($is_recorded = BP_Blogs_Post::is_recorded($post_id, $blog_id, $user_id))) { if ('publish' == $post->post_status && '' == $post->post_password) { $recorded_post = new BP_Blogs_Post(); $recorded_post->user_id = $user_id; $recorded_post->blog_id = $blog_id; $recorded_post->post_id = $post_id; $recorded_post->date_created = strtotime($post->post_date); $recorded_post_id = $recorded_post->save(); bp_blogs_update_blogmeta($recorded_post->blog_id, 'last_activity', time()); bp_blogs_record_activity(array('item_id' => $recorded_post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'is_private' => bp_blogs_is_blog_hidden($recorded_post->blog_id), 'user_id' => $recorded_post->user_id, 'recorded_time' => strtotime($post->post_date))); } } else { $existing_post = new BP_Blogs_Post(null, $blog_id, $post_id); /** * Delete the recorded post if: * - The status is no longer "published" * - The post is password protected */ if ('publish' != $post->post_status || '' != $post->post_password) { bp_blogs_remove_post($post_id, $blog_id); } // Check to see if the post author has changed. if ((int) $existing_post->user_id != (int) $post->post_author) { // Delete the existing recorded post bp_blogs_remove_post($post_id, $blog_id); // Re-record the post with the new author. bp_blogs_record_post($post_id); } $recorded_post = $existing_post; /* Delete and re-add the activity stream item to reflect potential content changes. */ bp_blogs_delete_activity(array('item_id' => $recorded_post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'user_id' => $recorded_post->user_id)); bp_blogs_record_activity(array('item_id' => $recorded_post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'is_private' => bp_blogs_is_blog_hidden($recorded_post->blog_id), 'user_id' => $recorded_post->user_id, 'recorded_time' => strtotime($post->post_date))); } do_action('bp_blogs_new_blog_post', $recorded_post, $is_private, $is_recorded); }