embed.php 46 KB

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