author | Anthony Ly <anthonyly.com@gmail.com> |
Wed, 19 Dec 2012 17:46:52 -0800 | |
changeset 204 | 09a1c134465b |
parent 194 | 32102edaa81b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* API for fetching the HTML to embed remote content based on a provided URL. |
|
4 |
* Used internally by the {@link WP_Embed} class, but is designed to be generic. |
|
5 |
* |
|
6 |
* @link http://codex.wordpress.org/oEmbed oEmbed Codex Article |
|
7 |
* @link http://oembed.com/ oEmbed Homepage |
|
8 |
* |
|
9 |
* @package WordPress |
|
10 |
* @subpackage oEmbed |
|
11 |
*/ |
|
12 |
||
13 |
/** |
|
14 |
* oEmbed class. |
|
15 |
* |
|
16 |
* @package WordPress |
|
17 |
* @subpackage oEmbed |
|
18 |
* @since 2.9.0 |
|
19 |
*/ |
|
20 |
class WP_oEmbed { |
|
21 |
var $providers = array(); |
|
22 |
||
23 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
24 |
* Constructor |
136 | 25 |
* |
26 |
* @uses apply_filters() Filters a list of pre-defined oEmbed providers. |
|
27 |
*/ |
|
28 |
function __construct() { |
|
29 |
// List out some popular sites that support oEmbed. |
|
30 |
// The WP_Embed class disables discovery for non-unfiltered_html users, so only providers in this array will be used for them. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
// Add to this list using the wp_oembed_add_provider() function (see its PHPDoc for details). |
136 | 32 |
$this->providers = apply_filters( 'oembed_providers', array( |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
33 |
'#https?://(www\.)?youtube.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ), |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
34 |
'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
35 |
'http://blip.tv/*' => array( 'http://blip.tv/oembed/', false ), |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
36 |
'#https?://(www\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ), |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
37 |
'#https?://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/services/oembed', true ), |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
38 |
'#https?://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ), |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
39 |
'#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ), |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
40 |
'#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ), |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
41 |
'#https?://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ), |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
42 |
'http://qik.com/*' => array( 'http://qik.com/api/oembed.{format}', false ), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
43 |
'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
44 |
'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
45 |
'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ), |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
46 |
'#https?://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ), |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
47 |
'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ), |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
48 |
'#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ), |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
49 |
'#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ), |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
50 |
'#https?://(www\.)?twitter.com/.+?/status(es)?/.*#i' => array( 'http://api.twitter.com/1/statuses/oembed.{format}', true ), |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
51 |
'#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ), |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
52 |
'#https?://(www\.)?slideshare.net/*#' => array( 'http://www.slideshare.net/api/oembed/2', true ), |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
53 |
'#http://instagr(\.am|am\.com)/p/.*#i' => array( 'http://api.instagram.com/oembed', true ), |
136 | 54 |
) ); |
55 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
56 |
// Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop(). |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
57 |
add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 ); |
136 | 58 |
} |
59 |
||
60 |
/** |
|
61 |
* The do-it-all function that takes a URL and attempts to return the HTML. |
|
62 |
* |
|
63 |
* @see WP_oEmbed::discover() |
|
64 |
* @see WP_oEmbed::fetch() |
|
65 |
* @see WP_oEmbed::data2html() |
|
66 |
* |
|
67 |
* @param string $url The URL to the content that should be attempted to be embedded. |
|
68 |
* @param array $args Optional arguments. Usually passed from a shortcode. |
|
69 |
* @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed. |
|
70 |
*/ |
|
71 |
function get_html( $url, $args = '' ) { |
|
72 |
$provider = false; |
|
73 |
||
74 |
if ( !isset($args['discover']) ) |
|
75 |
$args['discover'] = true; |
|
76 |
||
77 |
foreach ( $this->providers as $matchmask => $data ) { |
|
78 |
list( $providerurl, $regex ) = $data; |
|
79 |
||
80 |
// Turn the asterisk-type provider URLs into regex |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
81 |
if ( !$regex ) { |
136 | 82 |
$matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i'; |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
83 |
$matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
84 |
} |
136 | 85 |
|
86 |
if ( preg_match( $matchmask, $url ) ) { |
|
87 |
$provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML |
|
88 |
break; |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
if ( !$provider && $args['discover'] ) |
|
93 |
$provider = $this->discover( $url ); |
|
94 |
||
95 |
if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) |
|
96 |
return false; |
|
97 |
||
98 |
return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args ); |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* Attempts to find oEmbed provider discovery <link> tags at the given URL. |
|
103 |
* |
|
104 |
* @param string $url The URL that should be inspected for discovery <link> tags. |
|
105 |
* @return bool|string False on failure, otherwise the oEmbed provider URL. |
|
106 |
*/ |
|
107 |
function discover( $url ) { |
|
108 |
$providers = array(); |
|
109 |
||
110 |
// Fetch URL content |
|
111 |
if ( $html = wp_remote_retrieve_body( wp_remote_get( $url ) ) ) { |
|
112 |
||
113 |
// <link> types that contain oEmbed provider URLs |
|
114 |
$linktypes = apply_filters( 'oembed_linktypes', array( |
|
115 |
'application/json+oembed' => 'json', |
|
116 |
'text/xml+oembed' => 'xml', |
|
117 |
'application/xml+oembed' => 'xml', // Incorrect, but used by at least Vimeo |
|
118 |
) ); |
|
119 |
||
120 |
// Strip <body> |
|
121 |
$html = substr( $html, 0, stripos( $html, '</head>' ) ); |
|
122 |
||
123 |
// Do a quick check |
|
124 |
$tagfound = false; |
|
125 |
foreach ( $linktypes as $linktype => $format ) { |
|
126 |
if ( stripos($html, $linktype) ) { |
|
127 |
$tagfound = true; |
|
128 |
break; |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) { |
|
133 |
foreach ( $links[1] as $link ) { |
|
134 |
$atts = shortcode_parse_atts( $link ); |
|
135 |
||
136 |
if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) { |
|
137 |
$providers[$linktypes[$atts['type']]] = $atts['href']; |
|
138 |
||
139 |
// Stop here if it's JSON (that's all we need) |
|
140 |
if ( 'json' == $linktypes[$atts['type']] ) |
|
141 |
break; |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
// JSON is preferred to XML |
|
148 |
if ( !empty($providers['json']) ) |
|
149 |
return $providers['json']; |
|
150 |
elseif ( !empty($providers['xml']) ) |
|
151 |
return $providers['xml']; |
|
152 |
else |
|
153 |
return false; |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Connects to a oEmbed provider and returns the result. |
|
158 |
* |
|
159 |
* @param string $provider The URL to the oEmbed provider. |
|
160 |
* @param string $url The URL to the content that is desired to be embedded. |
|
161 |
* @param array $args Optional arguments. Usually passed from a shortcode. |
|
162 |
* @return bool|object False on failure, otherwise the result in the form of an object. |
|
163 |
*/ |
|
164 |
function fetch( $provider, $url, $args = '' ) { |
|
165 |
$args = wp_parse_args( $args, wp_embed_defaults() ); |
|
166 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
167 |
$provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
168 |
$provider = add_query_arg( 'maxheight', (int) $args['height'], $provider ); |
136 | 169 |
$provider = add_query_arg( 'url', urlencode($url), $provider ); |
170 |
||
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
171 |
$provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
172 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
173 |
foreach( array( 'json', 'xml' ) as $format ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
174 |
$result = $this->_fetch_with_format( $provider, $format ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
175 |
if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
176 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
177 |
return ( $result && ! is_wp_error( $result ) ) ? $result : false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
178 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
179 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
180 |
} |
136 | 181 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
182 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
183 |
* Fetches result from an oEmbed provider for a specific format and complete provider URL |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
184 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
185 |
* @since 3.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
186 |
* @access private |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
187 |
* @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
188 |
* @param string $format Format to use |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
189 |
* @return bool|object False on failure, otherwise the result in the form of an object. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
190 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
191 |
function _fetch_with_format( $provider_url_with_args, $format ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
192 |
$provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
193 |
$response = wp_remote_get( $provider_url_with_args ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
194 |
if ( 501 == wp_remote_retrieve_response_code( $response ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
195 |
return new WP_Error( 'not-implemented' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
196 |
if ( ! $body = wp_remote_retrieve_body( $response ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
197 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
198 |
$parse_method = "_parse_$format"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
199 |
return $this->$parse_method( $body ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
200 |
} |
136 | 201 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
202 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
203 |
* Parses a json response body. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
204 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
205 |
* @since 3.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
206 |
* @access private |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
207 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
208 |
function _parse_json( $response_body ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
209 |
return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
210 |
} |
136 | 211 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
212 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
213 |
* Parses an XML response body. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
214 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
215 |
* @since 3.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
216 |
* @access private |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
217 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
218 |
function _parse_xml( $response_body ) { |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
219 |
if ( !function_exists('simplexml_load_string') ) { |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
220 |
return false; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
221 |
} |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
222 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
223 |
if ( ! class_exists( 'DOMDocument' ) ) |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
224 |
return false; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
225 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
226 |
$errors = libxml_use_internal_errors( true ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
227 |
$old_value = null; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
228 |
if ( function_exists( 'libxml_disable_entity_loader' ) ) { |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
229 |
$old_value = libxml_disable_entity_loader( true ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
230 |
} |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
231 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
232 |
$dom = new DOMDocument; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
233 |
$success = $dom->loadXML( $response_body ); |
136 | 234 |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
235 |
if ( ! is_null( $old_value ) ) { |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
236 |
libxml_disable_entity_loader( $old_value ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
237 |
} |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
238 |
libxml_use_internal_errors( $errors ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
239 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
240 |
if ( ! $success || isset( $dom->doctype ) ) { |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
241 |
return false; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
242 |
} |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
243 |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
244 |
$data = simplexml_import_dom( $dom ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
245 |
if ( ! is_object( $data ) ) |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
246 |
return false; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
247 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
248 |
$return = new stdClass; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
249 |
foreach ( $data as $key => $value ) |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
250 |
$return->$key = (string) $value; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
251 |
return $return; |
136 | 252 |
} |
253 |
||
254 |
/** |
|
255 |
* Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML. |
|
256 |
* |
|
257 |
* @param object $data A data object result from an oEmbed provider. |
|
258 |
* @param string $url The URL to the content that is desired to be embedded. |
|
259 |
* @return bool|string False on error, otherwise the HTML needed to embed. |
|
260 |
*/ |
|
261 |
function data2html( $data, $url ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
262 |
if ( ! is_object( $data ) || empty( $data->type ) ) |
136 | 263 |
return false; |
264 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
265 |
$return = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
266 |
|
136 | 267 |
switch ( $data->type ) { |
268 |
case 'photo': |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
269 |
if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
270 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
271 |
if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
272 |
break; |
136 | 273 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
274 |
$title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
275 |
$return = '<a href="' . esc_url( $url ) . '"><img src="' . esc_url( $data->url ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" /></a>'; |
136 | 276 |
break; |
277 |
||
278 |
case 'video': |
|
279 |
case 'rich': |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
280 |
if ( ! empty( $data->html ) && is_string( $data->html ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
281 |
$return = $data->html; |
136 | 282 |
break; |
283 |
||
284 |
case 'link': |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
285 |
if ( ! empty( $data->title ) && is_string( $data->title ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
286 |
$return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>'; |
136 | 287 |
break; |
288 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
289 |
default: |
136 | 290 |
$return = false; |
291 |
} |
|
292 |
||
293 |
// You can use this filter to add support for custom data types or to filter the result |
|
294 |
return apply_filters( 'oembed_dataparse', $return, $data, $url ); |
|
295 |
} |
|
296 |
||
297 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
298 |
* Strip any new lines from the HTML. |
136 | 299 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
300 |
* @access private |
136 | 301 |
* @param string $html Existing HTML. |
302 |
* @param object $data Data object from WP_oEmbed::data2html() |
|
303 |
* @param string $url The original URL passed to oEmbed. |
|
304 |
* @return string Possibly modified $html |
|
305 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
306 |
function _strip_newlines( $html, $data, $url ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
307 |
if ( false !== strpos( $html, "\n" ) ) |
136 | 308 |
$html = str_replace( array( "\r\n", "\n" ), '', $html ); |
309 |
||
310 |
return $html; |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Returns the initialized {@link WP_oEmbed} object |
|
316 |
* |
|
317 |
* @since 2.9.0 |
|
318 |
* @access private |
|
319 |
* |
|
320 |
* @see WP_oEmbed |
|
321 |
* @uses WP_oEmbed |
|
322 |
* |
|
323 |
* @return WP_oEmbed object. |
|
324 |
*/ |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
325 |
function _wp_oembed_get_object() { |
136 | 326 |
static $wp_oembed; |
327 |
||
328 |
if ( is_null($wp_oembed) ) |
|
329 |
$wp_oembed = new WP_oEmbed(); |
|
330 |
||
331 |
return $wp_oembed; |
|
332 |
} |