Make WordPress Core

Changeset 12023


Ignore:
Timestamp:
10/13/2009 05:04:22 PM (17 years ago)
Author:
ryan
Message:

Embeds. Props Viper007Bond. see #10337

Location:
trunk
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r12022 r12023  
    124124
    125125        die();
     126        break;
     127case 'oembed-cache' :
     128        $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
     129        die( $return );
    126130        break;
    127131default :
  • trunk/wp-admin/includes/schema.php

    r11980 r12023  
    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
  • trunk/wp-admin/options-media.php

    r11761 r12023  
    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" />
     
    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" />
     
    6464
    6565<?php do_settings_fields('media', 'default'); ?>
     66</table>
     67
     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'); ?>
    6698</table>
    6799
  • trunk/wp-admin/options.php

    r11380 r12023  
    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' ),
  • trunk/wp-includes/capabilities.php

    r11929 r12023  
    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 *
  • trunk/wp-includes/default-filters.php

    r11929 r12023  
    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' );
  • trunk/wp-includes/formatting.php

    r11978 r12023  
    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':
  • trunk/wp-includes/functions.php

    r11995 r12023  
    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 *
  • trunk/wp-includes/media.php

    r11930 r12023  
    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->run_shortcode( $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}
Note: See TracChangeset for help on using the changeset viewer.