Extract mentions from the unmodified post content.

This commit is contained in:
Alex Kirk 2022-12-09 18:41:26 +01:00
parent 99b316db34
commit 483e0a85b2
3 changed files with 44 additions and 6 deletions

View file

@ -48,8 +48,15 @@ class Mention {
return $result[0];
}
public static function extract_mentions( $mentions, \ActivityPub\Model\Post $post ) {
\preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post->get_content(), $matches );
/**
* Extract the mentions from the post_content.
*
* @param array $mentions The already found mentions.
* @param string $post_content The post content.
* @return mixed The discovered mentions.
*/
public static function extract_mentions( $mentions, $post_content ) {
\preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches );
foreach ( $matches[0] as $match ) {
$link = \Activitypub\Rest\Webfinger::resolve( $match );
if ( ! is_wp_error( $link ) ) {

View file

@ -278,7 +278,7 @@ class Post {
}
}
$mentions = apply_filters( 'activitypub_extract_mentions', array(), $this );
$mentions = apply_filters( 'activitypub_extract_mentions', array(), $this->post->post_content, $this );
if ( $mentions ) {
foreach ( $mentions as $mention => $url ) {
$tag = array(
@ -456,4 +456,28 @@ class Post {
return $content;
}
/**
* Adds all tags as hashtags to the post/summary content
*
* @param string $content
* @param WP_Post $post
*
* @return string
*/
public function get_the_mentions() {
$post = $this->post;
$tags = \get_the_tags( $post->ID );
if ( ! $tags ) {
return '';
}
$hash_tags = array();
foreach ( $tags as $tag ) {
$hash_tags[] = \sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', \get_tag_link( $tag ), $tag->slug );
}
return \implode( ' ', $hash_tags );
}
}

View file

@ -413,7 +413,7 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
public function get_possible_mentions() {
static $users = null;
if ( is_null( $users ) ) {
if ( is_null( $users ) || true ) {
$feeds = \Friends\User_Feed::get_by_parser( 'activitypub' );
$users = array();
foreach ( $feeds as $feed ) {
@ -425,9 +425,16 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
return $users;
}
public function activitypub_extract_mentions( $mentions, \ActivityPub\Model\Post $post ) {
/**
* Extract the mentions from the post_content.
*
* @param array $mentions The already found mentions.
* @param string $post_content The post content.
* @return mixed The discovered mentions.
*/
public function activitypub_extract_mentions( $mentions, $post_content ) {
$users = $this->get_possible_mentions();
preg_match_all( '/@(?:[a-zA-Z0-9_-]+)/', $post->get_content(), $matches );
preg_match_all( '/@(?:[a-zA-Z0-9_-]+)/', $post_content, $matches );
foreach ( $matches[0] as $match ) {
if ( isset( $users[ $match ] ) ) {
$mentions[ $match ] = $users[ $match ];