| 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_action( 'wp_insert_post', array( self::class, 'insert_post' ), 10, 2 ); |
| 16 |
\add_filter( 'the_content', array( self::class, 'the_content' ), 10, 1 ); |
| 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 |
// small protection against execution timeouts: limit to 1 MB |
| 47 |
if ( mb_strlen( $the_content ) > MB_IN_BYTES ) { |
| 48 |
return $the_content; |
| 49 |
} |
| 50 |
$tag_stack = array(); |
| 51 |
$protected_tags = array( |
| 52 |
'pre', |
| 53 |
'code', |
| 54 |
'textarea', |
| 55 |
'style', |
| 56 |
'a', |
| 57 |
); |
| 58 |
$content_with_links = ''; |
| 59 |
$in_protected_tag = false; |
| 60 |
foreach ( wp_html_split( $the_content ) as $chunk ) { |
| 61 |
if ( preg_match( '#^<!--[\s\S]*-->$#i', $chunk, $m ) ) { |
| 62 |
$content_with_links .= $chunk; |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
if ( preg_match( '#^<(/)?([a-z-]+)\b[^>]*>$#i', $chunk, $m ) ) { |
| 67 |
$tag = strtolower( $m[2] ); |
| 68 |
if ( '/' === $m[1] ) { |
| 69 |
// Closing tag. |
| 70 |
$i = array_search( $tag, $tag_stack, true ); |
| 71 |
// We can only remove the tag from the stack if it is in the stack. |
| 72 |
if ( false !== $i ) { |
| 73 |
$tag_stack = array_slice( $tag_stack, 0, $i ); |
| 74 |
} |
| 75 |
} else { |
| 76 |
// Opening tag, add it to the stack. |
| 77 |
$tag_stack[] = $tag; |
| 78 |
} |
| 79 |
|
| 80 |
// If we're in a protected tag, the tag_stack contains at least one protected tag string. |
| 81 |
// The protected tag state can only change when we encounter a start or end tag. |
| 82 |
$in_protected_tag = array_intersect( $tag_stack, $protected_tags ); |
| 83 |
|
| 84 |
// Never inspect tags. |
| 85 |
$content_with_links .= $chunk; |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
if ( $in_protected_tag ) { |
| 90 |
// Don't inspect a chunk inside an inspected tag. |
| 91 |
$content_with_links .= $chunk; |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
// Only reachable when there is no protected tag in the stack. |
| 96 |
$content_with_links .= \preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $chunk ); |
| 97 |
} |
| 98 |
|
| 99 |
return $content_with_links; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* A callback for preg_replace to build the term links |
| 104 |
* |
| 105 |
* @param array $result the preg_match results |
| 106 |
* @return string the final string |
| 107 |
*/ |
| 108 |
public static function replace_with_links( $result ) { |
| 109 |
$tag = $result[1]; |
| 110 |
$tag_object = \get_term_by( 'name', $tag, 'post_tag' ); |
| 111 |
|
| 112 |
if ( $tag_object ) { |
| 113 |
$link = \get_term_link( $tag_object, 'post_tag' ); |
| 114 |
return \sprintf( '<a rel="tag" class="hashtag u-tag u-category" href="%s">#%s</a>', $link, $tag ); |
| 115 |
} |
| 116 |
|
| 117 |
return '#' . $tag; |
| 118 |
} |
| 119 |
} |
| 120 |
|