PluginProbe
ActivityPub / 3.2.5
ActivityPub v3.2.5
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-hashtag.php

class-hashtag.php in ActivityPub 3.2.5, at includes/class-hashtag.php

102 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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