PluginProbe
ActivityPub / 1.0.0
ActivityPub v1.0.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-hashtag.php

class-hashtag.php in ActivityPub 1.0.0, at includes/class-hashtag.php

98 lines 2.5 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( 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