diff --git a/src/wp-includes/embed-functions.php b/src/wp-includes/embed-functions.php new file mode 100644 index 0000000000..2fc255549b --- /dev/null +++ b/src/wp-includes/embed-functions.php @@ -0,0 +1,328 @@ +register_handler( $id, $regex, $callback, $priority ); +} + +/** + * Unregisters a previously-registered embed handler. + * + * @since 2.9.0 + * + * @global WP_Embed $wp_embed + * + * @param string $id The handler ID that should be removed. + * @param int $priority Optional. The priority of the handler to be removed. Default 10. + */ +function wp_embed_unregister_handler( $id, $priority = 10 ) { + global $wp_embed; + $wp_embed->unregister_handler( $id, $priority ); +} + +/** + * Create default array of embed parameters. + * + * The width defaults to the content width as specified by the theme. If the + * theme does not specify a content width, then 500px is used. + * + * The default height is 1.5 times the width, or 1000px, whichever is smaller. + * + * The 'embed_defaults' filter can be used to adjust either of these values. + * + * @since 2.9.0 + * + * @global int $content_width + * + * @param string $url Optional. The URL that should be embedded. Default empty. + * + * @return array Default embed parameters. + */ +function wp_embed_defaults( $url = '' ) { + if ( ! empty( $GLOBALS['content_width'] ) ) + $width = (int) $GLOBALS['content_width']; + + if ( empty( $width ) ) + $width = 500; + + $height = min( ceil( $width * 1.5 ), 1000 ); + + /** + * Filter the default array of embed dimensions. + * + * @since 2.9.0 + * + * @param int $width Width of the embed in pixels. + * @param int $height Height of the embed in pixels. + * @param string $url The URL that should be embedded. + */ + return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url ); +} + +/** + * Attempts to fetch the embed HTML for a provided URL using oEmbed. + * + * @since 2.9.0 + * + * @see WP_oEmbed + * + * @param string $url The URL that should be embedded. + * @param array $args Optional. Additional arguments and parameters for retrieving embed HTML. + * Default empty. + * @return false|string False on failure or the embed HTML on success. + */ +function wp_oembed_get( $url, $args = '' ) { + require_once( ABSPATH . WPINC . '/class-oembed.php' ); + $oembed = _wp_oembed_get_object(); + return $oembed->get_html( $url, $args ); +} + +/** + * Adds a URL format and oEmbed provider URL pair. + * + * @since 2.9.0 + * + * @see WP_oEmbed + * + * @param string $format The format of URL that this provider can handle. You can use asterisks + * as wildcards. + * @param string $provider The URL to the oEmbed provider. + * @param boolean $regex Optional. Whether the `$format` parameter is in a RegEx format. Default false. + */ +function wp_oembed_add_provider( $format, $provider, $regex = false ) { + require_once( ABSPATH . WPINC . '/class-oembed.php' ); + + 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 ); + } +} + +/** + * Removes an oEmbed provider. + * + * @since 3.5.0 + * + * @see WP_oEmbed + * + * @param string $format The URL format for the oEmbed provider to remove. + * @return bool Was the provider removed successfully? + */ +function wp_oembed_remove_provider( $format ) { + require_once( ABSPATH . WPINC . '/class-oembed.php' ); + + if ( did_action( 'plugins_loaded' ) ) { + $oembed = _wp_oembed_get_object(); + + if ( isset( $oembed->providers[ $format ] ) ) { + unset( $oembed->providers[ $format ] ); + return true; + } + } else { + WP_oEmbed::_remove_provider_early( $format ); + } + + return false; +} + +/** + * Determines if default embed handlers should be loaded. + * + * Checks to make sure that the embeds library hasn't already been loaded. If + * it hasn't, then it will load the embeds library. + * + * @since 2.9.0 + * + * @see wp_embed_register_handler() + */ +function wp_maybe_load_embeds() { + /** + * Filter whether to load the default embed handlers. + * + * Returning a falsey value will prevent loading the default embed handlers. + * + * @since 2.9.0 + * + * @param bool $maybe_load_embeds Whether to load the embeds library. Default true. + */ + if ( ! apply_filters( 'load_default_embeds', true ) ) { + return; + } + + wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' ); + + wp_embed_register_handler( 'googlevideo', '#http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)#i', 'wp_embed_handler_googlevideo' ); + + /** + * Filter the audio embed handler callback. + * + * @since 3.6.0 + * + * @param callable $handler Audio embed handler callback function. + */ + wp_embed_register_handler( 'audio', '#^https?://.+?\.(' . join( '|', wp_get_audio_extensions() ) . ')$#i', apply_filters( 'wp_audio_embed_handler', 'wp_embed_handler_audio' ), 9999 ); + + /** + * Filter the video embed handler callback. + * + * @since 3.6.0 + * + * @param callable $handler Video embed handler callback function. + */ + wp_embed_register_handler( 'video', '#^https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')$#i', apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' ), 9999 ); +} + +/** + * The Google Video embed handler callback. + * + * Google Video does not support oEmbed. + * + * @see WP_Embed::register_handler() + * @see WP_Embed::shortcode() + * + * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler(). + * @param array $attr Embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + * @return string The embed HTML. + */ +function wp_embed_handler_googlevideo( $matches, $attr, $url, $rawattr ) { + // If the user supplied a fixed width AND height, use it + if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) { + $width = (int) $rawattr['width']; + $height = (int) $rawattr['height']; + } else { + list( $width, $height ) = wp_expand_dimensions( 425, 344, $attr['width'], $attr['height'] ); + } + + /** + * Filter the Google Video embed output. + * + * @since 2.9.0 + * + * @param string $html Google Video HTML embed markup. + * @param array $matches The RegEx matches from the provided regex. + * @param array $attr An array of embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + */ + return apply_filters( 'embed_googlevideo', '', $matches, $attr, $url, $rawattr ); +} + +/** + * YouTube iframe embed handler callback. + * + * Catches YouTube iframe embed URLs that are not parsable by oEmbed but can be translated into a URL that is. + * + * @since 4.0.0 + * + * @global WP_Embed $wp_embed + * + * @param array $matches The RegEx matches from the provided regex when calling + * wp_embed_register_handler(). + * @param array $attr Embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + * @return string The embed HTML. + */ +function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) { + global $wp_embed; + $embed = $wp_embed->autoembed( "https://youtube.com/watch?v={$matches[2]}" ); + + /** + * Filter the YoutTube embed output. + * + * @since 4.0.0 + * + * @see wp_embed_handler_youtube() + * + * @param string $embed YouTube embed output. + * @param array $attr An array of embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + */ + return apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr ); +} + +/** + * Audio embed handler callback. + * + * @since 3.6.0 + * + * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler(). + * @param array $attr Embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + * @return string The embed HTML. + */ +function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) { + $audio = sprintf( '[audio src="%s" /]', esc_url( $url ) ); + + /** + * Filter the audio embed output. + * + * @since 3.6.0 + * + * @param string $audio Audio embed output. + * @param array $attr An array of embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + */ + return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr ); +} + +/** + * Video embed handler callback. + * + * @since 3.6.0 + * + * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler(). + * @param array $attr Embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + * @return string The embed HTML. + */ +function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) { + $dimensions = ''; + if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) { + $dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] ); + $dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] ); + } + $video = sprintf( '[video %s src="%s" /]', $dimensions, esc_url( $url ) ); + + /** + * Filter the video embed output. + * + * @since 3.6.0 + * + * @param string $video Video embed output. + * @param array $attr An array of embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + */ + return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr ); +} diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 44e18f4a20..53503e92a5 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -2176,80 +2176,6 @@ function wp_imagecreatetruecolor($width, $height) { return $img; } -/** - * Registers an embed handler. - * - * Should probably only be used for sites that do not support oEmbed. - * - * @since 2.9.0 - * - * @global WP_Embed $wp_embed - * - * @param string $id An internal ID/name for the handler. Needs to be unique. - * @param string $regex The regex that will be used to see if this handler should be used for a URL. - * @param callable $callback The callback function that will be called if the regex is matched. - * @param int $priority Optional. Used to specify the order in which the registered handlers will - * be tested. Default 10. - */ -function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) { - global $wp_embed; - $wp_embed->register_handler( $id, $regex, $callback, $priority ); -} - -/** - * Unregisters a previously-registered embed handler. - * - * @since 2.9.0 - * - * @global WP_Embed $wp_embed - * - * @param string $id The handler ID that should be removed. - * @param int $priority Optional. The priority of the handler to be removed. Default 10. - */ -function wp_embed_unregister_handler( $id, $priority = 10 ) { - global $wp_embed; - $wp_embed->unregister_handler( $id, $priority ); -} - -/** - * Create default array of embed parameters. - * - * The width defaults to the content width as specified by the theme. If the - * theme does not specify a content width, then 500px is used. - * - * The default height is 1.5 times the width, or 1000px, whichever is smaller. - * - * The 'embed_defaults' filter can be used to adjust either of these values. - * - * @since 2.9.0 - * - * @global int $content_width - * - * @param string $url Optional. The URL that should be embedded. Default empty. - * - * @return array Default embed parameters. - */ -function wp_embed_defaults( $url = '' ) { - if ( ! empty( $GLOBALS['content_width'] ) ) - $width = (int) $GLOBALS['content_width']; - - if ( empty( $width ) ) - $width = 500; - - $height = min( ceil( $width * 1.5 ), 1000 ); - - /** - * Filter the default array of embed dimensions. - * - * @since 2.9.0 - * - * @param int $width Width of the embed in pixels. - * @param int $height Height of the embed in pixels. - * @param string $url The URL that should be embedded. - */ - return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url ); -} - /** * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. * @@ -2272,252 +2198,6 @@ function wp_expand_dimensions( $example_width, $example_height, $max_width, $max return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height ); } -/** - * Attempts to fetch the embed HTML for a provided URL using oEmbed. - * - * @since 2.9.0 - * - * @see WP_oEmbed - * - * @param string $url The URL that should be embedded. - * @param array $args Optional. Additional arguments and parameters for retrieving embed HTML. - * Default empty. - * @return false|string False on failure or the embed HTML on success. - */ -function wp_oembed_get( $url, $args = '' ) { - require_once( ABSPATH . WPINC . '/class-oembed.php' ); - $oembed = _wp_oembed_get_object(); - return $oembed->get_html( $url, $args ); -} - -/** - * Adds a URL format and oEmbed provider URL pair. - * - * @since 2.9.0 - * - * @see WP_oEmbed - * - * @param string $format The format of URL that this provider can handle. You can use asterisks - * as wildcards. - * @param string $provider The URL to the oEmbed provider. - * @param boolean $regex Optional. Whether the `$format` parameter is in a RegEx format. Default false. - */ -function wp_oembed_add_provider( $format, $provider, $regex = false ) { - require_once( ABSPATH . WPINC . '/class-oembed.php' ); - - 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 ); - } -} - -/** - * Removes an oEmbed provider. - * - * @since 3.5.0 - * - * @see WP_oEmbed - * - * @param string $format The URL format for the oEmbed provider to remove. - * @return bool Was the provider removed successfully? - */ -function wp_oembed_remove_provider( $format ) { - require_once( ABSPATH . WPINC . '/class-oembed.php' ); - - if ( did_action( 'plugins_loaded' ) ) { - $oembed = _wp_oembed_get_object(); - - if ( isset( $oembed->providers[ $format ] ) ) { - unset( $oembed->providers[ $format ] ); - return true; - } - } else { - WP_oEmbed::_remove_provider_early( $format ); - } - - return false; -} - -/** - * Determines if default embed handlers should be loaded. - * - * Checks to make sure that the embeds library hasn't already been loaded. If - * it hasn't, then it will load the embeds library. - * - * @since 2.9.0 - * - * @see wp_embed_register_handler() - */ -function wp_maybe_load_embeds() { - /** - * Filter whether to load the default embed handlers. - * - * Returning a falsey value will prevent loading the default embed handlers. - * - * @since 2.9.0 - * - * @param bool $maybe_load_embeds Whether to load the embeds library. Default true. - */ - if ( ! apply_filters( 'load_default_embeds', true ) ) { - return; - } - - wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' ); - - wp_embed_register_handler( 'googlevideo', '#http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)#i', 'wp_embed_handler_googlevideo' ); - - /** - * Filter the audio embed handler callback. - * - * @since 3.6.0 - * - * @param callable $handler Audio embed handler callback function. - */ - wp_embed_register_handler( 'audio', '#^https?://.+?\.(' . join( '|', wp_get_audio_extensions() ) . ')$#i', apply_filters( 'wp_audio_embed_handler', 'wp_embed_handler_audio' ), 9999 ); - - /** - * Filter the video embed handler callback. - * - * @since 3.6.0 - * - * @param callable $handler Video embed handler callback function. - */ - wp_embed_register_handler( 'video', '#^https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')$#i', apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' ), 9999 ); -} - -/** - * The Google Video embed handler callback. - * - * Google Video does not support oEmbed. - * - * @see WP_Embed::register_handler() - * @see WP_Embed::shortcode() - * - * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler(). - * @param array $attr Embed attributes. - * @param string $url The original URL that was matched by the regex. - * @param array $rawattr The original unmodified attributes. - * @return string The embed HTML. - */ -function wp_embed_handler_googlevideo( $matches, $attr, $url, $rawattr ) { - // If the user supplied a fixed width AND height, use it - if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) { - $width = (int) $rawattr['width']; - $height = (int) $rawattr['height']; - } else { - list( $width, $height ) = wp_expand_dimensions( 425, 344, $attr['width'], $attr['height'] ); - } - - /** - * Filter the Google Video embed output. - * - * @since 2.9.0 - * - * @param string $html Google Video HTML embed markup. - * @param array $matches The RegEx matches from the provided regex. - * @param array $attr An array of embed attributes. - * @param string $url The original URL that was matched by the regex. - * @param array $rawattr The original unmodified attributes. - */ - return apply_filters( 'embed_googlevideo', '', $matches, $attr, $url, $rawattr ); -} - -/** - * YouTube iframe embed handler callback. - * - * Catches YouTube iframe embed URLs that are not parsable by oEmbed but can be translated into a URL that is. - * - * @since 4.0.0 - * - * @global WP_Embed $wp_embed - * - * @param array $matches The RegEx matches from the provided regex when calling - * wp_embed_register_handler(). - * @param array $attr Embed attributes. - * @param string $url The original URL that was matched by the regex. - * @param array $rawattr The original unmodified attributes. - * @return string The embed HTML. - */ -function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) { - global $wp_embed; - $embed = $wp_embed->autoembed( "https://youtube.com/watch?v={$matches[2]}" ); - - /** - * Filter the YoutTube embed output. - * - * @since 4.0.0 - * - * @see wp_embed_handler_youtube() - * - * @param string $embed YouTube embed output. - * @param array $attr An array of embed attributes. - * @param string $url The original URL that was matched by the regex. - * @param array $rawattr The original unmodified attributes. - */ - return apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr ); -} - -/** - * Audio embed handler callback. - * - * @since 3.6.0 - * - * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler(). - * @param array $attr Embed attributes. - * @param string $url The original URL that was matched by the regex. - * @param array $rawattr The original unmodified attributes. - * @return string The embed HTML. - */ -function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) { - $audio = sprintf( '[audio src="%s" /]', esc_url( $url ) ); - - /** - * Filter the audio embed output. - * - * @since 3.6.0 - * - * @param string $audio Audio embed output. - * @param array $attr An array of embed attributes. - * @param string $url The original URL that was matched by the regex. - * @param array $rawattr The original unmodified attributes. - */ - return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr ); -} - -/** - * Video embed handler callback. - * - * @since 3.6.0 - * - * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler(). - * @param array $attr Embed attributes. - * @param string $url The original URL that was matched by the regex. - * @param array $rawattr The original unmodified attributes. - * @return string The embed HTML. - */ -function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) { - $dimensions = ''; - if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) { - $dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] ); - $dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] ); - } - $video = sprintf( '[video %s src="%s" /]', $dimensions, esc_url( $url ) ); - - /** - * Filter the video embed output. - * - * @since 3.6.0 - * - * @param string $video Video embed output. - * @param array $attr An array of embed attributes. - * @param string $url The original URL that was matched by the regex. - * @param array $rawattr The original unmodified attributes. - */ - return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr ); -} - /** * Converts a shorthand byte value to an integer byte value. *