diff --git a/functions.php b/functions.php index daeceb6..9f52a50 100644 --- a/functions.php +++ b/functions.php @@ -212,11 +212,11 @@ function load_mastodon_js(){ global $helpers; $bot_tweets_html = get_post_meta( $post->ID, 'bot_tweets_html', true ); - $known_mastodon_instances = $helpers->get_known_mastodon_instances(); + $urls = wp_extract_urls($bot_tweets_html); - foreach ( $known_mastodon_instances as $instance ){ - if ( strpos( $bot_tweets_html, $instance ) !== false ){ - echo $helpers->get_mastodon_embed_script_tag($instance); + foreach ( $urls as $url ){ + if ( $helpers->is_mastodon_instance($url) ){ + echo $helpers->get_mastodon_embed_script_tag($helpers->get_domain_from_url($url)); break; } } diff --git a/includes/helpers.php b/includes/helpers.php index 2a87cce..698414c 100644 --- a/includes/helpers.php +++ b/includes/helpers.php @@ -336,6 +336,22 @@ function get_fediverse_url( $handle ){ return $fediverse_url; } + function is_mastodon_instance($url){ + $domain = $this->get_domain_from_url( $url ); + if (in_array($domain, $this->get_known_mastodon_instances()) ){ + return true; + } + + $response = wp_remote_get( "https://$domain/api/v2/instance" ); + $response_code = wp_remote_retrieve_response_code( $response ); + + if ($response_code === 200){ + return true; + } + + return false; + } + function get_known_mastodon_instances(){ // TODO: Move these to a settings page for easier maintanance. diff --git a/includes/post-types/bots.php b/includes/post-types/bots.php index e2dafba..710dc92 100644 --- a/includes/post-types/bots.php +++ b/includes/post-types/bots.php @@ -699,17 +699,11 @@ function save_meta( $post_id ) { // $bot_tweets_html .= $data->html; } 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 .= '
'; - break; - } - } - + global $helpers; + if ( $helpers->is_mastodon_instance($tweet_url) ){ + $bot_tweets_html .= '
'; + break; + } } }