<!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>[44154] trunk: Embeds: Filter HTML response in oEmbed proxy controller.</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="https://core.trac.wordpress.org/changeset/44154">44154</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/44154","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>pento</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-12-14 03:19:48 +0000 (Fri, 14 Dec 2018)</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'>Embeds: Filter HTML response in oEmbed proxy controller.

Adapts the response from `WP_oEmbed_Controller::get_proxy_item()` so that the response is correctly filtered and embeds work properly in JavaSccript editors. Introduces new `get_oembed_response_data_for_url()` function for preparing internal oEmbed responses.

Merges <a href="https://core.trac.wordpress.org/changeset/43810">[43810]</a> from the 5.0 branch to trunk.

Props danielbachhuber, imath, swissspidy.
Fixes <a href="https://core.trac.wordpress.org/ticket/45142">#45142</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclassoembedphp">trunk/src/wp-includes/class-oembed.php</a></li>
<li><a href="#trunksrcwpincludesclasswpoembedcontrollerphp">trunk/src/wp-includes/class-wp-oembed-controller.php</a></li>
<li><a href="#trunksrcwpincludesembedphp">trunk/src/wp-includes/embed.php</a></li>
<li><a href="#trunktestsphpunittestsoembedcontrollerphp">trunk/tests/phpunit/tests/oembed/controller.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>

