User Profile Picture - Version 1.0.9

Version Description

  • Adding support for the new 3.5 media uploader.
Download this release

Release Info

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

Code changes from version 1.0.3 to 1.0.9

assets/banner-772x250.png DELETED
Binary file
js/mpp.js CHANGED
@@ -1,17 +1,40 @@
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
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ uploader.on( 'toolbar:create:featured-image', function( toolbar ) {
9
+ this.createSelectToolbar( toolbar, {
10
+ text: wp.media.view.l10n.setFeaturedImage
11
+ });
12
+ }, uploader );
13
+
14
+ //For when the featured thumbnail is set
15
+ uploader.mt_featured_set = function( id ) {
16
+ var settings = wp.media.view.settings;
17
+
18
+ settings.post.featuredImageId = id;
19
+ $.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 ) {
20
+ jQuery( "#metronet-profile-image a" ).html( response );
21
+ } );
22
+ };
23
+
24
+ //For when the featured image is clicked
25
+ uploader.state('featured-image').on( 'select', function() {
26
+ var settings = wp.media.view.settings,
27
+ selection = this.get('selection').single();
28
+
29
+ if ( ! settings.post.featuredImageId )
30
+ return;
31
+
32
+ uploader.mt_featured_set( selection ? selection.id : -1 );
33
+ } );
34
+
35
+
36
+ //Open the media uploader
37
+ uploader.open();
38
+ return false;
39
+ });
40
+ } );
metronet-profile-picture.php CHANGED
@@ -4,8 +4,8 @@ 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.3
8
- Requires at least: 3.3
9
  Author URI: http://www.metronet.no
10
  Contributors: ronalfy, metronet
11
  */
