X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/67f24b02807a1ff7e9d1a97453ed84c404c0af0f..3f5685912e89eb3b0534acd85aa0946b1ca2bbe3:/wp-includes/media.php diff --git a/wp-includes/media.php b/wp-includes/media.php index 22e67819..8f0bf3aa 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -32,7 +32,7 @@ * @return array Width and height of what the result image should resize to. */ function image_constrain_size_for_editor($width, $height, $size = 'medium') { - global $content_width; + global $content_width, $_wp_additional_image_sizes; if ( is_array($size) ) { $max_width = $size[0]; @@ -61,6 +61,11 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium') { $max_height = intval(get_option('large_size_h')); if ( intval($content_width) > 0 ) $max_width = min( intval($content_width), $max_width ); + } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) { + $max_width = intval( $_wp_additional_image_sizes[$size]['width'] ); + $max_height = intval( $_wp_additional_image_sizes[$size]['height'] ); + if ( intval($content_width) > 0 ) + $max_width = min( intval($content_width), $max_width ); } // $size == 'full' has no constraint else { @@ -169,6 +174,21 @@ function image_downsize($id, $size = 'medium') { } +/** + * Registers a new image size + */ +function add_image_size( $name, $width = 0, $height = 0, $crop = FALSE ) { + global $_wp_additional_image_sizes; + $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => !!$crop ); +} + +/** + * Registers an image size for the post thumbnail + */ +function set_post_thumbnail_size( $width = 0, $height = 0, $crop = FALSE ) { + add_image_size( 'post-thumbnail', $width, $height, $crop ); +} + /** * An tag for an image attachment, scaling it down if requested. * @@ -259,7 +279,7 @@ function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, * @param bool $crop Optional, default is false. Whether to crop image or resize. * @return bool|array False, on failure. Returned array matches parameters for imagecopyresampled() PHP function. */ -function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false) { +function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) { if ($orig_w <= 0 || $orig_h <= 0) return false; @@ -272,22 +292,23 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false $aspect_ratio = $orig_w / $orig_h; $new_w = min($dest_w, $orig_w); $new_h = min($dest_h, $orig_h); - if (!$new_w) { + + if ( !$new_w ) { $new_w = intval($new_h * $aspect_ratio); } - if (!$new_h) { + + if ( !$new_h ) { $new_h = intval($new_w / $aspect_ratio); } $size_ratio = max($new_w / $orig_w, $new_h / $orig_h); - $crop_w = ceil($new_w / $size_ratio); - $crop_h = ceil($new_h / $size_ratio); + $crop_w = round($new_w / $size_ratio); + $crop_h = round($new_h / $size_ratio); - $s_x = floor(($orig_w - $crop_w)/2); - $s_y = floor(($orig_h - $crop_h)/2); - } - else { + $s_x = floor( ($orig_w - $crop_w) / 2 ); + $s_y = floor( ($orig_h - $crop_h) / 2 ); + } else { // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box $crop_w = $orig_w; $crop_h = $orig_h; @@ -299,12 +320,12 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false } // if the resulting image would be the same size or larger we don't want to resize it - if ($new_w >= $orig_w && $new_h >= $orig_h) + if ( $new_w >= $orig_w && $new_h >= $orig_h ) return false; // the return array matches the parameters to imagecopyresampled() // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h - return array(0, 0, $s_x, $s_y, $new_w, $new_h, $crop_w, $crop_h); + return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); } @@ -330,28 +351,30 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false * @param int $jpeg_quality Optional, default is 90. Image quality percentage. * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()} */ -function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) { +function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { $image = wp_load_image( $file ); if ( !is_resource( $image ) ) return new WP_Error('error_loading_image', $image); - list($orig_w, $orig_h, $orig_type) = getimagesize( $file ); + $size = @getimagesize( $file ); + if ( !$size ) + return new WP_Error('invalid_image', __('Could not read image size'), $file); + list($orig_w, $orig_h, $orig_type) = $size; + $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop); - if (!$dims) + if ( !$dims ) return $dims; list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; - $newimage = imagecreatetruecolor( $dst_w, $dst_h); - - // preserve PNG transparency - if ( IMAGETYPE_PNG == $orig_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { - imagealphablending( $newimage, false); - imagesavealpha( $newimage, true); - } + $newimage = wp_imagecreatetruecolor( $dst_w, $dst_h ); imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); + // convert from full colors to index colors, like original PNG. + if ( IMAGETYPE_PNG == $orig_type && !imageistruecolor( $image ) ) + imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) ); + // we don't need the original in memory anymore imagedestroy( $image ); @@ -367,18 +390,16 @@ function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_p $dir = $_dest_path; $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; - if ( $orig_type == IMAGETYPE_GIF ) { - if (!imagegif( $newimage, $destfilename ) ) + if ( IMAGETYPE_GIF == $orig_type ) { + if ( !imagegif( $newimage, $destfilename ) ) return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } - elseif ( $orig_type == IMAGETYPE_PNG ) { - if (!imagepng( $newimage, $destfilename ) ) + } elseif ( IMAGETYPE_PNG == $orig_type ) { + if ( !imagepng( $newimage, $destfilename ) ) return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } - else { + } else { // all other formats are converted to jpg $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; - if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) ) + if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) ) return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); } @@ -527,7 +548,7 @@ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = * @param bool $icon Optional, default is false. Whether it is an icon. * @return string HTML img element or empty string on failure. */ -function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) { +function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { $html = ''; $image = wp_get_attachment_image_src($attachment_id, $size, $icon); @@ -537,12 +558,13 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa if ( is_array($size) ) $size = join('x', $size); $attachment =& get_post($attachment_id); - $attr = array( + $default_attr = array( 'src' => $src, 'class' => "attachment-$size", 'alt' => trim(strip_tags( $attachment->post_excerpt )), 'title' => trim(strip_tags( $attachment->post_title )), - ); + ); + $attr = wp_parse_args($attr, $default_attr); $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); $attr = array_map( 'esc_attr', $attr ); $html = rtrim(" 'dt', 'captiontag' => 'dd', 'columns' => 3, - 'size' => 'thumbnail' + 'size' => 'thumbnail', + 'include' => '', + 'exclude' => '' ), $attr)); $id = intval($id); - $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); + if ( 'RAND' == $order ) + $orderby = 'none'; + + if ( !empty($include) ) { + $include = preg_replace( '/[^0-9,]+/', '', $include ); + $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); + + $attachments = array(); + foreach ( $_attachments as $key => $val ) { + $attachments[$val->ID] = $_attachments[$key]; + } + } elseif ( !empty($exclude) ) { + $exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); + $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); + } else { + $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); + } if ( empty($attachments) ) return ''; @@ -656,7 +731,8 @@ function gallery_shortcode($attr) { $captiontag = tag_escape($captiontag); $columns = intval($columns); $itemwidth = $columns > 0 ? floor(100/$columns) : 100; - + $float = $wp_locale->text_direction == 'rtl' ? 'right' : 'left'; + $selector = "gallery-{$instance}"; $output = apply_filters('gallery_style', " @@ -665,7 +741,7 @@ function gallery_shortcode($attr) { margin: auto; } #{$selector} .gallery-item { - float: left; + float: {$float}; margin-top: 10px; text-align: center; width: {$itemwidth}%; } @@ -793,4 +869,435 @@ function get_attachment_taxonomies($attachment) { return array_unique($taxonomies); } +/** + * Check if the installed version of GD supports particular image type + * + * @since 2.9.0 + * + * @param $mime_type string + * @return bool + */ +function gd_edit_image_support($mime_type) { + if ( function_exists('imagetypes') ) { + switch( $mime_type ) { + case 'image/jpeg': + return (imagetypes() & IMG_JPG) != 0; + case 'image/png': + return (imagetypes() & IMG_PNG) != 0; + case 'image/gif': + return (imagetypes() & IMG_GIF) != 0; + } + } else { + switch( $mime_type ) { + case 'image/jpeg': + return function_exists('imagecreatefromjpeg'); + case 'image/png': + return function_exists('imagecreatefrompng'); + case 'image/gif': + return function_exists('imagecreatefromgif'); + } + } + return false; +} + +/** + * Create new GD image resource with transparency support + * + * @since 2.9.0 + * + * @param $width + * @param $height + * @return image resource + */ +function wp_imagecreatetruecolor($width, $height) { + $img = imagecreatetruecolor($width, $height); + if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) { + imagealphablending($img, false); + imagesavealpha($img, true); + } + return $img; +} + +/** + * API for easily embedding rich media such as videos and images into content. + * + * @package WordPress + * @subpackage Embed + * @since 2.9.0 + */ +class WP_Embed { + var $handlers = array(); + var $post_ID; + var $usecache = true; + var $linkifunknown = true; + + /** + * PHP4 constructor + */ + function WP_Embed() { + return $this->__construct(); + } + + /** + * PHP5 constructor + */ + function __construct() { + // Hack to get the [embed] shortcode to run before wpautop() + add_filter( 'the_content', array(&$this, 'run_shortcode'), 8 ); + + // Attempts to embed all URLs in a post + if ( get_option('embed_autourls') ) + add_filter( 'the_content', array(&$this, 'autoembed'), 8 ); + + // After a post is saved, invalidate the oEmbed cache + add_action( 'save_post', array(&$this, 'delete_oembed_caches') ); + + // After a post is saved, cache oEmbed items via AJAX + add_action( 'edit_form_advanced', array(&$this, 'maybe_run_ajax_cache') ); + } + + /** + * Process the [embed] shortcode. + * + * Since the [embed] shortcode needs to be run earlier than other shortcodes, + * this function removes all existing shortcodes, registers the [embed] shortcode, + * calls {@link do_shortcode()}, and then re-registers the old shortcodes. + * + * @uses $shortcode_tags + * @uses remove_all_shortcodes() + * @uses add_shortcode() + * @uses do_shortcode() + * + * @param string $content Content to parse + * @return string Content with shortcode parsed + */ + function run_shortcode( $content ) { + global $shortcode_tags; + + // Backup current registered shortcodes and clear them all out + $orig_shortcode_tags = $shortcode_tags; + remove_all_shortcodes(); + + add_shortcode( 'embed', array(&$this, 'shortcode') ); + + // Do the shortcode (only the [embed] one is registered) + $content = do_shortcode( $content ); + + // Put the original shortcodes back + $shortcode_tags = $orig_shortcode_tags; + + return $content; + } + + /** + * If a post/page was saved, then output Javascript to make + * an AJAX request that will call WP_Embed::cache_oembed(). + */ + function maybe_run_ajax_cache() { + global $post_ID; + + if ( empty($post_ID) || empty($_GET['message']) || 1 != $_GET['message'] ) + return; + ?> + +handlers[$priority][$id] = array( + 'regex' => $regex, + 'callback' => $callback, + ); + } + + /** + * Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead. + * + * @param string $id The handler ID that should be removed. + * @param int $priority Optional. The priority of the handler to be removed (default: 10). + */ + function unregister_handler( $id, $priority = 10 ) { + if ( isset($this->handlers[$priority][$id]) ) + unset($this->handlers[$priority][$id]); + } + + /** + * The {@link do_shortcode()} callback function. + * + * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers. + * If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class. + * + * @uses wp_oembed_get() + * @uses wp_parse_args() + * @uses wp_embed_defaults() + * @uses WP_Embed::maybe_make_link() + * @uses get_option() + * @uses current_user_can() + * @uses wp_cache_get() + * @uses wp_cache_set() + * @uses get_post_meta() + * @uses update_post_meta() + * + * @param array $attr Shortcode attributes. + * @param string $url The URL attempting to be embeded. + * @return string The embed HTML on success, otherwise the original URL. + */ + function shortcode( $attr, $url = '' ) { + global $post; + + if ( empty($url) ) + return ''; + + $rawattr = $attr; + $attr = wp_parse_args( $attr, wp_embed_defaults() ); + + // Look for known internal handlers + ksort( $this->handlers ); + foreach ( $this->handlers as $priority => $handlers ) { + foreach ( $handlers as $id => $handler ) { + if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) { + if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) + return apply_filters( 'embed_handler_html', $return, $url, $attr ); + } + } + } + + $post_ID = ( !empty($post->ID) ) ? $post->ID : null; + if ( !empty($this->post_ID) ) // Potentially set by WP_Embed::cache_oembed() + $post_ID = $this->post_ID; + + // Unknown URL format. Let oEmbed have a go. + if ( $post_ID ) { + + // Check for a cached result (stored in the post meta) + $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); + if ( $this->usecache ) { + $cache = get_post_meta( $post_ID, $cachekey, true ); + + // Failures are cached + if ( '{{unknown}}' === $cache ) + return $this->maybe_make_link( $url ); + + if ( !empty($cache) ) + return apply_filters( 'embed_oembed_html', $cache, $url, $attr ); + } + + // Use oEmbed to get the HTML + $attr['discover'] = ( apply_filters('embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) ) ? true : false; + $html = wp_oembed_get( $url, $attr ); + + // Cache the result + $cache = ( $html ) ? $html : '{{unknown}}'; + update_post_meta( $post_ID, $cachekey, $cache ); + + // If there was a result, return it + if ( $html ) + return apply_filters( 'embed_oembed_html', $html, $url, $attr ); + } + + // Still unknown + return $this->maybe_make_link( $url ); + } + + /** + * Delete all oEmbed caches. + * + * @param int $post_ID Post ID to delete the caches for. + */ + function delete_oembed_caches( $post_ID ) { + $post_metas = get_post_custom_keys( $post_ID ); + if ( empty($post_metas) ) + return; + + foreach( $post_metas as $post_meta_key ) { + if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) + delete_post_meta( $post_ID, $post_meta_key ); + } + } + + /** + * Triggers a caching of all oEmbed results. + * + * @param int $post_ID Post ID to do the caching for. + */ + function cache_oembed( $post_ID ) { + $post = get_post( $post_ID ); + + if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) ) + return; + + // Trigger a caching + if ( !empty($post->post_content) ) { + $this->post_ID = $post->ID; + $this->usecache = false; + + $content = $this->run_shortcode( $post->post_content ); + if ( get_option('embed_autourls') ) + $this->autoembed( $content ); + + $this->usecache = true; + } + } + + /** + * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding. + * + * @uses WP_Embed::autoembed_callback() + * + * @param string $content The content to be searched. + * @return string Potentially modified $content. + */ + function autoembed( $content ) { + return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array(&$this, 'autoembed_callback'), $content ); + } + + /** + * Callback function for {@link WP_Embed::autoembed()}. + * + * @uses WP_Embed::shortcode() + * + * @param array $match A regex match array. + * @return string The embed HTML on success, otherwise the original URL. + */ + function autoembed_callback( $match ) { + $oldval = $this->linkifunknown; + $this->linkifunknown = false; + $return = $this->shortcode( array(), $match[1] ); + $this->linkifunknown = $oldval; + + return "\n$return\n"; + } + + /** + * Conditionally makes a hyperlink based on an internal class variable. + * + * @param string $url URL to potentially be linked. + * @return string Linked URL or the original URL. + */ + function maybe_make_link( $url ) { + $output = ( $this->linkifunknown ) ? '' . esc_html($url) . '' : $url; + return apply_filters( 'embed_maybe_make_link', $output, $url ); + } +} +$wp_embed = new WP_Embed(); + +/** + * Register an embed handler. This function should probably only be used for sites that do not support oEmbed. + * + * @since 2.9.0 + * @see WP_Embed::register_handler() + */ +function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) { + global $wp_embed; + $wp_embed->register_handler( $id, $regex, $callback, $priority ); +} + +/** + * Unregister a previously registered embed handler. + * + * @since 2.9.0 + * @see WP_Embed::unregister_handler() + */ +function wp_embed_unregister_handler( $id, $priority = 10 ) { + global $wp_embed; + $wp_embed->unregister_handler( $id, $priority ); +} + +/** + * Create default array of embed parameters. + * + * @since 2.9.0 + * + * @return array Default embed parameters. + */ +function wp_embed_defaults() { + if ( !empty($GLOBALS['content_width']) ) + $theme_width = (int) $GLOBALS['content_width']; + + $width = get_option('embed_size_w'); + + if ( !$width && !empty($theme_width) ) + $width = $theme_width; + + if ( !$width ) + $width = 500; + + return apply_filters( 'embed_defaults', array( + 'width' => $width, + 'height' => 700, + ) ); +} + +/** + * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. + * + * @since 2.9.0 + * @uses wp_constrain_dimensions() This function passes the widths and the heights. + * + * @param int $example_width The width of an example embed. + * @param int $example_height The height of an example embed. + * @param int $max_width The maximum allowed width. + * @param int $max_height The maximum allowed height. + * @return array The maximum possible width and height based on the example ratio. + */ +function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) { + $example_width = (int) $example_width; + $example_height = (int) $example_height; + $max_width = (int) $max_width; + $max_height = (int) $max_height; + + return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height ); +} + +/** + * Attempts to fetch the embed HTML for a provided URL using oEmbed. + * + * @since 2.9.0 + * @see WP_oEmbed + * + * @uses _wp_oembed_get_object() + * @uses WP_oEmbed::get_html() + * + * @param string $url The URL that should be embeded. + * @param array $args Addtional arguments and parameters. + * @return string The original URL on failure or the embed HTML on success. + */ +function wp_oembed_get( $url, $args = '' ) { + require_once( 'class-oembed.php' ); + $oembed = _wp_oembed_get_object(); + return $oembed->get_html( $url, $args ); +} + +/** + * Adds a URL format and oEmbed provider URL pair. + * + * @since 2.9.0 + * @see WP_oEmbed + * + * @uses _wp_oembed_get_object() + * + * @param string $format The format of URL that this provider can handle. You can use asterisks as wildcards. + * @param string $provider The URL to the oEmbed provider. + * @param boolean $regex Whether the $format parameter is in a regex format or not. + */ +function wp_oembed_add_provider( $format, $provider, $regex = false ) { + require_once( 'class-oembed.php' ); + $oembed = _wp_oembed_get_object(); + $oembed->providers[$format] = array( $provider, $regex ); +}