basic hashtag support

see #21 #20
This commit is contained in:
Matthias Pfefferle 2019-01-22 20:51:22 +01:00
parent e3dcf6cc91
commit 03f6da019d
5 changed files with 68 additions and 3 deletions

View file

@ -3,7 +3,7 @@
**Donate link:** https://notiz.blog/donate/
**Tags:** OStatus, fediverse, activitypub, activitystream
**Requires at least:** 4.7
**Tested up to:** 5.0.2
**Tested up to:** 5.0.3
**Stable tag:** 0.2.1
**Requires PHP:** 5.6
**License:** MIT
@ -55,6 +55,10 @@ To implement:
Project maintained on github at [pfefferle/wordpress-activitypub](https://github.com/pfefferle/wordpress-activitypub).
### 0.3.0 ###
* basic hashtag support
### 0.2.1 ###
* customizable backlink (permalink or shorturl)

View file

@ -16,6 +16,8 @@
* Initialize plugin
*/
function activitypub_init() {
defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(^|\s|>)#([^\s<>]+)\b' );
require_once dirname( __FILE__ ) . '/includes/class-activitypub-signature.php';
require_once dirname( __FILE__ ) . '/includes/class-activitypub-post.php';
require_once dirname( __FILE__ ) . '/includes/class-activitypub-activity.php';
@ -65,6 +67,10 @@ function activitypub_init() {
add_action( 'admin_menu', array( 'Activitypub_Admin', 'admin_menu' ) );
add_action( 'admin_init', array( 'Activitypub_Admin', 'register_settings' ) );
add_action( 'show_user_profile', array( 'Activitypub_Admin', 'add_fediverse_profile' ) );
require_once dirname( __FILE__ ) . '/includes/class-activitypub-hashtag.php';
add_filter( 'wp_insert_post', array( 'Activitypub_Hashtag', 'insert_post' ), 99, 2 );
add_filter( 'the_content', array( 'Activitypub_Hashtag', 'the_content' ), 99, 2 );
}
add_action( 'plugins_loaded', 'activitypub_init' );

View file

@ -0,0 +1,51 @@
<?php
/**
* ActivityPub Hashtag Class
*/
class Activitypub_Hashtag {
/**
* Filter to save #tags as real WordPress tags
*
* @param int $id the rev-id
* @param array $data the post-data as array
*/
public static function insert_post( $id, $data ) {
if ( preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $data->post_content, $match ) ) {
$tags = implode( ', ', $match[2] );
wp_add_post_tags( $data->post_parent, $tags );
}
return $id;
}
/**
* Filter to replace the #tags in the content with links
*
* @param string $the_content the post-content
*/
public static function the_content( $the_content ) {
$the_content = preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( 'Activitypub_Hashtag', 'replace_with_links' ), $the_content );
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 ) {
$tag = $result[2];
$space = $result[1];
$tag_object = get_term_by( 'name', $result[2], 'post_tag' );
if ( $tag_object ) {
$link = get_term_link( $tag_object, 'post_tag' );
return "$space<a href='$link' rel='tag'>#$tag</a>";
}
return $space . '#' . $tag;
}
}

View file

@ -4,7 +4,7 @@ msgid ""
msgstr ""
"Project-Id-Version: ActivityPub 0.2.1\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/activitypub\n"
"POT-Creation-Date: 2019-01-16 20:52:51+00:00\n"
"POT-Creation-Date: 2019-01-22 19:51:00+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

View file

@ -3,7 +3,7 @@ Contributors: pfefferle
Donate link: https://notiz.blog/donate/
Tags: OStatus, fediverse, activitypub, activitystream
Requires at least: 4.7
Tested up to: 5.0.2
Tested up to: 5.0.3
Stable tag: 0.2.1
Requires PHP: 5.6
License: MIT
@ -55,6 +55,10 @@ To implement:
Project maintained on github at [pfefferle/wordpress-activitypub](https://github.com/pfefferle/wordpress-activitypub).
= 0.3.0 =
* basic hashtag support
= 0.2.1 =
* customizable backlink (permalink or shorturl)