328 * @param string $suffix Optional. File Suffix. |
349 * @param string $suffix Optional. File Suffix. |
329 * @param string $dest_path Optional. New image file path. |
350 * @param string $dest_path Optional. New image file path. |
330 * @param int $jpeg_quality Optional, default is 90. Image quality percentage. |
351 * @param int $jpeg_quality Optional, default is 90. Image quality percentage. |
331 * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()} |
352 * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()} |
332 */ |
353 */ |
333 function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) { |
354 function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { |
334 |
355 |
335 $image = wp_load_image( $file ); |
356 $image = wp_load_image( $file ); |
336 if ( !is_resource( $image ) ) |
357 if ( !is_resource( $image ) ) |
337 return new WP_Error('error_loading_image', $image); |
358 return new WP_Error('error_loading_image', $image); |
338 |
359 |
339 list($orig_w, $orig_h, $orig_type) = getimagesize( $file ); |
360 $size = @getimagesize( $file ); |
|
361 if ( !$size ) |
|
362 return new WP_Error('invalid_image', __('Could not read image size'), $file); |
|
363 list($orig_w, $orig_h, $orig_type) = $size; |
|
364 |
340 $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop); |
365 $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop); |
341 if (!$dims) |
366 if ( !$dims ) |
342 return $dims; |
367 return $dims; |
343 list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; |
368 list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; |
344 |
369 |
345 $newimage = imagecreatetruecolor( $dst_w, $dst_h); |
370 $newimage = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
346 |
|
347 // preserve PNG transparency |
|
348 if ( IMAGETYPE_PNG == $orig_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
|
349 imagealphablending( $newimage, false); |
|
350 imagesavealpha( $newimage, true); |
|
351 } |
|
352 |
371 |
353 imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
372 imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
|
373 |
|
374 // convert from full colors to index colors, like original PNG. |
|
375 if ( IMAGETYPE_PNG == $orig_type && !imageistruecolor( $image ) ) |
|
376 imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) ); |
354 |
377 |
355 // we don't need the original in memory anymore |
378 // we don't need the original in memory anymore |
356 imagedestroy( $image ); |
379 imagedestroy( $image ); |
357 |
380 |
358 // $suffix will be appended to the destination filename, just before the extension |
381 // $suffix will be appended to the destination filename, just before the extension |
525 * @param int $attachment_id Image attachment ID. |
546 * @param int $attachment_id Image attachment ID. |
526 * @param string $size Optional, default is 'thumbnail'. |
547 * @param string $size Optional, default is 'thumbnail'. |
527 * @param bool $icon Optional, default is false. Whether it is an icon. |
548 * @param bool $icon Optional, default is false. Whether it is an icon. |
528 * @return string HTML img element or empty string on failure. |
549 * @return string HTML img element or empty string on failure. |
529 */ |
550 */ |
530 function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) { |
551 function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { |
531 |
552 |
532 $html = ''; |
553 $html = ''; |
533 $image = wp_get_attachment_image_src($attachment_id, $size, $icon); |
554 $image = wp_get_attachment_image_src($attachment_id, $size, $icon); |
534 if ( $image ) { |
555 if ( $image ) { |
535 list($src, $width, $height) = $image; |
556 list($src, $width, $height) = $image; |
536 $hwstring = image_hwstring($width, $height); |
557 $hwstring = image_hwstring($width, $height); |
537 if ( is_array($size) ) |
558 if ( is_array($size) ) |
538 $size = join('x', $size); |
559 $size = join('x', $size); |
539 $attachment =& get_post($attachment_id); |
560 $attachment =& get_post($attachment_id); |
540 $attr = array( |
561 $default_attr = array( |
541 'src' => $src, |
562 'src' => $src, |
542 'class' => "attachment-$size", |
563 'class' => "attachment-$size", |
543 'alt' => trim(strip_tags( $attachment->post_excerpt )), |
564 'alt' => trim(strip_tags( $attachment->post_excerpt )), |
544 'title' => trim(strip_tags( $attachment->post_title )), |
565 'title' => trim(strip_tags( $attachment->post_title )), |
545 ); |
566 ); |
|
567 $attr = wp_parse_args($attr, $default_attr); |
546 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); |
568 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); |
547 $attr = array_map( 'esc_attr', $attr ); |
569 $attr = array_map( 'esc_attr', $attr ); |
548 $html = rtrim("<img $hwstring"); |
570 $html = rtrim("<img $hwstring"); |
549 foreach ( $attr as $name => $value ) { |
571 foreach ( $attr as $name => $value ) { |
550 $html .= " $name=" . '"' . $value . '"'; |
572 $html .= " $name=" . '"' . $value . '"'; |
551 } |
573 } |
552 $html .= ' />'; |
574 $html .= ' />'; |
553 } |
575 } |
554 |
576 |
555 return $html; |
577 return $html; |
|
578 } |
|
579 |
|
580 /** |
|
581 * Adds a 'wp-post-image' class to post thumbnail thumbnails |
|
582 * Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to |
|
583 * dynamically add/remove itself so as to only filter post thumbnail thumbnails |
|
584 * |
|
585 * @author Mark Jaquith |
|
586 * @since 2.9.0 |
|
587 * @param array $attr Attributes including src, class, alt, title |
|
588 * @return array |
|
589 */ |
|
590 function _wp_post_thumbnail_class_filter( $attr ) { |
|
591 $attr['class'] .= ' wp-post-image'; |
|
592 return $attr; |
|
593 } |
|
594 |
|
595 /** |
|
596 * Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter |
|
597 * |
|
598 * @author Mark Jaquith |
|
599 * @since 2.9.0 |
|
600 */ |
|
601 function _wp_post_thumbnail_class_filter_add( $attr ) { |
|
602 add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); |
|
603 } |
|
604 |
|
605 /** |
|
606 * Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter |
|
607 * |
|
608 * @author Mark Jaquith |
|
609 * @since 2.9.0 |
|
610 */ |
|
611 function _wp_post_thumbnail_class_filter_remove( $attr ) { |
|
612 remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); |
556 } |
613 } |
557 |
614 |
558 add_shortcode('wp_caption', 'img_caption_shortcode'); |
615 add_shortcode('wp_caption', 'img_caption_shortcode'); |
559 add_shortcode('caption', 'img_caption_shortcode'); |
616 add_shortcode('caption', 'img_caption_shortcode'); |
560 |
617 |
791 $taxonomies = array_merge($taxonomies, $taxes); |
867 $taxonomies = array_merge($taxonomies, $taxes); |
792 |
868 |
793 return array_unique($taxonomies); |
869 return array_unique($taxonomies); |
794 } |
870 } |
795 |
871 |
|
872 /** |
|
873 * Check if the installed version of GD supports particular image type |
|
874 * |
|
875 * @since 2.9.0 |
|
876 * |
|
877 * @param $mime_type string |
|
878 * @return bool |
|
879 */ |
|
880 function gd_edit_image_support($mime_type) { |
|
881 if ( function_exists('imagetypes') ) { |
|
882 switch( $mime_type ) { |
|
883 case 'image/jpeg': |
|
884 return (imagetypes() & IMG_JPG) != 0; |
|
885 case 'image/png': |
|
886 return (imagetypes() & IMG_PNG) != 0; |
|
887 case 'image/gif': |
|
888 return (imagetypes() & IMG_GIF) != 0; |
|
889 } |
|
890 } else { |
|
891 switch( $mime_type ) { |
|
892 case 'image/jpeg': |
|
893 return function_exists('imagecreatefromjpeg'); |
|
894 case 'image/png': |
|
895 return function_exists('imagecreatefrompng'); |
|
896 case 'image/gif': |
|
897 return function_exists('imagecreatefromgif'); |
|
898 } |
|
899 } |
|
900 return false; |
|
901 } |
|
902 |
|
903 /** |
|
904 * Create new GD image resource with transparency support |
|
905 * |
|
906 * @since 2.9.0 |
|
907 * |
|
908 * @param $width |
|
909 * @param $height |
|
910 * @return image resource |
|
911 */ |
|
912 function wp_imagecreatetruecolor($width, $height) { |
|
913 $img = imagecreatetruecolor($width, $height); |
|
914 if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) { |
|
915 imagealphablending($img, false); |
|
916 imagesavealpha($img, true); |
|
917 } |
|
918 return $img; |
|
919 } |
|
920 |
|
921 /** |
|
922 * API for easily embedding rich media such as videos and images into content. |
|
923 * |
|
924 * @package WordPress |
|
925 * @subpackage Embed |
|
926 * @since 2.9.0 |
|
927 */ |
|
928 class WP_Embed { |
|
929 var $handlers = array(); |
|
930 var $post_ID; |
|
931 var $usecache = true; |
|
932 var $linkifunknown = true; |
|
933 |
|
934 /** |
|
935 * PHP4 constructor |
|
936 */ |
|
937 function WP_Embed() { |
|
938 return $this->__construct(); |
|
939 } |
|
940 |
|
941 /** |
|
942 * PHP5 constructor |
|
943 */ |
|
944 function __construct() { |
|
945 // Hack to get the [embed] shortcode to run before wpautop() |
|
946 add_filter( 'the_content', array(&$this, 'run_shortcode'), 8 ); |
|
947 |
|
948 // Attempts to embed all URLs in a post |
|
949 if ( get_option('embed_autourls') ) |
|
950 add_filter( 'the_content', array(&$this, 'autoembed'), 8 ); |
|
951 |
|
952 // After a post is saved, invalidate the oEmbed cache |
|
953 add_action( 'save_post', array(&$this, 'delete_oembed_caches') ); |
|
954 |
|
955 // After a post is saved, cache oEmbed items via AJAX |
|
956 add_action( 'edit_form_advanced', array(&$this, 'maybe_run_ajax_cache') ); |
|
957 } |
|
958 |
|
959 /** |
|
960 * Process the [embed] shortcode. |
|
961 * |
|
962 * Since the [embed] shortcode needs to be run earlier than other shortcodes, |
|
963 * this function removes all existing shortcodes, registers the [embed] shortcode, |
|
964 * calls {@link do_shortcode()}, and then re-registers the old shortcodes. |
|
965 * |
|
966 * @uses $shortcode_tags |
|
967 * @uses remove_all_shortcodes() |
|
968 * @uses add_shortcode() |
|
969 * @uses do_shortcode() |
|
970 * |
|
971 * @param string $content Content to parse |
|
972 * @return string Content with shortcode parsed |
|
973 */ |
|
974 function run_shortcode( $content ) { |
|
975 global $shortcode_tags; |
|
976 |
|
977 // Backup current registered shortcodes and clear them all out |
|
978 $orig_shortcode_tags = $shortcode_tags; |
|
979 remove_all_shortcodes(); |
|
980 |
|
981 add_shortcode( 'embed', array(&$this, 'shortcode') ); |
|
982 |
|
983 // Do the shortcode (only the [embed] one is registered) |
|
984 $content = do_shortcode( $content ); |
|
985 |
|
986 // Put the original shortcodes back |
|
987 $shortcode_tags = $orig_shortcode_tags; |
|
988 |
|
989 return $content; |
|
990 } |
|
991 |
|
992 /** |
|
993 * If a post/page was saved, then output Javascript to make |
|
994 * an AJAX request that will call WP_Embed::cache_oembed(). |
|
995 */ |
|
996 function maybe_run_ajax_cache() { |
|
997 global $post_ID; |
|
998 |
|
999 if ( empty($post_ID) || empty($_GET['message']) || 1 != $_GET['message'] ) |
|
1000 return; |
|
1001 |
796 ?> |
1002 ?> |
|
1003 <script type="text/javascript"> |
|
1004 /* <![CDATA[ */ |
|
1005 jQuery(document).ready(function($){ |
|
1006 $.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post_ID ); ?>"); |
|
1007 }); |
|
1008 /* ]]> */ |
|
1009 </script> |
|
1010 <?php |
|
1011 } |
|
1012 |
|
1013 /** |
|
1014 * Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead. |
|
1015 * This function should probably also only be used for sites that do not support oEmbed. |
|
1016 * |
|
1017 * @param string $id An internal ID/name for the handler. Needs to be unique. |
|
1018 * @param string $regex The regex that will be used to see if this handler should be used for a URL. |
|
1019 * @param callback $callback The callback function that will be called if the regex is matched. |
|
1020 * @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. |
|
1021 */ |
|
1022 function register_handler( $id, $regex, $callback, $priority = 10 ) { |
|
1023 $this->handlers[$priority][$id] = array( |
|
1024 'regex' => $regex, |
|
1025 'callback' => $callback, |
|
1026 ); |
|
1027 } |
|
1028 |
|
1029 /** |
|
1030 * Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead. |
|
1031 * |
|
1032 * @param string $id The handler ID that should be removed. |
|
1033 * @param int $priority Optional. The priority of the handler to be removed (default: 10). |
|
1034 */ |
|
1035 function unregister_handler( $id, $priority = 10 ) { |
|
1036 if ( isset($this->handlers[$priority][$id]) ) |
|
1037 unset($this->handlers[$priority][$id]); |
|
1038 } |
|
1039 |
|
1040 /** |
|
1041 * The {@link do_shortcode()} callback function. |
|
1042 * |
|
1043 * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers. |
|
1044 * If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class. |
|
1045 * |
|
1046 * @uses wp_oembed_get() |
|
1047 * @uses wp_parse_args() |
|
1048 * @uses wp_embed_defaults() |
|
1049 * @uses WP_Embed::maybe_make_link() |
|
1050 * @uses get_option() |
|
1051 * @uses current_user_can() |
|
1052 * @uses wp_cache_get() |
|
1053 * @uses wp_cache_set() |
|
1054 * @uses get_post_meta() |
|
1055 * @uses update_post_meta() |
|
1056 * |
|
1057 * @param array $attr Shortcode attributes. |
|
1058 * @param string $url The URL attempting to be embeded. |
|
1059 * @return string The embed HTML on success, otherwise the original URL. |
|
1060 */ |
|
1061 function shortcode( $attr, $url = '' ) { |
|
1062 global $post; |
|
1063 |
|
1064 if ( empty($url) ) |
|
1065 return ''; |
|
1066 |
|
1067 $rawattr = $attr; |
|
1068 $attr = wp_parse_args( $attr, wp_embed_defaults() ); |
|
1069 |
|
1070 // Look for known internal handlers |
|
1071 ksort( $this->handlers ); |
|
1072 foreach ( $this->handlers as $priority => $handlers ) { |
|
1073 foreach ( $handlers as $id => $handler ) { |
|
1074 if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) { |
|
1075 if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) |
|
1076 return apply_filters( 'embed_handler_html', $return, $url, $attr ); |
|
1077 } |
|
1078 } |
|
1079 } |
|
1080 |
|
1081 $post_ID = ( !empty($post->ID) ) ? $post->ID : null; |
|
1082 if ( !empty($this->post_ID) ) // Potentially set by WP_Embed::cache_oembed() |
|
1083 $post_ID = $this->post_ID; |
|
1084 |
|
1085 // Unknown URL format. Let oEmbed have a go. |
|
1086 if ( $post_ID ) { |
|
1087 |
|
1088 // Check for a cached result (stored in the post meta) |
|
1089 $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); |
|
1090 if ( $this->usecache ) { |
|
1091 $cache = get_post_meta( $post_ID, $cachekey, true ); |
|
1092 |
|
1093 // Failures are cached |
|
1094 if ( '{{unknown}}' === $cache ) |
|
1095 return $this->maybe_make_link( $url ); |
|
1096 |
|
1097 if ( !empty($cache) ) |
|
1098 return apply_filters( 'embed_oembed_html', $cache, $url, $attr ); |
|
1099 } |
|
1100 |
|
1101 // Use oEmbed to get the HTML |
|
1102 $attr['discover'] = ( apply_filters('embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) ) ? true : false; |
|
1103 $html = wp_oembed_get( $url, $attr ); |
|
1104 |
|
1105 // Cache the result |
|
1106 $cache = ( $html ) ? $html : '{{unknown}}'; |
|
1107 update_post_meta( $post_ID, $cachekey, $cache ); |
|
1108 |
|
1109 // If there was a result, return it |
|
1110 if ( $html ) |
|
1111 return apply_filters( 'embed_oembed_html', $html, $url, $attr ); |
|
1112 } |
|
1113 |
|
1114 // Still unknown |
|
1115 return $this->maybe_make_link( $url ); |
|
1116 } |
|
1117 |
|
1118 /** |
|
1119 * Delete all oEmbed caches. |
|
1120 * |
|
1121 * @param int $post_ID Post ID to delete the caches for. |
|
1122 */ |
|
1123 function delete_oembed_caches( $post_ID ) { |
|
1124 $post_metas = get_post_custom_keys( $post_ID ); |
|
1125 if ( empty($post_metas) ) |
|
1126 return; |
|
1127 |
|
1128 foreach( $post_metas as $post_meta_key ) { |
|
1129 if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) |
|
1130 delete_post_meta( $post_ID, $post_meta_key ); |
|
1131 } |
|
1132 } |
|
1133 |
|
1134 /** |
|
1135 * Triggers a caching of all oEmbed results. |
|
1136 * |
|
1137 * @param int $post_ID Post ID to do the caching for. |
|
1138 */ |
|
1139 function cache_oembed( $post_ID ) { |
|
1140 $post = get_post( $post_ID ); |
|
1141 |
|
1142 if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) ) |
|
1143 return; |
|
1144 |
|
1145 // Trigger a caching |
|
1146 if ( !empty($post->post_content) ) { |
|
1147 $this->post_ID = $post->ID; |
|
1148 $this->usecache = false; |
|
1149 |
|
1150 $content = $this->run_shortcode( $post->post_content ); |
|
1151 if ( get_option('embed_autourls') ) |
|
1152 $this->autoembed( $content ); |
|
1153 |
|
1154 $this->usecache = true; |
|
1155 } |
|
1156 } |
|
1157 |
|
1158 /** |
|
1159 * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding. |
|
1160 * |
|
1161 * @uses WP_Embed::autoembed_callback() |
|
1162 * |
|
1163 * @param string $content The content to be searched. |
|
1164 * @return string Potentially modified $content. |
|
1165 */ |
|
1166 function autoembed( $content ) { |
|
1167 return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array(&$this, 'autoembed_callback'), $content ); |
|
1168 } |
|
1169 |
|
1170 /** |
|
1171 * Callback function for {@link WP_Embed::autoembed()}. |
|
1172 * |
|
1173 * @uses WP_Embed::shortcode() |
|
1174 * |
|
1175 * @param array $match A regex match array. |
|
1176 * @return string The embed HTML on success, otherwise the original URL. |
|
1177 */ |
|
1178 function autoembed_callback( $match ) { |
|
1179 $oldval = $this->linkifunknown; |
|
1180 $this->linkifunknown = false; |
|
1181 $return = $this->shortcode( array(), $match[1] ); |
|
1182 $this->linkifunknown = $oldval; |
|
1183 |
|
1184 return "\n$return\n"; |
|
1185 } |
|
1186 |
|
1187 /** |
|
1188 * Conditionally makes a hyperlink based on an internal class variable. |
|
1189 * |
|
1190 * @param string $url URL to potentially be linked. |
|
1191 * @return string Linked URL or the original URL. |
|
1192 */ |
|
1193 function maybe_make_link( $url ) { |
|
1194 $output = ( $this->linkifunknown ) ? '<a href="' . esc_attr($url) . '">' . esc_html($url) . '</a>' : $url; |
|
1195 return apply_filters( 'embed_maybe_make_link', $output, $url ); |
|
1196 } |
|
1197 } |
|
1198 $wp_embed = new WP_Embed(); |
|
1199 |
|
1200 /** |
|
1201 * Register an embed handler. This function should probably only be used for sites that do not support oEmbed. |
|
1202 * |
|
1203 * @since 2.9.0 |
|
1204 * @see WP_Embed::register_handler() |
|
1205 */ |
|
1206 function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) { |
|
1207 global $wp_embed; |
|
1208 $wp_embed->register_handler( $id, $regex, $callback, $priority ); |
|
1209 } |
|
1210 |
|
1211 /** |
|
1212 * Unregister a previously registered embed handler. |
|
1213 * |
|
1214 * @since 2.9.0 |
|
1215 * @see WP_Embed::unregister_handler() |
|
1216 */ |
|
1217 function wp_embed_unregister_handler( $id, $priority = 10 ) { |
|
1218 global $wp_embed; |
|
1219 $wp_embed->unregister_handler( $id, $priority ); |
|
1220 } |
|
1221 |
|
1222 /** |
|
1223 * Create default array of embed parameters. |
|
1224 * |
|
1225 * @since 2.9.0 |
|
1226 * |
|
1227 * @return array Default embed parameters. |
|
1228 */ |
|
1229 function wp_embed_defaults() { |
|
1230 if ( !empty($GLOBALS['content_width']) ) |
|
1231 $theme_width = (int) $GLOBALS['content_width']; |
|
1232 |
|
1233 $width = get_option('embed_size_w'); |
|
1234 |
|
1235 if ( !$width && !empty($theme_width) ) |
|
1236 $width = $theme_width; |
|
1237 |
|
1238 if ( !$width ) |
|
1239 $width = 500; |
|
1240 |
|
1241 return apply_filters( 'embed_defaults', array( |
|
1242 'width' => $width, |
|
1243 'height' => 700, |
|
1244 ) ); |
|
1245 } |
|
1246 |
|
1247 /** |
|
1248 * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. |
|
1249 * |
|
1250 * @since 2.9.0 |
|
1251 * @uses wp_constrain_dimensions() This function passes the widths and the heights. |
|
1252 * |
|
1253 * @param int $example_width The width of an example embed. |
|
1254 * @param int $example_height The height of an example embed. |
|
1255 * @param int $max_width The maximum allowed width. |
|
1256 * @param int $max_height The maximum allowed height. |
|
1257 * @return array The maximum possible width and height based on the example ratio. |
|
1258 */ |
|
1259 function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) { |
|
1260 $example_width = (int) $example_width; |
|
1261 $example_height = (int) $example_height; |
|
1262 $max_width = (int) $max_width; |
|
1263 $max_height = (int) $max_height; |
|
1264 |
|
1265 return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height ); |
|
1266 } |
|
1267 |
|
1268 /** |
|
1269 * Attempts to fetch the embed HTML for a provided URL using oEmbed. |
|
1270 * |
|
1271 * @since 2.9.0 |
|
1272 * @see WP_oEmbed |
|
1273 * |
|
1274 * @uses _wp_oembed_get_object() |
|
1275 * @uses WP_oEmbed::get_html() |
|
1276 * |
|
1277 * @param string $url The URL that should be embeded. |
|
1278 * @param array $args Addtional arguments and parameters. |
|
1279 * @return string The original URL on failure or the embed HTML on success. |
|
1280 */ |
|
1281 function wp_oembed_get( $url, $args = '' ) { |
|
1282 require_once( 'class-oembed.php' ); |
|
1283 $oembed = _wp_oembed_get_object(); |
|
1284 return $oembed->get_html( $url, $args ); |
|
1285 } |
|
1286 |
|
1287 /** |
|
1288 * Adds a URL format and oEmbed provider URL pair. |
|
1289 * |
|
1290 * @since 2.9.0 |
|
1291 * @see WP_oEmbed |
|
1292 * |
|
1293 * @uses _wp_oembed_get_object() |
|
1294 * |
|
1295 * @param string $format The format of URL that this provider can handle. You can use asterisks as wildcards. |
|
1296 * @param string $provider The URL to the oEmbed provider. |
|
1297 * @param boolean $regex Whether the $format parameter is in a regex format or not. |
|
1298 */ |
|
1299 function wp_oembed_add_provider( $format, $provider, $regex = false ) { |
|
1300 require_once( 'class-oembed.php' ); |
|
1301 $oembed = _wp_oembed_get_object(); |
|
1302 $oembed->providers[$format] = array( $provider, $regex ); |
|
1303 } |