PluginProbe
ActivityPub / 0.3.0
ActivityPub v0.3.0
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-activitypub-hashtag.php

class-activitypub-hashtag.php in ActivityPub 0.3.0, at includes/class-activitypub-hashtag.php

52 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActivityPub Hashtag Class
4 */
5 class Activitypub_Hashtag {
6 /**
7 * Filter to save #tags as real WordPress tags
8 *
9 * @param int $id the rev-id
10 * @param array $data the post-data as array
11 */
12 public static function insert_post( $id, $data ) {
13 if ( preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $data->post_content, $match ) ) {
14 $tags = implode( ', ', $match[2] );
15
16 wp_add_post_tags( $data->post_parent, $tags );
17 }
18
19 return $id;
20 }
21
22 /**
23 * Filter to replace the #tags in the content with links
24 *
25 * @param string $the_content the post-content
26 */
27 public static function the_content( $the_content ) {
28 $the_content = preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( 'Activitypub_Hashtag', 'replace_with_links' ), $the_content );
29
30 return $the_content;
31 }
32
33 /**
34 * A callback for preg_replace to build the term links
35 *
36 * @param array $result the preg_match results
37 * @return string the final string
38 */
39 public static function replace_with_links( $result ) {
40 $tag = $result[2];
41 $space = $result[1];
42 $tag_object = get_term_by( 'name', $result[2], 'post_tag' );
43
44 if ( $tag_object ) {
45 $link = get_term_link( $tag_object, 'post_tag' );
46 return "$space<a href='$link' rel='tag'>#$tag</a>";
47 }
48
49 return $space . '#' . $tag;
50 }
51 }
52