Simple Local Avatars - Version 1.3

Version Description

  • Avatar file name saved as "user-display-name_avatar" (or other image extension)
  • Russian localization added
  • Assorted minor code optimizations
Download this release

Release Info

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

Code changes from version 1.2.3 to 1.3

Files changed (2) hide show
  1. readme.txt +13 -5
  2. simple-local-avatars.php +75 -95
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === Simple Local Avatars ===
2
- Contributors: jakemgold, thinkoomph
3
- Donate link: http://www.get10up.com/plugins/simple-local-avatars-wordpress/
4
  Tags: avatar, gravatar, user photos, users, profile
5
  Requires at least: 3.0
6
- Tested up to: 3.1
7
- Stable tag: 1.2.3
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
 
@@ -35,8 +35,16 @@ Unlike other avatar plug-ins, Simple Local Avatars:
35
 
36
  == Changelog ==
37
 
 
 
 
 
 
 
 
 
38
  = 1.2.3 =
39
- * Russion localization
40
 
41
  = 1.2.2 =
42
  * Fix for avatars uploaded pre-1.2.1 having a broken path after upgrade
1
  === Simple Local Avatars ===
2
+ Contributors: jakemgold, 10up, thinkoomph
3
+ Donate link: http://get10up.com/plugins/simple-local-avatars-wordpress/
4
  Tags: avatar, gravatar, user photos, users, profile
5
  Requires at least: 3.0
6
+ Tested up to: 3.2.1
7
+ Stable tag: 1.3
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
 
35
 
36
  == Changelog ==
37
 
38
+ = 1.3 =
39
+ * Avatar file name saved as "user-display-name_avatar" (or other image extension)
40
+ * Russian localization added
41
+ * Assorted minor code optimizations
42
+
43
+ = 1.2.4 =
44
+ * Support for front end avatar uploads (e.g. Theme My Profile)
45
+
46
  = 1.2.3 =
47
+ * Russian localization
48
 
49
  = 1.2.2 =
50
  * Fix for avatars uploaded pre-1.2.1 having a broken path after upgrade
simple-local-avatars.php CHANGED
@@ -1,13 +1,13 @@
1
  <?php
2
  /**
3
  Plugin Name: Simple Local Avatars
4
- Plugin URI: http://www.get10up.com/plugins/simple-local-avatars-wordpress/
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.2.3
7
- Author: Jake Goldman (10up), Oomph Inc
8
- Author URI: http://www.get10up.com
9
 
10
- Plugin: Copyright 2011 10up (email : jake@get10up.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
@@ -30,8 +30,7 @@
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( 'admin_init', array( $this, 'admin_init' ) );
@@ -45,77 +44,61 @@ class simple_local_avatars
45
  add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );
46
  }
47
 
48
- function get_avatar( $avatar = '', $id_or_email, $size = '96', $default = '', $alt = false )
49
- {
50
- if ( is_numeric($id_or_email) )
51
  $user_id = (int) $id_or_email;
52
- elseif ( is_string($id_or_email) )
53
- {
54
- if ( $user = get_user_by_email( $id_or_email ) )
55
- $user_id = $user->ID;
56
- }
57
- elseif ( is_object($id_or_email) && !empty($id_or_email->user_id) )
58
  $user_id = (int) $id_or_email->user_id;
59
 
60
- if ( !empty($user_id) )
61
- $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
 
 
62
 
63
- if ( !isset($local_avatars) || empty($local_avatars) || !isset($local_avatars['full']) )
64
- {
65
- if ( !empty($avatar) ) // if called by filter
66
- return $avatar;
67
-
68
- remove_filter( 'get_avatar', 'get_simple_local_avatar' );
69
- $avatar = get_avatar( $id_or_email, $size, $default );
70
- add_filter( 'get_avatar', 'get_simple_local_avatar', 10, 5 );
71
  return $avatar;
72
- }
73
 
74
- if ( !is_numeric($size) ) // ensure valid size
75
- $size = '96';
76
 
77
- if ( empty($alt) )
78
  $alt = get_the_author_meta( 'display_name', $user_id );
79
 
80
  // generate a new size
81
- if ( empty( $local_avatars[$size] ) )
82
- {
83
  $upload_path = wp_upload_dir();
84
  $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
85
  $image_sized = image_resize( $avatar_full_path, $size, $size, true );
86
 
87
- if ( is_wp_error($image_sized) ) // deal with original being >= to original image (or lack of sizing ability)
88
- $local_avatars[$size] = $local_avatars['full'];
89
- else
90
- $local_avatars[$size] = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );
91
 
92
  update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
93
- }
94
- elseif ( substr( $local_avatars[$size], 0, 4 ) != 'http' )
95
  $local_avatars[$size] = site_url( $local_avatars[$size] );
 
96
 
97
  $author_class = is_author( $user_id ) ? ' current-author' : '' ;
98
  $avatar = "<img alt='" . esc_attr($alt) . "' src='" . $local_avatars[$size] . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";
99
 
100
- return $avatar;
101
  }
102
 
103
- function admin_init()
104
- {
105
  load_plugin_textdomain( 'simple-local-avatars', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
106
 
107
  register_setting( 'discussion', 'simple_local_avatars_caps', array( $this, 'sanitize_options' ) );
108
  add_settings_field( 'simple-local-avatars-caps', __('Local Avatar Permissions','simple-local-avatars'), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars' );
109
  }
110
 
111
- function sanitize_options( $input )
112
- {
113
- $new_input['simple_local_avatars_caps'] = empty($input['simple_local_avatars_caps']) ? 0 : 1;
114
  return $new_input;
115
  }
116
 
117
- function avatar_settings_field( $args )
118
- {
119
  $options = get_option('simple_local_avatars_caps');
120
 
121
  echo '
@@ -126,8 +109,7 @@ class simple_local_avatars
126
  ';
127
  }
128
 
129
- function edit_user_profile( $profileuser )
130
- {
131
  ?>
132
  <h3><?php _e( 'Avatar','simple-local-avatars' ); ?></h3>
133
 
@@ -141,8 +123,7 @@ class simple_local_avatars
141
  <?php
142
  $options = get_option('simple_local_avatars_caps');
143
 
144
- if ( empty($options['simple_local_avatars_caps']) || current_user_can('upload_files') )
145
- {
146
  do_action( 'simple_local_avatar_notices' );
147
  wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
148
  ?>
@@ -155,37 +136,25 @@ class simple_local_avatars
155
  <input type="checkbox" name="simple-local-avatar-erase" value="1" /> ' . __('Delete local avatar','simple-local-avatars') . '<br />
156
  <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.','simple-local-avatars') . '</span>
157
  ';
158
- }
159
- else
160
- {
161
  if ( empty( $profileuser->simple_local_avatar ) )
162
  echo '<span class="description">' . __('No local avatar is set. Set up your avatar at Gravatar.com.','simple-local-avatars') . '</span>';
163
  else
164
- echo '
165
- <span class="description">' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.','simple-local-avatars') . '</span>
166
- ';
167
  }
168
  ?>
169
  </td>
170
  </tr>
171
  </table>
172
-
173
- <script type="text/javascript">
174
- var form = document.getElementById('your-profile');
175
- form.encoding = 'multipart/form-data';
176
- form.setAttribute('enctype', 'multipart/form-data');
177
- </script>
178
  <?php
179
-
180
  }
181
 
182
- function edit_user_profile_update( $user_id )
183
- {
184
- if ( !wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) ) //security
185
  return;
186
 
187
- if ( !empty( $_FILES['simple-local-avatar']['name'] ) )
188
- {
189
  $mimes = array(
190
  'jpg|jpeg|jpe' => 'image/jpeg',
191
  'gif' => 'image/gif',
@@ -194,12 +163,16 @@ class simple_local_avatars
194
  'tif|tiff' => 'image/tiff'
195
  );
196
 
197
- $avatar = wp_handle_upload( $_FILES['simple-local-avatar'], array( 'mimes' => $mimes, 'test_form' => false ) );
 
 
 
 
 
 
198
 
199
- if ( empty($avatar['file']) ) // handle failures
200
- {
201
- switch ( $avatar['error'] )
202
- {
203
  case 'File type does not meet security guidelines. Try another.' :
204
  add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error",__("Please upload a valid image file for the avatar.","simple-local-avatars"));') );
205
  break;
@@ -210,19 +183,16 @@ class simple_local_avatars
210
  return;
211
  }
212
 
213
- $this->avatar_delete( $user_id ); // delete old images if successful
214
-
215
  update_user_meta( $user_id, 'simple_local_avatar', array( 'full' => $avatar['url'] ) ); // save user information (overwriting old)
216
- }
217
- elseif ( isset($_POST['simple-local-avatar-erase']) && $_POST['simple-local-avatar-erase'] == 1 )
218
  $this->avatar_delete( $user_id );
 
219
  }
220
 
221
  /**
222
  * remove the custom get_avatar hook for the default avatar list output on options-discussion.php
223
  */
