Make WordPress Core

Ticket #38181: 38181.diff

File 38181.diff, 4.1 KB (added by jsepia, 9 years ago)

Changes to class-oembed.php to support Kindle embeds

  • wp-includes/class-oembed.php

     
    1212 */
    1313
    1414/**
     15 * Regex used to handle Amazon OEmbed requests.
     16 *
     17 * @var string
     18 */
     19define( 'OEMBED_AMAZON_FORMAT', '#https?://([a-z0-9-]+\.)?amazon\.(com|co\.uk|de|fr|it|es|co\.jp|cn|com\.au|in|com\.mx|com\.br|ca|nl|ru)/.*#i' );
     20
     21/**
    1522 * Core class used to implement oEmbed functionality.
    1623 *
    1724 * @since 2.9.0
     
    98105                        '#https?://videopress.com/v/.*#'                      => array( 'https://public-api.wordpress.com/oembed/1.0/?for=' . $host, true  ),
    99106                        '#https?://(www\.)?reddit\.com/r/[^/]+/comments/.*#i' => array( 'https://www.reddit.com/oembed',                             true  ),
    100107                        '#https?://(www\.)?speakerdeck\.com/.*#i'             => array( 'https://speakerdeck.com/oembed.{format}',                   true  ),
     108                        OEMBED_AMAZON_FORMAT                                  => array( 'https://read.amazon.com/kp/api/oembed',                     true  ),
     109                        '#https?://(www\.)a.co/.*#i'                          => array( 'https://read.amazon.com/kp/api/oembed',                     true  ),
     110                        '#https?://(www\.)amz.onl/.*#i'                       => array( 'https://read.amazon.com/kp/api/oembed',                     true  ),
     111                        '#https?://(www\.)amzn.to/.*#i'                       => array( 'https://read.amazon.com/kp/api/oembed',                     true  ),
     112                        '#https?://(www\.)amzn.eu/.*#i'                       => array( 'https://read.amazon.co.uk/kp/api/oembed',                   true  ),
     113                        '#https?://(www\.)amzn.in/.*#i'                       => array( 'https://read.amazon.in/kp/api/oembed',                      true  ),
     114                        '#https?://(www\.)amzn.asia/.*#i'                     => array( 'https://read.amazon.com.au/kp/api/oembed',                  true  ),
     115                        '#https?://(www\.)z.cn/.*#i'                          => array( 'https://read.amazon.cn/kp/api/oembed',                      true  ),
    101116                );
    102117
    103118                if ( ! empty( self::$early_providers['add'] ) ) {
     
    187202                 */
    188203                $this->providers = apply_filters( 'oembed_providers', $providers );
    189204
     205                // Ensure that any requests for Amazon URLs are routed to the correct endpoint based on their TLD
     206                add_filter( 'oembed_fetch_url', array($this, '_match_amazon_tld'), 10, 2 );
     207
    190208                // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
    191209                add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
    192210        }
     
    676694
    677695                return str_replace( $tokens, $pre, $stripped );
    678696        }
     697
     698        /**
     699         * Checks if the given URL is an Amazon URL, and if so, alters the
     700         * provider's subdomain and TLD to match the URL's TLD.
     701         *
     702         * These transformations are applied to the oembed endpoint served by
     703         * one of the read.amazon.com regionalized domains, which services all
     704         * *.amazon.* URLs for the corresponding region.
     705         *
     706         * This method ensures that oEmbed requests for different Amazon
     707         * marketplaces (.com, .de, .jp, etc) are routed to the correct oEmbed
     708         * endpoint for those marketplaces.
     709         *
     710         * @since 4.6.2
     711         * @access public
     712         *
     713         * @param string $provider The URL to the default OEmbed provider.
     714         * @param string $url      The URL to the content that is desired to be embedded.
     715         *
     716         * @return string The URL to the OEmbed provider.
     717         */
     718        public function _match_amazon_tld( $provider, $url ) {
     719                if ( 1 !== preg_match( OEMBED_AMAZON_FORMAT, $url, $matches ) ) {
     720                        return $provider;
     721                }
     722
     723                $tld = $matches[2];
     724                switch ( $tld ) {
     725                        case 'de': $subdomain = 'lesen'; break;
     726                        case 'fr': $subdomain = 'lire'; break;
     727                        case 'it': $subdomain = 'leggi'; break;
     728                        case 'es': $subdomain = 'leer'; break;
     729                        case 'com.mx': $subdomain = 'leer'; break;
     730                        case 'com.br': $subdomain = 'ler'; break;
     731                        case 'nl': $subdomain = 'lezen'; break;
     732                        default: $subdomain = 'read';
     733                }
     734               
     735                return preg_replace(
     736                        '#^https://read.amazon.com/#',
     737                        sprintf( 'https://%s.amazon.%s/', $subdomain, $tld ),
     738                        $provider
     739                );
     740        }
    679741}
    680742
    681743/**