WordPress.org

Make WordPress Core

Ticket #41206: 41206.diff

File 41206.diff, 1.8 KB (added by swissspidy, 2 years ago)
  • src/wp-includes/embed.php

    diff --git src/wp-includes/embed.php src/wp-includes/embed.php
    index 2985964ecc..602fb00736 100644
    function wp_maybe_load_embeds() { 
    189189
    190190        wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
    191191
     192        wp_embed_register_handler( 'github_gist', '#https?://gist\.github\.com/([a-zA-Z0-9/]+)(\#file\-[a-zA-Z0-9\_\-]+)?#', 'wp_embed_handler_gist' );
     193
    192194        /**
    193195         * Filters the audio embed handler callback.
    194196         *
    function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) { 
    244246}
    245247
    246248/**
     249 * GitHub Gist embed handler callback.
     250 *
     251 * Gist supports oEmbed, but only returns raw HTML with code excerpts.
     252 * Instead, we use the JavaScript embed method.
     253 *
     254 * @since 4.9.0
     255 *
     256 * @param array  $matches The RegEx matches from the provided regex when calling
     257 *                        wp_embed_register_handler().
     258 * @param array  $attr    Embed attributes.
     259 * @param string $url     The original URL that was matched by the regex.
     260 * @param array  $rawattr The original unmodified attributes.
     261 * @return string The embed HTML.
     262 */
     263function wp_embed_handler_gist( $matches, $attr, $url, $rawattr ) {
     264        if ( false !== strpos( $url, '#file-' ) ) {
     265                // URL points to a specific file in the gist.
     266                $url = str_replace( '#file-', '.json?file=', $url );
     267                $url = preg_replace( '/\-(?!.*\-)/', '.', $url );
     268        } else {
     269                $file = ( ! empty( $matches['file'] ) ) ? '?file=' . urlencode( $matches['file'] ) : '';
     270                // URL points to the entire gist.
     271                $url .= '.json' . $file;
     272        }
     273
     274        return sprintf(
     275                '<script type="text/javascript" src="%s"></script>',
     276                esc_url( str_replace( 'json', 'js', $url ) )
     277        );
     278}
     279
     280/**
    247281 * Audio embed handler callback.
    248282 *
    249283 * @since 3.6.0