YouTube API Key
HTML;
}
yourls_add_filter( 'shunt_get_remote_title', 'youtube_title_fix_get_remote_title' );
function youtube_title_fix_get_remote_title( $return , $url ) {
$url = yourls_sanitize_url( $url );
// only deal with http(s)
if ( !in_array( yourls_get_protocol( $url ), array( 'http://', 'https://' ) ) )
return 'PROTOCOL';
// parse url and check host + querry string
$parsed_url = parse_url( $url );
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$query = isset($parsed_url['query']) ? $parsed_url['query'] : '';
parse_str( $query , $array );
if ( preg_match( '/(youtube|youtu\.be)/', $host ) && isset( $array['v'] ) ) {
// we need an API key
$API_KEY = yourls_get_option( 'youtube_title_fix_api_key' );
if( $API_KEY ) {
$vid = $array['v'];
$data = json_decode( file_get_contents( "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" . $vid ."&key=" . $API_KEY ) );
$title = $data->items[0]->snippet->title;
return $title;
}
}
return false;
}