<h3>Property Changed</h3>
<ul>
<li><a href="#trunk">trunk/</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: trunk
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- trunk        2018-12-14 03:16:56 UTC (rev 44153)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ trunk 2018-12-14 03:19:48 UTC (rev 44154)
</ins><a id="trunk"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: trunk</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/3.3:20543
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.4:21757
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/4.9:43557,43622
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43751-43754,43758,43761-43765,43767-43770,43772,43774-43781,43783,43790-43800,43802,43804-43806,43808-43809,43811,43821,43842,43860,43869-43876,43892,43904,43909,43926-43929,43954,43956,43961-43963
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43751-43754,43758,43761-43765,43767-43770,43772,43774-43781,43783,43790-43800,43802,43804-43806,43808-43811,43821,43842,43860,43869-43876,43892,43904,43909,43926-43929,43954,43956,43961-43963
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesclassoembedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-oembed.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-oembed.php    2018-12-14 03:16:56 UTC (rev 44153)
+++ trunk/src/wp-includes/class-oembed.php      2018-12-14 03:19:48 UTC (rev 44154)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -404,9 +404,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @since 2.9.0
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @param string $data The returned oEmbed HTML.
-                * @param string $url  URL of the content to be embedded.
-                * @param array  $args Optional arguments, usually passed from a shortcode.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @param string|false $data The returned oEmbed HTML (false if unsafe).
+                * @param string       $url  URL of the content to be embedded.
+                * @param array        $args Optional arguments, usually passed from a shortcode.
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><span class="cx" style="display: block; padding: 0 10px">                return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpoembedcontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-oembed-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-oembed-controller.php      2018-12-14 03:16:56 UTC (rev 44153)
+++ trunk/src/wp-includes/class-wp-oembed-controller.php        2018-12-14 03:19:48 UTC (rev 44154)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -181,6 +181,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $args['height'] = $args['maxheight'];
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Short-circuit process for URLs belonging to the current site.
+               $data = get_oembed_response_data_for_url( $url, $args );
+
+               if ( $data ) {
+                       return $data;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $data = _wp_oembed_get_object()->get_data( $url, $args );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( false === $data ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -187,6 +194,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /** This filter is documented in wp-includes/class-oembed.php */
+               $data->html = apply_filters( 'oembed_result', _wp_oembed_get_object()->data2html( (object) $data, $url ), $url, $args );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Filters the oEmbed TTL value (time to live).
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span></span></pre></div>
<a id="trunksrcwpincludesembedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/embed.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/embed.php   2018-12-14 03:16:56 UTC (rev 44153)
+++ trunk/src/wp-includes/embed.php     2018-12-14 03:19:48 UTC (rev 44154)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -61,13 +61,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return array Default embed parameters.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_embed_defaults( $url = '' ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! empty( $GLOBALS['content_width'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $GLOBALS['content_width'] ) )
</ins><span class="cx" style="display: block; padding: 0 10px">                 $width = (int) $GLOBALS['content_width'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( empty( $width ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $width ) )
</ins><span class="cx" style="display: block; padding: 0 10px">                 $width = 500;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $height = min( ceil( $width * 1.5 ), 1000 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -76,7 +74,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.9.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param int[]  $size An array of embed width and height values
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array  $size An array of embed width and height values
</ins><span class="cx" style="display: block; padding: 0 10px">          *                     in pixels (in that order).
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $url  The URL that should be embedded.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -133,8 +131,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_oembed_add_provider( $format, $provider, $regex = false ) {
</span><span class="cx" style="display: block; padding: 0 10px">        if ( did_action( 'plugins_loaded' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $oembed                       = _wp_oembed_get_object();
-               $oembed->providers[ $format ] = array( $provider, $regex );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $oembed = _wp_oembed_get_object();
+               $oembed->providers[$format] = array( $provider, $regex );
</ins><span class="cx" style="display: block; padding: 0 10px">         } else {
</span><span class="cx" style="display: block; padding: 0 10px">                WP_oEmbed::_add_provider_early( $format, $provider, $regex );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -228,7 +226,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) {
</span><span class="cx" style="display: block; padding: 0 10px">        global $wp_embed;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $embed = $wp_embed->autoembed( sprintf( 'https://youtube.com/watch?v=%s', urlencode( $matches[2] ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $embed = $wp_embed->autoembed( sprintf( "https://youtube.com/watch?v=%s", urlencode( $matches[2] ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filters the YoutTube embed output.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -397,13 +395,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $url = rest_url( 'oembed/1.0/embed' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( '' !== $permalink ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $url = add_query_arg(
-                       array(
-                               'url'    => urlencode( $permalink ),
-                               'format' => ( 'json' !== $format ) ? $format : false,
-                       ),
-                       $url
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $url = add_query_arg( array(
+                       'url'    => urlencode( $permalink ),
+                       'format' => ( 'json' !== $format ) ? $format : false,
+               ), $url );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -454,7 +449,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
</span><span class="cx" style="display: block; padding: 0 10px">                 * and edit wp-embed.js directly.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $output .= <<<JS
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $output .=<<<JS
</ins><span class="cx" style="display: block; padding: 0 10px">                 include "js/wp-embed.min.js"
</span><span class="cx" style="display: block; padding: 0 10px"> JS;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -522,13 +517,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int $max Maximum width. Default 600.
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $min_max_width = apply_filters(
-               'oembed_min_max_width',
-               array(
-                       'min' => 200,
-                       'max' => 600,
-               )
-       );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $min_max_width = apply_filters( 'oembed_min_max_width', array(
+               'min' => 200,
+               'max' => 600
+       ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $width  = min( max( $min_max_width['min'], $width ), $min_max_width['max'] );
</span><span class="cx" style="display: block; padding: 0 10px">        $height = max( ceil( $width / 16 * 9 ), 200 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -563,7 +555,72 @@
</span><span class="cx" style="display: block; padding: 0 10px">        return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the oEmbed response data for a given URL.
+ *
+ * @since 5.0.0
+ *
+ * @param string $url  The URL that should be inspected for discovery `<link>` tags.
+ * @param array  $args oEmbed remote get arguments.
+ * @return object|false oEmbed response data if the URL does belong to the current site. False otherwise.
+ */
+function get_oembed_response_data_for_url( $url, $args ) {
+       $switched_blog = false;
+
+       if ( is_multisite() ) {
+               $url_parts = wp_parse_args( wp_parse_url( $url ), array(
+                       'host'   => '',
+                       'path'   => '/',
+               ) );
+
+               $qv = array( 'domain' => $url_parts['host'], 'path' => '/' );
+
+               // In case of subdirectory configs, set the path.
+               if ( ! is_subdomain_install() ) {
+                       $path = explode( '/', ltrim( $url_parts['path'], '/' ) );
+                       $path = reset( $path );
+
+                       if ( $path ) {
+                               $qv['path'] = get_network()->path . $path . '/';
+                       }
+               }
+
+               $sites = get_sites( $qv );
+               $site  = reset( $sites );
+
+               if ( $site && (int) $site->blog_id !== get_current_blog_id() ) {
+                       switch_to_blog( $site->blog_id );
+                       $switched_blog = true;
+               }
+       }
+
+       $post_id = url_to_postid( $url );
+
+       /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
+       $post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );
+
+       if ( ! $post_id ) {
+               if ( $switched_blog ) {
+                       restore_current_blog();
+               }
+
+               return false;
+       }
+
+       $width = isset( $args['width'] ) ? $args['width'] : 0;
+
+       $data = get_oembed_response_data( $post_id, $width );
+
+       if ( $switched_blog ) {
+               restore_current_blog();
+       }
+
+       return $data ? (object) $data : false;
+}
+
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Filters the oEmbed response data to return an iframe embed code.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -590,7 +647,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 'attachment' === get_post_type( $post ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( wp_attachment_is_image( $post ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $thumbnail_id = $post->ID;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                } elseif ( wp_attachment_is( 'video', $post ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         } else if ( wp_attachment_is( 'video', $post ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $thumbnail_id = get_post_thumbnail_id( $post );
</span><span class="cx" style="display: block; padding: 0 10px">                        $data['type'] = 'video';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -598,9 +655,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $thumbnail_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $data['thumbnail_url']                                      = $thumbnail_url;
-               $data['thumbnail_width']                                    = $thumbnail_width;
-               $data['thumbnail_height']                                   = $thumbnail_height;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $data['thumbnail_url']    = $thumbnail_url;
+               $data['thumbnail_width']  = $thumbnail_width;
+               $data['thumbnail_height'] = $thumbnail_height;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return $data;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -737,7 +794,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $allowed_html = array(
</span><span class="cx" style="display: block; padding: 0 10px">                'a'          => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'href' => true,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'href'         => true,
</ins><span class="cx" style="display: block; padding: 0 10px">                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                'blockquote' => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                'iframe'     => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -767,7 +824,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $secret = wp_generate_password( 10, false );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $url = esc_url( "{$results[2]}#?secret=$secret" );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $q   = $results[1];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $q = $results[1];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $html = str_replace( $results[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html );
</span><span class="cx" style="display: block; padding: 0 10px">                $html = str_replace( '<blockquote', "<blockquote data-secret=\"$secret\"", $html );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -774,7 +831,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $allowed_html['blockquote']['data-secret'] = true;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $allowed_html['iframe']['data-secret']     = true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $allowed_html['iframe']['data-secret'] = true;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $html = wp_kses( $html, $allowed_html );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -805,8 +862,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $more_string;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $link = sprintf(
-               '<a href="%1$s" class="wp-embed-more" target="_top">%2$s</a>',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $link = sprintf( '<a href="%1$s" class="wp-embed-more" target="_top">%2$s</a>',
</ins><span class="cx" style="display: block; padding: 0 10px">                 esc_url( get_permalink() ),
</span><span class="cx" style="display: block; padding: 0 10px">                /* translators: %s: Name of current post */
</span><span class="cx" style="display: block; padding: 0 10px">                sprintf( __( 'Continue reading %s' ), '<span class="screen-reader-text">' . get_the_title() . '</span>' )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -882,23 +938,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">        ?>
</span><span class="cx" style="display: block; padding: 0 10px">        <style type="text/css">
</span><span class="cx" style="display: block; padding: 0 10px">        <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( SCRIPT_DEBUG ) {
-               readfile( ABSPATH . WPINC . '/css/wp-embed-template.css' );
-       } else {
-               /*
-                * If you're looking at a src version of this file, you'll see an "include"
-                * statement below. This is used by the `grunt build` process to directly
-                * include a minified version of wp-oembed-embed.css, instead of using the
-                * readfile() method from above.
-                *
-                * If you're looking at a build version of this file, you'll see a string of
-                * minified CSS. If you need to debug it, please turn on SCRIPT_DEBUG
-                * and edit wp-embed-template.css directly.
-                */
-               ?>
-               include "css/wp-embed-template.min.css"
-               <?php
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( SCRIPT_DEBUG ) {
+                       readfile( ABSPATH . WPINC . "/css/wp-embed-template.css" );
+               } else {
+                       /*
+                        * If you're looking at a src version of this file, you'll see an "include"
+                        * statement below. This is used by the `grunt build` process to directly
+                        * include a minified version of wp-oembed-embed.css, instead of using the
+                        * readfile() method from above.
+                        *
+                        * If you're looking at a build version of this file, you'll see a string of
+                        * minified CSS. If you need to debug it, please turn on SCRIPT_DEBUG
+                        * and edit wp-embed-template.css directly.
+                        */
+                       ?>
+                       include "css/wp-embed-template.min.css"
+                       <?php
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         ?>
</span><span class="cx" style="display: block; padding: 0 10px">        </style>
</span><span class="cx" style="display: block; padding: 0 10px">        <?php
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -913,23 +969,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">        ?>
</span><span class="cx" style="display: block; padding: 0 10px">        <script type="text/javascript">
</span><span class="cx" style="display: block; padding: 0 10px">        <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( SCRIPT_DEBUG ) {
-               readfile( ABSPATH . WPINC . '/js/wp-embed-template.js' );
-       } else {
-               /*
-                * If you're looking at a src version of this file, you'll see an "include"
-                * statement below. This is used by the `grunt build` process to directly
-                * include a minified version of wp-embed-template.js, instead of using the
-                * readfile() method from above.
-                *
-                * If you're looking at a build version of this file, you'll see a string of
-                * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
-                * and edit wp-embed-template.js directly.
-                */
-               ?>
-               include "js/wp-embed-template.min.js"
-               <?php
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( SCRIPT_DEBUG ) {
+                       readfile( ABSPATH . WPINC . "/js/wp-embed-template.js" );
+               } else {
+                       /*
+                        * If you're looking at a src version of this file, you'll see an "include"
+                        * statement below. This is used by the `grunt build` process to directly
+                        * include a minified version of wp-embed-template.js, instead of using the
+                        * readfile() method from above.
+                        *
+                        * If you're looking at a build version of this file, you'll see a string of
+                        * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
+                        * and edit wp-embed-template.js directly.
+                        */
+                       ?>
+                       include "js/wp-embed-template.min.js"
+                       <?php
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         ?>
</span><span class="cx" style="display: block; padding: 0 10px">        </script>
</span><span class="cx" style="display: block; padding: 0 10px">        <?php
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1080,66 +1136,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                     Null if the URL does not belong to the current site.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_filter_pre_oembed_result( $result, $url, $args ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $switched_blog = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $data = get_oembed_response_data_for_url( $url, $args );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( is_multisite() ) {
-               $url_parts = wp_parse_args(
-                       wp_parse_url( $url ),
-                       array(
-                               'host' => '',
-                               'path' => '/',
-                       )
-               );
-
-               $qv = array(
-                       'domain' => $url_parts['host'],
-                       'path'   => '/',
-               );
-
-               // In case of subdirectory configs, set the path.
-               if ( ! is_subdomain_install() ) {
-                       $path = explode( '/', ltrim( $url_parts['path'], '/' ) );
-                       $path = reset( $path );
-
-                       if ( $path ) {
-                               $qv['path'] = get_network()->path . $path . '/';
-                       }
-               }
-
-               $sites = get_sites( $qv );
-               $site  = reset( $sites );
-
-               if ( $site && (int) $site->blog_id !== get_current_blog_id() ) {
-                       switch_to_blog( $site->blog_id );
-                       $switched_blog = true;
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $data ) {
+               return _wp_oembed_get_object()->data2html( $data, $url );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $post_id = url_to_postid( $url );
-
-       /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
-       $post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );
-
-       if ( ! $post_id ) {
-               if ( $switched_blog ) {
-                       restore_current_blog();
-               }
-
-               return $result;
-       }
-
-       $width = isset( $args['width'] ) ? $args['width'] : 0;
-
-       $data = get_oembed_response_data( $post_id, $width );
-       $data = _wp_oembed_get_object()->data2html( (object) $data, $url );
-
-       if ( $switched_blog ) {
-               restore_current_blog();
-       }
-
-       if ( ! $data ) {
-               return $result;
-       }
-
-       return $data;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $result;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsoembedcontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/oembed/controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/oembed/controller.php   2018-12-14 03:16:56 UTC (rev 44153)
+++ trunk/tests/phpunit/tests/oembed/controller.php     2018-12-14 03:19:48 UTC (rev 44154)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,6 +14,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected static $subscriber;
</span><span class="cx" style="display: block; padding: 0 10px">        const YOUTUBE_VIDEO_ID   = 'OQSNhk5ICTI';
</span><span class="cx" style="display: block; padding: 0 10px">        const INVALID_OEMBED_URL = 'https://www.notreallyanoembedprovider.com/watch?v=awesome-cat-video';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        const UNTRUSTED_PROVIDER_URL = 'https://www.untrustedprovider.com';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public static function wpSetUpBeforeClass( $factory ) {
</span><span class="cx" style="display: block; padding: 0 10px">                self::$subscriber    = $factory->user->create(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,7 +50,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( 'rest_api_init', $wp_rest_server );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10, 3 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'oembed_result', array( $this, 'filter_oembed_result' ), 10, 3 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->request_count = 0;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               $this->oembed_result_filter_count = 0;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function tearDown() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -59,6 +63,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_rest_server = null;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                remove_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'oembed_result', array( $this, 'filter_oembed_result' ), 10 );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -69,6 +74,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $request_count = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Count of the number of times the oembed_result filter was called.
+        *
+        * @var int
+        */
+       public $oembed_result_filter_count = 0;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Intercept oEmbed requests and mock responses.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param mixed  $preempt Whether to preempt an HTTP request's return value. Default false.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -80,7 +92,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                unset( $preempt, $r );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $parsed_url = wp_parse_url( $url );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                parse_str( $parsed_url['query'], $query_params );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $query      = isset( $parsed_url['query'] ) ? $parsed_url['query'] : '';
+               parse_str( $query, $query_params );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->request_count += 1;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Mock request to YouTube Embed.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -99,7 +112,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                'width'            => $query_params['maxwidth'],
</span><span class="cx" style="display: block; padding: 0 10px">                                                'thumbnail_height' => $query_params['maxheight'],
</span><span class="cx" style="display: block; padding: 0 10px">                                                'height'           => $query_params['maxheight'],
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                'html'             => '<iframe width="' . $query_params['maxwidth'] . '" height="' . $query_params['maxheight'] . '" src="https://www.youtube.com/embed/' . self::YOUTUBE_VIDEO_ID . '?feature=oembed" frameborder="0" allowfullscreen></iframe>',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         'html'             => '<b>Unfiltered</b><iframe width="' . $query_params['maxwidth'] . '" height="' . $query_params['maxheight'] . '" src="https://www.youtube.com/embed/' . self::YOUTUBE_VIDEO_ID . '?feature=oembed" frameborder="0" allowfullscreen></iframe>',
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 'author_name'      => 'Yosemitebear62',
</span><span class="cx" style="display: block; padding: 0 10px">                                                'thumbnail_url'    => 'https://i.ytimg.com/vi/' . self::YOUTUBE_VIDEO_ID . '/hqdefault.jpg',
</span><span class="cx" style="display: block; padding: 0 10px">                                                'title'            => 'Yosemitebear Mountain Double Rainbow 1-8-10',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -106,15 +119,61 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        )
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                } else {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         }
+
+               if ( $url === self::UNTRUSTED_PROVIDER_URL ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'response' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'code' => 404,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'code' => 200,
</ins><span class="cx" style="display: block; padding: 0 10px">                                 ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                'body'     => '<html><head><link rel="alternate" type="application/json+oembed" href="' . self::UNTRUSTED_PROVIDER_URL . '" /></head><body></body></html>',
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               if ( ! empty( $query_params['url'] ) && false !== strpos( $query_params['url'], self::UNTRUSTED_PROVIDER_URL ) ) {
+                       return array(
+                               'response' => array(
+                                       'code' => 200,
+                               ),
+                               'body'     => wp_json_encode(
+                                       array(
+                                               'version'          => '1.0',
+                                               'type'             => 'rich',
+                                               'provider_name'    => 'Untrusted',
+                                               'provider_url'     => self::UNTRUSTED_PROVIDER_URL,
+                                               'html'             => '<b>Filtered</b><a href="">Unfiltered</a>',
+                                               'author_name'      => 'Untrusted Embed Author',
+                                               'title'            => 'Untrusted Embed',
+                                       )
+                               ),
+                       );
+               }
+
+               return array(
+                       'response' => array(
+                               'code' => 404,
+                       ),
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Filters 'oembed_result' to ensure correct type.
+        *
+        * @param string|false $data The returned oEmbed HTML.
+        * @param string       $url  URL of the content to be embedded.
+        * @param array        $args Optional arguments, usually passed from a shortcode.
+        * @return string
+        */
+       public function filter_oembed_result( $data, $url, $args ) {
+               if ( ! is_string( $data ) && false !== $data ) {
+                       $this->fail( 'Unexpected type for $data.' );
+               }
+               $this->assertInternalType( 'string', $url );
+               $this->assertInternalType( 'array', $args );
+               $this->oembed_result_filter_count++;
+               return $data;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function test_wp_oembed_ensure_format() {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'json', wp_oembed_ensure_format( 'json' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'xml', wp_oembed_ensure_format( 'xml' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -543,7 +602,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNotEmpty( $data );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertTrue( is_object( $data ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertInternalType( 'object', $data );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 'YouTube', $data->provider_name );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'https://i.ytimg.com/vi/' . self::YOUTUBE_VIDEO_ID . '/hqdefault.jpg', $data->thumbnail_url );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $data->width, $request['maxwidth'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -585,4 +644,141 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $data['code'], 'rest_invalid_param' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 45142
+        */
+       function test_proxy_with_internal_url() {
+               wp_set_current_user( self::$editor );
+
+               $user = self::factory()->user->create_and_get( array(
+                       'display_name' => 'John Doe',
+               ) );
+               $post = self::factory()->post->create_and_get( array(
+                       'post_author' => $user->ID,
+                       'post_title'  => 'Hello World',
+               ) );
+
+               $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
+               $request->set_param( 'url', get_permalink( $post->ID ) );
+               $request->set_param( 'maxwidth', 400 );
+
+               $response = rest_get_server()->dispatch( $request );
+               $data     = $response->get_data();
+
+               $data = (array) $data;
+
+               $this->assertNotEmpty( $data );
+
+               $this->assertArrayHasKey( 'version', $data );
+               $this->assertArrayHasKey( 'provider_name', $data );
+               $this->assertArrayHasKey( 'provider_url', $data );
+               $this->assertArrayHasKey( 'author_name', $data );
+               $this->assertArrayHasKey( 'author_url', $data );
+               $this->assertArrayHasKey( 'title', $data );
+               $this->assertArrayHasKey( 'type', $data );
+               $this->assertArrayHasKey( 'width', $data );
+
+               $this->assertEquals( '1.0', $data['version'] );
+               $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
+               $this->assertEquals( get_home_url(), $data['provider_url'] );
+               $this->assertEquals( $user->display_name, $data['author_name'] );
+               $this->assertEquals( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
+               $this->assertEquals( $post->post_title, $data['title'] );
+               $this->assertEquals( 'rich', $data['type'] );
+               $this->assertTrue( $data['width'] <= $request['maxwidth'] );
+       }
+
+       /**
+        * @ticket 45142
+        */
+       function test_proxy_with_static_front_page_url() {
+               wp_set_current_user( self::$editor );
+
+               $post = self::factory()->post->create_and_get( array(
+                       'post_title'  => 'Front page',
+                       'post_type'   => 'page',
+                       'post_author' => 0,
+               ) );
+
+               update_option( 'show_on_front', 'page' );
+               update_option( 'page_on_front', $post->ID );
+
+               $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
+               $request->set_param( 'url', home_url() );
+               $request->set_param( 'maxwidth', 400 );
+
+               $response = rest_get_server()->dispatch( $request );
+               $data     = $response->get_data();
+
+               $this->assertInternalType( 'object', $data );
+
+               $data = (array) $data;
+
+               $this->assertNotEmpty( $data );
+
+               $this->assertArrayHasKey( 'version', $data );
+               $this->assertArrayHasKey( 'provider_name', $data );
+               $this->assertArrayHasKey( 'provider_url', $data );
+               $this->assertArrayHasKey( 'author_name', $data );
+               $this->assertArrayHasKey( 'author_url', $data );
+               $this->assertArrayHasKey( 'title', $data );
+               $this->assertArrayHasKey( 'type', $data );
+               $this->assertArrayHasKey( 'width', $data );
+
+               $this->assertEquals( '1.0', $data['version'] );
+               $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
+               $this->assertEquals( get_home_url(), $data['provider_url'] );
+               $this->assertEquals( get_bloginfo( 'name' ), $data['author_name'] );
+               $this->assertEquals( get_home_url(), $data['author_url'] );
+               $this->assertEquals( $post->post_title, $data['title'] );
+               $this->assertEquals( 'rich', $data['type'] );
+               $this->assertTrue( $data['width'] <= $request['maxwidth'] );
+
+               update_option( 'show_on_front', 'posts' );
+       }
+
+       /**
+        * @ticket 45142
+        */
+       public function test_proxy_filters_result_of_untrusted_oembed_provider() {
+               wp_set_current_user( self::$editor );
+
+               $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
+               $request->set_param( 'url', self::UNTRUSTED_PROVIDER_URL );
+               $request->set_param( 'maxwidth', 456 );
+               $request->set_param( 'maxheight', 789 );
+               $request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
+
+               $response = rest_get_server()->dispatch( $request );
+               $data     = $response->get_data();
+
+               $this->assertEquals( 1, $this->oembed_result_filter_count );
+               $this->assertInternalType( 'object', $data );
+               $this->assertEquals( 'Untrusted', $data->provider_name );
+               $this->assertEquals( self::UNTRUSTED_PROVIDER_URL, $data->provider_url );
+               $this->assertEquals( 'rich', $data->type );
+               $this->assertFalse( $data->html );
+       }
+
+       /**
+        * @ticket 45142
+        */
+       public function test_proxy_does_not_filter_result_of_trusted_oembed_provider() {
+               wp_set_current_user( self::$editor );
+
+               $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
+               $request->set_param( 'url', 'https://www.youtube.com/watch?v=' . self::YOUTUBE_VIDEO_ID );
+               $request->set_param( 'maxwidth', 456 );
+               $request->set_param( 'maxheight', 789 );
+               $request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
+
+               $response = rest_get_server()->dispatch( $request );
+               $data     = $response->get_data();
+
+               $this->assertEquals( 1, $this->oembed_result_filter_count );
+               $this->assertInternalType( 'object', $data );
+
+               $this->assertStringStartsWith( '<b>Unfiltered</b>', $data->html );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js    2018-12-14 03:16:56 UTC (rev 44153)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js      2018-12-14 03:19:48 UTC (rev 44154)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5083,16 +5083,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px">     },
</span><span class="cx" style="display: block; padding: 0 10px">     {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "author": 375,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "author": 376,
</ins><span class="cx" style="display: block; padding: 0 10px">         "date": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">         "date_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "id": 36744,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "id": 3162,
</ins><span class="cx" style="display: block; padding: 0 10px">         "modified": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">         "modified_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "parent": 36743,
-        "slug": "36743-revision-v1",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "parent": 3161,
+        "slug": "3161-revision-v1",
</ins><span class="cx" style="display: block; padding: 0 10px">         "guid": {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-            "rendered": "http://example.org/?p=36744"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+            "rendered": "http://example.org/?p=3162"
</ins><span class="cx" style="display: block; padding: 0 10px">         },
</span><span class="cx" style="display: block; padding: 0 10px">         "title": {
</span><span class="cx" style="display: block; padding: 0 10px">             "rendered": "REST API Client Fixture: Post"
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5106,7 +5106,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         "_links": {
</span><span class="cx" style="display: block; padding: 0 10px">             "parent": [
</span><span class="cx" style="display: block; padding: 0 10px">                 {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                    "href": "http://example.org/index.php?rest_route=/wp/v2/posts/36743"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                    "href": "http://example.org/index.php?rest_route=/wp/v2/posts/3161"
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">             ]
</span><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5138,16 +5138,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> mockedApiResponse.postAutosaves = [
</span><span class="cx" style="display: block; padding: 0 10px">     {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "author": 375,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "author": 376,
</ins><span class="cx" style="display: block; padding: 0 10px">         "date": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">         "date_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "id": 36745,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "id": 3163,
</ins><span class="cx" style="display: block; padding: 0 10px">         "modified": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">         "modified_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "parent": 36743,
-        "slug": "36743-autosave-v1",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "parent": 3161,
+        "slug": "3161-autosave-v1",
</ins><span class="cx" style="display: block; padding: 0 10px">         "guid": {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-            "rendered": "http://example.org/?p=36745"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+            "rendered": "http://example.org/?p=3163"
</ins><span class="cx" style="display: block; padding: 0 10px">         },
</span><span class="cx" style="display: block; padding: 0 10px">         "title": {
</span><span class="cx" style="display: block; padding: 0 10px">             "rendered": ""
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5161,7 +5161,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         "_links": {
</span><span class="cx" style="display: block; padding: 0 10px">             "parent": [
</span><span class="cx" style="display: block; padding: 0 10px">                 {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                    "href": "http://example.org/index.php?rest_route=/wp/v2/posts/36743"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                    "href": "http://example.org/index.php?rest_route=/wp/v2/posts/3161"
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">             ]
</span><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5169,16 +5169,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> mockedApiResponse.autosave = {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-    "author": 375,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+    "author": 376,
</ins><span class="cx" style="display: block; padding: 0 10px">     "date": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">     "date_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-    "id": 36745,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+    "id": 3163,
</ins><span class="cx" style="display: block; padding: 0 10px">     "modified": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">     "modified_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-    "parent": 36743,
-    "slug": "36743-autosave-v1",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+    "parent": 3161,
+    "slug": "3161-autosave-v1",
</ins><span class="cx" style="display: block; padding: 0 10px">     "guid": {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "rendered": "http://example.org/?p=36745"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "rendered": "http://example.org/?p=3163"
</ins><span class="cx" style="display: block; padding: 0 10px">     },
</span><span class="cx" style="display: block; padding: 0 10px">     "title": {
</span><span class="cx" style="display: block; padding: 0 10px">         "rendered": ""
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5343,16 +5343,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px">     },
</span><span class="cx" style="display: block; padding: 0 10px">     {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "author": 375,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "author": 376,
</ins><span class="cx" style="display: block; padding: 0 10px">         "date": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">         "date_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "id": 36747,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "id": 3165,
</ins><span class="cx" style="display: block; padding: 0 10px">         "modified": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">         "modified_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "parent": 36746,
-        "slug": "36746-revision-v1",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "parent": 3164,
+        "slug": "3164-revision-v1",
</ins><span class="cx" style="display: block; padding: 0 10px">         "guid": {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-            "rendered": "http://example.org/?p=36747"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+            "rendered": "http://example.org/?p=3165"
</ins><span class="cx" style="display: block; padding: 0 10px">         },
</span><span class="cx" style="display: block; padding: 0 10px">         "title": {
</span><span class="cx" style="display: block; padding: 0 10px">             "rendered": "REST API Client Fixture: Page"
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5366,7 +5366,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         "_links": {
</span><span class="cx" style="display: block; padding: 0 10px">             "parent": [
</span><span class="cx" style="display: block; padding: 0 10px">                 {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                    "href": "http://example.org/index.php?rest_route=/wp/v2/pages/36746"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                    "href": "http://example.org/index.php?rest_route=/wp/v2/pages/3164"
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">             ]
</span><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5398,16 +5398,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> mockedApiResponse.pageAutosaves = [
</span><span class="cx" style="display: block; padding: 0 10px">     {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "author": 375,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "author": 376,
</ins><span class="cx" style="display: block; padding: 0 10px">         "date": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">         "date_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "id": 36748,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "id": 3166,
</ins><span class="cx" style="display: block; padding: 0 10px">         "modified": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">         "modified_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "parent": 36746,
-        "slug": "36746-autosave-v1",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "parent": 3164,
+        "slug": "3164-autosave-v1",
</ins><span class="cx" style="display: block; padding: 0 10px">         "guid": {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-            "rendered": "http://example.org/?p=36748"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+            "rendered": "http://example.org/?p=3166"
</ins><span class="cx" style="display: block; padding: 0 10px">         },
</span><span class="cx" style="display: block; padding: 0 10px">         "title": {
</span><span class="cx" style="display: block; padding: 0 10px">             "rendered": ""
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5421,7 +5421,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         "_links": {
</span><span class="cx" style="display: block; padding: 0 10px">             "parent": [
</span><span class="cx" style="display: block; padding: 0 10px">                 {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                    "href": "http://example.org/index.php?rest_route=/wp/v2/pages/36746"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                    "href": "http://example.org/index.php?rest_route=/wp/v2/pages/3164"
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">             ]
</span><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5429,16 +5429,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> mockedApiResponse.pageAutosave = {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-    "author": 375,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+    "author": 376,
</ins><span class="cx" style="display: block; padding: 0 10px">     "date": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">     "date_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-    "id": 36748,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+    "id": 3166,
</ins><span class="cx" style="display: block; padding: 0 10px">     "modified": "2017-02-14T00:00:00",
</span><span class="cx" style="display: block; padding: 0 10px">     "modified_gmt": "2017-02-14T00:00:00",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-    "parent": 36746,
-    "slug": "36746-autosave-v1",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+    "parent": 3164,
+    "slug": "3164-autosave-v1",
</ins><span class="cx" style="display: block; padding: 0 10px">     "guid": {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        "rendered": "http://example.org/?p=36748"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "rendered": "http://example.org/?p=3166"
</ins><span class="cx" style="display: block; padding: 0 10px">     },
</span><span class="cx" style="display: block; padding: 0 10px">     "title": {
</span><span class="cx" style="display: block; padding: 0 10px">         "rendered": ""
</span></span></pre>
</div>
</div>

</body>
</html>