wp/wp-includes/class-wp-embed.php
author ymh <ymh.work@gmail.com>
Wed, 06 Nov 2013 03:21:17 +0000
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
permissions -rw-r--r--
first import
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 easily embedding rich media such as videos and images into content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Embed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
class WP_Embed {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
	var $handlers = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
	var $post_ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
	var $usecache = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
	var $linkifunknown = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	 * Constructor
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	function __construct() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
		// Hack to get the [embed] shortcode to run before wpautop()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
		add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
		// Shortcode placeholder for strip_shortcodes()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
		add_shortcode( 'embed', '__return_false' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		// Attempts to embed all URLs in a post
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
		add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
		// When a post is saved, invalidate the oEmbed cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
		add_action( 'pre_post_update', array( $this, 'delete_oembed_caches' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
		// After a post is saved, cache oEmbed items via AJAX
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	 * Process the [embed] shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
	 * Since the [embed] shortcode needs to be run earlier than other shortcodes,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
	 * this function removes all existing shortcodes, registers the [embed] shortcode,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	 * calls {@link do_shortcode()}, and then re-registers the old shortcodes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	 * @uses $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	 * @uses remove_all_shortcodes()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	 * @uses add_shortcode()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
	 * @uses do_shortcode()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	 * @param string $content Content to parse
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
	 * @return string Content with shortcode parsed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
	function run_shortcode( $content ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
		global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		// Back up current registered shortcodes and clear them all out
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		$orig_shortcode_tags = $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		remove_all_shortcodes();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		add_shortcode( 'embed', array( $this, 'shortcode' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		// Do the shortcode (only the [embed] one is registered)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		$content = do_shortcode( $content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		// Put the original shortcodes back
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
		$shortcode_tags = $orig_shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		return $content;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	 * If a post/page was saved, then output JavaScript to make
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	 * an AJAX request that will call WP_Embed::cache_oembed().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	function maybe_run_ajax_cache() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
		$post = get_post();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		if ( ! $post || empty($_GET['message']) || 1 != $_GET['message'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
<script type="text/javascript">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
/* <![CDATA[ */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
	jQuery(document).ready(function($){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		$.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post->ID, 'relative' ); ?>");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
/* ]]> */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
</script>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
	 * Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	 * This function should probably also only be used for sites that do not support oEmbed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	 * @param string $id An internal ID/name for the handler. Needs to be unique.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
	 * @param string $regex The regex that will be used to see if this handler should be used for a URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
	 * @param callback $callback The callback function that will be called if the regex is matched.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
	 * @param int $priority Optional. Used to specify the order in which the registered handlers will be tested (default: 10). Lower numbers correspond with earlier testing, and handlers with the same priority are tested in the order in which they were added to the action.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	function register_handler( $id, $regex, $callback, $priority = 10 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		$this->handlers[$priority][$id] = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			'regex'    => $regex,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
			'callback' => $callback,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
	 * Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	 * @param string $id The handler ID that should be removed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
	 * @param int $priority Optional. The priority of the handler to be removed (default: 10).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	function unregister_handler( $id, $priority = 10 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		if ( isset($this->handlers[$priority][$id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
			unset($this->handlers[$priority][$id]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
	 * The {@link do_shortcode()} callback function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	 * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
	 * If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	 * @uses wp_oembed_get()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	 * @uses wp_parse_args()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
	 * @uses wp_embed_defaults()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
	 * @uses WP_Embed::maybe_make_link()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
	 * @uses get_option()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	 * @uses author_can()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	 * @uses wp_cache_get()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	 * @uses wp_cache_set()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
	 * @uses get_post_meta()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	 * @uses update_post_meta()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
	 * @param array $attr Shortcode attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
	 * @param string $url The URL attempting to be embedded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
	 * @return string The embed HTML on success, otherwise the original URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
	function shortcode( $attr, $url = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		$post = get_post();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
		if ( empty( $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
			return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		$rawattr = $attr;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
		$attr = wp_parse_args( $attr, wp_embed_defaults() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
		// kses converts & into &amp; and we need to undo this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		// See http://core.trac.wordpress.org/ticket/11311
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		$url = str_replace( '&amp;', '&', $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		// Look for known internal handlers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		ksort( $this->handlers );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
		foreach ( $this->handlers as $priority => $handlers ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
			foreach ( $handlers as $id => $handler ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
				if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
					if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
						/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
						 * Filter the returned embed handler.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
						 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
						 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
						 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
						 * @param mixed  $return The shortcode callback function to call.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
						 * @param string $url    The attempted embed URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
						 * @param array  $attr   An array of shortcode attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
						 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
						return apply_filters( 'embed_handler_html', $return, $url, $attr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
			$post_ID = $this->post_ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		// Unknown URL format. Let oEmbed have a go.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		if ( $post_ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
			// Check for a cached result (stored in the post meta)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
			$cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
			if ( $this->usecache ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
				$cache = get_post_meta( $post_ID, $cachekey, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
				// Failures are cached
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
				if ( '{{unknown}}' === $cache )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
					return $this->maybe_make_link( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
				if ( ! empty( $cache ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
					/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
					 * Filter the cached oEmbed HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
					 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
					 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
					 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
					 * @param mixed  $cache   The cached HTML result, stored in post meta.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
					 * @param string $url     The attempted embed URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
					 * @param array  $attr    An array of shortcode attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
					 * @param int    $post_ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
					 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
					return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
			/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
			 * Filter whether to inspect the given URL for discoverable <link> tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
			 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
			 * @see WP_oEmbed::discover()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
			 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
			 * @param bool false Whether to enable <link> tag discovery. Default false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
			 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
			$attr['discover'] = ( apply_filters( 'embed_oembed_discover', false ) && author_can( $post_ID, 'unfiltered_html' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
			// Use oEmbed to get the HTML
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
			$html = wp_oembed_get( $url, $attr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
			// Cache the result
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
			$cache = ( $html ) ? $html : '{{unknown}}';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
			update_post_meta( $post_ID, $cachekey, $cache );
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 there was a result, return it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
			if ( $html ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
				/** This filter is documented in wp-includes/class-wp-embed.php */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
				return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
		// Still unknown
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
		return $this->maybe_make_link( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
	 * Delete all oEmbed caches.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	 * @param int $post_ID Post ID to delete the caches for.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
	function delete_oembed_caches( $post_ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		$post_metas = get_post_custom_keys( $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		if ( empty($post_metas) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
		foreach( $post_metas as $post_meta_key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
			if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
				delete_post_meta( $post_ID, $post_meta_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
	 * Triggers a caching of all oEmbed results.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
	 * @param int $post_ID Post ID to do the caching for.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
	function cache_oembed( $post_ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
		$post = get_post( $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
		$post_types = array( 'post', 'page' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		 * Filter the array of post types to cache oEmbed results for.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
		 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
		 * @param array $post_types Array of post types to cache oEmbed results for. Default 'post', 'page'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', $post_types ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
		// Trigger a caching
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
		if ( !empty($post->post_content) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
			$this->post_ID = $post->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
			$this->usecache = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
			$content = $this->run_shortcode( $post->post_content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
			$this->autoembed( $content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
			$this->usecache = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
	 * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
	 * @uses WP_Embed::autoembed_callback()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
	 * @param string $content The content to be searched.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
	 * @return string Potentially modified $content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
	function autoembed( $content ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
		return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
	 * Callback function for {@link WP_Embed::autoembed()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
	 * @uses WP_Embed::shortcode()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	 * @param array $match A regex match array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
	 * @return string The embed HTML on success, otherwise the original URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
	function autoembed_callback( $match ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
		$oldval = $this->linkifunknown;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
		$this->linkifunknown = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
		$return = $this->shortcode( array(), $match[1] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
		$this->linkifunknown = $oldval;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
		return "\n$return\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
	 * Conditionally makes a hyperlink based on an internal class variable.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
	 * @param string $url URL to potentially be linked.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
	 * @return string Linked URL or the original URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
	function maybe_make_link( $url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
		$output = ( $this->linkifunknown ) ? '<a href="' . esc_url($url) . '">' . esc_html($url) . '</a>' : $url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
		 * Filter the returned, maybe-linked embed URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
		 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
		 * @param string $output The linked or original URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
		 * @param string $url    The original URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
		return apply_filters( 'embed_maybe_make_link', $output, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
$GLOBALS['wp_embed'] = new WP_Embed();