WP User Avatar - Version 1.4

Version Description

  • Add: Uploader for Subscribers and Contributors
  • Add: Media states for avatar images
  • Add: Plugin admin settings
  • Update: Change support only to WP 3.4+
Download this release

Release Info

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

Version 1.4

css/wp-user-avatar.css ADDED
@@ -0,0 +1,10 @@
1
+ #wp-user-avatar-errors { color: #c00; font-weight: 700; }
2
+ #wp-user-avatar-message { color: #c00; display: none; }
3
+ #wp-user-avatar-preview, #wp-user-avatar-thumbnail { display: inline-block; text-align: center; vertical-align: top; }
4
+ #wp-user-avatar-preview { margin-right: 10px; }
5
+ #wp-user-avatar-preview img, #wp-user-avatar-thumbnail img { height: 96px; border: 1px solid #dfdfdf; display: block; }
6
+ .defaultavatarpicker #wp-user-avatar-preview { width: 32px; height: 32px; margin-right: 0; display: inline-block; overflow: hidden; vertical-align: middle; }
7
+ .defaultavatarpicker #wp-user-avatar-preview img { width: 32px; height: auto; border: 0; }
8
+ #edit-wp-user-avatar { padding-left: 15px; }
9
+ #edit-wp-user-avatar #remove-wp-user-avatar { margin-left: 10px; }
10
+ .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,47 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.4
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
+
12
+ function wpua_myplugin_addbuttons(){
13
+ // Don't bother doing this stuff if the current user lacks permissions
14
+ if(!current_user_can('edit_posts') && !current_user_can('edit_pages')){
15
+ return;
16
+ }
17
+ // Add only in Rich Editor mode
18
+ if(get_user_option('rich_editing') == 'true'){
19
+ add_filter('mce_external_plugins', 'wpua_add_myplugin_tinymce_plugin');
20
+ add_filter('mce_buttons', 'wpua_register_myplugin_button');
21
+ }
22
+ }
23
+ // init process for button control
24
+ add_action('init', 'wpua_myplugin_addbuttons');
25
+
26
+
27
+ function wpua_register_myplugin_button($buttons){
28
+ array_push($buttons, 'separator', 'wpUserAvatar');
29
+ return $buttons;
30
+ }
31
+
32
+ // Load the TinyMCE plugin : editor_plugin.js (wp2.5)
33
+ function wpua_add_myplugin_tinymce_plugin($plugin_array){
34
+ $plugin_array['wpUserAvatar'] = WPUA_URLPATH.'includes/tinymce/editor_plugin.js';
35
+ return $plugin_array;
36
+ }
37
+
38
+ // Call TinyMCE window content via admin-ajax
39
+ function wpua_ajax_tinymce(){
40
+ if(!current_user_can('edit_posts') && !current_user_can('edit_pages')){
41
+ die('You are not allowed to call this page directly.');
42
+ }
43
+ include_once(WPUA_ABSPATH.'includes/tinymce/window.php');
44
+ die();
45
+ }
46
+ add_action('wp_ajax_wp_user_avatar_tinymce', 'wpua_ajax_tinymce');
47
+ ?>
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:295,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.4"}}});tinymce.PluginManager.add('wpUserAvatar',tinymce.plugins.wpUserAvatar)})();
includes/tinymce/window.php ADDED
@@ -0,0 +1,119 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.4
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
+ ?>
12
+ <html xmlns="http://www.w3.org/1999/xhtml">
13
+ <head>
14
+ <title>WP User Avatar</title>
15
+ <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
16
+ <base target="_self" />
17
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/jquery/jquery.js"></script>
18
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/tinymce/tiny_mce_popup.js"></script>
19
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/tinymce/utils/form_utils.js"></script>
20
+ <script type="text/javascript">
21
+ function insert_wp_user_avatar(){
22
+ // Custom shortcode values
23
+ var shortcode;
24
+ var user = document.getElementById('wp_user_avatar_user').value;
25
+ var size = document.getElementById('wp_user_avatar_size').value;
26
+ var size_number = document.getElementById('wp_user_avatar_size_number').value;
27
+ var align = document.getElementById('wp_user_avatar_align').value;
28
+ var link = document.getElementById('wp_user_avatar_link').value;
29
+ var link_external = document.getElementById('wp_user_avatar_link_external').value;
30
+ var target = document.getElementById('wp_user_avatar_target').value;
31
+
32
+ // Add tag to shortcode only if not blank
33
+ var user_tag = (user != '') ? ' user="' + user + '"' : '';
34
+ var size_tag = (size != '' && size_number == '') ? ' size="' + size + '"' : '';
35
+ size_tag = (size_number != '') ? ' size="' + size_number + '"' : size_tag;
36
+ var align_tag = (align != '') ? ' align="' + align + '"' : '';
37
+ var link_tag = (link != '' && link_external == '') ? ' link="' + link + '"' : '';
38
+ link_tag = (link_external != '') ? ' link="' + link_external + '"' : link_tag;
39
+ var target_tag = document.getElementById('wp_user_avatar_target').checked && (link_tag != '') ? ' target="' + target + '"' : '';
40
+
41
+ shortcode = "<p>[avatar" + user_tag + size_tag + align_tag + link_tag + target_tag + "]</p>";
42
+
43
+ if(window.tinyMCE){
44
+ window.tinyMCE.execInstanceCommand(window.tinyMCE.activeEditor.id, 'mceInsertContent', false, shortcode);
45
+ tinyMCEPopup.editor.execCommand('mceRepaint');
46
+ tinyMCEPopup.close();
47
+ }
48
+ return;
49
+ }
50
+ </script>
51
+ <style type="text/css">
52
+ form { background: #fff; border: 1px solid #eee; }
53
+ p, h4 { margin: 0; padding: 12px 0 0; }
54
+ h4.center { text-align: center; }
55
+ label { width: 90px; display: inline-block; text-align: right; }
56
+ .mceActionPanel { padding: 7px 0 12px; text-align: center; }
57
+ .mceActionPanel #insert { float: none; width: 180px; margin: 0 auto; }
58
+ </style>
59
+ </head>
60
+ <body id="link" class="wp-core-ui" onload="document.body.style.display='';" style="display:none;">
61
+ <form name="wpUserAvatar" action="#">
62
+ <p><label for="<?php esc_attr_e('wp_user_avatar_user'); ?>"><strong><?php _e("User:"); ?></strong></label>
63
+ <select id="<?php esc_attr_e('wp_user_avatar_user'); ?>" name="<?php esc_attr_e('wp_user_avatar_user'); ?>">
64
+ <option value=""></option>
65
+ <?php $users = get_users(); foreach($users as $user) : ?>
66
+ <option value="<?php echo $user->user_login; ?>"><?php echo $user->display_name; ?></option>
67
+ <?php endforeach; ?>
68
+ </select></p>
69
+
70
+ <h4 class="center">Choose a preset size or enter a number value.</h4>
71
+
72
+ <p>
73
+ <label for="<?php esc_attr_e('wp_user_avatar_size'); ?>"><strong><?php _e("Size:"); ?></strong></label>
74
+ <select id="<?php esc_attr_e('wp_user_avatar_size'); ?>" name="<?php esc_attr_e('wp_user_avatar_size'); ?>">
75
+ <option value=""></option>
76
+ <option value="original"><?php _e("Original"); ?></option>
77
+ <option value="large"><?php _e("Large"); ?></option>
78
+ <option value="medium"><?php _e("Medium"); ?></option>
79
+ <option value="thumbnail"><?php _e("Thumbnail"); ?></option>
80
+ </select>
81
+ or
82
+ <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="" />
83
+ </p>
84
+
85
+ <p><label for="<?php esc_attr_e('wp_user_avatar_align'); ?>"><strong><?php _e("Alignment:"); ?></strong></label>
86
+ <select id="<?php esc_attr_e('wp_user_avatar_align'); ?>" name="<?php esc_attr_e('wp_user_avatar_align'); ?>">
87
+ <option value=""></option>
88
+ <option value="center"><?php _e("Center"); ?></option>
89
+ <option value="left"><?php _e("Left"); ?></option>
90
+ <option value="right"><?php _e("Right"); ?></option>
91
+ </select></p>
92
+
93
+ <h4 class="center">Link to image file, attachment page, or custom URL.</h4>
94
+
95
+ <p>
96
+ <label for="<?php esc_attr_e('wp_user_avatar_link'); ?>"><strong><?php _e("Link to:"); ?></strong></label>
97
+ <select id="<?php esc_attr_e('wp_user_avatar_link'); ?>" name="<?php esc_attr_e('wp_user_avatar_link'); ?>">
98
+ <option value=""></option>
99
+ <option value="file"><?php _e("Image File"); ?></option>
100
+ <option value="attachment"><?php _e("Attachment Page"); ?></option>
101
+ </select>
102
+ </p>
103
+
104
+ <p>
105
+ <label for="<?php esc_attr_e('wp_user_avatar_link_external'); ?>">or</label>
106
+ <input type="text" size="36" id="<?php esc_attr_e('wp_user_avatar_link_external'); ?>" name="<?php esc_attr_e('wp_user_avatar_link_external'); ?>" value="" />
107
+ </p>
108
+
109
+ <p>
110
+ <label for="<?php esc_attr_e('wp_user_avatar_target'); ?>"></label>
111
+ <input type="checkbox" id="<?php esc_attr_e('wp_user_avatar_target'); ?>" name="<?php esc_attr_e('wp_user_avatar_target'); ?>" value="_blank" /> <strong>Open link in a new window</strong>
112
+ </p>
113
+
114
+ <div class="mceActionPanel">
115
+ <input type="submit" id="insert" class="button-primary" name="insert" value="<?php _e("Insert WP User Avatar"); ?>" onclick="insert_wp_user_avatar();" />
116
+ </div>
117
+ </form>
118
+ </body>
119
+ </html>
index.html ADDED
@@ -0,0 +1 @@
1
+ <!-- Thanks for using WP User Avatar! -->
js/wp-user-avatar.js ADDED
@@ -0,0 +1 @@
1
+ eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('3 1q(c){0.7.k={M:3(){v 0.7.x.y.i.o},X:3(a){s b=0.7.x.y;b.i.o=a;b.i.T=2(\'19.h-18\').K(\'L\').m(\'q\');r(b.i.o){R(b.i.o,b.i.T);2(\'#t\').u(\'g\')}},w:3(){r(6.8){v 6.8}6.8=0.7({z:\'Q\',12:[1r 0.7.1d.1a({13:"V H I J: "+c})]});6.8.j(\'S\',3(){s a=6.z().M(\'l\');D=2(\'#0-4-5\').F();h=0.7.h(D);h.17();a.E(h?[h]:[])},6.8);6.8.j(\'1b:1c:p\',3(a){6.1e(a,{1f:\'1g H I J\'})},6.8);6.8.z(\'Q\').j(\'p\',6.p);v 6.8},p:3(a){s b=0.7.x.y,l=6.M(\'l\').1h();0.7.k.X(l?l.D:-1)},P:3(){2(\'C\').j(\'g\',\'#E-0-4-5\',3(e){e.A();e.14();0.7.k.w().S()})}};2(0.7.k.P)}3 15(a,b){2(\'C\').j(\'g\',\'#E-0-4-5\',3(e){e.A();16(\'V H I J: \'+a,b)})}3 R(a,b){2(\'#0-4-5\',f.d.9).F(a);2(\'#0-4-5-O\',f.d.9).K(\'L\').m(\'q\',b).n(\'U\',\'\').n(\'N\',\'\');2(\'#0-4-5-W\',f.d.9).G();2(\'#Y-0-4-5\',f.d.9).G();2(\'#0-4-5-Z\',f.d.9).B();2(\'#t\',f.d.9).u(\'g\');r(1i(0)!=\'1j\'){0.7.k.w().1k()}1l{f.d.1m()}}3 1n(a){2(\'C\').j(\'g\',\'#Y-0-4-5\',3(e){e.A();2(6).B();2(\'#1o-0-4-5, #0-4-5-Z\').B();2(\'#0-4-5-O\').K(\'L\').m(\'q\',a).n(\'U\',\'\').n(\'N\',\'\');2(\'#0-4-5\').F(\'\');2(\'#0-4-5-W\').G();2(\'#t\').u(\'g\')})}2(9).1p(3(){2(\'#10-11\',\'#1s-10-11\').m(\'1t\',\'1u/1v-1w\')});',62,95,'wp||jQuery|function|user|avatar|this|media|_frame|document||||parent||window|click|attachment|post|on|wpUserAvatar|selection|attr|removeAttr|wpUserAvatarId|select|src|if|var|wp_user_avatar_radio|trigger|return|frame|view|settings|state|preventDefault|hide|body|id|add|val|show|WP|User|Avatar|find|img|get|height|preview|init|library|setWPUserAvatar|open|wpUserAvatarSrc|width|Edit|message|set|remove|thumbnail|your|profile|states|title|stopPropagation|openThickboxUploader|tb_show|fetch|info|div|Library|toolbar|create|controller|createSelectToolbar|text|Set|single|typeof|undefined|close|else|tb_remove|removeWPUserAvatar|edit|ready|openMediaUploader|new|bbp|enctype|multipart|form|data'.split('|'),0,{}))
readme.txt ADDED
@@ -0,0 +1,350 @@
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.4
7
+ Tested up to: 3.5.1
8
+ Stable tag: 1.4
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
+ * Allow Contributors and Subscribers to upload their own avatars.
24
+
25
+ This plugin uses the Media uploader introduced in WordPress 3.5, but is also backwards-compatible to WordPress 3.4. It is also compatible with WordPress Multisite.
26
+
27
+ == Installation ==
28
+
29
+ 1. Download, install, and activate the WP User Avatar plugin.
30
+ 2. On your edit profile page, click "Edit WP User Avatar".
31
+ 3. Choose an image, then click "Set WP User Avatar".
32
+ 4. Click "Update Profile".
33
+ 5. Upload your own Default Avatar in your Discussion settings (optional).
34
+ 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/).
35
+ 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.
36
+
37
+ **Example Usage**
38
+
39
+ = Posts =
40
+
41
+ Within [The Loop](http://codex.wordpress.org/The_Loop), you may be using:
42
+
43
+ `<?php echo get_avatar(get_the_author_meta('ID'), 96); ?>`
44
+
45
+ Replace this function with:
46
+
47
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96); ?>`
48
+
49
+ You can also use the values "original", "large", "medium", or "thumbnail" for your avatar size:
50
+
51
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 'medium'); ?>`
52
+
53
+ You can also add an alignment of "left", "right", or "center":
54
+
55
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96, 'left'); ?>`
56
+
57
+ = Author Page =
58
+
59
+ On an author page outside of [The Loop](http://codex.wordpress.org/The_Loop), you may be using:
60
+
61
+ `<?php
62
+ $user = get_user_by('slug', $author_name);
63
+ echo get_avatar($user->ID, 96);
64
+ ?>`
65
+
66
+ Replace this function with:
67
+
68
+ `<?php
69
+ $user = get_user_by('slug', $author_name);
70
+ echo get_wp_user_avatar($user->ID, 96);
71
+ ?>`
72
+
73
+ 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:
74
+
75
+ `<?php echo get_wp_user_avatar(); ?>`
76
+
77
+ 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.
78
+
79
+ = Comments =
80
+
81
+ For comments, you might have in your template:
82
+
83
+ `<?php echo get_avatar($comment, 32); ?>`
84
+
85
+ Replace this function with:
86
+
87
+ `<?php echo get_wp_user_avatar($comment, 32); ?>`
88
+
89
+ For comments, you must specify the $comment variable.
90
+
91
+ **Other Available Functions**
92
+
93
+ = [avatar] shortcode =
94
+
95
+ 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.
96
+
97
+ `[avatar user="admin" size="medium" align="left" link="file"]`
98
+
99
+ = get_wp_user_avatar_src =
100
+
101
+ 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:
102
+
103
+ `<a href="<?php echo get_wp_user_avatar_src($user_id, 'large'); ?>">
104
+ <?php echo get_wp_user_avatar($user_id, 'thumbnail'); ?>
105
+ </a>`
106
+
107
+ = has_wp_user_avatar =
108
+
109
+ Returns true if the user has a WP User Avatar image. You must specify the user ID:
110
+
111
+ `<?php
112
+ if ( has_wp_user_avatar($user_id) ) {
113
+ echo get_wp_user_avatar($user_id, 96);
114
+ } else {
115
+ echo '<img src="my-alternate-image.jpg" />';
116
+ }
117
+ ?>`
118
+
119
+ == Frequently Asked Questions ==
120
+
121
+ = How do I use WP User Avatar? =
122
+
123
+ 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:
124
+
125
+ = get_wp_user_avatar =
126
+
127
+ 1. Allows you to use the values "original", "large", "medium", or "thumbnail" for your avatar size.
128
+ 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.
129
+ 3. Optionally adds CSS classes "alignleft", "alignright", or "aligncenter" to position your avatar.
130
+ 4. Shows nothing if the user has no WP User Avatar image.
131
+ 5. Shows the user's [gravatar.com](http://gravatar.com/) avatar or Default Avatar only if "Show Avatars" is enabled in your Discussion settings.
132
+
133
+ = get_avatar =
134
+
135
+ 1. Requires you to enable "Show Avatars" in your Discussion settings to show any avatars.
136
+ 2. Accepts only numeric values for your avatar size.
137
+ 3. Always adds a fixed width and height to your image. This may cause problems if you use responsive CSS in your theme.
138
+ 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.)
139
+ 5. Requires no changes to your theme files if you are currently using <code>get_avatar</code>.
140
+
141
+ [Read more about get_avatar in the WordPress Function Reference](http://codex.wordpress.org/Function_Reference/get_avatar).
142
+
143
+ = Can I create a custom Default Avatar? =
144
+ In your Discussion settings, you can upload your own Default Avatar.
145
+
146
+ = Can I insert WP User Avatar directly into a post? =
147
+
148
+ 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.
149
+
150
+ `[avatar user="admin" size="96" align="left" link="file"]`
151
+
152
+ Outputs:
153
+
154
+ `<a href="{fileURL}" class="wp-user-avatar-link wp-user-avatar-file">
155
+ <img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />
156
+ </a>`
157
+
158
+ = Can Contributors or Subscribers choose their own WP User Avatar image? =
159
+ Yes, if you enable "Allow Contributors & Subscribers to upload avatars" in the WP User Avatar settings. These users will see a slightly different interface because they are allowed only one image upload.
160
+
161
+ = Will WP User Avatar work with comment author avatars? =
162
+
163
+ Yes, for registered users. Non-registered comment authors will show their [gravatar.com](http://gravatar.com/) avatars or Default Avatar.
164
+
165
+ = Will WP User Avatar work with bbPress? =
166
+
167
+ Yes!
168
+
169
+ = Will WP User Avatar work with WordPress Multisite? =
170
+
171
+ 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.
172
+
173
+ = How can I see which users have an avatar? =
174
+
175
+ 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.
176
+
177
+ = What CSS can I use with WP User Avatar? =
178
+
179
+ 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:
180
+
181
+ `<?php echo get_wp_user_avatar($user_id, 96, 'left'); ?>`
182
+
183
+ Outputs:
184
+
185
+ `<img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />`
186
+
187
+ **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.
188
+
189
+ 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:
190
+
191
+ `<?php echo get_wp_user_avatar($user_id, 'medium'); ?>`
192
+
193
+ Outputs:
194
+
195
+ `<img src="{imageURL}" class="wp-user-avatar wp-user-avatar-medium" />`
196
+
197
+ **Note:** WordPress adds more CSS classes to the avatar not listed here.
198
+
199
+ 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.
200
+
201
+ * Image File: wp-user-avatar-file
202
+ * Attachment: wp-user-avatar-attachment
203
+ * Custom URL: wp-user-avatar-custom
204
+
205
+ `[avatar user="admin" size="96" align="left" link="attachment"]`
206
+
207
+ Outputs:
208
+
209
+ `<a href="{attachmentURL}" class="wp-user-avatar-link wp-user-avatar-attachment">
210
+ <img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />
211
+ </a>`
212
+
213
+ = What other functions are available for WP User Avatar? =
214
+ * <code>get_wp_user_avatar_src</code>: retrieves just the image URL
215
+ * <code>has_wp_user_avatar</code>: checks if the user has a WP User Avatar image
216
+ * [See example usage here](http://wordpress.org/extend/plugins/wp-user-avatar/installation/)
217
+
218
+ == Screenshots ==
219
+
220
+ 1. WP User Avatar lets you upload your own Default Avatar.
221
+ 2. WP User Avatar adds a field to your edit profile page.
222
+ 3. After you've chosen a WP User Avatar image, you will see the option to remove it.
223
+ 4. WP User Avatar adds a button to insert the [avatar] shortcode in the Visual Editor.
224
+ 5. Options for the [avatar] shortcode.
225
+ 6. WP User Avatar admin settings.
226
+
227
+ == Changelog ==
228
+
229
+ = 1.4 =
230
+ * Add: Uploader for Subscribers and Contributors
231
+ * Add: Media states for avatar images
232
+ * Add: Plugin admin settings
233
+ * Update: Change support only to WP 3.4+
234
+
235
+ = 1.3.6 =
236
+ * Add: Target for link in shortcode
237
+ * Update: Clean up code and add more comments
238
+
239
+ = 1.3.5 =
240
+ * Bug Fix: Swap TinyMCE file locations
241
+
242
+ = 1.3.4 =
243
+ * Update: Change support only to WP 3.3+ because of jQuery 1.7.1 support
244
+
245
+ = 1.3.3 =
246
+ * Update: Shortcode checks for user ID, login, slug, or e-mail address
247
+ * Update: Move jquery to register_script for < WP 3.5
248
+
249
+ = 1.3.2 =
250
+ * Bug Fix: Check for user before setting name in alt tag
251
+ * Update: readme.txt
252
+
253
+ = 1.3.1 =
254
+ * Bug Fix: Rename usermeta only if found
255
+
256
+ = 1.3 =
257
+ * Add: Multisite support
258
+ * Bug Fix: Warnings if no user found
259
+ * Update: Enable action_show_user_profile for any class using show_user_profile hook
260
+
261
+ = 1.2.6 =
262
+ * Bug Fix: options-discussion.php page doesn't show default avatars
263
+
264
+ = 1.2.5 =
265
+ * Bug Fix: Comment author showing wrong avatar
266
+ * Bug Fix: Avatar adds fixed dimensions when non-numeric size is used
267
+ * Update: Use local image for default avatar instead of calling image from Gravatar
268
+
269
+ = 1.2.4 =
270
+ * Bug Fix: Show default avatar when user removes custom avatar
271
+ * Bug Fix: Default Avatar save setting
272
+
273
+ = 1.2.3 =
274
+ * Bug Fix: Show default avatar when user removes custom avatar
275
+ * Bug Fix: Default Avatar save setting
276
+
277
+ = 1.2.2 =
278
+ * Add: Ability for bbPress users to edit avatar on front profile page
279
+ * Add: Link options for shortcode
280
+ * Bug Fix: Show WP User Avatar only to users with upload_files capability
281
+
282
+ = 1.2.1 =
283
+ * Add: TinyMCE button
284
+ * Update: Clean up redundant code
285
+ * Update: Compatibility only back to WordPress 3.3
286
+
287
+ = 1.2 =
288
+ * Add: Default Avatar setting
289
+
290
+ = 1.1.7 =
291
+ * Bug Fix: Change update_usermeta to update_user_meta
292
+
293
+ = 1.1.6 =
294
+ * Bug Fix: Image not showing in user profile edit
295
+
296
+ = 1.1.5a =
297
+ * Update: readme.txt
298
+
299
+ = 1.1.5 =
300
+ * Bug Fix: Remove stray curly bracket
301
+
302
+ = 1.1.4 =
303
+ * Bug Fix: Change get_usermeta to get_user_meta
304
+ * Bug Fix: Non-object warning when retrieving user ID
305
+
306
+ = 1.1.3 =
307
+ * Bug Fix: Comment author with no e-mail address
308
+
309
+ = 1.1.2 =
310
+ * Remove: Unused variables
311
+
312
+ = 1.1.1 =
313
+ * Bug Fix: Capabilities error in comment avatar
314
+
315
+ = 1.1 =
316
+ * Add: Add filter for get_avatar
317
+ * Add: CSS alignment classes
318
+ * Add: Replace comment author avatar
319
+ * Add: Shortcode
320
+ * Update: readme.txt
321
+
322
+ = 1.0.2 =
323
+ * Update: FAQ
324
+ * Remove: CSS that hides "Insert into Post"
325
+
326
+ = 1.0.1 =
327
+ * Add: CSS classes to image output
328
+
329
+ = 1.0 =
330
+ * Initial release
331
+
332
+ == Upgrade Notice ==
333
+
334
+ = 1.4 =
335
+ * New Features: Setting to allow all users to upload avatars and to add or remove Visual Editor button, media states for avatar images
336
+
337
+ = 1.3 =
338
+ * New Feature: Multisite support
339
+
340
+ = 1.2.2 =
341
+ * New Features: Link options for shortcode, bbPress integration
342
+
343
+ = 1.2.1 =
344
+ * New Feature: Shortcode insertion button for Visual Editor
345
+
346
+ = 1.2 =
347
+ * New Feature: Default Avatar customization
348
+
349
+ = 1.1 =
350
+ * New Features: [avatar] shortcode, direct replacement of get_avatar() and comment author avatar, more CSS classes
uninstall.php ADDED
@@ -0,0 +1,46 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.4
5
+ */
6
+
7
+ // Remove user metadata and options on plugin delete
8
+ if(!defined('WP_UNINSTALL_PLUGIN')){
9
+ die('You are not allowed to call this page directly.');
10
+ }
11
+
12
+ global $wpdb, $blog_id, $switched;
13
+ $users = get_users();
14
+ // Remove settings for all sites in multisite
15
+ if(is_multisite()){
16
+ $blogs = $wpdb->get_results("SELECT * FROM $wpdb->blogs");
17
+ foreach($users as $user){
18
+ foreach($blogs as $blog){
19
+ delete_user_meta($user->ID, $wpdb->get_blog_prefix($blog->blog_id).'user_avatar');
20
+ }
21
+ }
22
+ foreach($blogs as $blog){
23
+ switch_to_blog($blog->blog_id);
24
+ delete_option('avatar_default_wp_user_avatar');
25
+ delete_option('wp_user_avatar_tinymce');
26
+ delete_option('wp_user_avatar_allow_upload');
27
+ delete_option('wp_user_avatar_default_avatar_updated');
28
+ delete_option('wp_user_avatar_users_updated');
29
+ delete_option('wp_user_avatar_media_updated');
30
+ }
31
+ } else {
32
+ foreach($users as $user){
33
+ delete_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar');
34
+ }
35
+ delete_option('avatar_default_wp_user_avatar');
36
+ delete_option('wp_user_avatar_tinymce');
37
+ delete_option('wp_user_avatar_allow_upload');
38
+ delete_option('wp_user_avatar_default_avatar_updated');
39
+ delete_option('wp_user_avatar_users_updated');
40
+ delete_option('wp_user_avatar_media_updated');
41
+ }
42
+ // Delete post meta
43
+ delete_post_meta_by_key('_wp_attachment_wp_user_avatar');
44
+ // Reset all default avatars to Mystery Man
45
+ update_option('avatar_default', 'mystery');
46
+ ?>
wp-user-avatar.php ADDED
@@ -0,0 +1,899 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.4
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.4
11
+ Author: Bangbay Siboliban
12
+ Author URI: http://siboliban.org/
13
+ */
14
+
15
+ if(!defined('ABSPATH')){
16
+ die('You are not allowed to call this page directly.');
17
+ @header('Content-Type:'.get_option('html_type').';charset='.get_option('blog_charset'));
18
+ }
19
+
20
+ // Define paths and variables
21
+ define('WPUA_VERSION', '1.4');
22
+ define('WPUA_FOLDER', basename(dirname(__FILE__)));
23
+ define('WPUA_ABSPATH', trailingslashit(str_replace('\\', '/', WP_PLUGIN_DIR.'/'.WPUA_FOLDER)));
24
+ define('WPUA_URLPATH', trailingslashit(plugins_url(WPUA_FOLDER)));
25
+
26
+ // Define global variables
27
+ $avatar_default = get_option('avatar_default');
28
+ $wpua_avatar_default = get_option('avatar_default_wp_user_avatar');
29
+ $show_avatars = get_option('show_avatars');
30
+ $wpua_tinymce = get_option('wp_user_avatar_tinymce');
31
+ $wpua_allow_upload = get_option('wp_user_avatar_allow_upload');
32
+ $mustache_original = WPUA_URLPATH.'images/wp-user-avatar.png';
33
+ $mustache_medium = WPUA_URLPATH.'images/wp-user-avatar-300x300.png';
34
+ $mustache_thumbnail = WPUA_URLPATH.'images/wp-user-avatar-150x150.png';
35
+ $mustache_avatar = WPUA_URLPATH.'images/wp-user-avatar-96x96.png';
36
+ $mustache_admin = WPUA_URLPATH.'images/wp-user-avatar-32x32.png';
37
+ $ssl = is_ssl() ? 's' : '';
38
+
39
+ // Check for updates
40
+ $wpua_default_avatar_updated = get_option('wp_user_avatar_default_avatar_updated');
41
+ $wpua_users_updated = get_option('wp_user_avatar_users_updated');
42
+ $wpua_media_updated = get_option('wp_user_avatar_media_updated');
43
+
44
+ // Load add-ons
45
+ if($wpua_tinymce == '1'){
46
+ include_once(WPUA_ABSPATH.'includes/tinymce.php');
47
+ }
48
+
49
+ // Initialize default settings
50
+ register_activation_hook(WPUA_ABSPATH.'wp-user-avatar.php', 'wpua_options');
51
+
52
+ // Remove subscribers edit_posts capability
53
+ register_deactivation_hook(WPUA_ABSPATH.'wp-user-avatar.php', 'wpua_deactivate');
54
+
55
+ // Settings saved to wp_options
56
+ function wpua_options(){
57
+ global $wp_user_roles;
58
+ add_option('avatar_default_wp_user_avatar', '');
59
+ add_option('wp_user_avatar_tinymce', '1');
60
+ add_option('wp_user_avatar_allow_upload', '0');
61
+ }
62
+ add_action('admin_init', 'wpua_options');
63
+
64
+ // Update default avatar to new format
65
+ if(empty($wpua_default_avatar_updated)){
66
+ function wpua_default_avatar(){
67
+ global $avatar_default, $wpua_avatar_default, $mustache_original;
68
+ // If default avatar is the old mustache URL, update it
69
+ if($avatar_default == $mustache_original){
70
+ update_option('avatar_default', 'wp_user_avatar');
71
+ }
72
+ // If user had an image URL as the default avatar, replace with ID instead
73
+ if(!empty($wpua_avatar_default)){
74
+ $wpua_avatar_default_image = wp_get_attachment_image_src($wpua_avatar_default, 'medium');
75
+ if($avatar_default == $wpua_avatar_default_image[0]){
76
+ update_option('avatar_default', 'wp_user_avatar');
77
+ }
78
+ }
79
+ update_option('wp_user_avatar_default_avatar_updated', '1');
80
+ }
81
+ add_action('admin_init', 'wpua_default_avatar');
82
+ }
83
+
84
+ // Rename user meta to match database settings
85
+ if(empty($wpua_users_updated)){
86
+ function wpua_user_meta(){
87
+ global $wpdb, $blog_id;
88
+ $wpua_metakey = $wpdb->get_blog_prefix($blog_id).'user_avatar';
89
+ // If database tables start with something other than wp_
90
+ if($wpua_metakey != 'wp_user_avatar'){
91
+ $users = get_users();
92
+ // Move current user metakeys to new metakeys
93
+ foreach($users as $user){
94
+ $wpua = get_user_meta($user->ID, 'wp_user_avatar', true);
95
+ if(!empty($wpua)){
96
+ update_user_meta($user->ID, $wpua_metakey, $wpua);
97
+ delete_user_meta($user->ID, 'wp_user_avatar');
98
+ }
99
+ }
100
+ }
101
+ update_option('wp_user_avatar_users_updated', '1');
102
+ }
103
+ add_action('admin_init', 'wpua_user_meta');
104
+ }
105
+
106
+ // Add media state to existing avatars
107
+ if(empty($wpua_media_updated)){
108
+ function wpua_media_state(){
109
+ global $wpdb, $blog_id;
110
+ // Find all users with WPUA
111
+ $wpua_metakey = $wpdb->get_blog_prefix($blog_id).'user_avatar';
112
+ $wpuas = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value != %d AND meta_value != %d", $wpua_metakey, 0, ''));
113
+ foreach($wpuas as $usermeta){
114
+ add_post_meta($usermeta->meta_value, '_wp_attachment_wp_user_avatar', $usermeta->user_id);
115
+ }
116
+ update_option('wp_user_avatar_media_updated', '1');
117
+ }
118
+ add_action('admin_init', 'wpua_media_state');
119
+ }
120
+
121
+ // Settings for Subscribers
122
+ if($wpua_allow_upload == '1'){
123
+ // Check user role
124
+ function check_user_role($role, $user_id=null){
125
+ global $current_user;
126
+ if(is_numeric($user_id)){
127
+ $user = get_userdata($user_id);
128
+ } else {
129
+ $user = $current_user->ID;
130
+ }
131
+ if(empty($user)){
132
+ return false;
133
+ }
134
+ return in_array($role, (array) $user->roles);
135
+ }
136
+
137
+ // Give subscribers edit_posts capability
138
+ function wpua_subscriber_add_cap(){
139
+ global $wpdb, $blog_id;
140
+ $wp_user_roles = $wpdb->get_blog_prefix($blog_id).'user_roles';
141
+ $user_roles = get_option($wp_user_roles);
142
+ $user_roles['subscriber']['capabilities']['edit_posts'] = true;
143
+ update_option($wp_user_roles, $user_roles);
144
+ }
145
+ add_action('admin_init', 'wpua_subscriber_add_cap');
146
+
147
+ // Remove menu items
148
+ function wpua_subscriber_remove_menu_pages(){
149
+ global $current_user;
150
+ if(check_user_role('subscriber', $current_user->ID)){
151
+ remove_menu_page('edit.php');
152
+ remove_menu_page('edit-comments.php');
153
+ remove_menu_page('tools.php');
154
+ }
155
+ }
156
+ add_action('admin_menu', 'wpua_subscriber_remove_menu_pages');
157
+
158
+ // Remove dashboard items
159
+ function wpua_subscriber_remove_dashboard_widgets(){
160
+ global $current_user;
161
+ if(check_user_role('subscriber', $current_user->ID)){
162
+ remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
163
+ remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side');
164
+ remove_meta_box('dashboard_right_now', 'dashboard', 'normal');
165
+ }
166
+ }
167
+ add_action('wp_dashboard_setup', 'wpua_subscriber_remove_dashboard_widgets');
168
+
169
+ // Restrict access to pages
170
+ function wpua_subscriber_offlimits(){
171
+ global $current_user, $pagenow;
172
+ $offlimits = array('edit.php', 'post-new.php', 'edit-comments.php', 'tools.php');
173
+ if(check_user_role('subscriber', $current_user->ID)){
174
+ if(in_array($pagenow, $offlimits)){
175
+ do_action('admin_page_access_denied');
176
+ wp_die(__('You do not have sufficient permissions to access this page.'));
177
+ }
178
+ }
179
+ }
180
+ add_action('admin_init', 'wpua_subscriber_offlimits');
181
+ }
182
+
183
+ // Remove subscribers edit_posts capability
184
+ function wpua_subscriber_remove_cap(){
185
+ global $wpdb, $blog_id;
186
+ $wp_user_roles = $wpdb->get_blog_prefix($blog_id).'user_roles';
187
+ $user_roles = get_option($wp_user_roles);
188
+ unset($user_roles['subscriber']['capabilities']['edit_posts']);
189
+ update_option($wp_user_roles, $user_roles);
190
+ }
191
+
192
+ // On deactivation
193
+ function wpua_deactivate(){
194
+ // Remove subscribers edit_posts capability
195
+ wpua_subscriber_remove_cap();
196
+ // Reset all default avatar to Mystery Man
197
+ update_option('avatar_default', 'mystery');
198
+ }
199
+
200
+ // WP User Avatar
201
+ if(!class_exists('wp_user_avatar')){
202
+ class wp_user_avatar{
203
+ function wp_user_avatar(){
204
+ global $current_user, $show_avatars, $wpua_allow_upload, $pagenow;
205
+ // Adds WPUA to profile
206
+ if(current_user_can('upload_files') || ($wpua_allow_upload == '1' && is_user_logged_in())){
207
+ add_action('show_user_profile', array('wp_user_avatar', 'wpua_action_show_user_profile'));
208
+ add_action('edit_user_profile', array($this, 'wpua_action_show_user_profile'));
209
+ add_action('personal_options_update', array($this, 'wpua_action_process_option_update'));
210
+ add_action('edit_user_profile_update', array($this, 'wpua_action_process_option_update'));
211
+ if(is_admin()){
212
+ // Adds scripts to admin
213
+ add_action('admin_enqueue_scripts', array($this, 'wpua_media_upload_scripts'));
214
+ // Admin settings
215
+ add_action('admin_menu', 'wpua_admin');
216
+ add_filter('plugin_action_links', array($this, 'wpua_plugin_settings_links'), 10, 2);
217
+ } else {
218
+ // Adds scripts to front pages
219
+ add_action('wp_enqueue_scripts', array($this, 'wpua_media_upload_scripts'));
220
+ }
221
+ // Only add attachment field for WP 3.4 and older
222
+ if(!function_exists('wp_enqueue_media') && $pagenow == 'media-upload.php'){
223
+ add_filter('attachment_fields_to_edit', array($this, 'wpua_add_attachment_field_to_edit'), 10, 2);
224
+ }
225
+ // Hide column in Users table if default avatars are enabled
226
+ if(is_admin() && $show_avatars != '1'){
227
+ add_filter('manage_users_columns', array($this, 'wpua_add_column'), 10, 1);
228
+ add_filter('manage_users_custom_column', array($this, 'wpua_show_column'), 10, 3);
229
+ }
230
+ }
231
+ }
232
+
233
+ // Add to edit user profile
234
+ function wpua_action_show_user_profile($user){
235
+ global $wpdb, $blog_id, $current_user, $show_avatars;
236
+ // Get WPUA attachment ID
237
+ $wpua = get_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
238
+ // Show remove button if WPUA is set
239
+ $hide_notice = has_wp_user_avatar($user->ID) ? ' class="hide-me"' : '';
240
+ $hide_remove = !has_wp_user_avatar($user->ID) ? ' hide-me' : '';
241
+ // If avatars are enabled, get original avatar image or show blank
242
+ $avatar_medium_src = ($show_avatars == '1' && is_admin()) ? wpua_get_avatar_original($user->user_email, 96) : includes_url().'images/blank.gif';
243
+ // Check if user has wp_user_avatar, if not show image from above
244
+ $avatar_medium = has_wp_user_avatar($user->ID) ? get_wp_user_avatar_src($user->ID, 'medium') : $avatar_medium_src;
245
+ // Check if user has wp_user_avatar, if not show image from above
246
+ $avatar_thumbnail = has_wp_user_avatar($user->ID) ? get_wp_user_avatar_src($user->ID, 96) : $avatar_medium_src;
247
+ // Change text on message based on current user
248
+ $profile = ($current_user->ID == $user->ID) ? 'Profile' : 'User';
249
+ // Max upload size
250
+ if(!function_exists('wp_max_upload_size')){
251
+ require_once(ABSPATH.'wp-admin/includes/template.php');
252
+ }
253
+ $upload_size_unit = wp_max_upload_size();
254
+ $byte_sizes = array('KB', 'MB', 'GB');
255
+ for($u = -1; $upload_size_unit > 1024 && $u < count($byte_sizes) - 1; $u++){
256
+ $upload_size_unit /= 1024;
257
+ }
258
+ if($u < 0){
259
+ $upload_size_unit = 0;
260
+ $u = 0;
261
+ } else {
262
+ $upload_size_unit = (int) $upload_size_unit;
263
+ }
264
+ ?>
265
+ <?php if(class_exists('bbPress') && !is_admin()) : // Add to bbPress profile with same style ?>
266
+ <h2 class="entry-title"><?php _e('WP User Avatar'); ?></h2>
267
+ <fieldset class="bbp-form">
268
+ <legend><?php _e('WP User Avatar'); ?></legend>
269
+ <?php else : // Add to profile with admin style ?>
270
+ <h3><?php _e('WP User Avatar') ?></h3>
271
+ <table class="form-table">
272
+ <tr>
273
+ <th><label for="wp_user_avatar"><?php _e('WP User Avatar'); ?></label></th>
274
+ <td>
275
+ <?php endif; ?>
276
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wpua; ?>" />
277
+ <?php if(current_user_can('upload_files')) : // Button to launch Media uploader ?>
278
+ <p><button type="button" class="button" id="add-wp-user-avatar" name="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
279
+ <?php elseif(!current_user_can('upload_files') && !has_wp_user_avatar($current_user->ID)) : // Upload button ?>
280
+ <input name="wp-user-avatar-file" id="wp-user-avatar-file" type="file" />
281
+ <button type="submit" class="button" id="upload-wp-user-avatar" name="upload-wp-user-avatar" value="<?php _e('Upload'); ?>"><?php _e('Upload'); ?></button>
282
+ <?php if(isset($_GET['errors']) && $_GET['errors'] == 'type') : ?>
283
+ <p id="wp-user-avatar-errors"><?php _e('File format is not allowed.'); ?></p>
284
+ <?php endif; ?>
285
+ <?php $postdata = file_get_contents("php://input");
286
+ if(!empty($postdata['wp-user-avatar-file'])): ?>
287
+ <p id="wp-user-avatar-errors"><?php _e('File is too large.'); ?></p>
288
+ <?php endif; ?>
289
+ <p>
290
+ <?php _e('Maximum upload file size: '.esc_html($upload_size_unit).esc_html($byte_sizes[$u])); ?>
291
+ <br />
292
+ <?php _e('Allowed file formats: JPG, GIF, PNG'); ?>
293
+ </p>
294
+ <?php elseif(!current_user_can('upload_files') && has_wp_user_avatar($current_user->ID) && wpua_author($wpua, $current_user->ID)) : // Edit button ?>
295
+ <?php $edit_attachment_link = function_exists('wp_enqueue_media') ? add_query_arg(array('post' => $wpua, 'action' => 'edit'), admin_url('post.php')) : add_query_arg(array('attachment_id' => $wpua, 'action' => 'edit'), admin_url('media.php')) ?>
296
+ <p><button type="button" class="button" id="edit-wp-user-avatar" name="edit-wp-user-avatar" onclick="window.open('<?php echo $edit_attachment_link; ?>', '_self');"><?php _e('Edit WP User Avatar'); ?></button></p>
297
+ <?php endif; ?>
298
+ <p id="wp-user-avatar-preview">
299
+ <img src="<?php echo $avatar_medium; ?>" alt="" />
300
+ <?php _e('Original'); ?>
301
+ </p>
302
+ <p id="wp-user-avatar-thumbnail">
303
+ <img src="<?php echo $avatar_thumbnail; ?>" alt="" />
304
+ <?php _e('Thumbnail'); ?>
305
+ </p>
306
+ <p><button type="button" class="button<?php echo $hide_remove; ?>" id="remove-wp-user-avatar" name="remove-wp-user-avatar"><?php _e('Remove'); ?></button></p>
307
+ <p id="wp-user-avatar-message"><?php _e('Press "Update '.$profile.'" to save your changes.'); ?></p>
308
+ <?php if(class_exists('bbPress') && !is_admin()) : // Add to bbPress profile with same style ?>
309
+ </fieldset>
310
+ <?php else : // Add to profile with admin style ?>
311
+ </td>
312
+ </tr>
313
+ </table>
314
+ <?php endif; ?>
315
+ <?php echo wpua_js($user->display_name, $avatar_medium_src); // Add JS ?>
316
+ <?php
317
+ }
318
+
319
+ // Update user meta
320
+ function wpua_action_process_option_update($user_id){
321
+ global $wpdb, $blog_id;
322
+ // Check if user has upload_files capability
323
+ if(current_user_can('upload_files')){
324
+ $wpua_id = isset($_POST['wp-user-avatar']) ? intval($_POST['wp-user-avatar']) : '';
325
+ $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %d", '_wp_attachment_wp_user_avatar', $user_id));
326
+ add_post_meta($wpua_id, '_wp_attachment_wp_user_avatar', $user_id);
327
+ update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', $wpua_id);
328
+ } else {
329
+ if(isset($_POST['wp-user-avatar']) && empty($_POST['wp-user-avatar'])){
330
+ // Uploads by user
331
+ $attachments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_author = %d AND post_type = %s", $user_id, 'attachment'));
332
+ foreach($attachments as $attachment){
333
+ // Delete attachment if not used by another user
334
+ if(!wpua_image($attachment->ID, $user_id)){
335
+ wp_delete_post($attachment->ID);
336
+ }
337
+ }
338
+ update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', '');
339
+ }
340
+ // Create attachment from upload
341
+ if(isset($_POST['upload-wp-user-avatar']) && $_POST['upload-wp-user-avatar']){
342
+ if(!function_exists('wp_handle_upload')){
343
+ require_once(ABSPATH.'wp-admin/includes/admin.php');
344
+ require_once(ABSPATH.'wp-admin/includes/file.php');
345
+ }
346
+ $name = $_FILES['wp-user-avatar-file']['name'];
347
+ $file = wp_handle_upload($_FILES['wp-user-avatar-file'], array('test_form' => false));
348
+ $type = $file['type'];
349
+ // Allow only JPG, GIF, PNG
350
+ if($file['error'] || !preg_match('/(jpe?g|gif|png)#x2F;i', $type)){
351
+ $redirect = add_query_arg('errors', 'type', wp_get_referer());
352
+ wp_safe_redirect($redirect);
353
+ die();
354
+ }
355
+ // Break out file info
356
+ $name_parts = pathinfo($name);
357
+ $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
358
+ $url = $file['url'];
359
+ $file = $file['file'];
360
+ $title = $name;
361
+ // Use image exif/iptc data for title if possible
362
+ if($image_meta = @wp_read_image_metadata($file)){
363
+ if(trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))){
364
+ $title = $image_meta['title'];
365
+ }
366
+ }
367
+ // Construct the attachment array
368
+ $attachment = array(
369
+ 'guid' => $url,
370
+ 'post_mime_type' => $type,
371
+ 'post_title' => $title
372
+ );
373
+ // This should never be set as it would then overwrite an existing attachment
374
+ if(isset($attachment['ID'])){
375
+ unset($attachment['ID']);
376
+ }
377
+ // Save the attachment metadata
378
+ $attachment_id = wp_insert_attachment($attachment, $file);
379
+ if(!is_wp_error($attachment_id)){
380
+ require_once(ABSPATH.'wp-admin/includes/image.php');
381
+ wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
382
+ $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %d", '_wp_attachment_wp_user_avatar', $user_id));
383
+ add_post_meta($attachment_id, '_wp_attachment_wp_user_avatar', $user_id);
384
+ update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', $attachment_id);
385
+ }
386
+ }
387
+ }
388
+ }
389
+
390
+ // Add button to attach image for WP 3.4 and older
391
+ function wpua_add_attachment_field_to_edit($fields, $post){
392
+ $image = wp_get_attachment_image_src($post->ID, "medium");
393
+ $button = '<button type="button" class="button" id="set-wp-user-avatar-image" name="set-wp-user-avatar-image" onclick="setWPUserAvatar(\''.$post->ID.'\', \''.$image[0].'\')">Set WP User Avatar</button>';
394
+ $fields['wp-user-avatar'] = array(
395
+ 'label' => __('WP User Avatar'),
396
+ 'input' => 'html',
397
+ 'html' => $button
398
+ );
399
+ return $fields;
400
+ }
401
+
402
+ // Add settings link on plugin page
403
+ function wpua_plugin_settings_links($links, $file){
404
+ if(basename($file) == basename(plugin_basename(__FILE__))){
405
+ $settings_link = '<a href="'.add_query_arg(array('page' => 'wp-user-avatar'), admin_url('options-general.php')).'">'.__('Settings').'</a>';
406
+ $links = array_merge($links, array($settings_link));
407
+ }
408
+ return $links;
409
+ }
410
+
411
+ // Add column to Users table
412
+ function wpua_add_column($columns){
413
+ return $columns + array('wp-user-avatar' => __('WP User Avatar'));
414
+ }
415
+
416
+ // Show thumbnail in Users table
417
+ function wpua_show_column($value, $column_name, $user_id){
418
+ global $wpdb, $blog_id;
419
+ $wpua = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
420
+ $wpua_image = wp_get_attachment_image($wpua, array(32,32));
421
+ if($column_name == 'wp-user-avatar'){
422
+ return $wpua_image;
423
+ }
424
+ }
425
+
426
+ // Media uploader
427
+ function wpua_media_upload_scripts(){
428
+ if(function_exists('wp_enqueue_media')){
429
+ wp_enqueue_script('admin-bar');
430
+ wp_enqueue_media();
431
+ } else {
432
+ wp_enqueue_script('media-upload');
433
+ wp_enqueue_script('thickbox');
434
+ wp_enqueue_style('thickbox');
435
+ }
436
+ wp_enqueue_script('wp-user-avatar', WPUA_URLPATH.'js/wp-user-avatar.js', '', WPUA_VERSION);
437
+ wp_enqueue_style('wp-user-avatar', WPUA_URLPATH.'css/wp-user-avatar.css', '', WPUA_VERSION);
438
+ }
439
+ }
440
+
441
+ // Uploader scripts
442
+ function wpua_js($section, $avatar_thumb){ ?>
443
+ <script type="text/javascript">
444
+ jQuery(function(){
445
+ <?php if(current_user_can('upload_files')) : ?>
446
+ <?php if(function_exists('wp_enqueue_media')) : // Backbone uploader for WP 3.5+ ?>
447
+ openMediaUploader("<?php echo $section; ?>");
448
+ <?php else : // Fall back to Thickbox uploader ?>
449
+ openThickboxUploader("<?php echo $section; ?>", "<?php echo get_admin_url(); ?>media-upload.php?post_id=0&type=image&tab=library&TB_iframe=1");
450
+ <?php endif; ?>
451
+ <?php endif; ?>
452
+ removeWPUserAvatar("<?php echo htmlspecialchars_decode($avatar_thumb); ?>");
453
+ });
454
+ </script>
455
+ <?php
456
+ }
457
+
458
+ // Returns true if user has Gravatar-hosted image
459
+ function wpua_has_gravatar($id_or_email, $has_gravatar=false, $user='', $email=''){
460
+ global $ssl;
461
+ if(!is_object($id_or_email) && !empty($id_or_email)){
462
+ // Find user by ID or e-mail address
463
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
464
+ // Get registered user e-mail address
465
+ $email = !empty($user) ? $user->user_email : '';
466
+ }
467
+ // Check if Gravatar image returns 200 (OK) or 404 (Not Found)
468
+ if(!empty($email)){
469
+ $hash = md5(strtolower(trim($email)));
470
+ $gravatar = 'http'.$ssl.'://www.gravatar.com/avatar/'.$hash.'?d=404';
471
+ $headers = @get_headers($gravatar);
472
+ $has_gravatar = !preg_match("|200|", $headers[0]) ? false : true;
473
+ }
474
+ return $has_gravatar;
475
+ }
476
+
477
+ // Returns true if user has wp_user_avatar
478
+ function has_wp_user_avatar($id_or_email='', $has_wpua=false, $user='', $user_id=''){
479
+ global $wpdb, $blog_id;
480
+ if(!is_object($id_or_email) && !empty($id_or_email)){
481
+ // Find user by ID or e-mail address
482
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
483
+ // Get registered user ID
484
+ $user_id = !empty($user) ? $user->ID : '';
485
+ }
486
+ $wpua = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
487
+ $has_wpua = !empty($wpua) ? true : false;
488
+ return $has_wpua;
489
+ }
490
+
491
+ // Replace get_avatar only in get_wp_user_avatar
492
+ function wpua_get_avatar_filter($avatar, $id_or_email, $size='', $default='', $alt=''){
493
+ global $post, $comment, $avatar_default, $wpua_avatar_default, $mustache_original, $mustache_medium, $mustache_thumbnail, $mustache_avatar, $mustache_admin;
494
+ // User has WPUA
495
+ if(is_object($id_or_email)){
496
+ if(!empty($comment->comment_author_email)){
497
+ $avatar = get_wp_user_avatar($comment->comment_author_email, $size, $default, $alt);
498
+ } else {
499
+ $avatar = get_wp_user_avatar('unknown@gravatar.com', $size, $default, $alt);
500
+ }
501
+ } else {
502
+ if(has_wp_user_avatar($id_or_email)){
503
+ $avatar = get_wp_user_avatar($id_or_email, $size, $default, $alt);
504
+ // User has Gravatar
505
+ } elseif(wpua_has_gravatar($id_or_email)){
506
+ $avatar = $avatar;
507
+ // User doesn't have WPUA or Gravatar and Default Avatar is wp_user_avatar, show custom Default Avatar
508
+ } elseif($avatar_default == 'wp_user_avatar'){
509
+ // Show custom Default Avatar
510
+ if(!empty($wpua_avatar_default)){
511
+ // Get image
512
+ $wpua_avatar_default_image = wp_get_attachment_image_src($wpua_avatar_default, array($size,$size));
513
+ // Image src
514
+ $default = $wpua_avatar_default_image[0];
515
+ // Add dimensions if numeric size
516
+ $dimensions = ' width="'.$wpua_avatar_default_image[1].'" height="'.$wpua_avatar_default_image[2].'"';
517
+ $defaultcss = '';
518
+ } else {
519
+ // Get mustache image based on numeric size comparison
520
+ if($size > get_option('medium_size_w')){
521
+ $default = $mustache_original;
522
+ } elseif($size <= get_option('medium_size_w') && $size > get_option('thumbnail_size_w')){
523
+ $default = $mustache_medium;
524
+ } elseif($size <= get_option('thumbnail_size_w') && $size > 96){
525
+ $default = $mustache_thumbnail;
526
+ } elseif($size <= 96 && $size > 32){
527
+ $default = $mustache_avatar;
528
+ } elseif($size <= 32){
529
+ $default = $mustache_admin;
530
+ }
531
+ // Add dimensions if numeric size
532
+ $dimensions = ' width="'.$size.'" height="'.$size.'"';
533
+ $defaultcss = ' avatar-default';
534
+ }
535
+ // Construct the img tag
536
+ $avatar = "<img src='".$default."'".$dimensions." alt='".$alt."' class='wp-user-avatar wp-user-avatar-".$size." avatar avatar-".$size." photo'".$defaultcss." />";
537
+ }
538
+ }
539
+ return $avatar;
540
+ }
541
+ add_filter('get_avatar', 'wpua_get_avatar_filter', 10, 6);
542
+
543
+ // Get original avatar, for when user removes wp_user_avatar
544
+ function wpua_get_avatar_original($id_or_email, $size='', $default='', $alt=''){
545
+ global $avatar_default, $wpua_avatar_default, $mustache_avatar, $pagenow;
546
+ // Remove get_avatar filter
547
+ if(is_admin()){
548
+ remove_filter('get_avatar', 'wpua_get_avatar_filter');
549
+ }
550
+ // User doesn't Gravatar and Default Avatar is wp_user_avatar, show custom Default Avatar
551
+ if(!wpua_has_gravatar($id_or_email) && $avatar_default == 'wp_user_avatar'){
552
+ // Show custom Default Avatar
553
+ if(!empty($wpua_avatar_default)){
554
+ $wpua_avatar_default_image = wp_get_attachment_image_src($wpua_avatar_default, array($size,$size));
555
+ $default = $wpua_avatar_default_image[0];
556
+ } else {
557
+ $default = $mustache_avatar;
558
+ }
559
+ } else {
560
+ // Get image from Gravatar, whether it's the user's image or default image
561
+ $wpua_image = get_avatar($id_or_email, $size);
562
+ // Takes the img tag, extracts the src
563
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wpua_image, $matches, PREG_SET_ORDER);
564
+ $default = $matches [0] [1];
565
+ }
566
+ return $default;
567
+ }
568
+
569
+ // Find WPUA, show get_avatar if empty
570
+ function get_wp_user_avatar($id_or_email='', $size='96', $align='', $alt=''){
571
+ global $post, $comment, $avatar_default, $wpdb, $blog_id;
572
+ // Checks if comment
573
+ if(is_object($id_or_email)){
574
+ // Checks if comment author is registered user by user ID
575
+ if($comment->user_id != '0'){
576
+ $id_or_email = $comment->user_id;
577
+ $user = get_user_by('id', $id_or_email);
578
+ // Checks that comment author isn't anonymous
579
+ } elseif(!empty($comment->comment_author_email)){
580
+ // Checks if comment author is registered user by e-mail address
581
+ $user = get_user_by('email', $comment->comment_author_email);
582
+ // Get registered user info from profile, otherwise e-mail address should be value
583
+ $id_or_email = !empty($user) ? $user->ID : $comment->comment_author_email;
584
+ }
585
+ $alt = $comment->comment_author;
586
+ } else {
587
+ if(!empty($id_or_email)){
588
+ // Find user by ID or e-mail address
589
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
590
+ } else {
591
+ // Find author's name if id_or_email is empty
592
+ $author_name = get_query_var('author_name');
593
+ if(is_author()){
594
+ // On author page, get user by page slug
595
+ $user = get_user_by('slug', $author_name);
596
+ } else {
597
+ // On post, get user by author meta
598
+ $user_id = get_the_author_meta('ID');
599
+ $user = get_user_by('id', $user_id);
600
+ }
601
+ }
602
+ // Set user's ID and name
603
+ if(!empty($user)){
604
+ $id_or_email = $user->ID;
605
+ $alt = $user->display_name;
606
+ }
607
+ }
608
+ // Checks if user has WPUA
609
+ $wpua_meta = !empty($id_or_email) ? get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email) : '';
610
+ // Add alignment class
611
+ $alignclass = !empty($align) ? ' align'.$align : '';
612
+ // User has WPUA, bypass get_avatar
613
+ if(!empty($wpua_meta)){
614
+ // Numeric size use size array
615
+ $get_size = is_numeric($size) ? array($size,$size) : $size;
616
+ // Get image src
617
+ $wpua_image = wp_get_attachment_image_src($wpua_meta, $get_size);
618
+ // Add dimensions to img only if numeric size was specified
619
+ $dimensions = is_numeric($size) ? ' width="'.$wpua_image[1].'" height="'.$wpua_image[2].'"' : '';
620
+ // Construct the img tag
621
+ $avatar = '<img src="'.$wpua_image[0].'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.$alignclass.' avatar avatar avatar-'.$size.' photo" />';
622
+ } else {
623
+ // Get numeric sizes for non-numeric sizes based on media options
624
+ if($size == 'original' || $size == 'large' || $size == 'medium' || $size == 'thumbnail'){
625
+ $get_size = ($size == 'original') ? get_option('large_size_w') : get_option($size.'_size_w');
626
+ } else {
627
+ // Numeric sizes leave as-is
628
+ $get_size = $size;
629
+ }
630
+ // User with no WPUA uses get_avatar
631
+ $avatar = get_avatar($id_or_email, $get_size, $default='', $alt='');
632
+ // Remove width and height for non-numeric sizes
633
+ if(!is_numeric($size)){
634
+ $avatar = preg_replace("/(width|height)=\'\d*\'\s/", '', $avatar);
635
+ $avatar = preg_replace('/(width|height)=\"\d*\"\s/', '', $avatar);
636
+ $avatar = str_replace('wp-user-avatar wp-user-avatar-'.$get_size.' ', '', $avatar);
637
+ $avatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$size.$alignclass." ", $avatar);
638
+ }
639
+ }
640
+ return $avatar;
641
+ }
642
+
643
+ // Return just the image src
644
+ function get_wp_user_avatar_src($id_or_email, $size='', $align=''){
645
+ $wpua_image_src = '';
646
+ // Gets the avatar img tag
647
+ $wpua_image = get_wp_user_avatar($id_or_email, $size, $align);
648
+ // Takes the img tag, extracts the src
649
+ if(!empty($wpua_image)){
650
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wpua_image, $matches, PREG_SET_ORDER);
651
+ $wpua_image_src = $matches [0] [1];
652
+ }
653
+ return $wpua_image_src;
654
+ }
655
+
656
+ // Shortcode
657
+ function wpua_shortcode($atts, $content){
658
+ global $wpdb, $blog_id;
659
+ // Set shortcode attributes
660
+ extract(shortcode_atts(array('user' => '', 'size' => '96', 'align' => '', 'link' => '', 'target' => ''), $atts));
661
+ // Find user by ID, login, slug, or e-mail address
662
+ if(!empty($user)){
663
+ $user = is_numeric($user) ? get_user_by('id', $user) : get_user_by('login', $user);
664
+ $user = empty($user) ? get_user_by('slug', $user) : $user;
665
+ $user = empty($user) ? get_user_by('email', $user) : $user;
666
+ }
667
+ // Get user ID
668
+ $id_or_email = !empty($user) ? $user->ID : '';
669
+ // Check if link is set
670
+ if(!empty($link)){
671
+ // CSS class is same as link type, except for URL
672
+ $link_class = $link;
673
+ // Open in new window
674
+ $target_link = !empty($target) ? ' target="'.$target.'"' : '';
675
+ if($link == 'file'){
676
+ // Get image src
677
+ $image_link = get_wp_user_avatar_src($id_or_email, 'original', $align);
678
+ } elseif($link == 'attachment'){
679
+ // Get attachment URL
680
+ $image_link = get_attachment_link(get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email));
681
+ } else {
682
+ // URL
683
+ $image_link = $link;
684
+ $link_class = 'custom';
685
+ }
686
+ // Wrap the avatar inside the link
687
+ $avatar = '<a href="'.$image_link.'" class="wp-user-avatar-link wp-user-avatar-'.$link_class.'"'.$target_link.'>'.get_wp_user_avatar($id_or_email, $size, $align).'</a>';
688
+ } else {
689
+ // Get WPUA as normal
690
+ $avatar = get_wp_user_avatar($id_or_email, $size, $align);
691
+ }
692
+ return $avatar;
693
+ }
694
+ add_shortcode('avatar', 'wpua_shortcode');
695
+
696
+ // Add default avatar
697
+ function wpua_add_default_avatar($avatar_list=null){
698
+ global $avatar_default, $wpua_avatar_default, $mustache_medium, $mustache_admin;
699
+ // Remove get_avatar filter
700
+ remove_filter('get_avatar', 'wpua_get_avatar_filter');
701
+ // Set avatar_list variable
702
+ $avatar_list = '';
703
+ // Set avatar defaults
704
+ $avatar_defaults = array(
705
+ 'mystery' => __('Mystery Man'),
706
+ 'blank' => __('Blank'),
707
+ 'gravatar_default' => __('Gravatar Logo'),
708
+ 'identicon' => __('Identicon (Generated)'),
709
+ 'wavatar' => __('Wavatar (Generated)'),
710
+ 'monsterid' => __('MonsterID (Generated)'),
711
+ 'retro' => __('Retro (Generated)')
712
+ );
713
+ // No Default Avatar, set to Mystery Man
714
+ if(empty($avatar_default)){
715
+ $avatar_default = 'mystery';
716
+ }
717
+ // Take avatar_defaults and get examples for unknown@gravatar.com
718
+ foreach($avatar_defaults as $default_key => $default_name){
719
+ $avatar = get_avatar('unknown@gravatar.com', 32, $default_key);
720
+ $selected = ($avatar_default == $default_key) ? 'checked="checked" ' : '';
721
+ $avatar_list .= "\n\t<label><input type='radio' name='avatar_default' id='avatar_{$default_key}' value='".esc_attr($default_key)."' {$selected}/> ";
722
+ $avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&amp;forcedefault=1'", $avatar);
723
+ $avatar_list .= ' '.$default_name.'</label>';
724
+ $avatar_list .= '<br />';
725
+ }
726
+ // Show remove link if custom Default Avatar is set
727
+ if(!empty($wpua_avatar_default)){
728
+ $avatar_thumb_src = wp_get_attachment_image_src($wpua_avatar_default, array(32,32));
729
+ $avatar_thumb = $avatar_thumb_src[0];
730
+ $hide_remove = '';
731
+ } else {
732
+ $avatar_thumb = $mustache_admin;
733
+ $hide_remove = ' class="hide-me"';
734
+ }
735
+ // Default Avatar is wp_user_avatar, check the radio button next to it
736
+ $selected_avatar = ($avatar_default == 'wp_user_avatar') ? ' checked="checked" ' : '';
737
+ // Wrap WPUA in div
738
+ $avatar_thumb_img = '<div id="wp-user-avatar-preview"><img src="'.$avatar_thumb.'" width="32" /></div>';
739
+ // Add WPUA to list
740
+ $wpua_list = "\n\t<label><input type='radio' name='avatar_default' id='wp_user_avatar_radio' value='wp_user_avatar'$selected_avatar /> ";
741
+ $wpua_list .= preg_replace("/src='(.+?)'/", "src='\$1'", $avatar_thumb_img);
742
+ $wpua_list .= ' '.__('WP User Avatar').'</label>';
743
+ $wpua_list .= '<p id="edit-wp-user-avatar"><button type="button" class="button" id="add-wp-user-avatar" name="add-wp-user-avatar">'.__('Edit WP User Avatar').'</button>';
744
+ $wpua_list .= '<a href="#" id="remove-wp-user-avatar"'.$hide_remove.'>'.__('Remove').'</a></p>';
745
+ $wpua_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="'.$wpua_avatar_default.'">';
746
+ $wpua_list .= '<p id="wp-user-avatar-message">'.__('Press "Save Changes" to save your changes.').'</p>';
747
+ $wpua_list .= wpua_js('Default Avatar', $mustache_admin);
748
+ return $wpua_list.$avatar_list;
749
+ }
750
+ add_filter('default_avatar_select', 'wpua_add_default_avatar', 10);
751
+
752
+ // Add default avatar_default to whitelist
753
+ function wpua_whitelist_options($whitelist_options){
754
+ $whitelist_options['discussion'][] = 'avatar_default_wp_user_avatar';
755
+ return $whitelist_options;
756
+ }
757
+ add_filter('whitelist_options', 'wpua_whitelist_options', 10);
758
+
759
+ // Add media state
760
+ function wpua_add_media_state($media_states){
761
+ global $post, $wpua_avatar_default;
762
+ $is_wpua = get_post_custom_values('_wp_attachment_wp_user_avatar', $post->ID);
763
+ if(!empty($is_wpua)){
764
+ $media_states[] = __('Avatar');
765
+ }
766
+ if(!empty($wpua_avatar_default) && ($wpua_avatar_default == $post->ID)){
767
+ $media_states[] = __('Default Avatar');
768
+ }
769
+ return apply_filters('wpua_add_media_state', $media_states);
770
+ }
771
+ add_filter('display_media_states', 'wpua_add_media_state', 10, 1);
772
+
773
+ // Check if image is used as WPUA
774
+ function wpua_image($attachment_id, $user_id, $wpua_image=false){
775
+ global $wpdb;
776
+ $wpua = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value != %d", $attachment_id, '_wp_attachment_wp_user_avatar', $user_id));
777
+ if(!empty($wpua)){
778
+ $wpua_image = true;
779
+ }
780
+ return $wpua_image;
781
+ }
782
+
783
+ // Check who owns image
784
+ function wpua_author($attachment_id, $user_id, $wpua_author=false){
785
+ $attachment = get_post($attachment_id);
786
+ if(!empty($attachment) && $attachment->post_author == $user_id){
787
+ $wpua_author = true;
788
+ }
789
+ return $wpua_author;
790
+ }
791
+
792
+ // Admin page
793
+ function wpua_options_page(){
794
+ // Give subscribers edit_posts capability
795
+ if(isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true' && empty($wpua_allow_upload)){
796
+ wpua_subscriber_remove_cap();
797
+ }
798
+ ?>
799
+ <div class="wrap">
800
+ <?php screen_icon(); ?>
801
+ <h2><?php _e('WP User Avatar'); ?></h2>
802
+ <form method="post" action="options.php">
803
+ <?php settings_fields('wpua-settings-group'); ?>
804
+ <?php do_settings_fields('wpua-settings-group', ''); ?>
805
+ <table class="form-table">
806
+ <tr valign="top">
807
+ <th scope="row"><?php _e('WP User Avatar Settings') ?></th>
808
+ <td>
809
+ <fieldset>
810
+ <legend class="screen-reader-text"><span><?php _e('WP User Avatar Settings') ?></span></legend>
811
+ <label for="wp_user_avatar_tinymce">
812
+ <input name="wp_user_avatar_tinymce" type="checkbox" id="wp_user_avatar_tinymce" value="1" <?php checked('1', get_option('wp_user_avatar_tinymce')); ?> />
813
+ <?php _e('Add avatar button to Visual Editor'); ?>
814
+ </label>
815
+ <br />
816
+ <label for="wp_user_avatar_allow_upload">
817
+ <input name="wp_user_avatar_allow_upload" type="checkbox" id="wp_user_avatar_allow_upload" value="1" <?php checked('1', get_option('wp_user_avatar_allow_upload')); ?> />
818
+ <?php _e('Allow Contributors &amp; Subscribers to upload avatars'); ?>
819
+ </label>
820
+ </fieldset>
821
+ </td>
822
+ </tr>
823
+ </table>
824
+ <h3 class="title"><?php _e('Avatars'); ?></h3>
825
+ <p><?php _e('An avatar is an image that follows you from weblog to weblog appearing beside your name when you comment on avatar enabled sites. Here you can enable the display of avatars for people who comment on your site.'); ?></p>
826
+ <table class="form-table">
827
+ <tr valign="top">
828
+ <th scope="row"><?php _e('Avatar Display'); ?></th>
829
+ <td>
830
+ <fieldset>
831
+ <legend class="screen-reader-text"><span><?php _e('Avatar Display'); ?></span></legend>
832
+ <label for="show_avatars">
833
+ <input type="checkbox" id="show_avatars" name="show_avatars" value="1" <?php checked('1', get_option('show_avatars')); ?> />
834
+ <?php _e('Show Avatars'); ?>
835
+ </label>
836
+ </fieldset>
837
+ </td>
838
+ </tr>
839
+ <tr valign="top">
840
+ <th scope="row"><?php _e('Maximum Rating'); ?></th>
841
+ <td>
842
+ <fieldset>
843
+ <legend class="screen-reader-text"><span><?php _e('Maximum Rating'); ?></span></legend>
844
+ <?php
845
+ $ratings = array(
846
+ 'G' => __('G &#8212; Suitable for all audiences'),
847
+ 'PG' => __('PG &#8212; Possibly offensive, usually for audiences 13 and above'),
848
+ 'R' => __('R &#8212; Intended for adult audiences above 17'),
849
+ 'X' => __('X &#8212; Even more mature than above')
850
+ );
851
+ foreach ($ratings as $key => $rating) :
852
+ $selected = (get_option('avatar_rating') == $key) ? 'checked="checked"' : '';
853
+ echo "\n\t<label><input type='radio' name='avatar_rating' value='" . esc_attr($key) . "' $selected/> $rating</label><br />";
854
+ endforeach;
855
+ ?>
856
+ </fieldset>
857
+ </td>
858
+ </tr>
859
+ <tr valign="top">
860
+ <th scope="row"><?php _e('Default Avatar') ?></th>
861
+ <td class="defaultavatarpicker">
862
+ <fieldset>
863
+ <legend class="screen-reader-text"><span><?php _e('Default Avatar'); ?></span></legend>
864
+ <?php _e('For users without a custom avatar of their own, you can either display a generic logo or a generated one based on their e-mail address.'); ?><br />
865
+ <?php echo wpua_add_default_avatar(); ?>
866
+ </fieldset>
867
+ </td>
868
+ </tr>
869
+ </table>
870
+ <?php submit_button(); ?>
871
+ </form>
872
+ </div>
873
+ <?php
874
+ }
875
+
876
+ // Whitelist settings
877
+ function wpua_admin_settings(){
878
+ register_setting('wpua-settings-group', 'wp_user_avatar_tinymce');
879
+ register_setting('wpua-settings-group', 'wp_user_avatar_allow_upload');
880
+ register_setting('wpua-settings-group', 'show_avatars');
881
+ register_setting('wpua-settings-group', 'avatar_rating');
882
+ register_setting('wpua-settings-group', 'avatar_default');
883
+ register_setting('wpua-settings-group', 'avatar_default_wp_user_avatar');
884
+ }
885
+
886
+ // Add options page and settings
887
+ function wpua_admin(){
888
+ add_options_page('WP User Avatar Plugin Settings', 'WP User Avatar', 'manage_options', 'wp-user-avatar', 'wpua_options_page');
889
+ add_action('admin_init', 'wpua_admin_settings');
890
+ }
891
+
892
+ // Initialize WPUA after other plugins are loaded
893
+ function wpua_load(){
894
+ global $wpua_instance;
895
+ $wpua_instance = new wp_user_avatar();
896
+ }
897
+ add_action('plugins_loaded', 'wpua_load');
898
+ }
899
+ ?>