User Profile Picture - Version 1.0.15

Version Description

  • Built-in support for Post Thumbnail Editor
  • Better integration with the new WP 3.5 media uploader
  • Various bug fixes.
Download this release

Release Info

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

Code changes from version 1.0.10 to 1.0.15

Files changed (3) hide show
  1. js/mpp.js +45 -8
  2. metronet-profile-picture.php +89 -11
  3. readme.txt +38 -13
js/mpp.js CHANGED
@@ -1,10 +1,29 @@
1
  jQuery( document ).ready( function( $ ) {
2
- $('#metronet-upload-link a, #metronet-profile-image a').on( "click", function(e) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  //Assign the default view for the media uploader
4
  var uploader = wp.media({
5
  state: 'featured-image',
6
- states: [ new wp.media.controller.FeaturedImage() ]
7
  });
 
 
8
  //Create featured image button
9
  uploader.on( 'toolbar:create:featured-image', function( toolbar ) {
10
  this.createSelectToolbar( toolbar, {
@@ -15,11 +34,19 @@ jQuery( document ).ready( function( $ ) {
15
  //For when the featured thumbnail is set
16
  uploader.mt_featured_set = function( id ) {
17
  var settings = wp.media.view.settings;
18
-
19
- settings.post.featuredImageId = id;
20
- $.post( ajaxurl, { action: 'metronet_add_thumbnail', json: false, post_id: settings.post.id, user_id: jQuery( "#metronet_profile_id" ).val(), thumbnail_id: settings.post.featuredImageId,_wpnonce: settings.post.nonce }, function( response ) {
21
- jQuery( "#metronet-profile-image a" ).html( response );
22
- } );
 
 
 
 
 
 
 
 
23
  };
24
 
25
  //For when the featured image is clicked
@@ -30,12 +57,22 @@ jQuery( document ).ready( function( $ ) {
30
  if ( ! settings.post.featuredImageId )
31
  return;
32
 
 
33
  uploader.mt_featured_set( selection ? selection.id : -1 );
34
  } );
35
-
 
 
 
 
36
 
37
  //Open the media uploader
38
  uploader.open();
39
  return false;
40
  });
 
 
 
 
 
41
  } );
1
  jQuery( document ).ready( function( $ ) {
2
+
3
+ //Refresh the profile image thumbnail
4
+ function mt_ajax_thumbnail_refresh() {
5
+ var post_id = jQuery( "#metronet_post_id" ).val();
6
+ $.post( ajaxurl, {
7
+ action: 'metronet_get_thumbnail',
8
+ post_id: post_id,
9
+ },
10
+ function( response ) {
11
+ jQuery( "#metronet-profile-image a" ).html( response.thumb_html );
12
+ jQuery( "#metronet-pte" ).html( response.crop_html );
13
+ },
14
+ 'json'
15
+ );
16
+ //alert( 'yo' );
17
+ };
18
+
19
+ $('#metronet-upload-link a, #metronet-profile-image a.add_media').on( "click", function(e) {
20
  //Assign the default view for the media uploader
21
  var uploader = wp.media({
22
  state: 'featured-image',
23
+ states: [ new wp.media.controller.FeaturedImage() ],
24
  });
25
+ uploader.state('featured-image').set( 'title', metronet_profile_image.set_profile_text );
26
+
27
  //Create featured image button
28
  uploader.on( 'toolbar:create:featured-image', function( toolbar ) {
29
  this.createSelectToolbar( toolbar, {
34
  //For when the featured thumbnail is set
35
  uploader.mt_featured_set = function( id ) {
36
  var settings = wp.media.view.settings;
37
+ $.post( ajaxurl, {
38
+ action: 'metronet_add_thumbnail',
39
+ post_id: settings.post.id,
40
+ user_id: jQuery( "#metronet_profile_id" ).val(),
41
+ thumbnail_id: id,
42
+ _wpnonce: settings.post.nonce
43
+ },
44
+ function( response ) {
45
+ jQuery( "#metronet-profile-image a" ).html( response.thumb_html );
46
+ jQuery( "#metronet-pte" ).html( response.crop_html );
47
+ },
48
+ 'json'
49
+ );
50
  };
51
 
52
  //For when the featured image is clicked
57
  if ( ! settings.post.featuredImageId )
58
  return;
59
 
60
+ settings.post.featuredImageId = selection.id;
61
  uploader.mt_featured_set( selection ? selection.id : -1 );
62
  } );
63
+
64
+ //For when the window is closed (update the thumbnail)
65
+ uploader.on('escape', function(){
66
+ mt_ajax_thumbnail_refresh();
67
+ });
68
 
69
  //Open the media uploader
70
  uploader.open();
71
  return false;
72
  });
73
+
74
+ //For when Thickbox is closed - Trac - http://core.trac.wordpress.org/ticket/19189#comment:24
75
+ $(document).on('tb_unload', '#TB_window', function(e){
76
+ mt_ajax_thumbnail_refresh();
77
+ });
78
  } );
metronet-profile-picture.php CHANGED
@@ -4,7 +4,7 @@ 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.10
8
  Requires at least: 3.5
9
  Author URI: http://www.metronet.no
10
  Contributors: ronalfy, metronet
@@ -39,8 +39,13 @@ class Metronet_Profile_Picture {
39
  add_action( 'admin_print_scripts-user-edit.php', array( &$this, 'print_media_scripts' ) );
40
  add_action( 'admin_print_scripts-profile.php', array( &$this, 'print_media_scripts' ) );
41
 
 
 
 
 
42
  //Ajax
43
  add_action( 'wp_ajax_metronet_add_thumbnail', array( &$this, 'ajax_add_thumbnail' ) );
 
44
 
45
  //User update action
46
  add_action( 'edit_user_profile_update', array( &$this, 'save_user_profile' ) );
@@ -69,11 +74,35 @@ class Metronet_Profile_Picture {
69
 
70
  if ( has_post_thumbnail( $post_id ) ) {
71
  $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
72
- die( $post_thumbnail );
 
 
 
 
73
  }
74
- die( '' );
75
  } //end ajax_add_thumbnail
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  /**
78
  * avatar_override()
79
  *
@@ -181,6 +210,33 @@ class Metronet_Profile_Picture {
181
  return $post_id;
182
  } //end get_post_id
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  /**
185
  * get_user_id
186
  *
@@ -236,6 +292,7 @@ class Metronet_Profile_Picture {
236
  * Adds an upload form to the user profile page and outputs profile image if there is one
237
  */
238
  public function insert_upload_form() {
 
239
 
240
  $user_id = $this->get_user_id();
241
  $post_id = $this->get_post_id( $user_id );
@@ -250,17 +307,23 @@ class Metronet_Profile_Picture {
250
  <input type="hidden" name="metronet_post_id" id="metronet_post_id" value="<?php echo esc_attr( $post_id ); ?>" />
251
  <div id="metronet-profile-image">
252
  <?php
 
253
  if ( has_post_thumbnail( $post_id ) ) {
254
  $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
255
- printf( "<a href='#' class='add_media'>%s</a>", $post_thumbnail );
256
  }
 
257
  ?>
 
258
  </div><!-- #metronet-profile-image -->
259
- <div id="metronet-upload-link"><?php echo $upload_link; ?> - <span class="description"><?php esc_html_e( 'Select "Set profile image" after uploading to choose the profile image', 'metronet_profile_picture' ); ?></span></div><!-- #metronet-upload-link -->
260
  <div id="metronet-override-avatar">
261
- <input type="hidden" name="metronet-user-avatar" value="off" />
262
  <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>
263
  </div><!-- #metronet-override-avatar -->
 
 
 
264
  </td>
265
  </tr>
266
  <?php
@@ -274,10 +337,28 @@ class Metronet_Profile_Picture {
274
  public function print_media_scripts() {
275
  $post_id = $this->get_post_id( $this->get_user_id() );
276
  wp_enqueue_media( array( 'post' => $post_id ) );
277
- wp_enqueue_script( 'mt-pp', $this->get_plugin_url( '/js/mpp.js' ), array( 'media-editor' ) );
278
- wp_localize_script( 'mt-pp', 'metronet_profile_image', array( 'set_profile_text' => __( 'Set profile image', 'metronet_profile_picture' ) ) );
 
 
 
 
 
 
 
 
 
 
 
279
  } //end print_media_scripts
280
 
 
 
 
 
 
 
 
281
  /**
282
  * save_user_profile()
283
  *
@@ -319,9 +400,6 @@ function mt_mpp_instantiate() {
319
  attr - string || array (see get_the_post_thumbnail)
320
  echo - bool (true or false) - whether to echo the image or return it
321
  */
322
-
323
-
324
-
325
  function mt_profile_img( $user_id, $args = array() ) {
326
  $profile_post_id = absint( get_user_meta( $user_id, 'metronet_post_id', true ) );
327
 
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.15
8
  Requires at least: 3.5
9
  Author URI: http://www.metronet.no
10
  Contributors: ronalfy, metronet
39
  add_action( 'admin_print_scripts-user-edit.php', array( &$this, 'print_media_scripts' ) );
40
  add_action( 'admin_print_scripts-profile.php', array( &$this, 'print_media_scripts' ) );
41
 
42
+ //Styles
43
+ add_action( 'admin_print_styles-user-edit.php', array( &$this, 'print_media_styles' ) );
44
+ add_action( 'admin_print_styles-profile.php', array( &$this, 'print_media_styles' ) );
45
+
46
  //Ajax
47
  add_action( 'wp_ajax_metronet_add_thumbnail', array( &$this, 'ajax_add_thumbnail' ) );
48
+ add_action( 'wp_ajax_metronet_get_thumbnail', array( &$this, 'ajax_get_thumbnail' ) );
49
 
50
  //User update action
51
  add_action( 'edit_user_profile_update', array( &$this, 'save_user_profile' ) );
74
 
75
  if ( has_post_thumbnail( $post_id ) ) {
76
  $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
77
+ $crop_html = $this->get_post_thumbnail_editor_link( $post_id );
78
+ die( json_encode( array(
79
+ 'thumb_html' => $post_thumbnail,
80
+ 'crop_html' => $crop_html
81
+ ) ) );
82
  }
83
+ die( json_encode( array( 'thumb_html' => '', 'crop_html' => '' ) ) );
84
  } //end ajax_add_thumbnail
85
 
86
+ /**
87
+ * ajax_get_thumbnail()
88
+ *
89
+ * Retrieves a thumbnail based on a passed post id ($_POST)
90
+ *
91
+ */
92
+ public function ajax_get_thumbnail() {
93
+ $post_id = isset( $_POST[ 'post_id' ] ) ? absint( $_POST[ 'post_id' ] ) : 0;
94
+
95
+ if ( has_post_thumbnail( $post_id ) ) {
96
+ $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
97
+ $crop_html = $this->get_post_thumbnail_editor_link( $post_id );
98
+ die( json_encode( array(
99
+ 'thumb_html' => $post_thumbnail,
100
+ 'crop_html' => $crop_html
101
+ ) ) );
102
+ }
103
+ die( json_encode( array( 'thumb_html' => '', 'crop_html' => '' ) ) );
104
+ } //end ajax_get_thumbnail
105
+
106
  /**
107
  * avatar_override()
108
  *
210
  return $post_id;
211
  } //end get_post_id
212
 
213
+ /**
214
+ * get_post_thumbnail_editor_link
215
+ *
216
+ * Retrieve a crop-image link (HTML) based on the passed post_id
217
+ *
218
+ @param int post_id Post ID to find the featured image for
219
+ @return string html
220
+ */
221
+ private function get_post_thumbnail_editor_link( $post_id ) {
222
+ ob_start();
223
+ if ( has_post_thumbnail( $post_id ) && defined( 'PTE_VERSION' ) ) {
224
+ //Post Thumbnail Editor compatibility - http://wordpress.org/extend/plugins/post-thumbnail-editor/
225
+ $post_thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
226
+ $pte_options = pte_get_options();
227
+ $pte_url = add_query_arg( array(
228
+ 'action' => 'pte_ajax',
229
+ 'pte-action' => 'launch',
230
+ 'id' => $post_thumbnail_id,
231
+ 'TB_iframe' => 'true',
232
+ 'height' => $pte_options[ 'pte_tb_height' ],
233
+ 'width' => $pte_options[ 'pte_tb_width' ]
234
+ ), admin_url('admin-ajax.php') );
235
+ printf( ' - <a class="thickbox" href="%s">%s</a>', $pte_url, __( 'Crop Thumbnail', 'metronet_profile_picture' ) );
236
+ } //end post thumbnail editor
237
+ return ob_get_clean();
238
+ } //end get_post_thumbnail_editor_link
239
+
240
  /**
241
  * get_user_id
242
  *
292
  * Adds an upload form to the user profile page and outputs profile image if there is one
293
  */
294
  public function insert_upload_form() {
295
+ if ( !current_user_can( 'author' ) ) return; //Users must be author or greater
296
 
297
  $user_id = $this->get_user_id();
298
  $post_id = $this->get_post_id( $user_id );
307
  <input type="hidden" name="metronet_post_id" id="metronet_post_id" value="<?php echo esc_attr( $post_id ); ?>" />
308
  <div id="metronet-profile-image">
309
  <?php
310
+ echo '<a href="#" class="add_media">';
311
  if ( has_post_thumbnail( $post_id ) ) {
312
  $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
313
+ echo $post_thumbnail;
314
  }
315
+ echo '</a>';
316
  ?>
317
+ </a>
318
  </div><!-- #metronet-profile-image -->
319
+ <div id="metronet-upload-link"> - <?php echo $upload_link; ?> - <span class="description"><?php esc_html_e( 'Select "Set profile image" after uploading to choose the profile image', 'metronet_profile_picture' ); ?></span></div><!-- #metronet-upload-link -->
320
  <div id="metronet-override-avatar">
321
+ <input type="hidden" name="metronet-user-avatar" value="off" /> -
322
  <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>
323
  </div><!-- #metronet-override-avatar -->
324
+ <div id="metronet-pte">
325
+ <?php echo $this->get_post_thumbnail_editor_link( $post_id ); ?>
326
+ </div><!-- #metronet-pte -->
327
  </td>
328
  </tr>
329
  <?php
337
  public function print_media_scripts() {
338
  $post_id = $this->get_post_id( $this->get_user_id() );
339
  wp_enqueue_media( array( 'post' => $post_id ) );
340
+
341
+ $script_deps = array( 'media-editor' );
342
+ if ( defined( 'PTE_VERSION' ) ) {
343
+ //Post Thumbnail Editor compatibility - http://wordpress.org/extend/plugins/post-thumbnail-editor/
344
+ $script_deps[] = 'thickbox';
345
+ }
346
+ wp_enqueue_script( 'mt-pp', $this->get_plugin_url( '/js/mpp.js' ), $script_deps, '1.0.15', true );
347
+ wp_localize_script( 'mt-pp', 'metronet_profile_image',
348
+ array(
349
+ 'set_profile_text' => __( 'Set profile image', 'metronet_profile_picture' ),
350
+ 'crop' => __( 'Crop Thumbnail', 'metronet_profile_picture' )
351
+ )
352
+ );
353
  } //end print_media_scripts
354
 
355
+ public function print_media_styles() {
356
+ if ( defined( 'PTE_VERSION' ) ) {
357
+ //Post Thumbnail Editor compatibility - http://wordpress.org/extend/plugins/post-thumbnail-editor/
358
+ wp_enqueue_style( 'thickbox' );
359
+ }
360
+ } //end print_media_styles
361
+
362
  /**
363
  * save_user_profile()
364
  *
400
  attr - string || array (see get_the_post_thumbnail)
401
  echo - bool (true or false) - whether to echo the image or return it
402
  */
 
 
 
403
  function mt_profile_img( $user_id, $args = array() ) {
404
  $profile_post_id = absint( get_user_meta( $user_id, 'metronet_post_id', true ) );
405
 
readme.txt CHANGED
@@ -3,20 +3,27 @@ Contributors: metronet, ronalfy
3
  Tags: users, user, user profile
4
  Requires at least: 3.5
5
  Tested up to: 3.5
6
- Stable tag: 1.0.10
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
  Arguments:
22
 
@@ -35,12 +42,23 @@ Arguments:
35
 
36
  Example Usage:
37
  `
38
- $avatar = mt_profile_img( $user_id, array(
39
- 'size' => 'thumbnail',
40
- 'attr' => array( 'alt' => 'Alternative Text' ),
41
- 'echo' => false )
42
- );
 
 
 
 
 
 
43
  `
 
 
 
 
 
44
 
45
  == Frequently Asked Questions ==
46
 
@@ -48,7 +66,7 @@ $avatar = mt_profile_img( $user_id, array(
48
 
49
  1. Visit the profile page you would like to edit.
50
  2. Click "Upload or Change Profile Picture"
51
- 3. Upload a new image and select "Set profile image", which will save the image (ignore the "Insert Into Post" button).
52
 
53
  To override an avatar, select the "Override Avatar?" checkbox and save the profile page.
54
 
@@ -67,10 +85,16 @@ We highly recommend the <a href='http://wordpress.org/extend/plugins/post-thumbn
67
  == Screenshots ==
68
 
69
  1. Profile page options.
70
- 2. Media upload dialog
 
71
 
72
  == Changelog ==
73
 
 
 
 
 
 
74
  = 1.0.10 =
75
  * Usability enhancements.
76
  * Stripping out useless code.
@@ -94,13 +118,14 @@ We highly recommend the <a href='http://wordpress.org/extend/plugins/post-thumbn
94
 
95
  == Upgrade Notice ==
96
 
 
 
 
97
  = 1.0.10 =
98
- * 3.5 media uploader support
99
- * Usability enhancements
100
- * Code cleanup.
101
 
102
  = 1.0.9 =
103
- * 3.5 media uploader support.
104
 
105
  = 1.0.1 =
106
  Several important bug fixes including the ability to uncheck the avatar override, and the behavior when someone deletes their profile picture.
3
  Tags: users, user, user profile
4
  Requires at least: 3.5
5
  Tested up to: 3.5
6
+ Stable tag: 1.0.15
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.
14
+
15
+ A template tag is supplied for outputting to a theme and the option to override a user's default avatar is also available.
16
+
17
+ This plugin is fully compatible with <a href="http://wordpress.org/extend/plugins/post-thumbnail-editor/">Post Thumbnail Editor</a> for cropping any uploaded images.
18
+
19
+ If you like this plugin, please leave a rating/review and mark the plugin as working.
20
 
21
  == Installation ==
22
 
23
  1. Upload `metronet-profile-picture` folder to the `/wp-content/plugins/` directory
24
  2. Activate the plugin through the 'Plugins' menu in WordPress
25
  3. Place `<?php mt_profile_img() ?>` in your templates (arguments and usage are below)
26
+ 4. Use the "Override Avatar" function to change your default avatar.
27
 
28
  Arguments:
29
 
42
 
43
  Example Usage:
44
  `
45
+ <?php
46
+ //Assuming $post is in scope
47
+ if (function_exists ( 'mt_profile_img' ) ) {
48
+ $author_id=$post->post_author;
49
+ mt_profile_img( $author_id, array(
50
+ 'size' => 'thumbnail',
51
+ 'attr' => array( 'alt' => 'Alternative Text' ),
52
+ 'echo' => true )
53
+ );
54
+ }
55
+ ?>
56
  `
57
+ View the code on <a href="http://pastebin.com/Xaf8dJqQ">Pastebin</a>.
58
+
59
+ The `mt_profile_img` function internally uses the <a href="http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail">get_the_post_thumbnail</a> function to retrieve the profile image.
60
+
61
+ Optionally, if you choose the "Override Avatar" function, you can use <a href="http://codex.wordpress.org/Function_Reference/get_avatar">get_avatar</a> to retrieve the profile image.
62
 
63
  == Frequently Asked Questions ==
64
 
66
 
67
  1. Visit the profile page you would like to edit.
68
  2. Click "Upload or Change Profile Picture"
69
+ 3. Upload a new image and select "Set profile image", which will save the image.
70
 
71
  To override an avatar, select the "Override Avatar?" checkbox and save the profile page.
72
 
85
  == Screenshots ==
86
 
87
  1. Profile page options.
88
+ 2. Media upload dialog.
89
+ 3. Post Thumbnail Editor compatibility.
90
 
91
  == Changelog ==
92
 
93
+ = 1.0.15 =
94
+ * Built-in support for <a href="http://wordpress.org/extend/plugins/post-thumbnail-editor/">Post Thumbnail Editor</a>
95
+ * Better integration with the new WP 3.5 media uploader
96
+ * Various bug fixes.
97
+
98
  = 1.0.10 =
99
  * Usability enhancements.
100
  * Stripping out useless code.
118
 
119
  == Upgrade Notice ==
120
 
121
+ = 1.0.15 =
122
+ Built-in support for Post Thumbnail Editor. Better integration with the new WP 3.5 media uploader. Various bug fixes.
123
+
124
  = 1.0.10 =
125
+ 3.5 media uploader support. Usability enhancements. Code cleanup.
 
 
126
 
127
  = 1.0.9 =
128
+ 3.5 media uploader support.
129
 
130
  = 1.0.1 =
131
  Several important bug fixes including the ability to uncheck the avatar override, and the behavior when someone deletes their profile picture.