Skip to content
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

fix: user meta retrieval #302

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions includes/class-simple-local-avatars.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ public function get_user_id( $id_or_email ) {
return $user_id;
}

/**
* Get the local avatar user meta.
*
* @param int $user_id User ID.
* @return array Array with avatar data.
*/
public function get_user_local_avatar( $user_id ) {
$local_avatars = get_user_meta( $user_id, $this->user_key, true );
if ( ! is_array( $local_avatars ) || empty( $local_avatars ) ) {
return [];
}
return $local_avatars;
}

/**
* Get local avatar url.
*
Expand All @@ -343,7 +357,7 @@ public function get_simple_local_avatar_url( $id_or_email, $size ) {
}

// Fetch local avatar from meta and make sure it's properly set.
$local_avatars = get_user_meta( $user_id, $this->user_key, true );
$local_avatars = $this->get_user_local_avatar( $user_id );
if ( empty( $local_avatars['full'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition might cause a PHP notice or warning if the get_user_local_avatar() method returns an array that doesn't have the full key or if an empty array is returned. To avoid the notice, we can check if the key exists before accessing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check in eeb66bc

return '';
}
Expand Down Expand Up @@ -479,7 +493,7 @@ public function get_simple_local_avatar_alt( $id_or_email ) {
}

// Fetch local avatar from meta and make sure we have a media ID.
$local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
$local_avatars = $this->get_user_local_avatar( $user_id );
if ( empty( $local_avatars['media_id'] ) ) {
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved
$alt = '';
// If no avatar is set, check if we are using a default avatar with alt text.
Expand Down Expand Up @@ -1111,7 +1125,7 @@ public function edit_user_profile_update( $user_id ) {
endif;

// Handle ratings
if ( isset( $avatar_id ) || get_user_meta( $user_id, $this->user_key, true ) ) {
if ( isset( $avatar_id ) || ! empty( $this->get_user_local_avatar( $user_id ) ) ) {
if ( empty( $_POST['simple_local_avatar_rating'] ) || ! array_key_exists( $_POST['simple_local_avatar_rating'], $this->avatar_ratings ) ) {
$_POST['simple_local_avatar_rating'] = key( $this->avatar_ratings );
}
Expand Down Expand Up @@ -1181,7 +1195,7 @@ public function ajax_assign_simple_local_avatar_media() {
* @param int $user_id User ID.
*/
public function avatar_delete( $user_id ) {
$old_avatars = (array) get_user_meta( $user_id, $this->user_key, true );
$old_avatars = $this->get_user_local_avatar( $user_id );

if ( empty( $old_avatars ) ) {
return;
Expand Down Expand Up @@ -1272,7 +1286,7 @@ public function register_rest_fields() {
* @param object $user User object
*/
public function get_avatar_rest( $user ) {
$local_avatar = get_user_meta( $user['id'], $this->user_key, true );
$local_avatar = $this->get_user_local_avatar( $user['id'] );
if ( empty( $local_avatar ) ) {
return;
}
Expand Down Expand Up @@ -1397,7 +1411,7 @@ public function sla_clear_user_cache() {
if ( ! empty( $users ) ) {
foreach ( $users as $user ) {
$user_id = $user->ID;
$local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
$local_avatars = $this->get_user_local_avatar( $user_id );
$media_id = isset( $local_avatars['media_id'] ) ? $local_avatars['media_id'] : '';
$this->clear_user_avatar_cache( $local_avatars, $user_id, $media_id );
}
Expand Down
Loading