@@ -75,25 +75,15 @@ class Metronet_Profile_Picture {
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
@@ -247,6 +237,54 @@ class Metronet_Profile_Picture {
247
  $dir .= '/' . ltrim( $path, '/' );
248
  return $dir;
249
  } //get_plugin_url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
 
251
  /**
252
  * init()
@@ -259,6 +297,7 @@ class Metronet_Profile_Picture {
259
  //* Localization Code */
260
  load_plugin_textdomain( 'metronet_profile_picture', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
261
 
 
262
  //Register post types
263
  $post_type_args = array(
264
  'public' => false,
@@ -275,51 +314,20 @@ class Metronet_Profile_Picture {
275
 
276
  }//end function init
277
 
 
 
278
  /**
279
  * insert_upload_form
280
  *
281
  * Adds an upload form to the user profile page and outputs profile image if there is one
282
  */
283
  public function insert_upload_form() {
284
- //Get user ID
285
- $user_id = isset( $_GET[ 'user_id' ] ) ? absint( $_GET[ 'user_id' ] ) : 0;
286
- if ( $user_id == 0 && IS_PROFILE_PAGE ) {
287
- $current_user = wp_get_current_user();
288
- $user_id = $current_user->ID;
289
- }
290
-
291
- //Get/Create Profile Picture Post
292
- $post_args = array(
293
- 'post_type' => 'mt_pp',
294
- 'author' => $user_id,
295
- 'post_status' => 'publish'
296
- );
297
- $posts = get_posts( $post_args );
298
- if ( !$posts ) {
299
- $post_id = wp_insert_post( array(
300
- 'post_author' => $user_id,
301
- 'post_type' => 'mt_pp',
302
- 'post_status' => 'publish',
303
- ) );
304
- } else {
305
- $post = end( $posts );
306
- $post_id = $post->ID;
307
- }
308
 
309
- //Form upload link
310
- $upload_url = admin_url( 'media-upload.php' );
311
- $query_args = array(
312
- 'user_id' => absint( $user_id ),
313
- 'post_id' => $post_id,
314
- 'tab' => 'gallery',
315
- 'TB_iframe' => "1",
316
- 'width' => "640",
317
- 'height' => "425"
318
- );
319
- $upload_url = esc_url( add_query_arg( $query_args, $upload_url ) );
320
 
321
  //Create upload link
322
- $upload_link = sprintf( "<a href='%s' class='thickbox add_media'>%s</a>", $upload_url, esc_html__( "Upload or Change Profile Picture", 'metronet_profile_picture' ) );
323
  ?>
324
  <tr valign="top">
325
  <th scope="row"><?php esc_html_e( "Profile Image", "metronet_profile_picture" ); ?></th>
@@ -330,7 +338,7 @@ class Metronet_Profile_Picture {
330
  <?php
331
  if ( has_post_thumbnail( $post_id ) ) {
332
  $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
333
- printf( "<a href='%s' class='thickbox add_media'>%s</a>", $upload_url, $post_thumbnail );
334
  }
335
  ?>
336
  </div><!-- #metronet-profile-image -->
@@ -350,11 +358,14 @@ class Metronet_Profile_Picture {
350
  * Output media scripts for thickbox and media uploader
351
  **/
352
  public function print_media_scripts() {
353
- wp_enqueue_script( 'mt-pp', $this->get_plugin_url( '/js/mpp.js' ), array( 'media-upload', 'thickbox' ) );
 
 
354
  } //end print_media_scripts
355
 
 
356
  public function print_media_styles() {
357
- wp_enqueue_style( 'thickbox' );
358
  } //end print_media_styles
359
 
360
  /**
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.9
8
+ Requires at least: 3.5
9
  Author URI: http://www.metronet.no
10
  Contributors: ronalfy, metronet
11
  */
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
+ check_ajax_referer( "update-post_$post_id" );
79
 
80
  //Save user meta
81
  update_user_meta( $user_id, 'metronet_post_id', $post_id );
82
+ set_post_thumbnail( $post_id, $thumbnail_id );
83
+
 
 
 
 
 
 
 
 
 
 
84
  if ( has_post_thumbnail( $post_id ) ) {
85
  $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
86
+ die( $post_thumbnail );
 
87
  }
88
  die( '' );
89
  } //end ajax_add_thumbnail
237
  $dir .= '/' . ltrim( $path, '/' );
238
  return $dir;
239
  } //get_plugin_url
240
+
241
+ /**
242
+ * get_post_id
243
+ *
244
+ * Gets a post id for the user - Creates a post if a post doesn't exist
245
+ *
246
+ @param int user_id User ID of the user
247
+ @return int post_id
248
+ */
249
+ private function get_post_id( $user_id = 0 ) {
250
+ //Get/Create Profile Picture Post
251
+ $post_args = array(
252
+ 'post_type' => 'mt_pp',
253
+ 'author' => $user_id,
254
+ 'post_status' => 'publish'
255
+ );
256
+ $posts = get_posts( $post_args );
257
+ if ( !$posts ) {
258
+ $post_id = wp_insert_post( array(
259
+ 'post_author' => $user_id,
260
+ 'post_type' => 'mt_pp',
261
+ 'post_status' => 'publish',
262
+ ) );
263
+ } else {
264
+ $post = end( $posts );
265
+ $post_id = $post->ID;
266
+ }
267
+ return $post_id;
268
+ } //end get_post_id
269
+
270
+ /**
271
+ * get_user_id
272
+ *
273
+ * Gets a user ID for the user
274
+ *
275
+ *@return int user_id
276
+ *
277
+ @return int post_id
278
+ */
279
+ private function get_user_id() {
280
+ //Get user ID
281
+ $user_id = isset( $_GET[ 'user_id' ] ) ? absint( $_GET[ 'user_id' ] ) : 0;
282
+ if ( $user_id == 0 && IS_PROFILE_PAGE ) {
283
+ $current_user = wp_get_current_user();
284
+ $user_id = $current_user->ID;
285
+ }
286
+ return $user_id;
287
+ } //end get_user_id
288
 
289
  /**
290
  * init()
297
  //* Localization Code */
298
  load_plugin_textdomain( 'metronet_profile_picture', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
299
 
300
+ add_theme_support( 'post-thumbnails' ); //This should be part of the theme, but the plugin registers it just in case.
301
  //Register post types
302
  $post_type_args = array(
303
  'public' => false,
314
 
315
  }//end function init
316
 
317
+
318
+
319
  /**
320
  * insert_upload_form
321
  *
322
  * Adds an upload form to the user profile page and outputs profile image if there is one
323
  */
324
  public function insert_upload_form() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
 
326
+ $user_id = $this->get_user_id();
327
+ $post_id = $this->get_post_id( $user_id );
 
 
 
 
 
 
 
 
 
328
 
329
  //Create upload link
330
+ $upload_link = sprintf( "<a href='#' class='add_media'>%s</a>", esc_html__( "Upload or Change Profile Picture", 'metronet_profile_picture' ) );
331
  ?>
332
  <tr valign="top">
333
  <th scope="row"><?php esc_html_e( "Profile Image", "metronet_profile_picture" ); ?></th>
338
  <?php
339
  if ( has_post_thumbnail( $post_id ) ) {
340
  $post_thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );
341
+ printf( "<a href='#' class='add_media'>%s</a>", $post_thumbnail );
342
  }
343
  ?>
344
  </div><!-- #metronet-profile-image -->
358
  * Output media scripts for thickbox and media uploader
359
  **/
360
  public function print_media_scripts() {
361
+ $post_id = $this->get_post_id( $this->get_user_id() );
362
+ wp_enqueue_media( array( 'post' => $post_id ) );
363
+ wp_enqueue_script( 'mt-pp', $this->get_plugin_url( '/js/mpp.js' ), array( 'media-editor' ) );
364
  } //end print_media_scripts
365
 
366
+ //todo - remove
367
  public function print_media_styles() {
368
+ //wp_enqueue_style( 'thickbox' );
369
  } //end print_media_styles
370
 
371
  /**
readme.txt CHANGED
@@ -1,9 +1,9 @@
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.3
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -67,6 +67,9 @@ We highly recommend the <a href='http://wordpress.org/extend/plugins/post-thumbn
67
 
68
  == Changelog ==
69
 
 
 
 
70
  = 1.0.3 =
71
  * Bug fix: Avatar classes in the comment section
72
 
1
  === Metronet Profile Picture ===
2
  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.9
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
67
 
68
  == Changelog ==
69
 
70
+ = 1.0.9 =
71
+ * Adding support for the new 3.5 media uploader.
72
+
73
  = 1.0.3 =
74
  * Bug fix: Avatar classes in the comment section
75