| 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 |
|