<?php
/**
 * Add custom user fields in the user profile page
*/
function franz_show_custom_user_fields( $user ){ 
	global $current_user;
	?>
	<h3><?php _e( 'Franz Josef-specific User Profile Information', 'franz' ); ?></h3>
    <p><?php _e( 'Additional user profile fields used in the Franz Josef theme.', 'franz' ); ?></p>
    <table class="form-table">
    	<tr>
        	<th>
            	<label for="author_imgurl"><?php _e( 'Author image URL', 'franz' ); ?></label><br />
            </th>
            <td>
            	<input type="text" name="franz_author_imgurl" id="franz_author_imgurl" value="<?php echo esc_attr( get_user_meta( $user->ID, 'franz_author_imgurl', true ) ); ?>" class="code" size="75" /><br />
                <?php /* translators: %1$s will be replaced by link to gravatar.com */ ?>
                <span class="description">
					<?php printf( __( 'Use this option to override your %1$s. Recommended size is 150px square.', 'franz' ), '<a href="http://www.gravatar.com">Gravatar</a>' ); ?>
                </span>
            </td>
        </tr>
        <tr>
        	<th>
            	<label for="author_imgurl"><?php _e( 'Profile header image URL', 'franz' ); ?></label><br />
            </th>
            <td>
            	<input type="text" name="franz_author_header_imgurl" id="franz_author_header_imgurl" value="<?php echo esc_attr( get_user_meta( $user->ID, 'franz_author_header_imgurl', true ) ); ?>" class="code" size="75" /><br />
                <?php /* translators: %1$s will be replaced by link to the user's profile page */ ?>
                <span class="description">
					<?php printf( __( 'Header image for your %1$s. Recommended size is 1920x350 pixels.', 'franz' ), '<a href="' . get_author_posts_url( $user->ID ) . '">profile page</a>' ); ?>
                </span>
            </td>
        </tr>
        <tr>
        	<th>
            	<label for="franz_author_location"><?php _e( 'Current location', 'franz' ); ?></label><br />
            </th>
            <td>
            	<input type="text" name="franz_author_location" id="franz_author_location" value="<?php echo esc_attr( get_user_meta( $user->ID, 'franz_author_location', true ) ); ?>" class="code" size="75" /><br />
                <span class="description">
					<?php _e( 'Will be displayed on your front-facing author profile page. Recommended to use "City, Country" format, e.g. "Lisbon, Portugal".', 'franz' ); ?>
                </span>
            </td>
        </tr>
        <tr>
        	<th><?php _e( 'Social media URLs', 'franz' ); ?></th>
            <td>
            	<?php 
					$social_media = array( 
						'facebook' 		=> 'Facebook', 
						'twitter'		=> 'Twitter', 
						'google-plus'	=> 'Google+', 
						'linkedin' 		=> 'LinkedIn',
						'pinterest'		=> 'Pinterest',
						'youtube'		=> 'YouTube',
						'instagram'		=> 'Instagram',
						'github'		=> 'Github'
					);
					$social_media = apply_filters( 'franz_author_social_media', $social_media );
					
					$current_values = get_user_meta( $user->ID, 'franz_author_social', true );
					foreach ( $social_media as $name => $label ) :
				?>
                    <p>
                        <label for="franz_author_social_<?php echo $name; ?>" style="display:inline-block;width:100px"><?php echo $label; ?></label>
                        <input type="text" name="franz_author_social[<?php echo $name; ?>]" id="franz_author_social_<?php echo $name; ?>" value="<?php if ( isset( $current_values[$name] ) ) echo esc_attr( $current_values[$name] ); ?>" class="code" size="75" />
                    </p>
                <?php endforeach; ?>
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'franz_show_custom_user_fields' );
add_action( 'edit_user_profile', 'franz_show_custom_user_fields' );


/**
 * Save the custom user fields
*/
function franz_save_custom_user_fields( $user_id ){
	if ( ! current_user_can( 'edit_user', $user_id ) ) return false;
	update_user_meta( $user_id, 'franz_author_imgurl', esc_url_raw( $_POST['franz_author_imgurl'] ) );
	update_user_meta( $user_id, 'franz_author_header_imgurl', esc_url_raw( $_POST['franz_author_header_imgurl'] ) );
	update_user_meta( $user_id, 'franz_author_location', $_POST['franz_author_location'] );
	update_user_meta( $user_id, 'franz_author_social' . $social_media, $_POST['franz_author_social'] );
}
add_action( 'personal_options_update', 'franz_save_custom_user_fields' );
add_action( 'edit_user_profile_update', 'franz_save_custom_user_fields' );