soembed.module in Simple oEmbed 7
Lets author embed rich media by inserting URL using oEmbed techonology.
File
soembed.moduleView source
<?php
/**
* @file
* Lets author embed rich media by inserting URL using oEmbed techonology.
*/
/**
* Implements hook_filter_info().
*/
function soembed_filter_info() {
$filters['soembed'] = array(
'title' => t('Simple oEmbed filter'),
'description' => t('Embeds media for URL that supports oEmbed standard.'),
'process callback' => 'soembed_filter_embed_process',
'settings callback' => 'soembed_filter_oembed_settings',
'tips callback' => 'soembed_filter_embed_tips',
'default settings' => array(
'soembed_maxwidth' => '500',
'soembed_replace_inline' => 0,
),
);
return $filters;
}
/**
* Implements hook_filter_FILTER_process().
*/
function soembed_filter_embed_process($text, $filter, $format, $langcode, $cache, $cache_id) {
static $check_settings;
// Set maxwidth variable for use by soembed_filter_embed(), otherwise cannot pass the value.
if (empty($check_settings)) {
if ($filter->settings['soembed_maxwidth'] != variable_get('soembed_maxwidth', 0)) {
variable_set('soembed_maxwidth', (int) $filter->settings['soembed_maxwidth']);
}
$check_settings = TRUE;
}
$lines = explode("\n", $text);
if (!empty($filter->settings['soembed_replace_inline'])) {
// Match in-line urls. (First () in pattern is needed because the callback
// expects the url in the second pair of parentheses).
$lines = preg_replace_callback('#()(https?://[^\\s<]+)#', 'soembed_filter_embed', $lines);
}
else {
$lines = preg_replace_callback('#^(<p>)?(https?://\\S+?)(</p>)?$#', 'soembed_filter_embed', $lines);
}
return implode("\n", $lines);
}
/**
* Implements hook_filter_FILTER_settings().
*/
function soembed_filter_oembed_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
$filter->settings += $defaults;
$settings = array();
$settings['soembed_maxwidth'] = array(
'#type' => 'textfield',
'#title' => t('Maximum width of media embed'),
'#default_value' => $filter->settings['soembed_maxwidth'],
'#description' => t('Set the maximum width of an embedded media. The unit is in pixels, but only put a number in the textbox.'),
);
$settings['soembed_replace_inline'] = array(
'#type' => 'checkbox',
'#title' => t('Replace in-line URLs'),
'#default_value' => $filter->settings['soembed_replace_inline'],
'#description' => t('If this option is checked, the filter will recognize URLs even when they are not on their own lines.'),
);
return $settings;
}
/**
* Implements hook_filter_FILTER_tips().
*/
function soembed_filter_embed_tips($filter, $format, $long) {
if ($long) {
return t('To embed an external media, place the URL on its own line. If there is an error or no support, the URL will be printed as is.');
}
else {
return t('To embed an external media, place the URL on its own line.');
}
}
/**
* Make request against provider to get embedded code.
*/
function soembed_filter_embed($match) {
static $providers;
if (empty($providers)) {
module_load_include('inc', 'soembed', 'soembed.providers');
}
$url = $match[2];
foreach ($providers as $matchmask => $data) {
list($providerurl, $regex) = $data;
if (!$regex) {
$matchmask = '#' . str_replace('___wildcard___', '(.+)', preg_quote(str_replace('*', '___wildcard___', $matchmask), '#')) . '#i';
}
if (preg_match($matchmask, $url)) {
$provider = $providerurl;
break;
}
}
if (!empty($provider)) {
// http://www.php-code.net/2010/05/oembed-transforming-video-links-to-embeds/
// SEE FOR RESIZING MEDIA: https://gist.github.com/1313517
if ($regex === 'LOCAL') {
$output = soembed_get_contents($provider, $url);
}
elseif ($response = drupal_http_request($provider . '?url=' . urlencode($url) . '&format=json&maxwidth=' . variable_get('soembed_maxwidth', 0))) {
if ($response->code == 200) {
$embed = json_decode($response->data);
$embed_type = strtolower(str_replace(' ', '-', $embed->type));
if (!empty($embed->html)) {
$output = $embed->html;
}
elseif ($embed->type == 'photo') {
$output = '<img src="' . $embed->url . '" title="' . $embed->title . '" style="width: 100%" />';
$output = '<a href="' . $url . '">' . $output . '</a>';
}
}
}
}
$output = empty($output) ? $url : $output;
if ($embed_type != 'video' && count($match) > 3) {
// Add <p> and </p> back.
$output = $match[1] . $output . $match[3];
}
$output = '<div class="soembed-item ' . $embed_type . '">' . $output . '</div>';
return $output;
}
/**
* Locally create HTML after pattern study for sites that don't support oEmbed.
*/
function soembed_get_contents($provider, $url) {
$width = variable_get('soembed_maxwidth', 0);
switch ($provider) {
case 'google-maps':
//$url = str_replace('&', '&', $url); Though Google encodes ampersand, it seems to work without it.
$height = (int) ($width / 1.3);
$embed = "<iframe width='{$width}' height='{$height}' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='{$url}&output=embed'></iframe><br /><small><a href='{$url}&source=embed' style='color:#0000FF;text-align:left'>View Larger Map</a></small>";
break;
case 'google-docs':
$height = (int) ($width * 1.5);
$embed = "<iframe width='{$width}' height='{$height}' frameborder='0' src='{$url}&widget=true'></iframe>";
break;
default:
$embed = $url;
}
return $embed;
}
Functions
Name | Description |
---|---|
soembed_filter_embed | Make request against provider to get embedded code. |
soembed_filter_embed_process | Implements hook_filter_FILTER_process(). |
soembed_filter_embed_tips | Implements hook_filter_FILTER_tips(). |
soembed_filter_info | Implements hook_filter_info(). |
soembed_filter_oembed_settings | Implements hook_filter_FILTER_settings(). |
soembed_get_contents | Locally create HTML after pattern study for sites that don't support oEmbed. |