Changeset 12023 for trunk/wp-includes/media.php
- Timestamp:
- 10/13/2009 05:04:22 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/media.php
r11930 r12023 861 861 } 862 862 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 */ 870 class 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 */ 1139 function 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 */ 1149 function 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 */ 1159 function 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 */ 1188 function 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 */ 1209 function 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.