Created
April 22, 2017 05:51
-
-
Save y-krn/9aeae0609e035479e937d4895a681701 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
new Mstdn_Link_Builder(); | |
/** | |
* Class Name: Mstdn_Link_Builder | |
*@author @ottanxyz | |
*/ | |
class Mstdn_Link_Builder { | |
function __construct() { | |
wp_embed_register_handler( | |
'mstdn_link_builder', | |
'/^(https:\\/\\/mstdn\\.jp\\/@([a-zA-Z0-9]+)\\/[0-9]+)$/i', | |
array( &$this, 'handler' ) | |
); | |
add_shortcode( 'mstdn', array( &$this, 'display' ) ); | |
} | |
public function handler( $matches, $attr, $url, $rawattr ) { | |
$response = wp_remote_get( $url ); | |
$link = $response['headers']['link']; | |
preg_match( '/([0-9]+)\.atom/i' , $link, $code ); | |
$user = $matches[2]; | |
return "[mstdn user=\"${user}\" code=\"${code[1]}\"]"; | |
} | |
public function display( $p ) { | |
if ( ! isset( $p['code'] ) || ! $p['code'] ) { | |
return; | |
} | |
if ( ! isset( $p['user'] ) || ! $p['user'] ) { | |
return; | |
} | |
echo '<iframe src="https://mstdn.jp/users/' . $p['user'] . '/updates/' . $p['code'] . '/embed" style="width:100%;overflow:hidden" frameborder=0 scrolling="no"></iframe>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment