WP User Avatar - Version 1.4.2

Version Description

  • Bug Fix: Include screen.php for get_current_screen function
Download this release

Release Info

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

Version 1.4.2

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.2
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.2"}}});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.2
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}('5 16(c){0.7.k={s:5(){x 0.7.y.B.i.o},X:5(a){v b=0.7.y.B;b.i.o=a;b.i.T=2(\'19.j-18\').K(\'M\').p(\'q\');r(b.i.o){R(b.i.o,b.i.T);2(\'#t\').u(\'g\')}},w:5(){r(4.8){x 4.8}4.8=0.7({z:\'Q\',12:[11 0.7.1d.1a({13:"U H I G: "+c})]});4.8.f(\'O\',5(){v a=4.z().s(\'l\');L=2(\'#0-3-6\').F();j=0.7.j(L);j.17();a.E(j?[j]:[])},4.8);4.8.f(\'1b:1c:m\',5(a){4.1e(a,{1f:\'1g H I G\'})},4.8);4.8.z(\'Q\').f(\'m\',4.m);x 4.8},m:5(a){v b=0.7.y.B,l=4.s(\'l\').1h();0.7.k.X(l?l.L:-1)},P:5(){2(\'C\').f(\'g\',\'#E-0-3-6\',5(e){e.A();e.14();0.7.k.w().O()})}};2(0.7.k.P)}5 15(a,b){2(\'C\').f(\'g\',\'#E-0-3-6\',5(e){e.A();1o(\'U H I G: \'+a,b)})}5 R(a,b){2(\'#0-3-6\',d.9.h).F(a);2(\'#0-3-6-S\',d.9.h).K(\'M\').p(\'q\',b).n(\'N\',\'\').n(\'V\',\'\');2(\'#0-3-6-W\',d.9.h).J();2(\'#Y-0-3-6\',d.9.h).J();2(\'#0-3-6-Z\',d.9.h).D();2(\'#t\',d.9.h).u(\'g\');r(1i(0)!=\'1j\'){0.7.k.w().1k()}1l{d.9.1m()}}5 1n(a){2(\'C\').f(\'g\',\'#Y-0-3-6\',5(e){e.A();2(4).D();2(\'#10-0-3-6, #0-3-6-Z\').D();2(\'#0-3-6-S\').K(\'M\').p(\'q\',a).n(\'N\',\'\').n(\'V\',\'\');2(\'#0-3-6\').F(\'\');2(\'#0-3-6-W\').J();2(\'#t\').u(\'g\')})}',62,87,'wp||jQuery|user|this|function|avatar|media|_frame|parent||||window||on|click|document|post|attachment|wpUserAvatar|selection|select|removeAttr|wpUserAvatarId|attr|src|if|get|wp_user_avatar_radio|trigger|var|frame|return|view|state|preventDefault|settings|body|hide|add|val|Avatar|WP|User|show|find|id|img|width|open|init|library|setWPUserAvatar|preview|wpUserAvatarSrc|Edit|height|message|set|remove|thumbnail|edit|new|states|title|stopPropagation|openThickboxUploader|openMediaUploader|fetch|info|div|Library|toolbar|create|controller|createSelectToolbar|text|Set|single|typeof|undefined|close|else|tb_remove|removeWPUserAvatar|tb_show'.split('|'),0,{}))
readme.txt ADDED
@@ -0,0 +1,360 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2
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 Contribuors 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. You must specify the user ID:
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.2 =
230
+ * Bug Fix: Include screen.php for get_current_screen function
231
+
232
+ = 1.4.1 =
233
+ * Bug Fix: Allow multipart data in form
234
+ * Bug Fix: Use wp_die for errors
235
+
236
+ = 1.4 =
237
+ * Add: Uploader for Subscribers and Contributors
238
+ * Add: Media states for avatar images
239
+ * Add: Plugin admin settings
240
+ * Update: Change support only to WP 3.4+
241
+
242
+ = 1.3.6 =
243
+ * Add: Target for link in shortcode
244
+ * Update: Clean up code and add more comments
245
+
246
+ = 1.3.5 =
247
+ * Bug Fix: Swap TinyMCE file locations
248
+
249
+ = 1.3.4 =
250
+ * Update: Change support only to WP 3.3+ because of jQuery 1.7.1 support
251
+
252
+ = 1.3.3 =
253
+ * Update: Shortcode checks for user ID, login, slug, or e-mail address
254
+ * Update: Move jquery to register_script for < WP 3.5
255
+
256
+ = 1.3.2 =
257
+ * Bug Fix: Check for user before setting name in alt tag
258
+ * Update: readme.txt
259
+
260
+ = 1.3.1 =
261
+ * Bug Fix: Rename usermeta only if found
262
+
263
+ = 1.3 =
264
+ * Add: Multisite support
265
+ * Bug Fix: Warnings if no user found
266
+ * Update: Enable action_show_user_profile for any class using show_user_profile hook
267
+
268
+ = 1.2.6 =
269
+ * Bug Fix: options-discussion.php page doesn't show default avatars
270
+
271
+ = 1.2.5 =
272
+ * Bug Fix: Comment author showing wrong avatar
273
+ * Bug Fix: Avatar adds fixed dimensions when non-numeric size is used
274
+ * Update: Use local image for default avatar instead of calling image from Gravatar
275
+
276
+ = 1.2.4 =
277
+ * Bug Fix: Show default avatar when user removes custom avatar
278
+ * Bug Fix: Default Avatar save setting
279
+
280
+ = 1.2.3 =
281
+ * Bug Fix: Show default avatar when user removes custom avatar
282
+ * Bug Fix: Default Avatar save setting
283
+
284
+ = 1.2.2 =
285
+ * Add: Ability for bbPress users to edit avatar on front profile page
286
+ * Add: Link options for shortcode
287
+ * Bug Fix: Show WP User Avatar only to users with upload_files capability
288
+
289
+ = 1.2.1 =
290
+ * Add: TinyMCE button
291
+ * Update: Clean up redundant code
292
+ * Update: Compatibility only back to WordPress 3.3
293
+
294
+ = 1.2 =
295
+ * Add: Default Avatar setting
296
+
297
+ = 1.1.7 =
298
+ * Bug Fix: Change update_usermeta to update_user_meta
299
+
300
+ = 1.1.6 =
301
+ * Bug Fix: Image not showing in user profile edit
302
+
303
+ = 1.1.5a =
304
+ * Update: readme.txt
305
+
306
+ = 1.1.5 =
307
+ * Bug Fix: Remove stray curly bracket
308
+
309
+ = 1.1.4 =
310
+ * Bug Fix: Change get_usermeta to get_user_meta
311
+ * Bug Fix: Non-object warning when retrieving user ID
312
+
313
+ = 1.1.3 =
314
+ * Bug Fix: Comment author with no e-mail address
315
+
316
+ = 1.1.2 =
317
+ * Remove: Unused variables
318
+
319
+ = 1.1.1 =
320
+ * Bug Fix: Capabilities error in comment avatar
321
+
322
+ = 1.1 =
323
+ * Add: Add filter for get_avatar
324
+ * Add: CSS alignment classes
325
+ * Add: Replace comment author avatar
326
+ * Add: Shortcode
327
+ * Update: readme.txt
328
+
329
+ = 1.0.2 =
330
+ * Update: FAQ
331
+ * Remove: CSS that hides "Insert into Post"
332
+
333
+ = 1.0.1 =
334
+ * Add: CSS classes to image output
335
+
336
+ = 1.0 =
337
+ * Initial release
338
+
339
+ == Upgrade Notice ==
340
+
341
+ = 1.4 =
342
+ * New Feature: Setting to allow all users to upload avatars
343
+ * New Feature: Setting to add or remove Visual Editor button
344
+ * New Feature: Media states for avatar images
345
+ * Notice: WP User Avatar 1.4 only supports WordPress 3.4 and above. If you are using an older version of WordPress, please upgrade your version of WordPress first.
346
+
347
+ = 1.3 =
348
+ * New Feature: Multisite support
349
+
350
+ = 1.2.2 =
351
+ * New Features: Link options for shortcode, bbPress integration
352
+
353
+ = 1.2.1 =
354
+ * New Feature: Shortcode insertion button for Visual Editor
355
+
356
+ = 1.2 =
357
+ * New Feature: Default Avatar customization
358
+
359
+ = 1.1 =
360
+ * 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.2
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,903 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.4.2
5
+ */
6
+ /*
7
+ Plugin Name: WP User Avatar
8
+ Plugin URI: http://wordpress.org/extend/plugins/wp-user-avatar/
9
+ Description: Use any image from your WordPress Media Library as a custom user avatar. Add your own Default Avatar.
10
+ Version: 1.4.2
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.2');
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
+ // Allow multipart data in form
124
+ function wpua_add_edit_form_multipart_encoding(){
125
+ echo ' enctype="multipart/form-data"';
126
+ }
127
+ add_action('user_edit_form_tag', 'wpua_add_edit_form_multipart_encoding');
128
+
129
+ // Check user role
130
+ function check_user_role($role, $user_id=null){
131
+ global $current_user;
132
+ if(is_numeric($user_id)){
133
+ $user = get_userdata($user_id);
134
+ } else {
135
+ $user = $current_user->ID;
136
+ }
137
+ if(empty($user)){
138
+ return false;
139
+ }
140
+ return in_array($role, (array) $user->roles);
141
+ }
142
+
143
+ // Give subscribers edit_posts capability
144
+ function wpua_subscriber_add_cap(){
145
+ global $wpdb, $blog_id;
146
+ $wp_user_roles = $wpdb->get_blog_prefix($blog_id).'user_roles';
147
+ $user_roles = get_option($wp_user_roles);
148
+ $user_roles['subscriber']['capabilities']['edit_posts'] = true;
149
+ update_option($wp_user_roles, $user_roles);
150
+ }
151
+ add_action('admin_init', 'wpua_subscriber_add_cap');
152
+
153
+ // Remove menu items
154
+ function wpua_subscriber_remove_menu_pages(){
155
+ global $current_user;
156
+ if(check_user_role('subscriber', $current_user->ID)){
157
+ remove_menu_page('edit.php');
158
+ remove_menu_page('edit-comments.php');
159
+ remove_menu_page('tools.php');
160
+ }
161
+ }
162
+ add_action('admin_menu', 'wpua_subscriber_remove_menu_pages');
163
+
164
+ // Remove dashboard items
165
+ function wpua_subscriber_remove_dashboard_widgets(){
166
+ global $current_user;
167
+ if(check_user_role('subscriber', $current_user->ID)){
168
+ remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
169
+ remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side');
170
+ remove_meta_box('dashboard_right_now', 'dashboard', 'normal');
171
+ }
172
+ }
173
+ add_action('wp_dashboard_setup', 'wpua_subscriber_remove_dashboard_widgets');
174
+
175
+ // Restrict access to pages
176
+ function wpua_subscriber_offlimits(){
177
+ global $current_user, $pagenow;
178
+ $offlimits = array('edit.php', 'post-new.php', 'edit-comments.php', 'tools.php');
179
+ if(check_user_role('subscriber', $current_user->ID)){
180
+ if(in_array($pagenow, $offlimits)){
181
+ do_action('admin_page_access_denied');
182
+ wp_die(__('You do not have sufficient permissions to access this page.'));
183
+ }
184
+ }
185
+ }
186
+ add_action('admin_init', 'wpua_subscriber_offlimits');
187
+ }
188
+
189
+ // Remove subscribers edit_posts capability
190
+ function wpua_subscriber_remove_cap(){
191
+ global $wpdb, $blog_id;
192
+ $wp_user_roles = $wpdb->get_blog_prefix($blog_id).'user_roles';
193
+ $user_roles = get_option($wp_user_roles);
194
+ unset($user_roles['subscriber']['capabilities']['edit_posts']);
195
+ update_option($wp_user_roles, $user_roles);
196
+ }
197
+
198
+ // On deactivation
199
+ function wpua_deactivate(){
200
+ // Remove subscribers edit_posts capability
201
+ wpua_subscriber_remove_cap();
202
+ // Reset all default avatar to Mystery Man
203
+ update_option('avatar_default', 'mystery');
204
+ }
205
+
206
+ // WP User Avatar
207
+ if(!class_exists('wp_user_avatar')){
208
+ class wp_user_avatar{
209
+ function wp_user_avatar(){
210
+ global $current_user, $current_screen, $show_avatars, $wpua_allow_upload, $pagenow;
211
+ // Adds WPUA to profile
212
+ if(current_user_can('upload_files') || ($wpua_allow_upload == 1 && is_user_logged_in())){
213
+ add_action('show_user_profile', array('wp_user_avatar', 'wpua_action_show_user_profile'));
214
+ add_action('edit_user_profile', array($this, 'wpua_action_show_user_profile'));
215
+ add_action('personal_options_update', array($this, 'wpua_action_process_option_update'));
216
+ add_action('edit_user_profile_update', array($this, 'wpua_action_process_option_update'));
217
+ if(is_admin()){
218
+ // Adds scripts to admin
219
+ add_action('admin_enqueue_scripts', array($this, 'wpua_media_upload_scripts'));
220
+ // Admin settings
221
+ add_action('admin_menu', 'wpua_admin');
222
+ add_filter('plugin_action_links', array($this, 'wpua_plugin_settings_links'), 10, 2);
223
+ } else {
224
+ if(!function_exists('get_current_screen')){
225
+ require_once(ABSPATH.'wp-admin/includes/screen.php');
226
+ }
227
+ // Adds scripts to front pages
228
+ add_action('wp_enqueue_scripts', array($this, 'wpua_media_upload_scripts'));
229
+ }
230
+ // Only add attachment field for WP 3.4 and older
231
+ if(!function_exists('wp_enqueue_media') && $pagenow == 'media-upload.php'){
232
+ add_filter('attachment_fields_to_edit', array($this, 'wpua_add_attachment_field_to_edit'), 10, 2);
233
+ }
234
+ // Hide column in Users table if default avatars are enabled
235
+ if(is_admin() && $show_avatars != 1){
236
+ add_filter('manage_users_columns', array($this, 'wpua_add_column'), 10, 1);
237
+ add_filter('manage_users_custom_column', array($this, 'wpua_show_column'), 10, 3);
238
+ }
239
+ }
240
+ }
241
+
242
+ // Add to edit user profile
243
+ function wpua_action_show_user_profile($user){
244
+ global $wpdb, $blog_id, $current_user, $show_avatars;
245
+ // Get WPUA attachment ID
246
+ $wpua = get_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
247
+ // Show remove button if WPUA is set
248
+ $hide_notice = has_wp_user_avatar($user->ID) ? ' class="hide-me"' : "";
249
+ $hide_remove = !has_wp_user_avatar($user->ID) ? ' hide-me' : "";
250
+ // If avatars are enabled, get original avatar image or show blank
251
+ $avatar_medium_src = ($show_avatars == 1 && is_admin()) ? wpua_get_avatar_original($user->user_email, 96) : includes_url().'images/blank.gif';
252
+ // Check if user has wp_user_avatar, if not show image from above
253
+ $avatar_medium = has_wp_user_avatar($user->ID) ? get_wp_user_avatar_src($user->ID, 'medium') : $avatar_medium_src;
254
+ // Check if user has wp_user_avatar, if not show image from above
255
+ $avatar_thumbnail = has_wp_user_avatar($user->ID) ? get_wp_user_avatar_src($user->ID, 96) : $avatar_medium_src;
256
+ // Change text on message based on current user
257
+ $profile = ($current_user->ID == $user->ID) ? 'Profile' : 'User';
258
+ // Max upload size
259
+ if(!function_exists('wp_max_upload_size')){
260
+ require_once(ABSPATH.'wp-admin/includes/template.php');
261
+ }
262
+ $upload_size_unit = wp_max_upload_size();
263
+ $byte_sizes = array('KB', 'MB', 'GB');
264
+ for($u = -1; $upload_size_unit > 1024 && $u < count($byte_sizes) - 1; $u++){
265
+ $upload_size_unit /= 1024;
266
+ }
267
+ if($u < 0){
268
+ $upload_size_unit = 0;
269
+ $u = 0;
270
+ } else {
271
+ $upload_size_unit = (int) $upload_size_unit;
272
+ }
273
+ ?>
274
+ <?php if(class_exists('bbPress') && !is_admin()) : // Add to bbPress profile with same style ?>
275
+ <h2 class="entry-title"><?php _e('WP User Avatar'); ?></h2>
276
+ <fieldset class="bbp-form">
277
+ <legend><?php _e('WP User Avatar'); ?></legend>
278
+ <?php else : // Add to profile with admin style ?>
279
+ <h3><?php _e('WP User Avatar') ?></h3>
280
+ <table class="form-table">
281
+ <tr>
282
+ <th><label for="wp_user_avatar"><?php _e('WP User Avatar'); ?></label></th>
283
+ <td>
284
+ <?php endif; ?>
285
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wpua; ?>" />
286
+ <?php if(current_user_can('upload_files')) : // Button to launch Media uploader ?>
287
+ <p><button type="button" class="button" id="add-wp-user-avatar" name="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
288
+ <?php elseif(!current_user_can('upload_files') && !has_wp_user_avatar($current_user->ID)) : // Upload button ?>
289
+ <input name="wp-user-avatar-file" id="wp-user-avatar-file" type="file" />
290
+ <button type="submit" class="button" id="upload-wp-user-avatar" name="upload-wp-user-avatar" value="<?php _e('Upload'); ?>"><?php _e('Upload'); ?></button>
291
+ <p>
292
+ <?php _e('Maximum upload file size: '.esc_html($upload_size_unit).esc_html($byte_sizes[$u])); ?>
293
+ <br />
294
+ <?php _e('Allowed file formats: JPG, GIF, PNG'); ?>
295
+ </p>
296
+ <?php elseif(!current_user_can('upload_files') && has_wp_user_avatar($current_user->ID) && wpua_author($wpua, $current_user->ID)) : // Edit button ?>
297
+ <?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')) ?>
298
+ <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>
299
+ <?php endif; ?>
300
+ <p id="wp-user-avatar-preview">
301
+ <img src="<?php echo $avatar_medium; ?>" alt="" />
302
+ <?php _e('Original'); ?>
303
+ </p>
304
+ <p id="wp-user-avatar-thumbnail">
305
+ <img src="<?php echo $avatar_thumbnail; ?>" alt="" />
306
+ <?php _e('Thumbnail'); ?>
307
+ </p>
308
+ <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>
309
+ <p id="wp-user-avatar-message"><?php _e('Press "Update '.$profile.'" to save your changes.'); ?></p>
310
+ <?php if(class_exists('bbPress') && !is_admin()) : // Add to bbPress profile with same style ?>
311
+ </fieldset>
312
+ <?php else : // Add to profile with admin style ?>
313
+ </td>
314
+ </tr>
315
+ </table>
316
+ <?php endif; ?>
317
+ <?php echo wpua_js($user->display_name, $avatar_medium_src); // Add JS ?>
318
+ <?php
319
+ }
320
+
321
+ // Update user meta
322
+ function wpua_action_process_option_update($user_id){
323
+ global $wpdb, $blog_id;
324
+ // Check if user has upload_files capability
325
+ if(current_user_can('upload_files')){
326
+ $wpua_id = isset($_POST['wp-user-avatar']) ? intval($_POST['wp-user-avatar']) : "";
327
+ $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %d", '_wp_attachment_wp_user_avatar', $user_id));
328
+ add_post_meta($wpua_id, '_wp_attachment_wp_user_avatar', $user_id);
329
+ update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', $wpua_id);
330
+ } else {
331
+ if(isset($_POST['wp-user-avatar']) && empty($_POST['wp-user-avatar'])){
332
+ // Uploads by user
333
+ $attachments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_author = %d AND post_type = %s", $user_id, 'attachment'));
334
+ foreach($attachments as $attachment){
335
+ // Delete attachment if not used by another user
336
+ if(!wpua_image($attachment->ID, $user_id)){
337
+ wp_delete_post($attachment->ID);
338
+ }
339
+ }
340
+ update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', "");
341
+ }
342
+ // Create attachment from upload
343
+ if(isset($_POST['upload-wp-user-avatar']) && $_POST['upload-wp-user-avatar']){
344
+ if(!function_exists('wp_handle_upload')){
345
+ require_once(ABSPATH.'wp-admin/includes/admin.php');
346
+ require_once(ABSPATH.'wp-admin/includes/file.php');
347
+ }
348
+ $name = $_FILES['wp-user-avatar-file']['name'];
349
+ $file = wp_handle_upload($_FILES['wp-user-avatar-file'], array('test_form' => false));
350
+ $type = $file['type'];
351
+ // Allow only JPG, GIF, PNG
352
+ if($file['error'] || !preg_match('/(jpe?g|gif|png)$/i', $type)){
353
+ if($file['error']){
354
+ wp_die($file['error']);
355
+ } else {
356
+ wp_die(__('Sorry, this file type is not permitted for security reasons.'));
357
+ }
358
+ }
359
+ // Break out file info
360
+ $name_parts = pathinfo($name);
361
+ $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
362
+ $url = $file['url'];
363
+ $file = $file['file'];
364
+ $title = $name;
365
+ // Use image exif/iptc data for title if possible
366
+ if($image_meta = @wp_read_image_metadata($file)){
367
+ if(trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))){
368
+ $title = $image_meta['title'];
369
+ }
370
+ }
371
+ // Construct the attachment array
372
+ $attachment = array(
373
+ 'guid' => $url,
374
+ 'post_mime_type' => $type,
375
+ 'post_title' => $title
376
+ );
377
+ // This should never be set as it would then overwrite an existing attachment
378
+ if(isset($attachment['ID'])){
379
+ unset($attachment['ID']);
380
+ }
381
+ // Save the attachment metadata
382
+ $attachment_id = wp_insert_attachment($attachment, $file);
383
+ if(!is_wp_error($attachment_id)){
384
+ require_once(ABSPATH.'wp-admin/includes/image.php');
385
+ wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
386
+ $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %d", '_wp_attachment_wp_user_avatar', $user_id));
387
+ add_post_meta($attachment_id, '_wp_attachment_wp_user_avatar', $user_id);
388
+ update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', $attachment_id);
389
+ }
390
+ }
391
+ }
392
+ }
393
+
394
+ // Add button to attach image for WP 3.4 and older
395
+ function wpua_add_attachment_field_to_edit($fields, $post){
396
+ $image = wp_get_attachment_image_src($post->ID, "medium");
397
+ $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>';
398
+ $fields['wp-user-avatar'] = array(
399
+ 'label' => __('WP User Avatar'),
400
+ 'input' => 'html',
401
+ 'html' => $button
402
+ );
403
+ return $fields;
404
+ }
405
+
406
+ // Add settings link on plugin page
407
+ function wpua_plugin_settings_links($links, $file){
408
+ if(basename($file) == basename(plugin_basename(__FILE__))){
409
+ $settings_link = '<a href="'.add_query_arg(array('page' => 'wp-user-avatar'), admin_url('options-general.php')).'">'.__('Settings').'</a>';
410
+ $links = array_merge($links, array($settings_link));
411
+ }
412
+ return $links;
413
+ }
414
+
415
+ // Add column to Users table
416
+ function wpua_add_column($columns){
417
+ return $columns + array('wp-user-avatar' => __('WP User Avatar'));
418
+ }
419
+
420
+ // Show thumbnail in Users table
421
+ function wpua_show_column($value, $column_name, $user_id){
422
+ global $wpdb, $blog_id;
423
+ $wpua = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
424
+ $wpua_image = wp_get_attachment_image($wpua, array(32,32));
425
+ if($column_name == 'wp-user-avatar'){
426
+ return $wpua_image;
427
+ }
428
+ }
429
+
430
+ // Media uploader
431
+ function wpua_media_upload_scripts(){
432
+ if(function_exists('wp_enqueue_media')){
433
+ wp_enqueue_script('admin-bar');
434
+ wp_enqueue_media();
435
+ } else {
436
+ wp_enqueue_script('media-upload');
437
+ wp_enqueue_script('thickbox');
438
+ wp_enqueue_style('thickbox');
439
+ }
440
+ wp_enqueue_script('wp-user-avatar', WPUA_URLPATH.'js/wp-user-avatar.js', "", WPUA_VERSION);
441
+ wp_enqueue_style('wp-user-avatar', WPUA_URLPATH.'css/wp-user-avatar.css', "", WPUA_VERSION);
442
+ }
443
+ }
444
+
445
+ // Uploader scripts
446
+ function wpua_js($section, $avatar_thumb){ ?>
447
+ <script type="text/javascript">
448
+ jQuery(function(){
449
+ <?php if(current_user_can('upload_files')) : ?>
450
+ <?php if(function_exists('wp_enqueue_media')) : // Backbone uploader for WP 3.5+ ?>
451
+ openMediaUploader("<?php echo $section; ?>");
452
+ <?php else : // Fall back to Thickbox uploader ?>
453
+ openThickboxUploader("<?php echo $section; ?>", "<?php echo get_admin_url(); ?>media-upload.php?post_id=0&type=image&tab=library&TB_iframe=1");
454
+ <?php endif; ?>
455
+ <?php endif; ?>
456
+ removeWPUserAvatar("<?php echo htmlspecialchars_decode($avatar_thumb); ?>");
457
+ });
458
+ </script>
459
+ <?php
460
+ }
461
+
462
+ // Returns true if user has Gravatar-hosted image
463
+ function wpua_has_gravatar($id_or_email, $has_gravatar=false, $user="", $email=""){
464
+ global $ssl;
465
+ if(!is_object($id_or_email) && !empty($id_or_email)){
466
+ // Find user by ID or e-mail address
467
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
468
+ // Get registered user e-mail address
469
+ $email = !empty($user) ? $user->user_email : "";
470
+ }
471
+ // Check if Gravatar image returns 200 (OK) or 404 (Not Found)
472
+ if(!empty($email)){
473
+ $hash = md5(strtolower(trim($email)));
474
+ $gravatar = 'http'.$ssl.'://www.gravatar.com/avatar/'.$hash.'?d=404';
475
+ $headers = @get_headers($gravatar);
476
+ $has_gravatar = !preg_match("|200|", $headers[0]) ? false : true;
477
+ }
478
+ return $has_gravatar;
479
+ }
480
+
481
+ // Returns true if user has wp_user_avatar
482
+ function has_wp_user_avatar($id_or_email="", $has_wpua=false, $user="", $user_id=""){
483
+ global $wpdb, $blog_id;
484
+ if(!is_object($id_or_email) && !empty($id_or_email)){
485
+ // Find user by ID or e-mail address
486
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
487
+ // Get registered user ID
488
+ $user_id = !empty($user) ? $user->ID : "";
489
+ }
490
+ $wpua = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
491
+ $has_wpua = !empty($wpua) ? true : false;
492
+ return $has_wpua;
493
+ }
494
+
495
+ // Replace get_avatar only in get_wp_user_avatar
496
+ function wpua_get_avatar_filter($avatar, $id_or_email, $size="", $default="", $alt=""){
497
+ global $post, $comment, $avatar_default, $wpua_avatar_default, $mustache_original, $mustache_medium, $mustache_thumbnail, $mustache_avatar, $mustache_admin;
498
+ // User has WPUA
499
+ if(is_object($id_or_email)){
500
+ if(!empty($comment->comment_author_email)){
501
+ $avatar = get_wp_user_avatar($comment->comment_author_email, $size, $default, $alt);
502
+ } else {
503
+ $avatar = get_wp_user_avatar('unknown@gravatar.com', $size, $default, $alt);
504
+ }
505
+ } else {
506
+ if(has_wp_user_avatar($id_or_email)){
507
+ $avatar = get_wp_user_avatar($id_or_email, $size, $default, $alt);
508
+ // User has Gravatar
509
+ } elseif(wpua_has_gravatar($id_or_email)){
510
+ $avatar = $avatar;
511
+ // User doesn't have WPUA or Gravatar and Default Avatar is wp_user_avatar, show custom Default Avatar
512
+ } elseif($avatar_default == 'wp_user_avatar'){
513
+ // Show custom Default Avatar
514
+ if(!empty($wpua_avatar_default)){
515
+ // Get image
516
+ $wpua_avatar_default_image = wp_get_attachment_image_src($wpua_avatar_default, array($size,$size));
517
+ // Image src
518
+ $default = $wpua_avatar_default_image[0];
519
+ // Add dimensions if numeric size
520
+ $dimensions = ' width="'.$wpua_avatar_default_image[1].'" height="'.$wpua_avatar_default_image[2].'"';
521
+ $defaultcss = "";
522
+ } else {
523
+ // Get mustache image based on numeric size comparison
524
+ if($size > get_option('medium_size_w')){
525
+ $default = $mustache_original;
526
+ } elseif($size <= get_option('medium_size_w') && $size > get_option('thumbnail_size_w')){
527
+ $default = $mustache_medium;
528
+ } elseif($size <= get_option('thumbnail_size_w') && $size > 96){
529
+ $default = $mustache_thumbnail;
530
+ } elseif($size <= 96 && $size > 32){
531
+ $default = $mustache_avatar;
532
+ } elseif($size <= 32){
533
+ $default = $mustache_admin;
534
+ }
535
+ // Add dimensions if numeric size
536
+ $dimensions = ' width="'.$size.'" height="'.$size.'"';
537
+ $defaultcss = ' avatar-default';
538
+ }
539
+ // Construct the img tag
540
+ $avatar = "<img src='".$default."'".$dimensions." alt='".$alt."' class='wp-user-avatar wp-user-avatar-".$size." avatar avatar-".$size." photo'".$defaultcss." />";
541
+ }
542
+ }
543
+ return $avatar;
544
+ }
545
+ add_filter('get_avatar', 'wpua_get_avatar_filter', 10, 6);
546
+
547
+ // Get original avatar, for when user removes wp_user_avatar
548
+ function wpua_get_avatar_original($id_or_email, $size="", $default="", $alt=""){
549
+ global $avatar_default, $wpua_avatar_default, $mustache_avatar, $pagenow;
550
+ // Remove get_avatar filter
551
+ if(is_admin()){
552
+ remove_filter('get_avatar', 'wpua_get_avatar_filter');
553
+ }
554
+ // User doesn't Gravatar and Default Avatar is wp_user_avatar, show custom Default Avatar
555
+ if(!wpua_has_gravatar($id_or_email) && $avatar_default == 'wp_user_avatar'){
556
+ // Show custom Default Avatar
557
+ if(!empty($wpua_avatar_default)){
558
+ $wpua_avatar_default_image = wp_get_attachment_image_src($wpua_avatar_default, array($size,$size));
559
+ $default = $wpua_avatar_default_image[0];
560
+ } else {
561
+ $default = $mustache_avatar;
562
+ }
563
+ } else {
564
+ // Get image from Gravatar, whether it's the user's image or default image
565
+ $wpua_image = get_avatar($id_or_email, $size);
566
+ // Takes the img tag, extracts the src
567
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wpua_image, $matches, PREG_SET_ORDER);
568
+ $default = $matches [0] [1];
569
+ }
570
+ return $default;
571
+ }
572
+
573
+ // Find WPUA, show get_avatar if empty
574
+ function get_wp_user_avatar($id_or_email="", $size='96', $align="", $alt=""){
575
+ global $post, $comment, $avatar_default, $wpdb, $blog_id;
576
+ // Checks if comment
577
+ if(is_object($id_or_email)){
578
+ // Checks if comment author is registered user by user ID
579
+ if($comment->user_id != '0'){
580
+ $id_or_email = $comment->user_id;
581
+ $user = get_user_by('id', $id_or_email);
582
+ // Checks that comment author isn't anonymous
583
+ } elseif(!empty($comment->comment_author_email)){
584
+ // Checks if comment author is registered user by e-mail address
585
+ $user = get_user_by('email', $comment->comment_author_email);
586
+ // Get registered user info from profile, otherwise e-mail address should be value
587
+ $id_or_email = !empty($user) ? $user->ID : $comment->comment_author_email;
588
+ }
589
+ $alt = $comment->comment_author;
590
+ } else {
591
+ if(!empty($id_or_email)){
592
+ // Find user by ID or e-mail address
593
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
594
+ } else {
595
+ // Find author's name if id_or_email is empty
596
+ $author_name = get_query_var('author_name');
597
+ if(is_author()){
598
+ // On author page, get user by page slug
599
+ $user = get_user_by('slug', $author_name);
600
+ } else {
601
+ // On post, get user by author meta
602
+ $user_id = get_the_author_meta('ID');
603
+ $user = get_user_by('id', $user_id);
604
+ }
605
+ }
606
+ // Set user's ID and name
607
+ if(!empty($user)){
608
+ $id_or_email = $user->ID;
609
+ $alt = $user->display_name;
610
+ }
611
+ }
612
+ // Checks if user has WPUA
613
+ $wpua_meta = !empty($id_or_email) ? get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email) : "";
614
+ // Add alignment class
615
+ $alignclass = !empty($align) ? ' align'.$align : "";
616
+ // User has WPUA, bypass get_avatar
617
+ if(!empty($wpua_meta)){
618
+ // Numeric size use size array
619
+ $get_size = is_numeric($size) ? array($size,$size) : $size;
620
+ // Get image src
621
+ $wpua_image = wp_get_attachment_image_src($wpua_meta, $get_size);
622
+ // Add dimensions to img only if numeric size was specified
623
+ $dimensions = is_numeric($size) ? ' width="'.$wpua_image[1].'" height="'.$wpua_image[2].'"' : "";
624
+ // Construct the img tag
625
+ $avatar = '<img src="'.$wpua_image[0].'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.$alignclass.' avatar avatar avatar-'.$size.' photo" />';
626
+ } else {
627
+ // Get numeric sizes for non-numeric sizes based on media options
628
+ if($size == 'original' || $size == 'large' || $size == 'medium' || $size == 'thumbnail'){
629
+ $get_size = ($size == 'original') ? get_option('large_size_w') : get_option($size.'_size_w');
630
+ } else {
631
+ // Numeric sizes leave as-is
632
+ $get_size = $size;
633
+ }
634
+ // User with no WPUA uses get_avatar
635
+ $avatar = get_avatar($id_or_email, $get_size, $default="", $alt="");
636
+ // Remove width and height for non-numeric sizes
637
+ if(!is_numeric($size)){
638
+ $avatar = preg_replace("/(width|height)=\'\d*\'\s/", "", $avatar);
639
+ $avatar = preg_replace('/(width|height)=\"\d*\"\s/', "", $avatar);
640
+ $avatar = str_replace('wp-user-avatar wp-user-avatar-'.$get_size.' ', "", $avatar);
641
+ $avatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$size.$alignclass." ", $avatar);
642
+ }
643
+ }
644
+ return $avatar;
645
+ }
646
+
647
+ // Return just the image src
648
+ function get_wp_user_avatar_src($id_or_email, $size="", $align=""){
649
+ $wpua_image_src = "";
650
+ // Gets the avatar img tag
651
+ $wpua_image = get_wp_user_avatar($id_or_email, $size, $align);
652
+ // Takes the img tag, extracts the src
653
+ if(!empty($wpua_image)){
654
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wpua_image, $matches, PREG_SET_ORDER);
655
+ $wpua_image_src = $matches [0] [1];
656
+ }
657
+ return $wpua_image_src;
658
+ }
659
+
660
+ // Shortcode
661
+ function wpua_shortcode($atts, $content){
662
+ global $wpdb, $blog_id;
663
+ // Set shortcode attributes
664
+ extract(shortcode_atts(array('user' => "", 'size' => '96', 'align' => "", 'link' => "", 'target' => ""), $atts));
665
+ // Find user by ID, login, slug, or e-mail address
666
+ if(!empty($user)){
667
+ $user = is_numeric($user) ? get_user_by('id', $user) : get_user_by('login', $user);
668
+ $user = empty($user) ? get_user_by('slug', $user) : $user;
669
+ $user = empty($user) ? get_user_by('email', $user) : $user;
670
+ }
671
+ // Get user ID
672
+ $id_or_email = !empty($user) ? $user->ID : "";
673
+ // Check if link is set
674
+ if(!empty($link)){
675
+ // CSS class is same as link type, except for URL
676
+ $link_class = $link;
677
+ // Open in new window
678
+ $target_link = !empty($target) ? ' target="'.$target.'"' : "";
679
+ if($link == 'file'){
680
+ // Get image src
681
+ $image_link = get_wp_user_avatar_src($id_or_email, 'original', $align);
682
+ } elseif($link == 'attachment'){
683
+ // Get attachment URL
684
+ $image_link = get_attachment_link(get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email));
685
+ } else {
686
+ // URL
687
+ $image_link = $link;
688
+ $link_class = 'custom';
689
+ }
690
+ // Wrap the avatar inside the link
691
+ $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>';
692
+ } else {
693
+ // Get WPUA as normal
694
+ $avatar = get_wp_user_avatar($id_or_email, $size, $align);
695
+ }
696
+ return $avatar;
697
+ }
698
+ add_shortcode('avatar', 'wpua_shortcode');
699
+
700
+ // Add default avatar
701
+ function wpua_add_default_avatar($avatar_list=null){
702
+ global $avatar_default, $wpua_avatar_default, $mustache_medium, $mustache_admin;
703
+ // Remove get_avatar filter
704
+ remove_filter('get_avatar', 'wpua_get_avatar_filter');
705
+ // Set avatar_list variable
706
+ $avatar_list = "";
707
+ // Set avatar defaults
708
+ $avatar_defaults = array(
709
+ 'mystery' => __('Mystery Man'),
710
+ 'blank' => __('Blank'),
711
+ 'gravatar_default' => __('Gravatar Logo'),
712
+ 'identicon' => __('Identicon (Generated)'),
713
+ 'wavatar' => __('Wavatar (Generated)'),
714
+ 'monsterid' => __('MonsterID (Generated)'),
715
+ 'retro' => __('Retro (Generated)')
716
+ );
717
+ // No Default Avatar, set to Mystery Man
718
+ if(empty($avatar_default)){
719
+ $avatar_default = 'mystery';
720
+ }
721
+ // Take avatar_defaults and get examples for unknown@gravatar.com
722
+ foreach($avatar_defaults as $default_key => $default_name){
723
+ $avatar = get_avatar('unknown@gravatar.com', 32, $default_key);
724
+ $selected = ($avatar_default == $default_key) ? 'checked="checked" ' : "";
725
+ $avatar_list .= "\n\t<label><input type='radio' name='avatar_default' id='avatar_{$default_key}' value='".esc_attr($default_key)."' {$selected}/> ";
726
+ $avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&amp;forcedefault=1'", $avatar);
727
+ $avatar_list .= ' '.$default_name.'</label>';
728
+ $avatar_list .= '<br />';
729
+ }
730
+ // Show remove link if custom Default Avatar is set
731
+ if(!empty($wpua_avatar_default)){
732
+ $avatar_thumb_src = wp_get_attachment_image_src($wpua_avatar_default, array(32,32));
733
+ $avatar_thumb = $avatar_thumb_src[0];
734
+ $hide_remove = "";
735
+ } else {
736
+ $avatar_thumb = $mustache_admin;
737
+ $hide_remove = ' class="hide-me"';
738
+ }
739
+ // Default Avatar is wp_user_avatar, check the radio button next to it
740
+ $selected_avatar = ($avatar_default == 'wp_user_avatar') ? ' checked="checked" ' : "";
741
+ // Wrap WPUA in div
742
+ $avatar_thumb_img = '<div id="wp-user-avatar-preview"><img src="'.$avatar_thumb.'" width="32" /></div>';
743
+ // Add WPUA to list
744
+ $wpua_list = "\n\t<label><input type='radio' name='avatar_default' id='wp_user_avatar_radio' value='wp_user_avatar'$selected_avatar /> ";
745
+ $wpua_list .= preg_replace("/src='(.+?)'/", "src='\$1'", $avatar_thumb_img);
746
+ $wpua_list .= ' '.__('WP User Avatar').'</label>';
747
+ $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>';
748
+ $wpua_list .= '<a href="#" id="remove-wp-user-avatar"'.$hide_remove.'>'.__('Remove').'</a></p>';
749
+ $wpua_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="'.$wpua_avatar_default.'">';
750
+ $wpua_list .= '<p id="wp-user-avatar-message">'.__('Press "Save Changes" to save your changes.').'</p>';
751
+ $wpua_list .= wpua_js('Default Avatar', $mustache_admin);
752
+ return $wpua_list.$avatar_list;
753
+ }
754
+ add_filter('default_avatar_select', 'wpua_add_default_avatar', 10);
755
+
756
+ // Add default avatar_default to whitelist
757
+ function wpua_whitelist_options($whitelist_options){
758
+ $whitelist_options['discussion'][] = 'avatar_default_wp_user_avatar';
759
+ return $whitelist_options;
760
+ }
761
+ add_filter('whitelist_options', 'wpua_whitelist_options', 10);
762
+
763
+ // Add media state
764
+ function wpua_add_media_state($media_states){
765
+ global $post, $wpua_avatar_default;
766
+ $is_wpua = get_post_custom_values('_wp_attachment_wp_user_avatar', $post->ID);
767
+ if(!empty($is_wpua)){
768
+ $media_states[] = __('Avatar');
769
+ }
770
+ if(!empty($wpua_avatar_default) && ($wpua_avatar_default == $post->ID)){
771
+ $media_states[] = __('Default Avatar');
772
+ }
773
+ return apply_filters('wpua_add_media_state', $media_states);
774
+ }
775
+ add_filter('display_media_states', 'wpua_add_media_state', 10, 1);
776
+
777
+ // Check if image is used as WPUA
778
+ function wpua_image($attachment_id, $user_id, $wpua_image=false){
779
+ global $wpdb;
780
+ $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));
781
+ if(!empty($wpua)){
782
+ $wpua_image = true;
783
+ }
784
+ return $wpua_image;
785
+ }
786
+
787
+ // Check who owns image
788
+ function wpua_author($attachment_id, $user_id, $wpua_author=false){
789
+ $attachment = get_post($attachment_id);
790
+ if(!empty($attachment) && $attachment->post_author == $user_id){
791
+ $wpua_author = true;
792
+ }
793
+ return $wpua_author;
794
+ }
795
+
796
+ // Admin page
797
+ function wpua_options_page(){
798
+ // Give subscribers edit_posts capability
799
+ if(isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true' && empty($wpua_allow_upload)){
800
+ wpua_subscriber_remove_cap();
801
+ }
802
+ ?>
803
+ <div class="wrap">
804
+ <?php screen_icon(); ?>
805
+ <h2><?php _e('WP User Avatar'); ?></h2>
806
+ <form method="post" action="options.php">
807
+ <?php settings_fields('wpua-settings-group'); ?>
808
+ <?php do_settings_fields('wpua-settings-group', ""); ?>
809
+ <table class="form-table">
810
+ <tr valign="top">
811
+ <th scope="row"><?php _e('WP User Avatar Settings') ?></th>
812
+ <td>
813
+ <fieldset>
814
+ <legend class="screen-reader-text"><span><?php _e('WP User Avatar Settings') ?></span></legend>
815
+ <label for="wp_user_avatar_tinymce">
816
+ <input name="wp_user_avatar_tinymce" type="checkbox" id="wp_user_avatar_tinymce" value="1" <?php checked('1', get_option('wp_user_avatar_tinymce')); ?> />
817
+ <?php _e('Add avatar button to Visual Editor'); ?>
818
+ </label>
819
+ <br />
820
+ <label for="wp_user_avatar_allow_upload">
821
+ <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')); ?> />
822
+ <?php _e('Allow Contributors &amp; Subscribers to upload avatars'); ?>
823
+ </label>
824
+ </fieldset>
825
+ </td>
826
+ </tr>
827
+ </table>
828
+ <h3 class="title"><?php _e('Avatars'); ?></h3>
829
+ <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>
830
+ <table class="form-table">
831
+ <tr valign="top">
832
+ <th scope="row"><?php _e('Avatar Display'); ?></th>
833
+ <td>
834
+ <fieldset>
835
+ <legend class="screen-reader-text"><span><?php _e('Avatar Display'); ?></span></legend>
836
+ <label for="show_avatars">
837
+ <input type="checkbox" id="show_avatars" name="show_avatars" value="1" <?php checked('1', get_option('show_avatars')); ?> />
838
+ <?php _e('Show Avatars'); ?>
839
+ </label>
840
+ </fieldset>
841
+ </td>
842
+ </tr>
843
+ <tr valign="top">
844
+ <th scope="row"><?php _e('Maximum Rating'); ?></th>
845
+ <td>
846
+ <fieldset>
847
+ <legend class="screen-reader-text"><span><?php _e('Maximum Rating'); ?></span></legend>
848
+ <?php
849
+ $ratings = array(
850
+ 'G' => __('G &#8212; Suitable for all audiences'),
851
+ 'PG' => __('PG &#8212; Possibly offensive, usually for audiences 13 and above'),
852
+ 'R' => __('R &#8212; Intended for adult audiences above 17'),
853
+ 'X' => __('X &#8212; Even more mature than above')
854
+ );
855
+ foreach ($ratings as $key => $rating) :
856
+ $selected = (get_option('avatar_rating') == $key) ? 'checked="checked"' : "";
857
+ echo "\n\t<label><input type='radio' name='avatar_rating' value='" . esc_attr($key) . "' $selected/> $rating</label><br />";
858
+ endforeach;
859
+ ?>
860
+ </fieldset>
861
+ </td>
862
+ </tr>
863
+ <tr valign="top">
864
+ <th scope="row"><?php _e('Default Avatar') ?></th>
865
+ <td class="defaultavatarpicker">
866
+ <fieldset>
867
+ <legend class="screen-reader-text"><span><?php _e('Default Avatar'); ?></span></legend>
868
+ <?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 />
869
+ <?php echo wpua_add_default_avatar(); ?>
870
+ </fieldset>
871
+ </td>
872
+ </tr>
873
+ </table>
874
+ <?php submit_button(); ?>
875
+ </form>
876
+ </div>
877
+ <?php
878
+ }
879
+
880
+ // Whitelist settings
881
+ function wpua_admin_settings(){
882
+ register_setting('wpua-settings-group', 'wp_user_avatar_tinymce');
883
+ register_setting('wpua-settings-group', 'wp_user_avatar_allow_upload');
884
+ register_setting('wpua-settings-group', 'show_avatars');
885
+ register_setting('wpua-settings-group', 'avatar_rating');
886
+ register_setting('wpua-settings-group', 'avatar_default');
887
+ register_setting('wpua-settings-group', 'avatar_default_wp_user_avatar');
888
+ }
889
+
890
+ // Add options page and settings
891
+ function wpua_admin(){
892
+ add_options_page('WP User Avatar Plugin Settings', 'WP User Avatar', 'manage_options', 'wp-user-avatar', 'wpua_options_page');
893
+ add_action('admin_init', 'wpua_admin_settings');
894
+ }
895
+
896
+ // Initialize WPUA after other plugins are loaded
897
+ function wpua_load(){
898
+ global $wpua_instance;
899
+ $wpua_instance = new wp_user_avatar();
900
+ }
901
+ add_action('plugins_loaded', 'wpua_load');
902
+ }
903
+ ?>