224
- function avatar_defaults( $avatar_defaults )
225
- {
226
  remove_action( 'get_avatar', array( $this, 'get_avatar' ) );
227
  return $avatar_defaults;
228
  }
@@ -230,28 +200,37 @@ class simple_local_avatars
230
  /**
231
  * delete avatars based on user_id
232
  */
233
- function avatar_delete( $user_id )
234
- {
235
  $old_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
236
  $upload_path = wp_upload_dir();
237
 
238
- if ( is_array($old_avatars) )
239
- {
240
- foreach ($old_avatars as $old_avatar )
241
- {
242
  $old_avatar_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $old_avatar );
243
  @unlink( $old_avatar_path );
244
- }
245
  }
246
 
247
  delete_user_meta( $user_id, 'simple_local_avatar' );
248
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  }
250
 
251
  $simple_local_avatars = new simple_local_avatars;
252
 
253
- if ( !function_exists('get_simple_local_avatar') ) :
254
-
255
  /**
256
  * more efficient to call simple local avatar directly in theme and avoid gravatar setup
257
  *
@@ -261,22 +240,23 @@ if ( !function_exists('get_simple_local_avatar') ) :
261
  * @param string $alt Alternate text to use in image tag. Defaults to blank
262
  * @return string <img> tag for the user's avatar
263
  */
264
- function get_simple_local_avatar( $id_or_email, $size = '96', $default = '', $alt = false )
265
- {
266
  global $simple_local_avatars;
267
- return $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );
 
 
 
 
 
268
  }
269
 
270
- endif;
271
-
272
  /**
273
  * on uninstallation, remove the custom field from the users and delete the local avatars
274
  */
275
 
276
  register_uninstall_hook( __FILE__, 'simple_local_avatars_uninstall' );
277
 
