PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-avatars.php

class-avatars.php in ActivityPub trunk, at includes/class-avatars.php

88 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Avatars class file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub;
9
10 use Activitypub\Collection\Remote_Actors;
11
12 /**
13 * ActivityPub Avatars class.
14 */
15 class Avatars {
16 /**
17 * Initialize the class, registering WordPress hooks.
18 */
19 public static function init() {
20 \add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
21 }
22
23 /**
24 * Replaces the default avatar.
25 *
26 * @param array $args Arguments passed to get_avatar_data(), after processing.
27 * @param int|string|object $id_or_email A user ID, email address, or comment object.
28 *
29 * @return array $args
30 */
31 public static function pre_get_avatar_data( $args, $id_or_email ) {
32 if (
33 ! $id_or_email instanceof \WP_Comment ||
34 ! isset( $id_or_email->comment_type ) ||
35 $id_or_email->user_id
36 ) {
37 return $args;
38 }
39
40 /**
41 * Filter allowed comment types for avatars.
42 *
43 * @param array $allowed_comment_types Array of allowed comment types.
44 */
45 $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
46 if ( ! \in_array( $id_or_email->comment_type ?: 'comment', $allowed_comment_types, true ) ) {
47 return $args;
48 }
49
50 // Respect WordPress "show avatars" setting.
51 if ( ! \get_option( 'show_avatars' ) ) {
52 return $args;
53 }
54
55 $avatar = null;
56
57 // First, try to get avatar from remote actor.
58 $remote_actor_id = \get_comment_meta( $id_or_email->comment_ID, '_activitypub_remote_actor_id', true );
59 if ( $remote_actor_id ) {
60 $avatar = Remote_Actors::get_avatar_url( $remote_actor_id );
61 }
62
63 // Fall back to avatar_url comment meta for backward compatibility.
64 if ( ! $avatar ) {
65 $avatar = \get_comment_meta( $id_or_email->comment_ID, 'avatar_url', true );
66 }
67
68 if ( $avatar ) {
69 if ( empty( $args['class'] ) ) {
70 $args['class'] = array();
71 } elseif ( \is_string( $args['class'] ) ) {
72 $args['class'] = \explode( ' ', $args['class'] );
73 }
74
75 /** This filter is documented in wp-includes/link-template.php */
76 $args['url'] = \apply_filters( 'get_avatar_url', $avatar, $id_or_email, $args );
77 $args['class'][] = 'avatar';
78 $args['class'][] = 'avatar-activitypub';
79 $args['class'][] = 'avatar-' . (int) $args['size'];
80 $args['class'][] = 'photo';
81 $args['class'][] = 'u-photo';
82 $args['class'] = \array_unique( $args['class'] );
83 }
84
85 return $args;
86 }
87 }
88