Skip to content

Commit

Permalink
Use incoming tags as WordPress tags so that you can filter incoming p…
Browse files Browse the repository at this point in the history
…osts by tag
  • Loading branch information
akirk committed May 31, 2023
1 parent 2169ac7 commit 188ce66
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
40 changes: 40 additions & 0 deletions feed-parsers/class-feed-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public function __set( $key, $value ) {
$value = $this->validate_post_status( $value );
break;

case 'tags':
$value = $this->validate_tags( $value );
break;

case 'date':
$value = $this->validate_date( $value, 'invalid-date' );
break;
Expand Down Expand Up @@ -293,6 +297,42 @@ public function validate_post_status( $status ) {
return $status;
}

/**
* Validate a given post status.
*
* @param string|array $tags The tags.
*
* @return string|\WP_Error The validated status.
*/
public function validate_tags( $tags ) {
if ( ! is_array( $tags ) ) {
if ( ! is_string( $tags ) ) {
return new \WP_Error( 'invalid-tags', 'The tags need to be a hashtag string or an array.' );
}
if ( false !== strpos( $tags, '#' ) ) {
$tags = explode( '#', $tags );
} elseif ( false !== strpos( $tags, ',' ) ) {
$tags = explode( ',', $tags );
} else {
$tags = array( $tags );
}
}

$validated_tags = array();
foreach ( $tags as $tag ) {
if ( ! is_string( $tag ) ) {
continue;
}
$tag = mb_substr( trim( ltrim( trim( $tag ), '#,' ) ), 0, 50 );
if ( ! $tag ) {
continue;
}
$validated_tags[ $tag ] = true;
}

return array_keys( $validated_tags );
}

/**
* Determines if the item is new.
*
Expand Down
12 changes: 12 additions & 0 deletions includes/class-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private function register_hooks() {
add_filter( 'pre_option_rss_use_excerpt', array( $this, 'feed_use_excerpt' ), 90 );
add_filter( 'friends_early_modify_feed_item', array( $this, 'apply_early_feed_rules' ), 10, 3 );
add_filter( 'friends_modify_feed_item', array( $this, 'apply_feed_rules' ), 10, 3 );
add_filter( 'friends_modify_feed_item', array( $this, 'extract_tags' ), 10, 3 );

add_action( 'rss_item', array( $this, 'feed_additional_fields' ) );
add_action( 'rss2_item', array( $this, 'feed_additional_fields' ) );
Expand Down Expand Up @@ -441,6 +442,16 @@ public function validate_feed_catch_all( $catch_all ) {
return $catch_all;
}

public function extract_tags( $item, User_Feed $feed = null, User $friend_user = null ) {
// This is a variation of the ACTIVITYPUB_HASHTAGS_REGEXP.
if ( \preg_match_all( '/(?:(?<=\s)|(?<=<p>)|(?<=<br\s*/?>)|^)#(\w+)(?:(?=\s|[[:punct:]]|$))/ui', \wp_strip_all_tags( $item->post_content ), $match ) ) {
$tags = \implode( ', ', $match[1] );
$item->tags = $tags;
}

return $item;
}

/**
* Gets the notification keywords.
*
Expand Down Expand Up @@ -627,6 +638,7 @@ public function process_incoming_feed_items( array $items, User_Feed $user_feed
$post_data['post_date_gmt'] = $item->date;
$post_data['post_content'] = str_replace( '\\', '\\\\', $post_data['post_content'] );
$post_data['meta_input'] = $item->meta;
$post_data['tags_input'] = $item->tags;

$post_id = $friend_user->insert_post( $post_data, true );
if ( is_wp_error( $post_id ) ) {
Expand Down

0 comments on commit 188ce66

Please sign in to comment.