Skip to content

Commit

Permalink
Cleaned up fediverse embeds.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbohacek committed Dec 9, 2022
1 parent dc9196e commit a19dacd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
17 changes: 6 additions & 11 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,15 @@ function load_social_media_embed_js(){

function load_mastodon_js(){
global $post;
$bot_tweets_html = get_post_meta( $post->ID, 'bot_tweets_html', true );
global $helpers;

$known_mastodon_instances = [
'botsin.space',
'mastodon.social',
'beeping.town',
'mastodon.archive.org',
];
$bot_tweets_html = get_post_meta( $post->ID, 'bot_tweets_html', true );
$known_mastodon_instances = $helpers->get_known_mastodon_instances();

foreach ( $known_mastodon_instances as $instance ){
if ( strpos( $bot_tweets_html, $instance ) !== false ){ ?>
<script src="https://$instance/embed.js" async="async"></script>
<?php
break;
if ( strpos( $bot_tweets_html, $instance ) !== false ){
echo $helpers->get_mastodon_embed_script_tag($instance);
break;
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ function get_screenshotable_url( $urls ){

return $screenshotable_url;
}


function get_known_mastodon_instances(){
// TODO: Move these to a settings page for easier maintanance.

return [
'botsin.space',
'mastodon.social',
'beeping.town',
'fedibot.dev',
'mastodon.archive.org',
];

}

function get_mastodon_embed_script_tag( $instance ){
return '<script src="https://' . $instance . '/embed.js" async="async"></script>';
}

}

$helpers = new BW_Helpers();
17 changes: 12 additions & 5 deletions includes/post-types/bots.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,18 @@ function save_meta( $post_id ) {
// $data = json_decode($json_post);
// $bot_tweets_html .= $data->html;
}
elseif ( strpos( $tweet_url, 'botsin.space/' ) !== false ||
strpos( $tweet_url, 'beeping.town/' ) !== false ||
strpos( $tweet_url, 'fedibot.dev/' ) !== false ||
strpos( $tweet_url, 'mastodon.social/' ) !== false ) {
$bot_tweets_html .= '<blockquote><iframe src="' . $tweet_url . '/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400"></iframe></blockquote>';
else {

global $helpers;
$known_mastodon_instances = $helpers->get_known_mastodon_instances();

foreach ( $known_mastodon_instances as $instance ){
if ( strpos( $tweet_url, $instance ) !== false ){
$bot_tweets_html .= '<blockquote><iframe src="' . $tweet_url . '/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400"></iframe></blockquote>';
break;
}
}

}
}

Expand Down

0 comments on commit a19dacd

Please sign in to comment.