From 93079b0266f0519890657908aee45ba585ee4c54 Mon Sep 17 00:00:00 2001 From: Adam Cassis Date: Wed, 28 Aug 2024 10:37:46 +0200 Subject: [PATCH 1/5] fix: user meta retrieval --- includes/class-simple-local-avatars.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index dd2de71..16ed153 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -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 static function get_user_local_avatar( $user ) { + $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. * @@ -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 = self::get_user_local_avatar( $user_id ); if ( empty( $local_avatars['full'] ) ) { return ''; } @@ -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 = self::get_user_local_avatar( $user_id ); if ( empty( $old_avatars ) ) { return; From d9ff4bb302c3a36d70443aefc5c1273ff2187bb3 Mon Sep 17 00:00:00 2001 From: Adam Cassis Date: Mon, 2 Sep 2024 12:40:31 +0200 Subject: [PATCH 2/5] fix: static method; use in more places --- includes/class-simple-local-avatars.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 16ed153..7805830 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -332,7 +332,7 @@ public function get_user_id( $id_or_email ) { * @param int $user_id User ID. * @return array Array with avatar data. */ - public static function get_user_local_avatar( $user ) { + 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 []; @@ -357,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 = self::get_user_local_avatar( $user_id ); + $local_avatars = $this->get_user_local_avatar( $user_id ); if ( empty( $local_avatars['full'] ) ) { return ''; } @@ -493,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'] ) ) { $alt = ''; // If no avatar is set, check if we are using a default avatar with alt text. @@ -1125,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 ); } @@ -1195,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 = self::get_user_local_avatar( $user_id ); + $old_avatars = $this->get_user_local_avatar( $user_id ); if ( empty( $old_avatars ) ) { return; @@ -1286,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; } @@ -1411,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 ); } From eeb66bcdf86d6ca7d455edd2f363368603a97911 Mon Sep 17 00:00:00 2001 From: Adam Cassis Date: Fri, 13 Sep 2024 11:13:33 +0200 Subject: [PATCH 3/5] feat: add isset check --- includes/class-simple-local-avatars.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 7805830..1991cfb 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -358,7 +358,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 = $this->get_user_local_avatar( $user_id ); - if ( empty( $local_avatars['full'] ) ) { + if ( ! isset( $local_avatars['full'] ) ||empty( $local_avatars['full'] ) ) { return ''; } From 4d775cc749ec45fcfa03b556bf3e9251d3ea2e5e Mon Sep 17 00:00:00 2001 From: Faisal Alvi Date: Fri, 13 Sep 2024 23:56:34 +0530 Subject: [PATCH 4/5] Update includes/class-simple-local-avatars.php --- includes/class-simple-local-avatars.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 1991cfb..e632c7f 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -494,7 +494,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 = $this->get_user_local_avatar( $user_id ); - if ( empty( $local_avatars['media_id'] ) ) { + if ( ! isset( $local_avatars['media_id'] ) || empty( $local_avatars['media_id'] ) ) { $alt = ''; // If no avatar is set, check if we are using a default avatar with alt text. if ( 'simple_local_avatar' === get_option( 'avatar_default' ) ) { From 946d7578a61b7012fb114cbf318fc8b7999f8af4 Mon Sep 17 00:00:00 2001 From: Faisal Alvi Date: Fri, 13 Sep 2024 23:56:40 +0530 Subject: [PATCH 5/5] Update includes/class-simple-local-avatars.php --- includes/class-simple-local-avatars.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index e632c7f..7746c1b 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -358,7 +358,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 = $this->get_user_local_avatar( $user_id ); - if ( ! isset( $local_avatars['full'] ) ||empty( $local_avatars['full'] ) ) { + if ( ! isset( $local_avatars['full'] ) || empty( $local_avatars['full'] ) ) { return ''; }