-
Notifications
You must be signed in to change notification settings - Fork 33
Feature: Save google profile picture of the user during first login for both the new and existing users #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 5 commits
ed17145
573693b
cdb6f84
63a2c9b
86f6ffc
489c68b
0479da9
7bc0631
f01033a
c6659bb
877990d
68cc83c
15820bd
62c8c18
55118bc
4d93934
d0e204b
d8d9d61
daec6ee
5b1dedb
23098e0
17dbdc3
1a367d7
f8679f5
c36f21e
f8ac4e9
64e44b3
f6aa442
0d17dba
458e84c
df906b2
bcf6db9
1e6f6f7
964f6cf
2f04b1f
b651a08
9d4693d
d53d4f8
f94397e
268d19b
c1b6635
9a5a33b
e1f2707
3f975d5
596427b
f45b753
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -112,6 +112,21 @@ public function register( stdClass $user ): ?WP_User { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Filter to bypass the profile picture saving process. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since n.e.x.t | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param boolean $save Whether to save profile picture or not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param int $uid WP User ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
mi5t4n marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param \stdClass User object return by Google. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
mi5t4n marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $save_profile_picture = apply_filters( 'rtcamp.google_save_user_profile_picture', true, $uid, $user ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( $save_profile_picture ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->save_user_profile_picture( $uid, $user ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
mi5t4n marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Fires once the user has been registered successfully. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -179,4 +194,57 @@ private function can_register_with_email( string $email ): bool { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return in_array( $email_parts[1], $whitelisted_domains, true ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Save user profile picture. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param int $user_id WP User ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param \stdClass $user User object returned by google. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
mi5t4n marked this conversation as resolved.
Outdated
mi5t4n marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return void | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private function save_user_profile_picture( $user_id, $user ): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| global $wp_filesystem; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( is_null( $wp_filesystem ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| require_once ABSPATH . '/wp-admin/includes/file.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
mi5t4n marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| WP_Filesystem(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| require_once ABSPATH . 'wp-admin/includes/media.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| require_once ABSPATH . 'wp-admin/includes/file.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| require_once ABSPATH . 'wp-admin/includes/image.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Using larger image size. By default, profile picture has 96 width size with cropped. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
mi5t4n marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $profile_picture_url = str_replace( '=s96-c', '', $user->picture ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
mi5t4n marked this conversation as resolved.
Comment on lines
+302
to
+303
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $stripped_picture_url = str_replace( '=s96-c', '', $profile_picture_url ); | |
| // Remove '=s96-c' from the end of the path, and remove 'sz' query parameter if present. | |
| $parsed_url = parse_url( $profile_picture_url ); | |
| // Remove '=s96-c' from the end of the path if present. | |
| if ( isset( $parsed_url['path'] ) ) { | |
| $parsed_url['path'] = preg_replace( '/=s\d+-c$/', '', $parsed_url['path'] ); | |
| } | |
| // Remove 'sz' or 's' query parameter if present. | |
| if ( isset( $parsed_url['query'] ) ) { | |
| parse_str( $parsed_url['query'], $query_params ); | |
| unset( $query_params['sz'], $query_params['s'] ); | |
| $parsed_url['query'] = http_build_query( $query_params ); | |
| if ( $parsed_url['query'] === '' ) { | |
| unset( $parsed_url['query'] ); | |
| } | |
| } | |
| // Rebuild the URL. | |
| $stripped_picture_url = ( isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '' ) | |
| . ( isset( $parsed_url['host'] ) ? $parsed_url['host'] : '' ) | |
| . ( isset( $parsed_url['path'] ) ? $parsed_url['path'] : '' ) | |
| . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ) | |
| . ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a check for WPVIP? and if the site is hosted on WPVIP we directly use wpcom_vip_download_image() this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SH4LIN Yes, it can be a bit unclear—I’ll look into it further to make it more straightforward. As for re-syncing the image at each login, should we also extend this to include the First Name and Last Name, since those fields can also be updated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SH4LIN Regarding vip check, that is a good catch. I will promptly implement this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also extend this to include the First Name and Last Name, since those fields can also be updated?
In WordPress, we have the option to update the first name and last name. So, if they want to customize it, they can do so from the settings. However, there is no option in the WordPress dashboard to change the image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a check for WPVIP? and if the site is hosted on WPVIP we directly use
wpcom_vip_download_image()this function.
I tried to use this function, it seems it won't work in our scenario. I got the following error while trying to use it .

The function was expecting a POST request, but Google calls the redirect URL using the GET method instead. We could try to change the global variables to make it work, but that would be a hacky solution. So, I decided to remove the function for now.
Uh oh!
There was an error while loading. Please reload this page.