Simple Local Avatars - Version 1.0

Version Description

Download this release

Release Info

Developer jakemgold
Plugin Icon 128x128 Simple Local Avatars
Version 1.0
Comparing to
See all releases

Version 1.0

Files changed (3) hide show
  1. readme.txt +34 -0
  2. screenshot-1.png +0 -0
  3. simple-local-avatars.php +270 -0
readme.txt ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Simple Local Avatars ===
2
+ Contributors: jakemgold
3
+ Donate link: http://www.thinkoomph.com/plugins-modules/wordpress-simple-local-avatars/
4
+ Tags: avatar, gravatar, user photos, users, profile
5
+ Requires at least: 3.0
6
+ Tested up to: 3.1
7
+ Stable tag: 1.0
8
+
9
+ Adds an avatar upload field to user profiles if the current user has media permissions. Generates requested sizes on demand just like Gravatar!
10
+
11
+ == Description ==
12
+
13
+ Adds an avatar upload field to user profiles if the current user has media permissions. Generates requested sizes on demand just like Gravatar! Simple and lightweight.
14
+
15
+ Just edit a user profile, and scroll down to the new "Avatar" field. The plug-in will take care of cropping and sizing!
16
+
17
+ Unlike other avatar plug-ins, Simple Local Avatars:
18
+
19
+ 1. Stores avatars in the "uploads" folder where all of your other media is kept
20
+ 1. Has a simple, native interface
21
+ 1. Fully supports Gravatar and default avatars if no local avatar is set for the user
22
+ 1. Generates the requested avatar size on demand (and stores the new size for efficiency), so it looks great, just like Gravatar!
23
+
24
+
25
+ == Installation ==
26
+
27
+ 1. Install easily with the WordPress plugin control panel or manually download the plugin and upload the extracted folder to the `/wp-content/plugins/` directory
28
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
29
+ 1. Start uploading avatars by editing user profiles!
30
+
31
+
32
+ == Screenshots ==
33
+
34
+ 1. Avatar upload field on a user profile page
screenshot-1.png ADDED
Binary file
simple-local-avatars.php ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ Plugin Name: Simple Local Avatars
4
+ Plugin URI: http://www.thinkoomph.com/plugins-modules/wordpress-simple-local-avatars/
5
+ Description: Adds an avatar upload field to user profiles if the current user has media permissions. Generates requested sizes on demand just like Gravatar! Simple and lightweight.
6
+ Version: 1.0
7
+ Author: Jake Goldman (Oomph, Inc)
8
+ Author URI: http://www.thinkoomph.com
9
+
10
+ Plugin: Copyright 2011 Oomph, Inc (email : jake@thinkoomph.com)
11
+
12
+ This program is free software; you can redistribute it and/or modify
13
+ it under the terms of the GNU General Public License as published by
14
+ the Free Software Foundation; either version 2 of the License, or
15
+ (at your option) any later version.
16
+
17
+ This program is distributed in the hope that it will be useful,
18
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ GNU General Public License for more details.
21
+
22
+ You should have received a copy of the GNU General Public License
23
+ along with this program; if not, write to the Free Software
24
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
+ */
26
+
27
+ /**
28
+ * add field to user profiles
29
+ */
30
+
31
+ class simple_local_avatars
32
+ {
33
+ function simple_local_avatars()
34
+ {
35
+ add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 );
36
+
37
+ add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );
38
+ add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
39
+
40
+ add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) );
41
+ add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );
42
+
43
+ add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );
44
+ }
45
+
46
+ function get_avatar( $avatar = '', $id_or_email, $size = '96', $default = '', $alt = false )
47
+ {
48
+ if ( is_numeric($id_or_email) )
49
+ $user_id = (int) $id_or_email;
50
+ elseif ( is_string($id_or_email) )
51
+ {
52
+ if ( $user = get_user_by_email( $id_or_email ) )
53
+ $user_id = $user->ID;
54
+ }
55
+ elseif ( is_object($id_or_email) && !empty($id_or_email->user_id) )
56
+ $user_id = (int) $id_or_email->user_id;
57
+
58
+ if ( isset($user_id) )
59
+ $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
60
+
61
+ if ( !isset($local_avatars) || empty($local_avatars) || !isset($local_avatars['full']) )
62
+ {
63
+ if ( !empty($avatar) ) // if called by filter
64
+ return $avatar;
65
+
66
+ remove_filter( 'get_avatar', 'get_simple_local_avatar' );
67
+ $avatar = get_avatar( $id_or_email, $size, $default );
68
+ add_filter( 'get_avatar', 'get_simple_local_avatar', 10, 5 );
69
+ return $avatar;
70
+ }
71
+
72
+ if ( !is_numeric($size) ) // ensure valid size
73
+ $size = '96';
74
+
75
+ if ( empty($alt) )
76
+ $alt = get_the_author_meta( 'display_name', $user_id );
77
+
78
+ // generate a new size
79
+ if ( !isset( $local_avatars[$size] ) )
80
+ {
81
+ $image_sized = image_resize( ABSPATH . $local_avatars['full'], $size, $size, true );
82
+
83
+ if ( is_wp_error($image_sized) ) // deal with original being >= to original image (or lack of sizing ability)
84
+ $local_avatars[$size] = $local_avatars['full'];
85
+ else
86
+ $local_avatars[$size] = str_replace( ABSPATH, '', $image_sized );
87
+
88
+ update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
89
+ }
90
+
91
+ $author_class = is_author( $user_id ) ? ' current-author' : '' ;
92
+ $avatar = "<img alt='" . esc_attr($alt) . "' src='" . site_url( $local_avatars[$size] ) . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";
93
+
94
+ return $avatar;
95
+ }
96
+
97
+ function edit_user_profile( $profileuser )
98
+ {
99
+ ?>
100
+ <h3><?php _e( 'Avatar' ); ?></h3>
101
+
102
+ <table class="form-table">
103
+ <tr>
104
+ <th><label for="simple-local-avatar"><?php _e('Upload Avatar'); ?></label></th>
105
+ <td style="width: 50px;" valign="top">
106
+ <?php echo get_avatar( $profileuser->ID ); ?>
107
+ </td>
108
+ <td>
109
+ <?php
110
+ if ( current_user_can('upload_files') )
111
+ {
112
+ do_action( 'simple_local_avatar_notices' );
113
+ wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
114
+ ?>
115
+ <input type="file" name="simple-local-avatar" id="simple-local-avatar" /><br />
116
+ <?php
117
+ if ( !isset( $profileuser->simple_local_avatar ) || empty( $profileuser->simple_local_avatar ) )
118
+ echo '<span class="description">' . __('No local avatar is set. Use the upload field to add a local avatar.') . '</span>';
119
+ else
120
+ echo '
121
+ <input type="checkbox" name="simple-local-avatar-erase" value="1" /> ' . __('Delete local avatar') . '<br />
122
+ <span class="description">' . __('Replace the local avatar by uploading a new avatar, or erase the local avatar (falling back to a gravatar) by checking the delete option.') . '</span>
123
+ ';
124
+ }
125
+ else
126
+ {
127
+ if ( !isset( $profileuser->simple_local_avatar ) || empty( $profileuser->simple_local_avatar ) )
128
+ echo '<span class="description">' . __('No local avatar is set. Set up your avatar at Gravatar.com.') . '</span>';
129
+ else
130
+ echo '
131
+ <span class="description">' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.') . '</span>
132
+ ';
133
+ }
134
+ ?>
135
+ </td>
136
+ </tr>
137
+ </table>
138
+
139
+ <script type="text/javascript">
140
+ var form = document.getElementById('your-profile');
141
+ form.encoding = 'multipart/form-data';
142
+ form.setAttribute('enctype', 'multipart/form-data');
143
+ </script>
144
+ <?php
145
+
146
+ }
147
+
148
+ function edit_user_profile_update( $user_id )
149
+ {
150
+ if ( !wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) || !current_user_can('upload_files') ) //security
151
+ return;
152
+
153
+ if ( !empty( $_FILES['simple-local-avatar']['name'] ) )
154
+ {
155
+ $mimes = array(
156
+ 'jpg|jpeg|jpe' => 'image/jpeg',
157
+ 'gif' => 'image/gif',
158
+ 'png' => 'image/png',
159
+ 'bmp' => 'image/bmp',
160
+ 'tif|tiff' => 'image/tiff'
161
+ );
162
+
163
+ $avatar = wp_handle_upload( $_FILES['simple-local-avatar'], array( 'mimes' => $mimes, 'test_form' => false ) );
164
+
165
+ if ( !isset($avatar['file']) ) // handle failures
166
+ {
167
+ switch ( $avatar['error'] )
168
+ {
169
+ case 'File type does not meet security guidelines. Try another.' :
170
+ add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error",__("Please upload a valid image file for the avatar."));') );
171
+ break;
172
+ default :
173
+ add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error","<strong>".__("There was an error uploading the avatar:")."</strong> ' . esc_attr( $avatar['error'] ) . '");') );
174
+ }
175
+
176
+ return;
177
+ }
178
+
179
+ delete_user_meta( $user_id, 'simple_local_avatar' );
180
+
181
+ // delete old images if successful
182
+ $old_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
183
+ foreach ($old_avatars as $old_avatar )
184
+ @unlink( ABSPATH . $old_avatar );
185
+
186
+ // set up new avatar meta array
187
+ $avatar_meta = array();
188
+ $avatar_meta['full'] = str_replace( ABSPATH, '', $avatar['file'] );
189
+
190
+ // create some common sizes now
191
+
192
+ $sizes = array( 32, 50, 96 );
193
+
194
+ foreach ( $sizes as $size ) :
195
+
196
+ $image_sized = image_resize( $avatar['file'], $size, $size, true );
197
+
198
+ if ( is_wp_error($image_sized) ) // deal with original being >= to original image (or lack of sizing ability)
199
+ $image_sized = $avatar['file'];
200
+
201
+ $avatar_meta[$size] = str_replace( ABSPATH, '', $image_sized );
202
+
203
+ endforeach;
204
+
205
+ // save user information (overwriting old)
206
+ update_user_meta( $user_id, 'simple_local_avatar', $avatar_meta );
207
+ }
208
+ elseif ( isset($_POST['simple-local-avatar-erase']) && $_POST['simple-local-avatar-erase'] == 1 )
209
+ {
210
+ // delete old images if successful
211
+ $old_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
212
+ foreach ($old_avatars as $old_avatar )
213
+ unlink( ABSPATH . $old_avatar );
214
+
215
+ delete_user_meta( $user_id, 'simple_local_avatar' );
216
+ }
217
+ }
218
+
219
+ /**
220
+ * remove the custom get_avatar hook for the default avatar list output on options-discussion.php
221
+ */
222
+ function avatar_defaults( $avatar_defaults )
223
+ {
224
+ remove_action( 'get_avatar', array( $this, 'get_avatar' ) );
225
+ return $avatar_defaults;
226
+ }
227
+ }
228
+
229
+ $simple_local_avatars = new simple_local_avatars;
230
+
231
+ if ( !function_exists('get_simple_local_avatar') ) :
232
+
233
+ /**
234
+ * more efficient to call simple local avatar directly in theme and avoid gravatar setup
235
+ *
236
+ * @param int|string|object $id_or_email A user ID, email address, or comment object
237
+ * @param int $size Size of the avatar image
238
+ * @param string $default URL to a default image to use if no avatar is available
239
+ * @param string $alt Alternate text to use in image tag. Defaults to blank
240
+ * @return string <img> tag for the user's avatar
241
+ */
242
+ function get_simple_local_avatar( $id_or_email, $size = '96', $default = '', $alt = false )
243
+ {
244
+ global $simple_local_avatars;
245
+ return $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );
246
+ }
247
+
248
+ endif;
249
+
250
+ /**
251
+ * on uninstallation, remove the custom field from the users and delete the local avatars
252
+ */
253
+
254
+ register_uninstall_hook( __FILE__, 'simple_local_avatars_uninstall' );
255
+
256
+ function simple_local_avatars_uninstall()
257
+ {
258
+ $users = get_users_of_blog();
259
+
260
+ foreach ( $users as $user )
261
+ {
262
+ if ( $old_avatars = get_user_meta( $user->$user_id, 'simple_local_avatar', true ) )
263
+ {
264
+ foreach ($old_avatars as $old_avatar )
265
+ unlink( ABSPATH . $old_avatar );
266
+
267
+ delete_user_meta( $user->$user_id, 'simple_local_avatar' );
268
+ }
269
+ }
270
+ }