PageRenderTime 69ms CodeModel.GetById 2ms app.highlight 59ms RepoModel.GetById 1ms app.codeStats 1ms

/users.php

https://github.com/Jonathonbyrd/Wordpress-Plugin-Framework-deprecated
PHP | 1042 lines | 662 code | 135 blank | 245 comment | 126 complexity | 7ae15e47682d4aca90e92483573a33f8 MD5 | raw file
   1<?php 
   2/**
   3 * @Author	Jonathon byrd
   4 * @link http://www.jonathonbyrd.com
   5 * @Package Wordpress
   6 * @SubPackage Byrd Plugin Framework
   7 * @copyright Proprietary Software, Copyright Byrd Incorporated. All Rights Reserved
   8 * @Since 1.0
   9 * 
  10 * users.php
  11 * 
  12 */
  13
  14defined('ABSPATH') or die("Cannot access pages directly.");
  15
  16
  17if ( !function_exists( 'get_user' ) ):
  18
  19	/**
  20	 * Action Hooks
  21	 * 
  22	 */
  23	add_action( 'init', 'do_redirect_from_admin' );
  24	add_action( 'init', 'save_user_profile', 20 );
  25	
  26	//actions for user profile page
  27	add_action( 'show_user_profile', 'display_custom_user_fields' );
  28	add_action( 'edit_user_profile', 'display_custom_user_fields' );
  29	
  30	//actions for registration page
  31	add_action( 'register_form', 'do_registration_form' );
  32	add_action( 'edit_profile_fields', 'display_profile_fields' );
  33	
  34	//actions for updating fields
  35	add_action( 'personal_options_update', 'save_user_meta_data' );
  36	add_action( 'edit_user_profile_update', 'save_user_meta_data' );
  37
  38	
  39	/**
  40	 * Do registration form
  41	 * 
  42	 * @return boolean
  43	 * @since 1.2
  44	 */
  45	function do_registration_form()
  46	{
  47		//initializing variables
  48		$user_type = is_user_type();
  49		$pages = get_registration_pages();
  50		$status = true;
  51		
  52		//reasons to fail
  53		if (!$user_type) $status = false;
  54		if ($status && !isset($pages[$user_type])) $status = false;
  55		if (!$status) do_redirect( get_bloginfo('url').'/registration/' );
  56		
  57		display_custom_user_fields( null, get_registration_fields( $user_type ));
  58		return true;
  59	}
  60	
  61	/**
  62	 * Redirects to the proper page
  63	 */
  64	function do_redirect_from_admin()
  65	{
  66		//initializing variables
  67		$capability = 'activate_plugins';
  68		$user =& get_user();
  69		
  70		//reasons to fail
  71		if ( strpos($_SERVER["REQUEST_URI"], '/wp-admin') === false ) return false;
  72		if ( current_user_can($capability) ) return false;
  73		if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) return false;
  74		if ( defined('DOING_AJAX') && DOING_AJAX ) return false;
  75		if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return false;
  76		if ( defined('DOING_CRON') && DOING_CRON ) return false;
  77		if ( defined('WP_FIRST_INSTALL') && WP_FIRST_INSTALL ) return false;
  78		if ( defined('WP_IMPORTING') && WP_IMPORTING ) return false;
  79		if ( defined('WP_INSTALLING') && WP_INSTALLING ) return false;
  80		if ( defined('WP_REPAIRING') && WP_REPAIRING ) return false;
  81		if ( defined('WP_UNINSTALL_PLUGIN') && WP_UNINSTALL_PLUGIN ) return false;
  82		if ( is_ajaxrequest() ) return false;
  83		
  84		//if this is an ajax post
  85		if (!$user->ID && isset($_POST['logged_in_cookie']))
  86		{
  87			$parts = explode('|', $_POST['logged_in_cookie']);
  88			$user =& get_user( $parts[0] );
  89		}
  90		
  91		//if we can get the user ourself
  92		if ( $user->has_cap($capability) ) return false;
  93		
  94		if (is_user_logged_in())
  95		{
  96			do_redirect(get_bloginfo('url').'/profile/');
  97		}
  98		else
  99		{
 100			do_redirect(get_bloginfo('url').'/login/');
 101		}
 102	}
 103	
 104	/**
 105	 * Save the User Profile
 106	 * 
 107	 * This function is responsible for saving the user fields upon post. SO
 108	 * LONG AS, the user is already logged in. This does not create a new user.
 109	 * 
 110	 * @return boolean
 111	 * @since 1.2
 112	 */
 113	function save_user_profile()
 114	{
 115		//initializing variables
 116		$user =& get_user( BRequest::getVar('user_id') );
 117		
 118		//reasons to fail
 119		//handling any required actions
 120		if ( !is_user_logged_in() ) return false;
 121		if ( BRequest::getVar('action',false) != 'edit' ) return false;
 122		if ( !wp_verify_nonce(BRequest::getVar("user_meta_box_nonce"), basename(__FILE__)) ) 
 123			return false;
 124		
 125		//initializing variables
 126		$data = BRequest::get('post');
 127		$data['ID'] = $user->ID;
 128		
 129		//loading libraries
 130		require_once( ABSPATH.WPINC.DS.'registration.php' );
 131				
 132		
 133		//doing all the saves
 134		if (!save_useremail()) $data['user_email'] = $user->user_email;
 135		
 136		if (wp_insert_user($data) //update the user
 137		&& save_userpw( $data['pass1'], $data['pass2'] ) //update the users pw
 138		&& save_user_meta_data( $data['ID'] )) //update the users email
 139		{
 140			set_notification('Profile has been updated');
 141		}
 142		return true;
 143	}
 144	
 145	/**
 146	 * Save the password
 147	 * 
 148	 * @param $pass1
 149	 * @param $pass2
 150	 * @since 1.0
 151	 */
 152	function save_userpw( $pass1 = null, $pass2 = null )
 153	{
 154		//reasons to fail
 155		if ( !is_user_logged_in() ) return false;
 156		if ( is_null($pass1) ) return true;
 157		if ( is_null($pass2) ) return true;
 158		if ( trim($pass1) == "" ) return true;
 159		
 160		//checking for harmful injections
 161		$temp = strip_tags($pass1);
 162		if ($temp != $pass1) return false;
 163		if ($pass2 != $pass1) return false;
 164		
 165		//initializing variables
 166		$data = array();
 167		$data['user_pass'] = wp_hash_password($pass1);
 168		
 169		//loading resources
 170		global $wpdb;
 171		$user =& get_user();
 172		
 173		if ($wpdb->update( $wpdb->users, $data, array('ID' => $user->ID) ))
 174		{
 175			return true;
 176		}
 177		
 178		return false;
 179	}
 180	
 181	/**
 182	 * Saves the users email
 183	 * 
 184	 * @since 1.0
 185	 */
 186	function save_useremail()
 187	{	
 188		//initializing variables
 189		if ( !is_user_logged_in() ) return false;
 190		if ( !BRequest::getVar("user_email", false) )
 191		{
 192			set_warning('An email is required.');
 193			return false;
 194		}
 195		
 196		require_once dirname(__file__).DS."includes".DS.'mail.php';
 197		if (!check_email_address(BRequest::getVar("user_email")))
 198		{
 199			set_warning('The given email must be valid.');
 200			return false;
 201		}
 202		
 203		//loading resources
 204		require_once(ABSPATH . WPINC  . '/pluggable.php');
 205		
 206		//initializing variables
 207		global $wpdb;
 208		$user =& get_user();
 209		$data = array();
 210		$data["user_email"] = BRequest::getVar("user_email");
 211		
 212		if ($wpdb->update( $wpdb->users, $data, array('ID' => $user->ID) ))
 213		{
 214			return true;
 215		}
 216		return false;
 217	}
 218	
 219	/**
 220	 * Save user meta data
 221	 * 
 222	 * @param $user_id
 223	 */
 224	function save_user_meta_data( $user_id ) 
 225	{
 226		//initializing variables
 227		$user = new WP_User( $user_id );
 228		$fields = array();
 229		
 230		$fields = wp_parse_args($fields, get_custom_user_fields( $user->roles[0] ));
 231		
 232		//reasons to fail
 233		if (empty($fields)) return false;
 234		
 235		//load library
 236		require_once ABSPATH.WPINC."/pluggable.php";
 237		
 238		// verify nonce
 239		if (!wp_verify_nonce(BRequest::getVar('user_meta_box_nonce'), basename(__FILE__))) {
 240			return $user_id;
 241		}
 242		
 243		// check autosave
 244		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
 245			return $user_id;
 246		}
 247		
 248		if (is_array($fields))
 249		{
 250			foreach ($fields as $field)
 251			{
 252				if (!isset($_POST[$field['id']])) continue;
 253
 254				$old = get_user_meta($user_id, $field['id'], true);
 255	    		$new = BRequest::getVar($field['id'],"");
 256
 257	    		if ($new && $new != $old)
 258	    		{
 259	    			//if ($field['type'] == "address") save_latitude_and_longitude($post_id,$new);
 260	    			update_user_meta($user_id, $field['id'], $new);
 261	    		}
 262	    		elseif ('' == $new && $old)
 263	    		{
 264	    			delete_user_meta($user_id, $field['id'], $old);
 265	    		}
 266	    		
 267	    	}
 268	    	return true;
 269	    }
 270	    
 271	}
 272	
 273	/**
 274	 * Sets a new custom user field
 275	 * 
 276	 * @param array $args
 277	 */
 278	function add_custom_user_field( $user_type = 'subscriber', $args = null )
 279	{
 280		static $fields;
 281		if (!isset($fields))
 282		{
 283			$fields = array();
 284		}
 285		
 286		if (is_null($args)) return $fields;
 287		
 288		$defaults = array(
 289			'name' => 'example',
 290			'desc' => '',
 291			'id' => 'example',
 292			'type' => 'text',
 293			'std' => ''
 294		);
 295			
 296		$args = wp_parse_args( $args, $defaults );
 297		
 298		$fields[$user_type][$args['id']] = $args;
 299		return true;
 300	}
 301	
 302	/**
 303	 * Register all of the user types
 304	 * 
 305	 * @param array $user_types
 306	 */
 307	function register_user_types( $user_types )
 308	{
 309		//reasons to fail
 310		if (!is_array($user_types)) return false;
 311		
 312		foreach ($user_types as $user_type)
 313		{
 314			register_user_type( $user_type );
 315		}
 316		return true;
 317	}
 318	
 319	/**
 320	 * Register the user Type
 321	 * 
 322	 * @param array $user_type
 323	 */
 324	function register_user_type( $user_type = null )
 325	{
 326		//initializing variables
 327		static $user_types;
 328		$default = array(
 329			'role' => get_option('default_role'),
 330			'name' => ucfirst(get_option('default_role')),
 331			'registration' => false,
 332			'user_meta' => false,
 333		);
 334		
 335		if (!isset($user_types))
 336		{
 337			$user_types = array();
 338		}
 339		
 340		if (is_null($user_type)) return $user_types;
 341		
 342		$user_type = wp_parse_args($user_type, $default);
 343		
 344		//set the registration page if we have one
 345		if ($user_type['registration'])
 346		{
 347			$user_type['registration']['role'] = $user_type['role'];
 348			$user_type['registration']['name'] = $user_type['name'];
 349			registration_page( $user_type['registration'] );
 350		}
 351		
 352		if ($user_type['user_meta'])
 353		{
 354			register_user_metas($user_type['role'], $user_type['user_meta']);
 355		}
 356		
 357		$user_types[$user_type['role']] = $user_type;
 358		return true;
 359	}
 360	
 361	/**
 362	 * Get and return all of the custom user fields
 363	 * 
 364	 * @return array
 365	 */
 366	function register_user_metas( $user_type = 'subscriber', $fields = null )
 367	{
 368		if (is_null($fields)) return false;
 369		
 370		foreach ($fields as $field)
 371		{
 372			add_custom_user_field( $user_type, $field );
 373		}
 374		return true;
 375	}
 376	
 377	/**
 378	 * Register a bunch of pages
 379	 * 
 380	 * @param unknown_type $pages
 381	 */
 382	function registration_pages( $pages )
 383	{
 384		//reasons to fail
 385		if (!is_array($pages)) return false;
 386		
 387		foreach ($pages as $page)
 388		{
 389			registration_page( $page );
 390		}
 391		return true;
 392	}
 393	
 394	/**
 395	 * Register a single page
 396	 * 
 397	 * @param unknown_type $page
 398	 * @return array
 399	 * @since 1.2
 400	 */
 401	function registration_page( $page = null )
 402	{
 403		//initializing variables
 404		static $pages;
 405		$default = array(
 406			'role' => get_option('default_role'),
 407			'name' => ucfirst(get_option('default_role')),
 408			'redirect_to' => get_bloginfo('url').'/profile/',
 409			'fields' => array('user_login','user_email'),
 410			'force_login' => false
 411		);
 412		
 413		if (!isset($pages))
 414		{
 415			$pages = array();
 416			$pages[$default['role']] = $default;
 417		}
 418		
 419		if (is_null($page)) return $pages;
 420		
 421		$page = wp_parse_args($page, $default);
 422		
 423		$pages[$page['role']] = $page;
 424		return true;
 425	}
 426	
 427	/**
 428	 * Get's the display names for the given user
 429	 * 
 430	 * @return array
 431	 */
 432	function get_display_names( $user_id = null )
 433	{
 434		$user =& get_user( $user_id );
 435		
 436		$displays = array(
 437			BRequest::getVar('user_login', $user->user_login), 
 438			trim(BRequest::getVar('first_name', $user->first_name).' '.
 439				BRequest::getVar('last_name', $user->last_name)), 
 440			BRequest::getVar('nickname', $user->nickname)
 441		);
 442		
 443		return $displays;
 444	}
 445	
 446	/**
 447	 * Get the Roles Array
 448	 * 
 449	 * This will return an array of user roles
 450	 * 
 451	 * @param $author_id
 452	 * @param $post_type
 453	 * @return array
 454	 */
 455	function get_roles_array()
 456	{
 457		global $wpdb, $wp_roles;
 458		$user =& get_user();
 459		$roles = array();
 460		$continue = true;
 461		
 462		$capabilities = $user->{$wpdb->prefix . 'capabilities'};
 463		if (!is_array($capabilities) && !is_object($capabilities)) return false;
 464		
 465		if ( !isset( $wp_roles ) )
 466			$wp_roles = new WP_Roles();
 467	
 468		foreach ( $wp_roles->role_names as $role => $name ) :
 469			
 470			if ( array_key_exists( $role, $capabilities ) )
 471			{
 472				$continue = false;
 473			}
 474			if ($continue) continue;
 475			$roles[$role] = $name;
 476		endforeach;
 477		
 478		return $roles;
 479	}
 480	
 481	/**
 482	 * Count the posts
 483	 * 
 484	 * @param $author_id
 485	 * @param $post_type
 486	 * @return array
 487	 */
 488	function get_user_role( $user_id = null )
 489	{
 490		global $wpdb, $wp_roles;
 491		$user =& get_user();
 492		if (is_null($user_id))
 493		{
 494			$user_id = $user->ID;
 495		}
 496		$user = get_userdata( $user_id );
 497
 498		$capabilities = $user->{$wpdb->prefix . 'capabilities'};
 499		if (!is_array($capabilities) && !is_object($capabilities)) return false;
 500		
 501		if ( !isset( $wp_roles ) )
 502			$wp_roles = new WP_Roles();
 503	
 504		foreach ( $wp_roles->role_names as $role => $name ) :
 505			
 506			if ( array_key_exists( $role, $capabilities ) )
 507				break;
 508	
 509		endforeach;
 510		
 511		return $role;
 512	}
 513	
 514	/**
 515	 * Get the current user
 516	 * 
 517	 * Function is responsible for creating and returning the user object
 518	 * 
 519	 * @since 1.0
 520	 * @param $userid
 521	 * @return global object reference
 522	 */
 523	function &get_user( $userid = null )
 524	{
 525		//initializing variables
 526		static $users;
 527		if (is_null($users))
 528		{
 529			$users = array();
 530		}
 531				
 532		//loading library
 533		require_once ABSPATH . WPINC . DS . 'pluggable.php';
 534		
 535		//if we want the logged in user
 536		if (is_null($userid))
 537		{
 538			if ( !$user = wp_validate_auth_cookie() )
 539			{
 540				if ( is_admin() 
 541				|| empty($_COOKIE[LOGGED_IN_COOKIE]) 
 542				|| !$user = wp_validate_auth_cookie($_COOKIE[LOGGED_IN_COOKIE], 'logged_in') )
 543				{
 544					$userid = 0;
 545				}
 546			}
 547			$userid = $user;
 548		}
 549		
 550		//if we're wanting to standardize the userid
 551		if (is_object($userid) && isset($userid->ID))
 552		{
 553			$userid = $userid->ID;
 554		}
 555		
 556		if (!isset($users[$userid]))
 557		{
 558			$user = new WP_User( $userid );
 559			$users[$userid] =& $user;
 560		}
 561		
 562		return $users[$userid];
 563	}
 564	
 565	/**
 566	 * Get the users profile link
 567	 * 
 568	 * @param unknown_type $user_id
 569	 * @return string
 570	 * @since 1.2
 571	 */
 572	function get_profile_url( $user_id = null )
 573	{
 574		//initializing variables
 575		$user =& get_user($user_id);
 576		$link = get_bloginfo('url').'/profile/?user_id='.$user->ID;
 577		
 578		return $link;
 579	}
 580	
 581	/**
 582	 * Count the posts
 583	 * 
 584	 * @param $author_id
 585	 * @param $post_type
 586	 * @return array
 587	 */
 588	function get_users_by_role( $search_term = '', $page = '', $role = "Author" )
 589	{
 590		require_once ABSPATH."/wp-admin/includes/user.php";
 591		$wp_user_search = new WP_User_Search($search_term, $page, $role);
 592		return $wp_user_search->get_results();
 593	}
 594	
 595	/**
 596	 * Get and return all of the custom user fields
 597	 * 
 598	 * @return array
 599	 */
 600	function get_custom_user_fields( $user_type = 'subscriber' )
 601	{
 602		//initializing variables
 603		$fields = add_custom_user_field();
 604		
 605		if (!isset($fields[$user_type])) return false;
 606		return $fields[$user_type];
 607	}
 608	
 609	/**
 610	 * Get the registered pages
 611	 * 
 612	 * @return array
 613	 * @since 1.2
 614	 */
 615	function get_registration_pages()
 616	{
 617		return registration_page();
 618	}
 619	
 620	/**
 621	 * Get the registration fields
 622	 * 
 623	 * @return array
 624	 */
 625	function get_registration_fields( $user_type )
 626	{
 627		//initializing variables
 628		$pages = get_registration_pages();
 629		$page = $pages[$user_type];
 630		
 631		$defaults = get_default_profile_fields();
 632		$field_ids = $page['fields'];
 633		$fields = get_custom_user_fields( $user_type );
 634		$fields = wp_parse_args( $fields, $defaults );
 635		
 636		
 637		//verifying that we have what we need.
 638		if (!in_array('user_login', $field_ids)) $field_ids[] = 'user_login';
 639		if (!in_array('user_email', $field_ids)) $field_ids[] = 'user_email';
 640		
 641		$registration_fields = array();
 642		foreach ($field_ids as $id)
 643		{
 644			$registration_fields[] = $fields[$id];
 645		}
 646		return $registration_fields;
 647	}
 648	
 649	/**
 650	 * Get the registration details
 651	 * 
 652	 * @return array
 653	 * @since 1.2
 654	 */
 655	function get_registration_page()
 656	{
 657		$pages = get_registration_pages();
 658		$role = BRequest::getVar('user_type');
 659		$page = $pages[$role];
 660		
 661		return $page;
 662	}
 663	
 664	/**
 665	 * Contains all of the default user fields
 666	 * 
 667	 * @return array
 668	 */
 669	function get_default_profile_fields()
 670	{
 671		return array(
 672			'rich_editing' => array(
 673				'name' => 'Visual Editor',
 674				'desc' => 'Disable the visual editor when writing',
 675				'id' => 'rich_editing',
 676				'type' => 'checkbox',
 677				'std' => ''
 678			),
 679			'comment_shortcuts' => array(
 680				'name' => 'Keyboard Shortcuts',
 681				'desc' => 'Enable keyboard shortcuts for comment moderation. <a href="http://codex.wordpress.org/Keyboard_Shortcuts">More information</a>',
 682				'id' => 'comment_shortcuts',
 683				'type' => 'checkbox',
 684				'std' => ''
 685			),
 686			'user_login' => array(
 687				'name' => 'Username',
 688				'desc' => 'Usernames cannot be changed.',
 689				'id' => 'user_login',
 690				'type' => 'text',
 691				'std' => ''
 692			),
 693			'role' => array(
 694				'name' => 'Role',
 695				'desc' => 'Disable the visual editor when writing',
 696				'id' => 'role',
 697				'type' => 'select',
 698				'options' => create_function('', "return get_roles_array();")
 699			),
 700			'first_name' => array(
 701				'name' => 'First Name',
 702				'desc' => '',
 703				'id' => 'first_name',
 704				'type' => 'text',
 705				'std' => ''
 706			),
 707			'last_name' => array(
 708				'name' => 'Last Name',
 709				'desc' => '',
 710				'id' => 'last_name',
 711				'type' => 'text',
 712				'std' => ''
 713			),
 714			'nickname' => array(
 715				'name' => 'Nickname',
 716				'desc' => '',
 717				'id' => 'nickname',
 718				'type' => 'text',
 719				'std' => ''
 720			),
 721			'display_name' => array(
 722				'name' => 'Display name publicly as',
 723				'desc' => '',
 724				'id' => 'display_name',
 725				'type' => 'select',
 726				'options' => create_function('', "return get_display_names( BRequest::getVar( 'user_id' ));")
 727			),
 728			'user_email' => array(
 729				'name' => 'E-mail',
 730				'desc' => '',
 731				'id' => 'user_email',
 732				'type' => 'email',
 733				'std' => ''
 734			),
 735			'url' => array(
 736				'name' => 'Website',
 737				'desc' => '',
 738				'id' => 'url',
 739				'type' => 'text',
 740				'std' => ''
 741			),
 742			'aim' => array(
 743				'name' => 'AIM',
 744				'desc' => '',
 745				'id' => 'aim',
 746				'type' => 'text',
 747				'std' => ''
 748			),
 749			'yim' => array(
 750				'name' => 'Yahoo IM',
 751				'desc' => '',
 752				'id' => 'yim',
 753				'type' => 'text',
 754				'std' => ''
 755			),
 756			'jabber' => array(
 757				'name' => 'Jabber / Google Talk',
 758				'desc' => '',
 759				'id' => 'jabber',
 760				'type' => 'text',
 761				'std' => ''
 762			),
 763			'description' => array(
 764				'name' => 'Biographical Info',
 765				'desc' => 'Share a little biographical information to fill out your profile. This may be shown publicly.',
 766				'id' => 'description',
 767				'type' => 'textarea',
 768				'std' => ''
 769			),
 770			'password' => array(
 771				'name' => 'New Password',
 772				'desc' => '',
 773				'id' => 'password',
 774				'type' => 'password',
 775				'std' => ''
 776			),
 777		);
 778	}
 779	
 780	/**
 781	 * Checks to see if we have any custom user meta fields
 782	 * 
 783	 * @return boolean
 784	 */
 785	function has_custom_user_fields()
 786	{
 787		$fields = get_custom_user_fields();
 788		if (empty($fields)) return false;
 789		return true;
 790	}
 791	
 792	/**
 793	 * Display the user type links
 794	 * 
 795	 * @return boolean
 796	 * @since 1.2
 797	 */
 798	function display_user_type_links()
 799	{
 800		if (is_user_type()) return false;
 801		
 802		$pages = get_registration_pages();
 803		
 804		echo '<ul class="registration_types">';
 805		foreach ($pages as $page)
 806		{
 807			echo "<li><a href='?user_type={$page['role']}'><span>Register as a </span>{$page['name']}</a></li>";
 808		}
 809		echo '</ul>';
 810		
 811		return true;
 812	}
 813	
 814	/**
 815	 * Displays all of the user profile fields.
 816	 * 
 817	 * @param unknown_type $userid
 818	 */
 819	function display_profile_fields()
 820	{
 821		//initializing variables
 822		$user =& get_user();
 823		$fields = get_custom_user_fields( $user->roles[0] );
 824		$defaults = get_default_profile_fields();
 825		
 826		$fields = wp_parse_args( $fields, $defaults );
 827		display_custom_user_fields($user, $fields);
 828	}
 829	
 830	/**
 831	 * Display the user edit fields
 832	 * 
 833	 * @param unknown_type $user
 834	 */
 835	function display_custom_user_fields($user = null, $fields = null) 
 836	{
 837		//initializing variables
 838		if (!is_null($user)) $user = get_userdata($user->ID);
 839		$is_administration = false;
 840		if (is_null($fields)) $is_administration = true;
 841		
 842		if ($is_administration)
 843		{
 844			echo "<style>",
 845			".field_wrapper label {display:block;position:relative;float:left;width:220px;}",
 846			".typetext input {width: 25em;}",
 847			".typecheckbox input {margin-right:200px;position:relative;float:left;}",
 848			".field_wrapper span {display:block;padding-left:220px;}",
 849			".field_wrapper {padding: 10px;}",
 850			".typetextarea textarea {width: 500px;}",
 851			".field_wrapper .profile_description{font-family: 'Lucida Grande', Verdana, Arial, 'Bitstream Vera Sans', sans-serif;font-size: 12px;font-style: italic;color: #666;}",
 852			"</style>",
 853			"<h3>Additional Details</h3>";
 854			
 855			//initializing variables
 856			$currentUser = new WP_User( $user->ID );
 857			$fields = array();
 858			
 859			foreach ($currentUser->roles as $role)
 860			{
 861				$fields = wp_parse_args($fields, get_custom_user_fields( $role ));
 862			}
 863		}
 864		
 865		//reasons to fail
 866		if (empty($fields)) return false;
 867		
 868		// Use nonce for verification
 869		echo '<div class="nonce_wrapper"><input type="hidden" name="user_meta_box_nonce" value="',
 870			wp_create_nonce(basename(__FILE__)), '" /></div>',
 871			'<input type="hidden" name="user_type" value="',BRequest::getVar('user_type'),'" />';
 872		
 873	    foreach ($fields as $field) 
 874	    {
 875	    	if (!current_user_can('edit_users') && $field['id'] == 'role')
 876	    	{
 877	    		continue;
 878	    	}
 879	    	
 880	        // get current post meta data
 881	        $unique = md5(microtime());
 882	        if (!is_null($user) && isset($user->{$field['id']}))
 883	        {
 884	        	$meta = $user->{$field['id']};
 885	        }
 886	        elseif(!is_null($user))
 887	        {
 888	        	$meta = get_user_meta($user->ID, $field['id'], true);
 889	        }
 890	        else
 891	        {
 892	        	$meta = BRequest::getVar($field['id'], '');
 893	        }
 894	        
 895			echo '<div class="field_wrapper div', $field['id'], ' type',$field['type'],'">';
 896			if ($field['type'] != 'password') echo '<label for="', $field['id'], '">', $field['name'], '</label>';
 897	        
 898	        switch ($field['type'])
 899	        {
 900	            case 'password':
 901	                echo 
 902	                '<label for="', $field['id'], '">', $field['name'], '</label>',
 903	                '<input type="password" name="pass1" id="pass1" size="16" value="" autocomplete="off">',
 904	                '<span class="description">If you would like to change the password type a new one. Otherwise leave this blank.</span><br>',
 905	                '<input type="password" name="pass2" id="pass2" size="16" value="" autocomplete="off">',
 906	                '<span class="description">Type your new password again.</span><br>',
 907	                '<div id="pass-strength-result">Strength indicator</div>',
 908	                '<p class="description indicator-hint">Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ &amp; ).</p>',
 909	                '<script type="text/javascript"> /* <![CDATA[ */
 910					var pwsL10n = {
 911						empty: "Strength indicator",
 912						short: "Very weak",
 913						bad: "Weak",
 914						good: "Medium",
 915						strong: "Strong",
 916						mismatch: "Mismatch"
 917					};
 918					try{convertEntities(pwsL10n);}catch(e){};
 919					/* ]]> */
 920					</script>',
 921					'<script type="text/javascript" src="',get_bloginfo('url'),'/wp-admin/load-scripts.php?c=1&load=jquery,hoverIntent,common,jquery-color,user-profile,password-strength-meter"></script>';
 922	                break;
 923	            case 'address':
 924	                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', "\n", 
 925	                '<span class="profile_description">',$field['desc'],'</span>';
 926	                break;
 927	                
 928				case 'email':
 929					echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" class="regular-text" />', "\n";
 930					echo '<input type="text" name="', $field['id'], '1" id="', $field['id'], '" value="', ($default = "Please confirm your email"), 
 931					'" class="regular-text" onBlur="if (this.value == \'\') this.value = \'',$default,'\';"  onFocus="if (this.value == \'',$default,'\') this.value = \'\';" />', "\n", 
 932					'<span class="profile_description">',$field['desc'],'</span>';
 933					break;
 934					
 935	            case 'text':
 936	            	$disabled = '';
 937	            	if (is_user_logged_in() && $field['id'] == 'user_login') $disabled = 'readonly="true"';
 938	                echo '<input ',$disabled,' type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" class="regular-text" />', "\n", 
 939	                '<span class="profile_description">', $field['desc'], '</span>';
 940	                break;
 941	                
 942	            case 'textarea':
 943	                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="30" rows="5">', $meta ? $meta : $field['std'], '</textarea>', "\n", 
 944	                '<span class="profile_description">', $field['desc'], '</span>';
 945	                break;
 946	                
 947	            case 'select':
 948	                echo '<select name="', $field['id'], '" id="', $field['id'], '">';
 949	                if (!is_array($field['options']))
 950	                {
 951	                	$field['options'] = $field['options']();
 952	                }
 953	        		foreach ($field['options'] as $key => $option)
 954	        		{
 955	        			if (is_int($key)) $key = $option;
 956	                    echo '<option ', $meta == $option ? ' selected="selected"' : '', 
 957	        			' value="',$key,'">', $option, '</option>';
 958	        		}
 959	                echo '</select>';
 960	                break;
 961	                
 962	            case 'radio':
 963	                foreach ($field['options'] as $option)
 964	                {
 965	                    echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
 966	                }
 967	                echo '<br/>',$field['desc'];
 968	                break;
 969	                
 970	            case 'checkbox':
 971	                echo '<input type="hidden" name="', $field['id'], '" value="" /> ';
 972	                echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', ($meta && $meta != 'false') ? ' checked="checked"' : '', ' />',
 973	                '<span class="profile_description">', $field['desc'], '</span>';
 974	                break;
 975	                
 976	            case 'editor':
 977	            	echo 
 978	                '<div style="border:1px solid #DFDFDF;border-collapse: separate;border-top-left-radius: 6px 6px;border-top-right-radius: 6px 6px;">',
 979	                	'<textarea rows="10" class="theEditor" cols="40" name="', $field['id'], '" id="'.$unique.'"></textarea>',
 980	                '</div>', 
 981	                '<script type="text/javascript">edCanvas = document.getElementById(\''.$unique.'\');</script>', "\n", $field['desc'];
 982	                break;
 983	        }
 984	        
 985	        echo '</div>';
 986	    }
 987	}
 988	
 989	/**
 990	 * Prints the users profile link
 991	 * 
 992	 * @param unknown_type $user_id
 993	 * @return null
 994	 * @since 1.2
 995	 */
 996	function profile_url( $user_id = null )
 997	{
 998		echo get_profile_url($user_id);
 999	}
1000	
1001	/**
1002	 * Checks to see if there's a user type
1003	 * 
1004	 */
1005	function is_user_type()
1006	{
1007		if ($type = BRequest::getVar('user_type',false)) 
1008			return $type;
1009		return false;
1010	}
1011	
1012	/**
1013	 * Checks to see if the logged in user is the post owner
1014	 *
1015	 * @return unknown
1016	 */
1017	function is_post_owner()
1018	{
1019		//initializing variables
1020		global $authordata;
1021		$user =& get_user();
1022		
1023		if (!is_object($user)) return false;
1024		if (!is_object($authordata)) return false;
1025		if ($authordata->ID != $user->ID) return false;
1026		return true;
1027	}
1028	
1029	/**
1030	 * Count the posts
1031	 * 
1032	 * @param $author_id
1033	 * @param $post_type
1034	 * @return array
1035	 */
1036	function user_is( $role = null )
1037	{
1038		if (strtolower($role) != strtolower(get_user_role(null, true))) return false;
1039		return true;
1040	}
1041endif;
1042