Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Propagate comments recording from sub network to main site
<?php
add_action( 'comment_post', 'bpdev_record_comment_on_main_site', 20, 2 );
add_action( 'edit_comment', 'bpdev_record_comment_on_main_site', 20 );
function bpdev_record_comment_on_main_site($comment_id, $is_approved = true ) {
global $bp, $wpdb;
$blog_id = (int)$wpdb->blogid;
if($blog_id==1)
return;//it is recording main site and we don't have to worry about it
// 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;
$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;
switch_to_blog(1);
// 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
) );
restore_current_blog();
// Update the blogs last active date
bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
}
return $recorded_comment;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.