Make WordPress Core

Ticket #38181: 38181.3.diff

File 38181.3.diff, 5.6 KB (added by adamsilverstein, 9 years ago)
  • src/wp-includes/class-oembed.php

     
    4747        private $compat_methods = array( '_fetch_with_format', '_parse_json', '_parse_xml', '_parse_body' );
    4848
    4949        /**
     50         * Regex used to handle Amazon oEmbed requests.
     51         *
     52         * @since  4.7.0
     53         * @access public
     54         * @var string
     55         */
     56        public static $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';
     57
     58        /**
    5059         * Constructor.
    5160         *
    5261         * @since 2.9.0
     
    108117                        '#https?://www\.facebook\.com/notes/.*#i'             => array( 'https://www.facebook.com/plugins/post/oembed.json/',        true  ),
    109118                        '#https?://www\.facebook\.com/.*/videos/.*#i'         => array( 'https://www.facebook.com/plugins/video/oembed.json/',       true  ),
    110119                        '#https?://www\.facebook\.com/video\.php.*#i'         => array( 'https://www.facebook.com/plugins/video/oembed.json/',       true  ),
     120                        self::$oembed_amazon_format                           => array( 'https://read.amazon.com/kp/api/oembed',                     true  ),
     121                        '#https?://(www\.)?a.co/.*#i'                         => array( 'https://read.amazon.com/kp/api/oembed',                     true  ),
     122                        '#https?://(www\.)?amz.onl/.*#i'                      => array( 'https://read.amazon.com/kp/api/oembed',                     true  ),
     123                        '#https?://(www\.)?amzn.to/.*#i'                      => array( 'https://read.amazon.com/kp/api/oembed',                     true  ),
     124                        '#https?://(www\.)?amzn.eu/.*#i'                      => array( 'https://read.amazon.co.uk/kp/api/oembed',                   true  ),
     125                        '#https?://(www\.)?amzn.in/.*#i'                      => array( 'https://read.amazon.in/kp/api/oembed',                      true  ),
     126                        '#https?://(www\.)?amzn.asia/.*#i'                    => array( 'https://read.amazon.com.au/kp/api/oembed',                  true  ),
     127                        '#https?://(www\.)?z.cn/.*#i'                         => array( 'https://read.amazon.cn/kp/api/oembed',                      true  ),
    111128                );
    112129
    113130                if ( ! empty( self::$early_providers['add'] ) ) {
     
    181198                 * | Twitter      | twitter.com/user      |      Yes       | 4.7.0     |
    182199                 * | Twitter      | twitter.com/likes     |      Yes       | 4.7.0     |
    183200                 * | Twitter      | twitter.com/lists     |      Yes       | 4.7.0     |
     201                 * | Amazon Kindle| a.co                  |      Yes       | 4.7.0     |
     202                 * | Amazon Kindle| amzn.onl              |      Yes       | 4.7.0     |
     203                 * | Amazon Kindle| amzn.to               |      Yes       | 4.7.0     |
     204                 * | Amazon Kindle| amzn.eu               |      Yes       | 4.7.0     |
     205                 * | Amazon Kindle| amzn.in               |      Yes       | 4.7.0     |
     206                 * | Amazon Kindle| amzn.asia             |      Yes       | 4.7.0     |
     207                 * | Amazon Kindle| z.cn                  |      Yes       | 4.7.0     |
    184208                 *
    185209                 * No longer supported providers:
    186210                 *
     
    201225                 */
    202226                $this->providers = apply_filters( 'oembed_providers', $providers );
    203227
     228                // Route Amazon URLs to the correct endpoint based on their TLD.
     229                add_filter( 'oembed_fetch_url', array( $this, '_match_amazon_tld' ), 10, 2 );
     230
     231
    204232                // Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
    205233                add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
    206234        }
     
    690718
    691719                return str_replace( $tokens, $pre, $stripped );
    692720        }
     721
     722        /**
     723         * Route Amazon URLs to the correct endpoint based on their TLD.
     724         *
     725         * Checks if the given URL is an Amazon URL, and if so, alters the
     726         * provider's subdomain and TLD to match the URL's TLD.
     727         *
     728         * These transformations are applied to the oEmbed endpoint served by
     729         * one of the read.amazon.com regionalized domains, which services all
     730         * *.amazon.* URLs for the corresponding region.
     731         *
     732         * This method ensures that oEmbed requests for different Amazon
     733         * marketplaces (.com, .de, .jp, etc) are routed to the correct oEmbed
     734         * endpoint for those marketplaces.
     735         *
     736         * @since  4.7.0
     737         * @access public
     738         *
     739         * @param string $provider The URL to the default oEmbed provider.
     740         * @param string $url      The URL to the content that is desired to be embedded.
     741         * @return string          The URL to the oEmbed provider.
     742         */
     743        public function _match_amazon_tld( $provider, $url ) {
     744                if ( 1 !== preg_match( self::$oembed_amazon_format, $url, $matches ) ) {
     745                        return $provider;
     746                }
     747
     748                $tld = $matches[2];
     749                switch ( $tld ) {
     750                        case 'de':
     751                                $subdomain = 'lesen';
     752                                break;
     753                        case 'fr':
     754                                $subdomain = 'lire';
     755                                break;
     756                        case 'it':
     757                                $subdomain = 'leggi';
     758                                break;
     759                        case 'es':
     760                        case 'com.mx':
     761                                $subdomain = 'leer';
     762                                break;
     763                        case 'com.br':
     764                                $subdomain = 'ler';
     765                                break;
     766                        case 'nl':
     767                                $subdomain = 'lezen';
     768                                break;
     769                        default:
     770                                $subdomain = 'read';
     771                }
     772
     773                return preg_replace(
     774                        '#^https://read.amazon.com/#',
     775                        sprintf( 'https://%s.amazon.%s/', $subdomain, $tld ),
     776                        $provider
     777                );
     778        }
    693779}
  • tests/phpunit/tests/oembed.php

     
    801801                                        'https://www.facebook.com/video.php?v=317622575398',
    802802                                ),
    803803                        ),
     804                        array(
     805                                $providers['amazon'],
     806                                array(
     807                                        'https://read.amazon.com/kp/embed?asin=B00Y7WCHQW&reshareId=06R3BYY9AN8CQ15Q6FTB&reshareChannel=system',
     808                                ),
     809                        ),
    804810                );
    805811        }
    806812