wp-user-avatar
Last commit date
css
13 years ago
images
13 years ago
js
13 years ago
index.html
13 years ago
readme.txt
13 years ago
wp-user-avatar.php
13 years ago
wp-user-avatar.php
471 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package WP User Avatar |
| 4 | * @version 1.2 |
| 5 | */ |
| 6 | /* |
| 7 | Plugin Name: WP User Avatar |
| 8 | Plugin URI: http://wordpress.org/extend/plugins/wp-user-avatar/ |
| 9 | Description: Use any image from your WordPress Media Library as a custom user avatar. |
| 10 | Version: 1.2 |
| 11 | Author: Bangbay Siboliban |
| 12 | Author URI: http://siboliban.org/ |
| 13 | */ |
| 14 | |
| 15 | // Define paths and variables |
| 16 | define('WP_USER_AVATAR_FOLDER', basename(dirname(__FILE__))); |
| 17 | define('WP_USER_AVATAR_ABSPATH', trailingslashit(str_replace('\\','/', WP_PLUGIN_DIR.'/'.WP_USER_AVATAR_FOLDER))); |
| 18 | define('WP_USER_AVATAR_URLPATH', trailingslashit(plugins_url(WP_USER_AVATAR_FOLDER))); |
| 19 | |
| 20 | // Initialize default settings |
| 21 | register_activation_hook(__FILE__, 'wp_user_avatar_options'); |
| 22 | |
| 23 | // Remove user metadata on plugin delete |
| 24 | register_uninstall_hook(__FILE__, 'wp_user_avatar_delete_setup'); |
| 25 | |
| 26 | // Settings saved to wp_options |
| 27 | function wp_user_avatar_options(){ |
| 28 | add_option('avatar_default_wp_user_avatar',''); |
| 29 | } |
| 30 | add_action('init', 'wp_user_avatar_options'); |
| 31 | |
| 32 | // Remove user metadata |
| 33 | function wp_user_avatar_delete_setup(){ |
| 34 | $users = get_users(); |
| 35 | foreach($users as $user){ |
| 36 | delete_user_meta($user->ID, 'wp_user_avatar'); |
| 37 | } |
| 38 | delete_option('avatar_default_wp_user_avatar'); |
| 39 | update_option('avatar_default', 'mystery'); |
| 40 | } |
| 41 | |
| 42 | // WP User Avatar |
| 43 | if(!class_exists('wp_user_avatar')){ |
| 44 | class wp_user_avatar{ |
| 45 | function wp_user_avatar(){ |
| 46 | add_action('show_user_profile', array('wp_user_avatar','action_show_user_profile')); |
| 47 | add_action('edit_user_profile', array($this,'action_show_user_profile')); |
| 48 | add_action('personal_options_update', array($this,'action_process_option_update')); |
| 49 | add_action('edit_user_profile_update', array($this,'action_process_option_update')); |
| 50 | if(!function_exists('wp_enqueue_media')){ |
| 51 | add_filter('attachment_fields_to_edit', array($this,'add_wp_user_avatar_attachment_field_to_edit'), 10, 2); |
| 52 | } |
| 53 | if(get_option('show_avatars') != '1'){ |
| 54 | add_filter('manage_users_columns', array($this,'add_wp_user_avatar_column'), 10, 1); |
| 55 | add_filter('manage_users_custom_column', array($this,'show_wp_user_avatar_column'), 10, 3); |
| 56 | } |
| 57 | add_action('admin_enqueue_scripts', array($this,'media_upload_scripts')); |
| 58 | } |
| 59 | |
| 60 | // Add to user profile edit |
| 61 | function action_show_user_profile($user){ |
| 62 | $wp_user_avatar = get_user_meta($user->ID, 'wp_user_avatar', true); |
| 63 | $hide_notice = has_wp_user_avatar($user->ID) ? ' style="display:none;"' : ''; |
| 64 | $hide_remove = !has_wp_user_avatar($user->ID) ? ' style="display:none;"' : ''; |
| 65 | ?> |
| 66 | <h3><?php _e('WP User Avatar') ?></h3> |
| 67 | <table class="form-table"> |
| 68 | <tbody> |
| 69 | <tr> |
| 70 | <th><label for="wp_user_avatar"><?php _e('WP User Avatar'); ?></label></th> |
| 71 | <td> |
| 72 | <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wp_user_avatar; ?>" /> |
| 73 | <p><button type="button" class="button" id="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p> |
| 74 | <div id="wp-user-avatar-preview"> |
| 75 | <p> |
| 76 | <?php |
| 77 | if(has_wp_user_avatar($user->ID)){ |
| 78 | echo get_wp_user_avatar($user->ID, 'medium'); |
| 79 | } else { |
| 80 | if(get_option('show_avatars') == '1'){ |
| 81 | echo get_avatar($user->ID, 96); |
| 82 | } else { |
| 83 | echo '<img src="'.includes_url().'images/blank.gif" alt="" />'; |
| 84 | } |
| 85 | } |
| 86 | ?> |
| 87 | </p> |
| 88 | </div> |
| 89 | <?php if(get_option('show_avatars') == '1') : ?> |
| 90 | <p id="gravatar-notice"<?php echo $hide_notice; ?>>This is your <a href="http://gravatar.com/" target="_blank">gravatar.com</a> avatar.</p> |
| 91 | <?php endif; ?> |
| 92 | <p><button type="button" class="button" id="remove-wp-user-avatar"<?php echo $hide_remove; ?>><?php _e('Remove'); ?></button></p> |
| 93 | <p id="wp-user-avatar-message"><?php _e('Press "Update Profile" to save your changes.'); ?></p> |
| 94 | </td> |
| 95 | </tr> |
| 96 | </tbody> |
| 97 | </table> |
| 98 | <script type="text/javascript"> |
| 99 | jQuery(function($){ |
| 100 | <?php if(function_exists('wp_enqueue_media')) : // Use Backbone uploader for WP 3.5+ ?> |
| 101 | wp.media.wpUserAvatar = { |
| 102 | get: function() { |
| 103 | return wp.media.view.settings.post.wpUserAvatarId; |
| 104 | }, |
| 105 | set: function(id) { |
| 106 | var settings = wp.media.view.settings; |
| 107 | settings.post.wpUserAvatarId = id; |
| 108 | settings.post.wpUserAvatarSrc = $('div.attachment-info').find('img').attr('src'); |
| 109 | if(settings.post.wpUserAvatarId) |
| 110 | setWPUserAvatar(settings.post.wpUserAvatarId, settings.post.wpUserAvatarSrc); |
| 111 | }, |
| 112 | frame: function(){ |
| 113 | if(this._frame) |
| 114 | return this._frame; |
| 115 | this._frame = wp.media({ |
| 116 | state: 'library', |
| 117 | states: [ new wp.media.controller.Library({ title: "Edit WP User Avatar: <?php echo $user->display_name; ?>" }) ] |
| 118 | }); |
| 119 | this._frame.on('open', function(){ |
| 120 | var selection = this.state().get('selection'); |
| 121 | id = jQuery('#wp-user-avatar').val(); |
| 122 | attachment = wp.media.attachment(id); |
| 123 | attachment.fetch(); |
| 124 | selection.add(attachment ? [ attachment ] : []); |
| 125 | }, this._frame); |
| 126 | this._frame.on('toolbar:create:select', function(toolbar){ |
| 127 | this.createSelectToolbar(toolbar, { |
| 128 | text: 'Set WP User Avatar' |
| 129 | }); |
| 130 | }, this._frame); |
| 131 | this._frame.state('library').on('select', this.select); |
| 132 | return this._frame; |
| 133 | }, |
| 134 | select: function(id) { |
| 135 | var settings = wp.media.view.settings, |
| 136 | selection = this.get('selection').single(); |
| 137 | wp.media.wpUserAvatar.set(selection ? selection.id : -1); |
| 138 | }, |
| 139 | init: function() { |
| 140 | $('body').on('click', '#add-wp-user-avatar', function(e){ |
| 141 | e.preventDefault(); |
| 142 | e.stopPropagation(); |
| 143 | wp.media.wpUserAvatar.frame().open(); |
| 144 | }); |
| 145 | } |
| 146 | }; |
| 147 | $(wp.media.wpUserAvatar.init); |
| 148 | <?php else : // Fall back to Thickbox uploader ?> |
| 149 | $('#add-wp-user-avatar').click(function(e){ |
| 150 | e.preventDefault(); |
| 151 | tb_show('Edit WP User Avatar: <?php echo $user->display_name; ?>', 'media-upload.php?type=image&post_type=user&post_id=0&tab=library&TB_iframe=1'); |
| 152 | }); |
| 153 | <?php endif; ?> |
| 154 | }); |
| 155 | jQuery(function($){ |
| 156 | $('#remove-wp-user-avatar').click(function(e){ |
| 157 | <?php if(get_option('show_avatars') == '1') : ?> |
| 158 | var avatar_thumb = '<?php echo addslashes(get_avatar_original($user->ID, 96)); ?>'; |
| 159 | $('#gravatar-notice').show(); |
| 160 | <?php else : ?> |
| 161 | var avatar_thumb = '<img src="<?php echo includes_url(); ?>images/blank.gif" alt="" />'; |
| 162 | <?php endif; ?> |
| 163 | e.preventDefault(); |
| 164 | $(this).hide(); |
| 165 | $('#wp-user-avatar-preview').find('img').replaceWith(avatar_thumb); |
| 166 | $('#wp-user-avatar').val(''); |
| 167 | $('#wp-user-avatar-message').show(); |
| 168 | }); |
| 169 | }); |
| 170 | </script> |
| 171 | <?php |
| 172 | } |
| 173 | // Update user meta |
| 174 | function action_process_option_update($user_id){ |
| 175 | update_user_meta($user_id, 'wp_user_avatar', (isset($_POST['wp-user-avatar']) ? $_POST['wp-user-avatar'] : '')); |
| 176 | } |
| 177 | |
| 178 | // Add button to attach image |
| 179 | function add_wp_user_avatar_attachment_field_to_edit($fields, $post){ |
| 180 | $image = wp_get_attachment_image_src($post->ID, "medium"); |
| 181 | $button = '<button type="button" class="button" id="set-wp-user-avatar-image" onclick="setWPUserAvatar(\''.$post->ID.'\', \''.$image[0].'\')">Set WP User Avatar</button>'; |
| 182 | $fields['wp-user-avatar'] = array( |
| 183 | 'label' => __('WP User Avatar'), |
| 184 | 'input' => 'html', |
| 185 | 'html' => $button |
| 186 | ); |
| 187 | return $fields; |
| 188 | } |
| 189 | |
| 190 | // Add column to Users page |
| 191 | function add_wp_user_avatar_column($columns){ |
| 192 | return $columns + array('wp-user-avatar' => __('WP User Avatar'));; |
| 193 | } |
| 194 | |
| 195 | // Show thumbnail of wp_user_avatar |
| 196 | function show_wp_user_avatar_column($value, $column_name, $user_id){ |
| 197 | $wp_user_avatar = get_user_meta($user_id, 'wp_user_avatar', true); |
| 198 | $wp_user_avatar_image = wp_get_attachment_image($wp_user_avatar, array(32,32)); |
| 199 | if($column_name == 'wp-user-avatar'){ |
| 200 | return $wp_user_avatar_image; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Media uploader |
| 205 | function media_upload_scripts(){ |
| 206 | global $pagenow; |
| 207 | wp_enqueue_script('media-upload'); |
| 208 | wp_enqueue_script('thickbox'); |
| 209 | if(function_exists('wp_enqueue_media')){ |
| 210 | wp_enqueue_media(); |
| 211 | } |
| 212 | wp_enqueue_script('wp-user-avatar', WP_USER_AVATAR_URLPATH.'js/wp-user-avatar.js'); |
| 213 | wp_enqueue_style('thickbox'); |
| 214 | wp_enqueue_style('wp-user-avatar', WP_USER_AVATAR_URLPATH.'css/wp-user-avatar.css'); |
| 215 | } |
| 216 | } |
| 217 | // Initialize wp_user_avatar |
| 218 | global $wp_user_avatar_instance; |
| 219 | $wp_user_avatar_instance = new wp_user_avatar(); |
| 220 | } |
| 221 | |
| 222 | // Returns true if user has wp_user_avatar |
| 223 | function has_wp_user_avatar($user_id = ''){ |
| 224 | global $post; |
| 225 | if(empty($user_id)){ |
| 226 | $author_name = get_query_var('author_name'); |
| 227 | $user = is_author() ? get_user_by('slug', $author_name) : get_the_author_meta('ID'); |
| 228 | $user_id = $user->ID; |
| 229 | } |
| 230 | $wp_user_avatar = get_user_meta($user_id, 'wp_user_avatar', true); |
| 231 | if(!empty($wp_user_avatar)){ |
| 232 | return true; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Find wp_user_avatar, show get_avatar if empty |
| 237 | function get_wp_user_avatar($id_or_email = '', $size = '96', $align = ''){ |
| 238 | global $post, $comment; |
| 239 | // Find user ID on comment, author page, or post |
| 240 | if(is_object($id_or_email)){ |
| 241 | $id_or_email = $comment->user_id != '0' ? $comment->user_id : $comment->comment_author_email; |
| 242 | $alt = $comment->comment_author; |
| 243 | } else { |
| 244 | if(!empty($id_or_email)){ |
| 245 | $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email); |
| 246 | } else { |
| 247 | $author_name = get_query_var('author_name'); |
| 248 | if(is_author()){ |
| 249 | $user = get_user_by('slug', $author_name); |
| 250 | } else { |
| 251 | $user_id = get_the_author_meta('ID'); |
| 252 | $user = get_user_by('id', $user_id); |
| 253 | } |
| 254 | } |
| 255 | $id_or_email = !empty($user) ? $user->ID: ''; |
| 256 | $alt = $user->display_name; |
| 257 | } |
| 258 | $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta('wp_user_avatar', $id_or_email) : ''; |
| 259 | $alignclass = !empty($align) ? ' align'.$align : ''; |
| 260 | if(!empty($wp_user_avatar_meta)){ |
| 261 | $get_size = is_numeric($size) ? array($size,$size) : $size; |
| 262 | $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, $get_size); |
| 263 | $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : ''; |
| 264 | $wp_user_avatar = '<img src="'.$wp_user_avatar_image[0].'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.$alignclass.' avatar avatar-'.$size.' photo" />'; |
| 265 | } else { |
| 266 | $default = ''; |
| 267 | $alt = ''; |
| 268 | if($size == 'original' || $size == 'large'){ |
| 269 | $get_size = get_option('large_size_w'); |
| 270 | } elseif($size == 'medium'){ |
| 271 | $get_size = get_option('medium_size_w'); |
| 272 | } elseif($size == 'thumbnail'){ |
| 273 | $get_size = get_option('thumbnail_size_w'); |
| 274 | } else { |
| 275 | $get_size = $size; |
| 276 | } |
| 277 | $avatar = get_avatar($id_or_email, $get_size, $default, $alt); |
| 278 | $gravatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$get_size.$alignclass." ", $avatar); |
| 279 | $wp_user_avatar = $gravatar; |
| 280 | } |
| 281 | return $wp_user_avatar; |
| 282 | } |
| 283 | |
| 284 | // Return just the image src |
| 285 | function get_wp_user_avatar_src($id_or_email, $size = '', $align = ''){ |
| 286 | $wp_user_avatar_image = get_wp_user_avatar($id_or_email, $size, $align); |
| 287 | $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER); |
| 288 | $wp_user_avatar_image_src = $matches [0] [1]; |
| 289 | return $wp_user_avatar_image_src; |
| 290 | } |
| 291 | |
| 292 | // Return just the image src |
| 293 | function get_avatar_src($avatar, $id_or_email, $size = '', $default = '', $alt = false){ |
| 294 | $avatar_image = get_avatar($id_or_email); |
| 295 | $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $avatar_image, $matches, PREG_SET_ORDER); |
| 296 | $avatar_image_src = $matches [0] [1]; |
| 297 | return $avatar_image_src; |
| 298 | } |
| 299 | |
| 300 | // Replace get_avatar() |
| 301 | function get_wp_user_avatar_alt($avatar, $id_or_email, $size = '', $default = '', $alt = false){ |
| 302 | global $post, $pagenow, $comment; |
| 303 | $default = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar.png'; |
| 304 | // Find user ID on comment, author page, or post |
| 305 | if(is_object($id_or_email)){ |
| 306 | $id_or_email = $comment->user_id != '0' ? $comment->user_id : $comment->comment_author_email; |
| 307 | $alt = $comment->comment_author; |
| 308 | } else { |
| 309 | if(!empty($id_or_email)){ |
| 310 | $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email); |
| 311 | } else { |
| 312 | $author_name = get_query_var('author_name'); |
| 313 | if(is_author()){ |
| 314 | $user = get_user_by('slug', $author_name); |
| 315 | } else { |
| 316 | $user_id = get_the_author_meta('ID'); |
| 317 | $user = get_user_by('id', $user_id); |
| 318 | } |
| 319 | } |
| 320 | $id_or_email = $user->ID; |
| 321 | $alt = $user->display_name; |
| 322 | } |
| 323 | $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta('wp_user_avatar', $id_or_email) : ''; |
| 324 | if(!empty($wp_user_avatar_meta) && $pagenow != 'options-discussion.php'){ |
| 325 | $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, array($size,$size)); |
| 326 | $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : ''; |
| 327 | $wp_user_avatar = '<img src="'.$wp_user_avatar_image[0].'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.' avatar avatar-'.$size.' photo" />'; |
| 328 | } else { |
| 329 | $gravatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$size." ", $avatar); |
| 330 | $wp_user_avatar = $gravatar; |
| 331 | } |
| 332 | return $wp_user_avatar; |
| 333 | } |
| 334 | add_filter('get_avatar', 'get_wp_user_avatar_alt', 10, 6); |
| 335 | |
| 336 | // Shortcode |
| 337 | function wp_user_avatar_shortcode($atts, $content){ |
| 338 | // EXAMPLE USAGE: |
| 339 | // [avatar size="medium"] |
| 340 | |
| 341 | // Set shortcode attributes |
| 342 | extract(shortcode_atts(array('user' => '', 'size' => '96', 'align' => ''), $atts)); |
| 343 | $get_user = get_user_by('slug', $user); |
| 344 | $id_or_email = !empty($get_user) ? $get_user->ID : ''; |
| 345 | $wp_user_avatar = get_wp_user_avatar($id_or_email, $size, $align); |
| 346 | return $wp_user_avatar; |
| 347 | } |
| 348 | add_shortcode('avatar','wp_user_avatar_shortcode'); |
| 349 | |
| 350 | // Add default avatar |
| 351 | function add_default_wp_user_avatar($avatar_list){ |
| 352 | $avatar_default = get_option('avatar_default'); |
| 353 | $avatar_default_wp_user_avatar = get_option('avatar_default_wp_user_avatar'); |
| 354 | if(!empty($avatar_default_wp_user_avatar)){ |
| 355 | $avatar_full_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, 'medium'); |
| 356 | $avatar_thumb_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array(32,32)); |
| 357 | $avatar_full = $avatar_full_src[0]; |
| 358 | $avatar_thumb = $avatar_thumb_src[0]; |
| 359 | $hide_remove = ''; |
| 360 | } else { |
| 361 | $avatar_full = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar.png'; |
| 362 | $avatar_thumb = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-32x32.png'; |
| 363 | $hide_remove = ' style="display:none;"'; |
| 364 | } |
| 365 | $selected_avatar = ($avatar_default == $avatar_full) ? ' checked="checked" ' : ''; |
| 366 | $avatar_thumb_img = '<div id="wp-user-avatar-preview"><img src="'.$avatar_thumb.'" width="32" /></div>'; |
| 367 | $wp_user_avatar_list = ''; |
| 368 | $wp_user_avatar_list .= "\n\t<label><input type='radio' name='avatar_default' id='wp_user_avatar_radio' value='$avatar_full'$selected_avatar /> "; |
| 369 | $wp_user_avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&forcedefault=1'", $avatar_thumb_img); |
| 370 | $wp_user_avatar_list .= ' WP User Avatar</label>'; |
| 371 | $wp_user_avatar_list .= '<p style="padding-left:15px;"><button type="button" class="button" id="add-wp-user-avatar">Edit WP User Avatar</button>'; |
| 372 | $wp_user_avatar_list .= ' <a href="#" id="remove-wp-user-avatar"'.$hide_remove.'>Remove</a></p>'; |
| 373 | $wp_user_avatar_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="'.$avatar_default_wp_user_avatar.'">'; |
| 374 | $wp_user_avatar_list .= '<p id="wp-user-avatar-message">Press "Save Changes" to save your changes.</p>'; |
| 375 | $wp_user_avatar_list .= edit_default_wp_user_avatar(); |
| 376 | return $wp_user_avatar_list.$avatar_list; |
| 377 | } |
| 378 | add_filter('default_avatar_select', 'add_default_wp_user_avatar'); |
| 379 | |
| 380 | // Uploader for default avatar |
| 381 | function edit_default_wp_user_avatar(){ ?> |
| 382 | <script type="text/javascript"> |
| 383 | jQuery(function($){ |
| 384 | <?php if(function_exists('wp_enqueue_media')) : // Use Backbone uploader for WP 3.5+ ?> |
| 385 | wp.media.wpUserAvatar = { |
| 386 | get: function() { |
| 387 | return wp.media.view.settings.post.wpUserAvatarId; |
| 388 | }, |
| 389 | set: function(id) { |
| 390 | var settings = wp.media.view.settings; |
| 391 | settings.post.wpUserAvatarId = id; |
| 392 | settings.post.wpUserAvatarSrc = $('div.attachment-info').find('img').attr('src'); |
| 393 | if(settings.post.wpUserAvatarId) |
| 394 | setWPUserAvatar(settings.post.wpUserAvatarId, settings.post.wpUserAvatarSrc); |
| 395 | $('#wp_user_avatar_radio').val(settings.post.wpUserAvatarSrc).trigger('click'); |
| 396 | }, |
| 397 | frame: function(){ |
| 398 | if(this._frame) |
| 399 | return this._frame; |
| 400 | this._frame = wp.media({ |
| 401 | state: 'library', |
| 402 | states: [ new wp.media.controller.Library({ title: "Edit WP User Avatar Default Avatar" }) ] |
| 403 | }); |
| 404 | this._frame.on('open', function(){ |
| 405 | var selection = this.state().get('selection'); |
| 406 | id = jQuery('#wp-user-avatar').val(); |
| 407 | attachment = wp.media.attachment(id); |
| 408 | attachment.fetch(); |
| 409 | selection.add(attachment ? [ attachment ] : []); |
| 410 | }, this._frame); |
| 411 | this._frame.on('toolbar:create:select', function(toolbar){ |
| 412 | this.createSelectToolbar(toolbar, { |
| 413 | text: 'Set WP User Avatar' |
| 414 | }); |
| 415 | }, this._frame); |
| 416 | this._frame.state('library').on('select', this.select); |
| 417 | return this._frame; |
| 418 | }, |
| 419 | select: function(id) { |
| 420 | var settings = wp.media.view.settings, |
| 421 | selection = this.get('selection').single(); |
| 422 | wp.media.wpUserAvatar.set(selection ? selection.id : -1); |
| 423 | }, |
| 424 | init: function() { |
| 425 | $('body').on('click', '#add-wp-user-avatar', function(e){ |
| 426 | e.preventDefault(); |
| 427 | e.stopPropagation(); |
| 428 | wp.media.wpUserAvatar.frame().open(); |
| 429 | }); |
| 430 | } |
| 431 | }; |
| 432 | $(wp.media.wpUserAvatar.init); |
| 433 | <?php else : // Fall back to Thickbox uploader ?> |
| 434 | $('#add-wp-user-avatar').click(function(e){ |
| 435 | e.preventDefault(); |
| 436 | tb_show('Edit WP User Avatar Default Avatar', 'media-upload.php?type=image&post_type=user&post_id=0&tab=library&TB_iframe=1'); |
| 437 | }); |
| 438 | <?php endif; ?> |
| 439 | }); |
| 440 | jQuery(function($){ |
| 441 | $('#remove-wp-user-avatar').click(function(e){ |
| 442 | var avatar_full = '<?php echo WP_USER_AVATAR_URLPATH; ?>images/wp-user-avatar.png'; |
| 443 | var avatar_thumb = '<img src="<?php echo WP_USER_AVATAR_URLPATH; ?>images/wp-user-avatar-32x32.png" alt="" />'; |
| 444 | e.preventDefault(); |
| 445 | $(this).hide(); |
| 446 | $('#wp-user-avatar-preview').find('img').replaceWith(avatar_thumb); |
| 447 | $('#wp-user-avatar').val(''); |
| 448 | $('#wp-user-avatar-message').show(); |
| 449 | $('#wp_user_avatar_radio').val(avatar_full).trigger('click'); |
| 450 | }); |
| 451 | }); |
| 452 | </script> |
| 453 | <?php |
| 454 | } |
| 455 | |
| 456 | // Add default avatar field to whitelist |
| 457 | function wp_user_avatar_whitelist_options($whitelist_options){ |
| 458 | $whitelist_options['discussion'] = array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration', 'avatar_default_wp_user_avatar' ); |
| 459 | return $whitelist_options; |
| 460 | } |
| 461 | add_filter('whitelist_options', 'wp_user_avatar_whitelist_options'); |
| 462 | |
| 463 | // Get orginal avatar |
| 464 | function get_avatar_original($avatar, $id_or_email, $size = '', $default = '', $alt = false){ |
| 465 | remove_filter('get_avatar', 'get_wp_user_avatar_alt'); |
| 466 | $gravatar = get_avatar($avatar); |
| 467 | return $gravatar; |
| 468 | } |
| 469 | |
| 470 | ?> |
| 471 |