X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/874d2a2f468a0d1e69aab49b1fe2d9d79d3e1142..dc1231b7312fbdca99e9e887cc2bb35a28f85cdc:/wp-includes/media.php diff --git a/wp-includes/media.php b/wp-includes/media.php index 5e94cfeb..49d1717c 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -11,29 +11,33 @@ * * 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) + * @global int $content_width + * @global array $_wp_additional_image_sizes + * + * @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 ) @@ -58,10 +62,12 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium', $con // if no width is set, default to the theme content width if available } elseif ( $size == 'large' ) { - // We're inserting a large size image into the editor. If it's a really - // big image we'll scale it down to fit reasonably within the editor - // itself, and within the theme's content width if it's known. The user - // can resize it in the editor if they wish. + /* + * We're inserting a large size image into the editor. If it's a really + * big image we'll scale it down to fit reasonably within the editor + * itself, and within the theme's content width if it's known. The user + * can resize it in the editor if they wish. + */ $max_width = intval(get_option('large_size_w')); $max_height = intval(get_option('large_size_h')); if ( intval($content_width) > 0 ) @@ -108,11 +114,11 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium', $con * * @since 2.5.0 * - * @param int|string $width Optional. Width attribute value. - * @param int|string $height Optional. Height attribute value. + * @param int|string $width Image width in pixels. + * @param int|string $height Image height in pixels. * @return string HTML attributes for width and, or height. */ -function image_hwstring($width, $height) { +function image_hwstring( $width, $height ) { $out = ''; if ($width) $out .= 'width="'.intval($width).'" '; @@ -139,11 +145,12 @@ function image_hwstring($width, $height) { * * @since 2.5.0 * - * @param int $id Attachment ID for image. - * @param array|string $size Optional, default is 'medium'. Size of image, either array or string. - * @return bool|array False on failure, array on success. + * @param int $id Attachment ID for image. + * @param array|string $size Optional. Image size to scale to. Accepts a registered image size + * or flat array of height and width values. Default 'medium'. + * @return false|array False on failure, array on success. */ -function image_downsize($id, $size = 'medium') { +function image_downsize( $id, $size = 'medium' ) { if ( !wp_attachment_is_image($id) ) return false; @@ -215,12 +222,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; @@ -237,6 +245,8 @@ function add_image_size( $name, $width = 0, $height = 0, $crop = false ) { * * @since 3.9.0 * + * @global array $_wp_additional_image_sizes + * * @param string $name The image size to check. * @return bool True if the image size exists, false if not. */ @@ -251,6 +261,8 @@ function has_image_size( $name ) { * * @since 3.9.0 * + * @global array $_wp_additional_image_sizes + * * @param string $name The image size to remove. * @return bool True if the image size was successfully removed, false on failure. */ @@ -269,20 +281,20 @@ 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 ); } /** - * An tag for an image attachment, scaling it down if requested. + * Gets an img tag for an image attachment, scaling it down if requested. * * The filter 'get_image_tag_class' allows for changing the class name for the * image without having to use regular expressions on the HTML content. The @@ -295,14 +307,15 @@ function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) { * * @since 2.5.0 * - * @param int $id Attachment ID. - * @param string $alt Image Description for the alt attribute. - * @param string $title Image Description for the title attribute. - * @param string $align Part of the class name for aligning the image. - * @param string $size Optional. Default is 'medium'. + * @param int $id Attachment ID. + * @param string $alt Image Description for the alt attribute. + * @param string $title Image Description for the title attribute. + * @param string $align Part of the class name for aligning the image. + * @param string|array $size Optional. Registered image size to retrieve a tag for, or flat array + * of height and width values. Default 'medium'. * @return string HTML IMG element for given image attachment */ -function get_image_tag($id, $alt, $title, $align, $size='medium') { +function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { list( $img_src, $width, $height ) = image_downsize($id, $size); $hwstring = image_hwstring($width, $height); @@ -337,27 +350,25 @@ function get_image_tag($id, $alt, $title, $align, $size='medium') { * @param string $align Part of the class name for aligning the image. * @param string $size Optional. Default is 'medium'. */ - $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); - - return $html; + return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); } /** - * Calculates the new dimensions for a downsampled image. + * Calculates the new dimensions for a down-sampled image. * * If either width or height are empty, no constraint is applied on * that dimension. * * @since 2.5.0 * - * @param int $current_width Current width of the image. + * @param int $current_width Current width of the image. * @param int $current_height Current height of the image. - * @param int $max_width Optional. Maximum wanted width. - * @param int $max_height Optional. Maximum wanted height. + * @param int $max_width Optional. Max width in pixels to constrain to. Default 0. + * @param int $max_height Optional. Max height in pixels to constrain to. Default 0. * @return array First item is the width, the second item is the height. */ -function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) { - if ( !$max_width and !$max_height ) +function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) { + if ( !$max_width && !$max_height ) return array( $current_width, $current_height ); $width_ratio = $height_ratio = 1.0; @@ -377,30 +388,48 @@ 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 ); + /** + * Filter dimensions to constrain down-sampled images to. + * + * @since 4.1.0 + * + * @param array $dimensions The image width and height. + * @param int $current_width The current width of the image. + * @param int $current_height The current height of the image. + * @param int $max_width The maximum width permitted. + * @param int $max_height The maximum height permitted. + */ + return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height ); } /** - * Retrieve calculated resize dimensions for use in WP_Image_Editor. + * Retrieves calculated resize dimensions for use in WP_Image_Editor. * * Calculates dimensions and coordinates for a resized image that fits * within a specified width and height. @@ -421,7 +450,7 @@ function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, * @param int $dest_h New height in pixels. * @param bool|array $crop Optional. Whether to crop image to specified height and width or resize. * An array can specify positioning of the crop area. Default false. - * @return bool|array False on failure. Returned array matches parameters for `imagecopyresampled()`. + * @return false|array False on failure. Returned array matches parameters for `imagecopyresampled()`. */ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) { @@ -457,12 +486,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 +532,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 @@ -513,7 +543,7 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal } /** - * Resize an image to make a thumbnail or intermediate size. + * Resizes an image to make a thumbnail or intermediate size. * * The returned array has the file size, the image width, and image height. The * filter 'image_make_intermediate_size' can be used to hook in and change the @@ -521,11 +551,12 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal * * @since 2.5.0 * - * @param string $file File path. - * @param int $width Image width. - * @param int $height Image height. - * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize. - * @return bool|array False, if no image was created. Metadata array on success. + * @param string $file File path. + * @param int $width Image width. + * @param int $height Image height. + * @param bool $crop Optional. Whether to crop image to specified height and width or resize. + * Default false. + * @return false|array False, if no image was created. Metadata array on success. */ function image_make_intermediate_size( $file, $width, $height, $crop = false ) { if ( $width || $height ) { @@ -545,7 +576,7 @@ function image_make_intermediate_size( $file, $width, $height, $crop = false ) { } /** - * Retrieve the image's intermediate size (resized) path, width, and height. + * Retrieves the image's intermediate size (resized) path, width, and height. * * The $size parameter can be an array with the width and height respectively. * If the size matches the 'sizes' metadata array for width and height, then it @@ -564,18 +595,20 @@ function image_make_intermediate_size( $file, $width, $height, $crop = false ) { * browser scale down the image. * * @since 2.5.0 - * @see add_image_size() * - * @param int $post_id Attachment ID for image. - * @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string. - * @return bool|array False on failure or array of file path, width, and height on success. + * @param int $post_id Attachment ID. + * @param array|string $size Optional. Registered image size to retrieve or flat array of height + * and width dimensions. Default 'thumbnail'. + * @return false|array False on failure or array of file path, width, and height on success. */ -function image_get_intermediate_size($post_id, $size='thumbnail') { +function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) ) return false; // get the best one for a specified set of dimensions if ( is_array($size) && !empty($imagedata['sizes']) ) { + $areas = array(); + foreach ( $imagedata['sizes'] as $_size => $data ) { // already cropped to width or height; so use this size if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) { @@ -621,9 +654,13 @@ function image_get_intermediate_size($post_id, $size='thumbnail') { } /** - * Get the available image sizes + * Gets the available intermediate image sizes. + * * @since 3.0.0 - * @return array Returns a filtered array of image size strings + * + * @global array $_wp_additional_image_sizes + * + * @return array Returns a filtered array of image size strings. */ function get_intermediate_image_sizes() { global $_wp_additional_image_sizes; @@ -649,62 +686,75 @@ function get_intermediate_image_sizes() { * * @since 2.5.0 * - * @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. - * @return bool|array Returns an array (url, width, height), or false, if no image is available. + * @param int $attachment_id Image attachment ID. + * @param string|array $size Optional. Registered image size to retrieve the source for or a flat + * array of height and width dimensions. Default 'thumbnail'. + * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. + * @return false|array Returns an array (url, width, height), or false, if no image is available. */ -function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) { - +function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) { // get a thumbnail or intermediate image if there is one - if ( $image = image_downsize($attachment_id, $size) ) - return $image; + $image = image_downsize( $attachment_id, $size ); + if ( ! $image ) { + $src = false; - $src = false; + if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) { + /** This filter is documented in wp-includes/post.php */ + $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); - if ( $icon && $src = wp_mime_type_icon($attachment_id) ) { - /** This filter is documented in wp-includes/post.php */ - $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); - $src_file = $icon_dir . '/' . wp_basename($src); - @list($width, $height) = getimagesize($src_file); + $src_file = $icon_dir . '/' . wp_basename( $src ); + @list( $width, $height ) = getimagesize( $src_file ); + } + + if ( $src && $width && $height ) { + $image = array( $src, $width, $height ); + } } - if ( $src && $width && $height ) - return array( $src, $width, $height ); - return false; + /** + * Filter the image src result. + * + * @since 4.3.0 + * + * @param array|false $image Either array with src, width & height, icon src, or false. + * @param int $attachment_id Image attachment ID. + * @param string|array $size Registered image size to retrieve the source for or a flat + * array of height and width dimensions. Default 'thumbnail'. + * @param bool $icon Whether the image should be treated as an icon. Default false. + */ + return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon ); } /** * Get an HTML img element representing an image attachment * - * While $size will accept an array, it is better to register a size with + * While `$size` will accept an array, it is better to register a size with * add_image_size() so that a cropped version is generated. It's much more * efficient than having to find the closest-sized image and then having the * browser scale down the image. * * @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. Registered image size or flat array of height and width + * dimensions. Default 'thumbnail'. + * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. + * @param string|array $attr Optional. Attributes for the image markup. Default empty. * @return string HTML img element or empty string on failure. */ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { - $html = ''; $image = wp_get_attachment_image_src($attachment_id, $size, $icon); 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 +769,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 ) { @@ -735,13 +786,16 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa } /** - * Adds a 'wp-post-image' class to post thumbnails - * Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to - * dynamically add/remove itself so as to only filter post thumbnails + * Adds a 'wp-post-image' class to post thumbnails. Internal use only. + * + * Uses the 'begin_fetch_post_thumbnail_html' and 'end_fetch_post_thumbnail_html' action hooks to + * dynamically add/remove itself so as to only filter post thumbnails. * + * @ignore * @since 2.9.0 - * @param array $attr Attributes including src, class, alt, title - * @return array + * + * @param array $attr Thumbnail attributes including src, class, alt, title. + * @return array Modified array of attributes including the new 'wp-post-image' class. */ function _wp_post_thumbnail_class_filter( $attr ) { $attr['class'] .= ' wp-post-image'; @@ -749,18 +803,26 @@ function _wp_post_thumbnail_class_filter( $attr ) { } /** - * Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter + * Adds '_wp_post_thumbnail_class_filter' callback to the 'wp_get_attachment_image_attributes' + * filter hook. Internal use only. * + * @ignore * @since 2.9.0 + * + * @param array $attr Thumbnail attributes including src, class, alt, title. */ function _wp_post_thumbnail_class_filter_add( $attr ) { add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); } /** - * Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter + * Removes the '_wp_post_thumbnail_class_filter' callback from the 'wp_get_attachment_image_attributes' + * filter hook. Internal use only. * + * @ignore * @since 2.9.0 + * + * @param array $attr Thumbnail attributes including src, class, alt, title. */ function _wp_post_thumbnail_class_filter_remove( $attr ) { remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); @@ -770,7 +832,7 @@ add_shortcode('wp_caption', 'img_caption_shortcode'); add_shortcode('caption', 'img_caption_shortcode'); /** - * The Caption shortcode. + * Builds the Caption shortcode output. * * Allows a plugin to replace the content that would otherwise be returned. The * filter is 'img_caption_shortcode' and passes an empty string, the attr @@ -781,7 +843,7 @@ add_shortcode('caption', 'img_caption_shortcode'); * * @since 2.6.0 * - * @param array $attr { + * @param array $attr { * Attributes of the caption shortcode. * * @type string $id ID of the div element for the caption. @@ -791,7 +853,7 @@ add_shortcode('caption', 'img_caption_shortcode'); * @type string $caption The caption text. * @type string $class Additional class name(s) added to the caption container. * } - * @param string $content Optional. Shortcode content. + * @param string $content Shortcode content. * @return string HTML content to display the caption. */ function img_caption_shortcode( $attr, $content = null ) { @@ -801,6 +863,8 @@ function img_caption_shortcode( $attr, $content = null ) { $content = $matches[1]; $attr['caption'] = trim( $matches[2] ); } + } elseif ( strpos( $attr['caption'], '<' ) !== false ) { + $attr['caption'] = wp_kses( $attr['caption'], 'post' ); } /** @@ -834,7 +898,7 @@ function img_caption_shortcode( $attr, $content = null ) { return $content; if ( ! empty( $atts['id'] ) ) - $atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" '; + $atts['id'] = 'id="' . esc_attr( sanitize_html_class( $atts['id'] ) ) . '" '; $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] ); @@ -873,13 +937,15 @@ function img_caption_shortcode( $attr, $content = null ) { add_shortcode('gallery', 'gallery_shortcode'); /** - * The Gallery shortcode. + * Builds the Gallery shortcode output. * * This implements the functionality of the Gallery Shortcode for displaying * WordPress images on a post. * * @since 2.5.0 * + * @staticvar int $instance + * * @param array $attr { * Attributes of the gallery shortcode. * @@ -924,25 +990,19 @@ function gallery_shortcode( $attr ) { * the default gallery template. * * @since 2.5.0 + * @since 4.2.0 The `$instance` parameter was added. * * @see gallery_shortcode() * - * @param string $output The gallery output. Default empty. - * @param array $attr Attributes of the gallery shortcode. + * @param string $output The gallery output. Default empty. + * @param array $attr Attributes of the gallery shortcode. + * @param int $instance Unique numeric ID of this gallery shortcode instance. */ - $output = apply_filters( 'post_gallery', '', $attr ); + $output = apply_filters( 'post_gallery', '', $attr, $instance ); 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' ); $atts = shortcode_atts( array( 'order' => 'ASC', @@ -959,9 +1019,6 @@ function gallery_shortcode( $attr ) { ), $attr, 'gallery' ); $id = intval( $atts['id'] ); - if ( 'RAND' == $atts['order'] ) { - $atts['orderby'] = 'none'; - } 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'] ) ); @@ -1049,19 +1106,21 @@ function gallery_shortcode( $attr ) { * * @since 2.5.0 * - * @param string $gallery_style Default gallery shortcode CSS styles. - * @param string $gallery_div Opening HTML div container for the gallery shortcode output. + * @param string $gallery_style Default CSS styles and opening HTML div container + * for the gallery shortcode output. */ $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div ); $i = 0; foreach ( $attachments as $id => $attachment ) { + + $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : ''; if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) { - $image_output = wp_get_attachment_link( $id, $atts['size'], false, false ); + $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr ); } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) { - $image_output = wp_get_attachment_image( $id, $atts['size'], false ); + $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr ); } else { - $image_output = wp_get_attachment_link( $id, $atts['size'], true, false ); + $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr ); } $image_meta = wp_get_attachment_metadata( $id ); @@ -1076,7 +1135,7 @@ function gallery_shortcode( $attr ) { "; if ( $captiontag && trim($attachment->post_excerpt) ) { $output .= " - <{$captiontag} class='wp-caption-text gallery-caption'> + <{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'> " . wptexturize($attachment->post_excerpt) . " "; } @@ -1098,7 +1157,7 @@ function gallery_shortcode( $attr ) { } /** - * Output the templates used by playlists. + * Outputs the templates used by playlists. * * @since 3.9.0 */ @@ -1136,7 +1195,7 @@ function wp_underscore_playlist_templates() { } /** - * Output and enqueue default scripts and styles for playlists. + * Outputs and enqueue default scripts and styles for playlists. * * @since 3.9.0 * @@ -1151,22 +1210,24 @@ function wp_playlist_scripts( $type ) { add_action( 'wp_footer', 'wp_underscore_playlist_templates', 0 ); add_action( 'admin_footer', 'wp_underscore_playlist_templates', 0 ); } -add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' ); /** - * The playlist shortcode. + * Builds the Playlist shortcode output. * * This implements the functionality of the playlist shortcode for displaying * a collection of WordPress audio or video files in a post. * * @since 3.9.0 * + * @global int $content_width + * @staticvar int $instance + * * @param array $attr { * Array of default playlist attributes. * * @type string $type Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'. * @type string $order Designates ascending or descending order of items in the playlist. - * Accepts 'ASC', 'DESC', or 'RAND'. Default 'ASC'. + * Accepts 'ASC', 'DESC'. Default 'ASC'. * @type string $orderby Any column, or columns, to sort the playlist. If $ids are * passed, this defaults to the order of the $ids array ('post__in'). * Otherwise default is 'menu_order ID'. @@ -1209,25 +1270,17 @@ function wp_playlist_shortcode( $attr ) { * of the default playlist output, returning the passed value instead. * * @since 3.9.0 + * @since 4.2.0 The `$instance` parameter was added. * - * @param string $output Playlist output. Default empty. - * @param array $attr An array of shortcode attributes. + * @param string $output Playlist output. Default empty. + * @param array $attr An array of shortcode attributes. + * @param int $instance Unique numeric ID of this playlist shortcode instance. */ - $output = apply_filters( 'post_playlist', '', $attr ); + $output = apply_filters( 'post_playlist', '', $attr, $instance ); 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'] ); - } - $atts = shortcode_atts( array( 'type' => 'audio', 'order' => 'ASC', @@ -1243,9 +1296,6 @@ function wp_playlist_shortcode( $attr ) { ), $attr, 'playlist' ); $id = intval( $atts['id'] ); - if ( 'RAND' == $atts['order'] ) { - $atts['orderby'] = 'none'; - } if ( $atts['type'] !== 'audio' ) { $atts['type'] = 'video'; @@ -1403,7 +1453,7 @@ function wp_playlist_shortcode( $attr ) { } ?> - + ` element. Default empty. * @type string $preload The 'preload' attribute for the `