latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Integrating two sites with s2member

Home Forums Community Forum Integrating two sites with s2member

This topic contains 2 replies, has 2 voices. Last updated by  jorge 3 years, 12 months ago.

Topic Author Topic
Posted: Friday Jan 4th, 2013 at 8:24 am #36156
jorge
Username: jorgitoz

I am trying to integrate two websites as discussed in this previous forum http://www.s2member.com/forums/topic/multiple-websites-buddypress-integration-2/

I think I almost have a solution, but need a little help…

Working with a developer we figured the best path for what I’m looking for was to move my second site to a subdomain and we are using this code:

<?php
/*
Plugin Name: Multisite User Management
Plugin URI: http://github.com/thenbrent/multisite-user-management
Description: Running a WordPress network? You no longer need to manually add users to each of your sites.
Author: Brent Shepherd
Author URI: http://find.brentshepherd.com/
Network: true
Version: 1.1
*/

/**
 * Adds the default roles for all sites to a user, specified by $user_id
 
 function tests(){
 global $wpdb;   
 $user_id = get_current_user_id();
 $user = get_userdata( $user_id );
 
	$capabilities = $user->{$wpdb->prefix . 'capabilities'};
	 var_dump($capabilities);

	if ( !isset( $wp_roles ) )
		$wp_roles = new WP_Roles();

	foreach ( $wp_roles->role_names as $role => $name ) :

		if ( array_key_exists( $role, $capabilities ) )
			echo 'User Role====='.$role;

	endforeach;
   }
add_action('init','tests');   
 */
function msum_add_roles( $user_id ){
    
    global $wpdb;   
    $user = get_userdata( $user_id );

    $userObj = new WP_User($user_id);
	$u=get_user_meta($user_id);
	//print "<pre>"; print_r($u);
	$capabilities = $user->{$wpdb->prefix . 'capabilities'};
	
	if ( !isset( $wp_roles ) )
		$wp_roles = new WP_Roles();

	foreach ( $wp_roles->role_names as $prerole => $name ) :

	    $gd=get_role( $prerole );
		if ( array_key_exists( $prerole, $capabilities ) )
			$role = $prerole;

	endforeach;
	
    //$users = new WP_User( $id [, $name [, $blog_id ] ] );
	
	foreach( msum_get_blog_list( 0, 'all' ) as $key => $blog ) { 

		//if( is_user_member_of_blog( $user_id, $blog[ 'blog_id' ] ) )
		//	continue;

		switch_to_blog( $blog[ 'blog_id' ] );

		//$role = get_option( 'msum_default_user_role', 'none' ); // if no default set, use 'none'
        

		
		if( $role != 'none' ){
			add_user_to_blog( $blog[ 'blog_id' ], $user_id, $role );
			/*
			$user = new WP_User($user_id);
			foreach($userObj->allcaps as $cap){
			$user->add_cap($cap);
			}
			unset($user);
			*/
		}
        //wp_update_user($user); 
		
		restore_current_blog();
	}
	//unset($userObj);
	update_user_meta( $user_id, 'msum_has_caps', 'true' );
}
add_action( 'wpmu_activate_user', 'msum_add_roles', 10, 1 );
add_action( 'wpmu_new_user', 'msum_add_roles', 10, 1 );
add_action( 'user_register', 'msum_add_roles', 10, 1 );

add_action( 'profile_update', 'msum_add_roles',10, 1 );


/**
 * Sometimes a user will be activated along with their blog, this function implements @see msum_add_roles 
 * on the 'wpmu_activate_blog' action.
 */
function msum_activate_blog_user( $blog_id, $user_id ){
	msum_add_roles( $user_id, $blog_id );
}
add_action( 'wpmu_activate_blog', 'msum_activate_blog_user', 10, 2 );


/**
 * The 'wpmu_activate_user', 'wpmu_new_user' & 'wpmu_activate_blog' actions can only be 
 * hooked if the plugin is in the mu-plugins folder, which is a PITA. 
 * 
 * This function calls @see msum_add_roles from the 'wp_login' action.  
 */
