コード例 #1
0
function uni_user_avatar_plugin_function()
{
    ?>
<div class="wrap">
<h2><?php 
    _e('Uni Avatar Plugin Options Page', 'uni-avatar');
    ?>
</h2>

<form method="post" action="options.php">
    <?php 
    settings_fields('uni-avatar-settings-group');
    ?>
    <?php 
    do_settings_sections('uni-avatar-settings-group');
    ?>

    <h3><?php 
    _e('General options', 'uni-avatar');
    ?>
</h3>

    <table class="form-table">
        <tr valign="top">
            <th scope="row">
                <?php 
    _e('Enable custom avatars everywhere on the website?', 'uni-avatar');
    ?>
            </th>
            <td>
                <input type="checkbox" name="uni_avatar_enable_gravatar_override" value="1"<?php 
    echo checked(get_option('uni_avatar_enable_gravatar_override'), 1);
    ?>
 />
                <p class="description"><?php 
    _e('Check this option to enable custom avatars everywhere on the website and gravatars will be replaced. Or uncheck this and you will be able to use custom avatars only by using plugin\'s shortcode [uav-display-avatar].', 'uni-avatar');
    ?>
</p>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row">
                <?php 
    _e('Enable support of the plugin\'s upload form on the website?', 'uni-avatar');
    ?>
            </th>
            <td>
                <input type="checkbox" name="uni_upload_form_in_front" value="1"<?php 
    echo checked(get_option('uni_upload_form_in_front'), 1);
    ?>
 />
                <p class="description"><?php 
    _e('It is a very important option! You have to enable this only if you want to use an upload avatar form someshere on your website in front end. This option is disabled by default to prevent of loading some not needed scripts (they are really not needed if you don\'t use plugin\'s form for uploading avatars in the front end)', 'uni-avatar');
    ?>
</p>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row"><?php 
    _e('Choose default avatar image dimmensions. Default is 48 px.', 'uni-avatar');
    ?>
</th>
            <td>
                <div style="display:block;float:left;margin-right:20px;">
                        <input type="text" name="uni_avatar_default_dimm" value="<?php 
    echo get_option('uni_avatar_default_dimm') ? get_option('uni_avatar_default_dimm') : UNI_AVATAR_DEFAULT_DIMM;
    ?>
" />
                </div>
                <div style="clear:both;"></div>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row"><?php 
    _e('Choose default avatar image size (Kb). Default is 300 Kb.', 'uni-avatar');
    ?>
</th>
            <td>
                <div style="display:block;float:left;margin-right:20px;">
                        <input type="text" name="uni_avatar_default_size" value="<?php 
    echo get_option('uni_avatar_default_size') ? get_option('uni_avatar_default_size') : UNI_AVATAR_DEFAULT_SIZE;
    ?>
" />
                </div>
                <div style="clear:both;"></div>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row"><?php 
    _e('Choose default avatar image', 'uni-avatar');
    ?>
</th>
            <td>
            <?php 
    $aDefaultAvatars = uni_avatar_default_avatars_array();
    foreach ($aDefaultAvatars as $sSlug => $sTitle) {
        ?>
                <div style="display:block;float:left;margin-right:20px;">
                    <img style="display:block;margin:0;border:1px solid grey;" src="<?php 
        echo UNI_AVATAR_WP_PLUGIN_URL . 'css/images/user-avatar-default_' . $sSlug . '.png';
        ?>
" width="150" height="150" />
                    <label>
                        <input type="radio" name="uni_avatar_default_avatar_image" value="<?php 
        echo $sSlug;
        ?>
"<?php 
        echo checked(get_option('uni_avatar_default_avatar_image'), $sSlug);
        ?>
 />
                        <?php 
        echo $sTitle;
        ?>
                    </label>
                </div>
            <?php 
    }
    ?>
                <div style="clear:both;"></div>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row">
                <?php 
    _e('Enable different avatars depending on user\'s gender', 'uni-avatar');
    ?>
            </th>
            <td>
                <input type="checkbox" name="uni_avatar_gender_mode" value="1"<?php 
    echo checked(get_option('uni_avatar_gender_mode'), 1);
    ?>
 />
                <p class="description"><?php 
    _e('WP itself doesn\'t have feature of choosing users\'s gender! However this plugin adds this feature. User\'s gender can be chosen on his profile page.', 'uni-avatar');
    ?>
</p>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row"><?php 
    _e('Choose one of the predefined avatar image sets with male/female differentiation (displays if male/female differentiation is enabled)', 'uni-avatar');
    ?>
</th>
            <td>
            <?php 
    $aPredefinedAvatars = uni_avatar_predefined_avatars_array();
    foreach ($aPredefinedAvatars as $sSlug => $sTitle) {
        ?>
                <div style="display:block;float:left;margin-right:20px;">
                    <img style="margin:0;border:1px solid grey;" src="<?php 
        echo UNI_AVATAR_WP_PLUGIN_URL . 'css/images/user-avatar-male-' . $sSlug . '.png';
        ?>
" width="150" height="150" />
                    <img style="margin:0;border:1px solid grey;" src="<?php 
        echo UNI_AVATAR_WP_PLUGIN_URL . 'css/images/user-avatar-female-' . $sSlug . '.png';
        ?>
" width="150" height="150" />
                    <label style="display:block;">
                        <input type="radio" name="uni_avatar_predefined_avatars" value="<?php 
        echo $sSlug;
        ?>
"<?php 
        echo checked(get_option('uni_avatar_predefined_avatars'), $sSlug);
        ?>
 />
                        <?php 
        echo $sTitle;
        ?>
                    </label>
                </div>
            <?php 
    }
    ?>
                <div style="clear:both;"></div>
            </td>
        </tr>
        <tr valign="top" class="form-field">
        <th scope="row"><?php 
    _e('Custom uploaded avatars', 'uni-avatar');
    ?>
</th>
            <td>
			<label><?php 
    _e('Thumbnails', 'uni-avatar');
    ?>
</label><br><br>
            <div id="uni_avatar_thumb_container">
            <?php 
    $sAvatarIds = get_option('uni_avatar_custom_avatars');
    if (isset($sAvatarIds) && !empty($sAvatarIds)) {
        $aAvatarIds = explode(',', $sAvatarIds);
    }
    if (isset($aAvatarIds) && !empty($sAvatarIds)) {
        foreach ($aAvatarIds as $iImageId) {
            //$sAvatarImage = wp_get_attachment_thumb_url( $iImageId );
            ?>
			        <?php 
            /*<div class="uni_avatar_thumbnail" style="float:left;margin-right:10px;"><img src="<?php echo $sAvatarImage; ?>" width="100" height="100" /></div> */
            ?>
                    <div class="uni_avatar_thumbnail" style="float:left;margin-right:10px;"><?php 
            echo wp_get_attachment_image($iImageId, 'uni-avatar-thumb');
            ?>
</div>
            <?php 
        }
    }
    ?>
            </div>
            <div style="clear:both;"></div>
			<div style="line-height:60px;">
				<input type="hidden" id="uni_avatar_ids" name="uni_avatar_custom_avatars" value="<?php 
    echo $sAvatarIds;
    ?>
" />
				<button type="button" class="upload_image_button button"><?php 
    _e('Upload/Add avatars', 'uni-avatar');
    ?>
</button>
				<button type="button" class="remove_image_button button"><?php 
    _e('Remove avatars', 'uni-avatar');
    ?>
</button>
			</div>
			<script type="text/javascript">
				 // Only show the "remove image" button when needed
				 if ( ! jQuery('#uni_avatar_ids').val() )
					 jQuery('.remove_image_button').hide();

				// Uploading files
				var file_frame;

				jQuery(document).on( 'click', '.upload_image_button', function( event ){

					event.preventDefault();

					// If the media frame already exists, reopen it.
					if ( file_frame ) {
						file_frame.open();
						return;
					}

					// Create the media frame.
					file_frame = wp.media.frames.downloadable_file = wp.media({
						title: '<?php 
    _e('Choose images', 'uni-avatar');
    ?>
',
						button: {
							text: '<?php 
    _e('Use images', 'uni-avatar');
    ?>
',
						},
						multiple: true
					});

					// When an image is selected, run a callback.
					file_frame.on( 'select', function() {
					    selection = file_frame.state().get('selection');

                        if (!selection) {
                            return;
                        }
                        var aAttachments = [];
                        // iterate through selected elements
                        selection.each(function(attachment) {
                            //console.log( attachment );
                            jQuery('#uni_avatar_thumb_container').append('<div class="uni_avatar_thumbnail" style="float:left;margin-right:10px;"><img src="'+ attachment.attributes.url +'" width="100px" height="100px" /></div>');
                            aAttachments.push(attachment.id);
                        });

                        var sAttachments = aAttachments.toString();
                        //console.log( sAttachments );
                        jQuery('#uni_avatar_ids').val( sAttachments );
                        jQuery('.remove_image_button').show();

					});

					// Finally, open the modal.
					file_frame.open();
				});

				jQuery(document).on( 'click', '.remove_image_button', function( event ){
					jQuery('#uni_avatar_thumb_container').empty();
					jQuery('#uni_avatar_ids').val('');
					jQuery('.remove_image_button').hide();
					return false;
				});
			</script>
			<div class="clear"></div>
            </td>
        </tr>
    </table>

    <h3><?php 
    _e('Options for BuddyPress', 'uni-avatar');
    ?>
</h3>

    <?php 
    if (!function_exists('bp_is_active')) {
        ?>
        <p><?php 
        _e('It seems that you haven\'t activated BuddyPress yet!', 'uni-avatar');
        ?>
</p>
    <?php 
    }
    ?>

    <table class="form-table">
        <tr valign="top">
            <th scope="row">
                <?php 
    _e('Enable custom avatars for BuddyPress?', 'uni-avatar');
    ?>
            </th>
            <td>
                <input type="checkbox" name="uni_avatar_enable_buddypress_avatars_override" value="1"<?php 
    echo checked(get_option('uni_avatar_enable_buddypress_avatars_override'), 1);
    ?>
 />
                <p class="description"><?php 
    _e('Check this option to add support of this plugin to BuddyPress. Warning: enabling this option will disable photo upload form on BP user\'s profile page.', 'uni-avatar');
    ?>
</p>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row">
                <?php 
    _e('Enable avatar upload form on BP profile page?', 'uni-avatar');
    ?>
            </th>
            <td>
                <input type="checkbox" name="uni_avatar_enable_buddypress_form_override" value="1"<?php 
    echo checked(get_option('uni_avatar_enable_buddypress_form_override'), 1);
    ?>
 />
                <p class="description"><?php 
    _e('This option for those who enabled support of this plugin for BP and want to give their users an ability to change avatar any time.', 'uni-avatar');
    ?>
</p>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row">
                <?php 
    _e('Custom text above the upload form on "Change Avatar" profile tab', 'uni-avatar');
    ?>
            </th>
            <td>
                <textarea name="uni_avatar_enable_buddypress_form_text_above"><?php 
    echo get_option('uni_avatar_enable_buddypress_form_text_above');
    ?>
</textarea>
                <p class="description"><?php 
    _e('You can add custom text above the upload form. It can be a decription or whatever you want.', 'uni-avatar');
    ?>
</p>
            </td>
        </tr>
    </table>

    <?php 
    submit_button();
    ?>

</form>
</div>
<?php 
}
コード例 #2
0
 function get_user_avatar_image($avatar = '', $id_or_email, $iSize = null, $default = null, $alt = null, $args = null)
 {
     $aArgs = array();
     $aDefaults = array('size' => uni_avatar_get_avatars_size($iSize), 'crop' => true);
     $aArgs = wp_parse_args($aArgs, $aDefaults);
     $iUserId = '';
     if (is_numeric($id_or_email)) {
         $iUserId = absint($id_or_email);
     } elseif (is_string($id_or_email)) {
         $oUser = get_user_by('email', $id_or_email);
         $iUserId = $oUser->ID;
     } elseif (is_object($id_or_email)) {
         // No avatar for pingbacks or trackbacks
         $allowed_comment_types = apply_filters('get_avatar_comment_types', array('comment'));
         if (!empty($id_or_email->comment_type) && !in_array($id_or_email->comment_type, (array) $allowed_comment_types)) {
             return false;
         }
         if (!empty($id_or_email->user_id)) {
             $iUserId = (int) $id_or_email->user_id;
         }
     }
     $oUserData = get_userdata($iUserId);
     if (get_user_meta($iUserId, 'uni_user_avatar', true)) {
         $sUserAvatarId = get_user_meta($iUserId, 'uni_user_avatar', true);
         $aUserAvatar = wp_get_attachment_image_src($sUserAvatarId, 'full');
         $sUserAvatar = $aUserAvatar[0];
         $sImageUrl = bfi_thumb($sUserAvatar, $aArgs);
     } else {
         $sGender = get_user_meta($iUserId, '_uni_avatar_gender', true);
         if (get_option('uni_avatar_gender_mode') && isset($sGender) && !empty($sGender)) {
             $aPredefinedAvatars = uni_avatar_predefined_avatars_array();
             $sChosenPredefinedAvatar = get_option('uni_avatar_predefined_avatars');
             if ($sGender == 'male') {
                 $sDefaultImageUrl = UNI_AVATAR_WP_PLUGIN_URL . 'css/images/user-avatar-male-' . $sChosenPredefinedAvatar . '.png';
                 $sImageUrl = bfi_thumb($sDefaultImageUrl, $aArgs);
             } elseif ($sGender == 'female') {
                 $sDefaultImageUrl = UNI_AVATAR_WP_PLUGIN_URL . 'css/images/user-avatar-female-' . $sChosenPredefinedAvatar . '.png';
                 $sImageUrl = bfi_thumb($sDefaultImageUrl, $aArgs);
             }
         } else {
             $aDefaultAvatars = uni_avatar_default_avatars_array();
             $sChosenDefaultAvatar = get_option('uni_avatar_default_avatar_image');
             $sDefaultImageUrl = UNI_AVATAR_WP_PLUGIN_URL . 'css/images/user-avatar-default_' . $sChosenDefaultAvatar . '.png';
             $sImageUrl = bfi_thumb($sDefaultImageUrl, $aArgs);
         }
     }
     $sFinalSize = $aArgs['size'];
     $aAvatarClasses = array('uni-user-avatar-image', 'avatar', 'avatar-' . $sFinalSize, 'photo', 'avatar-default');
     $aAvatarClasses = apply_filters('uni_avatar_img_classes_filter', $aAvatarClasses);
     $sAvatarClasses = implode(' ', $aAvatarClasses);
     if (isset($alt) && !empty($alt)) {
         $sAvatar = '<img src="' . esc_url($sImageUrl) . '" data-user_id="' . $iUserId . '" width="' . $sFinalSize . '" height="' . $sFinalSize . '" class="' . esc_attr($sAvatarClasses) . '" alt="' . esc_attr($alt) . '" />';
     } else {
         $sAvatar = '<img src="' . esc_url($sImageUrl) . '" data-user_id="' . $iUserId . '" width="' . $sFinalSize . '" height="' . $sFinalSize . '" class="' . esc_attr($sAvatarClasses) . '" alt="' . esc_attr($oUserData->display_name) . '" />';
     }
     return $sAvatar;
 }