<?php

/*
Plugin Name: My Profile
Plugin URI: http://extendpress.net/my-profile-profile-avatar-wordpress-plugin/
Description: Easily add a user profile & avatar editing page to your WordPress website.
Version: 2.1
Author: <a href="http://extendpress.net/author/bangingthemes">Banging Themes</a>
*/

function user_profile_refresh_function(){
	wp_redirect( get_permalink().'?mymsg=s' );
}

/* ========================= SHORTCODES ============================== */

// Add the shortcodes
add_shortcode( 'user-profile-edit', 'user_profile_edit_func' );
add_shortcode( 'user_photo', 'user_photo_shortcode_func' );

add_shortcode( 'my-profile-display-user-info', 'my_profile_display_user_info_func' );
add_shortcode( 'my-profile-display-user-image', 'my_profile_display_user_image_func' );

// Get ready for user input
add_action( 'init', 'check_user_input' );

require_once(ABSPATH . 'wp-admin/includes/user.php');
// require_once(ABSPATH . WPINC . '/registration.php');

// user input
function check_user_input(){
	if(is_user_logged_in()){
		if(!empty($_POST['banging_my_profile'])){
			$to_return = ''; global $wpdb, $userdata, $user_success;
			// Check for input
			$wpdb->hide_errors(); nocache_headers();
			get_currentuserinfo(); // Grabs the user info and puts into vars
			$user_ID = $userdata->ID;
			check_admin_referer('update-profile_' . $user_ID);
			// Standars WP user data
			$errors = edit_user($user_ID);
			// Check for custom fields
			if(isset($_POST["custom"])){
				// Cycle through each custom field
				foreach($_POST["custom"] as $key => $value){
					update_user_meta($user_ID, $key, $value);
				}
			}
			// If error then act on
			if( is_wp_error( $errors ) ) {
				foreach( $errors->get_error_messages() as $message )
				$errmsg = "$message";
			}
			// if there are no errors, then process the ad updates
			 if (!empty($errmsg) == ''){
				$user_success = '<div class="update_success">' . __("Profile updated successfully", "Gameleon") . '</div>';
				do_action('personal_options_update');
				add_action('init', 'user_profile_refresh_function');
			} else {
				$user_success = '<div class="update_fail">'.$errmsg.'</div>';
			}
		}
	}
}

