function dwqa_record_comment_activity($comment_id) { $comment = get_comment($comment_id); $user_id = get_current_user_id(); $post_id = $comment->comment_post_ID; $content = $comment->comment_content; $post = get_post($post_id); $hide_sitewide = $post->post_status == 'private' ? true : false; if (get_post_type($post_id) == 'dwqa-question') { $type = 'comment_question'; $post_permalink = get_permalink($post_id); } else { $type = 'comment_answer'; $question_id = get_post_meta($post_id, '_question', true); $question = get_post($question_id); $post_permalink = get_permalink($question_id); } bp_blogs_record_activity(array('user_id' => $user_id, 'action' => $activity_action, 'content' => $content, 'primary_link' => $post_permalink, 'type' => $type, 'item_id' => 0, 'secondary_item_id' => $post_id, 'recorded_time' => $comment->comment_date_gmt, 'hide_sitewide' => $hide_sitewide)); }
/** * Record blog comment activity. Checks if blog is public and post is not * password protected. * * @param int $comment_id * @param mixed $is_approved * @return mixed */ function bp_blogs_record_comment($comment_id, $is_approved = true) { // 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(); $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 = htmlspecialchars(get_comment_link($recorded_comment->comment_ID)); // Prepare to record in activity streams if (is_multisite()) { $activity_action = sprintf(__('%1$s commented on the post, %2$s, on the site %3$s', 'buddypress'), bp_core_get_userlink($user_id), '<a href="' . $post_permalink . '">' . apply_filters('the_title', $recorded_comment->post->post_title) . '</a>', '<a href="' . get_blog_option($blog_id, 'home') . '">' . get_blog_option($blog_id, 'blogname') . '</a>'); } else { $activity_action = sprintf(__('%1$s commented on the post, %2$s', 'buddypress'), bp_core_get_userlink($user_id), '<a href="' . $post_permalink . '">' . apply_filters('the_title', $recorded_comment->post->post_title) . '</a>'); } $activity_content = $recorded_comment->comment_content; // Record in activity streams bp_blogs_record_activity(array('user_id' => $user_id, 'action' => apply_filters_ref_array('bp_blogs_activity_new_comment_action', array($activity_action, &$recorded_comment, $comment_link)), 'content' => apply_filters_ref_array('bp_blogs_activity_new_comment_content', array($activity_content, &$recorded_comment, $comment_link)), 'primary_link' => apply_filters_ref_array('bp_blogs_activity_new_comment_primary_link', array($comment_link, &$recorded_comment)), 'type' => 'new_blog_comment', 'item_id' => $blog_id, 'secondary_item_id' => $comment_id, 'recorded_time' => $recorded_comment->comment_date_gmt)); // Update the blogs last active date bp_blogs_update_blogmeta($blog_id, 'last_activity', bp_core_current_time()); } return $recorded_comment; }
/** * bp_blogs_record_comment() * * Record blog comment activity. Checks if blog is public and post is not * password protected. * * @global object $wpdb * @global $bp $bp * @param int $comment_id * @param bool $is_approved * @return mixed */ function bp_blogs_record_comment( $comment_id, $is_approved = true ) { global $wpdb, $bp; // 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; // Get the user_id from the comment author email. $user = get_user_by_email( $recorded_comment->comment_author_email ); $user_id = (int)$user->ID; // If there's no registered user id, don't record activity if ( empty( $user_id ) ) return false; // Get blog and post data $blog_id = (int)$wpdb->blogid; $recorded_comment->post = get_post( $recorded_comment->comment_post_ID ); // If this is a password protected post, don't record the comment if ( !empty( $recorded_comment->post->post_password ) ) return false; // If blog is public allow activity to be posted if ( get_blog_option( $blog_id, 'blog_public' ) ) { // Get activity related links $post_permalink = get_permalink( $recorded_comment->comment_post_ID ); $comment_link = htmlspecialchars( get_comment_link( $recorded_comment->comment_ID ) ); // Prepare to record in activity streams $activity_action = sprintf( __( '%s commented on the blog post %s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '</a>' ); $activity_content = $recorded_comment->comment_content; // Record in activity streams bp_blogs_record_activity( array( 'user_id' => $user_id, 'action' => apply_filters( 'bp_blogs_activity_new_comment_action', $activity_action, &$recorded_comment, $comment_link ), 'content' => apply_filters( 'bp_blogs_activity_new_comment_content', $activity_content, &$recorded_comment, $comment_link ), 'primary_link' => apply_filters( 'bp_blogs_activity_new_comment_primary_link', $comment_link, &$recorded_comment ), 'type' => 'new_blog_comment', 'item_id' => $blog_id, 'secondary_item_id' => $comment_id, 'recorded_time' => $recorded_comment->comment_date_gmt ) ); // Update the blogs last active date bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() ); } return $recorded_comment; }
/** * 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)); // 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; }
/** * Add an activity entry for a newly-created site. * * Hooked to the 'bp_blogs_new_blog' action. * * @since 2.6.0 * * @param BP_Blogs_Blog $recorded_blog Current site being recorded. Passed by reference. * @param bool $is_private Whether the current site being recorded is private. * @param bool $is_recorded Whether the current site was recorded. */ function bp_blogs_record_activity_on_site_creation($recorded_blog, $is_private, $is_recorded, $no_activity) { // Only record this activity if the blog is public. if (!$is_private && !$no_activity && bp_blogs_is_blog_trackable($recorded_blog->blog_id, $recorded_blog->user_id)) { bp_blogs_record_activity(array('user_id' => $recorded_blog->user_id, 'primary_link' => apply_filters('bp_blogs_activity_created_blog_primary_link', bp_blogs_get_blogmeta($recorded_blog->blog_id, 'url'), $recorded_blog->blog_id), 'type' => 'new_blog', 'item_id' => $recorded_blog->blog_id)); } }
function bp_import_blog_activity_admin_screen() { global $wpdb; ?> <div class="wrap"> <h2><?php _e('Import Blog Activity', 'bp-import-blog-activity'); ?> </h2> <form name="bp-iba-options-form" method="post" action=""> <div class="bp-iba-options"> <label for="bp-iba-submit"><?php _e('Press the button below to import blog entries and comments left before BuddyPress was installed into the BuddyPress activity streams. Warning: If you have a lot of blogs and comments on your system, this could take a while.', 'bp-import-blog-activity'); ?> </label> <p class="submit"> <input type="submit" name="Submit" value="<?php _e('Import'); ?> »" /> <input type="hidden" name="bp_iba_submit" value="1" /> </p> </form> </div> <?php if (!empty($_POST['bp_iba_submit']) && $_POST['bp_iba_submit'] == '1') { $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' "; $blog_list = $wpdb->get_results($query, ARRAY_A); foreach ($blog_list as $blog) { if ($blog['blog_id'] < 2) { continue; } if ($blog['blog_id'] > 10) { die; } switch_to_blog($blog['blog_id']); print "<pre>"; query_posts('order=ASC&posts_per_page=-1'); echo "Blog name: <strong>" . get_bloginfo('name') . "</strong>"; echo "<br />"; $blog_id = $blog['blog_id']; if (have_posts()) { while (have_posts()) { the_post(); //echo "The post is: "; the_title(); echo "<br />"; $filter = array('object' => 'blogs', 'primary_id' => $blog['blog_id'], 'secondary_id' => get_the_ID(), 'action' => 'new_blog_post'); $activities = bp_activity_get(array('filter' => $filter)); if (empty($activities->activities)) { global $post; if ((int) get_blog_option($blog['blog_id'], 'blog_public')) { $post_permalink = get_permalink(); $activity_action = sprintf(__('%1$s wrote a new post, %2$s, on the site %3$s', 'buddypress'), bp_core_get_userlink((int) $post->post_author), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option($blog_id, 'home') . '">' . get_blog_option($blog_id, 'blogname') . '</a>'); $activity_content = $post->post_content; $post_id = get_the_ID(); bp_blogs_record_activity(array('user_id' => (int) $post->post_author, 'action' => apply_filters('bp_blogs_activity_new_post_action', $activity_action, $post, $post_permalink), 'content' => apply_filters('bp_blogs_activity_new_post_content', $activity_content, $post, $post_permalink), 'primary_link' => apply_filters('bp_blogs_activity_new_post_primary_link', $post_permalink, $post_id), 'type' => 'new_blog_post', 'item_id' => $blog['blog_id'], 'secondary_item_id' => $post_id, 'recorded_time' => $post->post_date_gmt)); } echo "Importing: \"" . $activity_action . "\"<br />"; } $comments = get_comments("post_id=" . $post_id); foreach ($comments as $recorded_comment) { $user = get_user_by('email', $recorded_comment->comment_author_email); if (is_wp_error($user) || !is_a($user, 'WP_User')) { continue; } $user_id = $user->ID; $filter = array('object' => 'blogs', 'primary_id' => $blog['blog_id'], 'secondary_id' => $recorded_comment->comment_ID, 'action' => 'new_blog_comment'); $activities = bp_activity_get(array('filter' => $filter)); if (empty($activities->activities)) { if ((int) get_blog_option($blog_id, 'blog_public')) { global $post; $comment_link = $post_permalink . '#comment-' . $recorded_comment->comment_ID; $activity_action = sprintf(__('%s commented on the blog post %s', 'buddypress'), bp_core_get_userlink($user_id), '<a href="' . $comment_link . '#comment-' . $recorded_comment->comment_ID . '">' . $post->post_title . '</a>'); $activity_content = $recorded_comment->comment_content; /* Record this in activity streams */ bp_blogs_record_activity(array('user_id' => $user_id, 'action' => $activity_action, 'content' => $activity_content, 'primary_link' => $comment_link, 'type' => 'new_blog_comment', 'item_id' => $blog['blog_id'], 'secondary_item_id' => $recorded_comment->comment_ID, 'recorded_time' => $recorded_comment->comment_date_gmt)); echo "Importing: \"" . $activity_action . ": " . $activity_content . "\"<br />"; } } } } // end while have_posts } // end if have_posts wp_reset_query(); echo "<br /><br />"; restore_current_blog(); } } }
function nex_record_comment_activity($comment_id) { $comment = get_comment($comment_id); $user_id = get_current_user_id(); $post_id = $comment->comment_post_ID; $content = $comment->comment_content; if (get_post_type($post_id) == 'dwqa-question') { $post = get_post($post_id); $post_permalink = get_permalink($post_id); $activity_action = sprintf(__('%s commented on the question: %s', 'nex'), bp_core_get_userlink($user_id), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>'); $hide_sitewide = $post->post_status == 'private' ? true : false; } else { $post = get_post($post_id); $question_id = get_post_meta($post_id, '_question', true); $question = get_post($question_id); $post_permalink = get_permalink($question_id); $activity_action = sprintf(__('%s commented on the answer at: %s', 'nex'), bp_core_get_userlink($user_id), '<a href="' . $post_permalink . '">' . $question->post_title . '</a>'); $hide_sitewide = $question->post_status == 'private' ? true : false; } if (function_exists('bp_blogs_record_activity')) { bp_blogs_record_activity(array('user_id' => $user_id, 'action' => $activity_action, 'content' => $content, 'primary_link' => $post_permalink, 'type' => 'new_blog_comment', 'item_id' => 0, 'secondary_item_id' => $comment_id, 'recorded_time' => $comment->comment_date_gmt, 'hide_sitewide' => $hide_sitewide)); } if (function_exists('mycred_add')) { mycred_add('new_comment', $user_id, '10', '%plural% for a Comment', $post_id, array('ref_type' => 'comment')); } }
function bp_blogs_approve_comment($comment_id, $comment) { global $bp, $wpdb; $recorded_comment = bp_blogs_record_comment($comment_id, true); bp_blogs_delete_activity(array('item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_commment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'user_id' => $recorded_comment->user_id)); bp_blogs_record_activity(array('item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_commment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'is_private' => $is_private, 'user_id' => $recorded_comment->user_id, 'recorded_time' => $recorded_comment->date_created)); }
/** * Make BuddyPress aware of a new site so that it can track its activity. * * @since 1.0.0 * * @uses BP_Blogs_Blog * * @param int $blog_id ID of the blog being recorded. * @param int $user_id ID of the user for whom the blog is being recorded. * @param bool $no_activity Optional. Whether to skip recording an activity * item about this blog creation. Default: false. * @return bool|null Returns false on failure. */ function bp_blogs_record_blog($blog_id, $user_id, $no_activity = false) { if (empty($user_id)) { $user_id = bp_loggedin_user_id(); } // If blog is not recordable, do not record the activity. if (!bp_blogs_is_blog_recordable($blog_id, $user_id)) { return false; } $name = get_blog_option($blog_id, 'blogname'); $url = get_home_url($blog_id); if (empty($name)) { $name = $url; } $description = get_blog_option($blog_id, 'blogdescription'); $close_old_posts = get_blog_option($blog_id, 'close_comments_for_old_posts'); $close_days_old = get_blog_option($blog_id, 'close_comments_days_old'); $thread_depth = get_blog_option($blog_id, 'thread_comments'); if (!empty($thread_depth)) { $thread_depth = get_blog_option($blog_id, 'thread_comments_depth'); } else { // Perhaps filter this? $thread_depth = 1; } $recorded_blog = new BP_Blogs_Blog(); $recorded_blog->user_id = $user_id; $recorded_blog->blog_id = $blog_id; $recorded_blog_id = $recorded_blog->save(); $is_recorded = !empty($recorded_blog_id) ? true : false; bp_blogs_update_blogmeta($recorded_blog->blog_id, 'url', $url); bp_blogs_update_blogmeta($recorded_blog->blog_id, 'name', $name); bp_blogs_update_blogmeta($recorded_blog->blog_id, 'description', $description); bp_blogs_update_blogmeta($recorded_blog->blog_id, 'last_activity', bp_core_current_time()); bp_blogs_update_blogmeta($recorded_blog->blog_id, 'close_comments_for_old_posts', $close_old_posts); bp_blogs_update_blogmeta($recorded_blog->blog_id, 'close_comments_days_old', $close_days_old); bp_blogs_update_blogmeta($recorded_blog->blog_id, 'thread_comments_depth', $thread_depth); $is_private = !empty($_POST['blog_public']) && (int) $_POST['blog_public'] ? false : true; $is_private = !apply_filters('bp_is_new_blog_public', !$is_private); // Only record this activity if the activity component is active and the blog is public. if (bp_is_active('activity') && !$is_private && !$no_activity && bp_blogs_is_blog_trackable($blog_id, $user_id)) { // Record this in activity streams. bp_blogs_record_activity(array('user_id' => $recorded_blog->user_id, 'primary_link' => apply_filters('bp_blogs_activity_created_blog_primary_link', $url, $recorded_blog->blog_id), 'type' => 'new_blog', 'item_id' => $recorded_blog->blog_id)); } /** * Fires after BuddyPress has been made aware of a new site for activity tracking. * * @since 1.0.0 * * @param BP_Blogs_Blog $recorded_blog Current blog being recorded. Passed by reference. * @param bool $is_private Whether or not the current blog being recorded is private. * @param bool $is_recorded Whether or not the current blog was recorded. */ do_action_ref_array('bp_blogs_new_blog', array(&$recorded_blog, $is_private, $is_recorded)); }