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

97 lines 2.6 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 if ( '1' === get_option( 'activitypub_add_tags_as_hashtags', '0' ) ) {
19 add_filter( 'activitypub_the_summary', array( '\Activitypub\Hashtag', 'add_hashtags_to_content' ), 10, 2 );
20 add_filter( 'activitypub_the_content', array( '\Activitypub\Hashtag', 'add_hashtags_to_content' ), 10, 2 );
21 }
22 }
23
24 /**
25 * Filter to save #tags as real WordPress tags
26 *
27 * @param int $id the rev-id
28 * @param array $data the post-data as array
29 *
30 * @return
31 */
32 public static function insert_post( $id, $data ) {
33 if ( preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $data->post_content, $match ) ) {
34 $tags = implode( ', ', $match[1] );
35
36 wp_add_post_tags( $data->post_parent, $tags );
37 }
38
39 return $id;
40 }
41
42 /**
43 * Filter to replace the #tags in the content with links
44 *
45 * @param string $the_content the post-content
46 *
47 * @return string the filtered post-content
48 */
49 public static function the_content( $the_content ) {
50 $the_content = preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content );
51
52 return $the_content;
53 }
54
55 /**
56 * A callback for preg_replace to build the term links
57 *
58 * @param array $result the preg_match results
59 * @return string the final string
60 */
61 public static function replace_with_links( $result ) {
62 $tag = $result[1];
63 $tag_object = get_term_by( 'name', $tag, 'post_tag' );
64
65 if ( $tag_object ) {
66 $link = get_term_link( $tag_object, 'post_tag' );
67 return sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', $link, $tag );
68 }
69
70 return '#' . $tag;
71 }
72
73 /**
74 * Adds all tags as hashtags to the post/summary content
75 *
76 * @param string $content
77 * @param WP_Post $post
78 *
79 * @return string
80 */
81 public static function add_hashtags_to_content( $content, $post ) {
82 $tags = get_the_tags( $post->ID );
83
84 if ( ! $tags ) {
85 return $content;
86 }
87
88 $hash_tags = array();
89
90 foreach ( $tags as $tag ) {
91 $hash_tags[] = sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', get_tag_link( $tag ), $tag->slug );
92 }
93
94 return $content . '<p>' . implode( ' ', $hash_tags ) . '</p>';
95 }
96 }
97