diff -r c7c34916027a -r 177826044cd9 wp/wp-includes/embed.php --- a/wp/wp-includes/embed.php Mon Oct 14 18:06:33 2019 +0200 +++ b/wp/wp-includes/embed.php Mon Oct 14 18:28:13 2019 +0200 @@ -61,11 +61,13 @@ * @return array Default embed parameters. */ function wp_embed_defaults( $url = '' ) { - if ( ! empty( $GLOBALS['content_width'] ) ) + if ( ! empty( $GLOBALS['content_width'] ) ) { $width = (int) $GLOBALS['content_width']; + } - if ( empty( $width ) ) + if ( empty( $width ) ) { $width = 500; + } $height = min( ceil( $width * 1.5 ), 1000 ); @@ -131,8 +133,8 @@ */ function wp_oembed_add_provider( $format, $provider, $regex = false ) { if ( did_action( 'plugins_loaded' ) ) { - $oembed = _wp_oembed_get_object(); - $oembed->providers[$format] = array( $provider, $regex ); + $oembed = _wp_oembed_get_object(); + $oembed->providers[ $format ] = array( $provider, $regex ); } else { WP_oEmbed::_add_provider_early( $format, $provider, $regex ); } @@ -226,7 +228,7 @@ */ function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) { global $wp_embed; - $embed = $wp_embed->autoembed( sprintf( "https://youtube.com/watch?v=%s", urlencode( $matches[2] ) ) ); + $embed = $wp_embed->autoembed( sprintf( 'https://youtube.com/watch?v=%s', urlencode( $matches[2] ) ) ); /** * Filters the YoutTube embed output. @@ -395,10 +397,13 @@ $url = rest_url( 'oembed/1.0/embed' ); if ( '' !== $permalink ) { - $url = add_query_arg( array( - 'url' => urlencode( $permalink ), - 'format' => ( 'json' !== $format ) ? $format : false, - ), $url ); + $url = add_query_arg( + array( + 'url' => urlencode( $permalink ), + 'format' => ( 'json' !== $format ) ? $format : false, + ), + $url + ); } /** @@ -449,8 +454,8 @@ * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG * and edit wp-embed.js directly. */ - $output .=<<1e3)g=1e3;else if(~~g<200)g=200;f.height=g}if("link"===d.message)if(h=b.createElement("a"),i=b.createElement("a"),h.href=f.getAttribute("src"),i.href=d.value,i.host===h.host)if(b.activeElement===f)a.top.location.href=d.value}else;}},d)a.addEventListener("message",a.wp.receiveEmbedMessage,!1),b.addEventListener("DOMContentLoaded",c,!1),a.addEventListener("load",c,!1)}(window,document); + $output .= <<1e3)g=1e3;else if(~~g<200)g=200;f.height=g}if("link"===d.message)if(h=b.createElement("a"),i=b.createElement("a"),h.href=f.getAttribute("src"),i.href=d.value,i.host===h.host)if(b.activeElement===f)a.top.location.href=d.value}else;}},d)a.addEventListener("message",a.wp.receiveEmbedMessage,!1),b.addEventListener("DOMContentLoaded",c,!1),a.addEventListener("load",c,!1)}(window,document); JS; } $output .= "\n//-->"; @@ -517,10 +522,13 @@ * @type int $max Maximum width. Default 600. * } */ - $min_max_width = apply_filters( 'oembed_min_max_width', array( - 'min' => 200, - 'max' => 600 - ) ); + $min_max_width = apply_filters( + 'oembed_min_max_width', + array( + 'min' => 200, + 'max' => 600, + ) + ); $width = min( max( $min_max_width['min'], $width ), $min_max_width['max'] ); $height = max( ceil( $width / 16 * 9 ), 200 ); @@ -531,7 +539,7 @@ 'provider_url' => get_home_url(), 'author_name' => get_bloginfo( 'name' ), 'author_url' => get_home_url(), - 'title' => $post->post_title, + 'title' => get_the_title( $post ), 'type' => 'link', ); @@ -555,6 +563,78 @@ return apply_filters( 'oembed_response_data', $data, $post, $width, $height ); } + +/** + * Retrieves the oEmbed response data for a given URL. + * + * @since 5.0.0 + * + * @param string $url The URL that should be inspected for discovery `` 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' => '/', + 'update_site_meta_cache' => false, + ); + + // 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; +} + + /** * Filters the oEmbed response data to return an iframe embed code. * @@ -582,7 +662,7 @@ if ( 'attachment' === get_post_type( $post ) ) { if ( wp_attachment_is_image( $post ) ) { $thumbnail_id = $post->ID; - } else if ( wp_attachment_is( 'video', $post ) ) { + } elseif ( wp_attachment_is( 'video', $post ) ) { $thumbnail_id = get_post_thumbnail_id( $post ); $data['type'] = 'video'; } @@ -590,9 +670,9 @@ if ( $thumbnail_id ) { list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) ); - $data['thumbnail_url'] = $thumbnail_url; - $data['thumbnail_width'] = $thumbnail_width; - $data['thumbnail_height'] = $thumbnail_height; + $data['thumbnail_url'] = $thumbnail_url; + $data['thumbnail_width'] = $thumbnail_width; + $data['thumbnail_height'] = $thumbnail_height; } return $data; @@ -701,12 +781,61 @@ } /** + * Filters the given oEmbed HTML to make sure iframes have a title attribute. + * + * @since 5.2.0 + * + * @param string $result The oEmbed HTML result. + * @param object $data A data object result from an oEmbed provider. + * @param string $url The URL of the content to be embedded. + * @return string The filtered oEmbed result. + */ +function wp_filter_oembed_iframe_title_attribute( $result, $data, $url ) { + if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ) ) ) { + return $result; + } + + $title = ! empty( $data->title ) ? $data->title : ''; + + $pattern = '`]*?title=(\\\\\'|\\\\"|[\'"])([^>]*?)\1`i'; + $has_title_attr = preg_match( $pattern, $result, $matches ); + + if ( $has_title_attr && ! empty( $matches[2] ) ) { + $title = $matches[2]; + } + + /** + * Filters the title attribute of the given oEmbed HTML iframe. + * + * @since 5.2.0 + * + * @param string $title The title attribute. + * @param string $result The oEmbed HTML result. + * @param object $data A data object result from an oEmbed provider. + * @param string $url The URL of the content to be embedded. + */ + $title = apply_filters( 'oembed_iframe_title_attribute', $title, $result, $data, $url ); + + if ( '' === $title ) { + return $result; + } + + if ( $has_title_attr ) { + // Remove the old title, $matches[1]: quote, $matches[2]: title attribute value. + $result = str_replace( ' title=' . $matches[1] . $matches[2] . $matches[1], '', $result ); + } + + return str_ireplace( '