function msum_maybe_add_roles( $user_login ) {

	if ( function_exists( 'get_user_by' ) )
		$userdata = get_user_by( 'login', $user_login );
	else
		$userdata = get_userdatabylogin( $user_login );

	if( $userdata != false && get_user_meta( $userdata->ID, 'msum_has_caps', true ) != 'true' )
		msum_add_roles( $userdata->ID );
}
add_action( 'wp_login', 'msum_maybe_add_roles', 10, 1 );
add_action( 'social_connect_login', 'msum_maybe_add_roles', 10, 1 );


/**
 * Outputs the role selection boxes on the 'Network Admin | Settings' page. 
 */
 /*
function msum_options(){

	$blogs = msum_get_blog_list( 0, 'all' );
	echo '<h3>' . __( 'Multisite User Management', 'msum' ). '</h3>';

	if( empty( $blogs ) ) {
		echo '<p><b>' . __( 'No sites available.', 'msum' ) . '</b></p>';
	} else {
		echo '<p>' . __( 'Select the default role for each of your sites.', 'msum' ) . '</p>';
		echo '<p>' . __( 'New users will receive these roles when activating their account. Existing users will receive these roles only if they have the current default role or no role at all for each particular site.', 'msum' ) . '</p>';
		echo '<table class="form-table">';
		foreach( $blogs as $key => $blog ) { 

			switch_to_blog( $blog[ 'blog_id' ] );
			?>
			<tr valign="top">
				<th scope="row"><?php echo get_bloginfo( 'name' ); ?></th>
				<td>
					<select name="msum_default_user_role[<?php echo $blog[ 'blog_id' ]; ?>]" id="msum_default_user_role[<?php echo $blog[ 'blog_id' ]; ?>]">
						<option value="none"><?php _e( '-- None --', 'msum' )?></option>
						<?php wp_dropdown_roles( get_option( 'msum_default_user_role' ) ); ?>
					</select>
				</td> 
			</tr>
		<?php restore_current_blog();
		}
		echo '</table>';
	}
		echo '<p>' . __( '<b>Note:</b> only public, non-mature and non-dashboard sites appear here. Set the default role for the dashboard site above under <b>Dashboard Settings</b>.', 'msum' ) . '</p>';
}
add_action( 'wpmu_options', 'msum_options' );*/


/**
 * Update Default Roles on submission of the multisite options page.
 */
function msum_options_update(){


    $blog_users = get_users( array('blog_id'=>1) );
	
	foreach($blog_users as $blog_user):
	
	    $user_id=$blog_user->ID;    
		
        msum_add_roles( $user_id ); 		
	     
	endforeach;

/*
	if( !isset( $_POST[ 'msum_default_user_role' ] ) || !is_array( $_POST[ 'msum_default_user_role' ] ) )
		return;

    $blog_users = get_users( array('blog_id'=>1) );
	//foreach( $_POST[ 'msum_default_user_role' ] as $blog_id => $new_role ) { 
	foreach( msum_get_blog_list( 0, 'all' ) as $key => $blog ) { 
	
		//switch_to_blog( $blog_id );
		switch_to_blog( $blog[ 'blog_id' ] );
		//$old_role = get_option( 'msum_default_user_role', 'none' ); // default to none

		//if( $old_role == $new_role ) {
		//	restore_current_blog();
		//	continue;
		//}

		//$blog_users = msum_get_users_with_role( $old_role );
		foreach( $blog_users as $blog_user ) {
		
				if( is_user_member_of_blog( $blog_user->ID, $blog[ 'blog_id' ] ) )
        			continue;
/*
			if( $old_role != 'none' )
				remove_user_from_blog( $blog_user, $blog_id );
			if( $new_role != 'none' )

    global $wpdb;   
    $user = get_userdata( $blog_user->ID );
	$capabilities = $user->{$wpdb->prefix . 'capabilities'};
	
	if ( !isset( $wp_roles ) )
		$wp_roles = new WP_Roles();

	foreach ( $wp_roles->role_names as $prerole => $name ) :

		if ( array_key_exists( $prerole, $capabilities ) )
			$role = $prerole;

	endforeach;


				add_user_to_blog( $blog[ 'blog_id' ], $blog_user->ID, $new_role );
			//update_user_meta( $blog_user, 'msum_has_caps', 'true' );
		}
		//update_option( 'msum_default_user_role', $new_role );

		restore_current_blog();
	}
	*/
	
	
}
//add_action( 'update_wpmu_options', 'msum_options_update' );
register_activation_hook( __FILE__, 'msum_options_update' );

