From 30b939b5a139cbe90a5ff78a80f4cfeb929286c1 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 20 Feb 2019 21:05:29 +0100 Subject: [PATCH] show avatars --- README.md | 6 +++- activitypub.php | 3 +- includes/class-activitypub.php | 54 ++++++++++++++++++++++++++++++++++ languages/activitypub.pot | 2 +- readme.txt | 6 +++- 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d0896c8..6895c15 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Tags:** OStatus, fediverse, activitypub, activitystream **Requires at least:** 4.7 **Tested up to:** 5.1 -**Stable tag:** 0.4.3 +**Stable tag:** 0.4.4 **Requires PHP:** 5.6 **License:** MIT **License URI:** http://opensource.org/licenses/MIT @@ -55,6 +55,10 @@ To implement: Project maintained on github at [pfefferle/wordpress-activitypub](https://github.com/pfefferle/wordpress-activitypub). +### 0.4.4 ### + +* show avatars + ### 0.4.3 ### * finally fixed backlink in excerpt/summary posts diff --git a/activitypub.php b/activitypub.php index 42a7b37..71b7e33 100644 --- a/activitypub.php +++ b/activitypub.php @@ -3,7 +3,7 @@ * Plugin Name: ActivityPub * Plugin URI: https://github.com/pfefferle/wordpress-activitypub/ * Description: The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format. - * Version: 0.4.3 + * Version: 0.4.4 * Author: Matthias Pfefferle * Author URI: https://notiz.blog/ * License: MIT @@ -31,6 +31,7 @@ function activitypub_init() { add_filter( 'template_include', array( 'Activitypub', 'render_json_template' ), 99 ); add_filter( 'query_vars', array( 'Activitypub', 'add_query_vars' ) ); add_action( 'init', array( 'Activitypub', 'add_rewrite_endpoint' ) ); + add_filter( 'pre_get_avatar_data', array( 'Activitypub', 'pre_get_avatar_data' ), 11, 2 ); // Configure the REST API route require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-outbox.php'; diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index e1088fc..74b33e3 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -83,4 +83,58 @@ class Activitypub { wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) ); } } + + /** + * Replaces the default avatar + * + * @param array $args Arguments passed to get_avatar_data(), after processing. + * @param int|string|object $id_or_email A user ID, email address, or comment object + * + * @return array $args + */ + public static function pre_get_avatar_data( $args, $id_or_email ) { + if ( ! $id_or_email instanceof WP_Comment || + ! isset( $id_or_email->comment_type ) || + $id_or_email->user_id ) { + return $args; + } + + $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); + if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) { + $args['url'] = false; + /** This filter is documented in wp-includes/link-template.php */ + return apply_filters( 'get_avatar_data', $args, $id_or_email ); + } + + // check if comment has an avatar + $avatar = self::get_avatar_url( $id_or_email->comment_ID ); + + if ( $avatar ) { + if ( ! isset( $args['class'] ) || ! is_array( $args['class'] ) ) { + $args['class'] = array( 'u-photo' ); + } else { + $args['class'][] = 'u-photo'; + $args['class'] = array_unique( $args['class'] ); + } + $args['url'] = $avatar; + $args['class'][] = 'avatar-activitypub'; + } + + return $args; + } + + /** + * Function to retrieve Avatar URL if stored in meta + * + * + * @param int|WP_Comment $comment + * + * @return string $url + */ + public static function get_avatar_url( $comment ) { + if ( is_numeric( $comment ) ) { + $comment = get_comment( $comment ); + } + return get_comment_meta( $comment->comment_ID, 'avatar_url', true ); + } } diff --git a/languages/activitypub.pot b/languages/activitypub.pot index 6ffc55a..5984606 100644 --- a/languages/activitypub.pot +++ b/languages/activitypub.pot @@ -4,7 +4,7 @@ msgid "" msgstr "" "Project-Id-Version: ActivityPub 0.4.3\n" "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/activitypub\n" -"POT-Creation-Date: 2019-02-20 19:25:09+00:00\n" +"POT-Creation-Date: 2019-02-20 20:05:05+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" diff --git a/readme.txt b/readme.txt index 0a86bc7..82d824e 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://notiz.blog/donate/ Tags: OStatus, fediverse, activitypub, activitystream Requires at least: 4.7 Tested up to: 5.1 -Stable tag: 0.4.3 +Stable tag: 0.4.4 Requires PHP: 5.6 License: MIT License URI: http://opensource.org/licenses/MIT @@ -55,6 +55,10 @@ To implement: Project maintained on github at [pfefferle/wordpress-activitypub](https://github.com/pfefferle/wordpress-activitypub). += 0.4.4 = + +* show avatars + = 0.4.3 = * finally fixed backlink in excerpt/summary posts