<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[11411] sites/trunk/api.wordpress.org/public_html/dotorg/trac/oembed: Trac: Create an oEmbed API which can be used to embed Meta & Core trac on a WordPress site.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/11411">11411</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/11411","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-01-06 02:22:54 +0000 (Thu, 06 Jan 2022)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Trac: Create an oEmbed API which can be used to embed Meta & Core trac on a WordPress site.
oEmbed discovery doesn't work, but support can be enabled on any site that requires it.</pre>
<h3>Added Paths</h3>
<ul>
<li>sites/trunk/api.wordpress.org/public_html/dotorg/trac/oembed/</li>
<li><a href="#sitestrunkapiwordpressorgpublic_htmldotorgtracoembedindexphp">sites/trunk/api.wordpress.org/public_html/dotorg/trac/oembed/index.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkapiwordpressorgpublic_htmldotorgtracoembedindexphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/api.wordpress.org/public_html/dotorg/trac/oembed/index.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/api.wordpress.org/public_html/dotorg/trac/oembed/index.php (rev 0)
+++ sites/trunk/api.wordpress.org/public_html/dotorg/trac/oembed/index.php 2022-01-06 02:22:54 UTC (rev 11411)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,237 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Super 'basic' Trac oEmbed handler.
+ *
+ * This allows for WordPress Core & Meta trac to be embedded onto WordPress sites.
+ * Supported endpoints:
+ * - /ticket/
+ * - /changeset/
+ * - /ticketgraph
+ * - /milestone/
+ *
+ * Can be enabled on a site by adding:
+ * - wp_oembed_add_provider( '#https://(meta|core)\.trac\.wordpress.org/.*#', 'https://api.wordpress.org/dotorg/trac/oembed/?api_key=...' );
+ *
+ * oEmbed Discovery is not enabled, as although adding the tag to trac is possible, it requires inline Javascript.
+ *
+ * Please do not abuse this API, otherwise an API KEY will become required.
+ */
+include dirname( dirname( dirname( __DIR__ ) ) ) . '/wp-init.php';
+
+// Mark this as an oEmbed response for caching.
+header( 'X-WP-Embed: true' );
+
+$url = $_REQUEST['url'] ?? '';
+
+header( 'Allow: GET' );
+header( 'Expires: ' . gmdate( 'D, d M Y H:i:s \G\M\T', time() + HOUR_IN_SECONDS ), true );
+
+if (
+ // meta|core are the only tracs embedable.
+ // milestone|ticketgraph|ticket|changeset are the only endpoints allowable.
+ ! preg_match( '!^(?P<baseurl>https://(?P<trac>meta|core).trac.wordpress.org/)(?P<type>(milestone|ticketgraph|ticket|changeset))([/?]|$)!i', $url, $m ) ||
+ 'GET' !== $_SERVER['REQUEST_METHOD']
+) {
+ header( 'HTTP/1.1 404 Not Found', true, 404 );
+ die();
+}
+
+$type = $m['type'];
+
+// if not iframe embed, respond with oembed payload.
+if ( ! isset( $_GET['embed'] ) ) {
+ header( 'Content-Type: application/json; charset=UTF-8' );
+
+ // Unique ID for this instance of the iframe
+ $id = sha1( $url . microtime() );
+
+ $embed_url = 'https://' . ( $_SERVER['HTTP_X_ORIGINAL_HOST'] ?? $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] . '#el=' . $id;
+
+ $html = sprintf(
+ '<iframe sandbox="allow-scripts allow-top-navigation-by-user-activation" security="restricted" src="%s" id="%s" width="600" height="300" title="WordPress.org Trac" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content wporg-trac"></iframe>',
+ esc_url( add_query_arg( 'embed', 'true', $embed_url ) ),
+ esc_attr( 'el-' . $id )
+ );
+
+ $html .= sprintf(
+ // Note: Cannot have &
+ '<script type="text/javascript">
+ (function(id) {
+ var el = document.getElementById( "el-" + id );
+ window.addEventListener( "message", function(e) {
+ if ( ! e.data ) return;
+ if ( ! e.data.el || e.data.el != id ) return;
+ if ( ! e.data.height ) return;
+
+ el.height = e.data.height;
+ }, false );
+ el.contentWindow.postMessage( { action: "send" }, "*" );
+ })("%s");
+ </script>',
+ esc_attr( $id ),
+ );
+
+ $embed = [
+ 'version' => '1.0',
+ 'provider_name' => 'WordPress.org Trac',
+ 'provider_url' => $m['baseurl'],
+ 'title' => 'WordPress.org Trac',
+ 'type' => 'rich',
+ 'width' => 600,
+ 'height' => 300,
+ 'html' => $html,
+ ];
+
+ // Default milestone embeds to 120px.
+ if ( 'milestone' === $type ) {
+ $embed['height'] = 120;
+ }
+
+ echo wp_json_encode( $embed );
+ die();
+}
+
+$cache_key = sha1( $url );
+if ( $data = wp_cache_get( $cache_key, 'trac-oembed' ) ) {
+ die( $data );
+}
+
+$html = wp_remote_retrieve_body(
+ wp_remote_get(
+ $url,
+ [
+ 'user_agent' => 'WordPress.org Trac oEmbed; https://api.wordpress.org/dotorg/trac/oembed',
+ 'timeout' => 15,
+ 'limit_response_size' => 500 * KB_IN_BYTES,
+ ]
+ )
+);
+
+if ( ! $html ) {
+ $output = '<h1>Temporarily Unavailable</h1>';
+ wp_cache_set( $cache_key, $output, 'trac-oembed', MINUTE_IN_SECONDS );
+ die( $output );
+}
+
+$doc = new DOMDocument();
+$doc->loadHTML( $html );
+
+// IDs of elements to remove
+$remove_elements = [
+ 'wporg-header', 'wporg-footer',
+ 'headline', 'banner', 'mainnav',
+ 'ctxtnav', 'help', 'altlinks',
+ 'prefs',
+];
+
+// Tags to just strip out.
+$remove_tags = [
+ 'form'
+];
+
+// Additional elements per type of page.
+switch ( $type ) {
+ case 'milestone':
+ $remove_elements[] = 'stats';
+ break;
+ case 'ticket':
+ $remove_elements[] = 'changelog';
+ $remove_elements[] = 'attachments';
+
+ // Remove the 'Change History' element, which doesn't have an ID
+ foreach ( $doc->getElementById( 'content' )->childNodes as $node ) {
+ if ( false !== stripos( $node->textContent, 'Change History' ) ) {
+ $node->parentNode->removeChild( $node );
+ break;
+ }
+ }
+ break;
+}
+
+// Remove any elements that are not needed.
+foreach ( $remove_elements as $id ) {
+ $el = $doc->getElementById( $id );
+ if ( $el ) {
+ $el->parentNode->removeChild( $el );
+ }
+}
+
+// Remove any tags
+foreach ( $remove_tags as $tag ) {
+ foreach ( $doc->getElementsByTagName( $tag ) as $el ) {
+ $el->parentNode->removeChild( $el );
+ }
+}
+
+// Ensure all URLs are absolute to the trac host.
+$rewrite_attrs = [
+ 'script' => 'src',
+ 'link' => 'href',
+ 'a' => 'href',
+ 'img' => 'src',
+];
+foreach ( $rewrite_attrs as $tag => $attr ) {
+ foreach ( $doc->getElementsByTagName( $tag ) as $el ) {
+ $v = (string) $el->getAttribute( $attr );
+ if (
+ ! $v ||
+ false !== strpos( $v, '://' ) ||
+ 0 === strpos( $v, '//' )
+ ) {
+ continue;
+ }
+
+ $new = WP_Http::make_absolute_url( $v, $url );
+ if ( $new !== $v ) {
+ $el->setAttribute( $attr, $new );
+ }
+ }
+}
+
+// Ensure all links target the parent window.
+foreach ( $doc->getElementsByTagName( 'a' ) as $el ) {
+ $el->setAttribute( 'target', '_top' );
+}
+
+// Remove wp-trac.js, we don't need it here - It alters the page too much and adds elements on load.
+// Recursive as it can't iterate over the document while altering it properly.
+do {
+ $removed = 0;
+ foreach ( $doc->getElementsByTagName( 'script' ) as $script ) {
+ $src = (string) $script->getAttribute( 'src' );
+
+ if (
+ false !== stripos( $src, 'wp-trac.js' ) ||
+ false !== stripos( $script->textContent, 'wpTrac' )
+ ) {
+ $script->parentNode->removeChild( $script );
+ $removed++;
+ }
+ }
+} while ( $removed );
+
+// Add a script to the header.
+$js = <<<JS
+(function() {
+ var id = ( document.location.hash.match(/el=([0-9a-f]+)(&|$)/) || [ '', '' ] )[1];
+
+ function send() {
+ window.parent.postMessage( {
+ height: document.getElementById('main').offsetHeight || 0,
+ el: id
+ }, '*' );
+ }
+
+ window.addEventListener( 'message', send );
+ window.addEventListener( 'DOMNodeInserted', send );
+ window.addEventListener( 'load', send );
+ window.addEventListener( 'DOMContentLoaded', send );
+})();
+JS;
+$doc->getElementsByTagName( 'head' )[0]->appendChild( $doc->createElement( 'script', $js ) );
+
+$data = $doc->saveHTML();
+
+wp_cache_set( $cache_key, $data, 'trac-oembed', HOUR_IN_SECONDS );
+
+echo $data;
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/api.wordpress.org/public_html/dotorg/trac/oembed/index.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>