wordpress-activitypub/includes/class-hashtag.php

94 lines
2.4 KiB
PHP
Raw Normal View History

2019-01-22 20:51:22 +01:00
<?php
namespace Activitypub;
2019-01-22 20:51:22 +01:00
/**
* ActivityPub Hashtag Class
2019-02-24 13:01:28 +01:00
*
* @author Matthias Pfefferle
2019-01-22 20:51:22 +01:00
*/
class Hashtag {
2019-02-24 13:01:28 +01:00
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
2019-09-27 10:12:59 +02:00
if ( '1' === \get_option( 'activitypub_use_hashtags', '1' ) ) {
2023-01-16 20:28:45 +01:00
\add_filter( 'wp_insert_post', array( '\Activitypub\Hashtag', 'insert_post' ), 10, 2 );
\add_filter( 'the_content', array( '\Activitypub\Hashtag', 'the_content' ), 10, 2 );
2019-03-02 21:10:42 +01:00
}
}
2019-01-22 20:51:22 +01:00
/**
* Filter to save #tags as real WordPress tags
*
2023-01-27 16:50:04 +01:00
* @param int $id the rev-id
2022-12-09 09:28:00 +01:00
* @param WP_Post $post the post
2019-03-12 22:18:20 +01:00
*
* @return
2019-01-22 20:51:22 +01:00
*/
2022-12-09 09:28:00 +01:00
public static function insert_post( $id, $post ) {
if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $post->post_content, $match ) ) {
2019-09-27 10:12:59 +02:00
$tags = \implode( ', ', $match[1] );
2019-01-22 20:51:22 +01:00
2022-12-09 09:28:00 +01:00
\wp_add_post_tags( $post->post_parent, $tags );
2019-01-22 20:51:22 +01:00
}
return $id;
}
/**
* Filter to replace the #tags in the content with links
*
* @param string $the_content the post-content
2019-03-12 22:18:20 +01:00
*
* @return string the filtered post-content
2019-01-22 20:51:22 +01:00
*/
public static function the_content( $the_content ) {
2023-01-27 12:13:41 +01:00
$protected_tags = array();
2023-01-27 16:59:15 +01:00
$protect = function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
};
$the_content = preg_replace_callback(
2023-01-27 17:23:25 +01:00
'#<!\[CDATA\[.*?\]\]>#is',
2023-01-27 16:59:15 +01:00
$protect,
$the_content
);
2023-01-27 12:13:41 +01:00
$the_content = preg_replace_callback(
2023-01-27 16:55:52 +01:00
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
2023-01-27 16:59:15 +01:00
$protect,
2023-01-27 16:50:04 +01:00
$the_content
);
$the_content = preg_replace_callback(
2023-01-27 12:13:41 +01:00
'#<[^>]+>#i',
2023-01-27 16:59:15 +01:00
$protect,
2023-01-27 12:13:41 +01:00
$the_content
);
2019-09-27 10:12:59 +02:00
$the_content = \preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content );
2019-01-22 20:51:22 +01:00
2023-01-27 17:23:25 +01:00
$the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content );
2023-01-27 12:13:41 +01:00
2019-01-22 20:51:22 +01:00
return $the_content;
}
/**
* A callback for preg_replace to build the term links
*
* @param array $result the preg_match results
* @return string the final string
*/
public static function replace_with_links( $result ) {
2019-08-18 22:16:59 +02:00
$tag = $result[1];
2019-09-27 10:12:59 +02:00
$tag_object = \get_term_by( 'name', $tag, 'post_tag' );
2019-01-22 20:51:22 +01:00
if ( $tag_object ) {
2019-09-27 10:12:59 +02:00
$link = \get_term_link( $tag_object, 'post_tag' );
return \sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', $link, $tag );
2019-01-22 20:51:22 +01:00
}
2019-08-18 22:16:59 +02:00
return '#' . $tag;
2019-01-22 20:51:22 +01:00
}
}