/**
* Add or edit activities so that they are private
*
* @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 self::bulk_edit_activity to mark activities as private
* @uses get_current_blog_id() to get the current blog ID
* @uses buddypress() to get BuddyPress instance
* @uses add_query_arg(), get_home_url() to build the link to the idea.
* @uses bp_activity_thumbnail_content_images() to take content, remove images, and replace them with a single thumbnail image
* @uses bp_create_excerpt() to generate the content of the activity
* @uses bp_activity_add() to save the private activity
* @uses apply_filters() call 'bp_blogs_record_activity_content' to apply the filters on blog posts for private ideas
* call 'wp_idea_stream_buddypress_activity_post_private' to override the private activity args
*/
public function mark_activity_private($idea_id = 0, $idea = null)
{
if (empty($idea)) {
$idea = get_post($idea_id);
}
// Get activities
$activities = $this->get_idea_and_comments($idea_id, $idea->post_status);
// If activities, then update their visibility status
if (!empty($activities)) {
// Do not update activities if they all are already marked as private.
if (!empty($this->private_activities[$idea_id]) && !array_diff($activities, $this->private_activities[$idea_id])) {
return;
}
self::bulk_edit_activity($activities, 1);
// Otherwise, we need to create the activity
} else {
/**
* Here we can't use bp_blogs_record_activity() as we need
* to allow the groups component to eventually override the
* component argument. As a result, we need to set a fiew vars
*/
$bp = buddypress();
$bp_activity_actions = new stdClass();
$bp_activity_actions = $bp->activity->actions;
// First the item id: the current blog
$blog_id = get_current_blog_id();
// Then all needed args except content
$args = array('component' => buddypress()->blogs->id, 'type' => 'new_' . $this->post_type, 'primary_link' => add_query_arg('p', $idea_id, trailingslashit(get_home_url($blog_id))), 'user_id' => (int) $idea->post_author, 'item_id' => $blog_id, 'secondary_item_id' => $idea_id, 'recorded_time' => $idea->post_date_gmt, 'hide_sitewide' => 1);
// The content will require to use functions that bp_blogs_record_activity()
// is using to format it.
$content = '';
if (!empty($idea->post_content)) {
$content = bp_activity_thumbnail_content_images($idea->post_content, $args['primary_link'], $args);
$content = bp_create_excerpt($content);
}
// Add the content to the activity args
$args['content'] = $content;
/**
* Filter is used internally to override ideas posted within a private or hidden group
*
* @param array $args the private activity arguments
* @param WP_Post $idea the idea object
*/
$args = apply_filters('wp_idea_stream_buddypress_activity_post_private', $args, $idea);
// Define the corresponding index
$activity_type = $args['type'];
$activity_component = $args['component'];
// override the activity type
$args['type'] = $this->activity_actions[$activity_type]->type;
if (empty($bp->activity->actions->{$activity_component}->{$activity_type}['format_callback'])) {
bp_activity_set_action($this->activity_actions[$activity_type]->component, $this->activity_actions[$activity_type]->type, $this->activity_actions[$activity_type]->admin_caption, $this->activity_actions[$activity_type]->action_callback);
}
/**
* Finally publish the private activity
* and reset BuddyPress activity actions
*/
$private_activity_id = bp_activity_add($args);
// Check for comments
if (!empty($private_activity_id)) {
$activities = $this->get_idea_and_comments($idea_id, $idea->post_status);
$activities = array_diff($activities, array($private_activity_id));
// Update comments visibility
if (!empty($activities)) {
$test = self::bulk_edit_activity($activities, 1);
}
}
$bp->activity->actions = $bp_activity_actions;
// Reset edited activities
$this->edit_activities = array();
}
}
/**
* Get an excerpt of a group description.
*
* @param object $group Optional. The group being referenced. Defaults
* to the group currently being iterated on in the groups loop.
* @return string Excerpt.
*/
function bp_get_group_description_excerpt($group = false)
{
global $groups_template;
if (empty($group)) {
$group =& $groups_template->group;
}
return apply_filters('bp_get_group_description_excerpt', bp_create_excerpt($group->description), $group);
}
/**
* Get an excerpt of a group description.
*
* @since 1.0.0
*
* @param object|bool $group Optional. The group being referenced.
* Defaults to the group currently being
* iterated on in the groups loop.
* @return string Excerpt.
*/
function bp_get_group_description_excerpt($group = false)
{
global $groups_template;
if (empty($group)) {
$group =& $groups_template->group;
}
/**
* Filters the excerpt of a group description.
*
* @since 1.0.0
*
* @param string $value Excerpt of a group description.
* @param object $group Object for group whose description is made into an excerpt.
*/
return apply_filters('bp_get_group_description_excerpt', bp_create_excerpt($group->description), $group);
}
function bp_get_message_thread_excerpt()
{
global $messages_template;
return apply_filters('bp_get_message_thread_excerpt', bp_create_excerpt($messages_template->thread->last_message_message, 20));
}
//.........这里部分代码省略.........
/**
* Filter the arguments passed to the media extractor when creating an Activity summary.
*
* @since 2.3.0
*
* @param array $args Array of bespoke data for the media extractor.
* @param string $content The content of the activity item.
* @param array $activity The data passed to bp_activity_add() or the values from an Activity obj.
* @param BP_Media_Extractor $extractor The media extractor object.
*/
$args = apply_filters('bp_activity_create_summary_extractor_args', $args, $content, $activity, $extractor);
// Extract media information from the $content.
$media = $extractor->extract($content, BP_Media_Extractor::ALL, $args);
// If we converted $content to an object earlier, flip it back to a string.
if (is_a($content, 'WP_Post')) {
$content = $content->post_content;
}
$para_count = substr_count(strtolower(wpautop($content)), '<p>');
$has_audio = !empty($media['has']['audio']) && $media['has']['audio'];
$has_videos = !empty($media['has']['videos']) && $media['has']['videos'];
$has_feat_image = !empty($media['has']['featured_images']) && $media['has']['featured_images'];
$has_galleries = !empty($media['has']['galleries']) && $media['has']['galleries'];
$has_images = !empty($media['has']['images']) && $media['has']['images'];
$has_embeds = false;
// Embeds must be subtracted from the paragraph count.
if (!empty($media['has']['embeds'])) {
$has_embeds = $media['has']['embeds'] > 0;
$para_count -= count($media['has']['embeds']);
}
$extracted_media = array();
$use_media_type = '';
$image_source = '';
// If it's a short article and there's an embed/audio/video, use it.
if ($para_count <= 3) {
if ($has_embeds) {
$use_media_type = 'embeds';
} elseif ($has_audio) {
$use_media_type = 'audio';
} elseif ($has_videos) {
$use_media_type = 'videos';
}
}
// If not, or in any other situation, try to use an image.
if (!$use_media_type && $has_images) {
$use_media_type = 'images';
$image_source = 'html';
// Featured Image > Galleries > inline <img>.
if ($has_feat_image) {
$image_source = 'featured_images';
} elseif ($has_galleries) {
$image_source = 'galleries';
}
}
// Extract an item from the $media results.
if ($use_media_type) {
if ($use_media_type === 'images') {
$extracted_media = wp_list_filter($media[$use_media_type], array('source' => $image_source));
$extracted_media = array_shift($extracted_media);
} else {
$extracted_media = array_shift($media[$use_media_type]);
}
/**
* Filter the results of the media extractor when creating an Activity summary.
*
* @since 2.3.0
*
* @param array $extracted_media Extracted media item. See {@link BP_Media_Extractor::extract()} for format.
* @param string $content Content of the activity item.
* @param array $activity The data passed to bp_activity_add() or the values from an Activity obj.
* @param array $media All results from the media extraction.
* See {@link BP_Media_Extractor::extract()} for format.
* @param string $use_media_type The kind of media item that was preferentially extracted.
* @param string $image_source If $use_media_type was "images", the preferential source of the image.
* Otherwise empty.
*/
$extracted_media = apply_filters('bp_activity_create_summary_extractor_result', $extracted_media, $content, $activity, $media, $use_media_type, $image_source);
}
// Generate a text excerpt for this activity item (and remove any oEmbeds URLs).
$summary = strip_shortcodes(html_entity_decode(strip_tags($content)));
$summary = bp_create_excerpt(preg_replace('#^\\s*(https?://[^\\s"]+)\\s*$#im', '', $summary));
if ($use_media_type === 'embeds') {
$summary .= PHP_EOL . PHP_EOL . $extracted_media['url'];
} elseif ($use_media_type === 'images') {
$summary .= sprintf(' <img src="%s">', esc_url($extracted_media['url']));
} elseif (in_array($use_media_type, array('audio', 'videos'), true)) {
$summary .= PHP_EOL . PHP_EOL . $extracted_media['original'];
// Full shortcode.
}
/**
* Filters the newly-generated summary for the activity item.
*
* @since 2.3.0
*
* @param string $summary Activity summary HTML.
* @param string $content Content of the activity item.
* @param array $activity The data passed to bp_activity_add() or the values from an Activity obj.
* @param array $extracted_media Media item extracted. See {@link BP_Media_Extractor::extract()} for format.
*/
return apply_filters('bp_activity_create_summary', $summary, $content, $activity, $extracted_media);
}
function bp_get_message_thread_excerpt()
{
global $messages_template;
return apply_filters('bp_get_message_thread_excerpt', strip_tags(bp_create_excerpt($messages_template->thread->last_message_content, 75)));
}
/**
* Truncates long activity entries when viewed in activity streams
*
* @since 1.5.0
*
* @param $text The original activity entry text
*
* @uses bp_is_single_activity()
* @uses apply_filters() To call the 'bp_activity_excerpt_append_text' hook
* @uses apply_filters() To call the 'bp_activity_excerpt_length' hook
* @uses bp_create_excerpt()
* @uses bp_get_activity_id()
* @uses bp_get_activity_thread_permalink()
* @uses apply_filters() To call the 'bp_activity_truncate_entry' hook
*
* @return string $excerpt The truncated text
*/
function bp_activity_truncate_entry($text)
{
global $activities_template;
// The full text of the activity update should always show on the single activity screen
if (bp_is_single_activity()) {
return $text;
}
$append_text = apply_filters('bp_activity_excerpt_append_text', __('[Read more]', 'buddypress'));
$excerpt_length = apply_filters('bp_activity_excerpt_length', 358);
// Run the text through the excerpt function. If it's too short, the original text will be
// returned.
$excerpt = bp_create_excerpt($text, $excerpt_length, array('ending' => __('…', 'buddypress')));
// If the text returned by bp_create_excerpt() is different from the original text (ie it's
// been truncated), add the "Read More" link.
if ($excerpt != $text) {
$id = !empty($activities_template->activity->current_comment->id) ? 'acomment-read-more-' . $activities_template->activity->current_comment->id : 'activity-read-more-' . bp_get_activity_id();
$excerpt = sprintf('%1$s<span class="activity-read-more" id="%2$s"><a href="%3$s" rel="nofollow">%4$s</a></span>', $excerpt, $id, bp_get_activity_thread_permalink(), $append_text);
}
return apply_filters('bp_activity_truncate_entry', $excerpt, $text, $append_text);
}
/**
* 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();
$post_text = apply_filters('group_forum_post_text_before_save', $post_text);
$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));
}
// Update the entry in activity streams
groups_record_activity(array('id' => $id, 'action' => apply_filters_ref_array('groups_activity_new_forum_post_action', array($activity_action, $post_text, &$topic, &$topic)), 'content' => apply_filters_ref_array('groups_activity_new_forum_post_content', array($activity_content, $post_text, &$topic, &$topic)), 'primary_link' => apply_filters('groups_activity_new_forum_post_primary_link', $primary_link . "#post-" . $post_id), '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));
do_action_ref_array('groups_update_group_forum_post', array($post, &$topic));
return $post_id;
}
/**
* Create activity for paper edits.
*
* Edit activity is throttled: no more than one activity item per 60 minutes.
*
* @param int $post_id ID of the post.
* @param WP_Post $post_after New post.
* @param WP_Post $post_before Old post.
*/
function cacsp_create_edit_activity($post_id, WP_Post $post_after, WP_Post $post_before)
{
if ('cacsp_paper' !== $post_after->post_type) {
return;
}
// We only want to record edits of published posts. Drafts don't get activity, and BP handles publishing.
if ('publish' !== $post_before->post_status || 'publish' !== $post_after->post_status) {
return;
}
// The author of the edit is the one who wrote the last revision.
if ($revisions = wp_get_post_revisions($post_id)) {
// Grab the last revision, but not an autosave.
foreach ($revisions as $revision) {
if (false !== strpos($revision->post_name, "{$revision->post_parent}-revision")) {
$last_revision = $revision;
break;
}
}
}
// Either revisions are disabled, or something else has gone wrong. Just use the post author.
if (!isset($last_revision)) {
$rev_author = $post_after->post_author;
} else {
$rev_author = $last_revision->post_author;
}
// Throttle.
$existing = bp_activity_get(array('filter_query' => array(array('column' => 'component', 'value' => 'cacsp'), array('column' => 'type', 'value' => 'new_cacsp_edit'), array('column' => 'secondary_item_id', 'value' => $post_id), array('column' => 'user_id', 'value' => $rev_author)), 'update_meta_cache' => false, 'per_page' => 1));
if (!empty($existing['activities'])) {
/**
* Filters the number of seconds in the edit throttle.
*
* This prevents activity stream flooding by multiple edits of the same paper.
*
* @param int $throttle_period Defaults to 6 hours.
*/
$throttle_period = apply_filters('bpeo_event_edit_throttle_period', 6 * HOUR_IN_SECONDS);
if (time() - strtotime($existing['activities'][0]->date_recorded) < $throttle_period) {
return;
}
}
// Poor man's diff. https://coderwall.com/p/3j2hxq/find-and-format-difference-between-two-strings-in-php
$old = $post_before->post_content;
$new = $post_after->post_content;
$from_start = strspn($old ^ $new, "");
$from_end = strspn(strrev($old) ^ strrev($new), "");
$old_end = strlen($old) - $from_end;
$new_end = strlen($new) - $from_end;
$start = substr($new, 0, $from_start);
$end = substr($new, $new_end);
$new_diff = substr($new, $from_start, $new_end - $from_start);
// Take a few words before the diff.
$_start = explode(' ', $start);
$_start = implode(' ', array_slice($_start, -5));
$content = bp_create_excerpt('…' . $_start . $new_diff . $end);
$activity_id = bp_activity_add(array('content' => $content, 'component' => 'cacsp', 'type' => 'new_cacsp_edit', 'primary_link' => get_permalink($post_id), 'user_id' => $rev_author, 'item_id' => get_current_blog_id(), 'secondary_item_id' => $post_id, 'hide_sitewide' => 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;
}
function x_buddypress_get_message_thread_excerpt()
{
global $messages_template;
return strip_tags(bp_create_excerpt($messages_template->thread->last_message_content, 200));
}
请发表评论