diff -r c7c34916027a -r 177826044cd9 wp/wp-includes/media.php --- a/wp/wp-includes/media.php Mon Oct 14 18:06:33 2019 +0200 +++ b/wp/wp-includes/media.php Mon Oct 14 18:28:13 2019 +0200 @@ -59,58 +59,53 @@ $_wp_additional_image_sizes = wp_get_additional_image_sizes(); - if ( ! $context ) + if ( ! $context ) { $context = is_admin() ? 'edit' : 'display'; - - if ( is_array($size) ) { - $max_width = $size[0]; - $max_height = $size[1]; } - elseif ( $size == 'thumb' || $size == 'thumbnail' ) { - $max_width = intval(get_option('thumbnail_size_w')); - $max_height = intval(get_option('thumbnail_size_h')); + + if ( is_array( $size ) ) { + $max_width = $size[0]; + $max_height = $size[1]; + } elseif ( $size == 'thumb' || $size == 'thumbnail' ) { + $max_width = intval( get_option( 'thumbnail_size_w' ) ); + $max_height = intval( get_option( 'thumbnail_size_h' ) ); // last chance thumbnail size defaults - if ( !$max_width && !$max_height ) { - $max_width = 128; + if ( ! $max_width && ! $max_height ) { + $max_width = 128; $max_height = 96; } - } - elseif ( $size == 'medium' ) { - $max_width = intval(get_option('medium_size_w')); - $max_height = intval(get_option('medium_size_h')); - - } - elseif ( $size == 'medium_large' ) { - $max_width = intval( get_option( 'medium_large_size_w' ) ); + } elseif ( $size == 'medium' ) { + $max_width = intval( get_option( 'medium_size_w' ) ); + $max_height = intval( get_option( 'medium_size_h' ) ); + + } elseif ( $size == 'medium_large' ) { + $max_width = intval( get_option( 'medium_large_size_w' ) ); $max_height = intval( get_option( 'medium_large_size_h' ) ); if ( intval( $content_width ) > 0 ) { $max_width = min( intval( $content_width ), $max_width ); } - } - elseif ( $size == 'large' ) { + } 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. */ - $max_width = intval(get_option('large_size_w')); - $max_height = intval(get_option('large_size_h')); - if ( intval($content_width) > 0 ) { - $max_width = min( intval($content_width), $max_width ); + $max_width = intval( get_option( 'large_size_w' ) ); + $max_height = intval( get_option( 'large_size_h' ) ); + if ( intval( $content_width ) > 0 ) { + $max_width = min( intval( $content_width ), $max_width ); } } elseif ( ! empty( $_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'] ); + $max_width = intval( $_wp_additional_image_sizes[ $size ]['width'] ); + $max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] ); // Only in admin. Assume that theme authors know what they're doing. if ( intval( $content_width ) > 0 && 'edit' === $context ) { $max_width = min( intval( $content_width ), $max_width ); } - } - // $size == 'full' has no constraint - else { - $max_width = $width; + } else { // $size == 'full' has no constraint + $max_width = $width; $max_height = $height; } @@ -150,10 +145,12 @@ */ function image_hwstring( $width, $height ) { $out = ''; - if ($width) - $out .= 'width="'.intval($width).'" '; - if ($height) - $out .= 'height="'.intval($height).'" '; + if ( $width ) { + $out .= 'width="' . intval( $width ) . '" '; + } + if ( $height ) { + $out .= 'height="' . intval( $height ) . '" '; + } return $out; } @@ -202,48 +199,47 @@ return $out; } - $img_url = wp_get_attachment_url($id); - $meta = wp_get_attachment_metadata($id); - $width = $height = 0; - $is_intermediate = false; - $img_url_basename = wp_basename($img_url); + $img_url = wp_get_attachment_url( $id ); + $meta = wp_get_attachment_metadata( $id ); + $width = $height = 0; + $is_intermediate = false; + $img_url_basename = wp_basename( $img_url ); // If the file isn't an image, attempt to replace its URL with a rendered image from its meta. // Otherwise, a non-image type could be returned. if ( ! $is_image ) { if ( ! empty( $meta['sizes'] ) ) { - $img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url ); + $img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url ); $img_url_basename = $meta['sizes']['full']['file']; - $width = $meta['sizes']['full']['width']; - $height = $meta['sizes']['full']['height']; + $width = $meta['sizes']['full']['width']; + $height = $meta['sizes']['full']['height']; } else { return false; } } // try for a new style intermediate size - if ( $intermediate = image_get_intermediate_size($id, $size) ) { - $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url); - $width = $intermediate['width']; - $height = $intermediate['height']; + if ( $intermediate = image_get_intermediate_size( $id, $size ) ) { + $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url ); + $width = $intermediate['width']; + $height = $intermediate['height']; $is_intermediate = true; - } - elseif ( $size == 'thumbnail' ) { + } elseif ( $size == 'thumbnail' ) { // fall back to the old thumbnail - if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) { - $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); - $width = $info[0]; - $height = $info[1]; + if ( ( $thumb_file = wp_get_attachment_thumb_file( $id ) ) && $info = getimagesize( $thumb_file ) ) { + $img_url = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url ); + $width = $info[0]; + $height = $info[1]; $is_intermediate = true; } } - if ( !$width && !$height && isset( $meta['width'], $meta['height'] ) ) { + if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) { // any other type: use the real image - $width = $meta['width']; + $width = $meta['width']; $height = $meta['height']; } - if ( $img_url) { + if ( $img_url ) { // we have the actual image size, but might need to further constrain it if content_width is narrower list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size ); @@ -269,8 +265,8 @@ * @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 int $width Optional. Image width in pixels. Default 0. + * @param int $height Optional. Image height in pixels. Default 0. * @param bool|array $crop Optional. Whether to crop images to specified width and height or resize. * An array can specify positioning of the crop area. Default false. */ @@ -359,12 +355,12 @@ */ function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { - list( $img_src, $width, $height ) = image_downsize($id, $size); - $hwstring = image_hwstring($width, $height); + list( $img_src, $width, $height ) = image_downsize( $id, $size ); + $hwstring = image_hwstring( $width, $height ); $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; - $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id; + $class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size ) . ' wp-image-' . $id; /** * Filters the value of the attachment's image tag class attribute. @@ -379,7 +375,7 @@ */ $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); - $html = '' . esc_attr($alt) . ''; + $html = '' . esc_attr( $alt ) . ''; /** * Filters the HTML content for the image tag. @@ -412,20 +408,21 @@ * @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 && !$max_height ) + if ( ! $max_width && ! $max_height ) { return array( $current_width, $current_height ); + } $width_ratio = $height_ratio = 1.0; - $did_width = $did_height = false; + $did_width = $did_height = false; if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) { $width_ratio = $max_width / $current_width; - $did_width = true; + $did_width = true; } if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) { $height_ratio = $max_height / $current_height; - $did_height = true; + $did_height = true; } // Calculate the larger/smaller ratios @@ -433,7 +430,7 @@ $larger_ratio = max( $width_ratio, $height_ratio ); 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. + // The larger ratio is too big. It would result in an overflow. $ratio = $smaller_ratio; } else { // The larger ratio fits, and is likely to be a more "snug" fit. @@ -441,8 +438,8 @@ } // Very small dimensions may result in 0, 1 should be the minimum. - $w = max ( 1, (int) round( $current_width * $ratio ) ); - $h = max ( 1, (int) round( $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. @@ -464,10 +461,10 @@ * @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. + * @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 ); } @@ -498,11 +495,13 @@ */ function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) { - if ($orig_w <= 0 || $orig_h <= 0) + if ( $orig_w <= 0 || $orig_h <= 0 ) { return false; + } // at least one of dest_w or dest_h must be specific - if ($dest_w <= 0 && $dest_h <= 0) + if ( $dest_w <= 0 && $dest_h <= 0 ) { return false; + } /** * Filters whether to preempt calculating the image resize dimensions. @@ -521,14 +520,15 @@ * An array can specify positioning of the crop area. Default false. */ $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop ); - if ( null !== $output ) + if ( null !== $output ) { return $output; + } if ( $crop ) { // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h $aspect_ratio = $orig_w / $orig_h; - $new_w = min($dest_w, $orig_w); - $new_h = min($dest_h, $orig_h); + $new_w = min( $dest_w, $orig_w ); + $new_h = min( $dest_h, $orig_h ); if ( ! $new_w ) { $new_w = (int) round( $new_h * $aspect_ratio ); @@ -538,10 +538,10 @@ $new_h = (int) round( $new_w / $aspect_ratio ); } - $size_ratio = max($new_w / $orig_w, $new_h / $orig_h); - - $crop_w = round($new_w / $size_ratio); - $crop_h = round($new_h / $size_ratio); + $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h ); + + $crop_w = round( $new_w / $size_ratio ); + $crop_h = round( $new_h / $size_ratio ); if ( ! is_array( $crop ) || count( $crop ) !== 2 ) { $crop = array( 'center', 'center' ); @@ -606,8 +606,9 @@ if ( $width || $height ) { $editor = wp_get_image_editor( $file ); - if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) + if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) { return false; + } $resized_file = $editor->save(); @@ -637,10 +638,10 @@ */ if ( $source_width > $target_width ) { $constrained_size = wp_constrain_dimensions( $source_width, $source_height, $target_width ); - $expected_size = array( $target_width, $target_height ); + $expected_size = array( $target_width, $target_height ); } else { $constrained_size = wp_constrain_dimensions( $target_width, $target_height, $source_width ); - $expected_size = array( $source_width, $source_height ); + $expected_size = array( $source_width, $source_height ); } // If the image dimensions are within 1px of the expected size, we consider it a match. @@ -686,7 +687,7 @@ * } */ function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { - if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) { + if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) { return false; } @@ -730,11 +731,11 @@ } $data = array_shift( $candidates ); - /* - * When the size requested is smaller than the thumbnail dimensions, we - * fall back to the thumbnail size to maintain backwards compatibility with - * pre 4.6 versions of WordPress. - */ + /* + * When the size requested is smaller than the thumbnail dimensions, we + * fall back to the thumbnail size to maintain backward compatibility with + * pre 4.6 versions of WordPress. + */ } elseif ( ! empty( $imagedata['sizes']['thumbnail'] ) && $imagedata['sizes']['thumbnail']['width'] >= $size[0] && $imagedata['sizes']['thumbnail']['width'] >= $size[1] ) { $data = $imagedata['sizes']['thumbnail']; } else { @@ -755,9 +756,9 @@ // include the full filesystem path of the intermediate file if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) { - $file_url = wp_get_attachment_url($post_id); - $data['path'] = path_join( dirname($imagedata['file']), $data['file'] ); - $data['url'] = path_join( dirname($file_url), $data['file'] ); + $file_url = wp_get_attachment_url( $post_id ); + $data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] ); + $data['url'] = path_join( dirname( $file_url ), $data['file'] ); } /** @@ -785,7 +786,7 @@ */ function get_intermediate_image_sizes() { $_wp_additional_image_sizes = wp_get_additional_image_sizes(); - $image_sizes = array('thumbnail', 'medium', 'medium_large', 'large'); // Standard sizes + $image_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' ); // Standard sizes if ( ! empty( $_wp_additional_image_sizes ) ) { $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) ); } @@ -829,7 +830,7 @@ /** 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 ); + $src_file = $icon_dir . '/' . wp_basename( $src ); @list( $width, $height ) = getimagesize( $src_file ); } @@ -868,21 +869,21 @@ * @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); +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); - $size_class = $size; + $hwstring = image_hwstring( $width, $height ); + $size_class = $size; if ( is_array( $size_class ) ) { $size_class = join( 'x', $size_class ); } - $attachment = get_post($attachment_id); + $attachment = get_post( $attachment_id ); $default_attr = array( - 'src' => $src, - 'class' => "attachment-$size_class size-$size_class", - 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), + 'src' => $src, + 'class' => "attachment-$size_class size-$size_class", + 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), ); $attr = wp_parse_args( $attr, $default_attr ); @@ -893,8 +894,8 @@ if ( is_array( $image_meta ) ) { $size_array = array( absint( $width ), absint( $height ) ); - $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); - $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); + $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); + $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { $attr['srcset'] = $srcset; @@ -918,7 +919,7 @@ */ $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); $attr = array_map( 'esc_attr', $attr ); - $html = rtrim(" $value ) { $html .= " $name=" . '"' . $value . '"'; } @@ -988,10 +989,10 @@ absint( $image_meta['width'] ), absint( $image_meta['height'] ), ); - } elseif ( ! empty( $image_meta['sizes'][$size_name] ) ) { + } elseif ( ! empty( $image_meta['sizes'][ $size_name ] ) ) { return array( - absint( $image_meta['sizes'][$size_name]['width'] ), - absint( $image_meta['sizes'][$size_name]['height'] ), + absint( $image_meta['sizes'][ $size_name ]['width'] ), + absint( $image_meta['sizes'][ $size_name ]['height'] ), ); } @@ -1021,10 +1022,10 @@ $image_meta = wp_get_attachment_metadata( $attachment_id ); } - $image_src = $image[0]; + $image_src = $image[0]; $size_array = array( absint( $image[1] ), - absint( $image[2] ) + absint( $image[2] ), ); return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); @@ -1061,7 +1062,7 @@ $image_sizes = $image_meta['sizes']; // Get the width and height of the image. - $image_width = (int) $size_array[0]; + $image_width = (int) $size_array[0]; $image_height = (int) $size_array[1]; // Bail early if error/no width. @@ -1093,7 +1094,7 @@ $dirname = trailingslashit( $dirname ); } - $upload_dir = wp_get_upload_dir(); + $upload_dir = wp_get_upload_dir(); $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname; /* @@ -1239,10 +1240,10 @@ $image_meta = wp_get_attachment_metadata( $attachment_id ); } - $image_src = $image[0]; + $image_src = $image[0]; $size_array = array( absint( $image[1] ), - absint( $image[2] ) + absint( $image[2] ), ); return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); @@ -1319,7 +1320,7 @@ $selected_images = $attachment_ids = array(); - foreach( $matches[0] as $image ) { + foreach ( $matches[0] as $image ) { if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && ( $attachment_id = absint( $class_id[1] ) ) ) { @@ -1343,7 +1344,7 @@ foreach ( $selected_images as $image => $attachment_id ) { $image_meta = wp_get_attachment_metadata( $attachment_id ); - $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content ); + $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content ); } return $content; @@ -1368,7 +1369,7 @@ return $image; } - $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; + $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; list( $image_src ) = explode( '?', $image_src ); // Return early if we couldn't get the image source. @@ -1383,7 +1384,7 @@ return $image; } - $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; + $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; if ( ! $width || ! $height ) { @@ -1394,12 +1395,12 @@ $image_filename = wp_basename( $image_src ); if ( $image_filename === wp_basename( $image_meta['file'] ) ) { - $width = (int) $image_meta['width']; + $width = (int) $image_meta['width']; $height = (int) $image_meta['height']; } else { - foreach( $image_meta['sizes'] as $image_size_data ) { + foreach ( $image_meta['sizes'] as $image_size_data ) { if ( $image_filename === $image_size_data['file'] ) { - $width = (int) $image_size_data['width']; + $width = (int) $image_size_data['width']; $height = (int) $image_size_data['height']; break; } @@ -1412,7 +1413,7 @@ } $size_array = array( $width, $height ); - $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); + $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); if ( $srcset ) { // Check if there is already a 'sizes' attribute. @@ -1481,8 +1482,8 @@ remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); } -add_shortcode('wp_caption', 'img_caption_shortcode'); -add_shortcode('caption', 'img_caption_shortcode'); +add_shortcode( 'wp_caption', 'img_caption_shortcode' ); +add_shortcode( 'caption', 'img_caption_shortcode' ); /** * Builds the Caption shortcode output. @@ -1491,20 +1492,23 @@ * filter is {@see 'img_caption_shortcode'} and passes an empty string, the attr * parameter and the content parameter values. * - * The supported attributes for the shortcode are 'id', 'align', 'width', and - * 'caption'. + * The supported attributes for the shortcode are 'id', 'caption_id', 'align', + * 'width', 'caption', and 'class'. * * @since 2.6.0 + * @since 3.9.0 The `class` attribute was added. + * @since 5.1.0 The `caption_id` attribute was added. * * @param array $attr { * Attributes of the caption shortcode. * - * @type string $id ID of the div element for the caption. - * @type string $align Class name that aligns the caption. Default 'alignnone'. Accepts 'alignleft', - * 'aligncenter', alignright', 'alignnone'. - * @type int $width The width of the caption, in pixels. - * @type string $caption The caption text. - * @type string $class Additional class name(s) added to the caption container. + * @type string $id ID of the image and caption container element, i.e. `
` or `
`. + * @type string $caption_id ID of the caption element, i.e. `
` or `

`. + * @type string $align Class name that aligns the caption. Default 'alignnone'. Accepts 'alignleft', + * 'aligncenter', alignright', 'alignnone'. + * @type int $width The width of the caption, in pixels. + * @type string $caption The caption text. + * @type string $class Additional class name(s) added to the caption container. * } * @param string $content Shortcode content. * @return string HTML content to display the caption. @@ -1513,7 +1517,7 @@ // New-style shortcode with the caption inside the shortcode with the link and image tags. if ( ! isset( $attr['caption'] ) ) { if ( preg_match( '#((?:]+>\s*)?]+>(?:\s*)?)(.*)#is', $content, $matches ) ) { - $content = $matches[1]; + $content = $matches[1]; $attr['caption'] = trim( $matches[2] ); } } elseif ( strpos( $attr['caption'], '<' ) !== false ) { @@ -1535,23 +1539,45 @@ * @param string $content The image element, possibly wrapped in a hyperlink. */ $output = apply_filters( 'img_caption_shortcode', '', $attr, $content ); - if ( $output != '' ) + if ( $output != '' ) { return $output; - - $atts = shortcode_atts( array( - 'id' => '', - 'align' => 'alignnone', - 'width' => '', - 'caption' => '', - 'class' => '', - ), $attr, 'caption' ); + } + + $atts = shortcode_atts( + array( + 'id' => '', + 'caption_id' => '', + 'align' => 'alignnone', + 'width' => '', + 'caption' => '', + 'class' => '', + ), + $attr, + 'caption' + ); $atts['width'] = (int) $atts['width']; - if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) + if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) { return $content; - - if ( ! empty( $atts['id'] ) ) - $atts['id'] = 'id="' . esc_attr( sanitize_html_class( $atts['id'] ) ) . '" '; + } + + $id = $caption_id = $describedby = ''; + + if ( $atts['id'] ) { + $atts['id'] = sanitize_html_class( $atts['id'] ); + $id = 'id="' . esc_attr( $atts['id'] ) . '" '; + } + + if ( $atts['caption_id'] ) { + $atts['caption_id'] = sanitize_html_class( $atts['caption_id'] ); + } elseif ( $atts['id'] ) { + $atts['caption_id'] = 'caption-' . str_replace( '_', '-', $atts['id'] ); + } + + if ( $atts['caption_id'] ) { + $caption_id = 'id="' . esc_attr( $atts['caption_id'] ) . '" '; + $describedby = 'aria-describedby="' . esc_attr( $atts['caption_id'] ) . '" '; + } $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] ); @@ -1582,17 +1608,38 @@ } if ( $html5 ) { - $html = '

' - . do_shortcode( $content ) . '
' . $atts['caption'] . '
'; + $html = sprintf( + '
%s%s
', + $id, + $describedby, + $style, + esc_attr( $class ), + do_shortcode( $content ), + sprintf( + '
%s
', + $caption_id, + $atts['caption'] + ) + ); } else { - $html = '
' - . do_shortcode( $content ) . '

' . $atts['caption'] . '

'; + $html = sprintf( + '
%s%s
', + $id, + $style, + esc_attr( $class ), + str_replace( '%s

', + $caption_id, + $atts['caption'] + ) + ); } return $html; } -add_shortcode('gallery', 'gallery_shortcode'); +add_shortcode( 'gallery', 'gallery_shortcode' ); /** * Builds the Gallery shortcode output. @@ -1663,33 +1710,65 @@ } $html5 = current_theme_supports( 'html5', 'gallery' ); - $atts = shortcode_atts( array( - 'order' => 'ASC', - 'orderby' => 'menu_order ID', - 'id' => $post ? $post->ID : 0, - 'itemtag' => $html5 ? 'figure' : 'dl', - 'icontag' => $html5 ? 'div' : 'dt', - 'captiontag' => $html5 ? 'figcaption' : 'dd', - 'columns' => 3, - 'size' => 'thumbnail', - 'include' => '', - 'exclude' => '', - 'link' => '' - ), $attr, 'gallery' ); + $atts = shortcode_atts( + array( + 'order' => 'ASC', + 'orderby' => 'menu_order ID', + 'id' => $post ? $post->ID : 0, + 'itemtag' => $html5 ? 'figure' : 'dl', + 'icontag' => $html5 ? 'div' : 'dt', + 'captiontag' => $html5 ? 'figcaption' : 'dd', + 'columns' => 3, + 'size' => 'thumbnail', + 'include' => '', + 'exclude' => '', + 'link' => '', + ), + $attr, + 'gallery' + ); $id = intval( $atts['id'] ); 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 = 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]; + $attachments[ $val->ID ] = $_attachments[ $key ]; } } 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'] ) ); + $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' => $atts['order'], 'orderby' => $atts['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 ) ) { @@ -1704,9 +1783,9 @@ return $output; } - $itemtag = tag_escape( $atts['itemtag'] ); + $itemtag = tag_escape( $atts['itemtag'] ); $captiontag = tag_escape( $atts['captiontag'] ); - $icontag = tag_escape( $atts['icontag'] ); + $icontag = tag_escape( $atts['icontag'] ); $valid_tags = wp_kses_allowed_html( 'post' ); if ( ! isset( $valid_tags[ $itemtag ] ) ) { $itemtag = 'dl'; @@ -1718,9 +1797,9 @@ $icontag = 'dt'; } - $columns = intval( $atts['columns'] ); - $itemwidth = $columns > 0 ? floor(100/$columns) : 100; - $float = is_rtl() ? 'right' : 'left'; + $columns = intval( $atts['columns'] ); + $itemwidth = $columns > 0 ? floor( 100 / $columns ) : 100; + $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; @@ -1757,7 +1836,7 @@ \n\t\t"; } - $size_class = sanitize_html_class( $atts['size'] ); + $size_class = sanitize_html_class( $atts['size'] ); $gallery_div = "