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