X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/4713a14935b83517997f3c88f808eb41da55033d..a7cd4c052013b423c6301153f68c7fdbaa2a447b:/wp-includes/media.php?ds=sidebyside diff --git a/wp-includes/media.php b/wp-includes/media.php index d4061170..c7d092d9 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -11,29 +11,30 @@ * * This is so that the image is a better fit for the editor and theme. * - * The $size parameter accepts either an array or a string. The supported string + * The `$size` parameter accepts either an array or a string. The supported string * values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at * 128 width and 96 height in pixels. Also supported for the string value is * 'medium' and 'full'. The 'full' isn't actually supported, but any value other * than the supported will result in the content_width size or 500 if that is * not set. * - * Finally, there is a filter named 'editor_max_image_size', that will be called - * on the calculated array for width and height, respectively. The second + * Finally, there is a filter named {@see 'editor_max_image_size'}, that will be + * called on the calculated array for width and height, respectively. The second * parameter will be the value that was in the $size parameter. The returned * type for the hook is an array with the width as the first element and the * height as the second element. * * @since 2.5.0 - * @uses wp_constrain_dimensions() This function passes the widths and the heights. * - * @param int $width Width of the image - * @param int $height Height of the image - * @param string|array $size Size of what the result image should be. - * @param context Could be 'display' (like in a theme) or 'edit' (like inserting into an editor) + * @param int $width Width of the image in pixels. + * @param int $height Height of the image in pixels. + * @param string|array $size Optional. Size or array of sizes of what the result image + * should be. Accepts any valid image size name. Default 'medium'. + * @param string $context Optional. Could be 'display' (like in a theme) or 'edit' + * (like inserting into an editor). Default null. * @return array Width and height of what the result image should resize to. */ -function image_constrain_size_for_editor($width, $height, $size = 'medium', $context = null ) { +function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) { global $content_width, $_wp_additional_image_sizes; if ( ! $context ) @@ -215,12 +216,13 @@ function image_downsize($id, $size = 'medium') { * * @since 2.9.0 * + * @global array $_wp_additional_image_sizes Associative array of additional image sizes. + * * @param string $name Image size identifier. * @param int $width Image width in pixels. * @param int $height Image height in pixels. * @param bool|array $crop Optional. Whether to crop images to specified height and width or resize. * An array can specify positioning of the crop area. Default false. - * @return bool|array False, if no image was created. Metadata array on success. */ function add_image_size( $name, $width = 0, $height = 0, $crop = false ) { global $_wp_additional_image_sizes; @@ -269,13 +271,13 @@ function remove_image_size( $name ) { * Registers an image size for the post thumbnail. * * @since 2.9.0 + * * @see add_image_size() for details on cropping behavior. * * @param int $width Image width in pixels. * @param int $height Image height in pixels. * @param bool|array $crop Optional. Whether to crop images to specified height and width or resize. * An array can specify positioning of the crop area. Default false. - * @return bool|array False, if no image was created. Metadata array on success. */ function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) { add_image_size( 'post-thumbnail', $width, $height, $crop ); @@ -377,26 +379,33 @@ function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $smaller_ratio = min( $width_ratio, $height_ratio ); $larger_ratio = max( $width_ratio, $height_ratio ); - if ( intval( $current_width * $larger_ratio ) > $max_width || intval( $current_height * $larger_ratio ) > $max_height ) + if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) { // The larger ratio is too big. It would result in an overflow. $ratio = $smaller_ratio; - else + } else { // The larger ratio fits, and is likely to be a more "snug" fit. $ratio = $larger_ratio; + } // Very small dimensions may result in 0, 1 should be the minimum. - $w = max ( 1, intval( $current_width * $ratio ) ); - $h = max ( 1, intval( $current_height * $ratio ) ); + $w = max ( 1, (int) round( $current_width * $ratio ) ); + $h = max ( 1, (int) round( $current_height * $ratio ) ); // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short // We also have issues with recursive calls resulting in an ever-changing result. Constraining to the result of a constraint should yield the original result. // Thus we look for dimensions that are one pixel shy of the max value and bump them up - if ( $did_width && $w == $max_width - 1 ) + + // Note: $did_width means it is possible $smaller_ratio == $width_ratio. + if ( $did_width && $w == $max_width - 1 ) { $w = $max_width; // Round it up - if ( $did_height && $h == $max_height - 1 ) + } + + // Note: $did_height means it is possible $smaller_ratio == $height_ratio. + if ( $did_height && $h == $max_height - 1 ) { $h = $max_height; // Round it up + } - return array( $w, $h ); + return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height ); } /** @@ -457,12 +466,12 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal $new_w = min($dest_w, $orig_w); $new_h = min($dest_h, $orig_h); - if ( !$new_w ) { - $new_w = intval($new_h * $aspect_ratio); + if ( ! $new_w ) { + $new_w = (int) round( $new_h * $aspect_ratio ); } - if ( !$new_h ) { - $new_h = intval($new_w / $aspect_ratio); + if ( ! $new_h ) { + $new_h = (int) round( $new_w / $aspect_ratio ); } $size_ratio = max($new_w / $orig_w, $new_h / $orig_h); @@ -503,8 +512,9 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal } // 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 && $dest_w != $orig_w && $dest_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 @@ -684,12 +694,11 @@ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = * @since 2.5.0 * * @see add_image_size() - * @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions * - * @param int $attachment_id Image attachment ID. - * @param string $size Optional, default is 'thumbnail'. - * @param bool $icon Optional, default is false. Whether it is an icon. - * @param mixed $attr Optional, attributes for the image markup. + * @param int $attachment_id Image attachment ID. + * @param string|array $size Optional. Default 'thumbnail'. + * @param bool $icon Optional. Whether it is an icon. Default false. + * @param string|array $attr Optional. Attributes for the image markup. Default empty string. * @return string HTML img element or empty string on failure. */ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { @@ -699,12 +708,14 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa if ( $image ) { list($src, $width, $height) = $image; $hwstring = image_hwstring($width, $height); - if ( is_array($size) ) - $size = join('x', $size); + $size_class = $size; + if ( is_array( $size_class ) ) { + $size_class = join( 'x', $size_class ); + } $attachment = get_post($attachment_id); $default_attr = array( 'src' => $src, - 'class' => "attachment-$size", + 'class' => "attachment-$size_class", 'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first ); if ( empty($default_attr['alt']) ) @@ -719,10 +730,11 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa * * @since 2.8.0 * - * @param mixed $attr Attributes for the image markup. - * @param int $attachment_id Image attachment ID. + * @param array $attr Attributes for the image markup. + * @param WP_Post $attachment Image attachment post. + * @param string|array $size Requested size. */ - $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); + $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); $attr = array_map( 'esc_attr', $attr ); $html = rtrim(" $value ) { @@ -911,8 +923,9 @@ function gallery_shortcode( $attr ) { if ( ! empty( $attr['ids'] ) ) { // 'ids' is explicitly ordered, unless you specify otherwise. - if ( empty( $attr['orderby'] ) ) + if ( empty( $attr['orderby'] ) ) { $attr['orderby'] = 'post__in'; + } $attr['include'] = $attr['ids']; } @@ -930,18 +943,12 @@ function gallery_shortcode( $attr ) { * @param array $attr Attributes of the gallery shortcode. */ $output = apply_filters( 'post_gallery', '', $attr ); - if ( $output != '' ) + if ( $output != '' ) { return $output; - - // We're trusting author input, so let's at least make sure it looks like a valid orderby statement - if ( isset( $attr['orderby'] ) ) { - $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); - if ( !$attr['orderby'] ) - unset( $attr['orderby'] ); } $html5 = current_theme_supports( 'html5', 'gallery' ); - extract(shortcode_atts(array( + $atts = shortcode_atts( array( 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post ? $post->ID : 0, @@ -953,53 +960,56 @@ function gallery_shortcode( $attr ) { 'include' => '', 'exclude' => '', 'link' => '' - ), $attr, 'gallery')); + ), $attr, 'gallery' ); - $id = intval($id); - if ( 'RAND' == $order ) - $orderby = 'none'; + $id = intval( $atts['id'] ); - if ( !empty($include) ) { - $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); + if ( ! empty( $atts['include'] ) ) { + $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); $attachments = array(); foreach ( $_attachments as $key => $val ) { $attachments[$val->ID] = $_attachments[$key]; } - } elseif ( !empty($exclude) ) { - $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); + } elseif ( ! empty( $atts['exclude'] ) ) { + $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); } else { - $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); + $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); } - if ( empty($attachments) ) + if ( empty( $attachments ) ) { return ''; + } if ( is_feed() ) { $output = "\n"; - foreach ( $attachments as $att_id => $attachment ) - $output .= wp_get_attachment_link($att_id, $size, true) . "\n"; + foreach ( $attachments as $att_id => $attachment ) { + $output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n"; + } return $output; } - $itemtag = tag_escape($itemtag); - $captiontag = tag_escape($captiontag); - $icontag = tag_escape($icontag); + $itemtag = tag_escape( $atts['itemtag'] ); + $captiontag = tag_escape( $atts['captiontag'] ); + $icontag = tag_escape( $atts['icontag'] ); $valid_tags = wp_kses_allowed_html( 'post' ); - if ( ! isset( $valid_tags[ $itemtag ] ) ) + if ( ! isset( $valid_tags[ $itemtag ] ) ) { $itemtag = 'dl'; - if ( ! isset( $valid_tags[ $captiontag ] ) ) + } + if ( ! isset( $valid_tags[ $captiontag ] ) ) { $captiontag = 'dd'; - if ( ! isset( $valid_tags[ $icontag ] ) ) + } + if ( ! isset( $valid_tags[ $icontag ] ) ) { $icontag = 'dt'; + } - $columns = intval($columns); + $columns = intval( $atts['columns'] ); $itemwidth = $columns > 0 ? floor(100/$columns) : 100; $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; - $gallery_style = $gallery_div = ''; + $gallery_style = ''; /** * Filter whether to print default gallery styles. @@ -1032,7 +1042,7 @@ function gallery_shortcode( $attr ) { \n\t\t"; } - $size_class = sanitize_html_class( $size ); + $size_class = sanitize_html_class( $atts['size'] ); $gallery_div = "