| 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( self::class, 'insert_post' ), 10, 2 ); |
| 16 |
\add_filter( 'the_content', array( self::class, 'the_content' ), 10, 2 ); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Filter to save #tags as real WordPress tags |
| 22 |
* |
| 23 |
* @param int $id the rev-id |
| 24 |
* @param WP_Post $post the post |
| 25 |
* |
| 26 |
* @return |
| 27 |
*/ |
| 28 |
public static function insert_post( $id, $post ) { |
| 29 |
if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $post->post_content, $match ) ) { |
| 30 |
$tags = \implode( ', ', $match[1] ); |
| 31 |
|
| 32 |
\wp_add_post_tags( $post->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 |
$protected_tags = array(); |
| 47 |
$protect = function( $m ) use ( &$protected_tags ) { |
| 48 |
$c = \wp_rand( 100000, 999999 ); |
| 49 |
$protect = '!#!#PROTECT' . $c . '#!#!'; |
| 50 |
while ( isset( $protected_tags[ $protect ] ) ) { |
| 51 |
$c = \wp_rand( 100000, 999999 ); |
| 52 |
$protect = '!#!#PROTECT' . $c . '#!#!'; |
| 53 |
} |
| 54 |
$protected_tags[ $protect ] = $m[0]; |
| 55 |
return $protect; |
| 56 |
}; |
| 57 |
$the_content = preg_replace_callback( |
| 58 |
'#<!\[CDATA\[.*?\]\]>#is', |
| 59 |
$protect, |
| 60 |
$the_content |
| 61 |
); |
| 62 |
$the_content = preg_replace_callback( |
| 63 |
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is', |
| 64 |
$protect, |
| 65 |
$the_content |
| 66 |
); |
| 67 |
$the_content = preg_replace_callback( |
| 68 |
'#<[^>]+>#i', |
| 69 |
$protect, |
| 70 |
$the_content |
| 71 |
); |
| 72 |
|
| 73 |
$the_content = \preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content ); |
| 74 |
|
| 75 |
$the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content ); |
| 76 |
|
| 77 |
return $the_content; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* A callback for preg_replace to build the term links |
| 82 |
* |
| 83 |
* @param array $result the preg_match results |
| 84 |
* @return string the final string |
| 85 |
*/ |
| 86 |
public static function replace_with_links( $result ) { |
| 87 |
$tag = $result[1]; |
| 88 |
$tag_object = \get_term_by( 'name', $tag, 'post_tag' ); |
| 89 |
|
| 90 |
if ( $tag_object ) { |
| 91 |
$link = \get_term_link( $tag_object, 'post_tag' ); |
| 92 |
return \sprintf( '<a rel="tag" class="hashtag u-tag u-category" href="%s">#%s</a>', $link, $tag ); |
| 93 |
} |
| 94 |
|
| 95 |
return '#' . $tag; |
| 96 |
} |
| 97 |
} |
| 98 |
|