/**
 * Selects all the users with a given role and returns an array of the users' IDs. 
 * 
 * @param $role string The role to get the users for
 * @return $users array Array of user IDs for those users with the given role. 
 */
function msum_get_users_with_role( $role ) {
	global $wpdb;

	if( $role != 'none' ) {
		$sql = $wpdb->prepare( "SELECT DISTINCT($wpdb->users.ID) FROM $wpdb->users 
						INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id
						WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' 
						AND $wpdb->usermeta.meta_value LIKE %s", '%' . $role . '%' );

	} else { // get users without a role for current site
		$sql = "SELECT DISTINCT($wpdb->users.ID) FROM $wpdb->users
				 WHERE $wpdb->users.ID NOT IN (
					SELECT $wpdb->usermeta.user_id FROM $wpdb->usermeta
					WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' 
					)";
	}

	$users = $wpdb->get_col( $sql );

	if( $role == 'none' ) { // if we got all users without a capability for the site, that includes super admins
		$super_users = get_super_admins();

		foreach( $users as $key => $user ){ //never modify caps for super admins
			if( is_super_admin( $user ) )
				unset( $users[$key] );
		}
	}

	return $users;
}


/**
 * Clean up when plugin is deleted
 */
function msum_uninstall(){
	foreach( msum_get_blog_list( 0, 'all' ) as $key => $blog ) { 
		switch_to_blog( $blog[ 'blog_id' ] );
		delete_option( 'msum_default_user_role', $role );
		restore_current_blog();
	}
}
register_uninstall_hook( __FILE__, 'msum_uninstall' );


/**
 * Based on the deprecated WPMU get_blog_list function. 
 * 
 * Except this function gets all blogs, even if they are marked as mature and private.
 */
function msum_get_blog_list( $start = 0, $num = 10 ) {
	global $wpdb;

	$blogs = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND archived = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid ), ARRAY_A );

	foreach ( (array) $blogs as $details ) {
		$blog_list[ $details[ 'blog_id' ] ] = $details;
		$blog_list[ $details[ 'blog_id' ] ]['postcount'] = $wpdb->get_var( "SELECT COUNT(ID) FROM " . $wpdb->get_blog_prefix( $details['blog_id'] ). "posts WHERE post_status='publish' AND post_type='post'" );
	}
	unset( $blogs );
	$blogs = $blog_list;

	if ( false == is_array( $blogs ) )
		return array();

	if ( $num == 'all' )
		return array_slice( $blogs, $start, count( $blogs ) );
	else
		return array_slice( $blogs, $start, $num );
}

The code does work to create a new user on the subdomain, but s2member roles and custom capabilities don’t update…

Can you help me get that last part figured out??

Thanks!
Jorge

  • This topic was modified 4 years ago by  Bruce. Reason: Formatting

List Of Topic Replies

Viewing 2 replies - 1 through 2 (of 2 total)
Author Replies
Author Replies
Posted: Saturday Jan 5th, 2013 at 3:21 pm #36314
Bruce
Username: Bruce
Staff Member

Jorge,

I can’t really say what the problem is from that code. You should have two plugins, one on the blog that’s adding users to the other, and one that’s receiving the data and using it to edit user roles/capabilities, correct? Could you post the other snippet, and only comment out the sections that you’re not using.

Note: You have an error with commenting on this which is probably causing things to pop up on your website.

Posted: Sunday Jan 13th, 2013 at 11:25 am #37253
jorge
Username: jorgitoz

I’m still working on this, but my developer quit on me… so I’ll get back once I hire a new developer…

Viewing 2 replies - 1 through 2 (of 2 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.