Created
June 24, 2013 17:37
-
-
Save justinph/5851946 to your computer and use it in GitHub Desktop.
Add a fake/faux oEmbed provider for DocumentCloud. Also adds a shortcode.
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 | |
/* Faux DocumentCloud oEmbed | |
* Embed documentcloud inline without using their provided embed code because it sucks | |
* @usage Paste a Documentcloud link in your post and it will be replaced with an the proper code when published | |
*/ | |
/* | |
* Example embed URL: | |
* http://www.documentcloud.org/documents/693854-macalester-collge-letter-to-students-about-wells.html | |
*/ | |
wp_embed_register_handler( 'documentcloud_oembed', '{^https?://www\.documentcloud\.org/documents/(?P<id>.+)\.html}', 'embed_documentcloud_oembed' ); | |
function embed_documentcloud_oembed( $matches ) { | |
$id = parse_documentcloud_id_from_url($matches[0]); | |
$embed = " | |
<div id='DV-viewer-$id' class='DV-container'></div> | |
<script src='http://s3.documentcloud.org/viewer/loader.js'></script> | |
<script> | |
DV.load('http://www.documentcloud.org/documents/$id.js', { | |
width: '100%', | |
height: 500, | |
sidebar:false, | |
text: true, | |
pdf: true, | |
container: '#DV-viewer-$id' | |
}); | |
</script> | |
<style type='text/css'>#DV-viewer-{$id} * {box-sizing: content-box;}</style> | |
"; | |
return apply_filters( 'embed_dc_map', $embed ); | |
} | |
/* | |
* Documentcloud Shortcode support | |
* Borrowed from the argo project but dramatically simplified for our use: http://argoproject.org/documentcloud.php | |
* | |
*/ | |
function documentcloud_embed_shortcode($atts, $content){ | |
global $post; | |
//$defaults = $this->get_defaults(); | |
extract( shortcode_atts($defaults, $atts)); | |
// we need a document ID or URL, or it's a no op | |
if ($url && !$id) { | |
// parse id from url | |
$id = parse_documentcloud_id_from_url($url); | |
} | |
extract( $atts); | |
// still no id? nothin doing | |
if (!$id) return; | |
# we only deal with integers | |
$height = intval($height); | |
$width = intval($width); | |
return " | |
<div id='DV-viewer-$id' class='DV-container'></div> | |
<script src='http://s3.documentcloud.org/viewer/loader.js'></script> | |
<script> | |
DV.load('http://www.documentcloud.org/documents/$id.js', { | |
width: '100%', | |
height: 500, | |
sidebar:false, | |
text: true, | |
pdf: true, | |
container: '#DV-viewer-$id' | |
}); | |
</script> | |
<style type='text/css'>#DV-viewer-{$id} * {box-sizing: content-box;}</style> | |
"; | |
} | |
/* | |
* Support/helper function for documentcloud embed/shortcode support | |
*/ | |
function parse_documentcloud_id_from_url($url) { | |
$regex = '{^https?://www\.documentcloud\.org/documents/(?P<id>.+)\.html}'; | |
$matches = array(); | |
if (preg_match($regex, $url, $matches)) { | |
return $matches['id']; | |
} else { | |
return null; | |
} | |
} | |
add_shortcode('documentcloud', 'documentcloud_embed_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
<script src="//s3.amazonaws.com/s3.documentcloud.org/viewer/loader.js"></script>
to have this work on https URLs.