278
- function simple_local_avatars_uninstall()
279
- {
280
  $simple_local_avatars = new simple_local_avatars;
281
  $users = get_users_of_blog();
282
 
1
  <?php
2
  /**
3
  Plugin Name: Simple Local Avatars
4
+ Plugin URI: http://get10up.com/plugins/simple-local-avatars-wordpress/
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.3
7
+ Author: Jake Goldman (10up LLC), Oomph Inc
8
+ Author URI: http://get10up.com
9
 
10
+ Plugin: Copyright 2011 10up LLC (email : jake@get10up.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
30
 
31
  class simple_local_avatars
32
  {
33
+ function simple_local_avatars() {
 
34
  add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 );
35
 
36
  add_action( 'admin_init', array( $this, 'admin_init' ) );
44
  add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );
45
  }
46
 
47
+ function get_avatar( $avatar = '', $id_or_email, $size = '96', $default = '', $alt = false ) {
48
+
49
+ if ( is_numeric($id_or_email) )
50
  $user_id = (int) $id_or_email;
51
+ elseif ( is_string( $id_or_email ) && ( $user = get_user_by_email( $id_or_email ) ) )
52
+ $user_id = $user->ID;
53
+ elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) )
 
 
 
54
  $user_id = (int) $id_or_email->user_id;
55
 
56
+ if ( empty( $user_id ) )
57
+ return $avatar;
58
+
59
+ $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
60
 
61
+ if ( empty( $local_avatars ) || empty( $local_avatars['full'] ) )
 
 
 
 
 
 
 
62
  return $avatar;
 
63
 
64
+ $size = (int) $size;
 
65
 
66
+ if ( empty( $alt ) )
67
  $alt = get_the_author_meta( 'display_name', $user_id );
68
 
69
  // generate a new size
70
+ if ( empty( $local_avatars[$size] ) ) {
 
71
  $upload_path = wp_upload_dir();
72
  $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
73
  $image_sized = image_resize( $avatar_full_path, $size, $size, true );
74
 
75
+ // deal with original being >= to original image (or lack of sizing ability)
76
+ $local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars['full'] : str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );
 
 
77
 
78
  update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
79
+ } elseif ( substr( $local_avatars[$size], 0, 4 ) != 'http' ) {
 
80
  $local_avatars[$size] = site_url( $local_avatars[$size] );
81
+ }
82
 
83
  $author_class = is_author( $user_id ) ? ' current-author' : '' ;
84
  $avatar = "<img alt='" . esc_attr($alt) . "' src='" . $local_avatars[$size] . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";
85
 
86
+ return apply_filters( 'simple_local_avatar', $avatar );
87
  }
88
 
89
+ function admin_init() {
 
90
  load_plugin_textdomain( 'simple-local-avatars', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
91
 
92
  register_setting( 'discussion', 'simple_local_avatars_caps', array( $this, 'sanitize_options' ) );
93
  add_settings_field( 'simple-local-avatars-caps', __('Local Avatar Permissions','simple-local-avatars'), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars' );
94
  }
95
 
96
+ function sanitize_options( $input ) {
97
+ $new_input['simple_local_avatars_caps'] = empty( $input['simple_local_avatars_caps'] ) ? 0 : 1;
 
98
  return $new_input;
99
  }
100
 
101
+ function avatar_settings_field( $args ) {
 
102
  $options = get_option('simple_local_avatars_caps');
103
 
104
  echo '
109
  ';
110
  }
111
 
112
+ function edit_user_profile( $profileuser ) {
 
113
  ?>
114
  <h3><?php _e( 'Avatar','simple-local-avatars' ); ?></h3>
115
 
123
  <?php
124
  $options = get_option('simple_local_avatars_caps');
125
 
126
+ if ( empty($options['simple_local_avatars_caps']) || current_user_can('upload_files') ) {
 
127
  do_action( 'simple_local_avatar_notices' );
128
  wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
129
  ?>
136
  <input type="checkbox" name="simple-local-avatar-erase" value="1" /> ' . __('Delete local avatar','simple-local-avatars') . '<br />
137
  <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.','simple-local-avatars') . '</span>
138
  ';
139
+ } else {
 
 
140
  if ( empty( $profileuser->simple_local_avatar ) )
141
  echo '<span class="description">' . __('No local avatar is set. Set up your avatar at Gravatar.com.','simple-local-avatars') . '</span>';
142
  else
143
+ echo '<span class="description">' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.','simple-local-avatars') . '</span>';
 
 
144
  }
145
  ?>
146
  </td>
147
  </tr>
148
  </table>
149
+ <script type="text/javascript">var form = document.getElementById('your-profile');form.encoding = 'multipart/form-data';form.setAttribute('enctype', 'multipart/form-data');</script>
 
 
 
 
 
150
  <?php
 
151
  }
152
 
153
+ function edit_user_profile_update( $user_id ) {
154
+ if ( ! isset( $_POST['_simple_local_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) ) //security
 
155
  return;
156
 
157
+ if ( ! empty( $_FILES['simple-local-avatar']['name'] ) ) {
 
158
  $mimes = array(
159
  'jpg|jpeg|jpe' => 'image/jpeg',
160
  'gif' => 'image/gif',
163
  'tif|tiff' => 'image/tiff'
164
  );
165
 
166
+ // front end (theme my profile etc) support
167
+ if ( ! function_exists( 'wp_handle_upload' ) )
168
+ require_once( ABSPATH . 'wp-admin/includes/file.php' );
169
+
170
+ $this->avatar_delete( $user_id ); // delete old images if successful
171
+
172
+ $avatar = wp_handle_upload( $_FILES['simple-local-avatar'], array( 'mimes' => $mimes, 'test_form' => false, 'unique_filename_callback' => array( $this, 'unique_filename_callback' ) ) );
173
 
174
+ if ( empty($avatar['file']) ) { // handle failures
175
+ switch ( $avatar['error'] ) {
 
 
176
  case 'File type does not meet security guidelines. Try another.' :
177
  add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error",__("Please upload a valid image file for the avatar.","simple-local-avatars"));') );
178
  break;
183
  return;
184
  }
185
 
 
 
186
  update_user_meta( $user_id, 'simple_local_avatar', array( 'full' => $avatar['url'] ) ); // save user information (overwriting old)
187
+ } elseif ( ! empty( $_POST['simple-local-avatar-erase'] ) ) {
 
188
  $this->avatar_delete( $user_id );
189
+ }
190
  }
191
 
192
  /**
193
  * remove the custom get_avatar hook for the default avatar list output on options-discussion.php
194
  */
195
+ function avatar_defaults( $avatar_defaults ) {
 
196
  remove_action( 'get_avatar', array( $this, 'get_avatar' ) );
197
  return $avatar_defaults;
198
  }
200
  /**
201
  * delete avatars based on user_id
202
  */
203
+ function avatar_delete( $user_id ) {
 
204
  $old_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
205
  $upload_path = wp_upload_dir();
206
 
207
+ if ( is_array($old_avatars) ) {
208
+ foreach ($old_avatars as $old_avatar ) {
 
 
209
  $old_avatar_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $old_avatar );
210
  @unlink( $old_avatar_path );
211
+ }
212
  }
213
 
214
  delete_user_meta( $user_id, 'simple_local_avatar' );
215
  }
