diff --git a/includes/admin/helpers/class-rop-bluesky-api.php b/includes/admin/helpers/class-rop-bluesky-api.php index da2cd5665..34a1a3042 100644 --- a/includes/admin/helpers/class-rop-bluesky-api.php +++ b/includes/admin/helpers/class-rop-bluesky-api.php @@ -290,12 +290,19 @@ public function create_post( $did, $post, $post_type, $hashtags, $access_token = $now = gmdate( 'Y-m-d\TH:i:s\Z' ); + $text = $post['content'] . $hashtags; + $record = array( '$type' => 'app.bsky.feed.post', - 'text' => $post['content'] . $hashtags, + 'text' => $text, 'createdAt' => $now, ); + $facets = $this->generate_facets( $text ); + if ( ! empty( $facets ) ) { + $record['facets'] = $facets; + } + if ( $post_type === 'link' && isset( $post['post_url'] ) && ! empty( $post['post_url'] ) ) { $record['embed'] = array( '$type' => 'app.bsky.embed.external', @@ -369,6 +376,41 @@ public function create_post( $did, $post, $post_type, $hashtags, $access_token = } } + /** + * Generate facets for hashtags. + * + * @param string $text The text to parse. + * + * @return mixed|array + */ + private function generate_facets( $text ) { + $facets = array(); + preg_match_all( '/#\S+/', $text, $matches, PREG_OFFSET_CAPTURE ); + + if ( ! empty( $matches[0] ) ) { + foreach ( $matches[0] as $match ) { + $hashtag = $match[0]; + $start = $match[1]; + $end = $start + strlen( $hashtag ); + + $facets[] = array( + 'index' => array( + 'byteStart' => $start, + 'byteEnd' => $end, + ), + 'features' => array( + array( + '$type' => 'app.bsky.richtext.facet#tag', + 'tag' => ltrim( $hashtag, '#' ), + ), + ), + ); + } + } + + return $facets; + } + /** * Fetch Website Card embeds. *