Skip to content
This repository was archived by the owner on Mar 7, 2023. It is now read-only.

Commit b961ef6

Browse files
committed
nicer avatar code
1 parent f303835 commit b961ef6

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

salmon.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function init() {
4646
add_action( 'generate_rewrite_rules', array( 'Salmon_Plugin', 'add_rewrite_rules' ) );
4747

4848
// add avatar filter
49-
add_filter( 'get_avatar', array( 'Salmon_Plugin', 'add_salmon_avatar' ), 10, 5 );
49+
add_filter( 'pre_get_avatar_data', array( 'Salmon_Plugin', 'pre_get_avatar_data' ), 10, 2 );
5050

5151
add_action( 'comment_atom_entry', array( 'Salmon_Plugin', 'add_crossposting_extension' ) );
5252

@@ -84,38 +84,36 @@ public static function parse_query( $wp_query ) {
8484
}
8585

8686
/**
87-
* displays facebook avatars for the custom comment type "facebook"
87+
* Replaces the default avatar with the Salmon photo
8888
*
89-
* @param string $avatar the original img tag
89+
* @param array $args Arguments passed to get_avatar_data(), after processing.
90+
* @param int|string|object $id_or_email A user ID, email address, or comment object
9091
*
91-
* @return string the new image tag
92+
* @return array $args
9293
*/
93-
public static function add_salmon_avatar( $avatar, $id_or_email, $size, $default, $alt = '' ) {
94-
if ( ! is_object( $id_or_email ) || ! isset( $id_or_email->comment_type ) || get_comment_meta( $id_or_email->comment_ID, '_comment_type', true ) != 'salmon' ) {
95-
return $avatar;
94+
public static function pre_get_avatar_data( $args, $id_or_email ) {
95+
if ( ! $id_or_email instanceof WP_Comment ||
96+
! isset( $id_or_email->comment_type ) ||
97+
$id_or_email->user_id ) {
98+
return $args;
9699
}
97100

98101
$avatars = get_comment_meta( $id_or_email->comment_ID, '_salmon_avatars', true );
99102

100103
if ( ! $avatars ) {
101-
return $avatar;
104+
return $args;
102105
}
103106

104107
if ( array_key_exists( $size, $avatars ) ) {
105-
$url = $avatars[ $size ];
108+
$args['url'] = $avatars[ $size ];
106109
} else {
107-
$url = $avatars[ min( array_diff( array_keys( $avatars ), range( 0, $size ) ) ) ];
110+
$args['url'] = $avatars[ min( array_diff( array_keys( $avatars ), range( 0, $size ) ) ) ];
108111
}
109112

110-
if ( false === $alt ) {
111-
$safe_alt = '';
112-
} else {
113-
$safe_alt = esc_attr( $alt );
114-
}
115-
116-
$avatar = "<img alt='{$safe_alt}' src='{$url}' class='avatar avatar-{$size} photo avatar-facebook' height='{$size}' width='{$size}' />";
113+
$args['class'][] = 'avatar-salmon';
114+
$args['class'][] = "avatar-$size";
117115

118-
return $avatar;
116+
return $args;
119117
}
120118

121119
/**

0 commit comments

Comments
 (0)