Make WordPress Core


Ignore:
Timestamp:
10/06/2015 03:58:42 AM (9 years ago)
Author:
wonderboymusic
Message:

Embeds: move some functions from media.php to a new file, embed-functions.php, via svn cp

See #32522.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r34657 r34851  
    21782178
    21792179/**
    2180  * Registers an embed handler.
    2181  *
    2182  * Should probably only be used for sites that do not support oEmbed.
    2183  *
    2184  * @since 2.9.0
    2185  *
    2186  * @global WP_Embed $wp_embed
    2187  *
    2188  * @param string   $id       An internal ID/name for the handler. Needs to be unique.
    2189  * @param string   $regex    The regex that will be used to see if this handler should be used for a URL.
    2190  * @param callable $callback The callback function that will be called if the regex is matched.
    2191  * @param int      $priority Optional. Used to specify the order in which the registered handlers will
    2192  *                           be tested. Default 10.
    2193  */
    2194 function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
    2195     global $wp_embed;
    2196     $wp_embed->register_handler( $id, $regex, $callback, $priority );
    2197 }
    2198 
    2199 /**
    2200  * Unregisters a previously-registered embed handler.
    2201  *
    2202  * @since 2.9.0
    2203  *
    2204  * @global WP_Embed $wp_embed
    2205  *
    2206  * @param string $id       The handler ID that should be removed.
    2207  * @param int    $priority Optional. The priority of the handler to be removed. Default 10.
    2208  */
    2209 function wp_embed_unregister_handler( $id, $priority = 10 ) {
    2210     global $wp_embed;
    2211     $wp_embed->unregister_handler( $id, $priority );
    2212 }
    2213 
    2214 /**
    2215  * Create default array of embed parameters.
    2216  *
    2217  * The width defaults to the content width as specified by the theme. If the
    2218  * theme does not specify a content width, then 500px is used.
    2219  *
    2220  * The default height is 1.5 times the width, or 1000px, whichever is smaller.
    2221  *
    2222  * The 'embed_defaults' filter can be used to adjust either of these values.
    2223  *
    2224  * @since 2.9.0
    2225  *
    2226  * @global int $content_width
    2227  *
    2228  * @param string $url Optional. The URL that should be embedded. Default empty.
    2229  *
    2230  * @return array Default embed parameters.
    2231  */
    2232 function wp_embed_defaults( $url = '' ) {
    2233     if ( ! empty( $GLOBALS['content_width'] ) )
    2234         $width = (int) $GLOBALS['content_width'];
    2235 
    2236     if ( empty( $width ) )
    2237         $width = 500;
    2238 
    2239     $height = min( ceil( $width * 1.5 ), 1000 );
    2240 
    2241     /**
    2242      * Filter the default array of embed dimensions.
    2243      *
    2244      * @since 2.9.0
    2245      *
    2246      * @param int    $width  Width of the embed in pixels.
    2247      * @param int    $height Height of the embed in pixels.
    2248      * @param string $url    The URL that should be embedded.
    2249      */
    2250     return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url );
    2251 }
    2252 
    2253 /**
    22542180 * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height.
    22552181 *
     
    22712197
    22722198    return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height );
    2273 }
    2274 
    2275 /**
    2276  * Attempts to fetch the embed HTML for a provided URL using oEmbed.
    2277  *
    2278  * @since 2.9.0
    2279  *
    2280  * @see WP_oEmbed
    2281  *
    2282  * @param string $url  The URL that should be embedded.
    2283  * @param array  $args Optional. Additional arguments and parameters for retrieving embed HTML.
    2284  *                     Default empty.
    2285  * @return false|string False on failure or the embed HTML on success.
    2286  */
    2287 function wp_oembed_get( $url, $args = '' ) {
    2288     require_once( ABSPATH . WPINC . '/class-oembed.php' );
    2289     $oembed = _wp_oembed_get_object();
    2290     return $oembed->get_html( $url, $args );
    2291 }
    2292 
    2293 /**
    2294  * Adds a URL format and oEmbed provider URL pair.
    2295  *
    2296  * @since 2.9.0
    2297  *
    2298  * @see WP_oEmbed
    2299  *
    2300  * @param string  $format   The format of URL that this provider can handle. You can use asterisks
    2301  *                          as wildcards.
    2302  * @param string  $provider The URL to the oEmbed provider.
    2303  * @param boolean $regex    Optional. Whether the `$format` parameter is in a RegEx format. Default false.
    2304  */
    2305 function wp_oembed_add_provider( $format, $provider, $regex = false ) {
    2306     require_once( ABSPATH . WPINC . '/class-oembed.php' );
    2307 
    2308     if ( did_action( 'plugins_loaded' ) ) {
    2309         $oembed = _wp_oembed_get_object();
    2310         $oembed->providers[$format] = array( $provider, $regex );
    2311     } else {
    2312         WP_oEmbed::_add_provider_early( $format, $provider, $regex );
    2313     }
    2314 }
    2315 
    2316 /**
    2317  * Removes an oEmbed provider.
    2318  *
    2319  * @since 3.5.0
    2320  *
    2321  * @see WP_oEmbed
    2322  *
    2323  * @param string $format The URL format for the oEmbed provider to remove.
    2324  * @return bool Was the provider removed successfully?
    2325  */
    2326 function wp_oembed_remove_provider( $format ) {
    2327     require_once( ABSPATH . WPINC . '/class-oembed.php' );
    2328 
    2329     if ( did_action( 'plugins_loaded' ) ) {
    2330         $oembed = _wp_oembed_get_object();
    2331 
    2332         if ( isset( $oembed->providers[ $format ] ) ) {
    2333             unset( $oembed->providers[ $format ] );
    2334             return true;
    2335         }
    2336     } else {
    2337         WP_oEmbed::_remove_provider_early( $format );
    2338     }
    2339 
    2340     return false;
    2341 }
    2342 
    2343 /**
    2344  * Determines if default embed handlers should be loaded.
    2345  *
    2346  * Checks to make sure that the embeds library hasn't already been loaded. If
    2347  * it hasn't, then it will load the embeds library.
    2348  *
    2349  * @since 2.9.0
    2350  *
    2351  * @see wp_embed_register_handler()
    2352  */
    2353 function wp_maybe_load_embeds() {
    2354     /**
    2355      * Filter whether to load the default embed handlers.
    2356      *
    2357      * Returning a falsey value will prevent loading the default embed handlers.
    2358      *
    2359      * @since 2.9.0
    2360      *
    2361      * @param bool $maybe_load_embeds Whether to load the embeds library. Default true.
    2362      */
    2363     if ( ! apply_filters( 'load_default_embeds', true ) ) {
    2364         return;
    2365     }
    2366 
    2367     wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
    2368 
    2369     wp_embed_register_handler( 'googlevideo', '#http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)#i', 'wp_embed_handler_googlevideo' );
    2370 
    2371     /**
    2372      * Filter the audio embed handler callback.
    2373      *
    2374      * @since 3.6.0
    2375      *
    2376      * @param callable $handler Audio embed handler callback function.
    2377      */
    2378     wp_embed_register_handler( 'audio', '#^https?://.+?\.(' . join( '|', wp_get_audio_extensions() ) . ')$#i', apply_filters( 'wp_audio_embed_handler', 'wp_embed_handler_audio' ), 9999 );
    2379 
    2380     /**
    2381      * Filter the video embed handler callback.
    2382      *
    2383      * @since 3.6.0
    2384      *
    2385      * @param callable $handler Video embed handler callback function.
    2386      */
    2387     wp_embed_register_handler( 'video', '#^https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')$#i', apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' ), 9999 );
    2388 }
    2389 
    2390 /**
    2391  * The Google Video embed handler callback.
    2392  *
    2393  * Google Video does not support oEmbed.
    2394  *
    2395  * @see WP_Embed::register_handler()
    2396  * @see WP_Embed::shortcode()
    2397  *
    2398  * @param array  $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
    2399  * @param array  $attr    Embed attributes.
    2400  * @param string $url     The original URL that was matched by the regex.
    2401  * @param array  $rawattr The original unmodified attributes.
    2402  * @return string The embed HTML.
    2403  */
    2404 function wp_embed_handler_googlevideo( $matches, $attr, $url, $rawattr ) {
    2405     // If the user supplied a fixed width AND height, use it
    2406     if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
    2407         $width  = (int) $rawattr['width'];
    2408         $height = (int) $rawattr['height'];
    2409     } else {
    2410         list( $width, $height ) = wp_expand_dimensions( 425, 344, $attr['width'], $attr['height'] );
    2411     }
    2412 
    2413     /**
    2414      * Filter the Google Video embed output.
    2415      *
    2416      * @since 2.9.0
    2417      *
    2418      * @param string $html    Google Video HTML embed markup.
    2419      * @param array  $matches The RegEx matches from the provided regex.
    2420      * @param array  $attr    An array of embed attributes.
    2421      * @param string $url     The original URL that was matched by the regex.
    2422      * @param array  $rawattr The original unmodified attributes.
    2423      */
    2424     return apply_filters( 'embed_googlevideo', '<embed type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docid=' . esc_attr($matches[2]) . '&amp;hl=en&amp;fs=true" style="width:' . esc_attr($width) . 'px;height:' . esc_attr($height) . 'px" allowFullScreen="true" allowScriptAccess="always" />', $matches, $attr, $url, $rawattr );
    2425 }
    2426 
    2427 /**
    2428  * YouTube iframe embed handler callback.
    2429  *
    2430  * Catches YouTube iframe embed URLs that are not parsable by oEmbed but can be translated into a URL that is.
    2431  *
    2432  * @since 4.0.0
    2433  *
    2434  * @global WP_Embed $wp_embed
    2435  *
    2436  * @param array  $matches The RegEx matches from the provided regex when calling
    2437  *                        wp_embed_register_handler().
    2438  * @param array  $attr    Embed attributes.
    2439  * @param string $url     The original URL that was matched by the regex.
    2440  * @param array  $rawattr The original unmodified attributes.
    2441  * @return string The embed HTML.
    2442  */
    2443 function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) {
    2444     global $wp_embed;
    2445     $embed = $wp_embed->autoembed( "https://youtube.com/watch?v={$matches[2]}" );
    2446 
    2447     /**
    2448      * Filter the YoutTube embed output.
    2449      *
    2450      * @since 4.0.0
    2451      *
    2452      * @see wp_embed_handler_youtube()
    2453      *
    2454      * @param string $embed   YouTube embed output.
    2455      * @param array  $attr    An array of embed attributes.
    2456      * @param string $url     The original URL that was matched by the regex.
    2457      * @param array  $rawattr The original unmodified attributes.
    2458      */
    2459     return apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr );
    2460 }
    2461 
    2462 /**
    2463  * Audio embed handler callback.
    2464  *
    2465  * @since 3.6.0
    2466  *
    2467  * @param array  $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
    2468  * @param array  $attr Embed attributes.
    2469  * @param string $url The original URL that was matched by the regex.
    2470  * @param array  $rawattr The original unmodified attributes.
    2471  * @return string The embed HTML.
    2472  */
    2473 function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) {
    2474     $audio = sprintf( '[audio src="%s" /]', esc_url( $url ) );
    2475 
    2476     /**
    2477      * Filter the audio embed output.
    2478      *
    2479      * @since 3.6.0
    2480      *
    2481      * @param string $audio   Audio embed output.
    2482      * @param array  $attr    An array of embed attributes.
    2483      * @param string $url     The original URL that was matched by the regex.
    2484      * @param array  $rawattr The original unmodified attributes.
    2485      */
    2486     return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr );
    2487 }
    2488 
    2489 /**
    2490  * Video embed handler callback.
    2491  *
    2492  * @since 3.6.0
    2493  *
    2494  * @param array  $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
    2495  * @param array  $attr    Embed attributes.
    2496  * @param string $url     The original URL that was matched by the regex.
    2497  * @param array  $rawattr The original unmodified attributes.
    2498  * @return string The embed HTML.
    2499  */
    2500 function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) {
    2501     $dimensions = '';
    2502     if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) {
    2503         $dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] );
    2504         $dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] );
    2505     }
    2506     $video = sprintf( '[video %s src="%s" /]', $dimensions, esc_url( $url ) );
    2507 
    2508     /**
    2509      * Filter the video embed output.
    2510      *
    2511      * @since 3.6.0
    2512      *
    2513      * @param string $video   Video embed output.
    2514      * @param array  $attr    An array of embed attributes.
    2515      * @param string $url     The original URL that was matched by the regex.
    2516      * @param array  $rawattr The original unmodified attributes.
    2517      */
    2518     return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr );
    25192199}
    25202200
Note: See TracChangeset for help on using the changeset viewer.