Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion includes/admin/helpers/class-rop-bluesky-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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.
*
Expand Down
Loading