WP_oEmbed

Core class used to implement oEmbed functionality.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-oembed.php  
  1. class WP_oEmbed { 
  2.  
  3. /** 
  4. * A list of oEmbed providers. 
  5. * @since 2.9.0 
  6. * @access public 
  7. * @var array 
  8. */ 
  9. public $providers = array(); 
  10.  
  11. /** 
  12. * A list of an early oEmbed providers. 
  13. * @since 4.0.0 
  14. * @access public 
  15. * @static 
  16. * @var array 
  17. */ 
  18. public static $early_providers = array(); 
  19.  
  20. /** 
  21. * A list of private/protected methods, used for backward compatibility. 
  22. * @since 4.2.0 
  23. * @access private 
  24. * @var array 
  25. */ 
  26. private $compat_methods = array( '_fetch_with_format', '_parse_json', '_parse_xml', '_parse_body' ); 
  27.  
  28. /** 
  29. * Constructor. 
  30. * @since 2.9.0 
  31. * @access public 
  32. */ 
  33. public function __construct() { 
  34. $host = urlencode( home_url() ); 
  35. $providers = array( 
  36. '#http://((m|www)\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ),  
  37. '#https://((m|www)\.)?youtube\.com/watch.*#i' => array( 'http://www.youtube.com/oembed?scheme=https', true ),  
  38. '#http://((m|www)\.)?youtube\.com/playlist.*#i' => array( 'http://www.youtube.com/oembed', true ),  
  39. '#https://((m|www)\.)?youtube\.com/playlist.*#i' => array( 'http://www.youtube.com/oembed?scheme=https', true ),  
  40. '#http://youtu\.be/.*#i' => array( 'http://www.youtube.com/oembed', true ),  
  41. '#https://youtu\.be/.*#i' => array( 'http://www.youtube.com/oembed?scheme=https', true ),  
  42. '#https?://(.+\.)?vimeo\.com/.*#i' => array( 'http://vimeo.com/api/oembed.{format}', true ),  
  43. '#https?://(www\.)?dailymotion\.com/.*#i' => array( 'https://www.dailymotion.com/services/oembed', true ),  
  44. '#https?://dai.ly/.*#i' => array( 'https://www.dailymotion.com/services/oembed', true ),  
  45. '#https?://(www\.)?flickr\.com/.*#i' => array( 'https://www.flickr.com/services/oembed/', true ),  
  46. '#https?://flic\.kr/.*#i' => array( 'https://www.flickr.com/services/oembed/', true ),  
  47. '#https?://(.+\.)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ),  
  48. '#https?://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ),  
  49. 'http://i*.photobucket.com/albums/*' => array( 'http://api.photobucket.com/oembed', false ),  
  50. 'http://gi*.photobucket.com/groups/*' => array( 'http://api.photobucket.com/oembed', false ),  
  51. '#https?://(www\.)?scribd\.com/doc/.*#i' => array( 'http://www.scribd.com/services/oembed', true ),  
  52. '#https?://wordpress.tv/.*#i' => array( 'http://wordpress.tv/oembed/', true ),  
  53. '#https?://(.+\.)?polldaddy\.com/.*#i' => array( 'https://polldaddy.com/oembed/', true ),  
  54. '#https?://poll\.fm/.*#i' => array( 'https://polldaddy.com/oembed/', true ),  
  55. '#https?://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ),  
  56. '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i' => array( 'https://publish.twitter.com/oembed', true ),  
  57. '#https?://(www\.)?twitter\.com/.+?/timelines/.*#i' => array( 'https://publish.twitter.com/oembed', true ),  
  58. '#https?://(www\.)?twitter\.com/i/moments/.*#i' => array( 'https://publish.twitter.com/oembed', true ),  
  59. '#https?://vine.co/v/.*#i' => array( 'https://vine.co/oembed.{format}', true ),  
  60. '#https?://(www\.)?soundcloud\.com/.*#i' => array( 'http://soundcloud.com/oembed', true ),  
  61. '#https?://(.+?\.)?slideshare\.net/.*#i' => array( 'https://www.slideshare.net/api/oembed/2', true ),  
  62. '#https?://(www\.)?instagr(\.am|am\.com)/p/.*#i' => array( 'https://api.instagram.com/oembed', true ),  
  63. '#https?://(open|play)\.spotify\.com/.*#i' => array( 'https://embed.spotify.com/oembed/', true ),  
  64. '#https?://(.+\.)?imgur\.com/.*#i' => array( 'http://api.imgur.com/oembed', true ),  
  65. '#https?://(www\.)?meetu(\.ps|p\.com)/.*#i' => array( 'http://api.meetup.com/oembed', true ),  
  66. '#https?://(www\.)?issuu\.com/.+/docs/.+#i' => array( 'http://issuu.com/oembed_wp', true ),  
  67. '#https?://(www\.)?collegehumor\.com/video/.*#i' => array( 'http://www.collegehumor.com/oembed.{format}', true ),  
  68. '#https?://(www\.)?mixcloud\.com/.*#i' => array( 'http://www.mixcloud.com/oembed', true ),  
  69. '#https?://(www\.|embed\.)?ted\.com/talks/.*#i' => array( 'http://www.ted.com/talks/oembed.{format}', true ),  
  70. '#https?://(www\.)?(animoto|video214)\.com/play/.*#i' => array( 'https://animoto.com/oembeds/create', true ),  
  71. '#https?://(.+)\.tumblr\.com/post/.*#i' => array( 'https://www.tumblr.com/oembed/1.0', true ),  
  72. '#https?://(www\.)?kickstarter\.com/projects/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ),  
  73. '#https?://kck\.st/.*#i' => array( 'https://www.kickstarter.com/services/oembed', true ),  
  74. '#https?://cloudup\.com/.*#i' => array( 'https://cloudup.com/oembed', true ),  
  75. '#https?://(www\.)?reverbnation\.com/.*#i' => array( 'https://www.reverbnation.com/oembed', true ),  
  76. '#https?://videopress.com/v/.*#' => array( 'https://public-api.wordpress.com/oembed/1.0/?for=' . $host, true ),  
  77. '#https?://(www\.)?reddit\.com/r/[^/]+/comments/.*#i' => array( 'https://www.reddit.com/oembed', true ),  
  78. '#https?://(www\.)?speakerdeck\.com/.*#i' => array( 'https://speakerdeck.com/oembed.{format}', true ),  
  79. ); 
  80.  
  81. if ( ! empty( self::$early_providers['add'] ) ) { 
  82. foreach ( self::$early_providers['add'] as $format => $data ) { 
  83. $providers[ $format ] = $data; 
  84.  
  85. if ( ! empty( self::$early_providers['remove'] ) ) { 
  86. foreach ( self::$early_providers['remove'] as $format ) { 
  87. unset( $providers[ $format ] ); 
  88.  
  89. self::$early_providers = array(); 
  90.  
  91. /** 
  92. * Filters the list of whitelisted oEmbed providers. 
  93. * Since WordPress 4.4, oEmbed discovery is enabled for all users and allows embedding of sanitized 
  94. * iframes. The providers in this list are whitelisted, meaning they are trusted and allowed to 
  95. * embed any content, such as iframes, videos, JavaScript, and arbitrary HTML. 
  96. * Supported providers: 
  97. * | Provider | Flavor | Supports HTTPS | Since | 
  98. * | ------------ | --------------------- | :------------: | --------- | 
  99. * | Dailymotion | dailymotion.com | Yes | 2.9.0 | 
  100. * | Flickr | flickr.com | Yes | 2.9.0 | 
  101. * | Hulu | hulu.com | Yes | 2.9.0 | 
  102. * | Photobucket | photobucket.com | No | 2.9.0 | 
  103. * | Scribd | scribd.com | Yes | 2.9.0 | 
  104. * | Vimeo | vimeo.com | Yes | 2.9.0 | 
  105. * | WordPress.tv | wordpress.tv | Yes | 2.9.0 | 
  106. * | YouTube | youtube.com/watch | Yes | 2.9.0 | 
  107. * | Funny or Die | funnyordie.com | Yes | 3.0.0 | 
  108. * | Polldaddy | polldaddy.com | Yes | 3.0.0 | 
  109. * | SmugMug | smugmug.com | Yes | 3.0.0 | 
  110. * | YouTube | youtu.be | Yes | 3.0.0 | 
  111. * | Twitter | twitter.com | Yes | 3.4.0 | 
  112. * | Instagram | instagram.com | Yes | 3.5.0 | 
  113. * | Instagram | instagr.am | Yes | 3.5.0 | 
  114. * | Slideshare | slideshare.net | Yes | 3.5.0 | 
  115. * | SoundCloud | soundcloud.com | Yes | 3.5.0 | 
  116. * | Dailymotion | dai.ly | Yes | 3.6.0 | 
  117. * | Flickr | flic.kr | Yes | 3.6.0 | 
  118. * | Spotify | spotify.com | Yes | 3.6.0 | 
  119. * | Imgur | imgur.com | Yes | 3.9.0 | 
  120. * | Meetup.com | meetup.com | Yes | 3.9.0 | 
  121. * | Meetup.com | meetu.ps | Yes | 3.9.0 | 
  122. * | Animoto | animoto.com | Yes | 4.0.0 | 
  123. * | Animoto | video214.com | Yes | 4.0.0 | 
  124. * | CollegeHumor | collegehumor.com | Yes | 4.0.0 | 
  125. * | Issuu | issuu.com | Yes | 4.0.0 | 
  126. * | Mixcloud | mixcloud.com | Yes | 4.0.0 | 
  127. * | Polldaddy | poll.fm | Yes | 4.0.0 | 
  128. * | TED | ted.com | Yes | 4.0.0 | 
  129. * | YouTube | youtube.com/playlist | Yes | 4.0.0 | 
  130. * | Vine | vine.co | Yes | 4.1.0 | 
  131. * | Tumblr | tumblr.com | Yes | 4.2.0 | 
  132. * | Kickstarter | kickstarter.com | Yes | 4.2.0 | 
  133. * | Kickstarter | kck.st | Yes | 4.2.0 | 
  134. * | Cloudup | cloudup.com | Yes | 4.4.0 | 
  135. * | ReverbNation | reverbnation.com | Yes | 4.4.0 | 
  136. * | VideoPress | videopress.com | Yes | 4.4.0 | 
  137. * | Reddit | reddit.com | Yes | 4.4.0 | 
  138. * | Speaker Deck | speakerdeck.com | Yes | 4.4.0 | 
  139. * | Twitter | twitter.com/timelines | Yes | 4.5.0 | 
  140. * | Twitter | twitter.com/moments | Yes | 4.5.0 | 
  141. * No longer supported providers: 
  142. * | Provider | Flavor | Supports HTTPS | Since | Removed | 
  143. * | ------------ | -------------------- | :------------: | --------- | --------- | 
  144. * | Qik | qik.com | Yes | 2.9.0 | 3.9.0 | 
  145. * | Viddler | viddler.com | Yes | 2.9.0 | 4.0.0 | 
  146. * | Revision3 | revision3.com | No | 2.9.0 | 4.2.0 | 
  147. * | Blip | blip.tv | No | 2.9.0 | 4.4.0 | 
  148. * | Rdio | rdio.com | Yes | 3.6.0 | 4.4.1 | 
  149. * | Rdio | rd.io | Yes | 3.6.0 | 4.4.1 | 
  150. * @see wp_oembed_add_provider() 
  151. * @since 2.9.0 
  152. * @param array $providers An array of popular oEmbed providers. 
  153. */ 
  154. $this->providers = apply_filters( 'oembed_providers', $providers ); 
  155.  
  156. // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop(). 
  157. add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 ); 
  158.  
  159. /** 
  160. * Exposes private/protected methods for backward compatibility. 
  161. * @since 4.0.0 
  162. * @access public 
  163. * @param callable $name Method to call. 
  164. * @param array $arguments Arguments to pass when calling. 
  165. * @return mixed|bool Return value of the callback, false otherwise. 
  166. */ 
  167. public function __call( $name, $arguments ) { 
  168. if ( in_array( $name, $this->compat_methods ) ) { 
  169. return call_user_func_array( array( $this, $name ), $arguments ); 
  170. return false; 
  171.  
  172. /** 
  173. * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one. 
  174. * @since 4.0.0 
  175. * @access public 
  176. * @see WP_oEmbed::discover() 
  177. * @param string $url The URL to the content. 
  178. * @param string|array $args Optional provider arguments. 
  179. * @return false|string False on failure, otherwise the oEmbed provider URL. 
  180. */ 
  181. public function get_provider( $url, $args = '' ) { 
  182. $args = wp_parse_args( $args ); 
  183.  
  184. $provider = false; 
  185.  
  186. if ( !isset($args['discover']) ) 
  187. $args['discover'] = true; 
  188.  
  189. foreach ( $this->providers as $matchmask => $data ) { 
  190. list( $providerurl, $regex ) = $data; 
  191.  
  192. // Turn the asterisk-type provider URLs into regex 
  193. if ( !$regex ) { 
  194. $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i'; 
  195. $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask ); 
  196.  
  197. if ( preg_match( $matchmask, $url ) ) { 
  198. $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML 
  199. break; 
  200.  
  201. if ( !$provider && $args['discover'] ) 
  202. $provider = $this->discover( $url ); 
  203.  
  204. return $provider; 
  205.  
  206. /** 
  207. * Adds an oEmbed provider. 
  208. * The provider is removed just-in-time when wp_oembed_add_provider() is called before 
  209. * the {@see 'plugins_loaded'} hook. 
  210. * The just-in-time addition is for the benefit of the {@see 'oembed_providers'} filter. 
  211. * @static 
  212. * @since 4.0.0 
  213. * @access public 
  214. * @see wp_oembed_add_provider() 
  215. * @param string $format Format of URL that this provider can handle. You can use 
  216. * asterisks as wildcards. 
  217. * @param string $provider The URL to the oEmbed provider.. 
  218. * @param bool $regex Optional. Whether the $format parameter is in a regex format. 
  219. * Default false. 
  220. */ 
  221. public static function _add_provider_early( $format, $provider, $regex = false ) { 
  222. if ( empty( self::$early_providers['add'] ) ) { 
  223. self::$early_providers['add'] = array(); 
  224.  
  225. self::$early_providers['add'][ $format ] = array( $provider, $regex ); 
  226.  
  227. /** 
  228. * Removes an oEmbed provider. 
  229. * The provider is removed just-in-time when wp_oembed_remove_provider() is called before 
  230. * the {@see 'plugins_loaded'} hook. 
  231. * The just-in-time removal is for the benefit of the {@see 'oembed_providers'} filter. 
  232. * @since 4.0.0 
  233. * @access public 
  234. * @static 
  235. * @see wp_oembed_remove_provider() 
  236. * @param string $format The format of URL that this provider can handle. You can use 
  237. * asterisks as wildcards. 
  238. */ 
  239. public static function _remove_provider_early( $format ) { 
  240. if ( empty( self::$early_providers['remove'] ) ) { 
  241. self::$early_providers['remove'] = array(); 
  242.  
  243. self::$early_providers['remove'][] = $format; 
  244.  
  245. /** 
  246. * The do-it-all function that takes a URL and attempts to return the HTML. 
  247. * @see WP_oEmbed::fetch() 
  248. * @see WP_oEmbed::data2html() 
  249. * @since 2.9.0 
  250. * @access public 
  251. * @param string $url The URL to the content that should be attempted to be embedded. 
  252. * @param array|string $args Optional. Arguments, usually passed from a shortcode. Default empty. 
  253. * @return false|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed. 
  254. */ 
  255. public function get_html( $url, $args = '' ) { 
  256. $args = wp_parse_args( $args ); 
  257.  
  258. /** 
  259. * Filters the oEmbed result before any HTTP requests are made. 
  260. * This allows one to short-circuit the default logic, perhaps by 
  261. * replacing it with a routine that is more optimal for your setup. 
  262. * Passing a non-null value to the filter will effectively short-circuit retrieval,  
  263. * returning the passed value instead. 
  264. * @since 4.5.3 
  265. * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null. 
  266. * @param string $url The URL to the content that should be attempted to be embedded. 
  267. * @param array $args Optional. Arguments, usually passed from a shortcode. Default empty. 
  268. */ 
  269. $pre = apply_filters( 'pre_oembed_result', null, $url, $args ); 
  270.  
  271. if ( null !== $pre ) { 
  272. return $pre; 
  273.  
  274. $provider = $this->get_provider( $url, $args ); 
  275.  
  276. if ( ! $provider || false === $data = $this->fetch( $provider, $url, $args ) ) { 
  277. return false; 
  278.  
  279. /** 
  280. * Filters the HTML returned by the oEmbed provider. 
  281. * @since 2.9.0 
  282. * @param string $data The returned oEmbed HTML. 
  283. * @param string $url URL of the content to be embedded. 
  284. * @param array $args Optional arguments, usually passed from a shortcode. 
  285. */ 
  286. return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args ); 
  287.  
  288. /** 
  289. * Attempts to discover link tags at the given URL for an oEmbed provider. 
  290. * @since 2.9.0 
  291. * @access public 
  292. * @param string $url The URL that should be inspected for discovery `<link>` tags. 
  293. * @return false|string False on failure, otherwise the oEmbed provider URL. 
  294. */ 
  295. public function discover( $url ) { 
  296. $providers = array(); 
  297. $args = array( 
  298. 'limit_response_size' => 153600, // 150 KB 
  299. ); 
  300.  
  301. /** 
  302. * Filters oEmbed remote get arguments. 
  303. * @since 4.0.0 
  304. * @see WP_Http::request() 
  305. * @param array $args oEmbed remote get arguments. 
  306. * @param string $url URL to be inspected. 
  307. */ 
  308. $args = apply_filters( 'oembed_remote_get_args', $args, $url ); 
  309.  
  310. // Fetch URL content 
  311. $request = wp_safe_remote_get( $url, $args ); 
  312. if ( $html = wp_remote_retrieve_body( $request ) ) { 
  313.  
  314. /** 
  315. * Filters the link types that contain oEmbed provider URLs. 
  316. * @since 2.9.0 
  317. * @param array $format Array of oEmbed link types. Accepts 'application/json+oembed',  
  318. * 'text/xml+oembed', and 'application/xml+oembed' (incorrect,  
  319. * used by at least Vimeo). 
  320. */ 
  321. $linktypes = apply_filters( 'oembed_linktypes', array( 
  322. 'application/json+oembed' => 'json',  
  323. 'text/xml+oembed' => 'xml',  
  324. 'application/xml+oembed' => 'xml',  
  325. ) ); 
  326.  
  327. // Strip <body> 
  328. if ( $html_head_end = stripos( $html, '</head>' ) ) { 
  329. $html = substr( $html, 0, $html_head_end ); 
  330.  
  331. // Do a quick check 
  332. $tagfound = false; 
  333. foreach ( $linktypes as $linktype => $format ) { 
  334. if ( stripos($html, $linktype) ) { 
  335. $tagfound = true; 
  336. break; 
  337.  
  338. if ( $tagfound && preg_match_all( '#<link([^<>]+)/?>#iU', $html, $links ) ) { 
  339. foreach ( $links[1] as $link ) { 
  340. $atts = shortcode_parse_atts( $link ); 
  341.  
  342. if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) { 
  343. $providers[$linktypes[$atts['type']]] = htmlspecialchars_decode( $atts['href'] ); 
  344.  
  345. // Stop here if it's JSON (that's all we need) 
  346. if ( 'json' == $linktypes[$atts['type']] ) 
  347. break; 
  348.  
  349. // JSON is preferred to XML 
  350. if ( !empty($providers['json']) ) 
  351. return $providers['json']; 
  352. elseif ( !empty($providers['xml']) ) 
  353. return $providers['xml']; 
  354. else 
  355. return false; 
  356.  
  357. /** 
  358. * Connects to a oEmbed provider and returns the result. 
  359. * @since 2.9.0 
  360. * @access public 
  361. * @param string $provider The URL to the oEmbed provider. 
  362. * @param string $url The URL to the content that is desired to be embedded. 
  363. * @param array|string $args Optional. Arguments, usually passed from a shortcode. Default empty. 
  364. * @return false|object False on failure, otherwise the result in the form of an object. 
  365. */ 
  366. public function fetch( $provider, $url, $args = '' ) { 
  367. $args = wp_parse_args( $args, wp_embed_defaults( $url ) ); 
  368.  
  369. $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider ); 
  370. $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider ); 
  371. $provider = add_query_arg( 'url', urlencode($url), $provider ); 
  372.  
  373. /** 
  374. * Filters the oEmbed URL to be fetched. 
  375. * @since 2.9.0 
  376. * @param string $provider URL of the oEmbed provider. 
  377. * @param string $url URL of the content to be embedded. 
  378. * @param array $args Optional arguments, usually passed from a shortcode. 
  379. */ 
  380. $provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args ); 
  381.  
  382. foreach ( array( 'json', 'xml' ) as $format ) { 
  383. $result = $this->_fetch_with_format( $provider, $format ); 
  384. if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() ) 
  385. continue; 
  386. return ( $result && ! is_wp_error( $result ) ) ? $result : false; 
  387. return false; 
  388.  
  389. /** 
  390. * Fetches result from an oEmbed provider for a specific format and complete provider URL 
  391. * @since 3.0.0 
  392. * @access private 
  393. * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.) 
  394. * @param string $format Format to use 
  395. * @return false|object|WP_Error False on failure, otherwise the result in the form of an object. 
  396. */ 
  397. private function _fetch_with_format( $provider_url_with_args, $format ) { 
  398. $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args ); 
  399.  
  400. /** This filter is documented in wp-includes/class-oembed.php */ 
  401. $args = apply_filters( 'oembed_remote_get_args', array(), $provider_url_with_args ); 
  402.  
  403. $response = wp_safe_remote_get( $provider_url_with_args, $args ); 
  404. if ( 501 == wp_remote_retrieve_response_code( $response ) ) 
  405. return new WP_Error( 'not-implemented' ); 
  406. if ( ! $body = wp_remote_retrieve_body( $response ) ) 
  407. return false; 
  408. $parse_method = "_parse_$format"; 
  409. return $this->$parse_method( $body ); 
  410.  
  411. /** 
  412. * Parses a json response body. 
  413. * @since 3.0.0 
  414. * @access private 
  415. * @param string $response_body 
  416. * @return object|false 
  417. */ 
  418. private function _parse_json( $response_body ) { 
  419. $data = json_decode( trim( $response_body ) ); 
  420. return ( $data && is_object( $data ) ) ? $data : false; 
  421.  
  422. /** 
  423. * Parses an XML response body. 
  424. * @since 3.0.0 
  425. * @access private 
  426. * @param string $response_body 
  427. * @return object|false 
  428. */ 
  429. private function _parse_xml( $response_body ) { 
  430. if ( ! function_exists( 'libxml_disable_entity_loader' ) ) 
  431. return false; 
  432.  
  433. $loader = libxml_disable_entity_loader( true ); 
  434. $errors = libxml_use_internal_errors( true ); 
  435.  
  436. $return = $this->_parse_xml_body( $response_body ); 
  437.  
  438. libxml_use_internal_errors( $errors ); 
  439. libxml_disable_entity_loader( $loader ); 
  440.  
  441. return $return; 
  442.  
  443. /** 
  444. * Serves as a helper function for parsing an XML response body. 
  445. * @since 3.6.0 
  446. * @access private 
  447. * @param string $response_body 
  448. * @return object|false 
  449. */ 
  450. private function _parse_xml_body( $response_body ) { 
  451. if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument', false ) ) 
  452. return false; 
  453.  
  454. $dom = new DOMDocument; 
  455. $success = $dom->loadXML( $response_body ); 
  456. if ( ! $success ) 
  457. return false; 
  458.  
  459. if ( isset( $dom->doctype ) ) 
  460. return false; 
  461.  
  462. foreach ( $dom->childNodes as $child ) { 
  463. if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) 
  464. return false; 
  465.  
  466. $xml = simplexml_import_dom( $dom ); 
  467. if ( ! $xml ) 
  468. return false; 
  469.  
  470. $return = new stdClass; 
  471. foreach ( $xml as $key => $value ) { 
  472. $return->$key = (string) $value; 
  473.  
  474. return $return; 
  475.  
  476. /** 
  477. * Converts a data object from WP_oEmbed::fetch() and returns the HTML. 
  478. * @since 2.9.0 
  479. * @access public 
  480. * @param object $data A data object result from an oEmbed provider. 
  481. * @param string $url The URL to the content that is desired to be embedded. 
  482. * @return false|string False on error, otherwise the HTML needed to embed. 
  483. */ 
  484. public function data2html( $data, $url ) { 
  485. if ( ! is_object( $data ) || empty( $data->type ) ) 
  486. return false; 
  487.  
  488. $return = false; 
  489.  
  490. switch ( $data->type ) { 
  491. case 'photo': 
  492. if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) ) 
  493. break; 
  494. if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) ) 
  495. break; 
  496.  
  497. $title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : ''; 
  498. $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>'; 
  499. break; 
  500.  
  501. case 'video': 
  502. case 'rich': 
  503. if ( ! empty( $data->html ) && is_string( $data->html ) ) 
  504. $return = $data->html; 
  505. break; 
  506.  
  507. case 'link': 
  508. if ( ! empty( $data->title ) && is_string( $data->title ) ) 
  509. $return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>'; 
  510. break; 
  511.  
  512. default: 
  513. $return = false; 
  514.  
  515. /** 
  516. * Filters the returned oEmbed HTML. 
  517. * Use this filter to add support for custom data types, or to filter the result. 
  518. * @since 2.9.0 
  519. * @param string $return The returned oEmbed HTML. 
  520. * @param object $data A data object result from an oEmbed provider. 
  521. * @param string $url The URL of the content to be embedded. 
  522. */ 
  523. return apply_filters( 'oembed_dataparse', $return, $data, $url ); 
  524.  
  525. /** 
  526. * Strips any new lines from the HTML. 
  527. * @since 2.9.0 as strip_scribd_newlines() 
  528. * @since 3.0.0 
  529. * @access public 
  530. * @param string $html Existing HTML. 
  531. * @param object $data Data object from WP_oEmbed::data2html() 
  532. * @param string $url The original URL passed to oEmbed. 
  533. * @return string Possibly modified $html 
  534. */ 
  535. public function _strip_newlines( $html, $data, $url ) { 
  536. if ( false === strpos( $html, "\n" ) ) { 
  537. return $html; 
  538.  
  539. $count = 1; 
  540. $found = array(); 
  541. $token = '__PRE__'; 
  542. $search = array( "\t", "\n", "\r", ' ' ); 
  543. $replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' ); 
  544. $tokenized = str_replace( $search, $replace, $html ); 
  545.  
  546. preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER ); 
  547. foreach ( $matches as $i => $match ) { 
  548. $tag_html = str_replace( $replace, $search, $match[0] ); 
  549. $tag_token = $token . $i; 
  550.  
  551. $found[ $tag_token ] = $tag_html; 
  552. $html = str_replace( $tag_html, $tag_token, $html, $count ); 
  553.  
  554. $replaced = str_replace( $replace, $search, $html ); 
  555. $stripped = str_replace( array( "\r\n", "\n" ), '', $replaced ); 
  556. $pre = array_values( $found ); 
  557. $tokens = array_keys( $found ); 
  558.  
  559. return str_replace( $tokens, $pre, $stripped );