@@ -83,4 +83,58 @@ public static function schedule_post_activity( $new_status, $old_status, $post )
8383 wp_schedule_single_event ( time () + wp_rand ( 0 , 120 ), 'activitypub_send_update_activity ' , array ( $ post ->ID ) );
8484 }
8585 }
86+
87+ /**
88+ * Replaces the default avatar
89+ *
90+ * @param array $args Arguments passed to get_avatar_data(), after processing.
91+ * @param int|string|object $id_or_email A user ID, email address, or comment object
92+ *
93+ * @return array $args
94+ */
95+ public static function pre_get_avatar_data ( $ args , $ id_or_email ) {
96+ if ( ! $ id_or_email instanceof WP_Comment ||
97+ ! isset ( $ id_or_email ->comment_type ) ||
98+ $ id_or_email ->user_id ) {
99+ return $ args ;
100+ }
101+
102+ $ allowed_comment_types = apply_filters ( 'get_avatar_comment_types ' , array ( 'comment ' ) );
103+ if ( ! empty ( $ id_or_email ->comment_type ) && ! in_array ( $ id_or_email ->comment_type , (array ) $ allowed_comment_types , true ) ) {
104+ $ args ['url ' ] = false ;
105+ /** This filter is documented in wp-includes/link-template.php */
106+ return apply_filters ( 'get_avatar_data ' , $ args , $ id_or_email );
107+ }
108+
109+ // check if comment has an avatar
110+ $ avatar = self ::get_avatar_url ( $ id_or_email ->comment_ID );
111+
112+ if ( $ avatar ) {
113+ if ( ! isset ( $ args ['class ' ] ) || ! is_array ( $ args ['class ' ] ) ) {
114+ $ args ['class ' ] = array ( 'u-photo ' );
115+ } else {
116+ $ args ['class ' ][] = 'u-photo ' ;
117+ $ args ['class ' ] = array_unique ( $ args ['class ' ] );
118+ }
119+ $ args ['url ' ] = $ avatar ;
120+ $ args ['class ' ][] = 'avatar-activitypub ' ;
121+ }
122+
123+ return $ args ;
124+ }
125+
126+ /**
127+ * Function to retrieve Avatar URL if stored in meta
128+ *
129+ *
130+ * @param int|WP_Comment $comment
131+ *
132+ * @return string $url
133+ */
134+ public static function get_avatar_url ( $ comment ) {
135+ if ( is_numeric ( $ comment ) ) {
136+ $ comment = get_comment ( $ comment );
137+ }
138+ return get_comment_meta ( $ comment ->comment_ID , 'avatar_url ' , true );
139+ }
86140}
0 commit comments