Skip to content

Instantly share code, notes, and snippets.

@beck24
Created May 15, 2012 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beck24/2698771 to your computer and use it in GitHub Desktop.
Save beck24/2698771 to your computer and use it in GitHub Desktop.
embed_extender hooks demo
<?php
function au_landing_init(){
// provide custom video handling for au.teacherstv and slideshare
// these hooks are provided by my latest pull request
// custom_patterns gathers regex patterns to select urls
// custom_embed provides the embed code if a pattern is matched
elgg_register_plugin_hook_handler('embed_extender', 'custom_patterns', 'au_landing_embed_patterns');
elgg_register_plugin_hook_handler('embed_extender', 'custom_embed', 'au_landing_embed');
}
//
// called on the 'embed_extender', 'custom_patterns' hook
// $returnvalue is an array containing currently registered custom patterns
// here we can modify, add, or remove patterns
// for our purposes we're just adding patterns for teacherstv and slideshare
function au_landing_embed_patterns($hook, $type, $returnvalue, $params){
$returnvalue[] = '/(http:\/\/)(au\.teacherstv\.ca\/)(.*)/';
$returnvalue[] = '/(http:\/\/)(www\.slideshare\.net\/)(.*)/';
return $returnvalue;
}
//
// this is the hook for the embed code, this is called when a match has been found
// for one of the patterns. Check if it's one of our patterns and return the correct
// embed code. If there's no match,
function au_landing_embed($hook, $type, $returnvalue, $params){
// get our variables from $params
$url = $params['url'];
$guid = $params['guid'];
$videowidth = $params['videowidth'];
// check if it's a teacherstv url
$domain = au_landing_parse_teacherstv_url($url);
if($domain){
// it is teacherstv, so create the embed code
videoembed_calc_size($videowidth, $videoheight, 425/320, 24);
$embed_object = videoembed_add_css($guid, $videowidth, $videoheight);
$embed_object .= "<div id=\"{$guid}\">";
$embed_object .= "<object width=\"$videowidth\" height=\"$videoheight\"><param name=\"movie\" value=\"{$domain}\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"{$domain}\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"$videowidth\" height=\"$videoheight\"></embed></object>";
$embed_object .= "</div>";
return $embed_object;
}
// not teacherstv, try slideshare
if(strpos($url, 'slideshare.net')){
// it is a slideshare url
videoembed_calc_size($videowidth, $videoheight, 425/320, 24);
$embed_object = videoembed_add_css($guid, $videowidth, $videoheight);
// get embed code
// see http://www.slideshare.net/developers/oembed
// note that slideshare is a special case - we can't tell if the link is a static page or an embed
// as they don't have a standard url format (or it changed at some point)
// but they do provide us with an API to check against
$jsonurl = "http://www.slideshare.net/api/oembed/2?url={$url}&format=json&maxwidth={$videowidth}&maxheight={$videoheight}";
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
if(!empty($json_output->html)){
$embed_object .= "<div id=\"{$guid}\">";
$embed_object .= $json_output->html;
$embed_object .= "</div>";
return $embed_object;
}
// if we can't get embed data just output a link, as slideshare has weird urls
// that don't distinguish well, we can't tell if it's an embed until we check
// with their api. If we got this far, it's been checked and found not to be an embed
return "<a href=\"$url\">$url</a>";
}
// we got this far, it's nothing that concerns us
// return the default value
return $returnvalue;
}
//
// convenience function for creating the embed url from the teacherstv page url
function au_landing_parse_teacherstv_url($url){
// This provides some security against inserting bad content.
// Divides url into http://, www or localization, domain name, path.
if (!preg_match('/(http:\/\/)(au\.teacherstv\.ca\/)(video\/watch\/)(.*)/', $url, $matches)) {
return FALSE;
}
$domain = $matches[1].$matches[2] . 'v/' . $matches[4];
return $domain;
}
elgg_register_event_handler('init', 'system', 'au_landing_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment