Make WordPress Core

Ticket #44610: nocookie1-posts.patch

File nocookie1-posts.patch, 3.1 KB (added by adakaleh, 4 years ago)

Allow yt-nocookie embeds within posts

  • src/wp-includes/class-wp-oembed.php

    diff --git a/src/wp-includes/class-wp-oembed.php b/src/wp-includes/class-wp-oembed.php
    index ce75a4f196..acf2f5852f 100644
    a b class WP_oEmbed { 
    5050        public function __construct() {
    5151                $host      = urlencode( home_url() );
    5252                $providers = array(
    53                         '#https?://((m|www)\.)?youtube\.com/watch.*#i' => array( 'https://www.youtube.com/oembed', true ),
    54                         '#https?://((m|www)\.)?youtube\.com/playlist.*#i' => array( 'https://www.youtube.com/oembed', true ),
    55                         '#https?://((m|www)\.)?youtube\.com/shorts/*#i' => array( 'https://www.youtube.com/oembed', true ),
     53                        '#https?://((m|www)\.)?youtube(?:-nocookie)?\.com/watch.*#i' => array( 'https://www.youtube.com/oembed', true ),
     54                        '#https?://((m|www)\.)?youtube(?:-nocookie)?\.com/playlist.*#i' => array( 'https://www.youtube.com/oembed', true ),
     55                        '#https?://((m|www)\.)?youtube(?:-nocookie)?\.com/shorts/*#i' => array( 'https://www.youtube.com/oembed', true ),
    5656                        '#https?://youtu\.be/.*#i'                     => array( 'https://www.youtube.com/oembed', true ),
    5757                        '#https?://(.+\.)?vimeo\.com/.*#i'             => array( 'https://vimeo.com/api/oembed.{format}', true ),
    5858                        '#https?://(www\.)?dailymotion\.com/.*#i'      => array( 'https://www.dailymotion.com/services/oembed', true ),
    class WP_oEmbed { 
    359359                        return false;
    360360                }
    361361
     362                // If the input was a youtube-nocookie.com URL, modify the embed
     363                // to use youtube-nocookie.com.
     364                if ( $provider === 'https://www.youtube.com/oembed' && str_contains( $url, 'youtube-nocookie.com/' ) ) {
     365                        $data->html = str_replace( 'https://www.youtube.com/embed/', 'https://www.youtube-nocookie.com/embed/', $data->html );
     366                        // Remove "feature=oembed" parameter, since the iframe's code
     367                        // is no longer identical to YouTube's oembed output.
     368                        $data->html = str_replace( '?feature=oembed', '', $data->html );
     369                        $data->html = str_replace( '&feature=oembed', '', $data->html );
     370                }
     371
    362372                return $data;
    363373        }
    364374
  • src/wp-includes/embed.php

    diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php
    index 443d399217..20543eb995 100644
    a b function wp_maybe_load_embeds() { 
    202202                return;
    203203        }
    204204
    205         wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
     205        wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube(?:-nocookie)?\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
    206206
    207207        /**
    208208         * Filters the audio embed handler callback.
    function wp_maybe_load_embeds() { 
    241241 */
    242242function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) {
    243243        global $wp_embed;
    244         $embed = $wp_embed->autoembed( sprintf( 'https://youtube.com/watch?v=%s', urlencode( $matches[2] ) ) );
     244        if ( str_contains( $url, 'youtube-nocookie.com/' ) ) {
     245                $embed = $wp_embed->autoembed( sprintf( 'https://youtube-nocookie.com/watch?v=%s', urlencode( $matches[2] ) ) );
     246        } else {
     247                $embed = $wp_embed->autoembed( sprintf( 'https://youtube.com/watch?v=%s', urlencode( $matches[2] ) ) );
     248        }
    245249
    246250        /**
    247251         * Filters the YoutTube embed output.