WP User Avatar - Version 1.3.5

Version Description

  • Bug Fix: Swap TinyMCE file locations
Download this release

Release Info

Developer bangbay
Plugin Icon 128x128 WP User Avatar
Version 1.3.5
Comparing to
See all releases

Version 1.3.5

css/wp-user-avatar.css ADDED
@@ -0,0 +1,7 @@
1
+ #wp-user-avatar-message { color: #c00; display: none; }
2
+ #wp-user-avatar-preview img { max-width: 96px; }
3
+ .defaultavatarpicker #wp-user-avatar-preview { width: 32px; height: 32px; display: inline-block; overflow: hidden; vertical-align: middle; }
4
+ .defaultavatarpicker #wp-user-avatar-preview img { width: 32px; }
5
+ #edit-wp-user-avatar { padding-left: 15px; }
6
+ #edit-wp-user-avatar #remove-wp-user-avatar { margin-left: 10px; }
7
+ .hide-me, .wp-core-ui .hide-me { display: none; }
images/wp-user-avatar-150x150.png ADDED
Binary file
images/wp-user-avatar-300x300.png ADDED
Binary file
images/wp-user-avatar-32x32.png ADDED
Binary file
images/wp-user-avatar-96x96.png ADDED
Binary file
images/wp-user-avatar.png ADDED
Binary file
includes/tinymce.php ADDED
@@ -0,0 +1,42 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.3.5
5
+ */
6
+
7
+ function myplugin_addbuttons() {
8
+ // Don't bother doing this stuff if the current user lacks permissions
9
+ if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
10
+ return;
11
+
12
+ // Add only in Rich Editor mode
13
+ if(get_user_option('rich_editing') == 'true'){
14
+ add_filter('mce_external_plugins', 'add_myplugin_tinymce_plugin');
15
+ add_filter('mce_buttons', 'register_myplugin_button');
16
+ }
17
+ }
18
+
19
+ function register_myplugin_button($buttons) {
20
+ array_push($buttons, 'separator', 'wpUserAvatar');
21
+ return $buttons;
22
+ }
23
+
24
+ // Load the TinyMCE plugin : editor_plugin.js (wp2.5)
25
+ function add_myplugin_tinymce_plugin($plugin_array) {
26
+ $plugin_array['wpUserAvatar'] = WP_USER_AVATAR_URLPATH.'includes/tinymce/editor_plugin.js';
27
+ return $plugin_array;
28
+ }
29
+
30
+ // init process for button control
31
+ add_action('init', 'myplugin_addbuttons');
32
+
33
+ // Call TinyMCE window content via admin-ajax
34
+ function wp_user_avatar_ajax_tinymce(){
35
+ if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
36
+ die('You are not allowed to call this page directly.');
37
+ include_once(WP_USER_AVATAR_ABSPATH.'includes/tinymce/window.php');
38
+ die();
39
+ }
40
+ add_action('wp_ajax_wp_user_avatar_tinymce', 'wp_user_avatar_ajax_tinymce');
41
+
42
+ ?>
includes/tinymce/editor_plugin.js ADDED
@@ -0,0 +1 @@
1
+ (function(){tinymce.PluginManager.requireLangPack('wpUserAvatar');tinymce.create('tinymce.plugins.wpUserAvatar',{init:function(ed,url){ed.addCommand('mceWpUserAvatar',function(){ed.windowManager.open({file:ajaxurl+'?action=wp_user_avatar_tinymce',width:400,height:260,inline:1},{plugin_url:url})});ed.addButton('wpUserAvatar',{title:'Insert WP User Avatar',cmd:'mceWpUserAvatar',image:url+'/../../images/wp-user-avatar-32x32.png'});ed.onNodeChange.add(function(ed,cm,n){cm.setActive('wpUserAvatar',n.nodeName=='IMG')})},createControl:function(n,cm){return null},getInfo:function(){return{longname:'WP User Avatar',author:'Bangbay Siboliban',authorurl:'http://siboliban.org/',infourl:'http://wordpress.org/extend/plugins/wp-user-avatar/',version:"1.3.5"}}});tinymce.PluginManager.add('wpUserAvatar',tinymce.plugins.wpUserAvatar)})();
includes/tinymce/window.php ADDED
@@ -0,0 +1,111 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.3.5
5
+ */
6
+
7
+ if ( !defined('ABSPATH') )
8
+ die('You are not allowed to call this page directly.');
9
+ @header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
10
+ ?>
11
+ <html xmlns="http://www.w3.org/1999/xhtml">
12
+ <head>
13
+ <title>WP User Avatar</title>
14
+ <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
15
+ <base target="_self" />
16
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/tinymce/tiny_mce_popup.js"></script>
17
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/tinymce/utils/form_utils.js"></script>
18
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/jquery/jquery.js"></script>
19
+ <script type="text/javascript">
20
+ function insert_wp_user_avatar(){
21
+ // Custom shortcode values
22
+ var shortcode;
23
+ var user = document.getElementById('wp_user_avatar_user').value;
24
+ var size = document.getElementById('wp_user_avatar_size').value;
25
+ var size_number = document.getElementById('wp_user_avatar_size_number').value;
26
+ var align = document.getElementById('wp_user_avatar_align').value;
27
+ var link = document.getElementById('wp_user_avatar_link').value;
28
+ var link_external = document.getElementById('wp_user_avatar_link_external').value;
29
+
30
+ // Add tag to shortcode only if not blank
31
+ var user_tag = (user != '') ? ' user="' + user + '"' : '';
32
+ var size_tag = (size != '' && size_number == '') ? ' size="' + size + '"' : '';
33
+ size_tag = (size_number != '') ? ' size="' + size_number + '"' : size_tag;
34
+ var align_tag = (align != '') ? ' align="' + align + '"' : '';
35
+ var link_tag = (link != '' && link_external == '') ? ' link="' + link + '"' : '';
36
+ link_tag = (link_external != '') ? ' link="' + link_external + '"' : link_tag;
37
+
38
+ shortcode = "<p>[avatar" + user_tag + size_tag + align_tag + link_tag + "]</p>";
39
+
40
+ if(window.tinyMCE) {
41
+ window.tinyMCE.execInstanceCommand(window.tinyMCE.activeEditor.id, 'mceInsertContent', false, shortcode);
42
+ tinyMCEPopup.editor.execCommand('mceRepaint');
43
+ tinyMCEPopup.close();
44
+ }
45
+ return;
46
+ }
47
+ </script>
48
+ <style type="text/css">
49
+ form { background: #fff; border: 1px solid #eee; }
50
+ p, h4 { margin: 0; padding: 12px 0 0; }
51
+ h4.center { text-align: center; }
52
+ label { width: 90px; display: inline-block; text-align: right; }
53
+ .mceActionPanel { padding: 7px 0 12px; text-align: center; }
54
+ .mceActionPanel #insert { float: none; width: 180px; margin: 0 auto; }
55
+ </style>
56
+ </head>
57
+ <body id="link" class="wp-core-ui" onload="document.body.style.display='';" style="display:none;">
58
+ <form name="wpUserAvatar" action="#">
59
+ <p><label for="<?php esc_attr_e('wp_user_avatar_user'); ?>"><strong><?php _e("User:"); ?></strong></label>
60
+ <select id="<?php esc_attr_e('wp_user_avatar_user'); ?>" name="<?php esc_attr_e('wp_user_avatar_user'); ?>">
61
+ <option value=""></option>
62
+ <?php $users = get_users(); foreach($users as $user) : ?>
63
+ <option value="<?php echo $user->user_login; ?>"><?php echo $user->display_name; ?></option>
64
+ <?php endforeach; ?>
65
+ </select></p>
66
+
67
+ <h4 class="center">Choose a preset size or enter a number value.</h4>
68
+
69
+ <p>
70
+ <label for="<?php esc_attr_e('wp_user_avatar_size'); ?>"><strong><?php _e("Size:"); ?></strong></label>
71
+ <select id="<?php esc_attr_e('wp_user_avatar_size'); ?>" name="<?php esc_attr_e('wp_user_avatar_size'); ?>">
72
+ <option value=""></option>
73
+ <option value="original"><?php _e("Original"); ?></option>
74
+ <option value="large"><?php _e("Large"); ?></option>
75
+ <option value="medium"><?php _e("Medium"); ?></option>
76
+ <option value="thumbnail"><?php _e("Thumbnail"); ?></option>
77
+ </select>
78
+ or
79
+ <input type="text" size="8" id="<?php esc_attr_e('wp_user_avatar_size_number'); ?>" name="<?php esc_attr_e('wp_user_avatar_size'); ?>" value="" />
80
+ </p>
81
+
82
+ <p><label for="<?php esc_attr_e('wp_user_avatar_align'); ?>"><strong><?php _e("Alignment:"); ?></strong></label>
83
+ <select id="<?php esc_attr_e('wp_user_avatar_align'); ?>" name="<?php esc_attr_e('wp_user_avatar_align'); ?>">
84
+ <option value=""></option>
85
+ <option value="center"><?php _e("Center"); ?></option>
86
+ <option value="left"><?php _e("Left"); ?></option>
87
+ <option value="right"><?php _e("Right"); ?></option>
88
+ </select></p>
89
+
90
+ <h4 class="center">Link to image file, attachment page, or custom URL.</h4>
91
+
92
+ <p>
93
+ <label for="<?php esc_attr_e('wp_user_avatar_link'); ?>"><strong><?php _e("Link to:"); ?></strong></label>
94
+ <select id="<?php esc_attr_e('wp_user_avatar_link'); ?>" name="<?php esc_attr_e('wp_user_avatar_link'); ?>">
95
+ <option value=""></option>
96
+ <option value="file"><?php _e("Image File"); ?></option>
97
+ <option value="attachment"><?php _e("Attachment Page"); ?></option>
98
+ </select>
99
+ </p>
100
+
101
+ <p>
102
+ <label for="<?php esc_attr_e('wp_user_avatar_link_external'); ?>">or</label>
103
+ <input type="text" size="36" id="<?php esc_attr_e('wp_user_avatar_link_external'); ?>" name="<?php esc_attr_e('wp_user_avatar_link'); ?>" value="" />
104
+ </p>
105
+
106
+ <div class="mceActionPanel">
107
+ <input type="submit" id="insert" class="button-primary" name="insert" value="<?php _e("Insert WP User Avatar"); ?>" onclick="insert_wp_user_avatar();" />
108
+ </div>
109
+ </form>
110
+ </body>
111
+ </html>
index.html ADDED
@@ -0,0 +1 @@
1
+ <!-- Thanks for using WP User Avatar! -->
js/wp-user-avatar.js ADDED
@@ -0,0 +1 @@
1
+ function openMediaUploader(a){wp.media.wpUserAvatar={get:function(){return wp.media.view.settings.post.wpUserAvatarId},set:function(c){var b=wp.media.view.settings;b.post.wpUserAvatarId=c;b.post.wpUserAvatarSrc=jQuery("div.attachment-info").find("img").attr("src");if(b.post.wpUserAvatarId){setWPUserAvatar(b.post.wpUserAvatarId,b.post.wpUserAvatarSrc);jQuery("#wp_user_avatar_radio").trigger("click")}},frame:function(){if(this._frame){return this._frame}this._frame=wp.media({state:"library",states:[new wp.media.controller.Library({title:"Edit WP User Avatar: "+a})]});this._frame.on("open",function(){var b=this.state().get("selection");id=jQuery("#wp-user-avatar").val();attachment=wp.media.attachment(id);attachment.fetch();b.add(attachment?[attachment]:[])},this._frame);this._frame.on("toolbar:create:select",function(b){this.createSelectToolbar(b,{text:"Set WP User Avatar"})},this._frame);this._frame.state("library").on("select",this.select);return this._frame},select:function(d){var c=wp.media.view.settings,b=this.get("selection").single();wp.media.wpUserAvatar.set(b?b.id:-1)},init:function(){jQuery("body").on("click","#add-wp-user-avatar",function(b){b.preventDefault();b.stopPropagation();wp.media.wpUserAvatar.frame().open()})}};jQuery(wp.media.wpUserAvatar.init)}function openThickboxUploader(b,a){jQuery("body").on("click","#add-wp-user-avatar",function(c){c.preventDefault();tb_show("Edit WP User Avatar: "+b,a)})}function setWPUserAvatar(b,a){jQuery("#wp-user-avatar",window.parent.document).val(b);jQuery("#wp-user-avatar-preview",window.parent.document).find("img").attr("src",a).removeAttr("width","").removeAttr("height","");jQuery("#wp-user-avatar-message",window.parent.document).show();jQuery("#remove-wp-user-avatar",window.parent.document).show();jQuery("#wp-user-avatar-notice",window.parent.document).hide();jQuery("#wp_user_avatar_radio",window.parent.document).trigger("click");if(typeof(wp)!="undefined"){wp.media.wpUserAvatar.frame().close()}else{window.parent.tb_remove()}}function removeWPUserAvatar(a,b){jQuery("body").on("click","#remove-wp-user-avatar",function(c){c.preventDefault();jQuery(this).hide();jQuery("#wp-user-avatar-preview").find("img").attr("src",b).removeAttr("width","").removeAttr("height","");jQuery("#wp-user-avatar").val("");jQuery("#wp-user-avatar-message, #wp-user-avatar-notice").show();jQuery("#wp_user_avatar_radio").trigger("click")})};
readme.txt ADDED
@@ -0,0 +1,338 @@
1
+ === WP User Avatar ===
2
+
3
+ Contributors: bangbay
4
+ Donate link: http://siboliban.org/donate
5
+ Tags: author image, author photo, author avatar, avatar, bbPress, profile avatar, profile image, user avatar, user image, user photo
6
+ Requires at least: 3.3
7
+ Tested up to: 3.5.1
8
+ Stable tag: 1.3.5
9
+ License: GPLv2 or later
10
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
+
12
+ Use any image from your WordPress Media Library as a custom user avatar. Add your own Default Avatar.
13
+
14
+ == Description ==
15
+
16
+ WordPress currently only allows you to use custom avatars that are uploaded through [gravatar.com](http://gravatar.com/). WP User Avatar enables you to use any photo uploaded into your Media Library as an avatar. This means you use the same uploader and library as your posts. No extra folders or image editing functions are necessary.
17
+
18
+ WP User Avatar also lets you:
19
+
20
+ * Upload your own Default Avatar in your Discussion settings.
21
+ * Show the user's [gravatar.com](http://gravatar.com/) avatar or Default Avatar if the user doesn't have a WP User Avatar image.
22
+ * Use the <code>[avatar]</code> shortcode in your posts. The shortcode will work with any theme, whether it has avatar support or not.
23
+
24
+ This plugin uses the Media uploader introduced in WordPress 3.5, but is also backwards-compatible to WordPress 3.3. It is also compatible with WordPress Multisite.
25
+
26
+ == Installation ==
27
+
28
+ 1. Download, install, and activate the WP User Avatar plugin.
29
+ 2. On your edit profile page, click "Edit WP User Avatar".
30
+ 3. Choose an image, then click "Set WP User Avatar".
31
+ 4. Click "Update Profile".
32
+ 5. Upload your own Default Avatar in your Discussion settings (optional).
33
+ 6. Choose a theme that has avatar support. In your theme, manually replace <code>get_avatar</code> with <code>get_wp_user_avatar</code> or leave <code>get_avatar</code> as-is. [Read about the differences here](http://wordpress.org/extend/plugins/wp-user-avatar/faq/).
34
+ 7. You can also use the <code>[avatar]</code> shortcode in your posts. The shortcode will work with any theme, whether it has avatar support or not.
35
+
36
+ **Example Usage**
37
+
38
+ = Posts =
39
+
40
+ Within [The Loop](http://codex.wordpress.org/The_Loop), you may be using:
41
+
42
+ `<?php echo get_avatar(get_the_author_meta('ID'), 96); ?>`
43
+
44
+ Replace this function with:
45
+
46
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96); ?>`
47
+
48
+ You can also use the values "original", "large", "medium", or "thumbnail" for your avatar size:
49
+
50
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 'medium'); ?>`
51
+
52
+ You can also add an alignment of "left", "right", or "center":
53
+
54
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96, 'left'); ?>`
55
+
56
+ = Author Page =
57
+
58
+ On an author page outside of [The Loop](http://codex.wordpress.org/The_Loop), you may be using:
59
+
60
+ `<?php
61
+ $user = get_user_by('slug', $author_name);
62
+ echo get_avatar($user->ID, 96);
63
+ ?>`
64
+
65
+ Replace this function with:
66
+
67
+ `<?php
68
+ $user = get_user_by('slug', $author_name);
69
+ echo get_wp_user_avatar($user->ID, 96);
70
+ ?>`
71
+
72
+ If you leave the options blank, WP User Avatar will detect whether you're inside [The Loop](http://codex.wordpress.org/The_Loop) or on an author page and return the correct avatar in the default 96x96 size:
73
+
74
+ `<?php echo get_wp_user_avatar(); ?>`
75
+
76
+ The function <code>get_wp_user_avatar</code> can also fall back to <code>get_avatar</code> if there is no WP User Avatar image. For this to work, "Show Avatars" must be checked in your Discussion settings. When this setting is enabled, you will see the user's [gravatar.com](http://gravatar.com/) avatar or Default Avatar.
77
+
78
+ = Comments =
79
+
80
+ For comments, you might have in your template:
81
+
82
+ `<?php echo get_avatar($comment, 32); ?>`
83
+
84
+ Replace this function with:
85
+
86
+ `<?php echo get_wp_user_avatar($comment, 32); ?>`
87
+
88
+ For comments, you must specify the $comment variable.
89
+
90
+ **Other Available Functions**
91
+
92
+ = [avatar] shortcode =
93
+
94
+ You can use the <code>[avatar]</code> shortcode in your posts. It will detect the author of the post or you can specify an author by username. You can specify a size, alignment, and link, but they are optional. For links, you can link to the original image file, attachment page, or a custom URL.
95
+
96
+ `[avatar user="admin" size="medium" align="left" link="file"]`
97
+
98
+ = get_wp_user_avatar_src =
99
+
100
+ Works just like <code>get_wp_user_avatar</code> but returns just the image src. This is useful if you would like to link a thumbnail-sized avatar to a larger version of the image:
101
+
102
+ `<a href="<?php echo get_wp_user_avatar_src($user_id, 'large'); ?>">
103
+ <?php echo get_wp_user_avatar($user_id, 'thumbnail'); ?>
104
+ </a>`
105
+
106
+ = has_wp_user_avatar =
107
+
108
+ Returns true if the user has a WP User Avatar image. You can specify the user ID, or leave it blank to detect the author within [The Loop](http://codex.wordpress.org/The_Loop) or author page:
109
+
110
+ `<?php
111
+ if ( has_wp_user_avatar($user_id) ) {
112
+ echo get_wp_user_avatar($user_id, 96);
113
+ } else {
114
+ echo '<img src="my-alternate-image.jpg" />';
115
+ }
116
+ ?>`
117
+
118
+ == Frequently Asked Questions ==
119
+
120
+ = How do I use WP User Avatar? =
121
+
122
+ First, choose a theme that has avatar support. In your theme, you have a choice of manually replacing <code>get_avatar</code> with <code>get_wp_user_avatar</code>, or leaving <code>get_avatar</code> as-is. Here are the differences:
123
+
124
+ = get_wp_user_avatar =
125
+
126
+ 1. Allows you to use the values "original", "large", "medium", or "thumbnail" for your avatar size.
127
+ 2. Doesn't add a fixed width and height to the image if you use the aforementioned values. This will give you more flexibility to resize the image with CSS.
128
+ 3. Optionally adds CSS classes "alignleft", "alignright", or "aligncenter" to position your avatar.
129
+ 4. Shows nothing if the user has no WP User Avatar image.
130
+ 5. Shows the user's [gravatar.com](http://gravatar.com/) avatar or Default Avatar only if "Show Avatars" is enabled in your Discussion settings.
131
+
132
+ = get_avatar =
133
+
134
+ 1. Requires you to enable "Show Avatars" in your Discussion settings to show any avatars.
135
+ 2. Accepts only numeric values for your avatar size.
136
+ 3. Always adds a fixed width and height to your image. This may cause problems if you use responsive CSS in your theme.
137
+ 4. Shows the user's [gravatar.com](http://gravatar.com/) avatar or Default Avatar if the user doesn't have a WP User Avatar image. (Choosing "Blank" as your Default Avatar still generates a transparent image file.)
138
+ 5. Requires no changes to your theme files if you are currently using <code>get_avatar</code>.
139
+
140
+ [Read more about get_avatar in the WordPress Function Reference](http://codex.wordpress.org/Function_Reference/get_avatar).
141
+
142
+ = Can I create a custom Default Avatar? =
143
+ In your Discussion settings, you can upload your own Default Avatar.
144
+
145
+ = Can I insert WP User Avatar directly into a post? =
146
+
147
+ You can use the <code>[avatar]</code> shortcode in your posts. It will detect the author of the post or you can specify an author by username. You can specify a size, alignment, and link, but they are optional. For links, you can link to the original image file, attachment page, or a custom URL.
148
+
149
+ `[avatar user="admin" size="96" align="left" link="file"]`
150
+
151
+ Outputs:
152
+
153
+ `<a href="{fileURL}" class="wp-user-avatar-link wp-user-avatar-file">
154
+ <img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />
155
+ </a>`
156
+
157
+ = Can Contributors or Subscribers choose their own WP User Avatar image? =
158
+
159
+ Users need <code>upload_files</code> capability to choose their own WP User Avatar image. This means that only Administrators, Editors, and Authors can choose their own WP User Avatar image. Contributors and Subscribers cannot upload images. Administators can choose WP User Avatar images for Contributors and Subscribers.
160
+
161
+ [Read more about Roles and Capabilities here](http://codex.wordpress.org/Roles_and_Capabilities).
162
+
163
+ = Will WP User Avatar work with comment author avatars? =
164
+
165
+ Yes, for registered users. Non-registered comment authors will show their [gravatar.com](http://gravatar.com/) avatars or Default Avatar.
166
+
167
+ = Will WP User Avatar work with bbPress? =
168
+
169
+ Yes, but only users that have <code>upload_files</code> capability can choose their own WP User Avatar image.
170
+
171
+ = Will WP User Avatar work with WordPress Multisite? =
172
+
173
+ Yes, however, each site has its own avatar settings. If you set a WP User Avatar image on one site, you have to set it again for different sites in your network.
174
+
175
+ = How can I see which users have an avatar? =
176
+
177
+ For Administrators, WP User Avatar adds a column with avatar thumbnails to your Users list table. If "Show Avatars" is enabled in your Discussion settings, you will see avatars to the left of each username instead of in a new column.
178
+
179
+ = What CSS can I use with WP User Avatar? =
180
+
181
+ WP User Avatar will add the CSS classes "wp-user-avatar" and "wp-user-avatar-{size}" to your image. If you add an alignment, the corresponding alignment class will be added:
182
+
183
+ `<?php echo get_wp_user_avatar($user_id, 96, 'left'); ?>`
184
+
185
+ Outputs:
186
+
187
+ `<img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />`
188
+
189
+ **Note:** "alignleft", "alignright", and aligncenter" are common WordPress CSS classes, but not every theme supports them. Contact the theme author to add those CSS classes.
190
+
191
+ If you use the values "original", "large", "medium", or "thumbnail", no width or height will be added to the image. This will give you more flexibility to resize the image with CSS:
192
+
193
+ `<?php echo get_wp_user_avatar($user_id, 'medium'); ?>`
194
+
195
+ Outputs:
196
+
197
+ `<img src="{imageURL}" class="wp-user-avatar wp-user-avatar-medium" />`
198
+
199
+ **Note:** WordPress adds more CSS classes to the avatar not listed here.
200
+
201
+ If you use the <code>[avatar]</code> shortcode, WP User Avatar will add the CSS class "wp-user-avatar-link" to the link. It will also add CSS classes based on link type.
202
+
203
+ * Image File: wp-user-avatar-file
204
+ * Attachment: wp-user-avatar-attachment
205
+ * Custom URL: wp-user-avatar-custom
206
+
207
+ `[avatar user="admin" size="96" align="left" link="attachment"]`
208
+
209
+ Outputs:
210
+
211
+ `<a href="{attachmentURL}" class="wp-user-avatar-link wp-user-avatar-attachment">
212
+ <img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />
213
+ </a>`
214
+
215
+ = What other functions are available for WP User Avatar? =
216
+ * <code>get_wp_user_avatar_src</code>: retrieves just the image URL
217
+ * <code>has_wp_user_avatar</code>: checks if the user has a WP User Avatar image
218
+ * [See example usage here](http://wordpress.org/extend/plugins/wp-user-avatar/installation/)
219
+
220
+ == Screenshots ==
221
+
222
+ 1. WP User Avatar lets you upload your own Default Avatar.
223
+ 2. WP User Avatar adds a field to your edit profile page.
224
+ 3. After you've chosen a WP User Avatar image, you will see the option to remove it.
225
+ 4. WP User Avatar adds a button to insert the [avatar] shortcode in the Visual Editor.
226
+ 5. Options for the [avatar] shortcode.
227
+
228
+ == Changelog ==
229
+
230
+ = 1.3.5 =
231
+ * Bug Fix: Swap TinyMCE file locations
232
+
233
+ = 1.3.4 =
234
+ * Update: Change support only to WP 3.3+ because of jQuery 1.7.1 support
235
+
236
+ = 1.3.3 =
237
+ * Update: Shortcode checks for user ID, login, slug, or e-mail address
238
+ * Update: Move jquery to register_script for < WP 3.5
239
+
240
+ = 1.3.2 =
241
+ * Bug Fix: Check for user before setting name in alt tag
242
+ * Update: readme.txt
243
+
244
+ = 1.3.1 =
245
+ * Bug Fix: Rename usermeta only if found
246
+
247
+ = 1.3 =
248
+ * Add: Multisite support
249
+ * Bug Fix: Warnings if no user found
250
+ * Update: Enable action_show_user_profile for any class using show_user_profile hook
251
+
252
+ = 1.2.6 =
253
+ * Bug Fix: options-discussion.php page doesn't show default avatars
254
+
255
+ = 1.2.5 =
256
+ * Bug Fix: Comment author showing wrong avatar
257
+ * Bug Fix: Avatar adds fixed dimensions when non-numeric size is used
258
+ * Update: Use local image for default avatar instead of calling image from Gravatar
259
+
260
+ = 1.2.4 =
261
+ * Bug Fix: Show default avatar when user removes custom avatar
262
+ * Bug Fix: Default Avatar save setting
263
+
264
+ = 1.2.3 =
265
+ * Bug Fix: Show default avatar when user removes custom avatar
266
+ * Bug Fix: Default Avatar save setting
267
+
268
+ = 1.2.2 =
269
+ * Add: Ability for bbPress users to edit avatar on front profile page
270
+ * Add: Link options for shortcode
271
+ * Bug Fix: Show WP User Avatar only to users with upload_files capability
272
+
273
+ = 1.2.1 =
274
+ * Add: TinyMCE button
275
+ * Update: Clean up redundant code
276
+ * Update: Compatibility only back to WordPress 3.3
277
+
278
+ = 1.2 =
279
+ * Add: Default Avatar setting
280
+
281
+ = 1.1.7 =
282
+ * Bug Fix: Change update_usermeta to update_user_meta
283
+
284
+ = 1.1.6 =
285
+ * Bug Fix: Image not showing in user profile edit
286
+
287
+ = 1.1.5a =
288
+ * Update: readme.txt
289
+
290
+ = 1.1.5 =
291
+ * Bug Fix: Remove stray curly bracket
292
+
293
+ = 1.1.4 =
294
+ * Bug Fix: Change get_usermeta to get_user_meta
295
+ * Bug Fix: Non-object warning when retrieving user ID
296
+
297
+ = 1.1.3 =
298
+ * Bug Fix: Comment author with no e-mail address
299
+
300
+ = 1.1.2 =
301
+ * Remove: Unused variables
302
+
303
+ = 1.1.1 =
304
+ * Bug Fix: Capabilities error in comment avatar
305
+
306
+ = 1.1 =
307
+ * Add: Add filter for get_avatar
308
+ * Add: CSS alignment classes
309
+ * Add: Replace comment author avatar
310
+ * Add: Shortcode
311
+ * Update: readme.txt
312
+
313
+ = 1.0.2 =
314
+ * Update: FAQ
315
+ * Remove: CSS that hides "Insert into Post"
316
+
317
+ = 1.0.1 =
318
+ * Add: CSS classes to image output
319
+
320
+ = 1.0 =
321
+ * Initial release
322
+
323
+ == Upgrade Notice ==
324
+
325
+ = 1.3 =
326
+ * New Feature: Multisite support
327
+
328
+ = 1.2.2 =
329
+ * New Features: Link options for shortcode, bbPress integration
330
+
331
+ = 1.2.1 =
332
+ * New Feature: Shortcode insertion button for Visual Editor
333
+
334
+ = 1.2 =
335
+ * New Feature: Default Avatar customization
336
+
337
+ = 1.1 =
338
+ * New Features: [avatar] shortcode, direct replacement of get_avatar() and comment author avatar, more CSS classes
wp-user-avatar.php ADDED
@@ -0,0 +1,512 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.3.5
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. Add your own Default Avatar.
10
+ Version: 1.3.5
11
+ Author: Bangbay Siboliban
12
+ Author URI: http://siboliban.org/
13
+ */
14
+
15
+ // Define paths and variables
16
+ define('WP_USER_AVATAR_VERSION', '1.3.5');
17
+ define('WP_USER_AVATAR_FOLDER', basename(dirname(__FILE__)));
18
+ define('WP_USER_AVATAR_ABSPATH', trailingslashit(str_replace('\\','/', WP_PLUGIN_DIR.'/'.WP_USER_AVATAR_FOLDER)));
19
+ define('WP_USER_AVATAR_URLPATH', trailingslashit(plugins_url(WP_USER_AVATAR_FOLDER)));
20
+
21
+ // Define global variables
22
+ $avatar_default = get_option('avatar_default');
23
+ $avatar_default_wp_user_avatar = get_option('avatar_default_wp_user_avatar');
24
+ $mustache_original = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar.png';
25
+ $mustache_medium = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-300x300.png';
26
+ $mustache_thumbnail = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-150x150.png';
27
+ $mustache_avatar = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-96x96.png';
28
+ $mustache_admin = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-32x32.png';
29
+ $ssl = is_ssl() ? 's' : '';
30
+
31
+ // Load add-ons
32
+ include_once(WP_USER_AVATAR_ABSPATH.'includes/tinymce.php');
33
+
34
+ // Initialize default settings
35
+ register_activation_hook(__FILE__, 'wp_user_avatar_options');
36
+
37
+ // Remove user metadata and options on plugin delete
38
+ register_uninstall_hook(__FILE__, 'wp_user_avatar_delete_setup');
39
+
40
+ // Settings saved to wp_options
41
+ function wp_user_avatar_options(){
42
+ add_option('avatar_default_wp_user_avatar','');
43
+ }
44
+ add_action('init', 'wp_user_avatar_options');
45
+
46
+ // Update default avatar to new format
47
+ function wp_user_avatar_default_avatar(){
48
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_original;
49
+ if($avatar_default == $mustache_original){
50
+ update_option('avatar_default', 'wp_user_avatar');
51
+ }
52
+ if(!empty($avatar_default_wp_user_avatar)){
53
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, 'medium');
54
+ if($avatar_default == $avatar_default_wp_user_avatar_image[0]){
55
+ update_option('avatar_default', 'wp_user_avatar');
56
+ }
57
+ }
58
+ }
59
+ add_action('init', 'wp_user_avatar_default_avatar');
60
+
61
+ // Rename user meta to match database settings
62
+ function wp_user_avatar_user_meta(){
63
+ global $wpdb, $blog_id;
64
+ $wp_user_avatar_metakey = $wpdb->get_blog_prefix($blog_id).'user_avatar';
65
+ if($wp_user_avatar_metakey != 'wp_user_avatar'){
66
+ $users = get_users();
67
+ foreach($users as $user){
68
+ $wp_user_avatar = get_user_meta($user->ID, 'wp_user_avatar', true);
69
+ if(!empty($wp_user_avatar)){
70
+ update_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar', $wp_user_avatar);
71
+ delete_user_meta($user->ID, 'wp_user_avatar');
72
+ }
73
+ }
74
+ }
75
+ }
76
+ add_action('init', 'wp_user_avatar_user_meta');
77
+
78
+ // Remove user metadata and options on plugin delete
79
+ function wp_user_avatar_delete_setup(){
80
+ global $wpdb, $blog_id, $switched;
81
+ $users = get_users();
82
+ if(is_multisite()){
83
+ $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_blogs"));
84
+ foreach($users as $user){
85
+ foreach($blogs as $blog){
86
+ delete_user_meta($user->ID, $wpdb->get_blog_prefix($blog->blog_id).'user_avatar');
87
+ }
88
+ }
89
+ foreach($blogs as $blog){
90
+ switch_to_blog($blog->blog_id);
91
+ delete_option('avatar_default_wp_user_avatar');
92
+ }
93
+ } else {
94
+ foreach($users as $user){
95
+ delete_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar');
96
+ }
97
+ delete_option('avatar_default_wp_user_avatar');
98
+ }
99
+ update_option('avatar_default', 'mystery');
100
+ }
101
+
102
+ // WP User Avatar
103
+ if(!class_exists('wp_user_avatar')){
104
+ class wp_user_avatar{
105
+ function wp_user_avatar(){
106
+ // Only works if user can upload files
107
+ if(current_user_can('upload_files')){
108
+ add_action('show_user_profile', array('wp_user_avatar','action_show_user_profile'));
109
+ add_action('edit_user_profile', array($this,'action_show_user_profile'));
110
+ add_action('personal_options_update', array($this,'action_process_option_update'));
111
+ add_action('edit_user_profile_update', array($this,'action_process_option_update'));
112
+ add_action('discussion_update', array($this,'action_process_option_update'));
113
+ add_action('admin_enqueue_scripts', array($this, 'media_upload_scripts'));
114
+ add_action('wp_enqueue_scripts', array($this, 'media_upload_scripts'));
115
+ }
116
+ // Only add attachment field for WP 3.4 and older
117
+ if(!function_exists('wp_enqueue_media')){
118
+ add_filter('attachment_fields_to_edit', array($this, 'add_wp_user_avatar_attachment_field_to_edit'), 10, 2);
119
+ }
120
+ // Hide column in Users table if avatars are shown
121
+ if(get_option('show_avatars') != '1'){
122
+ add_filter('manage_users_columns', array($this, 'add_wp_user_avatar_column'), 10, 1);
123
+ add_filter('manage_users_custom_column', array($this, 'show_wp_user_avatar_column'), 10, 3);
124
+ }
125
+ }
126
+
127
+ // Add to edit user profile
128
+ function action_show_user_profile($user){
129
+ global $current_user, $wpdb, $blog_id;
130
+ $wp_user_avatar = get_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
131
+ $hide_notice = has_wp_user_avatar($user->ID) ? ' class="hide-me"' : '';
132
+ $hide_remove = !has_wp_user_avatar($user->ID) ? ' hide-me' : '';
133
+ $avatar_medium_src = (get_option('show_avatars') == '1') ? get_avatar_original($user->user_email, 96) : includes_url().'images/blank.gif';
134
+ $avatar_medium = has_wp_user_avatar($user->ID) ? get_wp_user_avatar_src($user->ID, 'medium') : $avatar_medium_src;
135
+ $profile = ($current_user->ID == $user->ID) ? 'Profile' : 'User';
136
+ ?>
137
+ <?php if(class_exists('bbPress')) : ?>
138
+ <h2 class="entry-title"><?php _e('WP User Avatar'); ?></h2>
139
+ <fieldset class="bbp-form">
140
+ <legend><?php _e('WP User Avatar'); ?></legend>
141
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wp_user_avatar; ?>" />
142
+ <p><button type="button" class="button" id="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
143
+ <p id="wp-user-avatar-preview"><?php echo '<img src="'.$avatar_medium.'" alt="" />'; ?></p>
144
+ <?php if(get_option('show_avatars') == '1') : ?>
145
+ <p id="wp-user-avatar-notice"<?php echo $hide_notice; ?>><?php _e('This is your default avatar.'); ?></p>
146
+ <?php endif; ?>
147
+ <p><button type="button" class="button<?php echo $hide_remove; ?>" id="remove-wp-user-avatar">><?php _e('Remove'); ?></button></p>
148
+ <p id="wp-user-avatar-message"><?php _e('Press "Update '.$profile.'" to save your changes.'); ?></p>
149
+ </fieldset>
150
+ <?php else : ?>
151
+ <h3><?php _e('WP User Avatar') ?></h3>
152
+ <table class="form-table">
153
+ <tr>
154
+ <th><label for="wp_user_avatar"><?php _e('WP User Avatar'); ?></label></th>
155
+ <td>
156
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wp_user_avatar; ?>" />
157
+ <p><button type="button" class="button" id="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
158
+ <p id="wp-user-avatar-preview"><?php echo '<img src="'.$avatar_medium.'" alt="" />'; ?></p>
159
+ <?php if(get_option('show_avatars') == '1') : ?>
160
+ <p id="wp-user-avatar-notice"<?php echo $hide_notice; ?>><?php _e('This is your default avatar.'); ?></p>
161
+ <?php endif; ?>
162
+ <p><button type="button" class="button<?php echo $hide_remove; ?>" id="remove-wp-user-avatar"><?php _e('Remove'); ?></button></p>
163
+ <p id="wp-user-avatar-message"><?php _e('Press "Update '.$profile.'" to save your changes.'); ?></p>
164
+ </td>
165
+ </tr>
166
+ </table>
167
+ <?php endif; ?>
168
+ <?php
169
+ echo edit_default_wp_user_avatar($user->display_name, $avatar_medium_src, $avatar_medium_src);
170
+ }
171
+
172
+ // Update user meta
173
+ function action_process_option_update($user_id){
174
+ global $wpdb, $blog_id;
175
+ update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', (isset($_POST['wp-user-avatar']) ? intval($_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
+ global $wpdb, $blog_id;
198
+ $wp_user_avatar = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
199
+ $wp_user_avatar_image = wp_get_attachment_image($wp_user_avatar, array(32,32));
200
+ if($column_name == 'wp-user-avatar'){
201
+ return $wp_user_avatar_image;
202
+ }
203
+ }
204
+
205
+ // Media uploader
206
+ function media_upload_scripts(){
207
+ global $ssl;
208
+ if(function_exists('wp_enqueue_media')){
209
+ wp_enqueue_media();
210
+ } else {
211
+ wp_enqueue_script('media-upload');
212
+ wp_enqueue_script('thickbox');
213
+ wp_enqueue_style('thickbox');
214
+ }
215
+ wp_enqueue_script('wp-user-avatar', WP_USER_AVATAR_URLPATH.'js/wp-user-avatar.js', '', WP_USER_AVATAR_VERSION);
216
+ wp_enqueue_style('wp-user-avatar', WP_USER_AVATAR_URLPATH.'css/wp-user-avatar.css', '', WP_USER_AVATAR_VERSION);
217
+ }
218
+ }
219
+
220
+ // Uploader scripts
221
+ function edit_default_wp_user_avatar($section, $avatar_medium, $avatar_thumb){ ?>
222
+ <script type="text/javascript">
223
+ jQuery(function(){
224
+ <?php if(function_exists('wp_enqueue_media')) : // Backbone uploader for WP 3.5+ ?>
225
+ openMediaUploader("<?php echo $section; ?>");
226
+ <?php else : // Fall back to Thickbox uploader ?>
227
+ openThickboxUploader("<?php echo $section; ?>", "<?php echo get_admin_url(); ?>media-upload.php?post_id=0&type=image&tab=library&TB_iframe=1");
228
+ <?php endif; ?>
229
+ removeWPUserAvatar("<?php echo htmlspecialchars_decode($avatar_medium); ?>", "<?php echo htmlspecialchars_decode($avatar_thumb); ?>");
230
+ });
231
+ </script>
232
+ <?php
233
+ }
234
+
235
+ // Returns true if user has Gravatar-hosted image
236
+ function has_gravatar($id_or_email, $has_gravatar=false){
237
+ global $ssl;
238
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
239
+ $email = !empty($user) ? $user->user_email : $id_or_email;
240
+ if(!empty($email)){
241
+ $hash = md5(strtolower(trim($email)));
242
+ $gravatar = 'http'.$ssl.'://www.gravatar.com/avatar/'.$hash.'?d=404';
243
+ $headers = @get_headers($gravatar);
244
+ $has_gravatar = !preg_match("|200|", $headers[0]) ? false : true;
245
+ }
246
+ return $has_gravatar;
247
+ }
248
+
249
+ // Returns true if user has wp_user_avatar
250
+ function has_wp_user_avatar($user_id=''){
251
+ global $post, $wpdb, $blog_id;
252
+ if(empty($user_id)){
253
+ $author_name = get_query_var('author_name');
254
+ $user = is_author() ? get_user_by('slug', $author_name) : get_the_author_meta('ID');
255
+ $user_id = $user->ID;
256
+ }
257
+ $wp_user_avatar = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
258
+ if(!empty($wp_user_avatar)){
259
+ return true;
260
+ }
261
+ }
262
+
263
+ // Find wp_user_avatar, show get_avatar if empty
264
+ function get_wp_user_avatar($id_or_email='', $size='96', $align=''){
265
+ global $post, $comment, $avatar_default, $wpdb, $blog_id;
266
+ // Find user ID on comment, author page, or post
267
+ if(is_object($id_or_email)){
268
+ if($comment->user_id != '0'){
269
+ $id_or_email = $comment->user_id;
270
+ $user = get_user_by('id', $id_or_email);
271
+ $email = $user->user_email;
272
+ } elseif(!empty($comment->comment_author_email)){
273
+ $user = get_user_by('email', $comment->comment_author_email);
274
+ $id_or_email = !empty($user) ? $user->ID : $comment->comment_author_email;
275
+ $email = !empty($user) ? $user->user_email : $id_or_email;
276
+ }
277
+ $alt = $comment->comment_author;
278
+ } else {
279
+ if(!empty($id_or_email)){
280
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
281
+ } else {
282
+ $author_name = get_query_var('author_name');
283
+ if(is_author()){
284
+ $user = get_user_by('slug', $author_name);
285
+ } else {
286
+ $user_id = get_the_author_meta('ID');
287
+ $user = get_user_by('id', $user_id);
288
+ }
289
+ }
290
+ $id_or_email = !empty($user) ? $user->ID : '';
291
+ $alt = !empty($user) ? $user->display_name : '';
292
+ }
293
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email) : '';
294
+ $alignclass = !empty($align) ? ' align'.$align : '';
295
+ if(!empty($wp_user_avatar_meta)){
296
+ $get_size = is_numeric($size) ? array($size,$size) : $size;
297
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, $get_size);
298
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
299
+ $wp_user_avatar = '<img src="'.$wp_user_avatar_image[0].'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.$alignclass.' avatar avatar avatar-'.$size.' photo" />';
300
+ } else {
301
+ if($size == 'original' || $size == 'large' || $size == 'medium' || $size == 'thumbnail'){
302
+ $get_size = ($size == 'original') ? get_option('large_size_w') : get_option($size.'_size_w');
303
+ } else {
304
+ $get_size = $size;
305
+ }
306
+ $avatar = get_avatar($id_or_email, $get_size, $default='', $alt='');
307
+ if(!is_numeric($size)){
308
+ $avatar = preg_replace("/(width|height)=\'\d*\'\s/", '', $avatar);
309
+ $avatar = preg_replace('/(width|height)=\"\d*\"\s/', '', $avatar);
310
+ $avatar = str_replace('wp-user-avatar wp-user-avatar-'.$get_size.' ', '', $avatar);
311
+ $avatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$size.$alignclass." ", $avatar);
312
+ }
313
+ $wp_user_avatar = $avatar;
314
+ }
315
+ return $wp_user_avatar;
316
+ }
317
+
318
+ // Return just the image src
319
+ function get_wp_user_avatar_src($id_or_email, $size='', $align=''){
320
+ $wp_user_avatar_image = get_wp_user_avatar($id_or_email, $size, $align);
321
+ $wp_user_avatar_image_src = '';
322
+ if(!empty($wp_user_avatar_image)){
323
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER);
324
+ $wp_user_avatar_image_src = $matches [0] [1];
325
+ }
326
+ return $wp_user_avatar_image_src;
327
+ }
328
+
329
+ // Replace get_avatar
330
+ function get_wp_user_avatar_alt($avatar, $id_or_email, $size='', $default='', $alt=''){
331
+ global $post, $pagenow, $comment, $avatar_default, $avatar_default_wp_user_avatar, $mustache_original, $mustache_medium, $mustache_thumbnail, $mustache_avatar, $mustache_admin, $wpdb, $blog_id;
332
+ // Find user ID on comment, author page, or post
333
+ $email = '';
334
+ if(is_object($id_or_email)){
335
+ if($comment->user_id != '0'){
336
+ $id_or_email = $comment->user_id;
337
+ $user = get_user_by('id', $id_or_email);
338
+ $email = $user->user_email;
339
+ } elseif(!empty($comment->comment_author_email)){
340
+ $user = get_user_by('email', $comment->comment_author_email);
341
+ $id_or_email = !empty($user) ? $user->ID : $comment->comment_author_email;
342
+ $email = !empty($user) ? $user->user_email : $id_or_email;
343
+ }
344
+ $alt = $comment->comment_author;
345
+ } else {
346
+ if(!empty($id_or_email)){
347
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
348
+ } else {
349
+ $author_name = get_query_var('author_name');
350
+ if(is_author()){
351
+ $user = get_user_by('slug', $author_name);
352
+ } else {
353
+ $user_id = get_the_author_meta('ID');
354
+ $user = get_user_by('id', $user_id);
355
+ }
356
+ }
357
+ $id_or_email = !empty($user) ? $user->ID : '';
358
+ $email = !empty($user) ? $user->user_email : '';
359
+ $alt = !empty($user) ? $user->display_name : '';
360
+ }
361
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email) : '';
362
+ if(!empty($wp_user_avatar_meta)){
363
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, array($size,$size));
364
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
365
+ $default = $wp_user_avatar_image[0];
366
+ $avatar = '<img src="'.$default.'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.' avatar avatar-'.$size.' photo" />';
367
+ } else {
368
+ if(!has_gravatar($email) && $avatar_default == 'wp_user_avatar'){
369
+ if(!empty($avatar_default_wp_user_avatar)){
370
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array($size,$size));
371
+ $default = $avatar_default_wp_user_avatar_image[0];
372
+ $dimensions = is_numeric($size) ? ' width="'.$avatar_default_wp_user_avatar_image[1].'" height="'.$avatar_default_wp_user_avatar_image[2].'"' : '';
373
+ } else {
374
+ if($size > get_option('medium_size_w')){
375
+ $default = $mustache_original;
376
+ } elseif($size <= get_option('medium_size_w') && $size > get_option('thumbnail_size_w')){
377
+ $default = $mustache_medium;
378
+ } elseif($size <= get_option('thumbnail_size_w') && $size > 96){
379
+ $default = $mustache_thumbnail;
380
+ } elseif($size <= 96 && $size > 32){
381
+ $default = $mustache_avatar;
382
+ } elseif($size <= 32){
383
+ $default = $mustache_admin;
384
+ }
385
+ $dimensions = is_numeric($size) ? ' width="'.$size.'" height="'.$size.'"' : '';
386
+ }
387
+ $avatar = "<img src='".$default."'".$dimensions." alt='".$alt."' class='wp-user-avatar wp-user-avatar-".$size." avatar avatar-".$size." photo avatar-default' />";
388
+ }
389
+ }
390
+ $wp_user_avatar = $avatar;
391
+ return $wp_user_avatar;
392
+ }
393
+ add_filter('get_avatar', 'get_wp_user_avatar_alt', 10, 6);
394
+
395
+ // Get original avatar
396
+ function get_avatar_original($id_or_email, $size='', $default='', $alt=''){
397
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_avatar;
398
+ remove_filter('get_avatar', 'get_wp_user_avatar_alt');
399
+ if(!has_gravatar($id_or_email) && $avatar_default == 'wp_user_avatar'){
400
+ if(!empty($avatar_default_wp_user_avatar)){
401
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array($size,$size));
402
+ $default = $avatar_default_wp_user_avatar_image[0];
403
+ } else {
404
+ $default = $mustache_avatar;
405
+ }
406
+ } else {
407
+ $wp_user_avatar_image = get_avatar($id_or_email);
408
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER);
409
+ $default = $matches [0] [1];
410
+ }
411
+ return $default;
412
+ }
413
+
414
+ // Shortcode
415
+ function wp_user_avatar_shortcode($atts, $content){
416
+ global $wpdb, $blog_id;
417
+ // EXAMPLE USAGE:
418
+ // [avatar size="medium"]
419
+
420
+ // Set shortcode attributes
421
+ extract(shortcode_atts(array('user' => '', 'size' => '96', 'align' => '', 'link' => ''), $atts));
422
+ $get_user = '';
423
+ if(!empty($user)){
424
+ $get_user = is_numeric($user) ? get_user_by('id', $user) : get_user_by('login', $user);
425
+ $get_user = !empty($get_user) ? $get_user : get_user_by('slug', $user);
426
+ $get_user = !empty($get_user) ? $get_user : get_user_by('email', $user);
427
+ }
428
+ $id_or_email = !empty($get_user) ? $get_user->ID : '';
429
+ if(!empty($link)){
430
+ $link_class = $link;
431
+ if($link == 'file'){
432
+ $image_link = get_wp_user_avatar_src($id_or_email, 'original', $align);
433
+ } elseif($link == 'attachment'){
434
+ $image_link = get_attachment_link(get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email));
435
+ } else {
436
+ $image_link = $link;
437
+ $link_class = 'custom';
438
+ }
439
+ $wp_user_avatar = '<a href="'.$image_link.'" class="wp-user-avatar-link wp-user-avatar-'.$link_class.'">'.get_wp_user_avatar($id_or_email, $size, $align).'</a>';
440
+ } else {
441
+ $wp_user_avatar = get_wp_user_avatar($id_or_email, $size, $align);
442
+ }
443
+ return $wp_user_avatar;
444
+ }
445
+ add_shortcode('avatar','wp_user_avatar_shortcode');
446
+
447
+ // Add default avatar
448
+ function add_default_wp_user_avatar($avatar_list){
449
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_medium, $mustache_admin;
450
+ remove_filter('get_avatar', 'get_wp_user_avatar_alt');
451
+ $avatar_defaults = array(
452
+ 'mystery' => __('Mystery Man'),
453
+ 'blank' => __('Blank'),
454
+ 'gravatar_default' => __('Gravatar Logo'),
455
+ 'identicon' => __('Identicon (Generated)'),
456
+ 'wavatar' => __('Wavatar (Generated)'),
457
+ 'monsterid' => __('MonsterID (Generated)'),
458
+ 'retro' => __('Retro (Generated)')
459
+ );
460
+ if(empty($avatar_default)){
461
+ $avatar_default = 'mystery';
462
+ }
463
+ $size = 32;
464
+ $avatar_list = '';
465
+ foreach($avatar_defaults as $default_key => $default_name){
466
+ $selected = ($avatar_default == $default_key) ? 'checked="checked" ' : '';
467
+ $avatar_list .= "\n\t<label><input type='radio' name='avatar_default' id='avatar_{$default_key}' value='".esc_attr($default_key)."' {$selected}/> ";
468
+ $avatar = get_avatar('unknown@gravatar.com', $size, $default_key);
469
+ $avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&amp;forcedefault=1'", $avatar);
470
+ $avatar_list .= ' '.$default_name.'</label>';
471
+ $avatar_list .= '<br />';
472
+ }
473
+ if(!empty($avatar_default_wp_user_avatar)){
474
+ $avatar_medium_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, 'medium');
475
+ $avatar_thumb_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array(32,32));
476
+ $avatar_medium = $avatar_medium_src[0];
477
+ $avatar_thumb = $avatar_thumb_src[0];
478
+ $hide_remove = '';
479
+ } else {
480
+ $avatar_medium = $mustache_medium;
481
+ $avatar_thumb = $mustache_admin;
482
+ $hide_remove = ' class="hide-me"';
483
+ }
484
+ $selected_avatar = ($avatar_default == 'wp_user_avatar') ? ' checked="checked" ' : '';
485
+ $avatar_thumb_img = '<div id="wp-user-avatar-preview"><img src="'.$avatar_thumb.'" width="32" /></div>';
486
+ $wp_user_avatar_list = "\n\t<label><input type='radio' name='avatar_default' id='wp_user_avatar_radio' value='wp_user_avatar'$selected_avatar /> ";
487
+ $wp_user_avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1'", $avatar_thumb_img);
488
+ $wp_user_avatar_list .= ' '.__('WP User Avatar').'</label>';
489
+ $wp_user_avatar_list .= '<p id="edit-wp-user-avatar"><button type="button" class="button" id="add-wp-user-avatar">'.__('Edit WP User Avatar').'</button>';
490
+ $wp_user_avatar_list .= '<a href="#" id="remove-wp-user-avatar"'.$hide_remove.'>'.__('Remove').'</a></p>';
491
+ $wp_user_avatar_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="'.$avatar_default_wp_user_avatar.'">';
492
+ $wp_user_avatar_list .= '<p id="wp-user-avatar-message">'.__('Press "Save Changes" to save your changes.').'</p>';
493
+ $wp_user_avatar_list .= edit_default_wp_user_avatar('Default Avatar', $mustache_medium, $mustache_admin);
494
+ return $wp_user_avatar_list.$avatar_list;
495
+ }
496
+ add_filter('default_avatar_select', 'add_default_wp_user_avatar');
497
+
498
+ // Add default avatar field to whitelist
499
+ function wp_user_avatar_whitelist_options($whitelist_options){
500
+ $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');
501
+ return $whitelist_options;
502
+ }
503
+ add_filter('whitelist_options', 'wp_user_avatar_whitelist_options');
504
+
505
+ // Initialize wp_user_avatar
506
+ function wp_user_avatar_load(){
507
+ global $wp_user_avatar_instance;
508
+ $wp_user_avatar_instance = new wp_user_avatar();
509
+ }
510
+ add_action('plugins_loaded','wp_user_avatar_load');
511
+ }
512
+ ?>