wp/wp-includes/class-oembed.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * API for fetching the HTML to embed remote content based on a provided URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * Used internally by the {@link WP_Embed} class, but is designed to be generic.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     6
 * @link https://codex.wordpress.org/oEmbed oEmbed Codex Article
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @link http://oembed.com/ oEmbed Homepage
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * @subpackage oEmbed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * oEmbed class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * @subpackage oEmbed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
class WP_oEmbed {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    21
	public $providers = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    22
	public static $early_providers = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    23
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
	private $compat_methods = array( '_fetch_with_format', '_parse_json', '_parse_xml', '_parse_body' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
	 * Constructor
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
	 * @since 2.9.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
	public function __construct() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		$providers = array(
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
			'#http://(www\.)?youtube\.com/watch.*#i'              => array( 'http://www.youtube.com/oembed',                      true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
			'#https://(www\.)?youtube\.com/watch.*#i'             => array( 'http://www.youtube.com/oembed?scheme=https',         true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
			'#http://(www\.)?youtube\.com/playlist.*#i'           => array( 'http://www.youtube.com/oembed',                      true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
			'#https://(www\.)?youtube\.com/playlist.*#i'          => array( 'http://www.youtube.com/oembed?scheme=https',         true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
			'#http://youtu\.be/.*#i'                              => array( 'http://www.youtube.com/oembed',                      true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
			'#https://youtu\.be/.*#i'                             => array( 'http://www.youtube.com/oembed?scheme=https',         true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
			'http://blip.tv/*'                                    => array( 'http://blip.tv/oembed/',                             false ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
			'#https?://(.+\.)?vimeo\.com/.*#i'                    => array( 'http://vimeo.com/api/oembed.{format}',               true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
			'#https?://(www\.)?dailymotion\.com/.*#i'             => array( 'http://www.dailymotion.com/services/oembed',         true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
			'http://dai.ly/*'                                     => array( 'http://www.dailymotion.com/services/oembed',         false ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
			'#https?://(www\.)?flickr\.com/.*#i'                  => array( 'https://www.flickr.com/services/oembed/',            true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
			'#https?://flic\.kr/.*#i'                             => array( 'https://www.flickr.com/services/oembed/',            true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
			'#https?://(.+\.)?smugmug\.com/.*#i'                  => array( 'http://api.smugmug.com/services/oembed/',            true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
			'#https?://(www\.)?hulu\.com/watch/.*#i'              => array( 'http://www.hulu.com/api/oembed.{format}',            true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
			'http://i*.photobucket.com/albums/*'                  => array( 'http://photobucket.com/oembed',                      false ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
			'http://gi*.photobucket.com/groups/*'                 => array( 'http://photobucket.com/oembed',                      false ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
			'#https?://(www\.)?scribd\.com/doc/.*#i'              => array( 'http://www.scribd.com/services/oembed',              true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
			'#https?://wordpress.tv/.*#i'                         => array( 'http://wordpress.tv/oembed/',                        true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    51
			'#https?://(.+\.)?polldaddy\.com/.*#i'                => array( 'https://polldaddy.com/oembed/',                      true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
			'#https?://poll\.fm/.*#i'                             => array( 'https://polldaddy.com/oembed/',                      true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
			'#https?://(www\.)?funnyordie\.com/videos/.*#i'       => array( 'http://www.funnyordie.com/oembed',                   true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
			'#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i' => array( 'https://api.twitter.com/1/statuses/oembed.{format}', true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
			'#https?://vine.co/v/.*#i'                            => array( 'https://vine.co/oembed.{format}',                    true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
 			'#https?://(www\.)?soundcloud\.com/.*#i'              => array( 'http://soundcloud.com/oembed',                       true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
			'#https?://(.+?\.)?slideshare\.net/.*#i'              => array( 'https://www.slideshare.net/api/oembed/2',            true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
			'#https?://instagr(\.am|am\.com)/p/.*#i'              => array( 'https://api.instagram.com/oembed',                   true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
			'#https?://(www\.)?rdio\.com/.*#i'                    => array( 'http://www.rdio.com/api/oembed/',                    true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
			'#https?://rd\.io/x/.*#i'                             => array( 'http://www.rdio.com/api/oembed/',                    true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
			'#https?://(open|play)\.spotify\.com/.*#i'            => array( 'https://embed.spotify.com/oembed/',                  true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
			'#https?://(.+\.)?imgur\.com/.*#i'                    => array( 'http://api.imgur.com/oembed',                        true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
			'#https?://(www\.)?meetu(\.ps|p\.com)/.*#i'           => array( 'http://api.meetup.com/oembed',                       true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
			'#https?://(www\.)?issuu\.com/.+/docs/.+#i'           => array( 'http://issuu.com/oembed_wp',                         true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
			'#https?://(www\.)?collegehumor\.com/video/.*#i'      => array( 'http://www.collegehumor.com/oembed.{format}',        true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
			'#https?://(www\.)?mixcloud\.com/.*#i'                => array( 'http://www.mixcloud.com/oembed',                     true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
			'#https?://(www\.|embed\.)?ted\.com/talks/.*#i'       => array( 'http://www.ted.com/talks/oembed.{format}',           true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
			'#https?://(www\.)?(animoto|video214)\.com/play/.*#i' => array( 'http://animoto.com/oembeds/create',                  true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
			'#https?://(.+)\.tumblr\.com/post/.*#i'               => array( 'https://www.tumblr.com/oembed/1.0',                  true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
			'#https?://(www\.)?kickstarter\.com/projects/.*#i'    => array( 'https://www.kickstarter.com/services/oembed',        true  ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    71
			'#https?://kck\.st/.*#i'                              => array( 'https://www.kickstarter.com/services/oembed',        true  ),
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    74
		if ( ! empty( self::$early_providers['add'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    75
			foreach ( self::$early_providers['add'] as $format => $data ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
				$providers[ $format ] = $data;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
		if ( ! empty( self::$early_providers['remove'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
			foreach ( self::$early_providers['remove'] as $format ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    82
				unset( $providers[ $format ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
		self::$early_providers = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		 * Filter the list of oEmbed providers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		 * Discovery is disabled for users lacking the unfiltered_html capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		 * Only providers in this array will be used for those users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
		 * Supported providers:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
		 * |   Provider   |        Flavor        |  SSL  |   Since   |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
		 * | Blip         | blip.tv              |   !   | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
		 * | Dailymotion  | dailymotion.com      |  Yes  | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
		 * | Flickr       | flickr.com           |  Yes  | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   102
		 * | Hulu         | hulu.com             |  Yes  | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
		 * | Photobucket  | photobucket.com      |   !   | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
		 * | Scribd       | scribd.com           |  Yes  | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   105
		 * | Vimeo        | vimeo.com            |  Yes  | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
		 * | WordPress.tv | wordpress.tv         |  Yes  | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
		 * | YouTube      | youtube.com/watch    |  Yes  | 2.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
		 * | Funny or Die | funnyordie.com       |  Yes  | 3.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
		 * | Polldaddy    | polldaddy.com        |  Yes  | 3.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
		 * | SmugMug      | smugmug.com          |  Yes  | 3.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
		 * | YouTube      | youtu.be             |  Yes  | 3.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
		 * | Twitter      | twitter.com          |  Yes  | 3.4.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
		 * | Instagram    | instagram.com        |  Yes  | 3.5.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
		 * | Instagram    | instagr.am           |  Yes  | 3.5.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
		 * | Slideshare   | slideshare.net       |  Yes  | 3.5.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
		 * | SoundCloud   | soundcloud.com       |  Yes  | 3.5.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
		 * | Dailymotion  | dai.ly               |   !   | 3.6.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
		 * | Flickr       | flic.kr              |  Yes  | 3.6.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   123
		 * | Rdio         | rdio.com             |  Yes  | 3.6.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
		 * | Rdio         | rd.io                |  Yes  | 3.6.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
		 * | Spotify      | spotify.com          |  Yes  | 3.6.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   127
		 * | Imgur        | imgur.com            |  Yes  | 3.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   128
		 * | Meetup.com   | meetup.com           |  Yes  | 3.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   129
		 * | Meetup.com   | meetu.ps             |  Yes  | 3.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   130
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   131
		 * | Animoto      | animoto.com          |  Yes  | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   132
		 * | Animoto      | video214.com         |  Yes  | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   133
		 * | CollegeHumor | collegehumor.com     |  Yes  | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   134
		 * | Issuu        | issuu.com            |  Yes  | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   135
		 * | Mixcloud     | mixcloud.com         |  Yes  | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   136
		 * | Polldaddy    | poll.fm              |  Yes  | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   137
		 * | TED          | ted.com              |  Yes  | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   138
		 * | YouTube      | youtube.com/playlist |  Yes  | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   139
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
		 * | Vine         | vine.co              |  Yes  | 4.1.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
		 * | Tumblr       | tumblr.com           |  Yes  | 4.2.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   143
		 * | Kickstarter  | kickstarter.com      |  Yes  | 4.2.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
		 * | Kickstarter  | kck.st               |  Yes  | 4.2.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
		 * | ------------ | -------------------- | ----- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
		 * No longer supported providers:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   149
		 * | ------------ | -------------------- | ----- | --------- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
		 * |   Provider   |        Flavor        |  SSL  |   Since   |  Removed  |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
		 * | ------------ | -------------------- | ----- | --------- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   152
		 * | Qik          | qik.com              |  Yes  | 2.9.0     | 3.9.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
		 * | ------------ | -------------------- | ----- | --------- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   154
		 * | Viddler      | viddler.com          |  Yes  | 2.9.0     | 4.0.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
		 * | ------------ | -------------------- | ----- | --------- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
		 * | Revision3    | revision3.com        |   !   | 2.9.0     | 4.2.0     |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
		 * | ------------ | -------------------- | ----- | --------- | --------- |
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
		 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
		 * @see wp_oembed_add_provider()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
		 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
		 * @param array $providers An array of popular oEmbed providers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
		$this->providers = apply_filters( 'oembed_providers', $providers );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		// Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
		add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   172
	 * Make private/protected methods readable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   173
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   174
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   175
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   176
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
	 * @param callable $name      Method to call.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
	 * @param array    $arguments Arguments to pass when calling.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
	 * @return mixed|bool Return value of the callback, false otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   180
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   181
	public function __call( $name, $arguments ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
		if ( in_array( $name, $this->compat_methods ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
			return call_user_func_array( array( $this, $name ), $arguments );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   184
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   186
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   187
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   188
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   189
	 * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   190
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   191
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   192
	 * @access public
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
	 * @see WP_oEmbed::discover()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
	 * @param string        $url  The URL to the content.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   197
	 * @param string|array  $args Optional provider arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   198
	 * @return bool|string False on failure, otherwise the oEmbed provider URL.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   200
	public function get_provider( $url, $args = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
		$provider = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
		if ( !isset($args['discover']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
			$args['discover'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		foreach ( $this->providers as $matchmask => $data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
			list( $providerurl, $regex ) = $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
			// Turn the asterisk-type provider URLs into regex
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
			if ( !$regex ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
				$matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
				$matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
			if ( preg_match( $matchmask, $url ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
				$provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
		if ( !$provider && $args['discover'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
			$provider = $this->discover( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
		return $provider;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
	 * Add an oEmbed provider just-in-time when wp_oembed_add_provider() is called
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
	 * before the 'plugins_loaded' hook.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   231
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   232
	 * The just-in-time addition is for the benefit of the 'oembed_providers' filter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   233
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   234
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   235
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   236
	 * @static
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   237
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   238
	 * @see wp_oembed_add_provider()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   239
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   240
	 * @param string $format   Format of URL that this provider can handle. You can use
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   241
	 *                         asterisks as wildcards.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   242
	 * @param string $provider The URL to the oEmbed provider..
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
	 * @param bool   $regex    Optional. Whether the $format parameter is in a regex format.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
	 *                         Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   246
	public static function _add_provider_early( $format, $provider, $regex = false ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   247
		if ( empty( self::$early_providers['add'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   248
			self::$early_providers['add'] = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   251
		self::$early_providers['add'][ $format ] = array( $provider, $regex );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   253
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   255
	 * Remove an oEmbed provider just-in-time when wp_oembed_remove_provider() is called
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   256
	 * before the 'plugins_loaded' hook.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
	 * The just-in-time removal is for the benefit of the 'oembed_providers' filter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   259
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   260
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
	 * @static
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
	 * @see wp_oembed_remove_provider()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   265
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   266
	 * @param string $format The format of URL that this provider can handle. You can use
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   267
	 *                       asterisks as wildcards.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   268
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   269
	public static function _remove_provider_early( $format ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
		if ( empty( self::$early_providers['remove'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   271
			self::$early_providers['remove'] = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   273
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
		self::$early_providers['remove'][] = $format;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
	 * The do-it-all function that takes a URL and attempts to return the HTML.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
	 * @see WP_oEmbed::fetch()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
	 * @see WP_oEmbed::data2html()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
	 * @param string $url The URL to the content that should be attempted to be embedded.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
	 * @param array $args Optional arguments. Usually passed from a shortcode.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   285
	 * @return false|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   287
	public function get_html( $url, $args = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
		$provider = $this->get_provider( $url, $args );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
		if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
		 * Filter the HTML returned by the oEmbed provider.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
		 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
		 * @param string $data The returned oEmbed HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
		 * @param string $url  URL of the content to be embedded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
		 * @param array  $args Optional arguments, usually passed from a shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
		return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   306
	 * Attempts to discover link tags at the given URL for an oEmbed provider.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
	 * @param string $url The URL that should be inspected for discovery `<link>` tags.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
	 * @return bool|string False on failure, otherwise the oEmbed provider URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
	public function discover( $url ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
		$providers = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
		 * Filter oEmbed remote get arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
		 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   318
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   319
		 * @see WP_Http::request()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   320
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   321
		 * @param array  $args oEmbed remote get arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   322
		 * @param string $url  URL to be inspected.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   324
		$args = apply_filters( 'oembed_remote_get_args', array(), $url );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   325
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
		// Fetch URL content
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   327
		$request = wp_safe_remote_get( $url, $args );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   328
		if ( $html = wp_remote_retrieve_body( $request ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
			/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
			 * Filter the link types that contain oEmbed provider URLs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
			 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
			 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
			 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
			 * @param array $format Array of oEmbed link types. Accepts 'application/json+oembed',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
			 *                      'text/xml+oembed', and 'application/xml+oembed' (incorrect,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
			 *                      used by at least Vimeo).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
			 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
			$linktypes = apply_filters( 'oembed_linktypes', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
				'application/json+oembed' => 'json',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
				'text/xml+oembed' => 'xml',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
				'application/xml+oembed' => 'xml',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
			) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
			// Strip <body>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
			$html = substr( $html, 0, stripos( $html, '</head>' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
			// Do a quick check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
			$tagfound = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
			foreach ( $linktypes as $linktype => $format ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
				if ( stripos($html, $linktype) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
					$tagfound = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
			if ( $tagfound && preg_match_all( '#<link([^<>]+)/?>#iU', $html, $links ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
				foreach ( $links[1] as $link ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
					$atts = shortcode_parse_atts( $link );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
					if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
						$providers[$linktypes[$atts['type']]] = htmlspecialchars_decode( $atts['href'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
						// Stop here if it's JSON (that's all we need)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
						if ( 'json' == $linktypes[$atts['type']] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
							break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
		// JSON is preferred to XML
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
		if ( !empty($providers['json']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
			return $providers['json'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
		elseif ( !empty($providers['xml']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
			return $providers['xml'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
	 * Connects to a oEmbed provider and returns the result.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
	 * @param string $provider The URL to the oEmbed provider.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
	 * @param string $url The URL to the content that is desired to be embedded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
	 * @param array $args Optional arguments. Usually passed from a shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	 * @return bool|object False on failure, otherwise the result in the form of an object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   389
	public function fetch( $provider, $url, $args = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   390
		$args = wp_parse_args( $args, wp_embed_defaults( $url ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		$provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
		$provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
		$provider = add_query_arg( 'url', urlencode($url), $provider );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
		 * Filter the oEmbed URL to be fetched.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
		 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
		 * @param string $provider URL of the oEmbed provider.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
		 * @param string $url      URL of the content to be embedded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
		 * @param array  $args     Optional arguments, usually passed from a shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
		$provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
		foreach( array( 'json', 'xml' ) as $format ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
			$result = $this->_fetch_with_format( $provider, $format );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
			if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
			return ( $result && ! is_wp_error( $result ) ) ? $result : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
	 * Fetches result from an oEmbed provider for a specific format and complete provider URL
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
	 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
	 * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
	 * @param string $format Format to use
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
	 * @return bool|object False on failure, otherwise the result in the form of an object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   425
	private function _fetch_with_format( $provider_url_with_args, $format ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
		$provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   427
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   428
		/** This filter is documented in wp-includes/class-oembed.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   429
		$args = apply_filters( 'oembed_remote_get_args', array(), $provider_url_with_args );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
		$response = wp_safe_remote_get( $provider_url_with_args, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
		if ( 501 == wp_remote_retrieve_response_code( $response ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
			return new WP_Error( 'not-implemented' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
		if ( ! $body = wp_remote_retrieve_body( $response ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
		$parse_method = "_parse_$format";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
		return $this->$parse_method( $body );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
	 * Parses a json response body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
	 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   446
	private function _parse_json( $response_body ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
		return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
	 * Parses an XML response body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
	 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   456
	private function _parse_xml( $response_body ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
		if ( ! function_exists( 'libxml_disable_entity_loader' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
		$loader = libxml_disable_entity_loader( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
		$errors = libxml_use_internal_errors( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
		$return = $this->_parse_xml_body( $response_body );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		libxml_use_internal_errors( $errors );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
		libxml_disable_entity_loader( $loader );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
	 * Helper function for parsing an XML response body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	private function _parse_xml_body( $response_body ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
		if ( ! function_exists( 'simplexml_import_dom' ) || ! class_exists( 'DOMDocument' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
		$dom = new DOMDocument;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
		$success = $dom->loadXML( $response_body );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
		if ( ! $success )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
		if ( isset( $dom->doctype ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
		foreach ( $dom->childNodes as $child ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
			if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
		$xml = simplexml_import_dom( $dom );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
		if ( ! $xml )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
		$return = new stdClass;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
		foreach ( $xml as $key => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
			$return->$key = (string) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
	 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
	 * @param object $data A data object result from an oEmbed provider.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
	 * @param string $url The URL to the content that is desired to be embedded.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   511
	 * @return false|string False on error, otherwise the HTML needed to embed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   513
	public function data2html( $data, $url ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
		if ( ! is_object( $data ) || empty( $data->type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
		$return = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
		switch ( $data->type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
			case 'photo':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
				if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
				if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
				$title = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
				$return = '<a href="' . esc_url( $url ) . '"><img src="' . esc_url( $data->url ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" /></a>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
			case 'video':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
			case 'rich':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
				if ( ! empty( $data->html ) && is_string( $data->html ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
					$return = $data->html;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
			case 'link':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
				if ( ! empty( $data->title ) && is_string( $data->title ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
					$return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
			default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
				$return = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
		 * Filter the returned oEmbed HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
		 * Use this filter to add support for custom data types, or to filter the result.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
		 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
		 * @param string $return The returned oEmbed HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
		 * @param object $data   A data object result from an oEmbed provider.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
		 * @param string $url    The URL of the content to be embedded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
		return apply_filters( 'oembed_dataparse', $return, $data, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
	 * Strip any new lines from the HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   562
	 * @access public
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
	 * @param string $html Existing HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
	 * @param object $data Data object from WP_oEmbed::data2html()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
	 * @param string $url The original URL passed to oEmbed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
	 * @return string Possibly modified $html
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   568
	public function _strip_newlines( $html, $data, $url ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   569
		if ( false === strpos( $html, "\n" ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   570
			return $html;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   571
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   572
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   573
		$count = 1;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   574
		$found = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   575
		$token = '__PRE__';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   576
		$search = array( "\t", "\n", "\r", ' ' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   577
		$replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   578
		$tokenized = str_replace( $search, $replace, $html );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   580
		preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   581
		foreach ( $matches as $i => $match ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   582
			$tag_html = str_replace( $replace, $search, $match[0] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   583
			$tag_token = $token . $i;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   584
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   585
			$found[ $tag_token ] = $tag_html;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   586
			$html = str_replace( $tag_html, $tag_token, $html, $count );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   587
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   588
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   589
		$replaced = str_replace( $replace, $search, $html );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   590
		$stripped = str_replace( array( "\r\n", "\n" ), '', $replaced );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   591
		$pre = array_values( $found );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   592
		$tokens = array_keys( $found );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   593
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   594
		return str_replace( $tokens, $pre, $stripped );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
 * Returns the initialized {@link WP_oEmbed} object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
 * @see WP_oEmbed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
 * @return WP_oEmbed object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
function _wp_oembed_get_object() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
	static $wp_oembed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
	if ( is_null($wp_oembed) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
		$wp_oembed = new WP_oEmbed();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
	return $wp_oembed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
}