WordPress.org

Make WordPress Core

Ticket #10337: 10337.5.patch

File 10337.5.patch, 33.8 KB (added by Viper007Bond, 9 years ago)
  • wp-admin/admin-ajax.php

     
    124124
    125125        die();
    126126        break;
     127case 'oembed-cache' :
     128        $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
     129        die( $return );
     130        break;
    127131default :
    128132        do_action( 'wp_ajax_' . $_GET['action'] );
    129133        die('0');
  • wp-admin/includes/schema.php

     
    313313        'widget_rss' => array(),
    314314
    315315        // 2.8
    316         'timezone_string' => ''
     316        'timezone_string' => '',
     317
     318        // 2.9
     319        'embed_useoembed' => 1,
     320        'embed_autourls' => 1,
     321        'embed_size_w' => '',
     322        'embed_size_h' => 600,
    317323        );
    318324
    319325        // Set autoload to no for these options
  • wp-admin/options-media.php

     
    4444
    4545<tr valign="top">
    4646<th scope="row"><?php _e('Medium size') ?></th>
    47 <td><fieldset><legend class="screen-reader-text"><span><?php _e('Medium size') ?></span></legend>
     47<td><fieldset><legend class="screen-reader-text"><span><?php _e('Medium size'); ?></span></legend>
    4848<label for="medium_size_w"><?php _e('Max Width'); ?></label>
    4949<input name="medium_size_w" type="text" id="medium_size_w" value="<?php form_option('medium_size_w'); ?>" class="small-text" />
    5050<label for="medium_size_h"><?php _e('Max Height'); ?></label>
     
    5454
    5555<tr valign="top">
    5656<th scope="row"><?php _e('Large size') ?></th>
    57 <td><fieldset><legend class="screen-reader-text"><span><?php _e('Large size') ?></span></legend>
     57<td><fieldset><legend class="screen-reader-text"><span><?php _e('Large size'); ?></span></legend>
    5858<label for="large_size_w"><?php _e('Max Width'); ?></label>
    5959<input name="large_size_w" type="text" id="large_size_w" value="<?php form_option('large_size_w'); ?>" class="small-text" />
    6060<label for="large_size_h"><?php _e('Max Height'); ?></label>
     
    6565<?php do_settings_fields('media', 'default'); ?>
    6666</table>
    6767
     68<h3><?php _e('Embeds') ?></h3>
     69
     70<table class="form-table">
     71
     72<tr valign="top">
     73<th scope="row"><?php _e('oEmbed'); ?></th>
     74<td><fieldset><legend class="screen-reader-text"><span><?php printf( __('Use <a href="%s">oEmbed</a> to assist in rich content embedding'), 'http://codex.wordpress.org/oEmbed' ); ?></span></legend>
     75<label for="embed_useoembed"><input name="embed_useoembed" type="checkbox" id="embed_useoembed" value="1" <?php checked( '1', get_option('embed_useoembed') ); ?>/> <?php printf( __('Use <a href="%s">oEmbed</a> to assist in rich content embedding'), 'http://codex.wordpress.org/oEmbed' ); ?></label>
     76</fieldset></td>
     77</tr>
     78
     79<tr valign="top">
     80<th scope="row"><?php _e('Auto-embeds'); ?></th>
     81<td><fieldset><legend class="screen-reader-text"><span><?php _e('Attempt to automatically embed all plain text URLs'); ?></span></legend>
     82<label for="embed_autourls"><input name="embed_autourls" type="checkbox" id="embed_autourls" value="1" <?php checked( '1', get_option('embed_autourls') ); ?>/> <?php _e('Attempt to automatically embed all plain text URLs'); ?></label>
     83</fieldset></td>
     84</tr>
     85
     86<tr valign="top">
     87<th scope="row"><?php _e('Embed size') ?></th>
     88<td>
     89<label for="embed_size_w"><?php _e('Width'); ?></label>
     90<input name="embed_size_w" type="text" id="embed_size_w" value="<?php form_option('embed_size_w'); ?>" class="small-text" />
     91<label for="embed_size_h"><?php _e('Height'); ?></label>
     92<input name="embed_size_h" type="text" id="embed_size_h" value="<?php form_option('embed_size_h'); ?>" class="small-text" />
     93<?php if ( !empty($content_width) ) echo '<br />' . __("If the width value is left blank, embeds will default to the max width of your theme."); ?>
     94</td>
     95</tr>
     96
     97<?php do_settings_fields('media', 'embeds'); ?>
     98</table>
     99
    68100<?php do_settings_sections('media'); ?>
    69101
    70102<p class="submit">
  • wp-admin/options.php

     
    2525        'general' => array( 'blogname', 'blogdescription', 'admin_email', 'users_can_register', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'default_role', 'timezone_string' ),
    2626        'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration' ),
    2727        'misc' => array( 'use_linksupdate', 'uploads_use_yearmonth_folders', 'upload_path', 'upload_url_path' ),
    28         'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type' ),
     28        'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type', 'embed_useoembed', 'embed_autourls', 'embed_size_w', 'embed_size_h' ),
    2929        'privacy' => array( 'blog_public' ),
    3030        'reading' => array( 'posts_per_page', 'posts_per_rss', 'rss_use_excerpt', 'blog_charset', 'show_on_front', 'page_on_front', 'page_for_posts' ),
    3131        'writing' => array( 'default_post_edit_rows', 'use_smilies', 'ping_sites', 'mailserver_url', 'mailserver_port', 'mailserver_login', 'mailserver_pass', 'default_category', 'default_email_category', 'use_balanceTags', 'default_link_category', 'enable_app', 'enable_xmlrpc' ),
  • wp-includes/capabilities.php

     
    960960}
    961961
    962962/**
     963 * Whether author of supplied post has capability or role.
     964 *
     965 * @since 2.9
     966 *
     967 * @param int|object $post Post ID or post object.
     968 * @param string $capability Capability or role name.
     969 * @return bool
     970 */
     971function author_can( $post, $capability ) {
     972        if ( !$post = get_post($post) )
     973                return false;
     974
     975        $author = new WP_User( $post->post_author );
     976
     977        if ( empty( $author ) )
     978                return false;
     979
     980        $args = array_slice( func_get_args(), 2 );
     981        $args = array_merge( array( $capability ), $args );
     982
     983        return call_user_func_array( array( &$author, 'has_cap' ), $args );
     984}
     985
     986/**
    963987 * Retrieve role object.
    964988 *
    965989 * @see WP_Roles::get_role() Uses method to retrieve role object.
  • wp-includes/class-oembed.php

     
     1<?php
     2/**
     3 * API for fetching the HTML to embed remote content based on a provided URL.
     4 * Used internally by the {@link WP_Embed} class, but is designed to be generic.
     5 *
     6 * @link http://codex.wordpress.org/oEmbed oEmbed Codex Article
     7 * @link http://oembed.com/ oEmbed Homepage
     8 *
     9 * @package WordPress
     10 * @subpackage oEmbed
     11 */
     12
     13/**
     14 * oEmbed class.
     15 *
     16 * @package WordPress
     17 * @subpackage oEmbed
     18 * @since 2.9.0
     19 */
     20class WP_oEmbed {
     21        var $providers = array();
     22
     23        /**
     24         * PHP4 constructor
     25         */
     26        function WP_oEmbed() {
     27                return $this->__construct();
     28        }
     29
     30        /**
     31         * PHP5 constructor
     32         *
     33         * @uses apply_filters() Filters a list of pre-defined oEmbed providers.
     34         */
     35        function __construct() {
     36                // List out some popular sites, mainly ones that don't have discovery tags in their <head>
     37                // The WP_Embed class disables discovery for non-unfiltered_html users,
     38                // so only providers in this array will be used for them.
     39                $this->providers = apply_filters( 'oembed_providers', array(
     40                        'http://blip.tv/file/*'       => 'http://blip.tv/oembed/',
     41                        'http://*.flickr.com/*'       => 'http://www.flickr.com/services/oembed/',
     42                        'http://*.viddler.com/*'      => 'http://lab.viddler.com/services/oembed/',
     43                        'http://qik.com/*'            => 'http://qik.com/api/oembed.{format}',
     44                        'http://*.revision3.com/*'    => 'http://revision3.com/api/oembed/',
     45                        'http://www.hulu.com/watch/*' => 'http://www.hulu.com/api/oembed.{format}',
     46
     47                        // Vimeo uses the discovery <link>, so leave this commented to use it as a discovery test
     48                        //'http://www.vimeo.com/*'      => 'http://www.vimeo.com/api/oembed.{format}',
     49                ) );
     50        }
     51
     52        /**
     53         * The do-it-all function that takes a URL and attempts to return the HTML.
     54         *
     55         * @see WP_eEmbed::discover()
     56         * @see WP_eEmbed::fetch()
     57         * @see WP_eEmbed::data2html()
     58         *
     59         * @param string $url The URL to the content that should be attempted to be embedded.
     60         * @param array $args Optional arguments. Usually passed from a shortcode.
     61         * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
     62         */
     63        function get_html( $url, $args = '' ) {
     64                $provider = false;
     65
     66                if ( !isset($args['discover']) )
     67                        $args['discover'] = true;
     68
     69                foreach ( $this->providers as $matchmask => $providerurl ) {
     70                        // Turn the asterisk-type provider URLs into regex
     71                        $regex = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $scheme ) ) ) . '#i';
     72
     73                        if ( preg_match( $regex, $url ) ) {
     74                                $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
     75                                break;
     76                        }
     77                }
     78
     79                if ( !$provider && $args['discover'] )
     80                        $provider = $this->discover( $url );
     81
     82                if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
     83                        return false;
     84
     85                return apply_filters( 'oembed_output', $this->data2html( $data, $url ), $url, $args );
     86        }
     87
     88        /**
     89         * Attempts to find oEmbed provider discovery <link> tags at the given URL.
     90         *
     91         * @param string $url The URL that should be inspected for discovery <link> tags.
     92         * @return bool|string False on failure, otherwise the oEmbed provider URL.
     93         */
     94        function discover( $url ) {
     95                $providers = array();
     96
     97                // Fetch URL content
     98                if ( $html = wp_remote_retrieve_body( wp_remote_get( $url ) ) ) {
     99
     100                        // <link> types that contain oEmbed provider URLs
     101                        $linktypes = apply_filters( 'oembed_linktypes', array(
     102                                'application/json+oembed' => 'json',
     103                                'text/xml+oembed' => 'xml',
     104                                'application/xml+oembed' => 'xml', // Incorrect, but used by at least Vimeo
     105                        ) );
     106
     107                        // Strip <body>
     108                        $html = substr( $html, 0, stripos( $html, '</head>' ) );
     109
     110                        // Do a quick check
     111                        $tagfound = false;
     112                        foreach ( $linktypes as $linktype => $format ) {
     113                                if ( stripos($html, $linktype) ) {
     114                                        $tagfound = true;
     115                                        break;
     116                                }
     117                        }
     118
     119                        if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) {
     120                                foreach ( $links[1] as $link ) {
     121                                        $atts = shortcode_parse_atts( $link );
     122
     123                                        if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) {
     124                                                $providers[$linktypes[$atts['type']]] = $atts['href'];
     125
     126                                                // Stop here if it's JSON (that's all we need)
     127                                                if ( 'json' == $linktypes[$atts['type']] )
     128                                                        break;
     129                                        }
     130                                }
     131                        }
     132                }
     133
     134                // JSON is preferred to XML
     135                if ( !empty($providers['json']) )
     136                        return $providers['json'];
     137                elseif ( !empty($providers['xml']) )
     138                        return $providers['xml'];
     139                else
     140                        return false;
     141        }
     142
     143        /**
     144         * Connects to a oEmbed provider and returns the result.
     145         *
     146         * @param string $provider The URL to the oEmbed provider.
     147         * @param string $url The URL to the content that is desired to be embedded.
     148         * @param array $args Optional arguments. Usually passed from a shortcode.
     149         * @return bool|object False on failure, otherwise the result in the form of an object.
     150         */
     151        function fetch( $provider, $url, $args = '' ) {
     152                $args = wp_parse_args( $args, wp_embed_defaults() );
     153
     154                $provider = add_query_arg( 'format', 'json', $provider ); // JSON is easier to deal with than XML
     155
     156                $provider = add_query_arg( 'maxwidth', $args['width'], $provider );
     157                $provider = add_query_arg( 'maxheight', $args['height'], $provider );
     158                $provider = add_query_arg( 'url', urlencode($url), $provider );
     159
     160                if ( !$result = wp_remote_retrieve_body( wp_remote_get( $provider ) ) )
     161                        return false;
     162
     163                $result = trim( $result );
     164
     165                // JSON?
     166                // Example content: http://vimeo.com/api/oembed.json?url=http%3A%2F%2Fvimeo.com%2F240975
     167                if ( $data = json_decode($result) ) {
     168                        return $data;
     169                }
     170
     171                // Must be XML. Only parse it if PHP5 is installed. (PHP4 isn't worth the trouble.)
     172                // Example content: http://vimeo.com/api/oembed.xml?url=http%3A%2F%2Fvimeo.com%2F240975
     173                elseif ( function_exists('simplexml_load_string') ) {
     174                        $errors = libxml_use_internal_errors( 'true' );
     175                       
     176                        $data = simplexml_load_string( $result );
     177
     178                        libxml_use_internal_errors( $errors );
     179
     180                        if ( is_object($data) )
     181                                return $data;
     182                }
     183
     184                return false;
     185        }
     186
     187        /**
     188         * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
     189         *
     190         * @param object $data A data object result from an oEmbed provider.
     191         * @param string $url The URL to the content that is desired to be embedded.
     192         * @return bool|string False on error, otherwise the HTML needed to embed.
     193         */
     194        function data2html( $data, $url ) {
     195                if ( !is_object($data) || empty($data->type) )
     196                        return false;
     197
     198                switch ( $data->type ) {
     199                        case 'photo':
     200                                if ( empty($data->url) || empty($data->width) || empty($data->height) )
     201                                        return false;
     202
     203                                $title = ( !empty($data->title) ) ? $data->title : '';
     204                                return '<img src="' . esc_attr( clean_url( $data->url ) ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" />';
     205
     206                        case 'video':
     207                        case 'rich':
     208                                return ( !empty($data->html) ) ? $data->html : false;
     209
     210                        case 'link':
     211                                return ( !empty($data->title) ) ? '<a href="' . clean_url($url) . '">' . esc_html($data->title) . '</a>' : false;
     212                }
     213
     214                return false;
     215        }
     216}
     217
     218/**
     219 * Returns the initialized {@link WP_oEmbed} object
     220 *
     221 * @since 2.9.0
     222 * @access private
     223 *
     224 * @see WP_oEmbed
     225 * @uses WP_oEmbed
     226 *
     227 * @return WP_oEmbed object.
     228 */
     229function &_wp_oembed_get_object() {
     230        static $wp_oembed;
     231
     232        if ( is_null($wp_oembed) )
     233                $wp_oembed = new WP_oEmbed();
     234
     235        return $wp_oembed;
     236}
  • wp-includes/default-embeds.php

     
     1<?php
     2
     3/**
     4 * Default Embed Handlers
     5 *
     6 * @package WordPress
     7 * @subpackage Embeds
     8 */
     9
     10/**
     11 * The YouTube.com embed handler callback. YouTube does not support oEmbed and we want to provide extra customization.
     12 *
     13 * @see WP_Embed::register_handler()
     14 * @see WP_Embed::shortcode()
     15 *
     16 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
     17 * @param array $attr Embed attributes.
     18 * @param string $url The original URL that was matched by the regex.
     19 * @param array $rawattr The original unmodified attributes.
     20 * @return string The embed HTML.
     21 */
     22function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) {
     23        // If the user supplied a fixed width AND height, use it
     24        if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
     25                $width  = (int) $rawattr['width'];
     26                $height = (int) $rawattr['height'];
     27        } else {
     28                list( $width, $height ) = wp_expand_dimensions( 425, 344, $attr['width'], $attr['height'] );
     29        }
     30
     31        return apply_filters( 'embed_youtube', '<object width="' . esc_attr($width) . '" height="' . esc_attr($height) . '"><param name="movie" value="http://www.youtube.com/v/' . esc_attr($matches[3]) . '&amp;hl=en&amp;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . esc_attr($matches[3]) . '&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' . esc_attr($width) . '" height="' . esc_attr($height) . '"></embed></object>', $matches, $attr, $url, $rawattr );
     32}
     33wp_embed_register_handler( 'youtube', '#http://(www.youtube|youtube|[A-Za-z]{2}.youtube)\.com/(watch\?v=|w/\?v=|\?v=)([\w-]+)(.*?)#i', 'wp_embed_handler_youtube' );
     34
     35
     36/**
     37 * The Google Video embed handler callback. Google Video does not support oEmbed.
     38 *
     39 * @see WP_Embed::register_handler()
     40 * @see WP_Embed::shortcode()
     41 *
     42 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
     43 * @param array $attr Embed attributes.
     44 * @param string $url The original URL that was matched by the regex.
     45 * @param array $rawattr The original unmodified attributes.
     46 * @return string The embed HTML.
     47 */
     48function wp_embed_handler_googlevideo( $matches, $attr, $url, $rawattr ) {
     49        // If the user supplied a fixed width AND height, use it
     50        if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
     51                $width  = (int) $rawattr['width'];
     52                $height = (int) $rawattr['height'];
     53        } else {
     54                list( $width, $height ) = wp_expand_dimensions( 425, 344, $attr['width'], $attr['height'] );
     55        }
     56
     57        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"></embed>', $matches, $attr, $url, $rawattr );
     58}
     59wp_embed_register_handler( 'googlevideo', '#http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)#i', 'wp_embed_handler_googlevideo' );
     60
     61/**
     62 * The PollDaddy.com embed handler callback. PollDaddy does not support oEmbed, at least not yet.
     63 *
     64 * @see WP_Embed::register_handler()
     65 * @see WP_Embed::shortcode()
     66 *
     67 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
     68 * @param array $attr Embed attributes.
     69 * @param string $url The original URL that was matched by the regex.
     70 * @param array $rawattr The original unmodified attributes.
     71 * @return string The embed HTML.
     72 */
     73function wp_embed_handler_polldaddy( $matches, $attr, $url, $rawattr ) {
     74        return apply_filters( 'embed_polldaddy', '<script type="text/javascript" charset="utf8" src="http://s3.polldaddy.com/p/' . esc_attr($matches[1]) . '"></script>', $matches, $attr, $url, $rawattr );
     75}
     76wp_embed_register_handler( 'polldaddy', '#http://answers.polldaddy.com/poll/(\d+)(.*?)#i', 'wp_embed_handler_polldaddy' );
     77
     78/**
     79 * The DailyMotion.com embed handler callback. DailyMotion does not support oEmbed.
     80 *
     81 * @see WP_Embed::register_handler()
     82 * @see WP_Embed::shortcode()
     83 *
     84 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
     85 * @param array $attr Embed attributes.
     86 * @param string $url The original URL that was matched by the regex.
     87 * @param array $rawattr The original unmodified attributes.
     88 * @return string The embed HTML.
     89 */
     90function wp_embed_handler_dailymotion( $matches, $attr, $url, $rawattr ) {
     91        // If the user supplied a fixed width AND height, use it
     92        if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
     93                $width  = (int) $rawattr['width'];
     94                $height = (int) $rawattr['height'];
     95        } else {
     96                list( $width, $height ) = wp_expand_dimensions( 480, 291, $attr['width'], $attr['height'] );
     97        }
     98
     99        return apply_filters( 'embed_dailymotion', '<object width="' . esc_attr($width) . '" height="' . esc_attr($height) . '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"><param name="movie" value="http://www.dailymotion.com/swf/' . esc_attr($matches[3]) . '&amp;related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.dailymotion.com/swf/' . esc_attr($matches[3]) . '&amp;related=0" type="application/x-shockwave-flash" width="' . esc_attr($width) . '" height="' . esc_attr($height) . '" allowFullScreen="true" allowScriptAccess="always"></embed></object>', $matches, $attr, $url, $rawattr );;
     100}
     101wp_embed_register_handler( 'dailymotion', '#http://(www.dailymotion|dailymotion)\.com/(.+)/([0-9a-zA-Z]+)\_(.*?)#i', 'wp_embed_handler_dailymotion' );
  • wp-includes/default-filters.php

     
    206206add_action('admin_print_styles', 'print_admin_styles', 20);
    207207add_action('init', 'smilies_init', 5);
    208208add_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 );
     209add_action( 'plugins_loaded', 'wp_maybe_load_embeds', 0 );
    209210add_action( 'shutdown', 'wp_ob_end_flush_all', 1);
    210211add_action( 'pre_post_update', 'wp_save_post_revision' );
    211212add_action('publish_post', '_publish_post_hook', 5, 1);
  • wp-includes/formatting.php

     
    23582358                case 'medium_size_h':
    23592359                case 'large_size_w':
    23602360                case 'large_size_h':
     2361                //case 'embed_size_w':
     2362                case 'embed_size_h':
    23612363                case 'default_post_edit_rows':
    23622364                case 'mailserver_port':
    23632365                case 'comment_max_links':
  • wp-includes/functions.php

     
    27422742}
    27432743
    27442744/**
     2745 * Determines if default embed handlers should be loaded.
     2746 *
     2747 * Checks to make sure that the embeds library hasn't already been loaded. If
     2748 * it hasn't, then it will load the embeds library.
     2749 *
     2750 * @since 2.9
     2751 */
     2752function wp_maybe_load_embeds() {
     2753        if ( ! apply_filters('load_default_embeds', true) )
     2754                return;
     2755        require_once( ABSPATH . WPINC . '/default-embeds.php' );
     2756}
     2757
     2758/**
    27452759 * Determines if Widgets library should be loaded.
    27462760 *
    27472761 * Checks to make sure that the widgets library hasn't already been loaded. If
  • wp-includes/media.php

     
    860860        return $img;
    861861}
    862862
     863/**
     864 * API for easily embedding rich media such as videos and images into content.
     865 *
     866 * @package WordPress
     867 * @subpackage Embed
     868 * @since 2.9.0
     869 */
     870class WP_Embed {
     871        var $handlers = array();
     872        var $post_ID;
     873        var $usecache = true;
     874        var $linkifunknown = true;
     875
     876        /**
     877         * PHP4 constructor
     878         */
     879        function WP_Embed() {
     880                return $this->__construct();
     881        }
     882
     883        /**
     884         * PHP5 constructor
     885         */
     886        function __construct() {
     887                // Hack to get the [embed] shortcode to run before wpautop()
     888                add_filter( 'the_content', array(&$this, 'run_shortcode'), 8 );
     889
     890                // Attempts to embed all URLs in a post
     891                if ( get_option('embed_autourls') )
     892                        add_filter( 'the_content', array(&$this, 'autoembed'), 8 );
     893
     894                // After a post is saved, cache oEmbed items via AJAX
     895                if ( get_option('embed_useoembed') )
     896                        add_action( 'edit_form_advanced', array(&$this, 'maybe_run_ajax_cache') );
     897        }
     898
     899        /**
     900         * Process the [embed] shortcode.
     901         *
     902         * Since the [embed] shortcode needs to be run earlier than other shortcodes,
     903         * this function removes all existing shortcodes, registers the [embed] shortcode,
     904         * calls {@link do_shortcode()}, and then re-registers the old shortcodes.
     905         *
     906         * @uses $shortcode_tags
     907         * @uses remove_all_shortcodes()
     908         * @uses add_shortcode()
     909         * @uses do_shortcode()
     910         *
     911         * @param string $content Content to parse
     912         * @return string Content with shortcode parsed
     913         */
     914        function run_shortcode( $content ) {
     915                global $shortcode_tags;
     916
     917                // Backup current registered shortcodes and clear them all out
     918                $orig_shortcode_tags = $shortcode_tags;
     919                remove_all_shortcodes();
     920
     921                add_shortcode( 'embed', array(&$this, 'shortcode') );
     922
     923                // Do the shortcode (only the [embed] one is registered)
     924                $content = do_shortcode( $content );
     925
     926                // Put the original shortcodes back
     927                $shortcode_tags = $orig_shortcode_tags;
     928
     929                return $content;
     930        }
     931
     932        /**
     933         * If a post/page was saved, then output Javascript to make
     934         * an AJAX request that will call WP_Embed::cache_oembed().
     935         */
     936        function maybe_run_ajax_cache() {
     937                global $post_ID;
     938
     939                if ( empty($post_ID) || empty($_GET['message']) || 1 != $_GET['message'] )
     940                        return;
     941
     942?>
     943<script type="text/javascript">
     944/* <![CDATA[ */
     945        jQuery(document).ready(function($){
     946                $.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post_ID ); ?>");
     947        });
     948/* ]]> */
     949</script>
     950<?php
     951        }
     952
     953        /**
     954         * Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead.
     955         * This function should probably also only be used for sites that do not support oEmbed.
     956         *
     957         * @param string $id An internal ID/name for the handler. Needs to be unique.
     958         * @param string $regex The regex that will be used to see if this handler should be used for a URL.
     959         * @param callback $callback The callback function that will be called if the regex is matched.
     960         * @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.
     961         */
     962        function register_handler( $id, $regex, $callback, $priority = 10 ) {
     963                $this->handlers[$priority][$id] = array(
     964                        'regex'    => $regex,
     965                        'callback' => $callback,
     966                );
     967        }
     968
     969        /**
     970         * Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead.
     971         *
     972         * @param string $id The handler ID that should be removed.
     973         * @param int $priority Optional. The priority of the handler to be removed (default: 10).
     974         */
     975        function unregister_handler( $id, $priority = 10 ) {
     976                if ( isset($this->handlers[$priority][$id]) )
     977                        unset($this->handlers[$priority][$id]);
     978        }
     979
     980        /**
     981         * The {@link do_shortcode()} callback function.
     982         *
     983         * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers.
     984         * If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class.
     985         *
     986         * @uses wp_oembed_get()
     987         * @uses wp_parse_args()
     988         * @uses wp_embed_defaults()
     989         * @uses WP_Embed::maybe_make_link()
     990         * @uses get_option()
     991         * @uses current_user_can()
     992         * @uses wp_cache_get()
     993         * @uses wp_cache_set()
     994         * @uses get_post_meta()
     995         * @uses update_post_meta()
     996         *
     997         * @param array $attr Shortcode attributes.
     998         * @param string $url The URL attempting to be embeded.
     999         * @return string The embed HTML on success, otherwise the original URL.
     1000         */
     1001        function shortcode( $attr, $url = '' ) {
     1002                global $post, $_wp_using_ext_object_cache;
     1003
     1004                if ( empty($url) )
     1005                        return '';
     1006
     1007                $rawattr = $attr;
     1008                $attr = wp_parse_args( $attr, wp_embed_defaults() );
     1009
     1010                // Look for known internal handlers
     1011                ksort( $this->handlers );
     1012                foreach ( $this->handlers as $priority => $handlers ) {
     1013                        foreach ( $handlers as $id => $handler ) {
     1014                                if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
     1015                                        if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
     1016                                                return $return;
     1017                                }
     1018                        }
     1019                }
     1020
     1021                $post_ID = ( !empty($post->ID) ) ? $post->ID : null;
     1022                if ( !empty($this->post_ID) ) // Potentially set by WP_Embed::cache_oembed()
     1023                        $post_ID = $this->post_ID;
     1024
     1025                // Unknown URL format. Let oEmbed have a go.
     1026                if ( $post_ID && get_option('embed_useoembed') ) {
     1027
     1028                        // Check for a cached result (stored in the post meta)
     1029                        $cachekey = '_oembed_' . md5( $url . implode( '|', $attr ) );
     1030                        if ( $this->usecache ) {
     1031                                $cache = ( $_wp_using_ext_object_cache ) ? wp_cache_get( "{$post_ID}_{$cachekey}", 'oembed' ) : get_post_meta( $post_ID, $cachekey, true );
     1032
     1033                                // Failures are cached
     1034                                if ( '{{unknown}}' === $cache )
     1035                                        return $this->maybe_make_link( $url );
     1036
     1037                                if ( !empty($cache) )
     1038                                        return $cache;
     1039                        }
     1040
     1041                        // Use oEmbed to get the HTML
     1042                        $attr['discover'] = author_can( $post_ID, 'unfiltered_html' );
     1043                        $html = wp_oembed_get( $url, $attr );
     1044
     1045                        // Cache the result
     1046                        $cache = ( $html ) ? $html : '{{unknown}}';
     1047                        if ( $_wp_using_ext_object_cache )
     1048                                wp_cache_set( "{$post_ID}_{$cachekey}", $cache, 'oembed' );
     1049                        else
     1050                                update_post_meta( $post_ID, $cachekey, $cache );
     1051
     1052                        // If there was a result, return it
     1053                        if ( $html )
     1054                                return $html;
     1055                }
     1056
     1057                // Still unknown
     1058                return $this->maybe_make_link( $url );
     1059        }
     1060
     1061        /**
     1062         * Triggers a caching of all oEmbed results.
     1063         *
     1064         * @param int $post_ID Post ID to do the caching for.
     1065         */
     1066        function cache_oembed( $post_ID ) {
     1067                $post = get_post( $post_ID );
     1068
     1069                // post_type check is incase of "save_post" usage
     1070                if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) )
     1071                        return;
     1072
     1073                // Dump existing caches
     1074                $post_metas = get_post_custom_keys( $post->ID );
     1075                foreach( $post_metas as $post_meta_key ) {
     1076                        if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
     1077                                delete_post_meta( $post->ID, $post_meta_key );
     1078                }
     1079
     1080                // Trigger a caching
     1081                if ( !empty($post->post_content) ) {
     1082                        $this->post_ID = $post->ID;
     1083                        $this->usecache = false;
     1084
     1085                        $content = $this->runshortcode( $post->post_content );
     1086                        if ( get_option('embed_autourls') )
     1087                                $this->autoembed( $content );
     1088
     1089                        $this->usecache = true;
     1090                }
     1091        }
     1092
     1093        /**
     1094         * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
     1095         *
     1096         * @uses WP_Embed::autoembed_callback()
     1097         *
     1098         * @param string $content The content to be searched.
     1099         * @return string Potentially modified $content.
     1100         */
     1101        function autoembed( $content ) {
     1102                return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array(&$this, 'autoembed_callback'), $content );
     1103        }
     1104
     1105        /**
     1106         * Callback function for {@link WP_Embed::autoembed()}.
     1107         *
     1108         * @uses WP_Embed::shortcode()
     1109         *
     1110         * @param array $match A regex match array.
     1111         * @return string The embed HTML on success, otherwise the original URL.
     1112         */
     1113        function autoembed_callback( $match ) {
     1114                $oldval = $this->linkifunknown;
     1115                $this->linkifunknown = false;
     1116                $return = $this->shortcode( array(), $match[1] );
     1117                $this->linkifunknown = $oldval;
     1118
     1119                return "\n$return\n";
     1120        }
     1121
     1122        /**
     1123         * Conditionally makes a hyperlink based on an internal class variable.
     1124         *
     1125         * @param string $url URL to potentially be linked.
     1126         * @return string Linked URL or the original URL.
     1127         */
     1128        function maybe_make_link( $url ) {
     1129                return ( $this->linkifunknown ) ? '<a href="' . esc_attr($url) . '">' . esc_html($url) . '</a>' : $url;
     1130        }
     1131}
     1132$wp_embed = new WP_Embed();
     1133
     1134/**
     1135 * Register an embed handler. This function should probably only be used for sites that do not support oEmbed.
     1136 *
     1137 * @see WP_Embed::register_handler()
     1138 */
     1139function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
     1140        global $wp_embed;
     1141        $wp_embed->register_handler( $id, $regex, $callback, $priority );
     1142}
     1143
     1144/**
     1145 * Unregister a previously registered embed handler.
     1146 *
     1147 * @see WP_Embed::unregister_handler()
     1148 */
     1149function wp_embed_unregister_handler( $id, $priority = 10 ) {
     1150        global $wp_embed;
     1151        $wp_embed->unregister_handler( $id, $priority );
     1152}
     1153
     1154/**
     1155 * Create default array of embed parameters.
     1156 *
     1157 * @return array Default embed parameters.
     1158 */
     1159function wp_embed_defaults() {
     1160        if ( !empty($GLOBALS['content_width']) )
     1161                $theme_width = (int) $GLOBALS['content_width'];
     1162
     1163        $width = get_option('embed_size_w');
     1164
     1165        if ( !$width && !empty($theme_width) )
     1166                $width = $theme_width;
     1167
     1168        if ( !$width )
     1169                $width = 500;
     1170
     1171        return apply_filters( 'embed_defaults', array(
     1172                'width' => $width,
     1173                'height' => 700,
     1174        ) );
     1175}
     1176
     1177/**
     1178 * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height.
     1179 *
     1180 * @uses wp_constrain_dimensions() This function passes the widths and the heights.
     1181 *
     1182 * @param int $example_width The width of an example embed.
     1183 * @param int $example_height The height of an example embed.
     1184 * @param int $max_width The maximum allowed width.
     1185 * @param int $max_height The maximum allowed height.
     1186 * @return array The maximum possible width and height based on the example ratio.
     1187 */
     1188function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) {
     1189        $example_width  = (int) $example_width;
     1190        $example_height = (int) $example_height;
     1191        $max_width      = (int) $max_width;
     1192        $max_height     = (int) $max_height;
     1193
     1194        return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height );
     1195}
     1196
     1197/**
     1198 * Attempts to fetch the embed HTML for a provided URL using oEmbed.
     1199 *
     1200 * @see WP_oEmbed
     1201 *
     1202 * @uses _wp_oembed_get_object()
     1203 * @uses WP_oEmbed::get_html()
     1204 *
     1205 * @param string $url The URL that should be embeded.
     1206 * @param array $args Addtional arguments and parameters.
     1207 * @return string The original URL on failure or the embed HTML on success.
     1208 */
     1209function wp_oembed_get( $url, $args = '' ) {
     1210        require_once( 'class-oembed.php' );
     1211        $oembed = _wp_oembed_get_object();
     1212        return $oembed->get_html( $url, $args );
     1213}