diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php
index 4385ebe..a4348f5 100644
--- a/includes/class-activity-dispatcher.php
+++ b/includes/class-activity-dispatcher.php
@@ -49,7 +49,7 @@ class Activity_Dispatcher {
}
foreach ( $inboxes as $inbox => $to ) {
- $to = array_unique( $to );
+ $to = array_values( array_unique( $to ) );
$activitypub_activity->set_to( $to );
$activity = $activitypub_activity->to_json();
diff --git a/includes/class-mention.php b/includes/class-mention.php
index 022b033..5bf88c1 100644
--- a/includes/class-mention.php
+++ b/includes/class-mention.php
@@ -16,34 +16,33 @@ class Mention {
}
/**
- * Filter to replace the #tags in the content with links
+ * Filter to replace the mentions in the content with links
*
* @param string $the_content the post-content
*
* @return string the filtered post-content
*/
public static function the_content( $the_content ) {
- $the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content );
+ $the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content );
return $the_content;
}
/**
- * A callback for preg_replace to build the term links
+ * A callback for preg_replace to build the user links
*
* @param array $result the preg_match results
* @return string the final string
*/
public static function replace_with_links( $result ) {
$metadata = \ActivityPub\get_remote_metadata_by_actor( $result[0] );
-
if ( ! is_wp_error( $metadata ) ) {
$username = $metadata['name'];
if ( ! empty( $metadata['preferredUsername'] ) ) {
$username = $metadata['preferredUsername'];
}
$username = '@' . $username . '';
- return \sprintf( '%s', $metadata['url'], $username );
+ return \sprintf( '%s', $metadata['url'], $username );
}
return $result[0];
diff --git a/includes/functions.php b/includes/functions.php
index db1611e..aba5e07 100644
--- a/includes/functions.php
+++ b/includes/functions.php
@@ -122,7 +122,8 @@ function get_remote_metadata_by_actor( $actor ) {
return $actor;
}
- $metadata = \get_transient( 'activitypub_' . $actor );
+ $transient_key = 'activitypub_' . $actor;
+ $metadata = \get_transient( $transient_key );
if ( $metadata ) {
return $metadata;
@@ -153,10 +154,12 @@ function get_remote_metadata_by_actor( $actor ) {
$metadata = \json_decode( $metadata, true );
if ( ! $metadata ) {
- return new \WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), $actor );
+ $metadata = new \WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), $actor );
+ \set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
+ return $metadata;
}
- \set_transient( 'activitypub_' . $actor, $metadata, WEEK_IN_SECONDS );
+ \set_transient( $transient_key, $metadata, WEEK_IN_SECONDS );
return $metadata;
}
diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php
index 9115bcb..5ec4385 100644
--- a/includes/rest/class-webfinger.php
+++ b/includes/rest/class-webfinger.php
@@ -123,6 +123,12 @@ class Webfinger {
if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $account, $m ) ) {
return null;
}
+ $transient_key = 'activitypub_resolve_' . ltrim( $account, '@' );
+
+ $link = \get_transient( $transient_key );
+ if ( $link ) {
+ return $link;
+ }
$url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[1] . '/.well-known/webfinger' );
if ( ! \wp_http_validate_url( $url ) ) {
@@ -139,7 +145,9 @@ class Webfinger {
);
if ( \is_wp_error( $response ) ) {
- return new \WP_Error( 'webfinger_url_not_accessible', null, $url );
+ $link = new \WP_Error( 'webfinger_url_not_accessible', null, $url );
+ \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
+ return $link;
}
$response_code = \wp_remote_retrieve_response_code( $response );
@@ -148,15 +156,20 @@ class Webfinger {
$body = \json_decode( $body, true );
if ( ! isset( $body['links'] ) ) {
- return new \WP_Error( 'webfinger_url_invalid_response', null, $url );
+ $link = new \WP_Error( 'webfinger_url_invalid_response', null, $url );
+ \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
+ return $link;
}
foreach ( $body['links'] as $link ) {
if ( 'self' === $link['rel'] && 'application/activity+json' === $link['type'] ) {
+ \set_transient( $transient_key, $link['href'], WEEK_IN_SECONDS );
return $link['href'];
}
}
- return new \WP_Error( 'webfinger_url_no_activity_pub', null, $body );
+ $link = new \WP_Error( 'webfinger_url_no_activity_pub', null, $body );
+ \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
+ return $link;
}
}
diff --git a/integration/class-friends-feed-parser-activitypub.php b/integration/class-friends-feed-parser-activitypub.php
index 688b03b..00e17b6 100644
--- a/integration/class-friends-feed-parser-activitypub.php
+++ b/integration/class-friends-feed-parser-activitypub.php
@@ -30,6 +30,7 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
\add_action( 'friends_feed_parser_activitypub_unfollow', array( $this, 'unfollow_user' ), 10, 2 );
\add_filter( 'friends_rewrite_incoming_url', array( $this, 'friends_rewrite_incoming_url' ), 10, 2 );
+ \add_filter( 'the_content', array( $this, 'the_content' ), 99, 2 );
\add_filter( 'activitypub_extract_mentions', array( $this, 'activitypub_extract_mentions' ), 10, 2 );
}
@@ -410,14 +411,22 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
}
- public function activitypub_extract_mentions( $mentions, \ActivityPub\Model\Post $post ) {
- $feeds = \Friends\User_Feed::get_by_parser( 'activitypub' );
- $users = array();
- foreach ( $feeds as $feed ) {
- $user = $feed->get_friend_user();
- $slug = sanitize_title( $user->user_nicename );
- $users[ '@' . $slug ] = $feed->get_url();
+ public function get_possible_mentions() {
+ static $users = null;
+ if ( is_null( $users ) ) {
+ $feeds = \Friends\User_Feed::get_by_parser( 'activitypub' );
+ $users = array();
+ foreach ( $feeds as $feed ) {
+ $user = $feed->get_friend_user();
+ $slug = sanitize_title( $user->user_nicename );
+ $users[ '@' . $slug ] = $feed->get_url();
+ }
}
+ return $users;
+ }
+
+ public function activitypub_extract_mentions( $mentions, \ActivityPub\Model\Post $post ) {
+ $users = $this->get_possible_mentions();
preg_match_all( '/@(?:[a-zA-Z0-9_-]+)/', $post->get_content(), $matches );
foreach ( $matches[0] as $match ) {
if ( isset( $users[ $match ] ) ) {
@@ -426,4 +435,20 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
}
return $mentions;
}
+
+
+ public function the_content( $the_content ) {
+ $the_content = \preg_replace_callback( '/@(?:[a-zA-Z0-9_-]+)/', array( $this, 'replace_with_links' ), $the_content );
+
+ return $the_content;
+ }
+
+ public function replace_with_links( $result ) {
+ $users = $this->get_possible_mentions();
+ if ( isset( $users[ $result[0] ] ) ) {
+ return \Activitypub\Mention::replace_with_links( array( $users[ $result[0] ] ) );
+ }
+
+ return $result[0];
+ }
}
diff --git a/tests/test-class-activitypub-mention.php b/tests/test-class-activitypub-mention.php
index b3f8864..274f432 100644
--- a/tests/test-class-activitypub-mention.php
+++ b/tests/test-class-activitypub-mention.php
@@ -19,8 +19,8 @@ class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP {
public function the_content_provider() {
return array(
- array( 'hallo @username@example.org test', 'hallo @username test' ),
- array( 'hallo @pfefferle@notiz.blog test', 'hallo @pfefferle test' ),
+ array( 'hallo @username@example.org test', 'hallo @username test' ),
+ array( 'hallo @pfefferle@notiz.blog test', 'hallo @pfefferle test' ),
);
}