/**
* @group bp_activity_catch_transition_post_type_status
* @group activity_tracking
*/
public function test_bp_activity_catch_transition_post_type_status_publish_existing_post()
{
$u = $this->factory->user->create();
$labels = array('bp_activity_admin_filter' => 'New Foo', 'bp_activity_front_filter' => 'Foos', 'bp_activity_new_post' => '%1$s posted a new <a href="%2$s">foo</a>', 'bp_activity_new_post_ms' => '%1$s posted a new <a href="%2$s">foo</a>, on the site %3$s');
/**
* 'public' must be set to true, otherwise bp_activity_get_post_types_tracking_args() fails.
*/
register_post_type('foo', array('labels' => $labels, 'public' => true, 'supports' => array('buddypress-activity'), 'bp_activity' => array('action_id' => 'new_foo', 'contexts' => array('activity'), 'position' => 40)));
// Temporarily remove post type activity hook so activity item isn't created.
remove_action('transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3);
// Create the initial post.
$p = $this->factory->post->create(array('post_author' => $u, 'post_type' => 'foo'));
$this->assertEmpty(bp_activity_get_activity_id(array('type' => 'new_foo')));
// Add the post type activity hook back.
add_action('transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3);
// Emulate updating a post; this should create an activity item.
wp_update_post(array('ID' => $p, 'post_title' => 'This is an edit'));
// Assert!
$this->assertNotEmpty(bp_activity_get_activity_id(array('type' => 'new_foo')), 'Activity item was not created during an edit of an existing WordPress post.');
// Clean up.
_unregister_post_type('foo');
}
/**
* @group bp_blogs_transition_activity_status
* @group bp_blogs_remove_comment
*/
public function test_bp_blogs_remove_comment_should_remove_spammed_activity_comment()
{
// save the current user and override logged-in user
$old_user = get_current_user_id();
$u = $this->factory->user->create();
$this->set_current_user($u);
$userdata = get_userdata($u);
// create the blog post
$post_id = $this->factory->post->create(array('post_status' => 'publish', 'post_type' => 'post', 'post_title' => 'First title'));
// let's use activity comments instead of single "new_blog_comment" activity items
add_filter('bp_disable_blogforum_comments', '__return_false');
$c1 = wp_new_comment(array('comment_post_ID' => $post_id, 'comment_author' => $userdata->user_nicename, 'comment_author_url' => 'http://buddypress.org', 'comment_author_email' => $userdata->user_email, 'comment_content' => 'this is a blog comment', 'comment_type' => '', 'comment_parent' => 0, 'user_id' => $u));
// save the corresponding activity comment ID
$a1 = bp_activity_get_activity_id(array('type' => 'activity_comment', 'display_comments' => 'stream', 'meta_query' => array(array('key' => 'bp_blogs_post_comment_id', 'value' => $c1))));
// trash the parent comment.
// corresponding activity comment should now be marked as spam
// @see bp_blogs_transition_activity_status()
wp_trash_comment($c1);
// now permanently delete the comment
wp_delete_comment($c1, true);
// activity comment should no longer exist
$a = bp_activity_get(array('in' => $a1, 'display_comments' => 'stream', 'spam' => 'all'));
// this is a convoluted way of testing if the activity comment still exists
$this->assertTrue(empty($a['activities'][0]));
// reset
$this->set_current_user($old_user);
remove_filter('bp_disable_blogforum_comments', '__return_false');
}
/**
* Update an existing group forum post.
*
* Uses the bundled version of bbPress packaged with BuddyPress.
*
* @since BuddyPress (1.1.0)
*
* @param int $post_id The post ID of the existing forum post.
* @param string $post_text The text for the forum post.
* @param int $topic_id The topic ID of the existing forum topic.
* @param mixed $page The page number where the new forum post should reside. Optional.
*
* @return mixed The forum post ID on success. Boolean false on failure.
*/
function groups_update_group_forum_post($post_id, $post_text, $topic_id, $page = false)
{
$bp = buddypress();
/** This filter is documented in bp-groups/bp-groups-forums.php */
$post_text = apply_filters('group_forum_post_text_before_save', $post_text);
/** This filter is documented in bp-groups/bp-groups-forums.php */
$topic_id = apply_filters('group_forum_post_topic_id_before_save', $topic_id);
$post = bp_forums_get_post($post_id);
$post_id = bp_forums_insert_post(array('post_id' => $post_id, 'post_text' => $post_text, 'post_time' => $post->post_time, 'topic_id' => $topic_id, 'poster_id' => $post->poster_id));
if (empty($post_id)) {
return false;
}
$topic = bp_forums_get_topic_details($topic_id);
$activity_action = sprintf(__('%1$s replied to the forum topic %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink($post->poster_id), '<a href="' . bp_get_group_permalink(groups_get_current_group()) . 'forum/topic/' . $topic->topic_slug . '">' . esc_attr($topic->topic_title) . '</a>', '<a href="' . bp_get_group_permalink(groups_get_current_group()) . '">' . esc_attr(bp_get_current_group_name()) . '</a>');
$activity_content = bp_create_excerpt($post_text);
$primary_link = bp_get_group_permalink(groups_get_current_group()) . 'forum/topic/' . $topic->topic_slug . '/';
if (!empty($page)) {
$primary_link .= "?topic_page=" . $page;
}
// Get the corresponding activity item
if (bp_is_active('activity')) {
$id = bp_activity_get_activity_id(array('user_id' => $post->poster_id, 'component' => $bp->groups->id, 'type' => 'new_forum_post', 'item_id' => bp_get_current_group_id(), 'secondary_item_id' => $post_id));
}
/** This filter is documented in bp-groups/bp-groups-forums.php */
$action = apply_filters_ref_array('groups_activity_new_forum_post_action', array($activity_action, $post_text, &$topic, &$topic));
/** This filter is documented in bp-groups/bp-groups-forums.php */
$content = apply_filters_ref_array('groups_activity_new_forum_post_content', array($activity_content, $post_text, &$topic, &$topic));
/** This filter is documented in bp-groups/bp-groups-forums.php */
$filtered_primary_link = apply_filters('groups_activity_new_forum_post_primary_link', $primary_link . "#post-" . $post_id);
groups_record_activity(array('id' => $id, 'action' => $action, 'content' => $content, 'primary_link' => $filtered_primary_link, 'type' => 'new_forum_post', 'item_id' => (int) bp_get_current_group_id(), 'user_id' => (int) $post->poster_id, 'secondary_item_id' => $post_id, 'recorded_time' => $post->post_time));
/**
* Fires after the update of a group forum post.
*
* @since BuddyPress (1.1.0)
*
* @param object $post Object holding current post being updated.
* @param object $topic Object holding current topic details. Passed by reference.
*/
do_action_ref_array('groups_update_group_forum_post', array($post, &$topic));
return $post_id;
}
/**
* bp_like_remove_user_like()
*
* Registers that the user has unliked a given item.
*
*/
function bp_like_remove_user_like($item_id = '', $type = 'activity')
{
global $bp;
if (!$item_id) {
return false;
}
if (!isset($user_id)) {
$user_id = $bp->loggedin_user->id;
}
if ($user_id == 0) {
echo bp_like_get_text('must_be_logged_in');
return false;
}
if ($type == 'activity') {
/* Remove this from the users liked activities. */
$user_likes = get_user_meta($user_id, 'bp_liked_activities', true);
unset($user_likes[$item_id]);
update_user_meta($user_id, 'bp_liked_activities', $user_likes);
/* Update the total number of users who have liked this activity. */
$users_who_like = bp_activity_get_meta($item_id, 'liked_count', true);
unset($users_who_like[$user_id]);
/* If nobody likes the activity, delete the meta for it to save space, otherwise, update the meta */
if (empty($users_who_like)) {
bp_activity_delete_meta($item_id, 'liked_count');
} else {
bp_activity_update_meta($item_id, 'liked_count', $users_who_like);
}
$liked_count = count($users_who_like);
/* Remove the update on the users profile from when they liked the activity. */
$update_id = bp_activity_get_activity_id(array('item_id' => $item_id, 'component' => 'bp-like', 'type' => 'activity_liked', 'user_id' => $user_id));
bp_activity_delete(array('id' => $update_id, 'item_id' => $item_id, 'component' => 'bp-like', 'type' => 'activity_liked', 'user_id' => $user_id));
} elseif ($type == 'blogpost') {
/* Remove this from the users liked activities. */
$user_likes = get_user_meta($user_id, 'bp_liked_blogposts', true);
unset($user_likes[$item_id]);
update_user_meta($user_id, 'bp_liked_blogposts', $user_likes);
/* Update the total number of users who have liked this blog post. */
$users_who_like = get_post_meta($item_id, 'liked_count', true);
unset($users_who_like[$user_id]);
/* If nobody likes the blog post, delete the meta for it to save space, otherwise, update the meta */
if (empty($users_who_like)) {
delete_post_meta($item_id, 'liked_count');
} else {
update_post_meta($item_id, 'liked_count', $users_who_like);
}
$liked_count = count($users_who_like);
/* Remove the update on the users profile from when they liked the activity. */
$update_id = bp_activity_get_activity_id(array('item_id' => $item_id, 'component' => 'bp-like', 'type' => 'blogpost_liked', 'user_id' => $user_id));
bp_activity_delete(array('id' => $update_id, 'item_id' => $item_id, 'component' => 'bp-like', 'type' => 'blogpost_liked', 'user_id' => $user_id));
}
echo bp_like_get_text('like');
if ($liked_count) {
echo ' (' . $liked_count . ')';
}
}
/**
* 2/ Catch the idea in order to use it just before the activity is saved
* @see $this->adjust_activity_args();
*
* We won't change anything to the $activity_content.
*
* @package WP Idea Stream
* @subpackage buddypress/activity
*
* @since 2.1.0
*
* @param string $activity_content the excerpt created by BuddyPress
* @param WP_Post $post the post object (can be an idea)
* @param string $post_permalink the permalink to this object
* @uses bp_activity_get_activity_id() to get an activity id
* @uses bp_activity_delete() to delete an activity
* @return string the activity content unchanged
*/
public function catch_idea_comment($activity_content = '', $post = null, $post_permalink = '')
{
// If it's a comment, BuddyPress is attaching it into the post var of the comment object
if (empty($post->post->post_type) || $this->post_type != $post->post->post_type) {
return $activity_content;
}
$this->idea = $post->post;
$this->secondary_item_id = $post->comment_ID;
/**
* For this particular case, we need to check for a previous entry to delete it
* As a the 'edit_comment' hook will be fired if the comment was edited from the
* 'wp-admin/comment.php?action=editcomment..' screen, bp_blogs_record_comment()
* will not find the new_blog_comment activity to edit and will post a new one
*/
$id = bp_activity_get_activity_id(array('type' => 'new_' . $this->post_type . '_comment', 'secondary_item_id' => $this->secondary_item_id));
// Found one, delete it to prevent duplicates
if (!empty($id)) {
bp_activity_delete(array('id' => $id));
}
// return without editing.
return $activity_content;
}
/**
* @since 1.0.3
* it is not used any more because qwe are linking to threaded view
* Remove notification for group forum posts/new topic
*/
function ac_notifier_remove_notification_for_topic()
{
global $bp;
//is group & group forum active ?
if (!(bp_is_active("groups") && bp_is_active("forums"))) {
return;
}
//if either group or forum is not actuive do not do anything
//check if we are on single topic screen, right?
if (bp_is_group_forum_topic() && is_user_logged_in()) {
$topic_id = bp_forums_get_topic_id_from_slug($bp->action_variables[1]);
//get id from slug
$topic = get_topic($topic_id);
$group_id = $bp->groups->current_group->id;
//find activity id for this topic
$activity_id = bp_activity_get_activity_id(array('user_id' => $topic->poster_id, 'component' => $bp->groups->id, 'type' => "new_forum_topic", "item_id" => $group_id, 'secondary_item_id' => $topic_id));
//remove notification for new topic comments: easy
if (!empty($activity_id)) {
bp_core_delete_notifications_by_item_id($bp->loggedin_user->id, $activity_id, $bp->ac_notifier->id, 'new_activity_comment_' . $activity_id);
}
$posts = ac_notifier_get_forum_post_ids($topic_id);
if (!empty($posts)) {
//find all activities for the post
$activities = ac_notifier_get_activity_ids(array("item_id" => $group_id, "secondary_ids" => $posts, "component" => $bp->groups->id, "type" => "new_forum_post"));
//pass the array
foreach ((array) $activities as $ac_id) {
bp_core_delete_notifications_by_item_id($bp->loggedin_user->id, $ac_id, $bp->ac_notifier->id, 'new_activity_comment_' . $ac_id);
}
}
}
}
/**
* 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
*
//.........这里部分代码省略.........
请发表评论