User Profile Picture - Version 1.0.0

Version Description

  • Initial release.

=

Download this release

Release Info

Developer ronalfy
Plugin Icon 128x128 User Profile Picture
Version 1.0.0
Comparing to
See all releases

Version 1.0.0

Files changed (5) hide show
  1. js/mpp.js +17 -0
  2. metronet-profile-picture.php +434 -0
  3. readme.txt +65 -0
  4. screenshot-1.jpg +0 -0
  5. screenshot-2.jpg +0 -0
js/mpp.js ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery( document ).ready( function() {
2
+ window.send_to_editor = function( html ) {
3
+ alert( "Please set as a featured image to set a profile picture" );
4
+ tb_remove();
5
+ };
6
+ } );
7
+ function WPSetThumbnailID( id ) {
8
+ tb_remove();
9
+ var user_id = jQuery( "#metronet_profile_id" ).val();
10
+ var post_id = jQuery( "#metronet_post_id" ).val();
11
+ jQuery.post( ajaxurl, { action: "metronet_add_thumbnail", thumbnail_id: id, post_id: post_id, user_id: user_id }, function( response ) {
12
+ jQuery( "#metronet-profile-image" ).html( response );
13
+ tb_remove();
14
+ } );
15
+ };
16
+ function WPSetThumbnailHTML( html ) {
17
+ };
metronet-profile-picture.php ADDED
@@ -0,0 +1,434 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Metronet Profile Picture
4
+ Plugin URI: http://wordpress.org/extend/plugins/metronet-profile-picture/
5
+ Description: Use the native WP uploader on your user profile page.
6
+ Author: Metronet
7
+ Version: 1.0.0
8
+ Requires at least: 3.3
9
+ Author URI: http://www.metronet.no
10
+ Contributors: ronalfy, metronet
11
+ */
12
+
13
+ class Metronet_Profile_Picture {
14
+
15
+ //private
16
+ private $admin_options = array();
17
+ private $errors = '';
18
+ private $plugin_url = '';
19
+ private $plugin_dir = '';
20
+ private $plugin_path = '';
21
+ private $plugin_slug = 'metronet_profile_picture';
22
+
23
+ /**
24
+ * __construct()
25
+ *
26
+ * Class constructor
27
+ *
28
+ */
29
+ function __construct(){
30
+
31
+ $this->admin_options = $this->get_admin_options();
32
+ $this->plugin_path = plugin_basename( __FILE__ );
33
+ $this->plugin_url = rtrim( plugin_dir_url(__FILE__), '/' );
34
+ $this->plugin_dir = rtrim( plugin_dir_path(__FILE__), '/' );
35
+
36
+ add_action( 'init', array( &$this, 'init' ) );
37
+ add_action( 'personal_options', array( &$this, 'insert_upload_form' ) );
38
+ add_action( 'admin_menu', array( &$this, 'admin_menus' ) );
39
+
40
+ //Scripts
41
+ add_action( 'admin_print_scripts-user-edit.php', array( &$this, 'print_media_scripts' ) );
42
+ add_action( 'admin_print_scripts-profile.php', array( &$this, 'print_media_scripts' ) );
43
+
44
+ //Styles
45
+ add_action( 'admin_print_styles-user-edit.php', array( &$this, 'print_media_styles' ) );
46
+ add_action( 'admin_print_styles-profile.php', array( &$this, 'print_media_styles' ) );
47
+
48
+ //Ajax
49
+ add_action( 'wp_ajax_metronet_add_thumbnail', array( &$this, 'ajax_add_thumbnail' ) );
50
+
51
+ //User update action
52
+ add_action( 'edit_user_profile_update', array( &$this, 'save_user_profile' ) );
53
+ add_action( 'personal_options_update', array( &$this, 'save_user_profile' ) );
54
+
55
+ //User Avatar override
56
+ add_filter( 'get_avatar', array( &$this, 'avatar_override' ), 10, 5 );
57
+ } //end constructor
58
+
59
+ /**
60
+ * admin_menus
61
+ * Helper method for initializes all admin menus and registering settings
62
+ */
63
+ public function admin_menus() {
64
+
65
+ } //end admin_menus
66
+
67
+ /**
68
+ * ajax_add_thumbnail()
69
+ *
70
+ * Adds a thumbnail to user meta and returns thumbnail html
71
+ *
72
+ */
73
+ public function ajax_add_thumbnail() {
74
+ $post_id = isset( $_POST[ 'post_id' ] ) ? absint( $_POST[ 'post_id' ] ) : 0;
75
+ $user_id = isset( $_POST[ 'user_id' ] ) ? absint( $_POST[ 'user_id' ] ) : 0;
76
+ $thumbnail_id = isset( $_POST[ 'thumbnail_id' ] ) ? absint( $_POST[ 'thumbnail_id' ] ) : 0;
77
+ if ( $post_id == 0 || $user_id == 0 || $thumbnail_id == 0 ) die( '' );
78
+
79
+ //Save user meta
80
+ update_user_meta( $user_id, 'metronet_post_id', $post_id );
81
+
82
+ //Form upload link
83
+ $upload_url = admin_url( 'media-upload.php' );
84
+ $query_args = array(
85
+ 'user_id' => $user_id,
86
+ 'post_id' => $post_id,
87
+ 'tab' => 'gallery',
88
+ 'TB_iframe' => "1",
89
+ 'width' => "640",
90
+ 'height' => "425"
91
+ );
92
+ $upload_url = esc_url( add_query_arg( $query_args, $upload_url ) );
93
+ if ( has_post_thumbnail( $post_id ) ) {
94
+ $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
95
+ $return_html = sprintf( "<a href='%s' class='thickbox add_media'>%s</a>", $upload_url, $post_thumbnail );
96
+ die( $return_html );
97
+ }
98
+ die( '' );
99
+ } //end ajax_add_thumbnail
100
+
101
+ /**
102
+ * avatar_override()
103
+ *
104
+ * Overrides an avatar with a profile image
105
+ *
106
+ * @param string $avatar SRC to the avatar
107
+ * @param mixed $id_or_email
108
+ * @param int $size Size of the image
109
+ * @param string $default URL to the default image
110
+ * @param string $alt Alternative text
111
+ **/
112
+ public function avatar_override( $avatar, $id_or_email, $size, $default, $alt ) {
113
+ //Get user data
114
+ if ( is_numeric( $id_or_email ) ) {
115
+ $user = get_user_by( 'id', ( int )$id_or_email );
116
+ } else {
117
+ $user = get_user_by( 'email', $id_or_email );
118
+ }
119
+ if ( !$user ) return $avatar;
120
+ $user_id = $user->ID;
121
+
122
+ //Determine if user has an avatar override
123
+ $avatar_override = get_user_meta( $user_id, 'metronet_avatar_override', true );
124
+ if ( !$avatar_override || $avatar_override != 'on' ) return $avatar;
125
+
126
+ $avatar = mt_profile_img( $user_id, array(
127
+ 'size' => array( $size, $size ),
128
+ 'attr' => array( 'alt' => $alt ),
129
+ 'echo' => false )
130
+ );
131
+ return $avatar;
132
+ } //end avatar_override
133
+
134
+ /**
135
+ * get_admin_option()
136
+ *
137
+ * Returns a localized admin option
138
+ *
139
+ * @param string $key Admin Option Key to Retrieve
140
+ * @return mixed The results of the admin key. False if not present.
141
+ */
142
+ public function get_admin_option( $key = '' ) {
143
+ $admin_options = $this->get_admin_options();
144
+ if ( array_key_exists( $key, $admin_options ) ) {
145
+ return $admin_options[ $key ];
146
+ }
147
+ return false;
148
+ }
149
+
150
+ /**
151
+ * get_admin_options()
152
+ *
153
+ * Initialize and return an array of all admin options
154
+ *
155
+ * @return array All Admin Options
156
+ */
157
+ public function get_admin_options( ) {
158
+
159
+ if (empty($this->admin_options)) {
160
+ $admin_options = $this->get_plugin_defaults();
161
+
162
+ $options = get_option( $this->plugin_slug );
163
+ if (!empty($options)) {
164
+ foreach ($options as $key => $option) {
165
+ if (array_key_exists($key, $admin_options)) {
166
+ $admin_options[$key] = $option;
167
+ }
168
+ }
169
+ }
170
+
171
+ //Save the options
172
+ $this->admin_options = $admin_options;
173
+ $this->save_admin_options();
174
+ }
175
+ return $this->admin_options;
176
+ } //end get_admin_options
177
+
178
+ /**
179
+ * get_all_admin_options()
180
+ *
181
+ * Returns an array of all admin options
182
+ *
183
+ * @return array All Admin Options
184
+ */
185
+ public function get_all_admin_options() {
186
+ return $this->admin_options;
187
+ }
188
+
189
+ /**
190
+ * get_plugin_defaults()
191
+ *
192
+ * Returns an array of default plugin options (to be stored in the options table)
193
+ *
194
+ * @return array Default plugin keys
195
+ */
196
+ public function get_plugin_defaults() {
197
+ if ( isset( $this->defaults ) ) {
198
+ return $this->defaults;
199
+ } else {
200
+ $this->defaults = array(
201
+ );
202
+ return $this->defaults;
203
+ }
204
+ } //end get_plugin_defaults
205
+
206
+ /**
207
+ * get_plugin_dir()
208
+ *
209
+ * Returns an absolute path to a plugin item
210
+ *
211
+ * @param string $path Relative path to make absolute (e.g., /css/image.png)
212
+ * @return string An absolute path (e.g., /htdocs/ithemes/wp-content/.../css/image.png)
213
+ */
214
+ public function get_plugin_dir( $path = '' ) {
215
+ $dir = $this->plugin_dir;
216
+ if ( !empty( $path ) && is_string( $path) )
217
+ $dir .= '/' . ltrim( $path, '/' );
218
+ return $dir;
219
+ } //end get_plugin_dir
220
+
221
+
222
+ /**
223
+ * get_plugin_url()
224
+ *
225
+ * Returns an absolute url to a plugin item
226
+ *
227
+ * @param string $path Relative path to plugin (e.g., /css/image.png)
228
+ * @return string An absolute url (e.g., http://www.domain.com/plugin_url/.../css/image.png)
229
+ */
230
+ public function get_plugin_url( $path = '' ) {
231
+ $dir = $this->plugin_url;
232
+ if ( !empty( $path ) && is_string( $path) )
233
+ $dir .= '/' . ltrim( $path, '/' );
234
+ return $dir;
235
+ } //get_plugin_url
236
+
237
+ /**
238
+ * init()
239
+ *
240
+ * Initializes plugin localization, post types, updaters, plugin info, and adds actions/filters
241
+ *
242
+ */
243
+ function init() {
244
+
245
+ //* Localization Code */
246
+ load_plugin_textdomain( 'metronet_profile_picture', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
247
+
248
+ //Register post types
249
+ $post_type_args = array(
250
+ 'public' => false,
251
+ 'publicly_queryable' => true,
252
+ 'show_ui' => false,
253
+ 'show_in_menu' => false,
254
+ 'query_var' => true,
255
+ 'rewrite' => false,
256
+ 'has_archive' => false,
257
+ 'hierarchical' => false,
258
+ 'supports' => array( 'thumbnail' )
259
+ );
260
+ register_post_type( 'mt_pp', $post_type_args );
261
+
262
+ }//end function init
263
+
264
+ /**
265
+ * insert_upload_form
266
+ *
267
+ * Adds an upload form to the user profile page and outputs profile image if there is one
268
+ */
269
+ public function insert_upload_form() {
270
+ //Get user ID
271
+ $user_id = isset( $_GET[ 'user_id' ] ) ? absint( $_GET[ 'user_id' ] ) : 0;
272
+ if ( $user_id == 0 && IS_PROFILE_PAGE ) {
273
+ $current_user = wp_get_current_user();
274
+ $user_id = $current_user->ID;
275
+ }
276
+
277
+ //Get/Create Profile Picture Post
278
+ $post_args = array(
279
+ 'post_type' => 'mt_pp',
280
+ 'author' => $user_id,
281
+ 'post_status' => 'publish'
282
+ );
283
+ $posts = get_posts( $post_args );
284
+ if ( !$posts ) {
285
+ $post_id = wp_insert_post( array(
286
+ 'post_author' => $user_id,
287
+ 'post_type' => 'mt_pp',
288
+ 'post_status' => 'publish',
289
+ ) );
290
+ } else {
291
+ $post = end( $posts );
292
+ $post_id = $post->ID;
293
+ }
294
+
295
+ //Form upload link
296
+ $upload_url = admin_url( 'media-upload.php' );
297
+ $query_args = array(
298
+ 'user_id' => absint( $user_id ),
299
+ 'post_id' => $post_id,
300
+ 'tab' => 'gallery',
301
+ 'TB_iframe' => "1",
302
+ 'width' => "640",
303
+ 'height' => "425"
304
+ );
305
+ $upload_url = esc_url( add_query_arg( $query_args, $upload_url ) );
306
+
307
+ //Create upload link
308
+ $upload_link = sprintf( "<a href='%s' class='thickbox add_media'>%s</a>", $upload_url, esc_html__( "Upload or Change Profile Picture", 'metronet_profile_picture' ) );
309
+ ?>
310
+ <tr valign="top">
311
+ <th scope="row"><?php esc_html_e( "Profile Image", "metronet_profile_picture" ); ?></th>
312
+ <td>
313
+ <input type="hidden" name="metronet_profile_id" id="metronet_profile_id" value="<?php echo esc_attr( $user_id ); ?>" />
314
+ <input type="hidden" name="metronet_post_id" id="metronet_post_id" value="<?php echo esc_attr( $post_id ); ?>" />
315
+ <div id="metronet-profile-image">
316
+ <?php
317
+ if ( has_post_thumbnail( $post_id ) ) {
318
+ $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
319
+ printf( "<a href='%s' class='thickbox add_media'>%s</a>", $upload_url, $post_thumbnail );
320
+ }
321
+ ?>
322
+ </div><!-- #metronet-profile-image -->
323
+ <div id="metronet-upload-link"><?php echo $upload_link; ?></div><!-- #metronet-upload-link -->
324
+ <div id="metronet-override-avatar">
325
+ <input type="checkbox" name="metronet-user-avatar" id="metronet-user-avatar" value="on" <?php checked( "on", get_user_meta( $user_id, 'metronet_avatar_override', true ) ); ?> /><label for="metronet-user-avatar"> <?php esc_html_e( "Override Avatar?", "metronet_profile_picture" ); ?></label>
326
+ </div><!-- #metronet-override-avatar -->
327
+ </td>
328
+ </tr>
329
+ <?php
330
+ } //end insert_upload_form
331
+
332
+ /**
333
+ * print_media_scripts
334
+ *
335
+ * Output media scripts for thickbox and media uploader
336
+ **/
337
+ public function print_media_scripts() {
338
+ wp_enqueue_script( 'mt-pp', $this->get_plugin_url( '/js/mpp.js' ), array( 'media-upload', 'thickbox' ) );
339
+ } //end print_media_scripts
340
+
341
+ public function print_media_styles() {
342
+ wp_enqueue_style( 'thickbox' );
343
+ } //end print_media_styles
344
+
345
+ /**
346
+ * save_admin_option()
347
+ *
348
+ * Saves an individual option to the options array
349
+ * @param string $key Option key to save
350
+ * @param mixed $value Value to save in the option
351
+ */
352
+ public function save_admin_option( $key = '', $value = '' ) {
353
+ $this->admin_options[ $key ] = $value;
354
+ $this->save_admin_options();
355
+ return $value;
356
+ } //end save_admin_option
357
+
358
+ /**
359
+ * save_admin_options()
360
+ *
361
+ * Saves a group of admin options to the options table
362
+ * @param array $admin_options Optional array of options to save (are merged with existing options)
363
+ */
364
+ public function save_admin_options( $admin_options = false ){
365
+ if (!empty($this->admin_options)) {
366
+ if ( is_array( $admin_options ) ) {
367
+ $this->admin_options = wp_parse_args( $admin_options, $this->admin_options );
368
+ }
369
+ update_option( $this->plugin_slug, $this->admin_options);
370
+ }
371
+ } //end save_admin_options
372
+
373
+ /**
374
+ * save_user_profile()
375
+ *
376
+ * Saves user profile fields
377
+ * @param int $user_id
378
+ **/
379
+ public function save_user_profile( $user_id ) {
380
+ if ( !isset( $_POST[ 'metronet-user-avatar' ] ) ) return;
381
+ check_admin_referer( 'update-user_' . $user_id );
382
+
383
+ $user_avatar = $_POST[ 'metronet-user-avatar' ];
384
+ if ( $user_avatar == 'on' ) {
385
+ update_user_meta( $user_id, 'metronet_avatar_override', 'on' );
386
+ } else {
387
+ delete_user_meta( $user_id, 'metronet_avatar_override' );
388
+ }
389
+ } //end save_user_profile
390
+
391
+ } //end class
392
+ //instantiate the class
393
+ global $mt_pp;
394
+ if (class_exists('Metronet_Profile_Picture')) {
395
+ if (get_bloginfo('version') >= "3.0") {
396
+ add_action( 'plugins_loaded', 'mt_mpp_instantiate' );
397
+ }
398
+ }
399
+ function mt_mpp_instantiate() {
400
+ global $mt_pp;
401
+ $mt_pp = new Metronet_Profile_Picture();
402
+ }
403
+ /**
404
+ * mt_profile_img
405
+ *
406
+ * Adds a profile image
407
+ *
408
+ @param $user_id INT - The user ID for the user to retrieve the image for
409
+ @ param $args mixed
410
+ size - string || array (see get_the_post_thumbnail)
411
+ attr - string || array (see get_the_post_thumbnail)
412
+ echo - bool (true or false) - whether to echo the image or return it
413
+ */
414
+
415
+
416
+
417
+ function mt_profile_img( $user_id, $args = array() ) {
418
+ $profile_post_id = absint( get_user_meta( $user_id, 'metronet_post_id', true ) );
419
+
420
+ $defaults = array(
421
+ 'size' => 'thumbnail',
422
+ 'attr' => '',
423
+ 'echo' => true
424
+ );
425
+ $args = wp_parse_args( $args, $defaults );
426
+ extract( $args );
427
+ $post_thumbnail = get_the_post_thumbnail( $profile_post_id, $size, $attr );
428
+ if ( $echo ) {
429
+ echo $post_thumbnail;
430
+ } else {
431
+ return $post_thumbnail;
432
+ }
433
+ } //end mt_profile_img
434
+ ?>
readme.txt ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Metronet Profile Picture ===
2
+ Contributors: metronet, ronalfy
3
+ Tags: users, user, user profile
4
+ Requires at least: 3.3
5
+ Tested up to: 3.4
6
+ Stable tag: 1.0.0
7
+ License: GPLv2 or later
8
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ Set a custom profile image for a user using the standard WordPress media upload tool.
11
+ == Description ==
12
+
13
+ Set a custom profile image for a user using the standard WordPress media upload tool. A template tag is supplied for outputting to a theme and the option to override a user's default avatar is also available.
14
+
15
+ == Installation ==
16
+
17
+ 1. Upload `metronet-profile-picture` folder to the `/wp-content/plugins/` directory
18
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
19
+ 3. Place `<?php mt_profile_img() ?>` in your templates (arguments and usage are below)
20
+
21
+ `/**
22
+ * mt_profile_img
23
+ *
24
+ * Adds a profile image
25
+ *
26
+ @param $user_id INT - The user ID for the user to retrieve the image for
27
+ @ param $args mixed
28
+ size - string || array (see get_the_post_thumbnail)
29
+ attr - string || array (see get_the_post_thumbnail)
30
+ echo - bool (true or false) - whether to echo the image or return it
31
+ */
32
+ `
33
+
34
+ == Frequently Asked Questions ==
35
+
36
+ = How do you set a user profile image? =
37
+
38
+ 1. Visit the profile page you would like to edit.
39
+ 2. Click "Upload or Change Profile Picture"
40
+ 3. Upload a new image and select "Use as featured image", which will save the image (ignore the "Insert Into Post" button).
41
+
42
+ To override an avatar, select the "Override Avatar?" checkbox and save the profile page.
43
+
44
+ = How do I create specific thumbnail sizes? =
45
+
46
+ Since the plugin uses the native uploader, you'll have to make use of <a href='http://codex.wordpress.org/Function_Reference/add_image_size'>add_image_size</a> in your theme. You can then call `mt_profile_img` and pass in the custom image size.
47
+
48
+ = The image is cropped wrong. How do I fix this? =
49
+
50
+ We highly recommend the <a href='http://wordpress.org/extend/plugins/post-thumbnail-editor/'>Post Thumbnail Editor</a> plugin for cropping thumbnails, as you can custom-crop various image sizes without affecting other images.
51
+
52
+ == Screenshots ==
53
+
54
+ 1. Profile page options.
55
+ 2. Media upload dialog
56
+
57
+ == Changelog ==
58
+
59
+ = 1.0.0 =
60
+ * Initial release.
61
+
62
+ == Upgrade Notice ==
63
+
64
+ = 1.0.0 =
65
+ Initial release.
screenshot-1.jpg ADDED
Binary file
screenshot-2.jpg ADDED
Binary file