| 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 |
if ( ! empty( $activity['summary'] ) && is_actor( $activity ) ) { |
| 36 |
$activity['summary'] = self::the_content( $activity['summary'] ); |
| 37 |
} |
| 38 |
|
| 39 |
if ( ! empty( $activity['content'] ) ) { |
| 40 |
$activity['content'] = self::the_content( $activity['content'] ); |
| 41 |
} |
| 42 |
|
| 43 |
return $activity; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Filter to save #tags as real WordPress tags. |
| 48 |
* |
| 49 |
* @param int $post_id Post ID. |
| 50 |
* @param \WP_Post $post Post object. |
| 51 |
*/ |
| 52 |
public static function insert_post( $post_id, $post ) { |
| 53 |
// Check if the post supports ActivityPub. |
| 54 |
if ( ! \post_type_supports( \get_post_type( $post ), 'activitypub' ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Check if the (custom) post supports tags. |
| 59 |
$taxonomies = \get_object_taxonomies( $post ); |
| 60 |
if ( ! in_array( 'post_tag', $taxonomies, true ) ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$tags = array(); |
| 65 |
|
| 66 |
// Skip hashtags in HTML attributes, like hex colors. |
| 67 |
$content = wp_strip_all_tags( $post->post_content . "\n" . $post->post_excerpt ); |
| 68 |
|
| 69 |
if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $content, $match ) ) { |
| 70 |
$tags = array_unique( $match[1] ); |
| 71 |
} |
| 72 |
|
| 73 |
\wp_add_post_tags( $post->ID, \implode( ', ', $tags ) ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Filter to replace the #tags in the content with links. |
| 78 |
* |
| 79 |
* @param string $the_content The post content. |
| 80 |
* |
| 81 |
* @return string The filtered post content. |
| 82 |
*/ |
| 83 |
public static function the_content( $the_content ) { |
| 84 |
return enrich_content_data( $the_content, '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( self::class, 'replace_with_links' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* A callback for preg_replace to build the term links. |
| 89 |
* |
| 90 |
* @param array $result The preg_match results. |
| 91 |
* @return string the final string |
| 92 |
*/ |
| 93 |
public static function replace_with_links( $result ) { |
| 94 |
$tag = $result[1]; |
| 95 |
$tag_object = \get_term_by( 'name', $tag, 'post_tag' ); |
| 96 |
if ( ! $tag_object ) { |
| 97 |
$tag_object = \get_term_by( 'name', $tag, 'category' ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( $tag_object ) { |
| 101 |
$link = \get_term_link( $tag_object, 'post_tag' ); |
| 102 |
return \sprintf( '<a rel="tag" class="hashtag u-tag u-category" href="%s">#%s</a>', esc_url( $link ), $tag ); |
| 103 |
} |
| 104 |
|
| 105 |
return '#' . $tag; |
| 106 |
} |
| 107 |
} |
| 108 |
|