Adapt Q&A for multisite

I'm trying to adapt Q&A for multisite with buddypress. I do think this is something that should be built into the core of it so it would allow the plugin to be optionally deployed on buddypress multisite network subsites.

I've successfully over-ridden the buddypress profile hooks to allow it to use the Q&A user page on the subsite. I did that by commenting out the code in the core file buddypress.php.

The built-in activity stream code in the core file edit.php is not multisite compatible either, so I commented that out. I replaced by adding some code to my bp-custom.php file that works with custom post types. I can get the questions loaded displaying correctly in the activity stream, the trouble comes with the answers.

I use the following code snippet to get custom post types into the activity stream on multisite, anytime I have a CPT I want supported in my activity stream, I copy/paste it and modify for that new CPT:

// begin record Q&A answers to the BP activity stream
function answer_record_activity( $post_id, $post, $user_id = false ) {

	global $bp, $wpdb;

	//check for multisite install
	if(!is_multisite()) return false;

	$post_id = (int)$post_id;
	$blog_id = (int)$wpdb->blogid;

	if ( !$user_id )
		$user_id = (int)$post->post_author;

	/* This is to stop infinite loops with Donncha's sitewide tags plugin */
	if ( (int)$bp->site_options['tags_blog_id'] == (int)$blog_id ) return false;

	/* Don't record this if it's a regular post */
	if ( $post->post_type != 'answer' ) return false;

	if ( ( 'publish' == $post->post_status ) && ( '' == $post->post_password ) ) {

		if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {

			/* Record this in activity streams */
			$post_permalink = get_permalink( $post_id );

			$activity_action = sprintf( __( '%1$s gave an answer, %2$s..., on the Q&A', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
			$activity_content = $post->post_content;

			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_id,
				'secondary_item_id' => $post_id,
				'recorded_time' => $post->post_modified_gmt
			));
		}

	} else
		bp_blogs_remove_post( $post_id, $blog_id );

	bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );

	do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );

	/* check for existing entry and update if one exists */
	$id = bp_activity_get_activity_id( array(
		'user_id' => $user_id,
		'component' => $component,
		'type' => $type,
		'item_id' => $item_id,
		'secondary_item_id' => $secondary_item_id
	) );

	return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time,
'hide_sitewide' => $hide_sitewide ) );

}

add_action( 'save_post', 'answer_record_activity', 10, 2 );
// end record Q&A answers to BP activity stream

The trouble with the snippet above is that it when it adds the link to the activity stream, there is no relation to the parent post. I links correctly to the permalink for the answer, but it displays the answer title instead of the question title. This is because the answer is being treated as its own custom post type with no relation. I have a similar snippet used for the questions but the two snippets don't share any data and I don't know enough php to figure out how to make it work. There must be a way in the above snippet to capture the parent post question data so that it displays the parent post title as the link to the answer instead of the answer title.

Can you guys help me with this? If I can get a hack for this, then I can provide a tutorial on how to make Q&A multisite compatible in buddypress without your guys having to code it into core (though, like I mention at the start, I think the the plugin should be multisite compatible)

  • Saunt Valerian

    Thanks @patrick

    I hope that multisite buddypress support can be added to Q&A. In taking a look at the StackExchange network - if you guys are really looking at creating a competing product - having multisite support for BP would be truly awesome since it would allow for a consolidated activity stream of Q&A posts. This would be truly something wonderful and would shift this plugin into high gear for anyone ambitious enough to take the StackExchange format head-on.

    Anyhow, even if you aren't considering multisite support (though you, this is WPMU DEV after all!) - I hope that you guys will help me finish this hack so that at least there will be a tutorial for it.

  • Hakan

    Hi,

    There must be a way in the above snippet to capture the parent post question data so that it displays the parent post title as the link to the answer instead of the answer title.

    I think you have almost done it. To get the question title, just use like this:

    // Get the question which is the parent of the answer (post)
     $question = get_post($post->post_parent);
    
    // Use question title
    $question_title = $question->post_title;

    When you finish it can you email it to me hakan at inscub.com ?

    I would like to integrate it.

    Cheers,
    Hakan

  • Saunt Valerian

    @Hakan - perfect, your advice worked smoothly and integrated nicely into the code I started with. I tested it and it is working!

    Looking forward to those rep points @tom !

    Here's the revised code for answers with just three little pieces added in:

    // begin record Q&A answers to the BP activity stream
    function answer_record_activity( $post_id, $post, $user_id = false ) {
    
    	global $bp, $wpdb;
    
    	//check for multisite install
    	if(!is_multisite()) return false;
    
    	$post_id = (int)$post_id;
    	$blog_id = (int)$wpdb->blogid;
    
    	if ( !$user_id )
    		$user_id = (int)$post->post_author;
    
    	/* This is to stop infinite loops with Donncha's sitewide tags plugin */
    	if ( (int)$bp->site_options['tags_blog_id'] == (int)$blog_id ) return false;
    
    	/* Don't record this if it's a regular post */
    	if ( $post->post_type != 'answer' ) return false;
    
    	if ( ( 'publish' == $post->post_status ) && ( '' == $post->post_password ) ) {
    
    		if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
    
    			/* Record this in activity streams */
    			$post_permalink = get_permalink( $post_id );
    			$question = get_post($post->post_parent);
    			$question_title = $question->post_title;
    
    			$activity_action = sprintf( __( '%1$s gave an answer to %2$s on the site %3$s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $question->post_title . '</a>', get_blog_option($blog_id, 'blogname') );
    			$activity_content = $post->post_content;
    
    			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_id,
    				'secondary_item_id' => $post_id,
    				'recorded_time' => $post->post_modified_gmt
    			));
    
    		}
    
    	} else
    		bp_blogs_remove_post( $post_id, $blog_id );
    
    	bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    
    	do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
    
    	/* check for existing entry and update if one exists */
    	$id = bp_activity_get_activity_id( array(
    		'user_id' => $user_id,
    		'component' => $component,
    		'type' => $type,
    		'item_id' => $item_id,
    		'secondary_item_id' => $secondary_item_id
    	) );
    
    	return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time,
    'hide_sitewide' => $hide_sitewide ) );
    
    }
    
    add_action( 'save_post', 'answer_record_activity', 10, 2 );
    // end record Q&A answers to BP activity stream

    @hakan - I'll write up the tutorial in another thread based on the current version of Q&A to keep it all clean since this thread only deals with one part of the adaptation for multisite

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.