embed.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. <?php
  2. /**
  3. * oEmbed API: Top-level oEmbed functionality
  4. *
  5. * @package WordPress
  6. * @subpackage oEmbed
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Registers an embed handler.
  11. *
  12. * Should probably only be used for sites that do not support oEmbed.
  13. *
  14. * @since 2.9.0
  15. *
  16. * @global WP_Embed $wp_embed
  17. *
  18. * @param string $id An internal ID/name for the handler. Needs to be unique.
  19. * @param string $regex The regex that will be used to see if this handler should be used for a URL.
  20. * @param callable $callback The callback function that will be called if the regex is matched.
  21. * @param int $priority Optional. Used to specify the order in which the registered handlers will
  22. * be tested. Default 10.
  23. */
  24. function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
  25. global $wp_embed;
  26. $wp_embed->register_handler( $id, $regex, $callback, $priority );
  27. }
  28. /**
  29. * Unregisters a previously-registered embed handler.
  30. *
  31. * @since 2.9.0
  32. *
  33. * @global WP_Embed $wp_embed
  34. *
  35. * @param string $id The handler ID that should be removed.
  36. * @param int $priority Optional. The priority of the handler to be removed. Default 10.
  37. */
  38. function wp_embed_unregister_handler( $id, $priority = 10 ) {
  39. global $wp_embed;
  40. $wp_embed->unregister_handler( $id, $priority );
  41. }
  42. /**
  43. * Creates default array of embed parameters.
  44. *
  45. * The width defaults to the content width as specified by the theme. If the
  46. * theme does not specify a content width, then 500px is used.
  47. *
  48. * The default height is 1.5 times the width, or 1000px, whichever is smaller.
  49. *
  50. * The {@see 'embed_defaults'} filter can be used to adjust either of these values.
  51. *
  52. * @since 2.9.0
  53. *
  54. * @global int $content_width
  55. *
  56. * @param string $url Optional. The URL that should be embedded. Default empty.
  57. *
  58. * @return array Default embed parameters.
  59. */
  60. function wp_embed_defaults( $url = '' ) {
  61. if ( ! empty( $GLOBALS['content_width'] ) )
  62. $width = (int) $GLOBALS['content_width'];
  63. if ( empty( $width ) )
  64. $width = 500;
  65. $height = min( ceil( $width * 1.5 ), 1000 );
  66. /**
  67. * Filters the default array of embed dimensions.
  68. *
  69. * @since 2.9.0
  70. *
  71. * @param array $size An array of embed width and height values
  72. * in pixels (in that order).
  73. * @param string $url The URL that should be embedded.
  74. */
  75. return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url );
  76. }
  77. /**
  78. * Attempts to fetch the embed HTML for a provided URL using oEmbed.
  79. *
  80. * @since 2.9.0
  81. *
  82. * @see WP_oEmbed
  83. *
  84. * @param string $url The URL that should be embedded.
  85. * @param array $args Optional. Additional arguments and parameters for retrieving embed HTML.
  86. * Default empty.
  87. * @return false|string False on failure or the embed HTML on success.
  88. */
  89. function wp_oembed_get( $url, $args = '' ) {
  90. $oembed = _wp_oembed_get_object();
  91. return $oembed->get_html( $url, $args );
  92. }
  93. /**
  94. * Returns the initialized WP_oEmbed object.
  95. *
  96. * @since 2.9.0
  97. * @access private
  98. *
  99. * @staticvar WP_oEmbed $wp_oembed
  100. *
  101. * @return WP_oEmbed object.
  102. */
  103. function _wp_oembed_get_object() {
  104. static $wp_oembed = null;
  105. if ( is_null( $wp_oembed ) ) {
  106. $wp_oembed = new WP_oEmbed();
  107. }
  108. return $wp_oembed;
  109. }
  110. /**
  111. * Adds a URL format and oEmbed provider URL pair.
  112. *
  113. * @since 2.9.0
  114. *
  115. * @see WP_oEmbed
  116. *
  117. * @param string $format The format of URL that this provider can handle. You can use asterisks
  118. * as wildcards.
  119. * @param string $provider The URL to the oEmbed provider.
  120. * @param boolean $regex Optional. Whether the `$format` parameter is in a RegEx format. Default false.
  121. */
  122. function wp_oembed_add_provider( $format, $provider, $regex = false ) {
  123. if ( did_action( 'plugins_loaded' ) ) {
  124. $oembed = _wp_oembed_get_object();
  125. $oembed->providers[$format] = array( $provider, $regex );
  126. } else {
  127. WP_oEmbed::_add_provider_early( $format, $provider, $regex );
  128. }
  129. }
  130. /**
  131. * Removes an oEmbed provider.
  132. *
  133. * @since 3.5.0
  134. *
  135. * @see WP_oEmbed
  136. *
  137. * @param string $format The URL format for the oEmbed provider to remove.
  138. * @return bool Was the provider removed successfully?
  139. */
  140. function wp_oembed_remove_provider( $format ) {
  141. if ( did_action( 'plugins_loaded' ) ) {
  142. $oembed = _wp_oembed_get_object();
  143. if ( isset( $oembed->providers[ $format ] ) ) {
  144. unset( $oembed->providers[ $format ] );
  145. return true;
  146. }
  147. } else {
  148. WP_oEmbed::_remove_provider_early( $format );
  149. }
  150. return false;
  151. }
  152. /**
  153. * Determines if default embed handlers should be loaded.
  154. *
  155. * Checks to make sure that the embeds library hasn't already been loaded. If
  156. * it hasn't, then it will load the embeds library.
  157. *
  158. * @since 2.9.0
  159. *
  160. * @see wp_embed_register_handler()
  161. */
  162. function wp_maybe_load_embeds() {
  163. /**
  164. * Filters whether to load the default embed handlers.
  165. *
  166. * Returning a falsey value will prevent loading the default embed handlers.
  167. *
  168. * @since 2.9.0
  169. *
  170. * @param bool $maybe_load_embeds Whether to load the embeds library. Default true.
  171. */
  172. if ( ! apply_filters( 'load_default_embeds', true ) ) {
  173. return;
  174. }
  175. wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
  176. /**
  177. * Filters the audio embed handler callback.
  178. *
  179. * @since 3.6.0
  180. *
  181. * @param callable $handler Audio embed handler callback function.
  182. */
  183. wp_embed_register_handler( 'audio', '#^https?://.+?\.(' . join( '|', wp_get_audio_extensions() ) . ')$#i', apply_filters( 'wp_audio_embed_handler', 'wp_embed_handler_audio' ), 9999 );
  184. /**
  185. * Filters the video embed handler callback.
  186. *
  187. * @since 3.6.0
  188. *
  189. * @param callable $handler Video embed handler callback function.
  190. */
  191. wp_embed_register_handler( 'video', '#^https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')$#i', apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' ), 9999 );
  192. }
  193. /**
  194. * YouTube iframe embed handler callback.
  195. *
  196. * Catches YouTube iframe embed URLs that are not parsable by oEmbed but can be translated into a URL that is.
  197. *
  198. * @since 4.0.0
  199. *
  200. * @global WP_Embed $wp_embed
  201. *
  202. * @param array $matches The RegEx matches from the provided regex when calling
  203. * wp_embed_register_handler().
  204. * @param array $attr Embed attributes.
  205. * @param string $url The original URL that was matched by the regex.
  206. * @param array $rawattr The original unmodified attributes.
  207. * @return string The embed HTML.
  208. */
  209. function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) {
  210. global $wp_embed;
  211. $embed = $wp_embed->autoembed( sprintf( "https://youtube.com/watch?v=%s", urlencode( $matches[2] ) ) );
  212. /**
  213. * Filters the YoutTube embed output.
  214. *
  215. * @since 4.0.0
  216. *
  217. * @see wp_embed_handler_youtube()
  218. *
  219. * @param string $embed YouTube embed output.
  220. * @param array $attr An array of embed attributes.
  221. * @param string $url The original URL that was matched by the regex.
  222. * @param array $rawattr The original unmodified attributes.
  223. */
  224. return apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr );
  225. }
  226. /**
  227. * Audio embed handler callback.
  228. *
  229. * @since 3.6.0
  230. *
  231. * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
  232. * @param array $attr Embed attributes.
  233. * @param string $url The original URL that was matched by the regex.
  234. * @param array $rawattr The original unmodified attributes.
  235. * @return string The embed HTML.
  236. */
  237. function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) {
  238. $audio = sprintf( '[audio src="%s" /]', esc_url( $url ) );
  239. /**
  240. * Filters the audio embed output.
  241. *
  242. * @since 3.6.0
  243. *
  244. * @param string $audio Audio embed output.
  245. * @param array $attr An array of embed attributes.
  246. * @param string $url The original URL that was matched by the regex.
  247. * @param array $rawattr The original unmodified attributes.
  248. */
  249. return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr );
  250. }
  251. /**
  252. * Video embed handler callback.
  253. *
  254. * @since 3.6.0
  255. *
  256. * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
  257. * @param array $attr Embed attributes.
  258. * @param string $url The original URL that was matched by the regex.
  259. * @param array $rawattr The original unmodified attributes.
  260. * @return string The embed HTML.
  261. */
  262. function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) {
  263. $dimensions = '';
  264. if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) {
  265. $dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] );
  266. $dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] );
  267. }
  268. $video = sprintf( '[video %s src="%s" /]', $dimensions, esc_url( $url ) );
  269. /**
  270. * Filters the video embed output.
  271. *
  272. * @since 3.6.0
  273. *
  274. * @param string $video Video embed output.
  275. * @param array $attr An array of embed attributes.
  276. * @param string $url The original URL that was matched by the regex.
  277. * @param array $rawattr The original unmodified attributes.
  278. */
  279. return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr );
  280. }
  281. /**
  282. * Registers the oEmbed REST API route.
  283. *
  284. * @since 4.4.0
  285. */
  286. function wp_oembed_register_route() {
  287. $controller = new WP_oEmbed_Controller();
  288. $controller->register_routes();
  289. }
  290. /**
  291. * Adds oEmbed discovery links in the website <head>.
  292. *
  293. * @since 4.4.0
  294. */
  295. function wp_oembed_add_discovery_links() {
  296. $output = '';
  297. if ( is_singular() ) {
  298. $output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
  299. if ( class_exists( 'SimpleXMLElement' ) ) {
  300. $output .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
  301. }
  302. }
  303. /**
  304. * Filters the oEmbed discovery links HTML.
  305. *
  306. * @since 4.4.0
  307. *
  308. * @param string $output HTML of the discovery links.
  309. */
  310. echo apply_filters( 'oembed_discovery_links', $output );
  311. }
  312. /**
  313. * Adds the necessary JavaScript to communicate with the embedded iframes.
  314. *
  315. * @since 4.4.0
  316. */
  317. function wp_oembed_add_host_js() {
  318. wp_enqueue_script( 'wp-embed' );
  319. }
  320. /**
  321. * Retrieves the URL to embed a specific post in an iframe.
  322. *
  323. * @since 4.4.0
  324. *
  325. * @param int|WP_Post $post Optional. Post ID or object. Defaults to the current post.
  326. * @return string|false The post embed URL on success, false if the post doesn't exist.
  327. */
  328. function get_post_embed_url( $post = null ) {
  329. $post = get_post( $post );
  330. if ( ! $post ) {
  331. return false;
  332. }
  333. $embed_url = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' );
  334. $path_conflict = get_page_by_path( str_replace( home_url(), '', $embed_url ), OBJECT, get_post_types( array( 'public' => true ) ) );
  335. if ( ! get_option( 'permalink_structure' ) || $path_conflict ) {
  336. $embed_url = add_query_arg( array( 'embed' => 'true' ), get_permalink( $post ) );
  337. }
  338. /**
  339. * Filters the URL to embed a specific post.
  340. *
  341. * @since 4.4.0
  342. *
  343. * @param string $embed_url The post embed URL.
  344. * @param WP_Post $post The corresponding post object.
  345. */
  346. return esc_url_raw( apply_filters( 'post_embed_url', $embed_url, $post ) );
  347. }
  348. /**
  349. * Retrieves the oEmbed endpoint URL for a given permalink.
  350. *
  351. * Pass an empty string as the first argument to get the endpoint base URL.
  352. *
  353. * @since 4.4.0
  354. *
  355. * @param string $permalink Optional. The permalink used for the `url` query arg. Default empty.
  356. * @param string $format Optional. The requested response format. Default 'json'.
  357. * @return string The oEmbed endpoint URL.
  358. */
  359. function get_oembed_endpoint_url( $permalink = '', $format = 'json' ) {
  360. $url = rest_url( 'oembed/1.0/embed' );
  361. if ( '' !== $permalink ) {
  362. $url = add_query_arg( array(
  363. 'url' => urlencode( $permalink ),
  364. 'format' => ( 'json' !== $format ) ? $format : false,
  365. ), $url );
  366. }
  367. /**
  368. * Filters the oEmbed endpoint URL.
  369. *
  370. * @since 4.4.0
  371. *
  372. * @param string $url The URL to the oEmbed endpoint.
  373. * @param string $permalink The permalink used for the `url` query arg.
  374. * @param string $format The requested response format.
  375. */
  376. return apply_filters( 'oembed_endpoint_url', $url, $permalink, $format );
  377. }
  378. /**
  379. * Retrieves the embed code for a specific post.
  380. *
  381. * @since 4.4.0
  382. *
  383. * @param int $width The width for the response.
  384. * @param int $height The height for the response.
  385. * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
  386. * @return string|false Embed code on success, false if post doesn't exist.
  387. */
  388. function get_post_embed_html( $width, $height, $post = null ) {
  389. $post = get_post( $post );
  390. if ( ! $post ) {
  391. return false;
  392. }
  393. $embed_url = get_post_embed_url( $post );
  394. $output = '<blockquote class="wp-embedded-content"><a href="' . esc_url( get_permalink( $post ) ) . '">' . get_the_title( $post ) . "</a></blockquote>\n";
  395. $output .= "<script type='text/javascript'>\n";
  396. $output .= "<!--//--><![CDATA[//><!--\n";
  397. if ( SCRIPT_DEBUG ) {
  398. $output .= file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' );
  399. } else {
  400. /*
  401. * If you're looking at a src version of this file, you'll see an "include"
  402. * statement below. This is used by the `grunt build` process to directly
  403. * include a minified version of wp-embed.js, instead of using the
  404. * file_get_contents() method from above.
  405. *
  406. * If you're looking at a build version of this file, you'll see a string of
  407. * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
  408. * and edit wp-embed.js directly.
  409. */
  410. $output .=<<<JS
  411. !function(a,b){"use strict";function c(){if(!e){e=!0;var a,c,d,f,g=-1!==navigator.appVersion.indexOf("MSIE 10"),h=!!navigator.userAgent.match(/Trident.*rv:11\./),i=b.querySelectorAll("iframe.wp-embedded-content");for(c=0;c<i.length;c++){if(d=i[c],!d.getAttribute("data-secret"))f=Math.random().toString(36).substr(2,10),d.src+="#?secret="+f,d.setAttribute("data-secret",f);if(g||h)a=d.cloneNode(!0),a.removeAttribute("security"),d.parentNode.replaceChild(a,d)}}}var d=!1,e=!1;if(b.querySelector)if(a.addEventListener)d=!0;if(a.wp=a.wp||{},!a.wp.receiveEmbedMessage)if(a.wp.receiveEmbedMessage=function(c){var d=c.data;if(d.secret||d.message||d.value)if(!/[^a-zA-Z0-9]/.test(d.secret)){var e,f,g,h,i,j=b.querySelectorAll('iframe[data-secret="'+d.secret+'"]'),k=b.querySelectorAll('blockquote[data-secret="'+d.secret+'"]');for(e=0;e<k.length;e++)k[e].style.display="none";for(e=0;e<j.length;e++)if(f=j[e],c.source===f.contentWindow){if(f.removeAttribute("style"),"height"===d.message){if(g=parseInt(d.value,10),g>1e3)g=1e3;else if(~~g<200)g=200;f.height=g}if("link"===d.message)if(h=b.createElement("a"),i=b.createElement("a"),h.href=f.getAttribute("src"),i.href=d.value,i.host===h.host)if(b.activeElement===f)a.top.location.href=d.value}else;}},d)a.addEventListener("message",a.wp.receiveEmbedMessage,!1),b.addEventListener("DOMContentLoaded",c,!1),a.addEventListener("load",c,!1)}(window,document);
  412. JS;
  413. }
  414. $output .= "\n//--><!]]>";
  415. $output .= "\n</script>";
  416. $output .= sprintf(
  417. '<iframe sandbox="allow-scripts" security="restricted" src="%1$s" width="%2$d" height="%3$d" title="%4$s" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>',
  418. esc_url( $embed_url ),
  419. absint( $width ),
  420. absint( $height ),
  421. esc_attr(
  422. sprintf(
  423. /* translators: 1: post title, 2: site name */
  424. __( '&#8220;%1$s&#8221; &#8212; %2$s' ),
  425. get_the_title( $post ),
  426. get_bloginfo( 'name' )
  427. )
  428. )
  429. );
  430. /**
  431. * Filters the embed HTML output for a given post.
  432. *
  433. * @since 4.4.0
  434. *
  435. * @param string $output The default iframe tag to display embedded content.
  436. * @param WP_Post $post Current post object.
  437. * @param int $width Width of the response.
  438. * @param int $height Height of the response.
  439. */
  440. return apply_filters( 'embed_html', $output, $post, $width, $height );
  441. }
  442. /**
  443. * Retrieves the oEmbed response data for a given post.
  444. *
  445. * @since 4.4.0
  446. *
  447. * @param WP_Post|int $post Post object or ID.
  448. * @param int $width The requested width.
  449. * @return array|false Response data on success, false if post doesn't exist.
  450. */
  451. function get_oembed_response_data( $post, $width ) {
  452. $post = get_post( $post );
  453. $width = absint( $width );
  454. if ( ! $post ) {
  455. return false;
  456. }
  457. if ( 'publish' !== get_post_status( $post ) ) {
  458. return false;
  459. }
  460. /**
  461. * Filters the allowed minimum and maximum widths for the oEmbed response.
  462. *
  463. * @since 4.4.0
  464. *
  465. * @param array $min_max_width {
  466. * Minimum and maximum widths for the oEmbed response.
  467. *
  468. * @type int $min Minimum width. Default 200.
  469. * @type int $max Maximum width. Default 600.
  470. * }
  471. */
  472. $min_max_width = apply_filters( 'oembed_min_max_width', array(
  473. 'min' => 200,
  474. 'max' => 600
  475. ) );
  476. $width = min( max( $min_max_width['min'], $width ), $min_max_width['max'] );
  477. $height = max( ceil( $width / 16 * 9 ), 200 );
  478. $data = array(
  479. 'version' => '1.0',
  480. 'provider_name' => get_bloginfo( 'name' ),
  481. 'provider_url' => get_home_url(),
  482. 'author_name' => get_bloginfo( 'name' ),
  483. 'author_url' => get_home_url(),
  484. 'title' => $post->post_title,
  485. 'type' => 'link',
  486. );
  487. $author = get_userdata( $post->post_author );
  488. if ( $author ) {
  489. $data['author_name'] = $author->display_name;
  490. $data['author_url'] = get_author_posts_url( $author->ID );
  491. }
  492. /**
  493. * Filters the oEmbed response data.
  494. *
  495. * @since 4.4.0
  496. *
  497. * @param array $data The response data.
  498. * @param WP_Post $post The post object.
  499. * @param int $width The requested width.
  500. * @param int $height The calculated height.
  501. */
  502. return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
  503. }
  504. /**
  505. * Filters the oEmbed response data to return an iframe embed code.
  506. *
  507. * @since 4.4.0
  508. *
  509. * @param array $data The response data.
  510. * @param WP_Post $post The post object.
  511. * @param int $width The requested width.
  512. * @param int $height The calculated height.
  513. * @return array The modified response data.
  514. */
  515. function get_oembed_response_data_rich( $data, $post, $width, $height ) {
  516. $data['width'] = absint( $width );
  517. $data['height'] = absint( $height );
  518. $data['type'] = 'rich';
  519. $data['html'] = get_post_embed_html( $width, $height, $post );
  520. // Add post thumbnail to response if available.
  521. $thumbnail_id = false;
  522. if ( has_post_thumbnail( $post->ID ) ) {
  523. $thumbnail_id = get_post_thumbnail_id( $post->ID );
  524. }
  525. if ( 'attachment' === get_post_type( $post ) ) {
  526. if ( wp_attachment_is_image( $post ) ) {
  527. $thumbnail_id = $post->ID;
  528. } else if ( wp_attachment_is( 'video', $post ) ) {
  529. $thumbnail_id = get_post_thumbnail_id( $post );
  530. $data['type'] = 'video';
  531. }
  532. }
  533. if ( $thumbnail_id ) {
  534. list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) );
  535. $data['thumbnail_url'] = $thumbnail_url;
  536. $data['thumbnail_width'] = $thumbnail_width;
  537. $data['thumbnail_height'] = $thumbnail_height;
  538. }
  539. return $data;
  540. }
  541. /**
  542. * Ensures that the specified format is either 'json' or 'xml'.
  543. *
  544. * @since 4.4.0
  545. *
  546. * @param string $format The oEmbed response format. Accepts 'json' or 'xml'.
  547. * @return string The format, either 'xml' or 'json'. Default 'json'.
  548. */
  549. function wp_oembed_ensure_format( $format ) {
  550. if ( ! in_array( $format, array( 'json', 'xml' ), true ) ) {
  551. return 'json';
  552. }
  553. return $format;
  554. }
  555. /**
  556. * Hooks into the REST API output to print XML instead of JSON.
  557. *
  558. * This is only done for the oEmbed API endpoint,
  559. * which supports both formats.
  560. *
  561. * @access private
  562. * @since 4.4.0
  563. *
  564. * @param bool $served Whether the request has already been served.
  565. * @param WP_HTTP_ResponseInterface $result Result to send to the client. Usually a WP_REST_Response.
  566. * @param WP_REST_Request $request Request used to generate the response.
  567. * @param WP_REST_Server $server Server instance.
  568. * @return true
  569. */
  570. function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) {
  571. $params = $request->get_params();
  572. if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) {
  573. return $served;
  574. }
  575. if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) {
  576. return $served;
  577. }
  578. // Embed links inside the request.
  579. $data = $server->response_to_data( $result, false );
  580. if ( ! class_exists( 'SimpleXMLElement' ) ) {
  581. status_header( 501 );
  582. die( get_status_header_desc( 501 ) );
  583. }
  584. $result = _oembed_create_xml( $data );
  585. // Bail if there's no XML.
  586. if ( ! $result ) {
  587. status_header( 501 );
  588. return get_status_header_desc( 501 );
  589. }
  590. if ( ! headers_sent() ) {
  591. $server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) );
  592. }
  593. echo $result;
  594. return true;
  595. }
  596. /**
  597. * Creates an XML string from a given array.
  598. *
  599. * @since 4.4.0
  600. * @access private
  601. *
  602. * @param array $data The original oEmbed response data.
  603. * @param SimpleXMLElement $node Optional. XML node to append the result to recursively.
  604. * @return string|false XML string on success, false on error.
  605. */
  606. function _oembed_create_xml( $data, $node = null ) {
  607. if ( ! is_array( $data ) || empty( $data ) ) {
  608. return false;
  609. }
  610. if ( null === $node ) {
  611. $node = new SimpleXMLElement( '<oembed></oembed>' );
  612. }
  613. foreach ( $data as $key => $value ) {
  614. if ( is_numeric( $key ) ) {
  615. $key = 'oembed';
  616. }
  617. if ( is_array( $value ) ) {
  618. $item = $node->addChild( $key );
  619. _oembed_create_xml( $value, $item );
  620. } else {
  621. $node->addChild( $key, esc_html( $value ) );
  622. }
  623. }
  624. return $node->asXML();
  625. }
  626. /**
  627. * Filters the given oEmbed HTML.
  628. *
  629. * If the `$url` isn't on the trusted providers list,
  630. * we need to filter the HTML heavily for security.
  631. *
  632. * Only filters 'rich' and 'html' response types.
  633. *
  634. * @since 4.4.0
  635. *
  636. * @param string $result The oEmbed HTML result.
  637. * @param object $data A data object result from an oEmbed provider.
  638. * @param string $url The URL of the content to be embedded.
  639. * @return string The filtered and sanitized oEmbed result.
  640. */
  641. function wp_filter_oembed_result( $result, $data, $url ) {
  642. if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ) ) ) {
  643. return $result;
  644. }
  645. $wp_oembed = _wp_oembed_get_object();
  646. // Don't modify the HTML for trusted providers.
  647. if ( false !== $wp_oembed->get_provider( $url, array( 'discover' => false ) ) ) {
  648. return $result;
  649. }
  650. $allowed_html = array(
  651. 'a' => array(
  652. 'href' => true,
  653. ),
  654. 'blockquote' => array(),
  655. 'iframe' => array(
  656. 'src' => true,
  657. 'width' => true,
  658. 'height' => true,
  659. 'frameborder' => true,
  660. 'marginwidth' => true,
  661. 'marginheight' => true,
  662. 'scrolling' => true,
  663. 'title' => true,
  664. ),
  665. );
  666. $html = wp_kses( $result, $allowed_html );
  667. preg_match( '|(<blockquote>.*?</blockquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content );
  668. // We require at least the iframe to exist.
  669. if ( empty( $content[2] ) ) {
  670. return false;
  671. }
  672. $html = $content[1] . $content[2];
  673. preg_match( '/ src=([\'"])(.*?)\1/', $html, $results );
  674. if ( ! empty( $results ) ) {
  675. $secret = wp_generate_password( 10, false );
  676. $url = esc_url( "{$results[2]}#?secret=$secret" );
  677. $q = $results[1];
  678. $html = str_replace( $results[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html );
  679. $html = str_replace( '<blockquote', "<blockquote data-secret=\"$secret\"", $html );
  680. }
  681. $allowed_html['blockquote']['data-secret'] = true;
  682. $allowed_html['iframe']['data-secret'] = true;
  683. $html = wp_kses( $html, $allowed_html );
  684. if ( ! empty( $content[1] ) ) {
  685. // We have a blockquote to fall back on. Hide the iframe by default.
  686. $html = str_replace( '<iframe', '<iframe style="position: absolute; clip: rect(1px, 1px, 1px, 1px);"', $html );
  687. $html = str_replace( '<blockquote', '<blockquote class="wp-embedded-content"', $html );
  688. }
  689. $html = str_ireplace( '<iframe', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $html );
  690. return $html;
  691. }
  692. /**
  693. * Filters the string in the 'more' link displayed after a trimmed excerpt.
  694. *
  695. * Replaces '[...]' (appended to automatically generated excerpts) with an
  696. * ellipsis and a "Continue reading" link in the embed template.
  697. *
  698. * @since 4.4.0
  699. *
  700. * @param string $more_string Default 'more' string.
  701. * @return string 'Continue reading' link prepended with an ellipsis.
  702. */
  703. function wp_embed_excerpt_more( $more_string ) {
  704. if ( ! is_embed() ) {
  705. return $more_string;
  706. }
  707. $link = sprintf( '<a href="%1$s" class="wp-embed-more" target="_top">%2$s</a>',
  708. esc_url( get_permalink() ),
  709. /* translators: %s: Name of current post */
  710. sprintf( __( 'Continue reading %s' ), '<span class="screen-reader-text">' . get_the_title() . '</span>' )
  711. );
  712. return ' &hellip; ' . $link;
  713. }
  714. /**
  715. * Displays the post excerpt for the embed template.
  716. *
  717. * Intended to be used in 'The Loop'.
  718. *
  719. * @since 4.4.0
  720. */
  721. function the_excerpt_embed() {
  722. $output = get_the_excerpt();
  723. /**
  724. * Filters the post excerpt for the embed template.
  725. *
  726. * @since 4.4.0
  727. *
  728. * @param string $output The current post excerpt.
  729. */
  730. echo apply_filters( 'the_excerpt_embed', $output );
  731. }
  732. /**
  733. * Filters the post excerpt for the embed template.
  734. *
  735. * Shows players for video and audio attachments.
  736. *
  737. * @since 4.4.0
  738. *
  739. * @param string $content The current post excerpt.
  740. * @return string The modified post excerpt.
  741. */
  742. function wp_embed_excerpt_attachment( $content ) {
  743. if ( is_attachment() ) {
  744. return prepend_attachment( '' );
  745. }
  746. return $content;
  747. }
  748. /**
  749. * Enqueue embed iframe default CSS and JS & fire do_action('enqueue_embed_scripts')
  750. *
  751. * Enqueue PNG fallback CSS for embed iframe for legacy versions of IE.
  752. *
  753. * Allows plugins to queue scripts for the embed iframe end using wp_enqueue_script().
  754. * Runs first in oembed_head().
  755. *
  756. * @since 4.4.0
  757. */
  758. function enqueue_embed_scripts() {
  759. wp_enqueue_style( 'wp-embed-template-ie' );
  760. /**
  761. * Fires when scripts and styles are enqueued for the embed iframe.
  762. *
  763. * @since 4.4.0
  764. */
  765. do_action( 'enqueue_embed_scripts' );
  766. }
  767. /**
  768. * Prints the CSS in the embed iframe header.
  769. *
  770. * @since 4.4.0
  771. */
  772. function print_embed_styles() {
  773. ?>
  774. <style type="text/css">
  775. <?php
  776. if ( SCRIPT_DEBUG ) {
  777. readfile( ABSPATH . WPINC . "/css/wp-embed-template.css" );
  778. } else {
  779. /*
  780. * If you're looking at a src version of this file, you'll see an "include"
  781. * statement below. This is used by the `grunt build` process to directly
  782. * include a minified version of wp-oembed-embed.css, instead of using the
  783. * readfile() method from above.
  784. *
  785. * If you're looking at a build version of this file, you'll see a string of
  786. * minified CSS. If you need to debug it, please turn on SCRIPT_DEBUG
  787. * and edit wp-embed-template.css directly.
  788. */
  789. ?>
  790. body,html{padding:0;margin:0}body{font-family:sans-serif}.wp-embed,.wp-embed-share-input{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif}.screen-reader-text{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal!important}.dashicons{display:inline-block;width:20px;height:20px;background-color:transparent;background-repeat:no-repeat;background-size:20px;background-position:center;transition:background .1s ease-in;position:relative;top:5px}.dashicons-no{background-image:url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M15.55%2013.7l-2.19%202.06-3.42-3.65-3.64%203.43-2.06-2.18%203.64-3.43-3.42-3.64%202.18-2.06%203.43%203.64%203.64-3.42%202.05%202.18-3.64%203.43z%27%20fill%3D%27%23fff%27%2F%3E%3C%2Fsvg%3E")}.dashicons-admin-comments{background-image:url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M5%202h9q.82%200%201.41.59T16%204v7q0%20.82-.59%201.41T14%2013h-2l-5%205v-5H5q-.82%200-1.41-.59T3%2011V4q0-.82.59-1.41T5%202z%27%20fill%3D%27%2382878c%27%2F%3E%3C%2Fsvg%3E")}.wp-embed-comments a:hover .dashicons-admin-comments{background-image:url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M5%202h9q.82%200%201.41.59T16%204v7q0%20.82-.59%201.41T14%2013h-2l-5%205v-5H5q-.82%200-1.41-.59T3%2011V4q0-.82.59-1.41T5%202z%27%20fill%3D%27%230073aa%27%2F%3E%3C%2Fsvg%3E")}.dashicons-share{background-image:url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M14.5%2012q1.24%200%202.12.88T17.5%2015t-.88%202.12-2.12.88-2.12-.88T11.5%2015q0-.34.09-.69l-4.38-2.3Q6.32%2013%205%2013q-1.24%200-2.12-.88T2%2010t.88-2.12T5%207q1.3%200%202.21.99l4.38-2.3q-.09-.35-.09-.69%200-1.24.88-2.12T14.5%202t2.12.88T17.5%205t-.88%202.12T14.5%208q-1.3%200-2.21-.99l-4.38%202.3Q8%209.66%208%2010t-.09.69l4.38%202.3q.89-.99%202.21-.99z%27%20fill%3D%27%2382878c%27%2F%3E%3C%2Fsvg%3E");display:none}.js .dashicons-share{display:inline-block}.wp-embed-share-dialog-open:hover .dashicons-share{background-image:url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M14.5%2012q1.24%200%202.12.88T17.5%2015t-.88%202.12-2.12.88-2.12-.88T11.5%2015q0-.34.09-.69l-4.38-2.3Q6.32%2013%205%2013q-1.24%200-2.12-.88T2%2010t.88-2.12T5%207q1.3%200%202.21.99l4.38-2.3q-.09-.35-.09-.69%200-1.24.88-2.12T14.5%202t2.12.88T17.5%205t-.88%202.12T14.5%208q-1.3%200-2.21-.99l-4.38%202.3Q8%209.66%208%2010t-.09.69l4.38%202.3q.89-.99%202.21-.99z%27%20fill%3D%27%230073aa%27%2F%3E%3C%2Fsvg%3E")}.wp-embed{padding:25px;font-size:14px;font-weight:400;line-height:1.5;color:#82878c;background:#fff;border:1px solid #e5e5e5;box-shadow:0 1px 1px rgba(0,0,0,.05);overflow:auto;zoom:1}.wp-embed a{color:#82878c;text-decoration:none}.wp-embed a:hover{text-decoration:underline}.wp-embed-featured-image{margin-bottom:20px}.wp-embed-featured-image img{width:100%;height:auto;border:none}.wp-embed-featured-image.square{float:left;max-width:160px;margin-right:20px}.wp-embed p{margin:0}p.wp-embed-heading{margin:0 0 15px;font-weight:600;font-size:22px;line-height:1.3}.wp-embed-heading a{color:#32373c}.wp-embed .wp-embed-more{color:#b4b9be}.wp-embed-footer{display:table;width:100%;margin-top:30px}.wp-embed-site-icon{position:absolute;top:50%;left:0;-webkit-transform:translateY(-50%);transform:translateY(-50%);height:25px;width:25px;border:0}.wp-embed-site-title{font-weight:600;line-height:25px}.wp-embed-site-title a{position:relative;display:inline-block;padding-left:35px}.wp-embed-meta,.wp-embed-site-title{display:table-cell}.wp-embed-meta{text-align:right;white-space:nowrap;vertical-align:middle}.wp-embed-comments,.wp-embed-share{display:inline}.wp-embed-comments a,.wp-embed-share-tab-button{display:inline-block}.wp-embed-meta a:hover{text-decoration:none;color:#0073aa}.wp-embed-comments a{line-height:25px}.wp-embed-comments+.wp-embed-share{margin-left:10px}.wp-embed-share-dialog{position:absolute;top:0;left:0;right:0;bottom:0;background-color:#222;background-color:rgba(10,10,10,.9);color:#fff;opacity:1;transition:opacity .25s ease-in-out}.wp-embed-share-dialog.hidden{opacity:0;visibility:hidden}.wp-embed-share-dialog-close,.wp-embed-share-dialog-open{margin:-8px 0 0;padding:0;background:0 0;border:none;cursor:pointer;outline:0}.wp-embed-share-dialog-close .dashicons,.wp-embed-share-dialog-open .dashicons{padding:4px}.wp-embed-share-dialog-open .dashicons{top:8px}.wp-embed-share-dialog-close:focus .dashicons,.wp-embed-share-dialog-open:focus .dashicons{box-shadow:0 0 0 1px #5b9dd9,0 0 2px 1px rgba(30,140,190,.8);border-radius:100%}.wp-embed-share-dialog-close{position:absolute;top:20px;right:20px;font-size:22px}.wp-embed-share-dialog-close:hover{text-decoration:none}.wp-embed-share-dialog-close .dashicons{height:24px;width:24px;background-size:24px}.wp-embed-share-dialog-content{height:100%;-webkit-transform-style:preserve-3d;transform-style:preserve-3d;overflow:hidden}.wp-embed-share-dialog-text{margin-top:25px;padding:20px}.wp-embed-share-tabs{margin:0 0 20px;padding:0;list-style:none}.wp-embed-share-tab-button button{margin:0;padding:0;border:none;background:0 0;font-size:16px;line-height:1.3;color:#aaa;cursor:pointer;transition:color .1s ease-in}.wp-embed-share-tab-button [aria-selected=true],.wp-embed-share-tab-button button:hover{color:#fff}.wp-embed-share-tab-button+.wp-embed-share-tab-button{margin:0 0 0 10px;padding:0 0 0 11px;border-left:1px solid #aaa}.wp-embed-share-tab[aria-hidden=true]{display:none}p.wp-embed-share-description{margin:0;font-size:14px;line-height:1;font-style:italic;color:#aaa}.wp-embed-share-input{box-sizing:border-box;width:100%;border:none;height:28px;margin:0 0 10px;padding:0 5px;font-size:14px;font-weight:400;line-height:1.5;resize:none;cursor:text}textarea.wp-embed-share-input{height:72px}html[dir=rtl] .wp-embed-featured-image.square{float:right;margin-right:0;margin-left:20px}html[dir=rtl] .wp-embed-site-title a{padding-left:0;padding-right:35px}html[dir=rtl] .wp-embed-site-icon{margin-right:0;margin-left:10px;left:auto;right:0}html[dir=rtl] .wp-embed-meta{text-align:left}html[dir=rtl] .wp-embed-share{margin-left:0;margin-right:10px}html[dir=rtl] .wp-embed-share-dialog-close{right:auto;left:20px}html[dir=rtl] .wp-embed-share-tab-button+.wp-embed-share-tab-button{margin:0 10px 0 0;padding:0 11px 0 0;border-left:none;border-right:1px solid #aaa}
  791. <?php
  792. }
  793. ?>
  794. </style>
  795. <?php
  796. }
  797. /**
  798. * Prints the JavaScript in the embed iframe header.
  799. *
  800. * @since 4.4.0
  801. */
  802. function print_embed_scripts() {
  803. ?>
  804. <script type="text/javascript">
  805. <?php
  806. if ( SCRIPT_DEBUG ) {
  807. readfile( ABSPATH . WPINC . "/js/wp-embed-template.js" );
  808. } else {
  809. /*
  810. * If you're looking at a src version of this file, you'll see an "include"
  811. * statement below. This is used by the `grunt build` process to directly
  812. * include a minified version of wp-embed-template.js, instead of using the
  813. * readfile() method from above.
  814. *
  815. * If you're looking at a build version of this file, you'll see a string of
  816. * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
  817. * and edit wp-embed-template.js directly.
  818. */
  819. ?>
  820. !function(a,b){"use strict";function c(b,c){a.parent.postMessage({message:b,value:c,secret:g},"*")}function d(){function d(){l.className=l.className.replace("hidden",""),b.querySelector('.wp-embed-share-tab-button [aria-selected="true"]').focus()}function e(){l.className+=" hidden",b.querySelector(".wp-embed-share-dialog-open").focus()}function f(a){var c=b.querySelector('.wp-embed-share-tab-button [aria-selected="true"]');c.setAttribute("aria-selected","false"),b.querySelector("#"+c.getAttribute("aria-controls")).setAttribute("aria-hidden","true"),a.target.setAttribute("aria-selected","true"),b.querySelector("#"+a.target.getAttribute("aria-controls")).setAttribute("aria-hidden","false")}function g(a){var c,d,e=a.target,f=e.parentElement.previousElementSibling,g=e.parentElement.nextElementSibling;if(37===a.keyCode)c=f;else{if(39!==a.keyCode)return!1;c=g}"rtl"===b.documentElement.getAttribute("dir")&&(c=c===f?g:f),c&&(d=c.firstElementChild,e.setAttribute("tabindex","-1"),e.setAttribute("aria-selected",!1),b.querySelector("#"+e.getAttribute("aria-controls")).setAttribute("aria-hidden","true"),d.setAttribute("tabindex","0"),d.setAttribute("aria-selected","true"),d.focus(),b.querySelector("#"+d.getAttribute("aria-controls")).setAttribute("aria-hidden","false"))}function h(a){var c=b.querySelector('.wp-embed-share-tab-button [aria-selected="true"]');n!==a.target||a.shiftKey?c===a.target&&a.shiftKey&&(n.focus(),a.preventDefault()):(c.focus(),a.preventDefault())}function i(a){var b,d=a.target;b=d.hasAttribute("href")?d.getAttribute("href"):d.parentElement.getAttribute("href"),b&&(c("link",b),a.preventDefault())}if(!k){k=!0;var j,l=b.querySelector(".wp-embed-share-dialog"),m=b.querySelector(".wp-embed-share-dialog-open"),n=b.querySelector(".wp-embed-share-dialog-close"),o=b.querySelectorAll(".wp-embed-share-input"),p=b.querySelectorAll(".wp-embed-share-tab-button button"),q=b.querySelector(".wp-embed-featured-image img");if(o)for(j=0;j<o.length;j++)o[j].addEventListener("click",function(a){a.target.select()});if(m&&m.addEventListener("click",function(){d()}),n&&n.addEventListener("click",function(){e()}),p)for(j=0;j<p.length;j++)p[j].addEventListener("click",f),p[j].addEventListener("keydown",g);b.addEventListener("keydown",function(a){27===a.keyCode&&-1===l.className.indexOf("hidden")?e():9===a.keyCode&&h(a)},!1),a.self!==a.top&&(c("height",Math.ceil(b.body.getBoundingClientRect().height)),q&&q.addEventListener("load",function(){c("height",Math.ceil(b.body.getBoundingClientRect().height))}),b.addEventListener("click",i))}}function e(){a.self!==a.top&&(clearTimeout(i),i=setTimeout(function(){c("height",Math.ceil(b.body.getBoundingClientRect().height))},100))}function f(){a.self===a.top||g||(g=a.location.hash.replace(/.*secret=([\d\w]{10}).*/,"$1"),clearTimeout(h),h=setTimeout(function(){f()},100))}var g,h,i,j=b.querySelector&&a.addEventListener,k=!1;j&&(f(),b.documentElement.className=b.documentElement.className.replace(/\bno-js\b/,"")+" js",b.addEventListener("DOMContentLoaded",d,!1),a.addEventListener("load",d,!1),a.addEventListener("resize",e,!1))}(window,document);
  821. <?php
  822. }
  823. ?>
  824. </script>
  825. <?php
  826. }
  827. /**
  828. * Prepare the oembed HTML to be displayed in an RSS feed.
  829. *
  830. * @since 4.4.0
  831. * @access private
  832. *
  833. * @param string $content The content to filter.
  834. * @return string The filtered content.
  835. */
  836. function _oembed_filter_feed_content( $content ) {
  837. return str_replace( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);"', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $content );
  838. }
  839. /**
  840. * Prints the necessary markup for the embed comments button.
  841. *
  842. * @since 4.4.0
  843. */
  844. function print_embed_comments_button() {
  845. if ( is_404() || ! ( get_comments_number() || comments_open() ) ) {
  846. return;
  847. }
  848. ?>
  849. <div class="wp-embed-comments">
  850. <a href="<?php comments_link(); ?>" target="_top">
  851. <span class="dashicons dashicons-admin-comments"></span>
  852. <?php
  853. printf(
  854. _n(
  855. '%s <span class="screen-reader-text">Comment</span>',
  856. '%s <span class="screen-reader-text">Comments</span>',
  857. get_comments_number()
  858. ),
  859. number_format_i18n( get_comments_number() )
  860. );
  861. ?>
  862. </a>
  863. </div>
  864. <?php
  865. }
  866. /**
  867. * Prints the necessary markup for the embed sharing button.
  868. *
  869. * @since 4.4.0
  870. */
  871. function print_embed_sharing_button() {
  872. if ( is_404() ) {
  873. return;
  874. }
  875. ?>
  876. <div class="wp-embed-share">
  877. <button type="button" class="wp-embed-share-dialog-open" aria-label="<?php esc_attr_e( 'Open sharing dialog' ); ?>">
  878. <span class="dashicons dashicons-share"></span>
  879. </button>
  880. </div>
  881. <?php
  882. }
  883. /**
  884. * Prints the necessary markup for the embed sharing dialog.
  885. *
  886. * @since 4.4.0
  887. */
  888. function print_embed_sharing_dialog() {
  889. if ( is_404() ) {
  890. return;
  891. }
  892. ?>
  893. <div class="wp-embed-share-dialog hidden" role="dialog" aria-label="<?php esc_attr_e( 'Sharing options' ); ?>">
  894. <div class="wp-embed-share-dialog-content">
  895. <div class="wp-embed-share-dialog-text">
  896. <ul class="wp-embed-share-tabs" role="tablist">
  897. <li class="wp-embed-share-tab-button wp-embed-share-tab-button-wordpress" role="presentation">
  898. <button type="button" role="tab" aria-controls="wp-embed-share-tab-wordpress" aria-selected="true" tabindex="0"><?php esc_html_e( 'WordPress Embed' ); ?></button>
  899. </li>
  900. <li class="wp-embed-share-tab-button wp-embed-share-tab-button-html" role="presentation">
  901. <button type="button" role="tab" aria-controls="wp-embed-share-tab-html" aria-selected="false" tabindex="-1"><?php esc_html_e( 'HTML Embed' ); ?></button>
  902. </li>
  903. </ul>
  904. <div id="wp-embed-share-tab-wordpress" class="wp-embed-share-tab" role="tabpanel" aria-hidden="false">
  905. <input type="text" value="<?php the_permalink(); ?>" class="wp-embed-share-input" aria-describedby="wp-embed-share-description-wordpress" tabindex="0" readonly/>
  906. <p class="wp-embed-share-description" id="wp-embed-share-description-wordpress">
  907. <?php _e( 'Copy and paste this URL into your WordPress site to embed' ); ?>
  908. </p>
  909. </div>
  910. <div id="wp-embed-share-tab-html" class="wp-embed-share-tab" role="tabpanel" aria-hidden="true">
  911. <textarea class="wp-embed-share-input" aria-describedby="wp-embed-share-description-html" tabindex="0" readonly><?php echo esc_textarea( get_post_embed_html( 600, 400 ) ); ?></textarea>
  912. <p class="wp-embed-share-description" id="wp-embed-share-description-html">
  913. <?php _e( 'Copy and paste this code into your site to embed' ); ?>
  914. </p>
  915. </div>
  916. </div>
  917. <button type="button" class="wp-embed-share-dialog-close" aria-label="<?php esc_attr_e( 'Close sharing dialog' ); ?>">
  918. <span class="dashicons dashicons-no"></span>
  919. </button>
  920. </div>
  921. </div>
  922. <?php
  923. }
  924. /**
  925. * Prints the necessary markup for the site title in an embed template.
  926. *
  927. * @since 4.5.0
  928. */
  929. function the_embed_site_title() {
  930. $site_title = sprintf(
  931. '<a href="%s" target="_top"><img src="%s" srcset="%s 2x" width="32" height="32" alt="" class="wp-embed-site-icon"/><span>%s</span></a>',
  932. esc_url( home_url() ),
  933. esc_url( get_site_icon_url( 32, admin_url( 'images/w-logo-blue.png' ) ) ),
  934. esc_url( get_site_icon_url( 64, admin_url( 'images/w-logo-blue.png' ) ) ),
  935. esc_html( get_bloginfo( 'name' ) )
  936. );
  937. $site_title = '<div class="wp-embed-site-title">' . $site_title . '</div>';
  938. /**
  939. * Filters the site title HTML in the embed footer.
  940. *
  941. * @since 4.4.0
  942. *
  943. * @param string $site_title The site title HTML.
  944. */
  945. echo apply_filters( 'embed_site_title_html', $site_title );
  946. }
  947. /**
  948. * Filters the oEmbed result before any HTTP requests are made.
  949. *
  950. * If the URL belongs to the current site, the result is fetched directly instead of
  951. * going through the oEmbed discovery process.
  952. *
  953. * @since 4.5.3
  954. *
  955. * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.
  956. * @param string $url The URL that should be inspected for discovery `<link>` tags.
  957. * @param array $args oEmbed remote get arguments.
  958. * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
  959. * Null if the URL does not belong to the current site.
  960. */
  961. function wp_filter_pre_oembed_result( $result, $url, $args ) {
  962. $switched_blog = false;
  963. if ( is_multisite() ) {
  964. $url_parts = wp_parse_args( wp_parse_url( $url ), array(
  965. 'host' => '',
  966. 'path' => '/',
  967. ) );
  968. $qv = array( 'domain' => $url_parts['host'], 'path' => '/' );
  969. // In case of subdirectory configs, set the path.
  970. if ( ! is_subdomain_install() ) {
  971. $path = explode( '/', ltrim( $url_parts['path'], '/' ) );
  972. $path = reset( $path );
  973. if ( $path ) {
  974. $qv['path'] = get_network()->path . $path . '/';
  975. }
  976. }
  977. $sites = get_sites( $qv );
  978. $site = reset( $sites );
  979. if ( $site && (int) $site->blog_id !== get_current_blog_id() ) {
  980. switch_to_blog( $site->blog_id );
  981. $switched_blog = true;
  982. }
  983. }
  984. $post_id = url_to_postid( $url );
  985. /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
  986. $post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );
  987. if ( ! $post_id ) {
  988. if ( $switched_blog ) {
  989. restore_current_blog();
  990. }
  991. return $result;
  992. }
  993. $width = isset( $args['width'] ) ? $args['width'] : 0;
  994. $data = get_oembed_response_data( $post_id, $width );
  995. $data = _wp_oembed_get_object()->data2html( (object) $data, $url );
  996. if ( $switched_blog ) {
  997. restore_current_blog();
  998. }
  999. if ( ! $data ) {
  1000. return $result;
  1001. }
  1002. return $data;
  1003. }