// The user profile editing shortcode
function user_profile_edit_func( $atts ){
	
	extract( shortcode_atts( array(
		'customfields' => '',
		'customnames' => ''
	), $atts ) );
	
	if(is_user_logged_in()){
	
	$to_return = ''; global $wpdb, $userdata, $user_success;
	// Check for input
	$wpdb->hide_errors(); nocache_headers();
	get_currentuserinfo(); // Grabs the user info and puts into vars
	$user_ID = $userdata->ID;
	
	if(isset($_GET['mymsg']) && $_GET['mymsg']=='s'){
		$to_return .= '<div class="update_success">' . __("Profile updated successfully", "Gameleon") . '</div>';
	}
	
	// If success message
	$to_return .= $user_success;
	
	// Build form to send back to page
	$to_return .= '<form class="profile_form" name="profile" method="POST">';
	$to_return .= wp_nonce_field('update-profile_' . $user_ID);
	$to_return .= '	<input type="hidden" name="from" value="profile" />';
	$to_return .= '	<input type="hidden" name="action" value="profile" />';
	$to_return .= '	<input type="hidden" name="checkuser_id" value="'.$user_ID.'" />';
	$to_return .= '	<input type="hidden" name="dashboard_url" value="'.get_option("dashboard_url").'" />';
	$to_return .= '	<input type="hidden" name="user_id" id="user_id" value="'.$user_ID.'>" />';
	$to_return .= '	<table>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="user_login">' . __("Username", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" class="disabled" name="user_login" id="user_login" value="'.$userdata->user_login.'" size="35" maxlength="100" disabled /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="first_name">' . __("First Name", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="first_name" id="first_name" value="'.$userdata->first_name.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="last_name">' . __("Last Name", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="last_name" id="last_name" value="'.$userdata->last_name.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="email">' . __("Email", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="email" id="email" value="'.$userdata->user_email.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="position">' . __("Position", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="position" id="position"  value="'.$userdata->position.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="position">' . __("Company Name", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="company" id="company"  value="'.$userdata->company.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="url">' . __("Website URL", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="url" id="url" value="'.$userdata->user_url.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="twitter">' . __("Twitter URL", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="twitter" id="twitter" value="'.$userdata->twitter.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="facebook">' . __("Facebook Url", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="facebook" id="facebook" value="'.$userdata->facebook.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="yim">' . __("Yahoo IM", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="yim" id="yim"  value="'.$userdata->yim.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="google">' . __("Google+ URL", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="google" id="google"  value="'.$userdata->google.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="youtube">' . __("Youtube URL", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="youtube" id="youtube"  value="'.$userdata->youtube.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="dribbble">' . __("Dribbble URL", "Gameleon") . '</label></th>';
	$to_return .= '			<td><input type="text" name="dribbble" id="dribbble"  value="'.$userdata->dribbble.'" size="35" maxlength="100" /></td>';
	$to_return .= '		</tr>';
	$to_return .= '		<tr>';
	$to_return .= '			<th><label for="description">' . __("About me", "Gameleon") . '</label></th>';
	$to_return .= '			<td><textarea name="description" id="description" rows="8">'.$userdata->description.'</textarea></td>';
	$to_return .= '		</tr>';

	$to_return .= '	</table>';
	$show_password_fields = apply_filters('show_password_fields', true);
    if($show_password_fields){
		$to_return .= '	<table>';
		$to_return .= '		<tr>';
		$to_return .= '			<th><label class="pass" for="pass1">' . __("New Password", "Gameleon") . '</label></th>';
		$to_return .= '			<td>';
		$to_return .= '				<input type="password" name="pass1" id="pass1" size="35" maxlength="50" placeholder="Leave blank to keep same password" value="" />';  
		$to_return .= '			</td>';
		$to_return .= '		</tr>';
		$to_return .= '		<tr>';
		$to_return .= '			<th><label class="pass" for="pass2">' . __("Password Again", "Gameleon") . '</label></th>';
		$to_return .= '			<td>';
		$to_return .= '				<input type="password" name="pass2" id="pass2" size="35" maxlength="50" placeholder="Type your new password again" value="" />';
		$to_return .= '			</td>';
		$to_return .= '		</tr>';
$to_return .= '	</table>';
	}
	$to_return .= '	<input type="hidden" name="banging_my_profile" value="1" />';
	$to_return .= '	<input type="submit" class="profile_update" value="Update Profile" />';
	$to_return .= '	<div class="clear"></div>';
	$to_return .= '</form>';
	
	} else {
		
		$to_return = '<div id="my-profile-must-sign-in">' . __("You must be logged in to", "Gameleon") . ' <strong>' . __("edit your profile", "Gameleon") . '</strong></div>';
		
	}

	return $to_return;
}




// Add shortcode for user photo
function user_photo_shortcode_func( $atts ){
	
	if(is_user_logged_in()){
	
	// Find out who the current user is
	$current_user = wp_get_current_user(); $profileuser = $current_user; global $userphoto_error;
	// Find out if User Photo is installed
	if(function_exists('userphoto_display_selector_fieldset')){
		// Update if needed
		userphoto_profile_update($current_user->ID);
		$isSelf = $profileuser->ID == $current_user->ID;
		global $wp_version;
        $toreturn = '';
		$toreturn .= '<form method="POST" id="your-photo" enctype="multipart/form-data">';
		$tablestart = '<table><tr><td>';
		if($profileuser->userphoto_image_file){
			if(!$current_user->has_cap('edit_users')){
				if($profileuser->userphoto_approvalstatus == USERPHOTO_PENDING){
						$toreturn .= '<p id="userphoto-status-pending">' . __("Your profile photo has been submitted for review", "Gameleon") . '</p>';
				} else if($profileuser->userphoto_approvalstatus == USERPHOTO_REJECTED){
						$toreturn .= '<p id="userphoto-status-rejected"><strong>Notice: </strong>' . __("Your chosen profile photo has been rejected", "Gameleon") . '';
						if($profileuser->userphoto_rejectionreason){
							$toreturn .= 'Reason: '.htmlspecialchars($profileuser->userphoto_rejectionreason);
						}
						$toreturn .= '</p>';
				}
			}
			$upload_dir = wp_upload_dir();
			$bdir = trailingslashit($upload_dir['baseurl']) . 'userphoto/';
			$toreturn .= $tablestart;
			$toreturn .= '<span id="my-image" style="position:relative;"><img src="'.$bdir . $profileuser->userphoto_image_file . "?" . rand().'" alt="Thumbnail image" style="width:100px;float:left;" />';
			$toreturn .= '<input type="image" src="'.get_template_directory_uri() .'/includes/plugins/my-profile/img/delete.png" value="Delete Image" onclick="this.form.userphoto_delete.checked=true;" style="position:absolute;top:0;right:0;padding:5px;" title="Click To Delete Image" /></span>';
		} else {
			$toreturn .= $tablestart;
			$toreturn .= '<img alt="" src="http://1.gravatar.com/avatar/3daef0dcbc01b39144837ace456327bb?s=58&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D58&amp;r=G" class="avatar avatar-58 photo" style="width:100px;">';
		}
		if($profileuser->userphoto_error){
			$toreturn .= '<p id="userphoto-upload-error"><strong>' . __("Upload error:", "Gameleon") . '</strong> '.$profileuser->userphoto_error.'</p>';
		}
		$toreturn .= '</td><td><div id="FileUpload">';

		$toreturn .= '<input type="file" size="110" name="userphoto_image_file" id="userphoto_image_file" onchange="this.form.submit()"/>';
		$toreturn .= '<div id="BrowserVisible"><input type="text" id="FileField" /></div></div></td></tr></table>';
	
	if($profileuser->userphoto_image_file){ $toreturn .= '<input type="checkbox" name="userphoto_delete" id="userphoto_delete" style="display:none;" />'; }
		$toreturn .= '</form>';
	} else {
		// WordPress website doesn't have User Photo installed
		$toreturn = 'The user_photo shortcode relies on another free plugin to operate call <a href="http://wordpress.org/extend/plugins/user-photo/" title="User Photo free WordPress plugin">User Photo</a>. Download and install it for free from WordPress.org and this section will turn into a photo upload form.';
	}
	return $toreturn;
	
	} else {
		// user not logged in
		return '<div class="mustlog-img">' . __("You must be logged in to edit your profile image", "Gameleon") . '</div>';
	}
}


// Add shortcode for returning user details
function my_profile_display_user_info_func( $atts ){
	extract( shortcode_atts( array(
		'field' => 'user_login'
	), $atts ) );
	
	if(is_user_logged_in()){
		// Get user ID
		global $current_user; get_currentuserinfo();
		// Return the avatar back to the page
		return $current_user->$field;
	}
}

// Add shortcode for returning user image
function my_profile_display_user_image_func( $atts ){
	extract( shortcode_atts( array(
		'default' => '',
		'align' => '',
		'padding' => 0,
		'margin' => 0
	), $atts ) );
	if(is_user_logged_in()){
		// Set up default styling
		if($align!=''){ $align_string = 'float:'.$align.';'; }
		if($padding!=0){ $padding_string = 'padding:'.$padding.'px;'; }
		if($margin!=0){ $margin_string = 'margin:'.$margin.'px;'; }
		// Get user ID
		global $current_user; get_currentuserinfo();
		// Return the avatar back to the page
		$avatar_return = get_avatar( $current_user->ID, 96, $default );
		if($align!='' || $padding!=0 || $margin!=0){ $avatar_return = str_replace('img','img style="'.$align_string.$padding_string.$margin_string.'"',$avatar_return); }
		return $avatar_return;
	}
}


// ====================== HIDE SOME PROFILE FIELDS ====================== //


add_filter('user_contactmethods','hide_profile_fields',10,1);

function hide_profile_fields( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
return $contactmethods;
}
?>