From 7139817d2e12b4c7f1455a5165b138323a64e01f Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 26 Jun 2014 02:29:20 +0000 Subject: [PATCH] When `wp_oembed_add_provider()` or `wp_oembed_remove_provider()` is called before the `plugins_loaded` hook has, store the values statically on the `WP_oEmbed` object and add them just-in-time when the object is instantiated. This ensures that all plugins have an accurate provider list when `apply_filters( 'oembed_providers', $providers )` is called. Props kovshenin. Fixes #28284. git-svn-id: https://develop.svn.wordpress.org/trunk@28846 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-oembed.php | 31 +++++++++++++++++++++++++++++++ src/wp-includes/media.php | 21 +++++++++++++++------ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-oembed.php b/src/wp-includes/class-oembed.php index 2844bd1f1f..363b7c9933 100644 --- a/src/wp-includes/class-oembed.php +++ b/src/wp-includes/class-oembed.php @@ -19,6 +19,7 @@ */ class WP_oEmbed { public $providers = array(); + public static $early_providers = array(); /** * Constructor @@ -65,6 +66,20 @@ class WP_oEmbed { '#https?://(www\.)?(animoto|video214)\.com/play/.*#i' => array( 'http://animoto.com/oembeds/create', true ), ); + if ( ! empty( self::$early_providers['add'] ) ) { + foreach ( self::$early_providers['add'] as $format => $data ) { + $providers[ $format ] = $data; + } + } + + if ( ! empty( self::$early_providers['remove'] ) ) { + foreach ( self::$early_providers['remove'] as $format ) { + unset( $providers[ $format ] ); + } + } + + self::$early_providers = array(); + /** * Filter the list of oEmbed providers. * @@ -133,6 +148,22 @@ class WP_oEmbed { return $provider; } + public static function _add_provider_early( $format, $provider, $regex = false ) { + if ( empty( self::$early_providers['add'] ) ) { + self::$early_providers['add'] = array(); + } + + self::$early_providers['add'][ $format ] = array( $provider, $regex ); + } + + public static function _remove_provider_early( $format ) { + if ( empty( self::$early_providers['remove'] ) ) { + self::$early_providers['remove'] = array(); + } + + self::$early_providers['remove'][] = $format; + } + /** * The do-it-all function that takes a URL and attempts to return the HTML. * diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 34ea96902f..3581b32e68 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -2135,8 +2135,13 @@ function wp_oembed_get( $url, $args = '' ) { */ function wp_oembed_add_provider( $format, $provider, $regex = false ) { require_once( ABSPATH . WPINC . '/class-oembed.php' ); - $oembed = _wp_oembed_get_object(); - $oembed->providers[$format] = array( $provider, $regex ); + + if ( did_action( 'plugins_loaded' ) ) { + $oembed = _wp_oembed_get_object(); + $oembed->providers[$format] = array( $provider, $regex ); + } else { + WP_oEmbed::_add_provider_early( $format, $provider, $regex ); + } } /** @@ -2152,11 +2157,15 @@ function wp_oembed_add_provider( $format, $provider, $regex = false ) { function wp_oembed_remove_provider( $format ) { require_once( ABSPATH . WPINC . '/class-oembed.php' ); - $oembed = _wp_oembed_get_object(); + if ( did_action( 'plugins_loaded' ) ) { + $oembed = _wp_oembed_get_object(); - if ( isset( $oembed->providers[ $format ] ) ) { - unset( $oembed->providers[ $format ] ); - return true; + if ( isset( $oembed->providers[ $format ] ) ) { + unset( $oembed->providers[ $format ] ); + return true; + } + } else { + WP_oEmbed::_remove_provider_early( $format ); } return false;