PluginProbe
ActivityPub / 0.14.1
ActivityPub v0.14.1
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 0.14.1, at includes/class-hashtag.php

69 lines 1.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 /**
5 * ActivityPub Hashtag Class
6 *
7 * @author Matthias Pfefferle
8 */
9 class Hashtag {
10 /**
11 * Initialize the class, registering WordPress hooks
12 */
13 public static function init() {
14 if ( '1' === \get_option( 'activitypub_use_hashtags', '1' ) ) {
15 \add_filter( 'wp_insert_post', array( '\Activitypub\Hashtag', 'insert_post' ), 99, 2 );
16 \add_filter( 'the_content', array( '\Activitypub\Hashtag', 'the_content' ), 99, 2 );
17 }
18 }
19
20 /**
21 * Filter to save #tags as real WordPress tags
22 *
23 * @param int $id the rev-id
24 * @param array $data the post-data as array
25 *
26 * @return
27 */
28 public static function insert_post( $id, $data ) {
29 if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $data->post_content, $match ) ) {
30 $tags = \implode( ', ', $match[1] );
31
32 \wp_add_post_tags( $data->post_parent, $tags );
33 }
34
35 return $id;
36 }
37
38 /**
39 * Filter to replace the #tags in the content with links
40 *
41 * @param string $the_content the post-content
42 *
43 * @return string the filtered post-content
44 */
45 public static function the_content( $the_content ) {
46 $the_content = \preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content );
47
48 return $the_content;
49 }
50
51 /**
52 * A callback for preg_replace to build the term links
53 *
54 * @param array $result the preg_match results
55 * @return string the final string
56 */
57 public static function replace_with_links( $result ) {
58 $tag = $result[1];
59 $tag_object = \get_term_by( 'name', $tag, 'post_tag' );
60
61 if ( $tag_object ) {
62 $link = \get_term_link( $tag_object, 'post_tag' );
63 return \sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', $link, $tag );
64 }
65
66 return '#' . $tag;
67 }
68 }
69