Skip to content

Instantly share code, notes, and snippets.

@Otto42
Last active December 18, 2015 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Otto42/5854277 to your computer and use it in GitHub Desktop.
Save Otto42/5854277 to your computer and use it in GitHub Desktop.
WordPress embed handler for shows from cbs.com
<?php
function cbscom_embed_register_handler() {
wp_embed_register_handler( 'cbscom', '|https?://www.cbs.com/shows/.*|i', 'cbscom_embed_handler' );
}
add_action( 'init', 'cbscom_embed_register_handler' );
function cbscom_embed_handler( $matches, $attr, $url, $rawattr ) {
global $post, $wp_embed;
// no post, no worky
if ( empty($post) ) {
return $wp_embed->maybe_make_link( $url );
}
// we use this cache key for our metadata because WP itself handles clearing it on post saves as part of oembed
$cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
$ret = get_post_meta( $post->ID, $cachekey, true );
// Failures are cached
if ( '{{unknown}}' === $ret )
return $wp_embed->maybe_make_link( $url );
// return early, no need to redo all this work
if ($ret) {
return $ret;
}
// get the html from cbs.com
$response = wp_remote_get($url);
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
update_post_meta( $post->ID, $cachekey, '{{unknown}}' );
return $wp_embed->maybe_make_link( $url );
}
$html = wp_remote_retrieve_body($response);
// find the og:video tag
preg_match('|<meta.*?og:video.*?>|i',$html, $m);
// parse it
if ( !empty($m) ) {
$meta = $m[0];
if ( preg_match_all('/<meta(.+?)>/', $meta, $m) ) {
foreach ($m[1] as $match) {
foreach ( wp_kses_hair($match, array('http')) as $a)
$info[$a['name']] = $a['value'];
}
}
// get the content attribute
if ( !empty( $info['content'] ) ) {
$parsed_url = $info['content'];
}
}
// no content, give up
if ( empty( $parsed_url ) ) {
update_post_meta( $post->ID, $cachekey, '{{unknown}}' );
return $wp_embed->maybe_make_link( $url );
}
// fix the url to look like their normal embed code
$parsed_url = remove_query_arg( array('partner','autoPlayVid'), $parsed_url );
$parsed_url = add_query_arg( array('partner'=>'cbs'), $parsed_url );
// decide on width and height
if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
$width = (int) $rawattr['width'];
$height = (int) $rawattr['height'];
} else {
list( $width, $height ) = wp_expand_dimensions( 480, 270, $attr['width'], $attr['height'] );
}
// yuck.. but it's their embed code. blame them for it.
$ret = "<object width='{$width}' height='{$height}'>
<param name='movie' value='{$parsed_url}'></param>
<param name='allowFullScreen' value='true'></param>
<param name='allowScriptAccess' value='always'></param>
<embed width='{$width}' height='{$height}' src='{$parsed_url}' allowFullScreen='true' allowScriptAccess='always' type='application/x-shockwave-flash'></embed>
</object>";
// cache, and return
update_post_meta( $post->ID, $cachekey, $ret );
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment