| 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 |
|