User Profile Picture - Version 1.5.0

Version Description

  • Released 2018-07-11
  • Support for AMP avatar has been added
  • Two REST API endpoints have been added to facilitate better programatic avatar selection
Download this release

Release Info

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

Code changes from version 1.4.3 to 1.5.0

Files changed (2) hide show
  1. metronet-profile-picture.php +146 -23
  2. readme.txt +13 -31
metronet-profile-picture.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: User 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: Ronald Huereca
7
- Version: 1.4.3
8
  Requires at least: 3.5
9
  Author URI: https://www.mediaron.com
10
  Contributors: ronalfy
@@ -60,6 +60,7 @@ class Metronet_Profile_Picture {
60
 
61
  //User Avatar override
62
  add_filter( 'get_avatar', array( &$this, 'avatar_override' ), 10, 6 );
 
63
 
64
  //Rest API
65
  add_action( 'rest_api_init', array( $this, 'rest_api_register' ) );
@@ -67,7 +68,7 @@ class Metronet_Profile_Picture {
67
  //Avatar check overridden - Can be overridden using a higher priority
68
  add_filter( 'mpp_hide_avatar_override', '__return_true', 5 );
69
  } //end constructor
70
-
71
  /**
72
  * ajax_add_thumbnail()
73
  *
@@ -221,6 +222,51 @@ class Metronet_Profile_Picture {
221
  return $custom_avatar;
222
  } //end avatar_override
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  /**
225
  * get_plugin_dir()
226
  *
@@ -328,7 +374,7 @@ class Metronet_Profile_Picture {
328
  * Initializes plugin localization, post types, updaters, plugin info, and adds actions/filters
329
  *
330
  */
331
- function init() {
332
 
333
  add_theme_support( 'post-thumbnails' ); //This should be part of the theme, but the plugin registers it just in case.
334
  //Register post types
@@ -344,6 +390,9 @@ class Metronet_Profile_Picture {
344
  'supports' => array( 'thumbnail' )
345
  );
346
  register_post_type( 'mt_pp', $post_type_args );
 
 
 
347
 
348
  }//end function init
349
 
@@ -488,6 +537,22 @@ class Metronet_Profile_Picture {
488
  * Registers REST API endpoint
489
  **/
490
  public function rest_api_register() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
491
  register_rest_route(
492
  'mpp/v1',
493
  '/user/(?P<id>\d+)',
@@ -504,33 +569,92 @@ class Metronet_Profile_Picture {
504
  );
505
  }
506
 
507
- /**
508
- * rest_api_get_profile()
509
- *
510
- * Returns an attachment image ID and profile image if available
511
- **/
512
- public function rest_api_get_profile( $data ) {
513
- $user_id = $data[ 'id' ];
514
- $user = get_user_by( 'id', $user_id );
515
- if ( ! $user ) {
516
- return new WP_Error( 'mpp_no_user', __( 'User not found.', 'metronet-profile-picture' ), array( 'status' => 404 ) );
517
  }
518
 
519
- //Get attachment ID
520
- $profile_post_id = absint( get_user_option( 'metronet_post_id', $user_id ) );
521
- $post_thumbnail_id = get_post_thumbnail_id( $profile_post_id );
522
- if ( ! $post_thumbnail_id ) {
523
- return new WP_Error( 'mpp_no_profile_picture', __( 'Profile picture not found.', 'metronet-profile-picture' ), array( 'status' => 404 ) );
524
  }
 
 
 
 
 
 
 
 
 
525
 
526
- //Get attachment URL
527
- $attachment_url = wp_get_attachment_url( $post_thumbnail_id );
 
528
 
529
  return array(
530
- 'attachment_id' => $post_thumbnail_id,
531
- 'attachment_url' => $attachment_url
 
 
532
  );
533
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534
 
535
  /**
536
  * rest_api_validate()
@@ -630,4 +754,3 @@ function mt_profile_img( $user_id, $args = array() ) {
630
  return $post_thumbnail;
631
  }
632
  } //end mt_profile_img
633
- ?>
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: Ronald Huereca
7
+ Version: 1.5.0
8
  Requires at least: 3.5
9
  Author URI: https://www.mediaron.com
10
  Contributors: ronalfy
60
 
61
  //User Avatar override
62
  add_filter( 'get_avatar', array( &$this, 'avatar_override' ), 10, 6 );
63
+ add_filter( 'pre_get_avatar_data', array( $this, 'pre_avatar_override' ), 10, 2 );
64
 
65
  //Rest API
66
  add_action( 'rest_api_init', array( $this, 'rest_api_register' ) );
68
  //Avatar check overridden - Can be overridden using a higher priority
69
  add_filter( 'mpp_hide_avatar_override', '__return_true', 5 );
70
  } //end constructor
71
+
72
  /**
73
  * ajax_add_thumbnail()
74
  *
222
  return $custom_avatar;
223
  } //end avatar_override
224
 
225
+ /**
226
+ * pre_avatar_override()
227
+ *
228
+ * Overrides an avatar with a profile image
229
+ *
230
+ * @param array $args Arguments to determine the avatar dimensions
231
+ * @param mixed $id_or_email
232
+ * @return array $args Overridden URL or default if none can be found
233
+ **/
234
+ public function pre_avatar_override( $args, $id_or_email ) {
235
+
236
+ //Get user data
237
+ if ( is_numeric( $id_or_email ) ) {
238
+ $user = get_user_by( 'id', ( int )$id_or_email );
239
+ } elseif( is_object( $id_or_email ) ) {
240
+ $comment = $id_or_email;
241
+ if ( empty( $comment->user_id ) ) {
242
+ $user = get_user_by( 'id', $comment->user_id );
243
+ } else {
244
+ $user = get_user_by( 'email', $comment->comment_author_email );
245
+ }
246
+ if ( !$user ) return $args;
247
+ } elseif( is_string( $id_or_email ) ) {
248
+ $user = get_user_by( 'email', $id_or_email );
249
+ } else {
250
+ return $args;
251
+ }
252
+ if ( ! $user ) return $args;
253
+ $user_id = $user->ID;
254
+
255
+ // Get the post the user is attached to
256
+ $size = $args[ 'size' ];
257
+
258
+ $profile_post_id = absint( get_user_option( 'metronet_post_id', $user_id ) );
259
+ $post_thumbnail_id = get_post_thumbnail_id( $profile_post_id );
260
+
261
+ // Attempt to get the image in the right size
262
+ $avatar_image = get_the_post_thumbnail_url( $profile_post_id, array( $size, $size ) );
263
+ if ( empty( $avatar_image ) ) {
264
+ return $args;
265
+ }
266
+ $args[ 'url' ] = $avatar_image;
267
+ return $args;
268
+ }
269
+
270
  /**
271
  * get_plugin_dir()
272
  *
374
  * Initializes plugin localization, post types, updaters, plugin info, and adds actions/filters
375
  *
376
  */
377
+ public function init() {
378
 
379
  add_theme_support( 'post-thumbnails' ); //This should be part of the theme, but the plugin registers it just in case.
380
  //Register post types
390
  'supports' => array( 'thumbnail' )
391
  );
392
  register_post_type( 'mt_pp', $post_type_args );
393
+ add_image_size( 'profile_24', 24, 24, true );
394
+ add_image_size( 'profile_48', 48, 48, true );
395
+ add_image_size( 'profile_96', 96, 96, true );
396
 
397
  }//end function init
398
 
537
  * Registers REST API endpoint
538
  **/
539
  public function rest_api_register() {
540
+ register_rest_field(
541
+ 'user',
542
+ 'mpp_avatar',
543
+ array(
544
+ 'get_callback' => array( $this, 'rest_api_add_profile_to_user' )
545
+ )
546
+ );
547
+ register_rest_route(
548
+ 'mpp/v2',
549
+ '/profile-image/me',
550
+ array(
551
+ 'methods' => 'POST',
552
+ 'callback' => array( $this, 'rest_api_put_profile' )
553
+ )
554
+ );
555
+ // keep it for backward compatibility
556
  register_rest_route(
557
  'mpp/v1',
558
  '/user/(?P<id>\d+)',
569
  );
570
  }
571
 
572
+ public function rest_api_put_profile( $request ) {
573
+
574
+ $user_id = get_current_user_id();
575
+ $media_id = (int) $request['media_id'];
576
+ if ( ! current_user_can( 'upload_files' ) ) {
577
+ return new WP_Error( 'mpp_insufficient_privs', __( 'You must be able to upload files.', 'metronet-profile-picture' ), array( 'status' => 403 ) );
 
 
 
 
578
  }
579
 
580
+ if ( ! $user_id ) {
581
+ return new WP_Error( 'mpp_no_user', __( 'User not found.', 'metronet-profile-picture' ), array( 'status' => 403 ) );
 
 
 
582
  }
583
+ $is_post_owner = ( $user_id == get_post($media_id) ->post_author ) ? true : false ;
584
+ if ( ! $is_post_owner ) {
585
+ return new WP_Error( 'mpp_not_owner', __( 'User not owner.', 'metronet-profile-picture' ), array( 'status' => 403 ) );
586
+ }
587
+
588
+ $post_id = $this->get_post_id( $user_id );
589
+ //Save user meta
590
+ update_user_option( $user_id, 'metronet_post_id', $post_id );
591
+ update_user_option( $user_id, 'metronet_image_id', $media_id ); //Added via this thread (Props Solinx) - https://wordpress.org/support/topic/storing-image-id-directly-as-user-meta-data
592
 
593
+ set_post_thumbnail( $post_id, $media_id );
594
+
595
+ $attachment_url = wp_get_attachment_url( $media_id );
596
 
597
  return array(
598
+ '24' => wp_get_attachment_image_url( $media_id, 'profile_24', false, '' ),
599
+ '48' => wp_get_attachment_image_url( $media_id, 'profile_48', false, '' ),
600
+ '96' => wp_get_attachment_image_url( $media_id, 'profile_96', false, '' ),
601
+ 'full'=> $attachment_url
602
  );
603
  }
604
+ /**
605
+ * rest_api_get_profile()
606
+ *
607
+ * Returns an attachment image ID and profile image if available
608
+ **/
609
+ public function rest_api_add_profile_to_user( $object, $field_name, $request ) {
610
+ $user_id = $object[ 'id' ];
611
+ $user = get_user_by( 'id', $user_id );
612
+ if ( ! $user ) {
613
+ return new WP_Error( 'mpp_no_user', __( 'User not found.', 'metronet-profile-picture' ), array( 'status' => 404 ) );
614
+ }
615
+
616
+ // No capability check here because we're just returning user profile data
617
+
618
+ //Get attachment ID
619
+ $profile_post_id = absint( get_user_option( 'metronet_post_id', $user_id ) );
620
+ $post_thumbnail_id = get_post_thumbnail_id( $profile_post_id );
621
+ if ( ! $post_thumbnail_id ) {
622
+ return new WP_Error( 'mpp_no_profile_picture', __( 'Profile picture not found.', 'metronet-profile-picture' ), array( 'status' => 404 ) );
623
+ }
624
+
625
+ //Get attachment URL
626
+ $attachment_url = wp_get_attachment_url( $post_thumbnail_id );
627
+
628
+ return array(
629
+ '24' => wp_get_attachment_image_url( $post_thumbnail_id, 'profile_24', false, '' ),
630
+ '48' => wp_get_attachment_image_url( $post_thumbnail_id, 'profile_48', false, '' ),
631
+ '96' => wp_get_attachment_image_url( $post_thumbnail_id, 'profile_96', false, '' ),
632
+ 'full'=> $attachment_url
633
+ );
634
+ }
635
+
636
+ public function rest_api_get_profile( $data ) {
637
+ $user_id = $data[ 'id' ];
638
+ $user = get_user_by( 'id', $user_id );
639
+ if ( ! $user ) {
640
+ return new WP_Error( 'mpp_no_user', __( 'User not found.', 'metronet-profile-picture' ), array( 'status' => 404 ) );
641
+ }
642
+
643
+ //Get attachment ID
644
+ $profile_post_id = absint( get_user_option( 'metronet_post_id', $user_id ) );
645
+ $post_thumbnail_id = get_post_thumbnail_id( $profile_post_id );
646
+ if ( ! $post_thumbnail_id ) {
647
+ return new WP_Error( 'mpp_no_profile_picture', __( 'Profile picture not found.', 'metronet-profile-picture' ), array( 'status' => 404 ) );
648
+ }
649
+
650
+ //Get attachment URL
651
+ $attachment_url = wp_get_attachment_url( $post_thumbnail_id );
652
+
653
+ return array(
654
+ 'attachment_id' => $post_thumbnail_id,
655
+ 'attachment_url' => $attachment_url
656
+ );
657
+ }
658
 
659
  /**
660
  * rest_api_validate()
754
  return $post_thumbnail;
755
  }
756
  } //end mt_profile_img
 
readme.txt CHANGED
@@ -3,12 +3,12 @@ Contributors: ronalfy
3
  Tags: users, user, user profile
4
  Requires at least: 3.5
5
  Tested up to: 4.9
6
- Stable tag: 1.4.3
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
  Donate link: https://mediaron.com/contribute/
10
 
11
- Set a custom profile image for a user using the standard WordPress media upload tool.
12
  == Description ==
13
 
14
  Set or remove a custom profile image for a user using the standard WordPress media upload tool.
@@ -17,18 +17,13 @@ Users must have the ability to upload images (typically author role or greater).
17
 
18
  A template tag is supplied for outputting to a theme and the option to override a user's default avatar is also available.
19
 
20
- <h3>Help Contribute</h3>
21
-
22
- * Leave a star rating
23
- * <a href="https://translate.wordpress.org/projects/wp-plugins/metronet-profile-picture">Contribute a translation</a>
24
- * <a href="https://github.com/ronalfy/user-profile-picture">Contribute some code</a>
25
 
26
  == Installation ==
27
 
28
  1. Upload `metronet-profile-picture` folder to the `/wp-content/plugins/` directory
29
  2. Activate the plugin through the 'Plugins' menu in WordPress
30
  3. Place `<?php mt_profile_img() ?>` in your templates (arguments and usage are below)
31
- 4. Use the "Override Avatar" function to change your default avatar.
32
 
33
  Arguments:
34
 
@@ -59,29 +54,10 @@ if (function_exists ( 'mt_profile_img' ) ) {
59
  }
60
  ?>
61
  `
62
- View the code on <a href="http://pastebin.com/Xaf8dJqQ">Pastebin</a>.
63
-
64
- 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.
65
-
66
- 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.
67
-
68
- If you want the "Override Avatar" checkbox to be checked by default, drop this into your theme's `functions.php` file or <a href="http://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/">Site-specific plugin</a>:
69
-
70
- `add_filter( 'mpp_avatar_override', '__return_true' );`
71
-
72
- If you want to hide the "Override Avatar" checkbox, use this filter (the override functionality will be enabled by default):
73
- `add_filter( 'mpp_hide_avatar_override', '__return_true' );`
74
 
75
  The REST API is currently supported for versions of WordPress 4.4 and up.
76
 
77
- The endpoint is: `http://www.yourdomain.com/wp-json/mpp/v1/user/{user_id}`
78
-
79
- If a profile picture is found, it'll return JSON as follows:
80
-
81
- `
82
- {"attachment_id":"3638","attachment_url":"http:\/\/localhost\/ronalfy\/wp-content\/uploads\/2015\/12\/Leaderboard-All-Time.png"}
83
- `
84
-
85
  == Frequently Asked Questions ==
86
 
87
  = How do you set a user profile image? =
@@ -89,15 +65,13 @@ If a profile picture is found, it'll return JSON as follows:
89
  1. Visit the profile page you would like to edit.
90
  2. Click on the profile picture to add, edit, or remove the profile picture.
91
 
92
- To override an avatar, select the "Override Avatar?" checkbox and save the profile page.
93
-
94
  = What role does a user have to be to set a profile image? =
95
 
96
  Author or greater.
97
 
98
  = How do I create specific thumbnail sizes? =
99
 
100
- 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.
101
 
102
  = The image is cropped wrong. How do I fix this? =
103
 
@@ -114,6 +88,11 @@ Yes, but you'll have to set a new profile image per site. This is currently a l
114
 
115
  == Changelog ==
116
 
 
 
 
 
 
117
  = 1.4.3 =
118
  * Released 2016-09-24
119
  * Bug fix: Post featured image is being shown as user's profile picture when no avatar is selected.
@@ -227,6 +206,9 @@ Yes, but you'll have to set a new profile image per site. This is currently a l
227
 
228
  == Upgrade Notice ==
229
 
 
 
 
230
  = 1.4.3 =
231
  Bug fix: Post featured image is being shown as user's profile picture when no avatar is selected.
232
 
3
  Tags: users, user, user profile
4
  Requires at least: 3.5
5
  Tested up to: 4.9
6
+ Stable tag: 1.5
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
  Donate link: https://mediaron.com/contribute/
10
 
11
+ Set a custom profile image (avatar) for a user using the standard WordPress media upload tool.
12
  == Description ==
13
 
14
  Set or remove a custom profile image for a user using the standard WordPress media upload tool.
17
 
18
  A template tag is supplied for outputting to a theme and the option to override a user's default avatar is also available.
19
 
20
+ See the documentation on <a href="https://github.com/ronalfy/user-profile-picture">GitHub</a>.
 
 
 
 
21
 
22
  == Installation ==
23
 
24
  1. Upload `metronet-profile-picture` folder to the `/wp-content/plugins/` directory
25
  2. Activate the plugin through the 'Plugins' menu in WordPress
26
  3. Place `<?php mt_profile_img() ?>` in your templates (arguments and usage are below)
 
27
 
28
  Arguments:
29
 
54
  }
55
  ?>
56
  `
57
+ View the code on <a href="https://github.com/ronalfy/user-profile-picture">GitHub</a>.
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  The REST API is currently supported for versions of WordPress 4.4 and up.
60
 
 
 
 
 
 
 
 
 
61
  == Frequently Asked Questions ==
62
 
63
  = How do you set a user profile image? =
65
  1. Visit the profile page you would like to edit.
66
  2. Click on the profile picture to add, edit, or remove the profile picture.
67
 
 
 
68
  = What role does a user have to be to set a profile image? =
69
 
70
  Author or greater.
71
 
72
  = How do I create specific thumbnail sizes? =
73
 
74
+ As of 1.5, three image sizes are created: profile_24, profile_48, and profile_96. You are not limited to these sizes, however.
75
 
76
  = The image is cropped wrong. How do I fix this? =
77
 
88
 
89
  == Changelog ==
90
 
91
+ = 1.5.0 =
92
+ * Released 2018-07-11
93
+ * Support for AMP avatar has been added
94
+ * Two REST API endpoints have been added to facilitate better programatic avatar selection
95
+
96
  = 1.4.3 =
97
  * Released 2016-09-24
98
  * Bug fix: Post featured image is being shown as user's profile picture when no avatar is selected.
206
 
207
  == Upgrade Notice ==
208
 
209
+ = 1.5.0 =
210
+ AMP now supported. Two REST API Endpoints added.
211
+
212
  = 1.4.3 =
213
  Bug fix: Post featured image is being shown as user's profile picture when no avatar is selected.
214