216
+
217
+ function unique_filename_callback( $dir, $name, $ext ) {
218
+ $user = wp_get_current_user();
219
+ $name = sanitize_file_name( $user->display_name . '_avatar' );
220
+
221
+ $number = 1;
222
+
223
+ while ( file_exists( $dir . "/$name$ext" ) ) {
224
+ $name = $name . '_' . $number;
225
+ $number++;
226
+ }
227
+
228
+ return $name . $ext;
229
+ }
230
  }
231
 
232
  $simple_local_avatars = new simple_local_avatars;
233
 
 
 
234
  /**
235
  * more efficient to call simple local avatar directly in theme and avoid gravatar setup
236
  *
240
  * @param string $alt Alternate text to use in image tag. Defaults to blank
241
  * @return string <img> tag for the user's avatar
242
  */
243
+ function get_simple_local_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
 
244
  global $simple_local_avatars;
245
+ $avatar = $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );
246
+
247
+ if ( empty ( $avatar ) )
248
+ $avatar = get_avatar( $id_or_email, $size, $default, $alt );
249
+
250
+ return $avatar;
251
  }
252
 
 
 
253
  /**
254
  * on uninstallation, remove the custom field from the users and delete the local avatars
255
  */
256
 
257
  register_uninstall_hook( __FILE__, 'simple_local_avatars_uninstall' );
258
 
259
+ function simple_local_avatars_uninstall() {
 
260
  $simple_local_avatars = new simple_local_avatars;
261
  $users = get_users_of_blog();
262