Commit 0cdccdb1 authored by lucha's avatar lucha Committed by lechuck
Browse files

Oembed Provider 2.0.0

parent 569bef3b
......@@ -3,104 +3,178 @@
Plugin Name: oEmbed Provider
Plugin URI: http://wordpress.org/extend/plugins/oembed-provider/
Description: An oEmbed provider for Wordpress
Version: 1.1
Author: Craig Andrews <candrews@integralblue.com>
Author URI: http://candrews.integralblue.com
Version: 2.0.0
Author: pfefferle, candrews
Author URI: https://github.com/pfefferle/oEmbedProvider/
*/
/*
Copyright 2009 Craig Andrews (candrews@integralblue.com)
/**
* oEmbed Provider for WordPress
*
* @author Matthias Pfefferle
* @author Craig Andrews
*/
class OembedProvider {
/**
* auto discovery links
*/
function add_oembed_links(){
if(is_singular()){
echo '<link rel="alternate" type="application/json+oembed" href="' . site_url('/?oembed=true&amp;format=json&amp;url=' . urlencode(get_permalink())) . '" />'."\n";
echo '<link rel="alternate" type="text/xml+oembed" href="' . site_url('/?oembed=true&amp;format=xml&amp;url=' . urlencode(get_permalink())) . '" />'."\n";
}
}
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
/**
* adds query vars
*/
function query_vars($query_vars) {
$query_vars[] = 'oembed';
$query_vars[] = 'format';
$query_vars[] = 'url';
$query_vars[] = 'callback';
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
return $query_vars;
}
/**
* handles request
*/
function parse_query($wp) {
if (!array_key_exists('oembed', $wp->query_vars) ||
!array_key_exists('url', $wp->query_vars)) {
return;
}
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
$post_ID = url_to_postid($wp->query_vars['url']);
$post = get_post($post_ID);
if(!$post) {
header('Status: 404');
die("Not found");
}
$post_type = get_post_type($post);
// add support for alternate output formats
$oembed_provider_formats = apply_filters("oembed_provider_formats", array('json', 'xml'));
// check output format
$format = "json";
if (array_key_exists('format', $wp->query_vars) && in_array(strtolower($wp->query_vars['format']), $oembed_provider_formats)) {
$format = $wp->query_vars['format'];
}
// content filter
$oembed_provider_data = apply_filters("oembed_provider_data", array(), $post_type, $post);
$oembed_provider_data = apply_filters("oembed_provider_data_{$post_type}", $oembed_provider_data, $post);
do_action("oembed_provider_render", $oembed_provider_data, $wp->query_vars);
do_action("oembed_provider_render_{$format}", $oembed_provider_data, $wp->query_vars);
}
/**
* adds default content
*
* @param array $oembed_provider_data
* @param string $post_type
* @param Object $post
*/
function generate_default_content($oembed_provider_data, $post_type, $post) {
$author = get_userdata($post->post_author);
if(function_exists('add_action')){
// Running inside of Wordpress
add_action('wp_head', 'add_oembed_links');
$oembed_provider_data['version'] = '1.0';
$oembed_provider_data['provider_name'] = get_bloginfo('name');
$oembed_provider_data['provider_url'] = home_url();
$oembed_provider_data['author_name'] = $author->display_name;
$oembed_provider_data['author_url'] = get_author_posts_url($author->ID, $author->nicename);
$oembed_provider_data['title'] = $post->post_title;
function add_oembed_links(){
if(is_single() || is_page() || is_attachment()){
print '<link rel="alternate" type="application/json+oembed" href="' . plugins_url('oembed-provider/oembed-provider.php') . '?format=json&url=' . urlencode(get_permalink()) . '" />';
print '<link rel="alternate" type="application/xml+oembed" href="' . plugins_url('oembed-provider/oembed-provider.php') . '?format=xml&url=' . urlencode(get_permalink()) . '" />';
}
return $oembed_provider_data;
}
/**
* adds attachement specific content
*
* @param array $oembed_provider_data
* @param Object $post
*/
function generate_attachement_content($oembed_provider_data, $post) {
if (substr($post->post_mime_type,0,strlen('image/'))=='image/') {
$oembed_provider_data['type']='photo';
} else {
$oembed_provider_data['type']='link';
}
}else{
//Directly called (not by Wordpress)
require('../../../wp-load.php');
$oembed_provider_data['url'] = wp_get_attachment_url($post->ID);
$url = $_GET['url'];
$post_ID = url_to_postid($url);
$post=get_post($post_ID);
if(empty($post)){
header('Status: 404');
die("Not found");
}else{
$author = get_userdata($post->post_author);
$oembed=array();
$oembed['version']='1.0';
$oembed['provider_name']=get_option('blogname');
$oembed['provider_url']=get_option('home');
$oembed['author_name']=$author->display_name;
$oembed['author_url']=get_author_posts_url($author->ID, $author->nicename);
$oembed['title']=$post->post_title;
switch(get_post_type($post)){
case 'attachment':
if(substr($post->post_mime_type,0,strlen('image/'))=='image/'){
$oembed['type']='photo';
}else{
$oembed['type']='link';
}
$oembed['url']=wp_get_attachment_url($post->ID);
break;
case 'post':
case 'page':
$oembed['type']='link';
$oembed['html']=empty($post->post_excerpt)?$post->post_content:$post->post_excerpt;
break;
default:
header('Status: 501');
die('oEmbed not supported for posts of type \'' . $post->type . '\'');
break;
}
return $oembed_provider_data;
}
/**
* adds post/page specific content
*
* @param array $oembed_provider_data
* @param Object $post
*/
function generate_post_content($oembed_provider_data, $post) {
if (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
$oembed_provider_data['thumbnail_url'] = $image[0];
$oembed_provider_data['thumbnail_width'] = $image[1];
$oembed_provider_data['thumbnail_height'] = $image[2];
}
$oembed_provider_data['type']='rich';
$oembed_provider_data['html'] = empty($post->post_excerpt) ? $post->post_content : $post->post_excerpt;
$format = $_GET['format'];
switch($format){
case 'json':
header('Content-Type: application/json; charset=' . get_option('blog_charset'), true);
$callback = $_GET['callback'];
if($callback){
print $callback . '(';
}
print(json_encode($oembed));
if($callback){
print ')';
}
break;
case 'xml':
header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
print '<?xml version="1.0" encoding="' . get_option('blog_charset') . '" standalone="yes"?>';
print '<oembed>';
foreach(array_keys($oembed) as $element){
print '<' . $element . '><![CDATA[' . $oembed[$element] . ']]></' . $element . '>';
}
print '</oembed>';
break;
default:
header('Status: 501');
die('Format \'' . $format . '\' not supported');
}
return $oembed_provider_data;
}
/**
* render json output
*
* @param array $oembed_provider_data
*/
function render_json($oembed_provider_data, $wp_query) {
header('Content-Type: application/json; charset=' . get_bloginfo('charset'), true);
// render json output
$json = json_encode($oembed_provider_data);
// add callback if available
if (array_key_exists('callback', $wp_query)) {
$json = $wp_query['callback'] . "($json);";
}
echo $json;
exit;
}
/**
* render xml output
*
* @param array $oembed_provider_data
*/
function render_xml($oembed_provider_data) {
header('Content-Type: text/xml; charset=' . get_bloginfo('charset'), true);
// render xml-output
echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '" ?>';
echo '<oembed>';
foreach(array_keys($oembed_provider_data) as $element){
echo '<' . $element . '>' . htmlspecialchars($oembed_provider_data[$element]) . '</' . $element . '>';
}
echo '</oembed>';
exit;
}
}
?>
add_action('wp_head', array('OembedProvider', 'add_oembed_links'));
add_action('parse_query', array('OembedProvider', 'parse_query'));
add_filter('query_vars', array('OembedProvider', 'query_vars'));
add_filter('oembed_provider_data', array('OembedProvider', 'generate_default_content'), 90, 3);
add_filter('oembed_provider_data_attachement', array('OembedProvider', 'generate_attachement_content'), 91, 2);
add_filter('oembed_provider_data_post', array('OembedProvider', 'generate_post_content'), 91, 2);
add_filter('oembed_provider_data_page', array('OembedProvider', 'generate_post_content'), 91, 2);
add_action('oembed_provider_render_json', array('OembedProvider', 'render_json'), 99, 2);
add_action('oembed_provider_render_xml', array('OembedProvider', 'render_xml'), 99);
\ No newline at end of file
=== oEmbed Provider ===
Contributors: candrews
Donate link: http://candrews.integralblue.com/
Contributors: pfefferle, candrews
Tags: oembed, links
Requires at least: 2.8.0
Tested up to: 2.8.2
Stable tag: 1.1
Requires at least: 3.0
Tested up to: 3.5.1
Stable tag: 2.0.0
An oEmbed provider for Wordpress.
......@@ -28,8 +27,8 @@ Yes!
== Changelog ==
= 1.1 =
* Added callback parameter for JSON-P
= 2.0.0 =
* complete refactoring
= 1.0 =
* Initial release
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment