Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Created November 1, 2011 19:39
Show Gist options
  • Save boonebgorges/1331676 to your computer and use it in GitHub Desktop.
Save boonebgorges/1331676 to your computer and use it in GitHub Desktop.
Display BuddyPress group content in the context of the associated groupblog
<?php
/**
* Does the grunt work
*/
function bbg_test_groupblog_idea2() {
global $wpdb, $bp, $current_blog;
// Never do this on the root blog
if ( bp_is_root_blog() )
return;
// Is this a group blog?
$group_id = $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_groupmeta} WHERE meta_key = 'groupblog_blog_id' AND meta_value = %s", get_current_blog_id() ) );
if ( !$group_id ) {
return;
}
$group = new BP_Groups_Group( $group_id );
// Fake the group setup process
$bp->current_component = 'groups';
$bp->is_single_item = true;
$bp->current_item = $group->slug;
$bp->groups->current_group = $group;
$bp->groups->current_group->user_has_access = 'public' == $group->status ? true : groups_is_user_member( $group_id, bp_loggedin_user_id() );
if ( empty( $bp->current_action ) && $bp->groups->current_group->user_has_access ) {
$bp->current_action = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
}
// Fake the current action and action variables
$this_blog_request = array_pop( explode( $current_blog->domain, home_url() ) );
$path = esc_url( str_replace( $this_blog_request, '', $_SERVER['REQUEST_URI'] ) );
// Filter the path
$path = apply_filters( 'bp_uri', $path );
// Take GET variables off the URL to avoid problems,
// they are still registered in the global $_GET variable
if ( $noget = substr( $path, 0, strpos( $path, '?' ) ) )
$path = $noget;
// Fetch the current URI and explode each part separated by '/' into an array
$bp_uri = explode( '/', $path );
// Loop and remove empties
foreach ( (array)$bp_uri as $key => $uri_chunk ) {
if ( empty( $bp_uri[$key] ) ) {
unset( $bp_uri[$key] );
}
}
$bp_uri = array_values( $bp_uri );
if ( isset( $bp_uri[0] ) ) {
$bp->current_action = $bp_uri[0];
unset( $bp_uri[0] );
$bp_uri = array_values( $bp_uri );
$bp->action_variables = $bp_uri;
}
// Catch form submits and redirects
add_action( 'bp_actions', 'bbg_catch_gb_form_submits', 1 );
add_filter( 'wp_redirect', 'bbg_catch_gb_redirects', 999 );
}
add_action( 'bp_setup_globals', 'bbg_test_groupblog_idea2', 11);
/**
* Catches form submits and redirects them to the BP group, so they can be handled properly
*/
function bbg_catch_gb_form_submits() {
global $bp, $wpdb;
// Is this a group blog?
$group_id = $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_groupmeta} WHERE meta_key = 'groupblog_blog_id' AND meta_value = %s", get_current_blog_id() ) );
$group = new BP_Groups_Group( $group_id );
$redirect = bp_get_group_permalink( $group );
$cookies = array();
foreach( $_COOKIE as $key => $value ) {
$cookies[] = new WP_Http_Cookie( array( 'name' => $key, 'value' => $value ) );
}
$args = array( 'body' => $_POST, 'cookies' => $cookies );
$test = wp_remote_post( $redirect, $args );
//wp_redirect( $redirect );
}
/**
* Ensures that redirects to a BP group always go to its groupblog
*/
function bbg_catch_gb_redirects( $redirect ) {
global $bp, $wpdb;
if ( bp_is_group() ) {
$groupblog_id = groups_get_groupmeta( bp_get_current_group_id(), 'groupblog_blog_id' );
if ( $groupblog_id ) {
$group_permalink = bp_get_group_permalink( groups_get_current_group() );
$groupblog_url = get_blogaddress_by_id( $groupblog_id );
$redirect = str_replace( $group_permalink, $groupblog_url, $redirect );
}
}
return $redirect;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment