]*?wp-embedded-content/', $html ) ) { wp_enqueue_script( 'wp-embed' ); } return $html; } /** * Retrieves the URL to embed a specific post in an iframe. * * @since 4.4.0 * * @param int|WP_Post $post Optional. Post ID or object. Defaults to the current post. * @return string|false The post embed URL on success, false if the post doesn't exist. */ function get_post_embed_url( $post = null ) { $post = get_post( $post ); if ( ! $post ) { return false; } $embed_url = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' ); $path_conflict = get_page_by_path( str_replace( home_url(), '', $embed_url ), OBJECT, get_post_types( array( 'public' => true ) ) ); if ( ! get_option( 'permalink_structure' ) || $path_conflict ) { $embed_url = add_query_arg( array( 'embed' => 'true' ), get_permalink( $post ) ); } /** * Filters the URL to embed a specific post. * * @since 4.4.0 * * @param string $embed_url The post embed URL. * @param WP_Post $post The corresponding post object. */ return sanitize_url( apply_filters( 'post_embed_url', $embed_url, $post ) ); } /** * Retrieves the oEmbed endpoint URL for a given permalink. * * Pass an empty string as the first argument to get the endpoint base URL. * * @since 4.4.0 * * @param string $permalink Optional. The permalink used for the `url` query arg. Default empty. * @param string $format Optional. The requested response format. Default 'json'. * @return string The oEmbed endpoint URL. */ function get_oembed_endpoint_url( $permalink = '', $format = 'json' ) { $url = rest_url( 'oembed/1.0/embed' ); if ( '' !== $permalink ) { $url = add_query_arg( array( 'url' => urlencode( $permalink ), 'format' => ( 'json' !== $format ) ? $format : false, ), $url ); } /** * Filters the oEmbed endpoint URL. * * @since 4.4.0 * * @param string $url The URL to the oEmbed endpoint. * @param string $permalink The permalink used for the `url` query arg. * @param string $format The requested response format. */ return apply_filters( 'oembed_endpoint_url', $url, $permalink, $format ); } /** * Retrieves the embed code for a specific post. * * @since 4.4.0 * * @param int $width The width for the response. * @param int $height The height for the response. * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. * @return string|false Embed code on success, false if post doesn't exist. */ function get_post_embed_html( $width, $height, $post = null ) { $post = get_post( $post ); if ( ! $post ) { return false; } $embed_url = get_post_embed_url( $post ); $secret = wp_generate_password( 10, false ); $embed_url .= "#?secret={$secret}"; $output = sprintf( '%3$s', esc_attr( $secret ), esc_url( get_permalink( $post ) ), get_the_title( $post ) ); $output .= sprintf( '', esc_url( $embed_url ), absint( $width ), absint( $height ), esc_attr( sprintf( /* translators: 1: Post title, 2: Site title. */ __( '“%1$s” — %2$s' ), get_the_title( $post ), get_bloginfo( 'name' ) ) ), esc_attr( $secret ) ); // Note that the script must be placed after the and due to a regexp parsing issue in // `wp_filter_oembed_result()`. Because of the regex pattern starts with `|(.*?)?.*|` // wherein the is marked as being optional, if it is not at the beginning of the string then the group // will fail to match and everything will be matched by `.*` and not included in the group. This regex issue goes // back to WordPress 4.4, so in order to not break older installs this script must come at the end. $output .= wp_get_inline_script_tag( file_get_contents( ABSPATH . WPINC . '/js/wp-embed' . wp_scripts_get_suffix() . '.js' ) ); /** * Filters the embed HTML output for a given post. * * @since 4.4.0 * * @param string $output The default iframe tag to display embedded content. * @param WP_Post $post Current post object. * @param int $width Width of the response. * @param int $height Height of the response. */ return apply_filters( 'embed_html', $output, $post, $width, $height ); } /** * Retrieves the oEmbed response data for a given post. * * @since 4.4.0 * * @param WP_Post|int $post Post ID or post object. * @param int $width The requested width. * @return array|false Response data on success, false if post doesn't exist * or is not publicly viewable. */ function get_oembed_response_data( $post, $width ) { $post = get_post( $post ); $width = absint( $width ); if ( ! $post ) { return false; } if ( ! is_post_publicly_viewable( $post ) ) { return false; } /** * Filters the allowed minimum and maximum widths for the oEmbed response. * * @since 4.4.0 * * @param array $min_max_width { * Minimum and maximum widths for the oEmbed response. * * @type int $min Minimum width. Default 200. * @type int $max Maximum width. Default 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 ); $data = array( 'version' => '1.0', 'provider_name' => get_bloginfo( 'name' ), 'provider_url' => get_home_url(), 'author_name' => get_bloginfo( 'name' ), 'author_url' => get_home_url(), 'title' => get_the_title( $post ), 'type' => 'link', ); $author = get_userdata( $post->post_author ); if ( $author ) { $data['author_name'] = $author->display_name; $data['author_url'] = get_author_posts_url( $author->ID ); } /** * Filters the oEmbed response data. * * @since 4.4.0 * * @param array $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @param int $height The calculated height. */ 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 ); // Do not allow embeds for deleted/archived/spam sites. if ( ! empty( $site->deleted ) || ! empty( $site->spam ) || ! empty( $site->archived ) ) { return false; } if ( $site && get_current_blog_id() !== (int) $site->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. * * @since 4.4.0 * * @param array $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @param int $height The calculated height. * @return array The modified response data. */ function get_oembed_response_data_rich( $data, $post, $width, $height ) { $data['width'] = absint( $width ); $data['height'] = absint( $height ); $data['type'] = 'rich'; $data['html'] = get_post_embed_html( $width, $height, $post ); // Add post thumbnail to response if available. $thumbnail_id = false; if ( has_post_thumbnail( $post->ID ) ) { $thumbnail_id = get_post_thumbnail_id( $post->ID ); } if ( 'attachment' === get_post_type( $post ) ) { if ( wp_attachment_is_image( $post ) ) { $thumbnail_id = $post->ID; } elseif ( wp_attachment_is( 'video', $post ) ) { $thumbnail_id = get_post_thumbnail_id( $post ); $data['type'] = 'video'; } } 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; } return $data; } /** * Ensures that the specified format is either 'json' or 'xml'. * * @since 4.4.0 * * @param string $format The oEmbed response format. Accepts 'json' or 'xml'. * @return string The format, either 'xml' or 'json'. Default 'json'. */ function wp_oembed_ensure_format( $format ) { if ( ! in_array( $format, array( 'json', 'xml' ), true ) ) { return 'json'; } return $format; } /** * Hooks into the REST API output to print XML instead of JSON. * * This is only done for the oEmbed API endpoint, * which supports both formats. * * @access private * @since 4.4.0 * * @param bool $served Whether the request has already been served. * @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * @return true */ function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { $params = $request->get_params(); if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) { return $served; } if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) { return $served; } // Embed links inside the request. $data = $server->response_to_data( $result, false ); if ( ! class_exists( 'SimpleXMLElement' ) ) { status_header( 501 ); die( get_status_header_desc( 501 ) ); } $result = _oembed_create_xml( $data ); // Bail if there's no XML. if ( ! $result ) { status_header( 501 ); return get_status_header_desc( 501 ); } if ( ! headers_sent() ) { $server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) ); } echo $result; return true; } /** * Creates an XML string from a given array. * * @since 4.4.0 * @access private * * @param array $data The original oEmbed response data. * @param SimpleXMLElement $node Optional. XML node to append the result to recursively. * @return string|false XML string on success, false on error. */ function _oembed_create_xml( $data, $node = null ) { if ( ! is_array( $data ) || empty( $data ) ) { return false; } if ( null === $node ) { $node = new SimpleXMLElement( '' ); } foreach ( $data as $key => $value ) { if ( is_numeric( $key ) ) { $key = 'oembed'; } if ( is_array( $value ) ) { $item = $node->addChild( $key ); _oembed_create_xml( $value, $item ); } else { $node->addChild( $key, esc_html( $value ) ); } } return $node->asXML(); } /** * 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' ), true ) ) { return $result; } $title = ! empty( $data->title ) ? $data->title : ''; $pattern = '`]*)>`i'; if ( preg_match( $pattern, $result, $matches ) ) { $attrs = wp_kses_hair( $matches[1], wp_allowed_protocols() ); foreach ( $attrs as $attr => $item ) { $lower_attr = strtolower( $attr ); if ( $lower_attr === $attr ) { continue; } if ( ! isset( $attrs[ $lower_attr ] ) ) { $attrs[ $lower_attr ] = $item; unset( $attrs[ $attr ] ); } } } if ( ! empty( $attrs['title']['value'] ) ) { $title = $attrs['title']['value']; } /** * 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 ( isset( $attrs['title'] ) ) { unset( $attrs['title'] ); $attr_string = implode( ' ', wp_list_pluck( $attrs, 'whole' ) ); $result = str_replace( $matches[0], '', $result ); } return str_ireplace( 'type, array( 'rich', 'video' ), true ) ) { return $result; } $wp_oembed = _wp_oembed_get_object(); // Don't modify the HTML for trusted providers. if ( false !== $wp_oembed->get_provider( $url, array( 'discover' => false ) ) ) { return $result; } $allowed_html = array( 'a' => array( 'href' => true, ), 'blockquote' => array(), 'iframe' => array( 'src' => true, 'width' => true, 'height' => true, 'frameborder' => true, 'marginwidth' => true, 'marginheight' => true, 'scrolling' => true, 'title' => true, ), ); $html = wp_kses( $result, $allowed_html ); preg_match( '|(.*?)?.*()|ms', $html, $content ); // We require at least the iframe to exist. if ( empty( $content[2] ) ) { return false; } $html = $content[1] . $content[2]; preg_match( '/ src=([\'"])(.*?)\1/', $html, $results ); if ( ! empty( $results ) ) { $secret = wp_generate_password( 10, false ); $url = esc_url( "{$results[2]}#?secret=$secret" ); $q = $results[1]; $html = str_replace( $results[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html ); $html = str_replace( '%2$s', esc_url( get_permalink() ), /* translators: %s: Post title. */ sprintf( __( 'Continue reading %s' ), '' . get_the_title() . '' ) ); return ' … ' . $link; } /** * Displays the post excerpt for the embed template. * * Intended to be used in 'The Loop'. * * @since 4.4.0 */ function the_excerpt_embed() { $output = get_the_excerpt(); /** * Filters the post excerpt for the embed template. * * @since 4.4.0 * * @param string $output The current post excerpt. */ echo apply_filters( 'the_excerpt_embed', $output ); } /** * Filters the post excerpt for the embed template. * * Shows players for video and audio attachments. * * @since 4.4.0 * * @param string $content The current post excerpt. * @return string The modified post excerpt. */ function wp_embed_excerpt_attachment( $content ) { if ( is_attachment() ) { return prepend_attachment( '' ); } return $content; } /** * Enqueues embed iframe default CSS and JS. * * Enqueue PNG fallback CSS for embed iframe for legacy versions of IE. * * Allows plugins to queue scripts for the embed iframe end using wp_enqueue_script(). * Runs first in oembed_head(). * * @since 4.4.0 */ function enqueue_embed_scripts() { wp_enqueue_style( 'wp-embed-template-ie' ); /** * Fires when scripts and styles are enqueued for the embed iframe. * * @since 4.4.0 */ do_action( 'enqueue_embed_scripts' ); } /** * Prints the CSS in the embed iframe header. * * @since 4.4.0 */ function print_embed_styles() { $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; $suffix = SCRIPT_DEBUG ? '' : '.min'; ?> Comment', '%s Comments', get_comments_number() ), number_format_i18n( get_comments_number() ) ); ?> </textarea> %s', esc_url( home_url() ), esc_url( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ), esc_url( get_site_icon_url( 64, includes_url( 'images/w-logo-blue.png' ) ) ), esc_html( get_bloginfo( 'name' ) ) ); $site_title = '' . $site_title . ''; /** * Filters the site title HTML in the embed footer. * * @since 4.4.0 * * @param string $site_title The site title HTML. */ echo apply_filters( 'embed_site_title_html', $site_title ); } /** * Filters the oEmbed result before any HTTP requests are made. * * If the URL belongs to the current site, the result is fetched directly instead of * going through the oEmbed discovery process. * * @since 4.5.3 * * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null. * @param string $url The URL that should be inspected for discovery `` tags. * @param array $args oEmbed remote get arguments. * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. * Null if the URL does not belong to the current site. */ function wp_filter_pre_oembed_result( $result, $url, $args ) { $data = get_oembed_response_data_for_url( $url, $args ); if ( $data ) { return _wp_oembed_get_object()->data2html( $data, $url ); } return $result; }
%3$s
and due to a regexp parsing issue in // `wp_filter_oembed_result()`. Because of the regex pattern starts with `|(.*?)?.*|` // wherein the is marked as being optional, if it is not at the beginning of the string then the group // will fail to match and everything will be matched by `.*` and not included in the group. This regex issue goes // back to WordPress 4.4, so in order to not break older installs this script must come at the end. $output .= wp_get_inline_script_tag( file_get_contents( ABSPATH . WPINC . '/js/wp-embed' . wp_scripts_get_suffix() . '.js' ) ); /** * Filters the embed HTML output for a given post. * * @since 4.4.0 * * @param string $output The default iframe tag to display embedded content. * @param WP_Post $post Current post object. * @param int $width Width of the response. * @param int $height Height of the response. */ return apply_filters( 'embed_html', $output, $post, $width, $height ); } /** * Retrieves the oEmbed response data for a given post. * * @since 4.4.0 * * @param WP_Post|int $post Post ID or post object. * @param int $width The requested width. * @return array|false Response data on success, false if post doesn't exist * or is not publicly viewable. */ function get_oembed_response_data( $post, $width ) { $post = get_post( $post ); $width = absint( $width ); if ( ! $post ) { return false; } if ( ! is_post_publicly_viewable( $post ) ) { return false; } /** * Filters the allowed minimum and maximum widths for the oEmbed response. * * @since 4.4.0 * * @param array $min_max_width { * Minimum and maximum widths for the oEmbed response. * * @type int $min Minimum width. Default 200. * @type int $max Maximum width. Default 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 ); $data = array( 'version' => '1.0', 'provider_name' => get_bloginfo( 'name' ), 'provider_url' => get_home_url(), 'author_name' => get_bloginfo( 'name' ), 'author_url' => get_home_url(), 'title' => get_the_title( $post ), 'type' => 'link', ); $author = get_userdata( $post->post_author ); if ( $author ) { $data['author_name'] = $author->display_name; $data['author_url'] = get_author_posts_url( $author->ID ); } /** * Filters the oEmbed response data. * * @since 4.4.0 * * @param array $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @param int $height The calculated height. */ 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 ); // Do not allow embeds for deleted/archived/spam sites. if ( ! empty( $site->deleted ) || ! empty( $site->spam ) || ! empty( $site->archived ) ) { return false; } if ( $site && get_current_blog_id() !== (int) $site->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. * * @since 4.4.0 * * @param array $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @param int $height The calculated height. * @return array The modified response data. */ function get_oembed_response_data_rich( $data, $post, $width, $height ) { $data['width'] = absint( $width ); $data['height'] = absint( $height ); $data['type'] = 'rich'; $data['html'] = get_post_embed_html( $width, $height, $post ); // Add post thumbnail to response if available. $thumbnail_id = false; if ( has_post_thumbnail( $post->ID ) ) { $thumbnail_id = get_post_thumbnail_id( $post->ID ); } if ( 'attachment' === get_post_type( $post ) ) { if ( wp_attachment_is_image( $post ) ) { $thumbnail_id = $post->ID; } elseif ( wp_attachment_is( 'video', $post ) ) { $thumbnail_id = get_post_thumbnail_id( $post ); $data['type'] = 'video'; } } 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; } return $data; } /** * Ensures that the specified format is either 'json' or 'xml'. * * @since 4.4.0 * * @param string $format The oEmbed response format. Accepts 'json' or 'xml'. * @return string The format, either 'xml' or 'json'. Default 'json'. */ function wp_oembed_ensure_format( $format ) { if ( ! in_array( $format, array( 'json', 'xml' ), true ) ) { return 'json'; } return $format; } /** * Hooks into the REST API output to print XML instead of JSON. * * This is only done for the oEmbed API endpoint, * which supports both formats. * * @access private * @since 4.4.0 * * @param bool $served Whether the request has already been served. * @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * @return true */ function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { $params = $request->get_params(); if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) { return $served; } if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) { return $served; } // Embed links inside the request. $data = $server->response_to_data( $result, false ); if ( ! class_exists( 'SimpleXMLElement' ) ) { status_header( 501 ); die( get_status_header_desc( 501 ) ); } $result = _oembed_create_xml( $data ); // Bail if there's no XML. if ( ! $result ) { status_header( 501 ); return get_status_header_desc( 501 ); } if ( ! headers_sent() ) { $server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) ); } echo $result; return true; } /** * Creates an XML string from a given array. * * @since 4.4.0 * @access private * * @param array $data The original oEmbed response data. * @param SimpleXMLElement $node Optional. XML node to append the result to recursively. * @return string|false XML string on success, false on error. */ function _oembed_create_xml( $data, $node = null ) { if ( ! is_array( $data ) || empty( $data ) ) { return false; } if ( null === $node ) { $node = new SimpleXMLElement( '' ); } foreach ( $data as $key => $value ) { if ( is_numeric( $key ) ) { $key = 'oembed'; } if ( is_array( $value ) ) { $item = $node->addChild( $key ); _oembed_create_xml( $value, $item ); } else { $node->addChild( $key, esc_html( $value ) ); } } return $node->asXML(); } /** * 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' ), true ) ) { return $result; } $title = ! empty( $data->title ) ? $data->title : ''; $pattern = '`]*)>`i'; if ( preg_match( $pattern, $result, $matches ) ) { $attrs = wp_kses_hair( $matches[1], wp_allowed_protocols() ); foreach ( $attrs as $attr => $item ) { $lower_attr = strtolower( $attr ); if ( $lower_attr === $attr ) { continue; } if ( ! isset( $attrs[ $lower_attr ] ) ) { $attrs[ $lower_attr ] = $item; unset( $attrs[ $attr ] ); } } } if ( ! empty( $attrs['title']['value'] ) ) { $title = $attrs['title']['value']; } /** * 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 ( isset( $attrs['title'] ) ) { unset( $attrs['title'] ); $attr_string = implode( ' ', wp_list_pluck( $attrs, 'whole' ) ); $result = str_replace( $matches[0], '', $result ); } return str_ireplace( 'type, array( 'rich', 'video' ), true ) ) { return $result; } $wp_oembed = _wp_oembed_get_object(); // Don't modify the HTML for trusted providers. if ( false !== $wp_oembed->get_provider( $url, array( 'discover' => false ) ) ) { return $result; } $allowed_html = array( 'a' => array( 'href' => true, ), 'blockquote' => array(), 'iframe' => array( 'src' => true, 'width' => true, 'height' => true, 'frameborder' => true, 'marginwidth' => true, 'marginheight' => true, 'scrolling' => true, 'title' => true, ), ); $html = wp_kses( $result, $allowed_html ); preg_match( '|(.*?)?.*()|ms', $html, $content ); // We require at least the iframe to exist. if ( empty( $content[2] ) ) { return false; } $html = $content[1] . $content[2]; preg_match( '/ src=([\'"])(.*?)\1/', $html, $results ); if ( ! empty( $results ) ) { $secret = wp_generate_password( 10, false ); $url = esc_url( "{$results[2]}#?secret=$secret" ); $q = $results[1]; $html = str_replace( $results[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html ); $html = str_replace( '%2$s', esc_url( get_permalink() ), /* translators: %s: Post title. */ sprintf( __( 'Continue reading %s' ), '' . get_the_title() . '' ) ); return ' … ' . $link; } /** * Displays the post excerpt for the embed template. * * Intended to be used in 'The Loop'. * * @since 4.4.0 */ function the_excerpt_embed() { $output = get_the_excerpt(); /** * Filters the post excerpt for the embed template. * * @since 4.4.0 * * @param string $output The current post excerpt. */ echo apply_filters( 'the_excerpt_embed', $output ); } /** * Filters the post excerpt for the embed template. * * Shows players for video and audio attachments. * * @since 4.4.0 * * @param string $content The current post excerpt. * @return string The modified post excerpt. */ function wp_embed_excerpt_attachment( $content ) { if ( is_attachment() ) { return prepend_attachment( '' ); } return $content; } /** * Enqueues embed iframe default CSS and JS. * * Enqueue PNG fallback CSS for embed iframe for legacy versions of IE. * * Allows plugins to queue scripts for the embed iframe end using wp_enqueue_script(). * Runs first in oembed_head(). * * @since 4.4.0 */ function enqueue_embed_scripts() { wp_enqueue_style( 'wp-embed-template-ie' ); /** * Fires when scripts and styles are enqueued for the embed iframe. * * @since 4.4.0 */ do_action( 'enqueue_embed_scripts' ); } /** * Prints the CSS in the embed iframe header. * * @since 4.4.0 */ function print_embed_styles() { $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; $suffix = SCRIPT_DEBUG ? '' : '.min'; ?> Comment', '%s Comments', get_comments_number() ), number_format_i18n( get_comments_number() ) ); ?> </textarea> %s', esc_url( home_url() ), esc_url( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ), esc_url( get_site_icon_url( 64, includes_url( 'images/w-logo-blue.png' ) ) ), esc_html( get_bloginfo( 'name' ) ) ); $site_title = '' . $site_title . ''; /** * Filters the site title HTML in the embed footer. * * @since 4.4.0 * * @param string $site_title The site title HTML. */ echo apply_filters( 'embed_site_title_html', $site_title ); } /** * Filters the oEmbed result before any HTTP requests are made. * * If the URL belongs to the current site, the result is fetched directly instead of * going through the oEmbed discovery process. * * @since 4.5.3 * * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null. * @param string $url The URL that should be inspected for discovery `` tags. * @param array $args oEmbed remote get arguments. * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. * Null if the URL does not belong to the current site. */ function wp_filter_pre_oembed_result( $result, $url, $args ) { $data = get_oembed_response_data_for_url( $url, $args ); if ( $data ) { return _wp_oembed_get_object()->data2html( $data, $url ); } return $result; }
.*?
is marked as being optional, if it is not at the beginning of the string then the group // will fail to match and everything will be matched by `.*` and not included in the group. This regex issue goes // back to WordPress 4.4, so in order to not break older installs this script must come at the end. $output .= wp_get_inline_script_tag( file_get_contents( ABSPATH . WPINC . '/js/wp-embed' . wp_scripts_get_suffix() . '.js' ) ); /** * Filters the embed HTML output for a given post. * * @since 4.4.0 * * @param string $output The default iframe tag to display embedded content. * @param WP_Post $post Current post object. * @param int $width Width of the response. * @param int $height Height of the response. */ return apply_filters( 'embed_html', $output, $post, $width, $height ); } /** * Retrieves the oEmbed response data for a given post. * * @since 4.4.0 * * @param WP_Post|int $post Post ID or post object. * @param int $width The requested width. * @return array|false Response data on success, false if post doesn't exist * or is not publicly viewable. */ function get_oembed_response_data( $post, $width ) { $post = get_post( $post ); $width = absint( $width ); if ( ! $post ) { return false; } if ( ! is_post_publicly_viewable( $post ) ) { return false; } /** * Filters the allowed minimum and maximum widths for the oEmbed response. * * @since 4.4.0 * * @param array $min_max_width { * Minimum and maximum widths for the oEmbed response. * * @type int $min Minimum width. Default 200. * @type int $max Maximum width. Default 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 ); $data = array( 'version' => '1.0', 'provider_name' => get_bloginfo( 'name' ), 'provider_url' => get_home_url(), 'author_name' => get_bloginfo( 'name' ), 'author_url' => get_home_url(), 'title' => get_the_title( $post ), 'type' => 'link', ); $author = get_userdata( $post->post_author ); if ( $author ) { $data['author_name'] = $author->display_name; $data['author_url'] = get_author_posts_url( $author->ID ); } /** * Filters the oEmbed response data. * * @since 4.4.0 * * @param array $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @param int $height The calculated height. */ 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 ); // Do not allow embeds for deleted/archived/spam sites. if ( ! empty( $site->deleted ) || ! empty( $site->spam ) || ! empty( $site->archived ) ) { return false; } if ( $site && get_current_blog_id() !== (int) $site->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. * * @since 4.4.0 * * @param array $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @param int $height The calculated height. * @return array The modified response data. */ function get_oembed_response_data_rich( $data, $post, $width, $height ) { $data['width'] = absint( $width ); $data['height'] = absint( $height ); $data['type'] = 'rich'; $data['html'] = get_post_embed_html( $width, $height, $post ); // Add post thumbnail to response if available. $thumbnail_id = false; if ( has_post_thumbnail( $post->ID ) ) { $thumbnail_id = get_post_thumbnail_id( $post->ID ); } if ( 'attachment' === get_post_type( $post ) ) { if ( wp_attachment_is_image( $post ) ) { $thumbnail_id = $post->ID; } elseif ( wp_attachment_is( 'video', $post ) ) { $thumbnail_id = get_post_thumbnail_id( $post ); $data['type'] = 'video'; } } 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; } return $data; } /** * Ensures that the specified format is either 'json' or 'xml'. * * @since 4.4.0 * * @param string $format The oEmbed response format. Accepts 'json' or 'xml'. * @return string The format, either 'xml' or 'json'. Default 'json'. */ function wp_oembed_ensure_format( $format ) { if ( ! in_array( $format, array( 'json', 'xml' ), true ) ) { return 'json'; } return $format; } /** * Hooks into the REST API output to print XML instead of JSON. * * This is only done for the oEmbed API endpoint, * which supports both formats. * * @access private * @since 4.4.0 * * @param bool $served Whether the request has already been served. * @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * @return true */ function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { $params = $request->get_params(); if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) { return $served; } if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) { return $served; } // Embed links inside the request. $data = $server->response_to_data( $result, false ); if ( ! class_exists( 'SimpleXMLElement' ) ) { status_header( 501 ); die( get_status_header_desc( 501 ) ); } $result = _oembed_create_xml( $data ); // Bail if there's no XML. if ( ! $result ) { status_header( 501 ); return get_status_header_desc( 501 ); } if ( ! headers_sent() ) { $server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) ); } echo $result; return true; } /** * Creates an XML string from a given array. * * @since 4.4.0 * @access private * * @param array $data The original oEmbed response data. * @param SimpleXMLElement $node Optional. XML node to append the result to recursively. * @return string|false XML string on success, false on error. */ function _oembed_create_xml( $data, $node = null ) { if ( ! is_array( $data ) || empty( $data ) ) { return false; } if ( null === $node ) { $node = new SimpleXMLElement( '' ); } foreach ( $data as $key => $value ) { if ( is_numeric( $key ) ) { $key = 'oembed'; } if ( is_array( $value ) ) { $item = $node->addChild( $key ); _oembed_create_xml( $value, $item ); } else { $node->addChild( $key, esc_html( $value ) ); } } return $node->asXML(); } /** * 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' ), true ) ) { return $result; } $title = ! empty( $data->title ) ? $data->title : ''; $pattern = '`]*)>`i'; if ( preg_match( $pattern, $result, $matches ) ) { $attrs = wp_kses_hair( $matches[1], wp_allowed_protocols() ); foreach ( $attrs as $attr => $item ) { $lower_attr = strtolower( $attr ); if ( $lower_attr === $attr ) { continue; } if ( ! isset( $attrs[ $lower_attr ] ) ) { $attrs[ $lower_attr ] = $item; unset( $attrs[ $attr ] ); } } } if ( ! empty( $attrs['title']['value'] ) ) { $title = $attrs['title']['value']; } /** * 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 ( isset( $attrs['title'] ) ) { unset( $attrs['title'] ); $attr_string = implode( ' ', wp_list_pluck( $attrs, 'whole' ) ); $result = str_replace( $matches[0], '', $result ); } return str_ireplace( 'type, array( 'rich', 'video' ), true ) ) { return $result; } $wp_oembed = _wp_oembed_get_object(); // Don't modify the HTML for trusted providers. if ( false !== $wp_oembed->get_provider( $url, array( 'discover' => false ) ) ) { return $result; } $allowed_html = array( 'a' => array( 'href' => true, ), 'blockquote' => array(), 'iframe' => array( 'src' => true, 'width' => true, 'height' => true, 'frameborder' => true, 'marginwidth' => true, 'marginheight' => true, 'scrolling' => true, 'title' => true, ), ); $html = wp_kses( $result, $allowed_html ); preg_match( '|(.*?)?.*()|ms', $html, $content ); // We require at least the iframe to exist. if ( empty( $content[2] ) ) { return false; } $html = $content[1] . $content[2]; preg_match( '/ src=([\'"])(.*?)\1/', $html, $results ); if ( ! empty( $results ) ) { $secret = wp_generate_password( 10, false ); $url = esc_url( "{$results[2]}#?secret=$secret" ); $q = $results[1]; $html = str_replace( $results[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html ); $html = str_replace( '%2$s', esc_url( get_permalink() ), /* translators: %s: Post title. */ sprintf( __( 'Continue reading %s' ), '' . get_the_title() . '' ) ); return ' … ' . $link; } /** * Displays the post excerpt for the embed template. * * Intended to be used in 'The Loop'. * * @since 4.4.0 */ function the_excerpt_embed() { $output = get_the_excerpt(); /** * Filters the post excerpt for the embed template. * * @since 4.4.0 * * @param string $output The current post excerpt. */ echo apply_filters( 'the_excerpt_embed', $output ); } /** * Filters the post excerpt for the embed template. * * Shows players for video and audio attachments. * * @since 4.4.0 * * @param string $content The current post excerpt. * @return string The modified post excerpt. */ function wp_embed_excerpt_attachment( $content ) { if ( is_attachment() ) { return prepend_attachment( '' ); } return $content; } /** * Enqueues embed iframe default CSS and JS. * * Enqueue PNG fallback CSS for embed iframe for legacy versions of IE. * * Allows plugins to queue scripts for the embed iframe end using wp_enqueue_script(). * Runs first in oembed_head(). * * @since 4.4.0 */ function enqueue_embed_scripts() { wp_enqueue_style( 'wp-embed-template-ie' ); /** * Fires when scripts and styles are enqueued for the embed iframe. * * @since 4.4.0 */ do_action( 'enqueue_embed_scripts' ); } /** * Prints the CSS in the embed iframe header. * * @since 4.4.0 */ function print_embed_styles() { $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; $suffix = SCRIPT_DEBUG ? '' : '.min'; ?> Comment', '%s Comments', get_comments_number() ), number_format_i18n( get_comments_number() ) ); ?> </textarea> %s', esc_url( home_url() ), esc_url( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ), esc_url( get_site_icon_url( 64, includes_url( 'images/w-logo-blue.png' ) ) ), esc_html( get_bloginfo( 'name' ) ) ); $site_title = '' . $site_title . ''; /** * Filters the site title HTML in the embed footer. * * @since 4.4.0 * * @param string $site_title The site title HTML. */ echo apply_filters( 'embed_site_title_html', $site_title ); } /** * Filters the oEmbed result before any HTTP requests are made. * * If the URL belongs to the current site, the result is fetched directly instead of * going through the oEmbed discovery process. * * @since 4.5.3 * * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null. * @param string $url The URL that should be inspected for discovery `` tags. * @param array $args oEmbed remote get arguments. * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. * Null if the URL does not belong to the current site. */ function wp_filter_pre_oembed_result( $result, $url, $args ) { $data = get_oembed_response_data_for_url( $url, $args ); if ( $data ) { return _wp_oembed_get_object()->data2html( $data, $url ); } return $result; }
%2$s', esc_url( get_permalink() ), /* translators: %s: Post title. */ sprintf( __( 'Continue reading %s' ), '' . get_the_title() . '' ) ); return ' … ' . $link; } /** * Displays the post excerpt for the embed template. * * Intended to be used in 'The Loop'. * * @since 4.4.0 */ function the_excerpt_embed() { $output = get_the_excerpt(); /** * Filters the post excerpt for the embed template. * * @since 4.4.0 * * @param string $output The current post excerpt. */ echo apply_filters( 'the_excerpt_embed', $output ); } /** * Filters the post excerpt for the embed template. * * Shows players for video and audio attachments. * * @since 4.4.0 * * @param string $content The current post excerpt. * @return string The modified post excerpt. */ function wp_embed_excerpt_attachment( $content ) { if ( is_attachment() ) { return prepend_attachment( '' ); } return $content; } /** * Enqueues embed iframe default CSS and JS. * * Enqueue PNG fallback CSS for embed iframe for legacy versions of IE. * * Allows plugins to queue scripts for the embed iframe end using wp_enqueue_script(). * Runs first in oembed_head(). * * @since 4.4.0 */ function enqueue_embed_scripts() { wp_enqueue_style( 'wp-embed-template-ie' ); /** * Fires when scripts and styles are enqueued for the embed iframe. * * @since 4.4.0 */ do_action( 'enqueue_embed_scripts' ); } /** * Prints the CSS in the embed iframe header. * * @since 4.4.0 */ function print_embed_styles() { $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; $suffix = SCRIPT_DEBUG ? '' : '.min'; ?> Comment', '%s Comments', get_comments_number() ), number_format_i18n( get_comments_number() ) ); ?> </textarea> %s', esc_url( home_url() ), esc_url( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ), esc_url( get_site_icon_url( 64, includes_url( 'images/w-logo-blue.png' ) ) ), esc_html( get_bloginfo( 'name' ) ) ); $site_title = '' . $site_title . ''; /** * Filters the site title HTML in the embed footer. * * @since 4.4.0 * * @param string $site_title The site title HTML. */ echo apply_filters( 'embed_site_title_html', $site_title ); } /** * Filters the oEmbed result before any HTTP requests are made. * * If the URL belongs to the current site, the result is fetched directly instead of * going through the oEmbed discovery process. * * @since 4.5.3 * * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null. * @param string $url The URL that should be inspected for discovery `` tags. * @param array $args oEmbed remote get arguments. * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. * Null if the URL does not belong to the current site. */ function wp_filter_pre_oembed_result( $result, $url, $args ) { $data = get_oembed_response_data_for_url( $url, $args ); if ( $data ) { return _wp_oembed_get_object()->data2html( $data, $url ); } return $result; }