| 1 |
<?php |
| 2 |
/** |
| 3 |
* Hashtag Class. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub; |
| 9 |
|
| 10 |
/** |
| 11 |
* ActivityPub Hashtag Class. |
| 12 |
* |
| 13 |
* @author Matthias Pfefferle |
| 14 |
*/ |
| 15 |
class Hashtag { |
| 16 |
/** |
| 17 |
* Initialize the class, registering WordPress hooks. |
| 18 |
*/ |
| 19 |
public static function init() { |
| 20 |
if ( '1' === \get_option( 'activitypub_use_hashtags', '1' ) ) { |
| 21 |
\add_action( 'wp_insert_post', array( self::class, 'insert_post' ), 10, 2 ); |
| 22 |
\add_filter( 'the_content', array( self::class, 'the_content' ) ); |
| 23 |
\add_filter( 'activitypub_activity_object_array', array( self::class, 'filter_activity_object' ), 99 ); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Filter only the activity object and replace summery it with URLs. |
| 29 |
* |
| 30 |
* @param array $activity The activity object array. |
| 31 |
* |
| 32 |
* @return array The filtered activity object array. |
| 33 |
*/ |
| 34 |
public static function filter_activity_object( $activity ) { |
| 35 |
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
| 36 |
Only changed it for Person and Group as long is not merged: https://github.com/mastodon/mastodon/pull/28629 |
| 37 |
*/ |
| 38 |
if ( ! empty( $activity['summary'] ) && in_array( $activity['type'], array( 'Person', 'Group' ), true ) ) { |
| 39 |
$activity['summary'] = self::the_content( $activity['summary'] ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( ! empty( $activity['content'] ) ) { |
| 43 |
$activity['content'] = self::the_content( $activity['content'] ); |
| 44 |
} |
| 45 |
|
| 46 |
return $activity; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Filter to save #tags as real WordPress tags. |
| 51 |
* |
| 52 |
* @param int $post_id Post ID. |
| 53 |
* @param \WP_Post $post Post object. |
| 54 |
*/ |
| 55 |
public static function insert_post( $post_id, $post ) { |
| 56 |
$tags = array(); |
| 57 |
|
| 58 |
if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $post->post_content, $match ) ) { |
| 59 |
$tags = array_merge( $tags, $match[1] ); |
| 60 |
} |
| 61 |
|
| 62 |
if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $post->post_excerpt, $match ) ) { |
| 63 |
$tags = array_merge( $tags, $match[1] ); |
| 64 |
} |
| 65 |
|
| 66 |
$tags = \implode( ', ', $tags ); |
| 67 |
|
| 68 |
\wp_add_post_tags( $post->ID, $tags ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Filter to replace the #tags in the content with links. |
| 73 |
* |
| 74 |
* @param string $the_content The post content. |
| 75 |
* |
| 76 |
* @return string The filtered post content. |
| 77 |
*/ |
| 78 |
public static function the_content( $the_content ) { |
| 79 |
return enrich_content_data( $the_content, '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( self::class, 'replace_with_links' ) ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* A callback for preg_replace to build the term links. |
| 84 |
* |
| 85 |
* @param array $result The preg_match results. |
| 86 |
* @return string the final string |
| 87 |
*/ |
| 88 |
public static function replace_with_links( $result ) { |
| 89 |
$tag = $result[1]; |
| 90 |
$tag_object = \get_term_by( 'name', $tag, 'post_tag' ); |
| 91 |
if ( ! $tag_object ) { |
| 92 |
$tag_object = \get_term_by( 'name', $tag, 'category' ); |
| 93 |
} |
| 94 |
|
| 95 |
if ( $tag_object ) { |
| 96 |
$link = \get_term_link( $tag_object, 'post_tag' ); |
| 97 |
return \sprintf( '<a rel="tag" class="hashtag u-tag u-category" href="%s">#%s</a>', esc_url( $link ), $tag ); |
| 98 |
} |
| 99 |
|
| 100 |
return '#' . $tag; |
| 101 |
} |
| 102 |
} |
| 103 |
|