wp/wp-includes/media.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    15  *
    15  *
    16  * @return array Additional images size data.
    16  * @return array Additional images size data.
    17  */
    17  */
    18 function wp_get_additional_image_sizes() {
    18 function wp_get_additional_image_sizes() {
    19 	global $_wp_additional_image_sizes;
    19 	global $_wp_additional_image_sizes;
       
    20 
    20 	if ( ! $_wp_additional_image_sizes ) {
    21 	if ( ! $_wp_additional_image_sizes ) {
    21 		$_wp_additional_image_sizes = array();
    22 		$_wp_additional_image_sizes = array();
    22 	}
    23 	}
       
    24 
    23 	return $_wp_additional_image_sizes;
    25 	return $_wp_additional_image_sizes;
    24 }
    26 }
    25 
    27 
    26 /**
    28 /**
    27  * Scale down the default size of an image.
    29  * Scale down the default size of an image.
    34  * 'medium', 'medium_large' and 'full'. The 'full' isn't actually supported, but any value other
    36  * 'medium', 'medium_large' and 'full'. The 'full' isn't actually supported, but any value other
    35  * than the supported will result in the content_width size or 500 if that is
    37  * than the supported will result in the content_width size or 500 if that is
    36  * not set.
    38  * not set.
    37  *
    39  *
    38  * Finally, there is a filter named {@see 'editor_max_image_size'}, that will be
    40  * Finally, there is a filter named {@see 'editor_max_image_size'}, that will be
    39  * called on the calculated array for width and height, respectively. The second
    41  * called on the calculated array for width and height, respectively.
    40  * parameter will be the value that was in the $size parameter. The returned
       
    41  * type for the hook is an array with the width as the first element and the
       
    42  * height as the second element.
       
    43  *
    42  *
    44  * @since 2.5.0
    43  * @since 2.5.0
    45  *
    44  *
    46  * @global int   $content_width
    45  * @global int   $content_width
    47  *
    46  *
    50  * @param string|array $size    Optional. Image size. Accepts any valid image size, or an array
    49  * @param string|array $size    Optional. Image size. Accepts any valid image size, or an array
    51  *                              of width and height values in pixels (in that order).
    50  *                              of width and height values in pixels (in that order).
    52  *                              Default 'medium'.
    51  *                              Default 'medium'.
    53  * @param string       $context Optional. Could be 'display' (like in a theme) or 'edit'
    52  * @param string       $context Optional. Could be 'display' (like in a theme) or 'edit'
    54  *                              (like inserting into an editor). Default null.
    53  *                              (like inserting into an editor). Default null.
    55  * @return array Width and height of what the result image should resize to.
    54  * @return int[] {
       
    55  *     An array of width and height values.
       
    56  *
       
    57  *     @type int $0 The maximum width in pixels.
       
    58  *     @type int $1 The maximum height in pixels.
       
    59  * }
    56  */
    60  */
    57 function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
    61 function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
    58 	global $content_width;
    62 	global $content_width;
    59 
    63 
    60 	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
    64 	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
    64 	}
    68 	}
    65 
    69 
    66 	if ( is_array( $size ) ) {
    70 	if ( is_array( $size ) ) {
    67 		$max_width  = $size[0];
    71 		$max_width  = $size[0];
    68 		$max_height = $size[1];
    72 		$max_height = $size[1];
    69 	} elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
    73 	} elseif ( 'thumb' === $size || 'thumbnail' === $size ) {
    70 		$max_width  = intval( get_option( 'thumbnail_size_w' ) );
    74 		$max_width  = intval( get_option( 'thumbnail_size_w' ) );
    71 		$max_height = intval( get_option( 'thumbnail_size_h' ) );
    75 		$max_height = intval( get_option( 'thumbnail_size_h' ) );
    72 		// last chance thumbnail size defaults
    76 		// Last chance thumbnail size defaults.
    73 		if ( ! $max_width && ! $max_height ) {
    77 		if ( ! $max_width && ! $max_height ) {
    74 			$max_width  = 128;
    78 			$max_width  = 128;
    75 			$max_height = 96;
    79 			$max_height = 96;
    76 		}
    80 		}
    77 	} elseif ( $size == 'medium' ) {
    81 	} elseif ( 'medium' === $size ) {
    78 		$max_width  = intval( get_option( 'medium_size_w' ) );
    82 		$max_width  = intval( get_option( 'medium_size_w' ) );
    79 		$max_height = intval( get_option( 'medium_size_h' ) );
    83 		$max_height = intval( get_option( 'medium_size_h' ) );
    80 
    84 
    81 	} elseif ( $size == 'medium_large' ) {
    85 	} elseif ( 'medium_large' === $size ) {
    82 		$max_width  = intval( get_option( 'medium_large_size_w' ) );
    86 		$max_width  = intval( get_option( 'medium_large_size_w' ) );
    83 		$max_height = intval( get_option( 'medium_large_size_h' ) );
    87 		$max_height = intval( get_option( 'medium_large_size_h' ) );
    84 
    88 
    85 		if ( intval( $content_width ) > 0 ) {
    89 		if ( intval( $content_width ) > 0 ) {
    86 			$max_width = min( intval( $content_width ), $max_width );
    90 			$max_width = min( intval( $content_width ), $max_width );
    87 		}
    91 		}
    88 	} elseif ( $size == 'large' ) {
    92 	} elseif ( 'large' === $size ) {
    89 		/*
    93 		/*
    90 		 * We're inserting a large size image into the editor. If it's a really
    94 		 * We're inserting a large size image into the editor. If it's a really
    91 		 * big image we'll scale it down to fit reasonably within the editor
    95 		 * big image we'll scale it down to fit reasonably within the editor
    92 		 * itself, and within the theme's content width if it's known. The user
    96 		 * itself, and within the theme's content width if it's known. The user
    93 		 * can resize it in the editor if they wish.
    97 		 * can resize it in the editor if they wish.
    94 		 */
    98 		 */
    95 		$max_width  = intval( get_option( 'large_size_w' ) );
    99 		$max_width  = intval( get_option( 'large_size_w' ) );
    96 		$max_height = intval( get_option( 'large_size_h' ) );
   100 		$max_height = intval( get_option( 'large_size_h' ) );
       
   101 
    97 		if ( intval( $content_width ) > 0 ) {
   102 		if ( intval( $content_width ) > 0 ) {
    98 			$max_width = min( intval( $content_width ), $max_width );
   103 			$max_width = min( intval( $content_width ), $max_width );
    99 		}
   104 		}
   100 	} elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
   105 	} elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) {
   101 		$max_width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
   106 		$max_width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
   102 		$max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
   107 		$max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
   103 		// Only in admin. Assume that theme authors know what they're doing.
   108 		// Only in admin. Assume that theme authors know what they're doing.
   104 		if ( intval( $content_width ) > 0 && 'edit' === $context ) {
   109 		if ( intval( $content_width ) > 0 && 'edit' === $context ) {
   105 			$max_width = min( intval( $content_width ), $max_width );
   110 			$max_width = min( intval( $content_width ), $max_width );
   106 		}
   111 		}
   107 	} else { // $size == 'full' has no constraint
   112 	} else { // $size === 'full' has no constraint.
   108 		$max_width  = $width;
   113 		$max_width  = $width;
   109 		$max_height = $height;
   114 		$max_height = $height;
   110 	}
   115 	}
   111 
   116 
   112 	/**
   117 	/**
   113 	 * Filters the maximum image size dimensions for the editor.
   118 	 * Filters the maximum image size dimensions for the editor.
   114 	 *
   119 	 *
   115 	 * @since 2.5.0
   120 	 * @since 2.5.0
   116 	 *
   121 	 *
   117 	 * @param array        $max_image_size An array with the width as the first element,
   122 	 * @param int[]        $max_image_size {
   118 	 *                                     and the height as the second element.
   123 	 *     An array of width and height values.
       
   124 	 *
       
   125 	 *     @type int $0 The maximum width in pixels.
       
   126 	 *     @type int $1 The maximum height in pixels.
       
   127 	 * }
   119 	 * @param string|array $size           Size of what the result image should be.
   128 	 * @param string|array $size           Size of what the result image should be.
   120 	 * @param string       $context        The context the image is being resized for.
   129 	 * @param string       $context        The context the image is being resized for.
   121 	 *                                     Possible values are 'display' (like in a theme)
   130 	 *                                     Possible values are 'display' (like in a theme)
   122 	 *                                     or 'edit' (like inserting into an editor).
   131 	 *                                     or 'edit' (like inserting into an editor).
   123 	 */
   132 	 */
   155 }
   164 }
   156 
   165 
   157 /**
   166 /**
   158  * Scale an image to fit a particular size (such as 'thumb' or 'medium').
   167  * Scale an image to fit a particular size (such as 'thumb' or 'medium').
   159  *
   168  *
   160  * Array with image url, width, height, and whether is intermediate size, in
       
   161  * that order is returned on success is returned. $is_intermediate is true if
       
   162  * $url is a resized image, false if it is the original.
       
   163  *
       
   164  * The URL might be the original image, or it might be a resized version. This
   169  * The URL might be the original image, or it might be a resized version. This
   165  * function won't create a new resized copy, it will just return an already
   170  * function won't create a new resized copy, it will just return an already
   166  * resized one if it exists.
   171  * resized one if it exists.
   167  *
   172  *
   168  * A plugin may use the {@see 'image_downsize'} filter to hook into and offer image
   173  * A plugin may use the {@see 'image_downsize'} filter to hook into and offer image
   169  * resizing services for images. The hook must return an array with the same
   174  * resizing services for images. The hook must return an array with the same
   170  * elements that are returned in the function. The first element being the URL
   175  * elements that are normally returned from the function.
   171  * to the new image that was resized.
       
   172  *
   176  *
   173  * @since 2.5.0
   177  * @since 2.5.0
   174  *
   178  *
   175  * @param int          $id   Attachment ID for image.
   179  * @param int          $id   Attachment ID for image.
   176  * @param array|string $size Optional. Image size to scale to. Accepts any valid image size,
   180  * @param string|int[] $size Optional. Image size to scale to. Accepts any valid image size name,
   177  *                           or an array of width and height values in pixels (in that order).
   181  *                           or an array of width and height values in pixels (in that order).
   178  *                           Default 'medium'.
   182  *                           Default 'medium'.
   179  * @return false|array Array containing the image URL, width, height, and boolean for whether
   183  * @return array|false {
   180  *                     the image is an intermediate size. False on failure.
   184  *     Array of image data, or boolean false if no image is available.
       
   185  *
       
   186  *     @type string $0 Image source URL.
       
   187  *     @type int    $1 Image width in pixels.
       
   188  *     @type int    $2 Image height in pixels.
       
   189  *     @type bool   $3 Whether the image is a resized image.
       
   190  * }
   181  */
   191  */
   182 function image_downsize( $id, $size = 'medium' ) {
   192 function image_downsize( $id, $size = 'medium' ) {
   183 	$is_image = wp_attachment_is_image( $id );
   193 	$is_image = wp_attachment_is_image( $id );
   184 
   194 
   185 	/**
   195 	/**
   186 	 * Filters whether to preempt the output of image_downsize().
   196 	 * Filters whether to preempt the output of image_downsize().
   187 	 *
   197 	 *
   188 	 * Passing a truthy value to the filter will effectively short-circuit
   198 	 * Returning a truthy value from the filter will effectively short-circuit
   189 	 * down-sizing the image, returning that value as output instead.
   199 	 * down-sizing the image, returning that value instead.
   190 	 *
   200 	 *
   191 	 * @since 2.5.0
   201 	 * @since 2.5.0
   192 	 *
   202 	 *
   193 	 * @param bool         $downsize Whether to short-circuit the image downsize. Default false.
   203 	 * @param bool|array   $downsize Whether to short-circuit the image downsize.
   194 	 * @param int          $id       Attachment ID for image.
   204 	 * @param int          $id       Attachment ID for image.
   195 	 * @param array|string $size     Size of image. Image size or array of width and height values (in that order).
   205 	 * @param array|string $size     Requested size of image. Image size name, or array of width
   196 	 *                               Default 'medium'.
   206 	 *                               and height values (in that order).
   197 	 */
   207 	 */
   198 	if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) {
   208 	$out = apply_filters( 'image_downsize', false, $id, $size );
       
   209 
       
   210 	if ( $out ) {
   199 		return $out;
   211 		return $out;
   200 	}
   212 	}
   201 
   213 
   202 	$img_url          = wp_get_attachment_url( $id );
   214 	$img_url          = wp_get_attachment_url( $id );
   203 	$meta             = wp_get_attachment_metadata( $id );
   215 	$meta             = wp_get_attachment_metadata( $id );
   204 	$width            = $height = 0;
   216 	$width            = 0;
       
   217 	$height           = 0;
   205 	$is_intermediate  = false;
   218 	$is_intermediate  = false;
   206 	$img_url_basename = wp_basename( $img_url );
   219 	$img_url_basename = wp_basename( $img_url );
   207 
   220 
   208 	// If the file isn't an image, attempt to replace its URL with a rendered image from its meta.
   221 	// If the file isn't an image, attempt to replace its URL with a rendered image from its meta.
   209 	// Otherwise, a non-image type could be returned.
   222 	// Otherwise, a non-image type could be returned.
   210 	if ( ! $is_image ) {
   223 	if ( ! $is_image ) {
   211 		if ( ! empty( $meta['sizes'] ) ) {
   224 		if ( ! empty( $meta['sizes']['full'] ) ) {
   212 			$img_url          = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url );
   225 			$img_url          = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url );
   213 			$img_url_basename = $meta['sizes']['full']['file'];
   226 			$img_url_basename = $meta['sizes']['full']['file'];
   214 			$width            = $meta['sizes']['full']['width'];
   227 			$width            = $meta['sizes']['full']['width'];
   215 			$height           = $meta['sizes']['full']['height'];
   228 			$height           = $meta['sizes']['full']['height'];
   216 		} else {
   229 		} else {
   217 			return false;
   230 			return false;
   218 		}
   231 		}
   219 	}
   232 	}
   220 
   233 
   221 	// try for a new style intermediate size
   234 	// Try for a new style intermediate size.
   222 	if ( $intermediate = image_get_intermediate_size( $id, $size ) ) {
   235 	$intermediate = image_get_intermediate_size( $id, $size );
       
   236 
       
   237 	if ( $intermediate ) {
   223 		$img_url         = str_replace( $img_url_basename, $intermediate['file'], $img_url );
   238 		$img_url         = str_replace( $img_url_basename, $intermediate['file'], $img_url );
   224 		$width           = $intermediate['width'];
   239 		$width           = $intermediate['width'];
   225 		$height          = $intermediate['height'];
   240 		$height          = $intermediate['height'];
   226 		$is_intermediate = true;
   241 		$is_intermediate = true;
   227 	} elseif ( $size == 'thumbnail' ) {
   242 	} elseif ( 'thumbnail' === $size ) {
   228 		// fall back to the old thumbnail
   243 		// Fall back to the old thumbnail.
   229 		if ( ( $thumb_file = wp_get_attachment_thumb_file( $id ) ) && $info = getimagesize( $thumb_file ) ) {
   244 		$thumb_file = wp_get_attachment_thumb_file( $id );
       
   245 		$info       = null;
       
   246 
       
   247 		if ( $thumb_file ) {
       
   248 			$info = @getimagesize( $thumb_file );
       
   249 		}
       
   250 
       
   251 		if ( $thumb_file && $info ) {
   230 			$img_url         = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url );
   252 			$img_url         = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url );
   231 			$width           = $info[0];
   253 			$width           = $info[0];
   232 			$height          = $info[1];
   254 			$height          = $info[1];
   233 			$is_intermediate = true;
   255 			$is_intermediate = true;
   234 		}
   256 		}
   235 	}
   257 	}
       
   258 
   236 	if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) {
   259 	if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) {
   237 		// any other type: use the real image
   260 		// Any other type: use the real image.
   238 		$width  = $meta['width'];
   261 		$width  = $meta['width'];
   239 		$height = $meta['height'];
   262 		$height = $meta['height'];
   240 	}
   263 	}
   241 
   264 
   242 	if ( $img_url ) {
   265 	if ( $img_url ) {
   243 		// we have the actual image size, but might need to further constrain it if content_width is narrower
   266 		// We have the actual image size, but might need to further constrain it if content_width is narrower.
   244 		list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
   267 		list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
   245 
   268 
   246 		return array( $img_url, $width, $height, $is_intermediate );
   269 		return array( $img_url, $width, $height, $is_intermediate );
   247 	}
   270 	}
       
   271 
   248 	return false;
   272 	return false;
   249 
       
   250 }
   273 }
   251 
   274 
   252 /**
   275 /**
   253  * Register a new image size.
   276  * Register a new image size.
   254  *
       
   255  * Cropping behavior for the image size is dependent on the value of $crop:
       
   256  * 1. If false (default), images will be scaled, not cropped.
       
   257  * 2. If an array in the form of array( x_crop_position, y_crop_position ):
       
   258  *    - x_crop_position accepts 'left' 'center', or 'right'.
       
   259  *    - y_crop_position accepts 'top', 'center', or 'bottom'.
       
   260  *    Images will be cropped to the specified dimensions within the defined crop area.
       
   261  * 3. If true, images will be cropped to the specified dimensions using center positions.
       
   262  *
   277  *
   263  * @since 2.9.0
   278  * @since 2.9.0
   264  *
   279  *
   265  * @global array $_wp_additional_image_sizes Associative array of additional image sizes.
   280  * @global array $_wp_additional_image_sizes Associative array of additional image sizes.
   266  *
   281  *
   267  * @param string     $name   Image size identifier.
   282  * @param string     $name   Image size identifier.
   268  * @param int        $width  Optional. Image width in pixels. Default 0.
   283  * @param int        $width  Optional. Image width in pixels. Default 0.
   269  * @param int        $height Optional. Image height in pixels. Default 0.
   284  * @param int        $height Optional. Image height in pixels. Default 0.
   270  * @param bool|array $crop   Optional. Whether to crop images to specified width and height or resize.
   285  * @param bool|array $crop   Optional. Image cropping behavior. If false, the image will be scaled (default),
   271  *                           An array can specify positioning of the crop area. Default false.
   286  *                           If true, image will be cropped to the specified dimensions using center positions.
       
   287  *                           If an array, the image will be cropped using the array to specify the crop location.
       
   288  *                           Array values must be in the format: array( x_crop_position, y_crop_position ) where:
       
   289  *                               - x_crop_position accepts: 'left', 'center', or 'right'.
       
   290  *                               - y_crop_position accepts: 'top', 'center', or 'bottom'.
   272  */
   291  */
   273 function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
   292 function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
   274 	global $_wp_additional_image_sizes;
   293 	global $_wp_additional_image_sizes;
   275 
   294 
   276 	$_wp_additional_image_sizes[ $name ] = array(
   295 	$_wp_additional_image_sizes[ $name ] = array(
   343  * content.
   362  * content.
   344  *
   363  *
   345  * @since 2.5.0
   364  * @since 2.5.0
   346  *
   365  *
   347  * @param int          $id    Attachment ID.
   366  * @param int          $id    Attachment ID.
   348  * @param string       $alt   Image Description for the alt attribute.
   367  * @param string       $alt   Image description for the alt attribute.
   349  * @param string       $title Image Description for the title attribute.
   368  * @param string       $title Image description for the title attribute.
   350  * @param string       $align Part of the class name for aligning the image.
   369  * @param string       $align Part of the class name for aligning the image.
   351  * @param string|array $size  Optional. Registered image size to retrieve a tag for. Accepts any
   370  * @param string|array $size  Optional. Registered image size to retrieve a tag for. Accepts any
   352  *                            valid image size, or an array of width and height values in pixels
   371  *                            valid image size, or an array of width and height values in pixels
   353  *                            (in that order). Default 'medium'.
   372  *                            (in that order). Default 'medium'.
   354  * @return string HTML IMG element for given image attachment
   373  * @return string HTML IMG element for given image attachment
   382 	 *
   401 	 *
   383 	 * @since 2.6.0
   402 	 * @since 2.6.0
   384 	 *
   403 	 *
   385 	 * @param string       $html  HTML content for the image.
   404 	 * @param string       $html  HTML content for the image.
   386 	 * @param int          $id    Attachment ID.
   405 	 * @param int          $id    Attachment ID.
   387 	 * @param string       $alt   Alternate text.
   406 	 * @param string       $alt   Image description for the alt attribute.
   388 	 * @param string       $title Attachment title.
   407 	 * @param string       $title Image description for the title attribute.
   389 	 * @param string       $align Part of the class name for aligning the image.
   408 	 * @param string       $align Part of the class name for aligning the image.
   390 	 * @param string|array $size  Size of image. Image size or array of width and height values (in that order).
   409 	 * @param string|array $size  Size of image. Image size or array of width and height values (in that order).
   391 	 *                            Default 'medium'.
   410 	 *                            Default 'medium'.
   392 	 */
   411 	 */
   393 	return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
   412 	return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
   403  *
   422  *
   404  * @param int $current_width  Current width of the image.
   423  * @param int $current_width  Current width of the image.
   405  * @param int $current_height Current height of the image.
   424  * @param int $current_height Current height of the image.
   406  * @param int $max_width      Optional. Max width in pixels to constrain to. Default 0.
   425  * @param int $max_width      Optional. Max width in pixels to constrain to. Default 0.
   407  * @param int $max_height     Optional. Max height in pixels to constrain to. Default 0.
   426  * @param int $max_height     Optional. Max height in pixels to constrain to. Default 0.
   408  * @return array First item is the width, the second item is the height.
   427  * @return int[] {
       
   428  *     An array of width and height values.
       
   429  *
       
   430  *     @type int $0 The width in pixels.
       
   431  *     @type int $1 The height in pixels.
       
   432  * }
   409  */
   433  */
   410 function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) {
   434 function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) {
   411 	if ( ! $max_width && ! $max_height ) {
   435 	if ( ! $max_width && ! $max_height ) {
   412 		return array( $current_width, $current_height );
   436 		return array( $current_width, $current_height );
   413 	}
   437 	}
   414 
   438 
   415 	$width_ratio = $height_ratio = 1.0;
   439 	$width_ratio  = 1.0;
   416 	$did_width   = $did_height = false;
   440 	$height_ratio = 1.0;
       
   441 	$did_width    = false;
       
   442 	$did_height   = false;
   417 
   443 
   418 	if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
   444 	if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
   419 		$width_ratio = $max_width / $current_width;
   445 		$width_ratio = $max_width / $current_width;
   420 		$did_width   = true;
   446 		$did_width   = true;
   421 	}
   447 	}
   423 	if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
   449 	if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
   424 		$height_ratio = $max_height / $current_height;
   450 		$height_ratio = $max_height / $current_height;
   425 		$did_height   = true;
   451 		$did_height   = true;
   426 	}
   452 	}
   427 
   453 
   428 	// Calculate the larger/smaller ratios
   454 	// Calculate the larger/smaller ratios.
   429 	$smaller_ratio = min( $width_ratio, $height_ratio );
   455 	$smaller_ratio = min( $width_ratio, $height_ratio );
   430 	$larger_ratio  = max( $width_ratio, $height_ratio );
   456 	$larger_ratio  = max( $width_ratio, $height_ratio );
   431 
   457 
   432 	if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
   458 	if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
   433 		// The larger ratio is too big. It would result in an overflow.
   459 		// The larger ratio is too big. It would result in an overflow.
   439 
   465 
   440 	// Very small dimensions may result in 0, 1 should be the minimum.
   466 	// Very small dimensions may result in 0, 1 should be the minimum.
   441 	$w = max( 1, (int) round( $current_width * $ratio ) );
   467 	$w = max( 1, (int) round( $current_width * $ratio ) );
   442 	$h = max( 1, (int) round( $current_height * $ratio ) );
   468 	$h = max( 1, (int) round( $current_height * $ratio ) );
   443 
   469 
   444 	// Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
   470 	/*
   445 	// 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.
   471 	 * Sometimes, due to rounding, we'll end up with a result like this:
   446 	// Thus we look for dimensions that are one pixel shy of the max value and bump them up
   472 	 * 465x700 in a 177x177 box is 117x176... a pixel short.
       
   473 	 * We also have issues with recursive calls resulting in an ever-changing result.
       
   474 	 * Constraining to the result of a constraint should yield the original result.
       
   475 	 * Thus we look for dimensions that are one pixel shy of the max value and bump them up.
       
   476 	 */
   447 
   477 
   448 	// Note: $did_width means it is possible $smaller_ratio == $width_ratio.
   478 	// Note: $did_width means it is possible $smaller_ratio == $width_ratio.
   449 	if ( $did_width && $w == $max_width - 1 ) {
   479 	if ( $did_width && $w === $max_width - 1 ) {
   450 		$w = $max_width; // Round it up
   480 		$w = $max_width; // Round it up.
   451 	}
   481 	}
   452 
   482 
   453 	// Note: $did_height means it is possible $smaller_ratio == $height_ratio.
   483 	// Note: $did_height means it is possible $smaller_ratio == $height_ratio.
   454 	if ( $did_height && $h == $max_height - 1 ) {
   484 	if ( $did_height && $h === $max_height - 1 ) {
   455 		$h = $max_height; // Round it up
   485 		$h = $max_height; // Round it up.
   456 	}
   486 	}
   457 
   487 
   458 	/**
   488 	/**
   459 	 * Filters dimensions to constrain down-sampled images to.
   489 	 * Filters dimensions to constrain down-sampled images to.
   460 	 *
   490 	 *
   461 	 * @since 4.1.0
   491 	 * @since 4.1.0
   462 	 *
   492 	 *
   463 	 * @param array $dimensions     The image width and height.
   493 	 * @param int[] $dimensions     {
       
   494 	 *     An array of width and height values.
       
   495 	 *
       
   496 	 *     @type int $0 The width in pixels.
       
   497 	 *     @type int $1 The height in pixels.
       
   498 	 * }
   464 	 * @param int   $current_width  The current width of the image.
   499 	 * @param int   $current_width  The current width of the image.
   465 	 * @param int   $current_height The current height of the image.
   500 	 * @param int   $current_height The current height of the image.
   466 	 * @param int   $max_width      The maximum width permitted.
   501 	 * @param int   $max_width      The maximum width permitted.
   467 	 * @param int   $max_height     The maximum height permitted.
   502 	 * @param int   $max_height     The maximum height permitted.
   468 	 */
   503 	 */
   489  * @param int        $orig_h Original height in pixels.
   524  * @param int        $orig_h Original height in pixels.
   490  * @param int        $dest_w New width in pixels.
   525  * @param int        $dest_w New width in pixels.
   491  * @param int        $dest_h New height in pixels.
   526  * @param int        $dest_h New height in pixels.
   492  * @param bool|array $crop   Optional. Whether to crop image to specified width and height or resize.
   527  * @param bool|array $crop   Optional. Whether to crop image to specified width and height or resize.
   493  *                           An array can specify positioning of the crop area. Default false.
   528  *                           An array can specify positioning of the crop area. Default false.
   494  * @return false|array False on failure. Returned array matches parameters for `imagecopyresampled()`.
   529  * @return array|false Returned array matches parameters for `imagecopyresampled()`. False on failure.
   495  */
   530  */
   496 function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) {
   531 function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) {
   497 
   532 
   498 	if ( $orig_w <= 0 || $orig_h <= 0 ) {
   533 	if ( $orig_w <= 0 || $orig_h <= 0 ) {
   499 		return false;
   534 		return false;
   500 	}
   535 	}
   501 	// at least one of dest_w or dest_h must be specific
   536 	// At least one of $dest_w or $dest_h must be specific.
   502 	if ( $dest_w <= 0 && $dest_h <= 0 ) {
   537 	if ( $dest_w <= 0 && $dest_h <= 0 ) {
   503 		return false;
   538 		return false;
   504 	}
   539 	}
   505 
   540 
   506 	/**
   541 	/**
   507 	 * Filters whether to preempt calculating the image resize dimensions.
   542 	 * Filters whether to preempt calculating the image resize dimensions.
   508 	 *
   543 	 *
   509 	 * Passing a non-null value to the filter will effectively short-circuit
   544 	 * Returning a non-null value from the filter will effectively short-circuit
   510 	 * image_resize_dimensions(), returning that value instead.
   545 	 * image_resize_dimensions(), returning that value instead.
   511 	 *
   546 	 *
   512 	 * @since 3.4.0
   547 	 * @since 3.4.0
   513 	 *
   548 	 *
   514 	 * @param null|mixed $null   Whether to preempt output of the resize dimensions.
   549 	 * @param null|mixed $null   Whether to preempt output of the resize dimensions.
   518 	 * @param int        $dest_h New height in pixels.
   553 	 * @param int        $dest_h New height in pixels.
   519 	 * @param bool|array $crop   Whether to crop image to specified width and height or resize.
   554 	 * @param bool|array $crop   Whether to crop image to specified width and height or resize.
   520 	 *                           An array can specify positioning of the crop area. Default false.
   555 	 *                           An array can specify positioning of the crop area. Default false.
   521 	 */
   556 	 */
   522 	$output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
   557 	$output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
       
   558 
   523 	if ( null !== $output ) {
   559 	if ( null !== $output ) {
   524 		return $output;
   560 		return $output;
   525 	}
   561 	}
   526 
   562 
       
   563 	// Stop if the destination size is larger than the original image dimensions.
       
   564 	if ( empty( $dest_h ) ) {
       
   565 		if ( $orig_w < $dest_w ) {
       
   566 			return false;
       
   567 		}
       
   568 	} elseif ( empty( $dest_w ) ) {
       
   569 		if ( $orig_h < $dest_h ) {
       
   570 			return false;
       
   571 		}
       
   572 	} else {
       
   573 		if ( $orig_w < $dest_w && $orig_h < $dest_h ) {
       
   574 			return false;
       
   575 		}
       
   576 	}
       
   577 
   527 	if ( $crop ) {
   578 	if ( $crop ) {
   528 		// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
   579 		/*
       
   580 		 * Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h.
       
   581 		 * Note that the requested crop dimensions are used as a maximum bounding box for the original image.
       
   582 		 * If the original image's width or height is less than the requested width or height
       
   583 		 * only the greater one will be cropped.
       
   584 		 * For example when the original image is 600x300, and the requested crop dimensions are 400x400,
       
   585 		 * the resulting image will be 400x300.
       
   586 		 */
   529 		$aspect_ratio = $orig_w / $orig_h;
   587 		$aspect_ratio = $orig_w / $orig_h;
   530 		$new_w        = min( $dest_w, $orig_w );
   588 		$new_w        = min( $dest_w, $orig_w );
   531 		$new_h        = min( $dest_h, $orig_h );
   589 		$new_h        = min( $dest_h, $orig_h );
   532 
   590 
   533 		if ( ! $new_w ) {
   591 		if ( ! $new_w ) {
   563 			$s_y = $orig_h - $crop_h;
   621 			$s_y = $orig_h - $crop_h;
   564 		} else {
   622 		} else {
   565 			$s_y = floor( ( $orig_h - $crop_h ) / 2 );
   623 			$s_y = floor( ( $orig_h - $crop_h ) / 2 );
   566 		}
   624 		}
   567 	} else {
   625 	} else {
   568 		// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
   626 		// Resize using $dest_w x $dest_h as a maximum bounding box.
   569 		$crop_w = $orig_w;
   627 		$crop_w = $orig_w;
   570 		$crop_h = $orig_h;
   628 		$crop_h = $orig_h;
   571 
   629 
   572 		$s_x = 0;
   630 		$s_x = 0;
   573 		$s_y = 0;
   631 		$s_y = 0;
   574 
   632 
   575 		list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
   633 		list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
   576 	}
   634 	}
   577 
   635 
   578 	// if the resulting image would be the same size or larger we don't want to resize it
   636 	if ( wp_fuzzy_number_match( $new_w, $orig_w ) && wp_fuzzy_number_match( $new_h, $orig_h ) ) {
   579 	if ( $new_w >= $orig_w && $new_h >= $orig_h && $dest_w != $orig_w && $dest_h != $orig_h ) {
   637 		// The new size has virtually the same dimensions as the original image.
   580 		return false;
   638 
   581 	}
   639 		/**
   582 
   640 		 * Filters whether to proceed with making an image sub-size with identical dimensions
   583 	// the return array matches the parameters to imagecopyresampled()
   641 		 * with the original/source image. Differences of 1px may be due to rounding and are ignored.
       
   642 		 *
       
   643 		 * @since 5.3.0
       
   644 		 *
       
   645 		 * @param bool $proceed The filtered value.
       
   646 		 * @param int  $orig_w  Original image width.
       
   647 		 * @param int  $orig_h  Original image height.
       
   648 		 */
       
   649 		$proceed = (bool) apply_filters( 'wp_image_resize_identical_dimensions', false, $orig_w, $orig_h );
       
   650 
       
   651 		if ( ! $proceed ) {
       
   652 			return false;
       
   653 		}
       
   654 	}
       
   655 
       
   656 	// The return array matches the parameters to imagecopyresampled().
   584 	// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
   657 	// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
   585 	return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
   658 	return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
   586 
       
   587 }
   659 }
   588 
   660 
   589 /**
   661 /**
   590  * Resizes an image to make a thumbnail or intermediate size.
   662  * Resizes an image to make a thumbnail or intermediate size.
   591  *
   663  *
   598  * @param string $file   File path.
   670  * @param string $file   File path.
   599  * @param int    $width  Image width.
   671  * @param int    $width  Image width.
   600  * @param int    $height Image height.
   672  * @param int    $height Image height.
   601  * @param bool   $crop   Optional. Whether to crop image to specified width and height or resize.
   673  * @param bool   $crop   Optional. Whether to crop image to specified width and height or resize.
   602  *                       Default false.
   674  *                       Default false.
   603  * @return false|array False, if no image was created. Metadata array on success.
   675  * @return array|false Metadata array on success. False if no image was created.
   604  */
   676  */
   605 function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
   677 function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
   606 	if ( $width || $height ) {
   678 	if ( $width || $height ) {
   607 		$editor = wp_get_image_editor( $file );
   679 		$editor = wp_get_image_editor( $file );
   608 
   680 
   643 		$constrained_size = wp_constrain_dimensions( $target_width, $target_height, $source_width );
   715 		$constrained_size = wp_constrain_dimensions( $target_width, $target_height, $source_width );
   644 		$expected_size    = array( $source_width, $source_height );
   716 		$expected_size    = array( $source_width, $source_height );
   645 	}
   717 	}
   646 
   718 
   647 	// If the image dimensions are within 1px of the expected size, we consider it a match.
   719 	// If the image dimensions are within 1px of the expected size, we consider it a match.
   648 	$matched = ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 );
   720 	$matched = ( wp_fuzzy_number_match( $constrained_size[0], $expected_size[0] ) && wp_fuzzy_number_match( $constrained_size[1], $expected_size[1] ) );
   649 
   721 
   650 	return $matched;
   722 	return $matched;
   651 }
   723 }
   652 
   724 
   653 /**
   725 /**
   673  *
   745  *
   674  * @param int          $post_id Attachment ID.
   746  * @param int          $post_id Attachment ID.
   675  * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
   747  * @param array|string $size    Optional. Image size. Accepts any valid image size, or an array
   676  *                              of width and height values in pixels (in that order).
   748  *                              of width and height values in pixels (in that order).
   677  *                              Default 'thumbnail'.
   749  *                              Default 'thumbnail'.
   678  * @return false|array $data {
   750  * @return array|false {
   679  *     Array of file relative path, width, and height on success. Additionally includes absolute
   751  *     Array of file relative path, width, and height on success. Additionally includes absolute
   680  *     path and URL if registered size is passed to $size parameter. False on failure.
   752  *     path and URL if registered size is passed to $size parameter. False on failure.
   681  *
   753  *
   682  *     @type string $file   Image's path relative to uploads directory
   754  *     @type string $file   Image's path relative to uploads directory
   683  *     @type int    $width  Width of image
   755  *     @type int    $width  Width of image
   685  *     @type string $path   Image's absolute filesystem path.
   757  *     @type string $path   Image's absolute filesystem path.
   686  *     @type string $url    Image's URL.
   758  *     @type string $url    Image's URL.
   687  * }
   759  * }
   688  */
   760  */
   689 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
   761 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
   690 	if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) {
   762 	$imagedata = wp_get_attachment_metadata( $post_id );
       
   763 
       
   764 	if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
   691 		return false;
   765 		return false;
   692 	}
   766 	}
   693 
   767 
   694 	$data = array();
   768 	$data = array();
   695 
   769 
   702 			$imagedata['width']  = $imagedata['sizes']['full']['width'];
   776 			$imagedata['width']  = $imagedata['sizes']['full']['width'];
   703 		}
   777 		}
   704 
   778 
   705 		foreach ( $imagedata['sizes'] as $_size => $data ) {
   779 		foreach ( $imagedata['sizes'] as $_size => $data ) {
   706 			// If there's an exact match to an existing image size, short circuit.
   780 			// If there's an exact match to an existing image size, short circuit.
   707 			if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
   781 			if ( intval( $data['width'] ) === intval( $size[0] ) && intval( $data['height'] ) === intval( $size[1] ) ) {
   708 				$candidates[ $data['width'] * $data['height'] ] = $data;
   782 				$candidates[ $data['width'] * $data['height'] ] = $data;
   709 				break;
   783 				break;
   710 			}
   784 			}
   711 
   785 
   712 			// If it's not an exact match, consider larger sizes with the same aspect ratio.
   786 			// If it's not an exact match, consider larger sizes with the same aspect ratio.
   752 	// If we still don't have a match at this point, return false.
   826 	// If we still don't have a match at this point, return false.
   753 	if ( empty( $data ) ) {
   827 	if ( empty( $data ) ) {
   754 		return false;
   828 		return false;
   755 	}
   829 	}
   756 
   830 
   757 	// include the full filesystem path of the intermediate file
   831 	// Include the full filesystem path of the intermediate file.
   758 	if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
   832 	if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
   759 		$file_url     = wp_get_attachment_url( $post_id );
   833 		$file_url     = wp_get_attachment_url( $post_id );
   760 		$data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] );
   834 		$data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] );
   761 		$data['url']  = path_join( dirname( $file_url ), $data['file'] );
   835 		$data['url']  = path_join( dirname( $file_url ), $data['file'] );
   762 	}
   836 	}
   776 	 */
   850 	 */
   777 	return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
   851 	return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
   778 }
   852 }
   779 
   853 
   780 /**
   854 /**
   781  * Gets the available intermediate image sizes.
   855  * Gets the available intermediate image size names.
   782  *
   856  *
   783  * @since 3.0.0
   857  * @since 3.0.0
   784  *
   858  *
   785  * @return array Returns a filtered array of image size strings.
   859  * @return string[] An array of image size names.
   786  */
   860  */
   787 function get_intermediate_image_sizes() {
   861 function get_intermediate_image_sizes() {
   788 	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
   862 	$default_sizes    = array( 'thumbnail', 'medium', 'medium_large', 'large' );
   789 	$image_sizes                = array( 'thumbnail', 'medium', 'medium_large', 'large' ); // Standard sizes
   863 	$additional_sizes = wp_get_additional_image_sizes();
   790 	if ( ! empty( $_wp_additional_image_sizes ) ) {
   864 
   791 		$image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
   865 	if ( ! empty( $additional_sizes ) ) {
       
   866 		$default_sizes = array_merge( $default_sizes, array_keys( $additional_sizes ) );
   792 	}
   867 	}
   793 
   868 
   794 	/**
   869 	/**
   795 	 * Filters the list of intermediate image sizes.
   870 	 * Filters the list of intermediate image sizes.
   796 	 *
   871 	 *
   797 	 * @since 2.5.0
   872 	 * @since 2.5.0
   798 	 *
   873 	 *
   799 	 * @param array $image_sizes An array of intermediate image sizes. Defaults
   874 	 * @param string[] $default_sizes An array of intermediate image size names. Defaults
   800 	 *                           are 'thumbnail', 'medium', 'medium_large', 'large'.
   875 	 *                                are 'thumbnail', 'medium', 'medium_large', 'large'.
   801 	 */
   876 	 */
   802 	return apply_filters( 'intermediate_image_sizes', $image_sizes );
   877 	return apply_filters( 'intermediate_image_sizes', $default_sizes );
   803 }
   878 }
   804 
   879 
   805 /**
   880 /**
   806  * Retrieve an image to represent an attachment.
   881  * Returns a normalized list of all currently registered image sub-sizes.
   807  *
   882  *
   808  * A mime icon for files, thumbnail or intermediate size for images.
   883  * @since 5.3.0
   809  *
   884  * @uses wp_get_additional_image_sizes()
   810  * The returned array contains four values: the URL of the attachment image src,
   885  * @uses get_intermediate_image_sizes()
   811  * the width of the image file, the height of the image file, and a boolean
   886  *
   812  * representing whether the returned array describes an intermediate (generated)
   887  * @return array Associative array of the registered image sub-sizes.
   813  * image size or the original, full-sized upload.
   888  */
       
   889 function wp_get_registered_image_subsizes() {
       
   890 	$additional_sizes = wp_get_additional_image_sizes();
       
   891 	$all_sizes        = array();
       
   892 
       
   893 	foreach ( get_intermediate_image_sizes() as $size_name ) {
       
   894 		$size_data = array(
       
   895 			'width'  => 0,
       
   896 			'height' => 0,
       
   897 			'crop'   => false,
       
   898 		);
       
   899 
       
   900 		if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
       
   901 			// For sizes added by plugins and themes.
       
   902 			$size_data['width'] = intval( $additional_sizes[ $size_name ]['width'] );
       
   903 		} else {
       
   904 			// For default sizes set in options.
       
   905 			$size_data['width'] = intval( get_option( "{$size_name}_size_w" ) );
       
   906 		}
       
   907 
       
   908 		if ( isset( $additional_sizes[ $size_name ]['height'] ) ) {
       
   909 			$size_data['height'] = intval( $additional_sizes[ $size_name ]['height'] );
       
   910 		} else {
       
   911 			$size_data['height'] = intval( get_option( "{$size_name}_size_h" ) );
       
   912 		}
       
   913 
       
   914 		if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) {
       
   915 			// This size isn't set.
       
   916 			continue;
       
   917 		}
       
   918 
       
   919 		if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) {
       
   920 			$size_data['crop'] = $additional_sizes[ $size_name ]['crop'];
       
   921 		} else {
       
   922 			$size_data['crop'] = get_option( "{$size_name}_crop" );
       
   923 		}
       
   924 
       
   925 		if ( ! is_array( $size_data['crop'] ) || empty( $size_data['crop'] ) ) {
       
   926 			$size_data['crop'] = (bool) $size_data['crop'];
       
   927 		}
       
   928 
       
   929 		$all_sizes[ $size_name ] = $size_data;
       
   930 	}
       
   931 
       
   932 	return $all_sizes;
       
   933 }
       
   934 
       
   935 /**
       
   936  * Retrieves an image to represent an attachment.
   814  *
   937  *
   815  * @since 2.5.0
   938  * @since 2.5.0
       
   939  *
       
   940  * @param int          $attachment_id Image attachment ID.
       
   941  * @param string|int[] $size          Optional. Image size. Accepts any valid image size name, or an array of width
       
   942  *                                    and height values in pixels (in that order). Default 'thumbnail'.
       
   943  * @param bool         $icon          Optional. Whether the image should fall back to a mime type icon. Default false.
       
   944  * @return array|false {
       
   945  *     Array of image data, or boolean false if no image is available.
       
   946  *
       
   947  *     @type string $0 Image source URL.
       
   948  *     @type int    $1 Image width in pixels.
       
   949  *     @type int    $2 Image height in pixels.
       
   950  *     @type bool   $3 Whether the image is a resized image.
       
   951  * }
       
   952  */
       
   953 function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
       
   954 	// Get a thumbnail or intermediate image if there is one.
       
   955 	$image = image_downsize( $attachment_id, $size );
       
   956 	if ( ! $image ) {
       
   957 		$src = false;
       
   958 
       
   959 		if ( $icon ) {
       
   960 			$src = wp_mime_type_icon( $attachment_id );
       
   961 
       
   962 			if ( $src ) {
       
   963 				/** This filter is documented in wp-includes/post.php */
       
   964 				$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
       
   965 
       
   966 				$src_file               = $icon_dir . '/' . wp_basename( $src );
       
   967 				list( $width, $height ) = @getimagesize( $src_file );
       
   968 			}
       
   969 		}
       
   970 
       
   971 		if ( $src && $width && $height ) {
       
   972 			$image = array( $src, $width, $height, false );
       
   973 		}
       
   974 	}
       
   975 	/**
       
   976 	 * Filters the attachment image source result.
       
   977 	 *
       
   978 	 * @since 4.3.0
       
   979 	 *
       
   980 	 * @param array|false  $image         {
       
   981 	 *     Array of image data, or boolean false if no image is available.
       
   982 	 *
       
   983 	 *     @type string $0 Image source URL.
       
   984 	 *     @type int    $1 Image width in pixels.
       
   985 	 *     @type int    $2 Image height in pixels.
       
   986 	 *     @type bool   $3 Whether the image is a resized image.
       
   987 	 * }
       
   988 	 * @param int          $attachment_id Image attachment ID.
       
   989 	 * @param string|int[] $size          Requested size of image. Image size name, or array of width
       
   990 	 *                                    and height values (in that order).
       
   991 	 * @param bool         $icon          Whether the image should be treated as an icon.
       
   992 	 */
       
   993 	return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
       
   994 }
       
   995 
       
   996 /**
       
   997  * Get an HTML img element representing an image attachment
       
   998  *
       
   999  * While `$size` will accept an array, it is better to register a size with
       
  1000  * add_image_size() so that a cropped version is generated. It's much more
       
  1001  * efficient than having to find the closest-sized image and then having the
       
  1002  * browser scale down the image.
       
  1003  *
       
  1004  * @since 2.5.0
       
  1005  * @since 4.4.0 The `$srcset` and `$sizes` attributes were added.
       
  1006  * @since 5.5.0 The `$loading` attribute was added.
   816  *
  1007  *
   817  * @param int          $attachment_id Image attachment ID.
  1008  * @param int          $attachment_id Image attachment ID.
   818  * @param string|array $size          Optional. Image size. Accepts any valid image size, or an array of width
  1009  * @param string|array $size          Optional. Image size. Accepts any valid image size, or an array of width
   819  *                                    and height values in pixels (in that order). Default 'thumbnail'.
  1010  *                                    and height values in pixels (in that order). Default 'thumbnail'.
   820  * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
  1011  * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
   821  * @return false|array Returns an array (url, width, height, is_intermediate), or false, if no image is available.
  1012  * @param string|array $attr {
   822  */
  1013  *     Optional. Attributes for the image markup.
   823 function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
  1014  *
   824 	// get a thumbnail or intermediate image if there is one
  1015  *     @type string       $src     Image attachment URL.
   825 	$image = image_downsize( $attachment_id, $size );
  1016  *     @type string       $class   CSS class name or space-separated list of classes.
   826 	if ( ! $image ) {
  1017  *                                 Default `attachment-$size_class size-$size_class`,
   827 		$src = false;
  1018  *                                 where `$size_class` is the image size being requested.
   828 
  1019  *     @type string       $alt     Image description for the alt attribute.
   829 		if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) {
  1020  *     @type string       $srcset  The 'srcset' attribute value.
   830 			/** This filter is documented in wp-includes/post.php */
  1021  *     @type string       $sizes   The 'sizes' attribute value.
   831 			$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
  1022  *     @type string|false $loading The 'loading' attribute value. Passing a value of false
   832 
  1023  *                                 will result in the attribute being omitted for the image.
   833 			$src_file                = $icon_dir . '/' . wp_basename( $src );
  1024  *                                 Defaults to 'lazy', depending on wp_lazy_loading_enabled().
   834 			@list( $width, $height ) = getimagesize( $src_file );
  1025  * }
   835 		}
       
   836 
       
   837 		if ( $src && $width && $height ) {
       
   838 			$image = array( $src, $width, $height );
       
   839 		}
       
   840 	}
       
   841 	/**
       
   842 	 * Filters the image src result.
       
   843 	 *
       
   844 	 * @since 4.3.0
       
   845 	 *
       
   846 	 * @param array|false  $image         Either array with src, width & height, icon src, or false.
       
   847 	 * @param int          $attachment_id Image attachment ID.
       
   848 	 * @param string|array $size          Size of image. Image size or array of width and height values
       
   849 	 *                                    (in that order). Default 'thumbnail'.
       
   850 	 * @param bool         $icon          Whether the image should be treated as an icon. Default false.
       
   851 	 */
       
   852 	return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
       
   853 }
       
   854 
       
   855 /**
       
   856  * Get an HTML img element representing an image attachment
       
   857  *
       
   858  * While `$size` will accept an array, it is better to register a size with
       
   859  * add_image_size() so that a cropped version is generated. It's much more
       
   860  * efficient than having to find the closest-sized image and then having the
       
   861  * browser scale down the image.
       
   862  *
       
   863  * @since 2.5.0
       
   864  *
       
   865  * @param int          $attachment_id Image attachment ID.
       
   866  * @param string|array $size          Optional. Image size. Accepts any valid image size, or an array of width
       
   867  *                                    and height values in pixels (in that order). Default 'thumbnail'.
       
   868  * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
       
   869  * @param string|array $attr          Optional. Attributes for the image markup. Default empty.
       
   870  * @return string HTML img element or empty string on failure.
  1026  * @return string HTML img element or empty string on failure.
   871  */
  1027  */
   872 function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
  1028 function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
   873 	$html  = '';
  1029 	$html  = '';
   874 	$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
  1030 	$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
       
  1031 
   875 	if ( $image ) {
  1032 	if ( $image ) {
   876 		list($src, $width, $height) = $image;
  1033 		list( $src, $width, $height ) = $image;
   877 		$hwstring                   = image_hwstring( $width, $height );
  1034 
   878 		$size_class                 = $size;
  1035 		$attachment = get_post( $attachment_id );
       
  1036 		$hwstring   = image_hwstring( $width, $height );
       
  1037 		$size_class = $size;
       
  1038 
   879 		if ( is_array( $size_class ) ) {
  1039 		if ( is_array( $size_class ) ) {
   880 			$size_class = join( 'x', $size_class );
  1040 			$size_class = join( 'x', $size_class );
   881 		}
  1041 		}
   882 		$attachment   = get_post( $attachment_id );
  1042 
   883 		$default_attr = array(
  1043 		$default_attr = array(
   884 			'src'   => $src,
  1044 			'src'   => $src,
   885 			'class' => "attachment-$size_class size-$size_class",
  1045 			'class' => "attachment-$size_class size-$size_class",
   886 			'alt'   => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
  1046 			'alt'   => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
   887 		);
  1047 		);
   888 
  1048 
       
  1049 		// Add `loading` attribute.
       
  1050 		if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
       
  1051 			$default_attr['loading'] = 'lazy';
       
  1052 		}
       
  1053 
   889 		$attr = wp_parse_args( $attr, $default_attr );
  1054 		$attr = wp_parse_args( $attr, $default_attr );
       
  1055 
       
  1056 		// If the default value of `lazy` for the `loading` attribute is overridden
       
  1057 		// to omit the attribute for this image, ensure it is not included.
       
  1058 		if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) {
       
  1059 			unset( $attr['loading'] );
       
  1060 		}
   890 
  1061 
   891 		// Generate 'srcset' and 'sizes' if not already present.
  1062 		// Generate 'srcset' and 'sizes' if not already present.
   892 		if ( empty( $attr['srcset'] ) ) {
  1063 		if ( empty( $attr['srcset'] ) ) {
   893 			$image_meta = wp_get_attachment_metadata( $attachment_id );
  1064 			$image_meta = wp_get_attachment_metadata( $attachment_id );
   894 
  1065 
   910 		/**
  1081 		/**
   911 		 * Filters the list of attachment image attributes.
  1082 		 * Filters the list of attachment image attributes.
   912 		 *
  1083 		 *
   913 		 * @since 2.8.0
  1084 		 * @since 2.8.0
   914 		 *
  1085 		 *
   915 		 * @param array        $attr       Attributes for the image markup.
  1086 		 * @param array        $attr       Array of attribute values for the image markup, keyed by attribute name.
       
  1087 		 *                                 See wp_get_attachment_image().
   916 		 * @param WP_Post      $attachment Image attachment post.
  1088 		 * @param WP_Post      $attachment Image attachment post.
   917 		 * @param string|array $size       Requested size. Image size or array of width and height values
  1089 		 * @param string|array $size       Requested size. Image size or array of width and height values
   918 		 *                                 (in that order). Default 'thumbnail'.
  1090 		 *                                 (in that order). Default 'thumbnail'.
   919 		 */
  1091 		 */
   920 		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
  1092 		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
       
  1093 
   921 		$attr = array_map( 'esc_attr', $attr );
  1094 		$attr = array_map( 'esc_attr', $attr );
   922 		$html = rtrim( "<img $hwstring" );
  1095 		$html = rtrim( "<img $hwstring" );
       
  1096 
   923 		foreach ( $attr as $name => $value ) {
  1097 		foreach ( $attr as $name => $value ) {
   924 			$html .= " $name=" . '"' . $value . '"';
  1098 			$html .= " $name=" . '"' . $value . '"';
   925 		}
  1099 		}
       
  1100 
   926 		$html .= ' />';
  1101 		$html .= ' />';
   927 	}
  1102 	}
   928 
  1103 
   929 	return $html;
  1104 	return $html;
   930 }
  1105 }
   960 	if ( '.' === $dirname ) {
  1135 	if ( '.' === $dirname ) {
   961 		return '';
  1136 		return '';
   962 	}
  1137 	}
   963 
  1138 
   964 	if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) {
  1139 	if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) {
   965 		// Get the directory name relative to the upload directory (back compat for pre-2.7 uploads)
  1140 		// Get the directory name relative to the upload directory (back compat for pre-2.7 uploads).
   966 		$dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 );
  1141 		$dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 );
   967 		$dirname = ltrim( $dirname, '/' );
  1142 		$dirname = ltrim( $dirname, '/' );
   968 	}
  1143 	}
   969 
  1144 
   970 	return $dirname;
  1145 	return $dirname;
   978  * @since 4.4.0
  1153  * @since 4.4.0
   979  * @access private
  1154  * @access private
   980  *
  1155  *
   981  * @param string $size_name  Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.).
  1156  * @param string $size_name  Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.).
   982  * @param array  $image_meta The image meta data.
  1157  * @param array  $image_meta The image meta data.
   983  * @return array|bool Array of width and height values in pixels (in that order)
  1158  * @return array|bool The image meta data as returned by `wp_get_attachment_metadata()`.
   984  *                    or false if the size doesn't exist.
       
   985  */
  1159  */
   986 function _wp_get_image_size_from_meta( $size_name, $image_meta ) {
  1160 function _wp_get_image_size_from_meta( $size_name, $image_meta ) {
   987 	if ( $size_name === 'full' ) {
  1161 	if ( 'full' === $size_name ) {
   988 		return array(
  1162 		return array(
   989 			absint( $image_meta['width'] ),
  1163 			absint( $image_meta['width'] ),
   990 			absint( $image_meta['height'] ),
  1164 			absint( $image_meta['height'] ),
   991 		);
  1165 		);
   992 	} elseif ( ! empty( $image_meta['sizes'][ $size_name ] ) ) {
  1166 	} elseif ( ! empty( $image_meta['sizes'][ $size_name ] ) ) {
  1012  * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
  1186  * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
  1013  *                                    Default null.
  1187  *                                    Default null.
  1014  * @return string|bool A 'srcset' value string or false.
  1188  * @return string|bool A 'srcset' value string or false.
  1015  */
  1189  */
  1016 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
  1190 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
  1017 	if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) {
  1191 	$image = wp_get_attachment_image_src( $attachment_id, $size );
       
  1192 
       
  1193 	if ( ! $image ) {
  1018 		return false;
  1194 		return false;
  1019 	}
  1195 	}
  1020 
  1196 
  1021 	if ( ! is_array( $image_meta ) ) {
  1197 	if ( ! is_array( $image_meta ) ) {
  1022 		$image_meta = wp_get_attachment_metadata( $attachment_id );
  1198 		$image_meta = wp_get_attachment_metadata( $attachment_id );
  1034 /**
  1210 /**
  1035  * A helper function to calculate the image sources to include in a 'srcset' attribute.
  1211  * A helper function to calculate the image sources to include in a 'srcset' attribute.
  1036  *
  1212  *
  1037  * @since 4.4.0
  1213  * @since 4.4.0
  1038  *
  1214  *
  1039  * @param array  $size_array    Array of width and height values in pixels (in that order).
  1215  * @param int[]  $size_array    {
       
  1216  *     An array of width and height values.
       
  1217  *
       
  1218  *     @type int $0 The width in pixels.
       
  1219  *     @type int $1 The height in pixels.
       
  1220  * }
  1040  * @param string $image_src     The 'src' of the image.
  1221  * @param string $image_src     The 'src' of the image.
  1041  * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
  1222  * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
  1042  * @param int    $attachment_id Optional. The image attachment ID to pass to the filter. Default 0.
  1223  * @param int    $attachment_id Optional. The image attachment ID. Default 0.
  1043  * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
  1224  * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
  1044  */
  1225  */
  1045 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
  1226 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
  1046 	/**
  1227 	/**
  1047 	 * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
  1228 	 * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
  1048 	 *
  1229 	 *
  1049 	 * @since 4.5.0
  1230 	 * @since 4.5.0
  1050 	 *
  1231 	 *
  1051 	 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
  1232 	 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
  1052 	 * @param array  $size_array    Array of width and height values in pixels (in that order).
  1233 	 * @param int[]  $size_array    {
       
  1234 	 *     An array of requested width and height values.
       
  1235 	 *
       
  1236 	 *     @type int $0 The width in pixels.
       
  1237 	 *     @type int $1 The height in pixels.
       
  1238 	 * }
  1053 	 * @param string $image_src     The 'src' of the image.
  1239 	 * @param string $image_src     The 'src' of the image.
  1054 	 * @param int    $attachment_id The image attachment ID or 0 if not supplied.
  1240 	 * @param int    $attachment_id The image attachment ID or 0 if not supplied.
  1055 	 */
  1241 	 */
  1056 	$image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
  1242 	$image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
  1057 
  1243 
  1115 	/**
  1301 	/**
  1116 	 * Filters the maximum image width to be included in a 'srcset' attribute.
  1302 	 * Filters the maximum image width to be included in a 'srcset' attribute.
  1117 	 *
  1303 	 *
  1118 	 * @since 4.4.0
  1304 	 * @since 4.4.0
  1119 	 *
  1305 	 *
  1120 	 * @param int   $max_width  The maximum image width to be included in the 'srcset'. Default '1600'.
  1306 	 * @param int   $max_width  The maximum image width to be included in the 'srcset'. Default '2048'.
  1121 	 * @param array $size_array Array of width and height values in pixels (in that order).
  1307 	 * @param int[] $size_array {
  1122 	 */
  1308 	 *     An array of requested width and height values.
  1123 	$max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array );
  1309 	 *
       
  1310 	 *     @type int $0 The width in pixels.
       
  1311 	 *     @type int $1 The height in pixels.
       
  1312 	 * }
       
  1313 	 */
       
  1314 	$max_srcset_image_width = apply_filters( 'max_srcset_image_width', 2048, $size_array );
  1124 
  1315 
  1125 	// Array to hold URL candidates.
  1316 	// Array to hold URL candidates.
  1126 	$sources = array();
  1317 	$sources = array();
  1127 
  1318 
  1128 	/**
  1319 	/**
  1144 			continue;
  1335 			continue;
  1145 		}
  1336 		}
  1146 
  1337 
  1147 		// If the file name is part of the `src`, we've confirmed a match.
  1338 		// If the file name is part of the `src`, we've confirmed a match.
  1148 		if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
  1339 		if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
  1149 			$src_matched = $is_src = true;
  1340 			$src_matched = true;
       
  1341 			$is_src      = true;
  1150 		}
  1342 		}
  1151 
  1343 
  1152 		// Filter out images that are from previous edits.
  1344 		// Filter out images that are from previous edits.
  1153 		if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
  1345 		if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
  1154 			continue;
  1346 			continue;
  1194 	 *                                  either 'w' or 'x'.
  1386 	 *                                  either 'w' or 'x'.
  1195 	 *         @type int    $value      The source width if paired with a 'w' descriptor, or a
  1387 	 *         @type int    $value      The source width if paired with a 'w' descriptor, or a
  1196 	 *                                  pixel density value if paired with an 'x' descriptor.
  1388 	 *                                  pixel density value if paired with an 'x' descriptor.
  1197 	 *     }
  1389 	 *     }
  1198 	 * }
  1390 	 * }
  1199 	 * @param array  $size_array    Array of width and height values in pixels (in that order).
  1391 	 * @param array $size_array     {
       
  1392 	 *     An array of requested width and height values.
       
  1393 	 *
       
  1394 	 *     @type int $0 The width in pixels.
       
  1395 	 *     @type int $1 The height in pixels.
       
  1396 	 * }
  1200 	 * @param string $image_src     The 'src' of the image.
  1397 	 * @param string $image_src     The 'src' of the image.
  1201 	 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
  1398 	 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
  1202 	 * @param int    $attachment_id Image attachment ID or 0.
  1399 	 * @param int    $attachment_id Image attachment ID or 0.
  1203 	 */
  1400 	 */
  1204 	$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
  1401 	$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
  1230  * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
  1427  * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
  1231  *                                    Default null.
  1428  *                                    Default null.
  1232  * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
  1429  * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
  1233  */
  1430  */
  1234 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
  1431 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
  1235 	if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) {
  1432 	$image = wp_get_attachment_image_src( $attachment_id, $size );
       
  1433 
       
  1434 	if ( ! $image ) {
  1236 		return false;
  1435 		return false;
  1237 	}
  1436 	}
  1238 
  1437 
  1239 	if ( ! is_array( $image_meta ) ) {
  1438 	if ( ! is_array( $image_meta ) ) {
  1240 		$image_meta = wp_get_attachment_metadata( $attachment_id );
  1439 		$image_meta = wp_get_attachment_metadata( $attachment_id );
  1302 	 */
  1501 	 */
  1303 	return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );
  1502 	return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );
  1304 }
  1503 }
  1305 
  1504 
  1306 /**
  1505 /**
  1307  * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
  1506  * Determines if the image meta data is for the image source file.
       
  1507  *
       
  1508  * The image meta data is retrieved by attachment post ID. In some cases the post IDs may change.
       
  1509  * For example when the website is exported and imported at another website. Then the
       
  1510  * attachment post IDs that are in post_content for the exported website may not match
       
  1511  * the same attachments at the new website.
       
  1512  *
       
  1513  * @since 5.5.0
       
  1514  *
       
  1515  * @param string $image_location The full path or URI to the image file.
       
  1516  * @param array  $image_meta     The attachment meta data as returned by 'wp_get_attachment_metadata()'.
       
  1517  * @param int    $attachment_id  Optional. The image attachment ID. Default 0.
       
  1518  * @return bool Whether the image meta is for this image file.
       
  1519  */
       
  1520 function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) {
       
  1521 	$match = false;
       
  1522 
       
  1523 	// Ensure the $image_meta is valid.
       
  1524 	if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) {
       
  1525 		// Remove quiery args if image URI.
       
  1526 		list( $image_location ) = explode( '?', $image_location );
       
  1527 
       
  1528 		// Check if the relative image path from the image meta is at the end of $image_location.
       
  1529 		if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) {
       
  1530 			$match = true;
       
  1531 		} else {
       
  1532 			// Retrieve the uploads sub-directory from the full size image.
       
  1533 			$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
       
  1534 
       
  1535 			if ( $dirname ) {
       
  1536 				$dirname = trailingslashit( $dirname );
       
  1537 			}
       
  1538 
       
  1539 			if ( ! empty( $image_meta['original_image'] ) ) {
       
  1540 				$relative_path = $dirname . $image_meta['original_image'];
       
  1541 
       
  1542 				if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
       
  1543 					$match = true;
       
  1544 				}
       
  1545 			}
       
  1546 
       
  1547 			if ( ! $match && ! empty( $image_meta['sizes'] ) ) {
       
  1548 				foreach ( $image_meta['sizes'] as $image_size_data ) {
       
  1549 					$relative_path = $dirname . $image_size_data['file'];
       
  1550 
       
  1551 					if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
       
  1552 						$match = true;
       
  1553 						break;
       
  1554 					}
       
  1555 				}
       
  1556 			}
       
  1557 		}
       
  1558 	}
       
  1559 
       
  1560 	/**
       
  1561 	 * Filter whether an image path or URI matches image meta.
       
  1562 	 *
       
  1563 	 * @since 5.5.0
       
  1564 	 *
       
  1565 	 * @param bool   $match          Whether the image relative path from the image meta
       
  1566 	 *                               matches the end of the URI or path to the image file.
       
  1567 	 * @param string $image_location Full path or URI to the tested image file.
       
  1568 	 * @param array  $image_meta     The image meta data as returned by 'wp_get_attachment_metadata()'.
       
  1569 	 * @param int    $attachment_id  The image attachment ID or 0 if not supplied.
       
  1570 	 */
       
  1571 	return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id );
       
  1572 }
       
  1573 
       
  1574 /**
       
  1575  * Determines an image's width and height dimensions based on the source file.
       
  1576  *
       
  1577  * @since 5.5.0
       
  1578  *
       
  1579  * @param string $image_src     The image source file.
       
  1580  * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
       
  1581  * @param int    $attachment_id Optional. The image attachment ID. Default 0.
       
  1582  * @return array|false Array with first element being the width and second element being the height,
       
  1583  *                     or false if dimensions cannot be determined.
       
  1584  */
       
  1585 function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) {
       
  1586 	if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta, $attachment_id ) ) {
       
  1587 		return false;
       
  1588 	}
       
  1589 
       
  1590 	// Is it a full size image?
       
  1591 	if ( strpos( $image_src, $image_meta['file'] ) !== false ) {
       
  1592 		return array(
       
  1593 			(int) $image_meta['width'],
       
  1594 			(int) $image_meta['height'],
       
  1595 		);
       
  1596 	}
       
  1597 
       
  1598 	if ( ! empty( $image_meta['sizes'] ) ) {
       
  1599 		$src_filename = wp_basename( $image_src );
       
  1600 
       
  1601 		foreach ( $image_meta['sizes'] as $image_size_data ) {
       
  1602 			if ( $src_filename === $image_size_data['file'] ) {
       
  1603 				return array(
       
  1604 					(int) $image_size_data['width'],
       
  1605 					(int) $image_size_data['height'],
       
  1606 				);
       
  1607 			}
       
  1608 		}
       
  1609 	}
       
  1610 
       
  1611 	return false;
       
  1612 }
       
  1613 
       
  1614 /**
       
  1615  * Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
  1308  *
  1616  *
  1309  * @since 4.4.0
  1617  * @since 4.4.0
  1310  *
  1618  *
  1311  * @see wp_image_add_srcset_and_sizes()
  1619  * @see wp_calculate_image_srcset()
  1312  *
  1620  * @see wp_calculate_image_sizes()
  1313  * @param string $content The raw post content to be filtered.
  1621  *
  1314  * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
  1622  * @param string $image         An HTML 'img' element to be filtered.
  1315  */
  1623  * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
  1316 function wp_make_content_images_responsive( $content ) {
  1624  * @param int    $attachment_id Image attachment ID.
  1317 	if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
  1625  * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added.
       
  1626  */
       
  1627 function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
       
  1628 	// Ensure the image meta exists.
       
  1629 	if ( empty( $image_meta['sizes'] ) ) {
       
  1630 		return $image;
       
  1631 	}
       
  1632 
       
  1633 	$image_src         = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
       
  1634 	list( $image_src ) = explode( '?', $image_src );
       
  1635 
       
  1636 	// Return early if we couldn't get the image source.
       
  1637 	if ( ! $image_src ) {
       
  1638 		return $image;
       
  1639 	}
       
  1640 
       
  1641 	// Bail early if an image has been inserted and later edited.
       
  1642 	if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
       
  1643 		strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
       
  1644 
       
  1645 		return $image;
       
  1646 	}
       
  1647 
       
  1648 	$width  = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0;
       
  1649 	$height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
       
  1650 
       
  1651 	if ( $width && $height ) {
       
  1652 		$size_array = array( $width, $height );
       
  1653 	} else {
       
  1654 		$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );
       
  1655 		if ( ! $size_array ) {
       
  1656 			return $image;
       
  1657 		}
       
  1658 	}
       
  1659 
       
  1660 	$srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
       
  1661 
       
  1662 	if ( $srcset ) {
       
  1663 		// Check if there is already a 'sizes' attribute.
       
  1664 		$sizes = strpos( $image, ' sizes=' );
       
  1665 
       
  1666 		if ( ! $sizes ) {
       
  1667 			$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
       
  1668 		}
       
  1669 	}
       
  1670 
       
  1671 	if ( $srcset && $sizes ) {
       
  1672 		// Format the 'srcset' and 'sizes' string and escape attributes.
       
  1673 		$attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );
       
  1674 
       
  1675 		if ( is_string( $sizes ) ) {
       
  1676 			$attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
       
  1677 		}
       
  1678 
       
  1679 		// Add the srcset and sizes attributes to the image markup.
       
  1680 		return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
       
  1681 	}
       
  1682 
       
  1683 	return $image;
       
  1684 }
       
  1685 
       
  1686 /**
       
  1687  * Determine whether to add the `loading` attribute to the specified tag in the specified context.
       
  1688  *
       
  1689  * @since 5.5.0
       
  1690  *
       
  1691  * @param string $tag_name The tag name.
       
  1692  * @param string $context  Additional context, like the current filter name or the function name from where this was called.
       
  1693  * @return bool Whether to add the attribute.
       
  1694  */
       
  1695 function wp_lazy_loading_enabled( $tag_name, $context ) {
       
  1696 	// By default add to all 'img' tags.
       
  1697 	// See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading
       
  1698 	$default = ( 'img' === $tag_name );
       
  1699 
       
  1700 	/**
       
  1701 	 * Filters whether to add the `loading` attribute to the specified tag in the specified context.
       
  1702 	 *
       
  1703 	 * @since 5.5.0
       
  1704 	 *
       
  1705 	 * @param bool   $default  Default value.
       
  1706 	 * @param string $tag_name The tag name.
       
  1707 	 * @param string $context  Additional context, like the current filter name or the function name from where this was called.
       
  1708 	 */
       
  1709 	return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );
       
  1710 }
       
  1711 
       
  1712 /**
       
  1713  * Filters specific tags in post content and modifies their markup.
       
  1714  *
       
  1715  * Modifies HTML tags in post content to include new browser and HTML technologies
       
  1716  * that may not have existed at the time of post creation. These modifications currently
       
  1717  * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags.
       
  1718  * Future similar optimizations should be added/expected here.
       
  1719  *
       
  1720  * @since 5.5.0
       
  1721  *
       
  1722  * @see wp_img_tag_add_width_and_height_attr()
       
  1723  * @see wp_img_tag_add_srcset_and_sizes_attr()
       
  1724  * @see wp_img_tag_add_loading_attr()
       
  1725  *
       
  1726  * @param string $content The HTML content to be filtered.
       
  1727  * @param string $context Optional. Additional context to pass to the filters.
       
  1728  *                        Defaults to `current_filter()` when not set.
       
  1729  * @return string Converted content with images modified.
       
  1730  */
       
  1731 function wp_filter_content_tags( $content, $context = null ) {
       
  1732 	if ( null === $context ) {
       
  1733 		$context = current_filter();
       
  1734 	}
       
  1735 
       
  1736 	$add_loading_attr = wp_lazy_loading_enabled( 'img', $context );
       
  1737 
       
  1738 	if ( false === strpos( $content, '<img' ) ) {
  1318 		return $content;
  1739 		return $content;
  1319 	}
  1740 	}
  1320 
  1741 
  1321 	$selected_images = $attachment_ids = array();
  1742 	if ( ! preg_match_all( '/<img\s[^>]+>/', $content, $matches ) ) {
       
  1743 		return $content;
       
  1744 	}
       
  1745 
       
  1746 	// List of the unique `img` tags found in $content.
       
  1747 	$images = array();
  1322 
  1748 
  1323 	foreach ( $matches[0] as $image ) {
  1749 	foreach ( $matches[0] as $image ) {
  1324 		if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
  1750 		if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {
  1325 			( $attachment_id = absint( $class_id[1] ) ) ) {
  1751 			$attachment_id = absint( $class_id[1] );
  1326 
  1752 
  1327 			/*
  1753 			if ( $attachment_id ) {
  1328 			 * If exactly the same image tag is used more than once, overwrite it.
  1754 				// If exactly the same image tag is used more than once, overwrite it.
  1329 			 * All identical tags will be replaced later with 'str_replace()'.
  1755 				// All identical tags will be replaced later with 'str_replace()'.
  1330 			 */
  1756 				$images[ $image ] = $attachment_id;
  1331 			$selected_images[ $image ] = $attachment_id;
  1757 				continue;
  1332 			// Overwrite the ID when the same image is included more than once.
  1758 			}
  1333 			$attachment_ids[ $attachment_id ] = true;
  1759 		}
  1334 		}
  1760 
  1335 	}
  1761 		$images[ $image ] = 0;
       
  1762 	}
       
  1763 
       
  1764 	// Reduce the array to unique attachment IDs.
       
  1765 	$attachment_ids = array_unique( array_filter( array_values( $images ) ) );
  1336 
  1766 
  1337 	if ( count( $attachment_ids ) > 1 ) {
  1767 	if ( count( $attachment_ids ) > 1 ) {
  1338 		/*
  1768 		/*
  1339 		 * Warm the object cache with post and meta information for all found
  1769 		 * Warm the object cache with post and meta information for all found
  1340 		 * images to avoid making individual database calls.
  1770 		 * images to avoid making individual database calls.
  1341 		 */
  1771 		 */
  1342 		_prime_post_caches( array_keys( $attachment_ids ), false, true );
  1772 		_prime_post_caches( $attachment_ids, false, true );
  1343 	}
  1773 	}
  1344 
  1774 
  1345 	foreach ( $selected_images as $image => $attachment_id ) {
  1775 	foreach ( $images as $image => $attachment_id ) {
  1346 		$image_meta = wp_get_attachment_metadata( $attachment_id );
  1776 		$filtered_image = $image;
  1347 		$content    = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );
  1777 
       
  1778 		// Add 'width' and 'height' attributes if applicable.
       
  1779 		if ( $attachment_id > 0 && false === strpos( $filtered_image, ' width=' ) && false === strpos( $filtered_image, ' height=' ) ) {
       
  1780 			$filtered_image = wp_img_tag_add_width_and_height_attr( $filtered_image, $context, $attachment_id );
       
  1781 		}
       
  1782 
       
  1783 		// Add 'srcset' and 'sizes' attributes if applicable.
       
  1784 		if ( $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) {
       
  1785 			$filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id );
       
  1786 		}
       
  1787 
       
  1788 		// Add 'loading' attribute if applicable.
       
  1789 		if ( $add_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) {
       
  1790 			$filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context );
       
  1791 		}
       
  1792 
       
  1793 		if ( $filtered_image !== $image ) {
       
  1794 			$content = str_replace( $image, $filtered_image, $content );
       
  1795 		}
  1348 	}
  1796 	}
  1349 
  1797 
  1350 	return $content;
  1798 	return $content;
  1351 }
  1799 }
  1352 
  1800 
  1353 /**
  1801 /**
  1354  * Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
  1802  * Adds `loading` attribute to an `img` HTML tag.
  1355  *
  1803  *
  1356  * @since 4.4.0
  1804  * @since 5.5.0
  1357  *
  1805  *
  1358  * @see wp_calculate_image_srcset()
  1806  * @param string $image   The HTML `img` tag where the attribute should be added.
  1359  * @see wp_calculate_image_sizes()
  1807  * @param string $context Additional context to pass to the filters.
  1360  *
  1808  * @return string Converted `img` tag with `loading` attribute added.
  1361  * @param string $image         An HTML 'img' element to be filtered.
  1809  */
  1362  * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
  1810 function wp_img_tag_add_loading_attr( $image, $context ) {
       
  1811 	/**
       
  1812 	 * Filters the `loading` attribute value. Default `lazy`.
       
  1813 	 *
       
  1814 	 * Returning `false` or an empty string will not add the attribute.
       
  1815 	 * Returning `true` will add the default value.
       
  1816 	 *
       
  1817 	 * @since 5.5.0
       
  1818 	 *
       
  1819 	 * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
       
  1820 	 *                             the attribute being omitted for the image. Default is `lazy`.
       
  1821 	 * @param string      $image   The HTML `img` tag to be filtered.
       
  1822 	 * @param string      $context Additional context about how the function was called or where the img tag is.
       
  1823 	 */
       
  1824 	$value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context );
       
  1825 
       
  1826 	if ( $value ) {
       
  1827 		if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
       
  1828 			$value = 'lazy';
       
  1829 		}
       
  1830 
       
  1831 		// Images should have source and dimension attributes for the `loading` attribute to be added.
       
  1832 		if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) {
       
  1833 			return $image;
       
  1834 		}
       
  1835 
       
  1836 		return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image );
       
  1837 	}
       
  1838 
       
  1839 	return $image;
       
  1840 }
       
  1841 
       
  1842 /**
       
  1843  * Adds `width` and `height` attributes to an `img` HTML tag.
       
  1844  *
       
  1845  * @since 5.5.0
       
  1846  *
       
  1847  * @param string $image         The HTML `img` tag where the attribute should be added.
       
  1848  * @param string $context       Additional context to pass to the filters.
  1363  * @param int    $attachment_id Image attachment ID.
  1849  * @param int    $attachment_id Image attachment ID.
  1364  * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added.
  1850  * @return string Converted 'img' element with 'width' and 'height' attributes added.
  1365  */
  1851  */
  1366 function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
  1852 function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id ) {
  1367 	// Ensure the image meta exists.
       
  1368 	if ( empty( $image_meta['sizes'] ) ) {
       
  1369 		return $image;
       
  1370 	}
       
  1371 
       
  1372 	$image_src         = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
  1853 	$image_src         = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
  1373 	list( $image_src ) = explode( '?', $image_src );
  1854 	list( $image_src ) = explode( '?', $image_src );
  1374 
  1855 
  1375 	// Return early if we couldn't get the image source.
  1856 	// Return early if we couldn't get the image source.
  1376 	if ( ! $image_src ) {
  1857 	if ( ! $image_src ) {
  1377 		return $image;
  1858 		return $image;
  1378 	}
  1859 	}
  1379 
  1860 
  1380 	// Bail early if an image has been inserted and later edited.
  1861 	/**
  1381 	if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
  1862 	 * Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`.
  1382 		strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
  1863 	 *
  1383 
  1864 	 * Returning anything else than `true` will not add the attributes.
  1384 		return $image;
  1865 	 *
  1385 	}
  1866 	 * @since 5.5.0
  1386 
  1867 	 *
  1387 	$width  = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0;
  1868 	 * @param bool   $value         The filtered value, defaults to `true`.
  1388 	$height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
  1869 	 * @param string $image         The HTML `img` tag where the attribute should be added.
  1389 
  1870 	 * @param string $context       Additional context about how the function was called or where the img tag is.
  1390 	if ( ! $width || ! $height ) {
  1871 	 * @param int    $attachment_id The image attachment ID.
  1391 		/*
  1872 	 */
  1392 		 * If attempts to parse the size value failed, attempt to use the image meta data to match
  1873 	$add = apply_filters( 'wp_img_tag_add_width_and_height_attr', true, $image, $context, $attachment_id );
  1393 		 * the image file name from 'src' against the available sizes for an attachment.
  1874 
  1394 		 */
  1875 	if ( true === $add ) {
  1395 		$image_filename = wp_basename( $image_src );
  1876 		$image_meta = wp_get_attachment_metadata( $attachment_id );
  1396 
  1877 		$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );
  1397 		if ( $image_filename === wp_basename( $image_meta['file'] ) ) {
  1878 
  1398 			$width  = (int) $image_meta['width'];
  1879 		if ( $size_array ) {
  1399 			$height = (int) $image_meta['height'];
  1880 			$hw = trim( image_hwstring( $size_array[0], $size_array[1] ) );
  1400 		} else {
  1881 			return str_replace( '<img', "<img {$hw}", $image );
  1401 			foreach ( $image_meta['sizes'] as $image_size_data ) {
  1882 		}
  1402 				if ( $image_filename === $image_size_data['file'] ) {
  1883 	}
  1403 					$width  = (int) $image_size_data['width'];
  1884 
  1404 					$height = (int) $image_size_data['height'];
  1885 	return $image;
  1405 					break;
  1886 }
  1406 				}
  1887 
  1407 			}
  1888 /**
  1408 		}
  1889  * Adds `srcset` and `sizes` attributes to an existing `img` HTML tag.
  1409 	}
  1890  *
  1410 
  1891  * @since 5.5.0
  1411 	if ( ! $width || ! $height ) {
  1892  *
  1412 		return $image;
  1893  * @param string $image         The HTML `img` tag where the attribute should be added.
  1413 	}
  1894  * @param string $context       Additional context to pass to the filters.
  1414 
  1895  * @param int    $attachment_id Image attachment ID.
  1415 	$size_array = array( $width, $height );
  1896  * @return string Converted 'img' element with 'loading' attribute added.
  1416 	$srcset     = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
  1897  */
  1417 
  1898 function wp_img_tag_add_srcset_and_sizes_attr( $image, $context, $attachment_id ) {
  1418 	if ( $srcset ) {
  1899 	/**
  1419 		// Check if there is already a 'sizes' attribute.
  1900 	 * Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`.
  1420 		$sizes = strpos( $image, ' sizes=' );
  1901 	 *
  1421 
  1902 	 * Returning anything else than `true` will not add the attributes.
  1422 		if ( ! $sizes ) {
  1903 	 *
  1423 			$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
  1904 	 * @since 5.5.0
  1424 		}
  1905 	 *
  1425 	}
  1906 	 * @param bool   $value         The filtered value, defaults to `true`.
  1426 
  1907 	 * @param string $image         The HTML `img` tag where the attribute should be added.
  1427 	if ( $srcset && $sizes ) {
  1908 	 * @param string $context       Additional context about how the function was called or where the img tag is.
  1428 		// Format the 'srcset' and 'sizes' string and escape attributes.
  1909 	 * @param int    $attachment_id The image attachment ID.
  1429 		$attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );
  1910 	 */
  1430 
  1911 	$add = apply_filters( 'wp_img_tag_add_srcset_and_sizes_attr', true, $image, $context, $attachment_id );
  1431 		if ( is_string( $sizes ) ) {
  1912 
  1432 			$attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
  1913 	if ( true === $add ) {
  1433 		}
  1914 		$image_meta = wp_get_attachment_metadata( $attachment_id );
  1434 
  1915 		return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id );
  1435 		// Add 'srcset' and 'sizes' attributes to the image markup.
       
  1436 		$image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
       
  1437 	}
  1916 	}
  1438 
  1917 
  1439 	return $image;
  1918 	return $image;
  1440 }
  1919 }
  1441 
  1920 
  1446  * action hooks to dynamically add/remove itself so as to only filter post thumbnails.
  1925  * action hooks to dynamically add/remove itself so as to only filter post thumbnails.
  1447  *
  1926  *
  1448  * @ignore
  1927  * @ignore
  1449  * @since 2.9.0
  1928  * @since 2.9.0
  1450  *
  1929  *
  1451  * @param array $attr Thumbnail attributes including src, class, alt, title.
  1930  * @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name.
  1452  * @return array Modified array of attributes including the new 'wp-post-image' class.
  1931  * @return string[] Modified array of attributes including the new 'wp-post-image' class.
  1453  */
  1932  */
  1454 function _wp_post_thumbnail_class_filter( $attr ) {
  1933 function _wp_post_thumbnail_class_filter( $attr ) {
  1455 	$attr['class'] .= ' wp-post-image';
  1934 	$attr['class'] .= ' wp-post-image';
  1456 	return $attr;
  1935 	return $attr;
  1457 }
  1936 }
  1461  * filter hook. Internal use only.
  1940  * filter hook. Internal use only.
  1462  *
  1941  *
  1463  * @ignore
  1942  * @ignore
  1464  * @since 2.9.0
  1943  * @since 2.9.0
  1465  *
  1944  *
  1466  * @param array $attr Thumbnail attributes including src, class, alt, title.
  1945  * @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name.
  1467  */
  1946  */
  1468 function _wp_post_thumbnail_class_filter_add( $attr ) {
  1947 function _wp_post_thumbnail_class_filter_add( $attr ) {
  1469 	add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
  1948 	add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
  1470 }
  1949 }
  1471 
  1950 
  1474  * filter hook. Internal use only.
  1953  * filter hook. Internal use only.
  1475  *
  1954  *
  1476  * @ignore
  1955  * @ignore
  1477  * @since 2.9.0
  1956  * @since 2.9.0
  1478  *
  1957  *
  1479  * @param array $attr Thumbnail attributes including src, class, alt, title.
  1958  * @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name.
  1480  */
  1959  */
  1481 function _wp_post_thumbnail_class_filter_remove( $attr ) {
  1960 function _wp_post_thumbnail_class_filter_remove( $attr ) {
  1482 	remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
  1961 	remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
  1483 }
  1962 }
  1484 
  1963 
  1537 	 * @param string $output  The caption output. Default empty.
  2016 	 * @param string $output  The caption output. Default empty.
  1538 	 * @param array  $attr    Attributes of the caption shortcode.
  2017 	 * @param array  $attr    Attributes of the caption shortcode.
  1539 	 * @param string $content The image element, possibly wrapped in a hyperlink.
  2018 	 * @param string $content The image element, possibly wrapped in a hyperlink.
  1540 	 */
  2019 	 */
  1541 	$output = apply_filters( 'img_caption_shortcode', '', $attr, $content );
  2020 	$output = apply_filters( 'img_caption_shortcode', '', $attr, $content );
  1542 	if ( $output != '' ) {
  2021 
       
  2022 	if ( ! empty( $output ) ) {
  1543 		return $output;
  2023 		return $output;
  1544 	}
  2024 	}
  1545 
  2025 
  1546 	$atts = shortcode_atts(
  2026 	$atts = shortcode_atts(
  1547 		array(
  2027 		array(
  1555 		$attr,
  2035 		$attr,
  1556 		'caption'
  2036 		'caption'
  1557 	);
  2037 	);
  1558 
  2038 
  1559 	$atts['width'] = (int) $atts['width'];
  2039 	$atts['width'] = (int) $atts['width'];
       
  2040 
  1560 	if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) {
  2041 	if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) {
  1561 		return $content;
  2042 		return $content;
  1562 	}
  2043 	}
  1563 
  2044 
  1564 	$id = $caption_id = $describedby = '';
  2045 	$id          = '';
       
  2046 	$caption_id  = '';
       
  2047 	$describedby = '';
  1565 
  2048 
  1566 	if ( $atts['id'] ) {
  2049 	if ( $atts['id'] ) {
  1567 		$atts['id'] = sanitize_html_class( $atts['id'] );
  2050 		$atts['id'] = sanitize_html_class( $atts['id'] );
  1568 		$id         = 'id="' . esc_attr( $atts['id'] ) . '" ';
  2051 		$id         = 'id="' . esc_attr( $atts['id'] ) . '" ';
  1569 	}
  2052 	}
  1580 	}
  2063 	}
  1581 
  2064 
  1582 	$class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );
  2065 	$class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );
  1583 
  2066 
  1584 	$html5 = current_theme_supports( 'html5', 'caption' );
  2067 	$html5 = current_theme_supports( 'html5', 'caption' );
  1585 	// HTML5 captions never added the extra 10px to the image width
  2068 	// HTML5 captions never added the extra 10px to the image width.
  1586 	$width = $html5 ? $atts['width'] : ( 10 + $atts['width'] );
  2069 	$width = $html5 ? $atts['width'] : ( 10 + $atts['width'] );
  1587 
  2070 
  1588 	/**
  2071 	/**
  1589 	 * Filters the width of an image's caption.
  2072 	 * Filters the width of an image's caption.
  1590 	 *
  2073 	 *
  1601 	 * @param string $content  The image element, possibly wrapped in a hyperlink.
  2084 	 * @param string $content  The image element, possibly wrapped in a hyperlink.
  1602 	 */
  2085 	 */
  1603 	$caption_width = apply_filters( 'img_caption_shortcode_width', $width, $atts, $content );
  2086 	$caption_width = apply_filters( 'img_caption_shortcode_width', $width, $atts, $content );
  1604 
  2087 
  1605 	$style = '';
  2088 	$style = '';
       
  2089 
  1606 	if ( $caption_width ) {
  2090 	if ( $caption_width ) {
  1607 		$style = 'style="width: ' . (int) $caption_width . 'px" ';
  2091 		$style = 'style="width: ' . (int) $caption_width . 'px" ';
  1608 	}
  2092 	}
  1609 
  2093 
  1610 	if ( $html5 ) {
  2094 	if ( $html5 ) {
  1647  * This implements the functionality of the Gallery Shortcode for displaying
  2131  * This implements the functionality of the Gallery Shortcode for displaying
  1648  * WordPress images on a post.
  2132  * WordPress images on a post.
  1649  *
  2133  *
  1650  * @since 2.5.0
  2134  * @since 2.5.0
  1651  *
  2135  *
  1652  * @staticvar int $instance
       
  1653  *
       
  1654  * @param array $attr {
  2136  * @param array $attr {
  1655  *     Attributes of the gallery shortcode.
  2137  *     Attributes of the gallery shortcode.
  1656  *
  2138  *
  1657  *     @type string       $order      Order of the images in the gallery. Default 'ASC'. Accepts 'ASC', 'DESC'.
  2139  *     @type string       $order      Order of the images in the gallery. Default 'ASC'. Accepts 'ASC', 'DESC'.
  1658  *     @type string       $orderby    The field to use when ordering the images. Default 'menu_order ID'.
  2140  *     @type string       $orderby    The field to use when ordering the images. Default 'menu_order ID'.
  1703 	 * @param string $output   The gallery output. Default empty.
  2185 	 * @param string $output   The gallery output. Default empty.
  1704 	 * @param array  $attr     Attributes of the gallery shortcode.
  2186 	 * @param array  $attr     Attributes of the gallery shortcode.
  1705 	 * @param int    $instance Unique numeric ID of this gallery shortcode instance.
  2187 	 * @param int    $instance Unique numeric ID of this gallery shortcode instance.
  1706 	 */
  2188 	 */
  1707 	$output = apply_filters( 'post_gallery', '', $attr, $instance );
  2189 	$output = apply_filters( 'post_gallery', '', $attr, $instance );
  1708 	if ( $output != '' ) {
  2190 
       
  2191 	if ( ! empty( $output ) ) {
  1709 		return $output;
  2192 		return $output;
  1710 	}
  2193 	}
  1711 
  2194 
  1712 	$html5 = current_theme_supports( 'html5', 'gallery' );
  2195 	$html5 = current_theme_supports( 'html5', 'gallery' );
  1713 	$atts  = shortcode_atts(
  2196 	$atts  = shortcode_atts(
  1776 	}
  2259 	}
  1777 
  2260 
  1778 	if ( is_feed() ) {
  2261 	if ( is_feed() ) {
  1779 		$output = "\n";
  2262 		$output = "\n";
  1780 		foreach ( $attachments as $att_id => $attachment ) {
  2263 		foreach ( $attachments as $att_id => $attachment ) {
  1781 			$output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n";
  2264 			if ( ! empty( $atts['link'] ) ) {
       
  2265 				if ( 'none' === $atts['link'] ) {
       
  2266 					$output .= wp_get_attachment_image( $att_id, $atts['size'], false, $attr );
       
  2267 				} else {
       
  2268 					$output .= wp_get_attachment_link( $att_id, $atts['size'], false );
       
  2269 				}
       
  2270 			} else {
       
  2271 				$output .= wp_get_attachment_link( $att_id, $atts['size'], true );
       
  2272 			}
       
  2273 			$output .= "\n";
  1782 		}
  2274 		}
  1783 		return $output;
  2275 		return $output;
  1784 	}
  2276 	}
  1785 
  2277 
  1786 	$itemtag    = tag_escape( $atts['itemtag'] );
  2278 	$itemtag    = tag_escape( $atts['itemtag'] );
  1813 	 * @param bool $print Whether to print default gallery styles.
  2305 	 * @param bool $print Whether to print default gallery styles.
  1814 	 *                    Defaults to false if the theme supports HTML5 galleries.
  2306 	 *                    Defaults to false if the theme supports HTML5 galleries.
  1815 	 *                    Otherwise, defaults to true.
  2307 	 *                    Otherwise, defaults to true.
  1816 	 */
  2308 	 */
  1817 	if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) {
  2309 	if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) {
       
  2310 		$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
       
  2311 
  1818 		$gallery_style = "
  2312 		$gallery_style = "
  1819 		<style type='text/css'>
  2313 		<style{$type_attr}>
  1820 			#{$selector} {
  2314 			#{$selector} {
  1821 				margin: auto;
  2315 				margin: auto;
  1822 			}
  2316 			}
  1823 			#{$selector} .gallery-item {
  2317 			#{$selector} .gallery-item {
  1824 				float: {$float};
  2318 				float: {$float};
  1848 	 *                              for the gallery shortcode output.
  2342 	 *                              for the gallery shortcode output.
  1849 	 */
  2343 	 */
  1850 	$output = apply_filters( 'gallery_style', $gallery_style . $gallery_div );
  2344 	$output = apply_filters( 'gallery_style', $gallery_style . $gallery_div );
  1851 
  2345 
  1852 	$i = 0;
  2346 	$i = 0;
       
  2347 
  1853 	foreach ( $attachments as $id => $attachment ) {
  2348 	foreach ( $attachments as $id => $attachment ) {
  1854 
  2349 
  1855 		$attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : '';
  2350 		$attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : '';
       
  2351 
  1856 		if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
  2352 		if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
  1857 			$image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
  2353 			$image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
  1858 		} elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
  2354 		} elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
  1859 			$image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
  2355 			$image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
  1860 		} else {
  2356 		} else {
  1861 			$image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
  2357 			$image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
  1862 		}
  2358 		}
       
  2359 
  1863 		$image_meta = wp_get_attachment_metadata( $id );
  2360 		$image_meta = wp_get_attachment_metadata( $id );
  1864 
  2361 
  1865 		$orientation = '';
  2362 		$orientation = '';
       
  2363 
  1866 		if ( isset( $image_meta['height'], $image_meta['width'] ) ) {
  2364 		if ( isset( $image_meta['height'], $image_meta['width'] ) ) {
  1867 			$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
  2365 			$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
  1868 		}
  2366 		}
       
  2367 
  1869 		$output .= "<{$itemtag} class='gallery-item'>";
  2368 		$output .= "<{$itemtag} class='gallery-item'>";
  1870 		$output .= "
  2369 		$output .= "
  1871 			<{$icontag} class='gallery-icon {$orientation}'>
  2370 			<{$icontag} class='gallery-icon {$orientation}'>
  1872 				$image_output
  2371 				$image_output
  1873 			</{$icontag}>";
  2372 			</{$icontag}>";
       
  2373 
  1874 		if ( $captiontag && trim( $attachment->post_excerpt ) ) {
  2374 		if ( $captiontag && trim( $attachment->post_excerpt ) ) {
  1875 			$output .= "
  2375 			$output .= "
  1876 				<{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'>
  2376 				<{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'>
  1877 				" . wptexturize( $attachment->post_excerpt ) . "
  2377 				" . wptexturize( $attachment->post_excerpt ) . "
  1878 				</{$captiontag}>";
  2378 				</{$captiontag}>";
  1879 		}
  2379 		}
       
  2380 
  1880 		$output .= "</{$itemtag}>";
  2381 		$output .= "</{$itemtag}>";
  1881 		if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) {
  2382 
       
  2383 		if ( ! $html5 && $columns > 0 && 0 === ++$i % $columns ) {
  1882 			$output .= '<br style="clear: both" />';
  2384 			$output .= '<br style="clear: both" />';
  1883 		}
  2385 		}
  1884 	}
  2386 	}
  1885 
  2387 
  1886 	if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) {
  2388 	if ( ! $html5 && $columns > 0 && 0 !== $i % $columns ) {
  1887 		$output .= "
  2389 		$output .= "
  1888 			<br style='clear: both' />";
  2390 			<br style='clear: both' />";
  1889 	}
  2391 	}
  1890 
  2392 
  1891 	$output .= "
  2393 	$output .= "
  1906 	<img src="{{ data.thumb.src }}" alt="" />
  2408 	<img src="{{ data.thumb.src }}" alt="" />
  1907 	<# } #>
  2409 	<# } #>
  1908 	<div class="wp-playlist-caption">
  2410 	<div class="wp-playlist-caption">
  1909 		<span class="wp-playlist-item-meta wp-playlist-item-title">
  2411 		<span class="wp-playlist-item-meta wp-playlist-item-title">
  1910 		<?php
  2412 		<?php
  1911 			/* translators: playlist item title */
  2413 			/* translators: %s: Playlist item title. */
  1912 			printf( _x( '&#8220;%s&#8221;', 'playlist item title' ), '{{ data.title }}' );
  2414 			printf( _x( '&#8220;%s&#8221;', 'playlist item title' ), '{{ data.title }}' );
  1913 		?>
  2415 		?>
  1914 		</span>
  2416 		</span>
  1915 		<# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #>
  2417 		<# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #>
  1916 		<# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #>
  2418 		<# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #>
  1923 			<# if ( data.caption ) { #>
  2425 			<# if ( data.caption ) { #>
  1924 				{{ data.caption }}
  2426 				{{ data.caption }}
  1925 			<# } else { #>
  2427 			<# } else { #>
  1926 				<span class="wp-playlist-item-title">
  2428 				<span class="wp-playlist-item-title">
  1927 				<?php
  2429 				<?php
  1928 					/* translators: playlist item title */
  2430 					/* translators: %s: Playlist item title. */
  1929 					printf( _x( '&#8220;%s&#8221;', 'playlist item title' ), '{{{ data.title }}}' );
  2431 					printf( _x( '&#8220;%s&#8221;', 'playlist item title' ), '{{{ data.title }}}' );
  1930 				?>
  2432 				?>
  1931 				</span>
  2433 				</span>
  1932 				<# if ( data.artists && data.meta.artist ) { #>
  2434 				<# if ( data.artists && data.meta.artist ) { #>
  1933 				<span class="wp-playlist-item-artist"> &mdash; {{ data.meta.artist }}</span>
  2435 				<span class="wp-playlist-item-artist"> &mdash; {{ data.meta.artist }}</span>
  1966  * a collection of WordPress audio or video files in a post.
  2468  * a collection of WordPress audio or video files in a post.
  1967  *
  2469  *
  1968  * @since 3.9.0
  2470  * @since 3.9.0
  1969  *
  2471  *
  1970  * @global int $content_width
  2472  * @global int $content_width
  1971  * @staticvar int $instance
       
  1972  *
  2473  *
  1973  * @param array $attr {
  2474  * @param array $attr {
  1974  *     Array of default playlist attributes.
  2475  *     Array of default playlist attributes.
  1975  *
  2476  *
  1976  *     @type string  $type         Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'.
  2477  *     @type string  $type         Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'.
  2012 	}
  2513 	}
  2013 
  2514 
  2014 	/**
  2515 	/**
  2015 	 * Filters the playlist output.
  2516 	 * Filters the playlist output.
  2016 	 *
  2517 	 *
  2017 	 * Passing a non-empty value to the filter will short-circuit generation
  2518 	 * Returning a non-empty value from the filter will short-circuit generation
  2018 	 * of the default playlist output, returning the passed value instead.
  2519 	 * of the default playlist output, returning the passed value instead.
  2019 	 *
  2520 	 *
  2020 	 * @since 3.9.0
  2521 	 * @since 3.9.0
  2021 	 * @since 4.2.0 The `$instance` parameter was added.
  2522 	 * @since 4.2.0 The `$instance` parameter was added.
  2022 	 *
  2523 	 *
  2023 	 * @param string $output   Playlist output. Default empty.
  2524 	 * @param string $output   Playlist output. Default empty.
  2024 	 * @param array  $attr     An array of shortcode attributes.
  2525 	 * @param array  $attr     An array of shortcode attributes.
  2025 	 * @param int    $instance Unique numeric ID of this playlist shortcode instance.
  2526 	 * @param int    $instance Unique numeric ID of this playlist shortcode instance.
  2026 	 */
  2527 	 */
  2027 	$output = apply_filters( 'post_playlist', '', $attr, $instance );
  2528 	$output = apply_filters( 'post_playlist', '', $attr, $instance );
  2028 	if ( $output != '' ) {
  2529 
       
  2530 	if ( ! empty( $output ) ) {
  2029 		return $output;
  2531 		return $output;
  2030 	}
  2532 	}
  2031 
  2533 
  2032 	$atts = shortcode_atts(
  2534 	$atts = shortcode_atts(
  2033 		array(
  2535 		array(
  2047 		'playlist'
  2549 		'playlist'
  2048 	);
  2550 	);
  2049 
  2551 
  2050 	$id = intval( $atts['id'] );
  2552 	$id = intval( $atts['id'] );
  2051 
  2553 
  2052 	if ( $atts['type'] !== 'audio' ) {
  2554 	if ( 'audio' !== $atts['type'] ) {
  2053 		$atts['type'] = 'video';
  2555 		$atts['type'] = 'video';
  2054 	}
  2556 	}
  2055 
  2557 
  2056 	$args = array(
  2558 	$args = array(
  2057 		'post_status'    => 'inherit',
  2559 		'post_status'    => 'inherit',
  2088 			$output .= wp_get_attachment_link( $att_id ) . "\n";
  2590 			$output .= wp_get_attachment_link( $att_id ) . "\n";
  2089 		}
  2591 		}
  2090 		return $output;
  2592 		return $output;
  2091 	}
  2593 	}
  2092 
  2594 
  2093 	$outer = 22; // default padding and border of wrapper
  2595 	$outer = 22; // Default padding and border of wrapper.
  2094 
  2596 
  2095 	$default_width  = 640;
  2597 	$default_width  = 640;
  2096 	$default_height = 360;
  2598 	$default_height = 360;
  2097 
  2599 
  2098 	$theme_width  = empty( $content_width ) ? $default_width : ( $content_width - $outer );
  2600 	$theme_width  = empty( $content_width ) ? $default_width : ( $content_width - $outer );
  2099 	$theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width );
  2601 	$theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width );
  2100 
  2602 
  2101 	$data = array(
  2603 	$data = array(
  2102 		'type'         => $atts['type'],
  2604 		'type'         => $atts['type'],
  2103 		// don't pass strings to JSON, will be truthy in JS
  2605 		// Don't pass strings to JSON, will be truthy in JS.
  2104 		'tracklist'    => wp_validate_boolean( $atts['tracklist'] ),
  2606 		'tracklist'    => wp_validate_boolean( $atts['tracklist'] ),
  2105 		'tracknumbers' => wp_validate_boolean( $atts['tracknumbers'] ),
  2607 		'tracknumbers' => wp_validate_boolean( $atts['tracknumbers'] ),
  2106 		'images'       => wp_validate_boolean( $atts['images'] ),
  2608 		'images'       => wp_validate_boolean( $atts['images'] ),
  2107 		'artists'      => wp_validate_boolean( $atts['artists'] ),
  2609 		'artists'      => wp_validate_boolean( $atts['artists'] ),
  2108 	);
  2610 	);
  2238 	 */
  2740 	 */
  2239 	return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url );
  2741 	return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url );
  2240 }
  2742 }
  2241 
  2743 
  2242 /**
  2744 /**
  2243  * Returns a filtered list of WP-supported audio formats.
  2745  * Returns a filtered list of supported audio formats.
  2244  *
  2746  *
  2245  * @since 3.6.0
  2747  * @since 3.6.0
  2246  *
  2748  *
  2247  * @return array Supported audio formats.
  2749  * @return string[] Supported audio formats.
  2248  */
  2750  */
  2249 function wp_get_audio_extensions() {
  2751 function wp_get_audio_extensions() {
  2250 	/**
  2752 	/**
  2251 	 * Filters the list of supported audio formats.
  2753 	 * Filters the list of supported audio formats.
  2252 	 *
  2754 	 *
  2253 	 * @since 3.6.0
  2755 	 * @since 3.6.0
  2254 	 *
  2756 	 *
  2255 	 * @param array $extensions An array of supported audio formats. Defaults are
  2757 	 * @param string[] $extensions An array of supported audio formats. Defaults are
  2256 	 *                          'mp3', 'ogg', 'flac', 'm4a', 'wav'.
  2758 	 *                            'mp3', 'ogg', 'flac', 'm4a', 'wav'.
  2257 	 */
  2759 	 */
  2258 	return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'flac', 'm4a', 'wav' ) );
  2760 	return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'flac', 'm4a', 'wav' ) );
  2259 }
  2761 }
  2260 
  2762 
  2261 /**
  2763 /**
  2263  *
  2765  *
  2264  * @since 3.9.0
  2766  * @since 3.9.0
  2265  *
  2767  *
  2266  * @param WP_Post $attachment The current attachment, provided for context.
  2768  * @param WP_Post $attachment The current attachment, provided for context.
  2267  * @param string  $context    Optional. The context. Accepts 'edit', 'display'. Default 'display'.
  2769  * @param string  $context    Optional. The context. Accepts 'edit', 'display'. Default 'display'.
  2268  * @return array Key/value pairs of field keys to labels.
  2770  * @return string[] Key/value pairs of field keys to labels.
  2269  */
  2771  */
  2270 function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) {
  2772 function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) {
  2271 	$fields = array(
  2773 	$fields = array(
  2272 		'artist' => __( 'Artist' ),
  2774 		'artist' => __( 'Artist' ),
  2273 		'album'  => __( 'Album' ),
  2775 		'album'  => __( 'Album' ),
  2298  *
  2800  *
  2299  * This implements the functionality of the Audio Shortcode for displaying
  2801  * This implements the functionality of the Audio Shortcode for displaying
  2300  * WordPress mp3s in a post.
  2802  * WordPress mp3s in a post.
  2301  *
  2803  *
  2302  * @since 3.6.0
  2804  * @since 3.6.0
  2303  *
       
  2304  * @staticvar int $instance
       
  2305  *
  2805  *
  2306  * @param array  $attr {
  2806  * @param array  $attr {
  2307  *     Attributes of the audio shortcode.
  2807  *     Attributes of the audio shortcode.
  2308  *
  2808  *
  2309  *     @type string $src      URL to the source of the audio file. Default empty.
  2809  *     @type string $src      URL to the source of the audio file. Default empty.
  2333 	 * @param array  $attr     Attributes of the shortcode. @see wp_audio_shortcode()
  2833 	 * @param array  $attr     Attributes of the shortcode. @see wp_audio_shortcode()
  2334 	 * @param string $content  Shortcode content.
  2834 	 * @param string $content  Shortcode content.
  2335 	 * @param int    $instance Unique numeric ID of this audio shortcode instance.
  2835 	 * @param int    $instance Unique numeric ID of this audio shortcode instance.
  2336 	 */
  2836 	 */
  2337 	$override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance );
  2837 	$override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance );
       
  2838 
  2338 	if ( '' !== $override ) {
  2839 	if ( '' !== $override ) {
  2339 		return $override;
  2840 		return $override;
  2340 	}
  2841 	}
  2341 
  2842 
  2342 	$audio = null;
  2843 	$audio = null;
  2357 	$atts = shortcode_atts( $defaults_atts, $attr, 'audio' );
  2858 	$atts = shortcode_atts( $defaults_atts, $attr, 'audio' );
  2358 
  2859 
  2359 	$primary = false;
  2860 	$primary = false;
  2360 	if ( ! empty( $atts['src'] ) ) {
  2861 	if ( ! empty( $atts['src'] ) ) {
  2361 		$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
  2862 		$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
  2362 		if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) {
  2863 
       
  2864 		if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
  2363 			return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
  2865 			return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
  2364 		}
  2866 		}
       
  2867 
  2365 		$primary = true;
  2868 		$primary = true;
  2366 		array_unshift( $default_types, 'src' );
  2869 		array_unshift( $default_types, 'src' );
  2367 	} else {
  2870 	} else {
  2368 		foreach ( $default_types as $ext ) {
  2871 		foreach ( $default_types as $ext ) {
  2369 			if ( ! empty( $atts[ $ext ] ) ) {
  2872 			if ( ! empty( $atts[ $ext ] ) ) {
  2370 				$type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() );
  2873 				$type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() );
       
  2874 
  2371 				if ( strtolower( $type['ext'] ) === $ext ) {
  2875 				if ( strtolower( $type['ext'] ) === $ext ) {
  2372 					$primary = true;
  2876 					$primary = true;
  2373 				}
  2877 				}
  2374 			}
  2878 			}
  2375 		}
  2879 		}
  2376 	}
  2880 	}
  2377 
  2881 
  2378 	if ( ! $primary ) {
  2882 	if ( ! $primary ) {
  2379 		$audios = get_attached_media( 'audio', $post_id );
  2883 		$audios = get_attached_media( 'audio', $post_id );
       
  2884 
  2380 		if ( empty( $audios ) ) {
  2885 		if ( empty( $audios ) ) {
  2381 			return;
  2886 			return;
  2382 		}
  2887 		}
  2383 
  2888 
  2384 		$audio       = reset( $audios );
  2889 		$audio       = reset( $audios );
  2385 		$atts['src'] = wp_get_attachment_url( $audio->ID );
  2890 		$atts['src'] = wp_get_attachment_url( $audio->ID );
       
  2891 
  2386 		if ( empty( $atts['src'] ) ) {
  2892 		if ( empty( $atts['src'] ) ) {
  2387 			return;
  2893 			return;
  2388 		}
  2894 		}
  2389 
  2895 
  2390 		array_unshift( $default_types, 'src' );
  2896 		array_unshift( $default_types, 'src' );
  2396 	 * @since 3.6.0
  2902 	 * @since 3.6.0
  2397 	 *
  2903 	 *
  2398 	 * @param string $library Media library used for the audio shortcode.
  2904 	 * @param string $library Media library used for the audio shortcode.
  2399 	 */
  2905 	 */
  2400 	$library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
  2906 	$library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
       
  2907 
  2401 	if ( 'mediaelement' === $library && did_action( 'init' ) ) {
  2908 	if ( 'mediaelement' === $library && did_action( 'init' ) ) {
  2402 		wp_enqueue_style( 'wp-mediaelement' );
  2909 		wp_enqueue_style( 'wp-mediaelement' );
  2403 		wp_enqueue_script( 'wp-mediaelement' );
  2910 		wp_enqueue_script( 'wp-mediaelement' );
  2404 	}
  2911 	}
  2405 
  2912 
  2421 		'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
  2928 		'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
  2422 		'preload'  => $atts['preload'],
  2929 		'preload'  => $atts['preload'],
  2423 		'style'    => $atts['style'],
  2930 		'style'    => $atts['style'],
  2424 	);
  2931 	);
  2425 
  2932 
  2426 	// These ones should just be omitted altogether if they are blank
  2933 	// These ones should just be omitted altogether if they are blank.
  2427 	foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) {
  2934 	foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) {
  2428 		if ( empty( $html_atts[ $a ] ) ) {
  2935 		if ( empty( $html_atts[ $a ] ) ) {
  2429 			unset( $html_atts[ $a ] );
  2936 			unset( $html_atts[ $a ] );
  2430 		}
  2937 		}
  2431 	}
  2938 	}
  2432 
  2939 
  2433 	$attr_strings = array();
  2940 	$attr_strings = array();
       
  2941 
  2434 	foreach ( $html_atts as $k => $v ) {
  2942 	foreach ( $html_atts as $k => $v ) {
  2435 		$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
  2943 		$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
  2436 	}
  2944 	}
  2437 
  2945 
  2438 	$html = '';
  2946 	$html = '';
       
  2947 
  2439 	if ( 'mediaelement' === $library && 1 === $instance ) {
  2948 	if ( 'mediaelement' === $library && 1 === $instance ) {
  2440 		$html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
  2949 		$html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
  2441 	}
  2950 	}
       
  2951 
  2442 	$html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) );
  2952 	$html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) );
  2443 
  2953 
  2444 	$fileurl = '';
  2954 	$fileurl = '';
  2445 	$source  = '<source type="%s" src="%s" />';
  2955 	$source  = '<source type="%s" src="%s" />';
       
  2956 
  2446 	foreach ( $default_types as $fallback ) {
  2957 	foreach ( $default_types as $fallback ) {
  2447 		if ( ! empty( $atts[ $fallback ] ) ) {
  2958 		if ( ! empty( $atts[ $fallback ] ) ) {
  2448 			if ( empty( $fileurl ) ) {
  2959 			if ( empty( $fileurl ) ) {
  2449 				$fileurl = $atts[ $fallback ];
  2960 				$fileurl = $atts[ $fallback ];
  2450 			}
  2961 			}
       
  2962 
  2451 			$type  = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
  2963 			$type  = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
  2452 			$url   = add_query_arg( '_', $instance, $atts[ $fallback ] );
  2964 			$url   = add_query_arg( '_', $instance, $atts[ $fallback ] );
  2453 			$html .= sprintf( $source, $type['type'], esc_url( $url ) );
  2965 			$html .= sprintf( $source, $type['type'], esc_url( $url ) );
  2454 		}
  2966 		}
  2455 	}
  2967 	}
  2456 
  2968 
  2457 	if ( 'mediaelement' === $library ) {
  2969 	if ( 'mediaelement' === $library ) {
  2458 		$html .= wp_mediaelement_fallback( $fileurl );
  2970 		$html .= wp_mediaelement_fallback( $fileurl );
  2459 	}
  2971 	}
       
  2972 
  2460 	$html .= '</audio>';
  2973 	$html .= '</audio>';
  2461 
  2974 
  2462 	/**
  2975 	/**
  2463 	 * Filters the audio shortcode output.
  2976 	 * Filters the audio shortcode output.
  2464 	 *
  2977 	 *
  2473 	return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );
  2986 	return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );
  2474 }
  2987 }
  2475 add_shortcode( 'audio', 'wp_audio_shortcode' );
  2988 add_shortcode( 'audio', 'wp_audio_shortcode' );
  2476 
  2989 
  2477 /**
  2990 /**
  2478  * Returns a filtered list of WP-supported video formats.
  2991  * Returns a filtered list of supported video formats.
  2479  *
  2992  *
  2480  * @since 3.6.0
  2993  * @since 3.6.0
  2481  *
  2994  *
  2482  * @return array List of supported video formats.
  2995  * @return string[] List of supported video formats.
  2483  */
  2996  */
  2484 function wp_get_video_extensions() {
  2997 function wp_get_video_extensions() {
  2485 	/**
  2998 	/**
  2486 	 * Filters the list of supported video formats.
  2999 	 * Filters the list of supported video formats.
  2487 	 *
  3000 	 *
  2488 	 * @since 3.6.0
  3001 	 * @since 3.6.0
  2489 	 *
  3002 	 *
  2490 	 * @param array $extensions An array of supported video formats. Defaults are
  3003 	 * @param string[] $extensions An array of supported video formats. Defaults are
  2491 	 *                          'mp4', 'm4v', 'webm', 'ogv', 'flv'.
  3004 	 *                             'mp4', 'm4v', 'webm', 'ogv', 'flv'.
  2492 	 */
  3005 	 */
  2493 	return apply_filters( 'wp_video_extensions', array( 'mp4', 'm4v', 'webm', 'ogv', 'flv' ) );
  3006 	return apply_filters( 'wp_video_extensions', array( 'mp4', 'm4v', 'webm', 'ogv', 'flv' ) );
  2494 }
  3007 }
  2495 
  3008 
  2496 /**
  3009 /**
  2500  * WordPress mp4s in a post.
  3013  * WordPress mp4s in a post.
  2501  *
  3014  *
  2502  * @since 3.6.0
  3015  * @since 3.6.0
  2503  *
  3016  *
  2504  * @global int $content_width
  3017  * @global int $content_width
  2505  * @staticvar int $instance
       
  2506  *
  3018  *
  2507  * @param array  $attr {
  3019  * @param array  $attr {
  2508  *     Attributes of the shortcode.
  3020  *     Attributes of the shortcode.
  2509  *
  3021  *
  2510  *     @type string $src      URL to the source of the video file. Default empty.
  3022  *     @type string $src      URL to the source of the video file. Default empty.
  2542 	 * @param array  $attr     Attributes of the shortcode. @see wp_video_shortcode()
  3054 	 * @param array  $attr     Attributes of the shortcode. @see wp_video_shortcode()
  2543 	 * @param string $content  Video shortcode content.
  3055 	 * @param string $content  Video shortcode content.
  2544 	 * @param int    $instance Unique numeric ID of this video shortcode instance.
  3056 	 * @param int    $instance Unique numeric ID of this video shortcode instance.
  2545 	 */
  3057 	 */
  2546 	$override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance );
  3058 	$override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance );
       
  3059 
  2547 	if ( '' !== $override ) {
  3060 	if ( '' !== $override ) {
  2548 		return $override;
  3061 		return $override;
  2549 	}
  3062 	}
  2550 
  3063 
  2551 	$video = null;
  3064 	$video = null;
  2567 	}
  3080 	}
  2568 
  3081 
  2569 	$atts = shortcode_atts( $defaults_atts, $attr, 'video' );
  3082 	$atts = shortcode_atts( $defaults_atts, $attr, 'video' );
  2570 
  3083 
  2571 	if ( is_admin() ) {
  3084 	if ( is_admin() ) {
  2572 		// shrink the video so it isn't huge in the admin
  3085 		// Shrink the video so it isn't huge in the admin.
  2573 		if ( $atts['width'] > $defaults_atts['width'] ) {
  3086 		if ( $atts['width'] > $defaults_atts['width'] ) {
  2574 			$atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] );
  3087 			$atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] );
  2575 			$atts['width']  = $defaults_atts['width'];
  3088 			$atts['width']  = $defaults_atts['width'];
  2576 		}
  3089 		}
  2577 	} else {
  3090 	} else {
  2578 		// if the video is bigger than the theme
  3091 		// If the video is bigger than the theme.
  2579 		if ( ! empty( $content_width ) && $atts['width'] > $content_width ) {
  3092 		if ( ! empty( $content_width ) && $atts['width'] > $content_width ) {
  2580 			$atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] );
  3093 			$atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] );
  2581 			$atts['width']  = $content_width;
  3094 			$atts['width']  = $content_width;
  2582 		}
  3095 		}
  2583 	}
  3096 	}
  2584 
  3097 
  2585 	$is_vimeo      = $is_youtube = false;
  3098 	$is_vimeo      = false;
       
  3099 	$is_youtube    = false;
  2586 	$yt_pattern    = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
  3100 	$yt_pattern    = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
  2587 	$vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';
  3101 	$vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';
  2588 
  3102 
  2589 	$primary = false;
  3103 	$primary = false;
  2590 	if ( ! empty( $atts['src'] ) ) {
  3104 	if ( ! empty( $atts['src'] ) ) {
  2591 		$is_vimeo   = ( preg_match( $vimeo_pattern, $atts['src'] ) );
  3105 		$is_vimeo   = ( preg_match( $vimeo_pattern, $atts['src'] ) );
  2592 		$is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) );
  3106 		$is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) );
       
  3107 
  2593 		if ( ! $is_youtube && ! $is_vimeo ) {
  3108 		if ( ! $is_youtube && ! $is_vimeo ) {
  2594 			$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
  3109 			$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
  2595 			if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) {
  3110 
       
  3111 			if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
  2596 				return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
  3112 				return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
  2597 			}
  3113 			}
  2598 		}
  3114 		}
  2599 
  3115 
  2600 		if ( $is_vimeo ) {
  3116 		if ( $is_vimeo ) {
  2641 		wp_enqueue_style( 'wp-mediaelement' );
  3157 		wp_enqueue_style( 'wp-mediaelement' );
  2642 		wp_enqueue_script( 'wp-mediaelement' );
  3158 		wp_enqueue_script( 'wp-mediaelement' );
  2643 		wp_enqueue_script( 'mediaelement-vimeo' );
  3159 		wp_enqueue_script( 'mediaelement-vimeo' );
  2644 	}
  3160 	}
  2645 
  3161 
  2646 	// Mediaelement has issues with some URL formats for Vimeo and YouTube, so
  3162 	// MediaElement.js has issues with some URL formats for Vimeo and YouTube,
  2647 	// update the URL to prevent the ME.js player from breaking.
  3163 	// so update the URL to prevent the ME.js player from breaking.
  2648 	if ( 'mediaelement' === $library ) {
  3164 	if ( 'mediaelement' === $library ) {
  2649 		if ( $is_youtube ) {
  3165 		if ( $is_youtube ) {
  2650 			// Remove `feature` query arg and force SSL - see #40866.
  3166 			// Remove `feature` query arg and force SSL - see #40866.
  2651 			$atts['src'] = remove_query_arg( 'feature', $atts['src'] );
  3167 			$atts['src'] = remove_query_arg( 'feature', $atts['src'] );
  2652 			$atts['src'] = set_url_scheme( $atts['src'], 'https' );
  3168 			$atts['src'] = set_url_scheme( $atts['src'], 'https' );
  2681 		'loop'     => wp_validate_boolean( $atts['loop'] ),
  3197 		'loop'     => wp_validate_boolean( $atts['loop'] ),
  2682 		'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
  3198 		'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
  2683 		'preload'  => $atts['preload'],
  3199 		'preload'  => $atts['preload'],
  2684 	);
  3200 	);
  2685 
  3201 
  2686 	// These ones should just be omitted altogether if they are blank
  3202 	// These ones should just be omitted altogether if they are blank.
  2687 	foreach ( array( 'poster', 'loop', 'autoplay', 'preload' ) as $a ) {
  3203 	foreach ( array( 'poster', 'loop', 'autoplay', 'preload' ) as $a ) {
  2688 		if ( empty( $html_atts[ $a ] ) ) {
  3204 		if ( empty( $html_atts[ $a ] ) ) {
  2689 			unset( $html_atts[ $a ] );
  3205 			unset( $html_atts[ $a ] );
  2690 		}
  3206 		}
  2691 	}
  3207 	}
  2811 			)
  3327 			)
  2812 		)
  3328 		)
  2813 	);
  3329 	);
  2814 
  3330 
  2815 	foreach ( $attachments as $k => $attachment ) {
  3331 	foreach ( $attachments as $k => $attachment ) {
  2816 		if ( $attachment->ID == $post->ID ) {
  3332 		if ( intval( $attachment->ID ) === intval( $post->ID ) ) {
  2817 			break;
  3333 			break;
  2818 		}
  3334 		}
  2819 	}
  3335 	}
  2820 
  3336 
  2821 	$output        = '';
  3337 	$output        = '';
  2856  *
  3372  *
  2857  * @param int|array|object $attachment Attachment ID, data array, or data object.
  3373  * @param int|array|object $attachment Attachment ID, data array, or data object.
  2858  * @param string           $output     Output type. 'names' to return an array of taxonomy names,
  3374  * @param string           $output     Output type. 'names' to return an array of taxonomy names,
  2859  *                                     or 'objects' to return an array of taxonomy objects.
  3375  *                                     or 'objects' to return an array of taxonomy objects.
  2860  *                                     Default is 'names'.
  3376  *                                     Default is 'names'.
  2861  * @return array Empty array on failure. List of taxonomies on success.
  3377  * @return string[]|WP_Taxonomy[] List of taxonomies or taxonomy names. Empty array on failure.
  2862  */
  3378  */
  2863 function get_attachment_taxonomies( $attachment, $output = 'names' ) {
  3379 function get_attachment_taxonomies( $attachment, $output = 'names' ) {
  2864 	if ( is_int( $attachment ) ) {
  3380 	if ( is_int( $attachment ) ) {
  2865 		$attachment = get_post( $attachment );
  3381 		$attachment = get_post( $attachment );
  2866 	} elseif ( is_array( $attachment ) ) {
  3382 	} elseif ( is_array( $attachment ) ) {
  2867 		$attachment = (object) $attachment;
  3383 		$attachment = (object) $attachment;
  2868 	}
  3384 	}
       
  3385 
  2869 	if ( ! is_object( $attachment ) ) {
  3386 	if ( ! is_object( $attachment ) ) {
  2870 		return array();
  3387 		return array();
  2871 	}
  3388 	}
  2872 
  3389 
  2873 	$file     = get_attached_file( $attachment->ID );
  3390 	$file     = get_attached_file( $attachment->ID );
  2876 	$objects = array( 'attachment' );
  3393 	$objects = array( 'attachment' );
  2877 
  3394 
  2878 	if ( false !== strpos( $filename, '.' ) ) {
  3395 	if ( false !== strpos( $filename, '.' ) ) {
  2879 		$objects[] = 'attachment:' . substr( $filename, strrpos( $filename, '.' ) + 1 );
  3396 		$objects[] = 'attachment:' . substr( $filename, strrpos( $filename, '.' ) + 1 );
  2880 	}
  3397 	}
       
  3398 
  2881 	if ( ! empty( $attachment->post_mime_type ) ) {
  3399 	if ( ! empty( $attachment->post_mime_type ) ) {
  2882 		$objects[] = 'attachment:' . $attachment->post_mime_type;
  3400 		$objects[] = 'attachment:' . $attachment->post_mime_type;
       
  3401 
  2883 		if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
  3402 		if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
  2884 			foreach ( explode( '/', $attachment->post_mime_type ) as $token ) {
  3403 			foreach ( explode( '/', $attachment->post_mime_type ) as $token ) {
  2885 				if ( ! empty( $token ) ) {
  3404 				if ( ! empty( $token ) ) {
  2886 					$objects[] = "attachment:$token";
  3405 					$objects[] = "attachment:$token";
  2887 				}
  3406 				}
  2888 			}
  3407 			}
  2889 		}
  3408 		}
  2890 	}
  3409 	}
  2891 
  3410 
  2892 	$taxonomies = array();
  3411 	$taxonomies = array();
       
  3412 
  2893 	foreach ( $objects as $object ) {
  3413 	foreach ( $objects as $object ) {
  2894 		if ( $taxes = get_object_taxonomies( $object, $output ) ) {
  3414 		$taxes = get_object_taxonomies( $object, $output );
       
  3415 
       
  3416 		if ( $taxes ) {
  2895 			$taxonomies = array_merge( $taxonomies, $taxes );
  3417 			$taxonomies = array_merge( $taxonomies, $taxes );
  2896 		}
  3418 		}
  2897 	}
  3419 	}
  2898 
  3420 
  2899 	if ( 'names' === $output ) {
  3421 	if ( 'names' === $output ) {
  2907  * Retrieves all of the taxonomies that are registered for attachments.
  3429  * Retrieves all of the taxonomies that are registered for attachments.
  2908  *
  3430  *
  2909  * Handles mime-type-specific taxonomies such as attachment:image and attachment:video.
  3431  * Handles mime-type-specific taxonomies such as attachment:image and attachment:video.
  2910  *
  3432  *
  2911  * @since 3.5.0
  3433  * @since 3.5.0
       
  3434  *
  2912  * @see get_taxonomies()
  3435  * @see get_taxonomies()
  2913  *
  3436  *
  2914  * @param string $output Optional. The type of taxonomy output to return. Accepts 'names' or 'objects'.
  3437  * @param string $output Optional. The type of taxonomy output to return. Accepts 'names' or 'objects'.
  2915  *                       Default 'names'.
  3438  *                       Default 'names'.
  2916  * @return string[]|WP_Taxonomy[] Array of names or objects of registered taxonomies for attachments.
  3439  * @return string[]|WP_Taxonomy[] Array of names or objects of registered taxonomies for attachments.
  2917  */
  3440  */
  2918 function get_taxonomies_for_attachments( $output = 'names' ) {
  3441 function get_taxonomies_for_attachments( $output = 'names' ) {
  2919 	$taxonomies = array();
  3442 	$taxonomies = array();
       
  3443 
  2920 	foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
  3444 	foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
  2921 		foreach ( $taxonomy->object_type as $object_type ) {
  3445 		foreach ( $taxonomy->object_type as $object_type ) {
  2922 			if ( 'attachment' == $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
  3446 			if ( 'attachment' === $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
  2923 				if ( 'names' == $output ) {
  3447 				if ( 'names' === $output ) {
  2924 					$taxonomies[] = $taxonomy->name;
  3448 					$taxonomies[] = $taxonomy->name;
  2925 				} else {
  3449 				} else {
  2926 					$taxonomies[ $taxonomy->name ] = $taxonomy;
  3450 					$taxonomies[ $taxonomy->name ] = $taxonomy;
  2927 				}
  3451 				}
  2928 				break;
  3452 				break;
  2934 }
  3458 }
  2935 
  3459 
  2936 /**
  3460 /**
  2937  * Create new GD image resource with transparency support
  3461  * Create new GD image resource with transparency support
  2938  *
  3462  *
  2939  * @todo: Deprecate if possible.
  3463  * @todo Deprecate if possible.
  2940  *
  3464  *
  2941  * @since 2.9.0
  3465  * @since 2.9.0
  2942  *
  3466  *
  2943  * @param int $width  Image width in pixels.
  3467  * @param int $width  Image width in pixels.
  2944  * @param int $height Image height in pixels..
  3468  * @param int $height Image height in pixels..
  2962  *
  3486  *
  2963  * @param int $example_width  The width of an example embed.
  3487  * @param int $example_width  The width of an example embed.
  2964  * @param int $example_height The height of an example embed.
  3488  * @param int $example_height The height of an example embed.
  2965  * @param int $max_width      The maximum allowed width.
  3489  * @param int $max_width      The maximum allowed width.
  2966  * @param int $max_height     The maximum allowed height.
  3490  * @param int $max_height     The maximum allowed height.
  2967  * @return array The maximum possible width and height based on the example ratio.
  3491  * @return int[] {
       
  3492  *     An array of maximum width and height values.
       
  3493  *
       
  3494  *     @type int $0 The maximum width in pixels.
       
  3495  *     @type int $1 The maximum height in pixels.
       
  3496  * }
  2968  */
  3497  */
  2969 function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) {
  3498 function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) {
  2970 	$example_width  = (int) $example_width;
  3499 	$example_width  = (int) $example_width;
  2971 	$example_height = (int) $example_height;
  3500 	$example_height = (int) $example_height;
  2972 	$max_width      = (int) $max_width;
  3501 	$max_width      = (int) $max_width;
  3068 	/**
  3597 	/**
  3069 	 * Filters the list of image editing library classes.
  3598 	 * Filters the list of image editing library classes.
  3070 	 *
  3599 	 *
  3071 	 * @since 3.5.0
  3600 	 * @since 3.5.0
  3072 	 *
  3601 	 *
  3073 	 * @param array $image_editors List of available image editors. Defaults are
  3602 	 * @param string[] $image_editors Array of available image editor class names. Defaults are
  3074 	 *                             'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
  3603 	 *                                'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
  3075 	 */
  3604 	 */
  3076 	$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
  3605 	$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
  3077 
  3606 
  3078 	foreach ( $implementations as $implementation ) {
  3607 	foreach ( $implementations as $implementation ) {
  3079 		if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
  3608 		if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
  3123 	/*
  3652 	/*
  3124 	 * Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`,
  3653 	 * Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`,
  3125 	 * and the `flash_swf_url` and `silverlight_xap_url` are not used.
  3654 	 * and the `flash_swf_url` and `silverlight_xap_url` are not used.
  3126 	 */
  3655 	 */
  3127 	$defaults = array(
  3656 	$defaults = array(
  3128 		'file_data_name' => 'async-upload', // key passed to $_FILE.
  3657 		'file_data_name' => 'async-upload', // Key passed to $_FILE.
  3129 		'url'            => admin_url( 'async-upload.php', 'relative' ),
  3658 		'url'            => admin_url( 'async-upload.php', 'relative' ),
  3130 		'filters'        => array(
  3659 		'filters'        => array(
  3131 			'max_file_size' => $max_upload_size . 'b',
  3660 			'max_file_size' => $max_upload_size . 'b',
  3132 			'mime_types'    => array( array( 'extensions' => implode( ',', $extensions ) ) ),
  3661 			'mime_types'    => array( array( 'extensions' => implode( ',', $extensions ) ) ),
  3133 		),
  3662 		),
  3134 	);
  3663 	);
  3135 
  3664 
  3136 	// Currently only iOS Safari supports multiple files uploading but iOS 7.x has a bug that prevents uploading of videos
  3665 	/*
  3137 	// when enabled. See #29602.
  3666 	 * Currently only iOS Safari supports multiple files uploading,
       
  3667 	 * but iOS 7.x has a bug that prevents uploading of videos when enabled.
       
  3668 	 * See #29602.
       
  3669 	 */
  3138 	if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
  3670 	if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
  3139 		strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) {
  3671 		strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) {
  3140 
  3672 
  3141 		$defaults['multi_selection'] = false;
  3673 		$defaults['multi_selection'] = false;
  3142 	}
  3674 	}
  3159 	 *
  3691 	 *
  3160 	 * @since 3.4.0
  3692 	 * @since 3.4.0
  3161 	 *
  3693 	 *
  3162 	 * @param array $params Default Plupload parameters array.
  3694 	 * @param array $params Default Plupload parameters array.
  3163 	 */
  3695 	 */
  3164 	$params                       = apply_filters( 'plupload_default_params', $params );
  3696 	$params = apply_filters( 'plupload_default_params', $params );
  3165 	$params['_wpnonce']           = wp_create_nonce( 'media-form' );
  3697 
       
  3698 	$params['_wpnonce'] = wp_create_nonce( 'media-form' );
       
  3699 
  3166 	$defaults['multipart_params'] = $params;
  3700 	$defaults['multipart_params'] = $params;
  3167 
  3701 
  3168 	$settings = array(
  3702 	$settings = array(
  3169 		'defaults'      => $defaults,
  3703 		'defaults'      => $defaults,
  3170 		'browser'       => array(
  3704 		'browser'       => array(
  3191  *
  3725  *
  3192  * @param int|WP_Post $attachment Attachment ID or object.
  3726  * @param int|WP_Post $attachment Attachment ID or object.
  3193  * @return array|void Array of attachment details.
  3727  * @return array|void Array of attachment details.
  3194  */
  3728  */
  3195 function wp_prepare_attachment_for_js( $attachment ) {
  3729 function wp_prepare_attachment_for_js( $attachment ) {
  3196 	if ( ! $attachment = get_post( $attachment ) ) {
  3730 	$attachment = get_post( $attachment );
       
  3731 
       
  3732 	if ( ! $attachment ) {
  3197 		return;
  3733 		return;
  3198 	}
  3734 	}
  3199 
  3735 
  3200 	if ( 'attachment' != $attachment->post_type ) {
  3736 	if ( 'attachment' !== $attachment->post_type ) {
  3201 		return;
  3737 		return;
  3202 	}
  3738 	}
  3203 
  3739 
  3204 	$meta = wp_get_attachment_metadata( $attachment->ID );
  3740 	$meta = wp_get_attachment_metadata( $attachment->ID );
  3205 	if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
  3741 	if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
  3307 				'full'      => __( 'Full Size' ),
  3843 				'full'      => __( 'Full Size' ),
  3308 			)
  3844 			)
  3309 		);
  3845 		);
  3310 		unset( $possible_sizes['full'] );
  3846 		unset( $possible_sizes['full'] );
  3311 
  3847 
  3312 		// Loop through all potential sizes that may be chosen. Try to do this with some efficiency.
  3848 		/*
  3313 		// First: run the image_downsize filter. If it returns something, we can use its data.
  3849 		 * Loop through all potential sizes that may be chosen. Try to do this with some efficiency.
  3314 		// If the filter does not return something, then image_downsize() is just an expensive
  3850 		 * First: run the image_downsize filter. If it returns something, we can use its data.
  3315 		// way to check the image metadata, which we do second.
  3851 		 * If the filter does not return something, then image_downsize() is just an expensive way
       
  3852 		 * to check the image metadata, which we do second.
       
  3853 		 */
  3316 		foreach ( $possible_sizes as $size => $label ) {
  3854 		foreach ( $possible_sizes as $size => $label ) {
  3317 
  3855 
  3318 			/** This filter is documented in wp-includes/media.php */
  3856 			/** This filter is documented in wp-includes/media.php */
  3319 			if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) {
  3857 			$downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size );
       
  3858 
       
  3859 			if ( $downsize ) {
  3320 				if ( empty( $downsize[3] ) ) {
  3860 				if ( empty( $downsize[3] ) ) {
  3321 					continue;
  3861 					continue;
  3322 				}
  3862 				}
  3323 
  3863 
  3324 				$sizes[ $size ] = array(
  3864 				$sizes[ $size ] = array(
  3343 				);
  3883 				);
  3344 			}
  3884 			}
  3345 		}
  3885 		}
  3346 
  3886 
  3347 		if ( 'image' === $type ) {
  3887 		if ( 'image' === $type ) {
       
  3888 			if ( ! empty( $meta['original_image'] ) ) {
       
  3889 				$response['originalImageURL']  = wp_get_original_image_url( $attachment->ID );
       
  3890 				$response['originalImageName'] = wp_basename( wp_get_original_image_path( $attachment->ID ) );
       
  3891 			}
       
  3892 
  3348 			$sizes['full'] = array( 'url' => $attachment_url );
  3893 			$sizes['full'] = array( 'url' => $attachment_url );
  3349 
  3894 
  3350 			if ( isset( $meta['height'], $meta['width'] ) ) {
  3895 			if ( isset( $meta['height'], $meta['width'] ) ) {
  3351 				$sizes['full']['height']      = $meta['height'];
  3896 				$sizes['full']['height']      = $meta['height'];
  3352 				$sizes['full']['width']       = $meta['width'];
  3897 				$sizes['full']['width']       = $meta['width'];
  3426  * all media JS APIs.
  3971  * all media JS APIs.
  3427  *
  3972  *
  3428  * @since 3.5.0
  3973  * @since 3.5.0
  3429  *
  3974  *
  3430  * @global int       $content_width
  3975  * @global int       $content_width
  3431  * @global wpdb      $wpdb
  3976  * @global wpdb      $wpdb          WordPress database abstraction object.
  3432  * @global WP_Locale $wp_locale
  3977  * @global WP_Locale $wp_locale     WordPress date and time locale object.
  3433  *
  3978  *
  3434  * @param array $args {
  3979  * @param array $args {
  3435  *     Arguments for enqueuing media scripts.
  3980  *     Arguments for enqueuing media scripts.
  3436  *
  3981  *
  3437  *     @type int|WP_Post A post object or ID.
  3982  *     @type int|WP_Post A post object or ID.
  3463 	/** This filter is documented in wp-admin/includes/media.php */
  4008 	/** This filter is documented in wp-admin/includes/media.php */
  3464 	$tabs = apply_filters( 'media_upload_tabs', $tabs );
  4009 	$tabs = apply_filters( 'media_upload_tabs', $tabs );
  3465 	unset( $tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library'] );
  4010 	unset( $tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library'] );
  3466 
  4011 
  3467 	$props = array(
  4012 	$props = array(
  3468 		'link'  => get_option( 'image_default_link_type' ), // db default is 'file'
  4013 		'link'  => get_option( 'image_default_link_type' ), // DB default is 'file'.
  3469 		'align' => get_option( 'image_default_align' ), // empty default
  4014 		'align' => get_option( 'image_default_align' ),     // Empty default.
  3470 		'size'  => get_option( 'image_default_size' ),  // empty default
  4015 		'size'  => get_option( 'image_default_size' ),      // Empty default.
  3471 	);
  4016 	);
  3472 
  4017 
  3473 	$exts      = array_merge( wp_get_audio_extensions(), wp_get_video_extensions() );
  4018 	$exts      = array_merge( wp_get_audio_extensions(), wp_get_video_extensions() );
  3474 	$mimes     = get_allowed_mime_types();
  4019 	$mimes     = get_allowed_mime_types();
  3475 	$ext_mimes = array();
  4020 	$ext_mimes = array();
  3494 	 * @since 4.7.4
  4039 	 * @since 4.7.4
  3495 	 * @since 4.8.0 The filter's default value is `true` rather than `null`.
  4040 	 * @since 4.8.0 The filter's default value is `true` rather than `null`.
  3496 	 *
  4041 	 *
  3497 	 * @link https://core.trac.wordpress.org/ticket/31071
  4042 	 * @link https://core.trac.wordpress.org/ticket/31071
  3498 	 *
  4043 	 *
  3499 	 * @param bool|null Whether to show the button, or `null` to decide based
  4044 	 * @param bool|null $show Whether to show the button, or `null` to decide based
  3500 	 *                  on whether any audio files exist in the media library.
  4045 	 *                        on whether any audio files exist in the media library.
  3501 	 */
  4046 	 */
  3502 	$show_audio_playlist = apply_filters( 'media_library_show_audio_playlist', true );
  4047 	$show_audio_playlist = apply_filters( 'media_library_show_audio_playlist', true );
  3503 	if ( null === $show_audio_playlist ) {
  4048 	if ( null === $show_audio_playlist ) {
  3504 		$show_audio_playlist = $wpdb->get_var(
  4049 		$show_audio_playlist = $wpdb->get_var(
  3505 			"
  4050 			"
  3524 	 * @since 4.7.4
  4069 	 * @since 4.7.4
  3525 	 * @since 4.8.0 The filter's default value is `true` rather than `null`.
  4070 	 * @since 4.8.0 The filter's default value is `true` rather than `null`.
  3526 	 *
  4071 	 *
  3527 	 * @link https://core.trac.wordpress.org/ticket/31071
  4072 	 * @link https://core.trac.wordpress.org/ticket/31071
  3528 	 *
  4073 	 *
  3529 	 * @param bool|null Whether to show the button, or `null` to decide based
  4074 	 * @param bool|null $show Whether to show the button, or `null` to decide based
  3530 	 *                  on whether any video files exist in the media library.
  4075 	 *                        on whether any video files exist in the media library.
  3531 	 */
  4076 	 */
  3532 	$show_video_playlist = apply_filters( 'media_library_show_video_playlist', true );
  4077 	$show_video_playlist = apply_filters( 'media_library_show_video_playlist', true );
  3533 	if ( null === $show_video_playlist ) {
  4078 	if ( null === $show_video_playlist ) {
  3534 		$show_video_playlist = $wpdb->get_var(
  4079 		$show_video_playlist = $wpdb->get_var(
  3535 			"
  4080 			"
  3552 	 *
  4097 	 *
  3553 	 * @since 4.7.4
  4098 	 * @since 4.7.4
  3554 	 *
  4099 	 *
  3555 	 * @link https://core.trac.wordpress.org/ticket/31071
  4100 	 * @link https://core.trac.wordpress.org/ticket/31071
  3556 	 *
  4101 	 *
  3557 	 * @param array|null An array of objects with `month` and `year`
  4102 	 * @param array|null $months An array of objects with `month` and `year`
  3558 	 *                   properties, or `null` (or any other non-array value)
  4103 	 *                           properties, or `null` (or any other non-array value)
  3559 	 *                   for default behavior.
  4104 	 *                           for default behavior.
  3560 	 */
  4105 	 */
  3561 	$months = apply_filters( 'media_library_months_with_files', null );
  4106 	$months = apply_filters( 'media_library_months_with_files', null );
  3562 	if ( ! is_array( $months ) ) {
  4107 	if ( ! is_array( $months ) ) {
  3563 		$months = $wpdb->get_results(
  4108 		$months = $wpdb->get_results(
  3564 			$wpdb->prepare(
  4109 			$wpdb->prepare(
  3571 				'attachment'
  4116 				'attachment'
  3572 			)
  4117 			)
  3573 		);
  4118 		);
  3574 	}
  4119 	}
  3575 	foreach ( $months as $month_year ) {
  4120 	foreach ( $months as $month_year ) {
  3576 		$month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year );
  4121 		$month_year->text = sprintf(
       
  4122 			/* translators: 1: Month, 2: Year. */
       
  4123 			__( '%1$s %2$d' ),
       
  4124 			$wp_locale->get_month( $month_year->month ),
       
  4125 			$month_year->year
       
  4126 		);
  3577 	}
  4127 	}
  3578 
  4128 
  3579 	$settings = array(
  4129 	$settings = array(
  3580 		'tabs'             => $tabs,
  4130 		'tabs'             => $tabs,
  3581 		'tabUrl'           => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ),
  4131 		'tabUrl'           => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ),
  3629 	} else {
  4179 	} else {
  3630 		$post_type_object = get_post_type_object( 'post' );
  4180 		$post_type_object = get_post_type_object( 'post' );
  3631 	}
  4181 	}
  3632 
  4182 
  3633 	$strings = array(
  4183 	$strings = array(
  3634 		// Generic
  4184 		// Generic.
       
  4185 		'mediaFrameDefaultTitle'      => __( 'Media' ),
  3635 		'url'                         => __( 'URL' ),
  4186 		'url'                         => __( 'URL' ),
  3636 		'addMedia'                    => __( 'Add Media' ),
  4187 		'addMedia'                    => __( 'Add media' ),
  3637 		'search'                      => __( 'Search' ),
  4188 		'search'                      => __( 'Search' ),
  3638 		'select'                      => __( 'Select' ),
  4189 		'select'                      => __( 'Select' ),
  3639 		'cancel'                      => __( 'Cancel' ),
  4190 		'cancel'                      => __( 'Cancel' ),
  3640 		'update'                      => __( 'Update' ),
  4191 		'update'                      => __( 'Update' ),
  3641 		'replace'                     => __( 'Replace' ),
  4192 		'replace'                     => __( 'Replace' ),
  3647 		 * lack of plural support here, turn it into "selected: %d" then translate it.
  4198 		 * lack of plural support here, turn it into "selected: %d" then translate it.
  3648 		 */
  4199 		 */
  3649 		'selected'                    => __( '%d selected' ),
  4200 		'selected'                    => __( '%d selected' ),
  3650 		'dragInfo'                    => __( 'Drag and drop to reorder media files.' ),
  4201 		'dragInfo'                    => __( 'Drag and drop to reorder media files.' ),
  3651 
  4202 
  3652 		// Upload
  4203 		// Upload.
  3653 		'uploadFilesTitle'            => __( 'Upload Files' ),
  4204 		'uploadFilesTitle'            => __( 'Upload files' ),
  3654 		'uploadImagesTitle'           => __( 'Upload Images' ),
  4205 		'uploadImagesTitle'           => __( 'Upload images' ),
  3655 
  4206 
  3656 		// Library
  4207 		// Library.
  3657 		'mediaLibraryTitle'           => __( 'Media Library' ),
  4208 		'mediaLibraryTitle'           => __( 'Media Library' ),
  3658 		'insertMediaTitle'            => __( 'Add Media' ),
  4209 		'insertMediaTitle'            => __( 'Add media' ),
  3659 		'createNewGallery'            => __( 'Create a new gallery' ),
  4210 		'createNewGallery'            => __( 'Create a new gallery' ),
  3660 		'createNewPlaylist'           => __( 'Create a new playlist' ),
  4211 		'createNewPlaylist'           => __( 'Create a new playlist' ),
  3661 		'createNewVideoPlaylist'      => __( 'Create a new video playlist' ),
  4212 		'createNewVideoPlaylist'      => __( 'Create a new video playlist' ),
  3662 		'returnToLibrary'             => __( '&#8592; Return to library' ),
  4213 		'returnToLibrary'             => __( '&#8592; Return to library' ),
  3663 		'allMediaItems'               => __( 'All media items' ),
  4214 		'allMediaItems'               => __( 'All media items' ),
  3669 		'trash'                       => _x( 'Trash', 'noun' ),
  4220 		'trash'                       => _x( 'Trash', 'noun' ),
  3670 		'uploadedToThisPost'          => $post_type_object->labels->uploaded_to_this_item,
  4221 		'uploadedToThisPost'          => $post_type_object->labels->uploaded_to_this_item,
  3671 		'warnDelete'                  => __( "You are about to permanently delete this item from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ),
  4222 		'warnDelete'                  => __( "You are about to permanently delete this item from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ),
  3672 		'warnBulkDelete'              => __( "You are about to permanently delete these items from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ),
  4223 		'warnBulkDelete'              => __( "You are about to permanently delete these items from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ),
  3673 		'warnBulkTrash'               => __( "You are about to trash these items.\n  'Cancel' to stop, 'OK' to delete." ),
  4224 		'warnBulkTrash'               => __( "You are about to trash these items.\n  'Cancel' to stop, 'OK' to delete." ),
  3674 		'bulkSelect'                  => __( 'Bulk Select' ),
  4225 		'bulkSelect'                  => __( 'Bulk select' ),
  3675 		'trashSelected'               => __( 'Move to Trash' ),
  4226 		'trashSelected'               => __( 'Move to Trash' ),
  3676 		'restoreSelected'             => __( 'Restore from Trash' ),
  4227 		'restoreSelected'             => __( 'Restore from Trash' ),
  3677 		'deletePermanently'           => __( 'Delete Permanently' ),
  4228 		'deletePermanently'           => __( 'Delete permanently' ),
  3678 		'apply'                       => __( 'Apply' ),
  4229 		'apply'                       => __( 'Apply' ),
  3679 		'filterByDate'                => __( 'Filter by date' ),
  4230 		'filterByDate'                => __( 'Filter by date' ),
  3680 		'filterByType'                => __( 'Filter by type' ),
  4231 		'filterByType'                => __( 'Filter by type' ),
  3681 		'searchMediaLabel'            => __( 'Search Media' ),
  4232 		'searchLabel'                 => __( 'Search' ),
  3682 		'searchMediaPlaceholder'      => __( 'Search media items...' ), // placeholder (no ellipsis)
  4233 		'searchMediaLabel'            => __( 'Search media' ),          // Backward compatibility pre-5.3.
  3683 		'noMedia'                     => __( 'No media files found.' ),
  4234 		'searchMediaPlaceholder'      => __( 'Search media items...' ), // Placeholder (no ellipsis), backward compatibility pre-5.3.
  3684 
  4235 		'mediaFound'                  => __( 'Number of media items found: %d' ),
  3685 		// Library Details
  4236 		'mediaFoundHasMoreResults'    => __( 'Number of media items displayed: %d. Scroll the page for more results.' ),
  3686 		'attachmentDetails'           => __( 'Attachment Details' ),
  4237 		'noMedia'                     => __( 'No media items found.' ),
  3687 
  4238 		'noMediaTryNewSearch'         => __( 'No media items found. Try a different search.' ),
  3688 		// From URL
  4239 
       
  4240 		// Library Details.
       
  4241 		'attachmentDetails'           => __( 'Attachment details' ),
       
  4242 
       
  4243 		// From URL.
  3689 		'insertFromUrlTitle'          => __( 'Insert from URL' ),
  4244 		'insertFromUrlTitle'          => __( 'Insert from URL' ),
  3690 
  4245 
  3691 		// Featured Images
  4246 		// Featured Images.
  3692 		'setFeaturedImageTitle'       => $post_type_object->labels->featured_image,
  4247 		'setFeaturedImageTitle'       => $post_type_object->labels->featured_image,
  3693 		'setFeaturedImage'            => $post_type_object->labels->set_featured_image,
  4248 		'setFeaturedImage'            => $post_type_object->labels->set_featured_image,
  3694 
  4249 
  3695 		// Gallery
  4250 		// Gallery.
  3696 		'createGalleryTitle'          => __( 'Create Gallery' ),
  4251 		'createGalleryTitle'          => __( 'Create gallery' ),
  3697 		'editGalleryTitle'            => __( 'Edit Gallery' ),
  4252 		'editGalleryTitle'            => __( 'Edit gallery' ),
  3698 		'cancelGalleryTitle'          => __( '&#8592; Cancel Gallery' ),
  4253 		'cancelGalleryTitle'          => __( '&#8592; Cancel gallery' ),
  3699 		'insertGallery'               => __( 'Insert gallery' ),
  4254 		'insertGallery'               => __( 'Insert gallery' ),
  3700 		'updateGallery'               => __( 'Update gallery' ),
  4255 		'updateGallery'               => __( 'Update gallery' ),
  3701 		'addToGallery'                => __( 'Add to gallery' ),
  4256 		'addToGallery'                => __( 'Add to gallery' ),
  3702 		'addToGalleryTitle'           => __( 'Add to Gallery' ),
  4257 		'addToGalleryTitle'           => __( 'Add to gallery' ),
  3703 		'reverseOrder'                => __( 'Reverse order' ),
  4258 		'reverseOrder'                => __( 'Reverse order' ),
  3704 
  4259 
  3705 		// Edit Image
  4260 		// Edit Image.
  3706 		'imageDetailsTitle'           => __( 'Image Details' ),
  4261 		'imageDetailsTitle'           => __( 'Image details' ),
  3707 		'imageReplaceTitle'           => __( 'Replace Image' ),
  4262 		'imageReplaceTitle'           => __( 'Replace image' ),
  3708 		'imageDetailsCancel'          => __( 'Cancel Edit' ),
  4263 		'imageDetailsCancel'          => __( 'Cancel edit' ),
  3709 		'editImage'                   => __( 'Edit Image' ),
  4264 		'editImage'                   => __( 'Edit image' ),
  3710 
  4265 
  3711 		// Crop Image
  4266 		// Crop Image.
  3712 		'chooseImage'                 => __( 'Choose Image' ),
  4267 		'chooseImage'                 => __( 'Choose image' ),
  3713 		'selectAndCrop'               => __( 'Select and Crop' ),
  4268 		'selectAndCrop'               => __( 'Select and crop' ),
  3714 		'skipCropping'                => __( 'Skip Cropping' ),
  4269 		'skipCropping'                => __( 'Skip cropping' ),
  3715 		'cropImage'                   => __( 'Crop Image' ),
  4270 		'cropImage'                   => __( 'Crop image' ),
  3716 		'cropYourImage'               => __( 'Crop your image' ),
  4271 		'cropYourImage'               => __( 'Crop your image' ),
  3717 		'cropping'                    => __( 'Cropping&hellip;' ),
  4272 		'cropping'                    => __( 'Cropping&hellip;' ),
  3718 		/* translators: 1: suggested width number, 2: suggested height number. */
  4273 		/* translators: 1: Suggested width number, 2: Suggested height number. */
  3719 		'suggestedDimensions'         => __( 'Suggested image dimensions: %1$s by %2$s pixels.' ),
  4274 		'suggestedDimensions'         => __( 'Suggested image dimensions: %1$s by %2$s pixels.' ),
  3720 		'cropError'                   => __( 'There has been an error cropping your image.' ),
  4275 		'cropError'                   => __( 'There has been an error cropping your image.' ),
  3721 
  4276 
  3722 		// Edit Audio
  4277 		// Edit Audio.
  3723 		'audioDetailsTitle'           => __( 'Audio Details' ),
  4278 		'audioDetailsTitle'           => __( 'Audio details' ),
  3724 		'audioReplaceTitle'           => __( 'Replace Audio' ),
  4279 		'audioReplaceTitle'           => __( 'Replace audio' ),
  3725 		'audioAddSourceTitle'         => __( 'Add Audio Source' ),
  4280 		'audioAddSourceTitle'         => __( 'Add audio source' ),
  3726 		'audioDetailsCancel'          => __( 'Cancel Edit' ),
  4281 		'audioDetailsCancel'          => __( 'Cancel edit' ),
  3727 
  4282 
  3728 		// Edit Video
  4283 		// Edit Video.
  3729 		'videoDetailsTitle'           => __( 'Video Details' ),
  4284 		'videoDetailsTitle'           => __( 'Video details' ),
  3730 		'videoReplaceTitle'           => __( 'Replace Video' ),
  4285 		'videoReplaceTitle'           => __( 'Replace video' ),
  3731 		'videoAddSourceTitle'         => __( 'Add Video Source' ),
  4286 		'videoAddSourceTitle'         => __( 'Add video source' ),
  3732 		'videoDetailsCancel'          => __( 'Cancel Edit' ),
  4287 		'videoDetailsCancel'          => __( 'Cancel edit' ),
  3733 		'videoSelectPosterImageTitle' => __( 'Select Poster Image' ),
  4288 		'videoSelectPosterImageTitle' => __( 'Select poster image' ),
  3734 		'videoAddTrackTitle'          => __( 'Add Subtitles' ),
  4289 		'videoAddTrackTitle'          => __( 'Add subtitles' ),
  3735 
  4290 
  3736 		// Playlist
  4291 		// Playlist.
  3737 		'playlistDragInfo'            => __( 'Drag and drop to reorder tracks.' ),
  4292 		'playlistDragInfo'            => __( 'Drag and drop to reorder tracks.' ),
  3738 		'createPlaylistTitle'         => __( 'Create Audio Playlist' ),
  4293 		'createPlaylistTitle'         => __( 'Create audio playlist' ),
  3739 		'editPlaylistTitle'           => __( 'Edit Audio Playlist' ),
  4294 		'editPlaylistTitle'           => __( 'Edit audio playlist' ),
  3740 		'cancelPlaylistTitle'         => __( '&#8592; Cancel Audio Playlist' ),
  4295 		'cancelPlaylistTitle'         => __( '&#8592; Cancel audio playlist' ),
  3741 		'insertPlaylist'              => __( 'Insert audio playlist' ),
  4296 		'insertPlaylist'              => __( 'Insert audio playlist' ),
  3742 		'updatePlaylist'              => __( 'Update audio playlist' ),
  4297 		'updatePlaylist'              => __( 'Update audio playlist' ),
  3743 		'addToPlaylist'               => __( 'Add to audio playlist' ),
  4298 		'addToPlaylist'               => __( 'Add to audio playlist' ),
  3744 		'addToPlaylistTitle'          => __( 'Add to Audio Playlist' ),
  4299 		'addToPlaylistTitle'          => __( 'Add to Audio Playlist' ),
  3745 
  4300 
  3746 		// Video Playlist
  4301 		// Video Playlist.
  3747 		'videoPlaylistDragInfo'       => __( 'Drag and drop to reorder videos.' ),
  4302 		'videoPlaylistDragInfo'       => __( 'Drag and drop to reorder videos.' ),
  3748 		'createVideoPlaylistTitle'    => __( 'Create Video Playlist' ),
  4303 		'createVideoPlaylistTitle'    => __( 'Create video playlist' ),
  3749 		'editVideoPlaylistTitle'      => __( 'Edit Video Playlist' ),
  4304 		'editVideoPlaylistTitle'      => __( 'Edit video playlist' ),
  3750 		'cancelVideoPlaylistTitle'    => __( '&#8592; Cancel Video Playlist' ),
  4305 		'cancelVideoPlaylistTitle'    => __( '&#8592; Cancel video playlist' ),
  3751 		'insertVideoPlaylist'         => __( 'Insert video playlist' ),
  4306 		'insertVideoPlaylist'         => __( 'Insert video playlist' ),
  3752 		'updateVideoPlaylist'         => __( 'Update video playlist' ),
  4307 		'updateVideoPlaylist'         => __( 'Update video playlist' ),
  3753 		'addToVideoPlaylist'          => __( 'Add to video playlist' ),
  4308 		'addToVideoPlaylist'          => __( 'Add to video playlist' ),
  3754 		'addToVideoPlaylistTitle'     => __( 'Add to Video Playlist' ),
  4309 		'addToVideoPlaylistTitle'     => __( 'Add to video Playlist' ),
  3755 
  4310 
  3756 		// Headings
  4311 		// Headings.
  3757 		'attachmentsList'             => __( 'Attachments list' ),
  4312 		'filterAttachments'           => __( 'Filter media' ),
       
  4313 		'attachmentsList'             => __( 'Media list' ),
  3758 	);
  4314 	);
  3759 
  4315 
  3760 	/**
  4316 	/**
  3761 	 * Filters the media view settings.
  4317 	 * Filters the media view settings.
  3762 	 *
  4318 	 *
  3770 	/**
  4326 	/**
  3771 	 * Filters the media view strings.
  4327 	 * Filters the media view strings.
  3772 	 *
  4328 	 *
  3773 	 * @since 3.5.0
  4329 	 * @since 3.5.0
  3774 	 *
  4330 	 *
  3775 	 * @param array   $strings List of media view strings.
  4331 	 * @param string[] $strings Array of media view strings keyed by the name they'll be referenced by in JavaScript.
  3776 	 * @param WP_Post $post    Post object.
  4332 	 * @param WP_Post  $post    Post object.
  3777 	 */
  4333 	 */
  3778 	$strings = apply_filters( 'media_view_strings', $strings, $post );
  4334 	$strings = apply_filters( 'media_view_strings', $strings, $post );
  3779 
  4335 
  3780 	$strings['settings'] = $settings;
  4336 	$strings['settings'] = $settings;
  3781 
  4337 
  3782 	// Ensure we enqueue media-editor first, that way media-views is
  4338 	// Ensure we enqueue media-editor first, that way media-views
  3783 	// registered internally before we try to localize it. see #24724.
  4339 	// is registered internally before we try to localize it. See #24724.
  3784 	wp_enqueue_script( 'media-editor' );
  4340 	wp_enqueue_script( 'media-editor' );
  3785 	wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings );
  4341 	wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings );
  3786 
  4342 
  3787 	wp_enqueue_script( 'media-audiovideo' );
  4343 	wp_enqueue_script( 'media-audiovideo' );
  3788 	wp_enqueue_style( 'media-views' );
  4344 	wp_enqueue_style( 'media-views' );
  3811  *
  4367  *
  3812  * @since 3.6.0
  4368  * @since 3.6.0
  3813  *
  4369  *
  3814  * @param string      $type Mime type.
  4370  * @param string      $type Mime type.
  3815  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  4371  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  3816  * @return array Found attachments.
  4372  * @return WP_Post[] Array of media attached to the given post.
  3817  */
  4373  */
  3818 function get_attached_media( $type, $post = 0 ) {
  4374 function get_attached_media( $type, $post = 0 ) {
  3819 	if ( ! $post = get_post( $post ) ) {
  4375 	$post = get_post( $post );
       
  4376 
       
  4377 	if ( ! $post ) {
  3820 		return array();
  4378 		return array();
  3821 	}
  4379 	}
  3822 
  4380 
  3823 	$args = array(
  4381 	$args = array(
  3824 		'post_parent'    => $post->ID,
  4382 		'post_parent'    => $post->ID,
  3832 	/**
  4390 	/**
  3833 	 * Filters arguments used to retrieve media attached to the given post.
  4391 	 * Filters arguments used to retrieve media attached to the given post.
  3834 	 *
  4392 	 *
  3835 	 * @since 3.6.0
  4393 	 * @since 3.6.0
  3836 	 *
  4394 	 *
  3837 	 * @param array  $args Post query arguments.
  4395 	 * @param array   $args Post query arguments.
  3838 	 * @param string $type Mime type of the desired media.
  4396 	 * @param string  $type Mime type of the desired media.
  3839 	 * @param mixed  $post Post ID or object.
  4397 	 * @param WP_Post $post Post object.
  3840 	 */
  4398 	 */
  3841 	$args = apply_filters( 'get_attached_media_args', $args, $type, $post );
  4399 	$args = apply_filters( 'get_attached_media_args', $args, $type, $post );
  3842 
  4400 
  3843 	$children = get_children( $args );
  4401 	$children = get_children( $args );
  3844 
  4402 
  3845 	/**
  4403 	/**
  3846 	 * Filters the list of media attached to the given post.
  4404 	 * Filters the list of media attached to the given post.
  3847 	 *
  4405 	 *
  3848 	 * @since 3.6.0
  4406 	 * @since 3.6.0
  3849 	 *
  4407 	 *
  3850 	 * @param array  $children Associative array of media attached to the given post.
  4408 	 * @param WP_Post[] $children Array of media attached to the given post.
  3851 	 * @param string $type     Mime type of the media desired.
  4409 	 * @param string    $type     Mime type of the media desired.
  3852 	 * @param mixed  $post     Post ID or object.
  4410 	 * @param WP_Post   $post     Post object.
  3853 	 */
  4411 	 */
  3854 	return (array) apply_filters( 'get_attached_media', $children, $type, $post );
  4412 	return (array) apply_filters( 'get_attached_media', $children, $type, $post );
  3855 }
  4413 }
  3856 
  4414 
  3857 /**
  4415 /**
  3858  * Check the content blob for an audio, video, object, embed, or iframe tags.
  4416  * Check the content HTML for a audio, video, object, embed, or iframe tags.
  3859  *
  4417  *
  3860  * @since 3.6.0
  4418  * @since 3.6.0
  3861  *
  4419  *
  3862  * @param string $content A string which might contain media data.
  4420  * @param string   $content A string of HTML which might contain media elements.
  3863  * @param array  $types   An array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'.
  4421  * @param string[] $types   An array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'.
  3864  * @return array A list of found HTML media embeds.
  4422  * @return string[] Array of found HTML media elements.
  3865  */
  4423  */
  3866 function get_media_embedded_in_content( $content, $types = null ) {
  4424 function get_media_embedded_in_content( $content, $types = null ) {
  3867 	$html = array();
  4425 	$html = array();
  3868 
  4426 
  3869 	/**
  4427 	/**
  3870 	 * Filters the embedded media types that are allowed to be returned from the content blob.
  4428 	 * Filters the embedded media types that are allowed to be returned from the content blob.
  3871 	 *
  4429 	 *
  3872 	 * @since 4.2.0
  4430 	 * @since 4.2.0
  3873 	 *
  4431 	 *
  3874 	 * @param array $allowed_media_types An array of allowed media types. Default media types are
  4432 	 * @param string[] $allowed_media_types An array of allowed media types. Default media types are
  3875 	 *                                   'audio', 'video', 'object', 'embed', and 'iframe'.
  4433 	 *                                      'audio', 'video', 'object', 'embed', and 'iframe'.
  3876 	 */
  4434 	 */
  3877 	$allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) );
  4435 	$allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) );
  3878 
  4436 
  3879 	if ( ! empty( $types ) ) {
  4437 	if ( ! empty( $types ) ) {
  3880 		if ( ! is_array( $types ) ) {
  4438 		if ( ! is_array( $types ) ) {
  3904  * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
  4462  * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
  3905  * @return array A list of arrays, each containing gallery data and srcs parsed
  4463  * @return array A list of arrays, each containing gallery data and srcs parsed
  3906  *               from the expanded shortcode.
  4464  *               from the expanded shortcode.
  3907  */
  4465  */
  3908 function get_post_galleries( $post, $html = true ) {
  4466 function get_post_galleries( $post, $html = true ) {
  3909 	if ( ! $post = get_post( $post ) ) {
  4467 	$post = get_post( $post );
       
  4468 
       
  4469 	if ( ! $post ) {
  3910 		return array();
  4470 		return array();
  3911 	}
  4471 	}
  3912 
  4472 
  3913 	if ( ! has_shortcode( $post->post_content, 'gallery' ) ) {
  4473 	if ( ! has_shortcode( $post->post_content, 'gallery' ) ) {
  3914 		return array();
  4474 		return array();
  3923 				$shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
  4483 				$shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
  3924 				if ( ! is_array( $shortcode_attrs ) ) {
  4484 				if ( ! is_array( $shortcode_attrs ) ) {
  3925 					$shortcode_attrs = array();
  4485 					$shortcode_attrs = array();
  3926 				}
  4486 				}
  3927 
  4487 
  3928 				// Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already.
  4488 				// Specify the post ID of the gallery we're viewing if the shortcode doesn't reference another post already.
  3929 				if ( ! isset( $shortcode_attrs['id'] ) ) {
  4489 				if ( ! isset( $shortcode_attrs['id'] ) ) {
  3930 					$shortcode[3] .= ' id="' . intval( $post->ID ) . '"';
  4490 					$shortcode[3] .= ' id="' . intval( $post->ID ) . '"';
  3931 				}
  4491 				}
  3932 
  4492 
  3933 				$gallery = do_shortcode_tag( $shortcode );
  4493 				$gallery = do_shortcode_tag( $shortcode );
  4010  * @since 3.6.0
  4570  * @since 3.6.0
  4011  *
  4571  *
  4012  * @see get_post_gallery()
  4572  * @see get_post_gallery()
  4013  *
  4573  *
  4014  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  4574  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
  4015  * @return array A list of a gallery's image srcs in order.
  4575  * @return string[] A list of a gallery's image srcs in order.
  4016  */
  4576  */
  4017 function get_post_gallery_images( $post = 0 ) {
  4577 function get_post_gallery_images( $post = 0 ) {
  4018 	$gallery = get_post_gallery( $post, false );
  4578 	$gallery = get_post_gallery( $post, false );
  4019 	return empty( $gallery['src'] ) ? array() : $gallery['src'];
  4579 	return empty( $gallery['src'] ) ? array() : $gallery['src'];
  4020 }
  4580 }
  4025  * @since 3.9.0
  4585  * @since 3.9.0
  4026  *
  4586  *
  4027  * @param WP_Post $attachment Attachment object.
  4587  * @param WP_Post $attachment Attachment object.
  4028  */
  4588  */
  4029 function wp_maybe_generate_attachment_metadata( $attachment ) {
  4589 function wp_maybe_generate_attachment_metadata( $attachment ) {
  4030 	if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int) $attachment->ID ) ) {
  4590 	if ( empty( $attachment ) || empty( $attachment->ID ) ) {
  4031 		return;
  4591 		return;
  4032 	}
  4592 	}
  4033 
  4593 
  4034 	$file = get_attached_file( $attachment_id );
  4594 	$attachment_id = (int) $attachment->ID;
  4035 	$meta = wp_get_attachment_metadata( $attachment_id );
  4595 	$file          = get_attached_file( $attachment_id );
       
  4596 	$meta          = wp_get_attachment_metadata( $attachment_id );
       
  4597 
  4036 	if ( empty( $meta ) && file_exists( $file ) ) {
  4598 	if ( empty( $meta ) && file_exists( $file ) ) {
  4037 		$_meta             = get_post_meta( $attachment_id );
  4599 		$_meta = get_post_meta( $attachment_id );
  4038 		$regeneration_lock = 'wp_generating_att_' . $attachment_id;
  4600 		$_lock = 'wp_generating_att_' . $attachment_id;
  4039 		if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) {
  4601 
  4040 			set_transient( $regeneration_lock, $file );
  4602 		if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) {
       
  4603 			set_transient( $_lock, $file );
  4041 			wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
  4604 			wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
  4042 			delete_transient( $regeneration_lock );
  4605 			delete_transient( $_lock );
  4043 		}
  4606 		}
  4044 	}
  4607 	}
  4045 }
  4608 }
  4046 
  4609 
  4047 /**
  4610 /**
  4061 	$path = $url;
  4624 	$path = $url;
  4062 
  4625 
  4063 	$site_url   = parse_url( $dir['url'] );
  4626 	$site_url   = parse_url( $dir['url'] );
  4064 	$image_path = parse_url( $path );
  4627 	$image_path = parse_url( $path );
  4065 
  4628 
  4066 	//force the protocols to match if needed
  4629 	// Force the protocols to match if needed.
  4067 	if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) {
  4630 	if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) {
  4068 		$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path );
  4631 		$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path );
  4069 	}
  4632 	}
  4070 
  4633 
  4071 	if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) {
  4634 	if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) {
  4072 		$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
  4635 		$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
  4073 	}
  4636 	}
  4074 
  4637 
  4075 	$sql     = $wpdb->prepare(
  4638 	$sql = $wpdb->prepare(
  4076 		"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
  4639 		"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
  4077 		$path
  4640 		$path
  4078 	);
  4641 	);
  4079 	$post_id = $wpdb->get_var( $sql );
  4642 
  4080 
  4643 	$results = $wpdb->get_results( $sql );
  4081 	/**
  4644 	$post_id = null;
  4082 	 * Filters an attachment id found by URL.
  4645 
       
  4646 	if ( $results ) {
       
  4647 		// Use the first available result, but prefer a case-sensitive match, if exists.
       
  4648 		$post_id = reset( $results )->post_id;
       
  4649 
       
  4650 		if ( count( $results ) > 1 ) {
       
  4651 			foreach ( $results as $result ) {
       
  4652 				if ( $path === $result->meta_value ) {
       
  4653 					$post_id = $result->post_id;
       
  4654 					break;
       
  4655 				}
       
  4656 			}
       
  4657 		}
       
  4658 	}
       
  4659 
       
  4660 	/**
       
  4661 	 * Filters an attachment ID found by URL.
  4083 	 *
  4662 	 *
  4084 	 * @since 4.2.0
  4663 	 * @since 4.2.0
  4085 	 *
  4664 	 *
  4086 	 * @param int|null $post_id The post_id (if any) found by the function.
  4665 	 * @param int|null $post_id The post_id (if any) found by the function.
  4087 	 * @param string   $url     The URL being looked up.
  4666 	 * @param string   $url     The URL being looked up.
  4092 /**
  4671 /**
  4093  * Returns the URLs for CSS files used in an iframe-sandbox'd TinyMCE media view.
  4672  * Returns the URLs for CSS files used in an iframe-sandbox'd TinyMCE media view.
  4094  *
  4673  *
  4095  * @since 4.0.0
  4674  * @since 4.0.0
  4096  *
  4675  *
  4097  * @return array The relevant CSS file URLs.
  4676  * @return string[] The relevant CSS file URLs.
  4098  */
  4677  */
  4099 function wpview_media_sandbox_styles() {
  4678 function wpview_media_sandbox_styles() {
  4100 	$version        = 'ver=' . get_bloginfo( 'version' );
  4679 	$version        = 'ver=' . get_bloginfo( 'version' );
  4101 	$mediaelement   = includes_url( "js/mediaelement/mediaelementplayer-legacy.min.css?$version" );
  4680 	$mediaelement   = includes_url( "js/mediaelement/mediaelementplayer-legacy.min.css?$version" );
  4102 	$wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" );
  4681 	$wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" );
  4103 
  4682 
  4104 	return array( $mediaelement, $wpmediaelement );
  4683 	return array( $mediaelement, $wpmediaelement );
  4105 }
  4684 }
  4106 
  4685 
  4107 /**
  4686 /**
  4108  * Registers the personal data exporter for media
  4687  * Registers the personal data exporter for media.
  4109  *
  4688  *
  4110  * @param array   $exporters   An array of personal data exporters.
  4689  * @param array[] $exporters An array of personal data exporters, keyed by their ID.
  4111  * @return array  An array of personal data exporters.
  4690  * @return array[] Updated array of personal data exporters.
  4112  */
  4691  */
  4113 function wp_register_media_personal_data_exporter( $exporters ) {
  4692 function wp_register_media_personal_data_exporter( $exporters ) {
  4114 	$exporters['wordpress-media'] = array(
  4693 	$exporters['wordpress-media'] = array(
  4115 		'exporter_friendly_name' => __( 'WordPress Media' ),
  4694 		'exporter_friendly_name' => __( 'WordPress Media' ),
  4116 		'callback'               => 'wp_media_personal_data_exporter',
  4695 		'callback'               => 'wp_media_personal_data_exporter',
  4122 /**
  4701 /**
  4123  * Finds and exports attachments associated with an email address.
  4702  * Finds and exports attachments associated with an email address.
  4124  *
  4703  *
  4125  * @since 4.9.6
  4704  * @since 4.9.6
  4126  *
  4705  *
  4127  * @param  string $email_address The attachment owner email address.
  4706  * @param string $email_address The attachment owner email address.
  4128  * @param  int    $page          Attachment page.
  4707  * @param int    $page          Attachment page.
  4129  * @return array  $return        An array of personal data.
  4708  * @return array An array of personal data.
  4130  */
  4709  */
  4131 function wp_media_personal_data_exporter( $email_address, $page = 1 ) {
  4710 function wp_media_personal_data_exporter( $email_address, $page = 1 ) {
  4132 	// Limit us to 50 attachments at a time to avoid timing out.
  4711 	// Limit us to 50 attachments at a time to avoid timing out.
  4133 	$number = 50;
  4712 	$number = 50;
  4134 	$page   = (int) $page;
  4713 	$page   = (int) $page;
  4165 					'value' => $attachment_url,
  4744 					'value' => $attachment_url,
  4166 				),
  4745 				),
  4167 			);
  4746 			);
  4168 
  4747 
  4169 			$data_to_export[] = array(
  4748 			$data_to_export[] = array(
  4170 				'group_id'    => 'media',
  4749 				'group_id'          => 'media',
  4171 				'group_label' => __( 'Media' ),
  4750 				'group_label'       => __( 'Media' ),
  4172 				'item_id'     => "post-{$post->ID}",
  4751 				'group_description' => __( 'User&#8217;s media data.' ),
  4173 				'data'        => $post_data_to_export,
  4752 				'item_id'           => "post-{$post->ID}",
       
  4753 				'data'              => $post_data_to_export,
  4174 			);
  4754 			);
  4175 		}
  4755 		}
  4176 	}
  4756 	}
  4177 
  4757 
  4178 	$done = $post_query->max_num_pages <= $page;
  4758 	$done = $post_query->max_num_pages <= $page;
  4180 	return array(
  4760 	return array(
  4181 		'data' => $data_to_export,
  4761 		'data' => $data_to_export,
  4182 		'done' => $done,
  4762 		'done' => $done,
  4183 	);
  4763 	);
  4184 }
  4764 }
       
  4765 
       
  4766 /**
       
  4767  * Add additional default image sub-sizes.
       
  4768  *
       
  4769  * These sizes are meant to enhance the way WordPress displays images on the front-end on larger,
       
  4770  * high-density devices. They make it possible to generate more suitable `srcset` and `sizes` attributes
       
  4771  * when the users upload large images.
       
  4772  *
       
  4773  * The sizes can be changed or removed by themes and plugins but that is not recommended.
       
  4774  * The size "names" reflect the image dimensions, so changing the sizes would be quite misleading.
       
  4775  *
       
  4776  * @since 5.3.0
       
  4777  * @access private
       
  4778  */
       
  4779 function _wp_add_additional_image_sizes() {
       
  4780 	// 2x medium_large size.
       
  4781 	add_image_size( '1536x1536', 1536, 1536 );
       
  4782 	// 2x large size.
       
  4783 	add_image_size( '2048x2048', 2048, 2048 );
       
  4784 }
       
  4785 
       
  4786 /**
       
  4787  * Callback to enable showing of the user error when uploading .heic images.
       
  4788  *
       
  4789  * @since 5.5.0
       
  4790  *
       
  4791  * @param array[] $plupload_settings The settings for Plupload.js.
       
  4792  * @return array[] Modified settings for Plupload.js.
       
  4793  */
       
  4794 function wp_show_heic_upload_error( $plupload_settings ) {
       
  4795 	$plupload_settings['heic_upload_error'] = true;
       
  4796 	return $plupload_settings;
       
  4797 }