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