embed.php 37 KB

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