Index: media.php
===================================================================
--- media.php	(revision 51266)
+++ media.php	(working copy)
@@ -175,8 +175,9 @@
  * elements that are normally returned from the function.
  *
  * @since 2.5.0
+ * @since 5.6.0 allow WP_Post object to be passed.
  *
- * @param int          $id   Attachment ID for image.
+ * @param int|WP_Post  $attachment Attachment post or attachment ID for image.
  * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
  *                           of width and height values in pixels (in that order). Default 'medium'.
  * @return array|false {
@@ -188,9 +189,16 @@
  *     @type bool   $3 Whether the image is a resized image.
  * }
  */
-function image_downsize( $id, $size = 'medium' ) {
-	$is_image = wp_attachment_is_image( $id );
+function image_downsize( $attachment, $size = 'medium' ) {
 
+	$post = get_post( $attachment );
+
+	if ( ! $post || 'attachment' !== $post->post_type ) {
+		return false;
+	}
+
+	$is_image = wp_attachment_is_image( $post );
+
 	/**
 	 * Filters whether to preempt the output of image_downsize().
 	 *
@@ -198,20 +206,22 @@
 	 * down-sizing the image, returning that value instead.
 	 *
 	 * @since 2.5.0
+	 * @since 5.6.0 add $post parameter.
 	 *
 	 * @param bool|array   $downsize Whether to short-circuit the image downsize.
 	 * @param int          $id       Attachment ID for image.
 	 * @param string|int[] $size     Requested image size. Can be any registered image size name, or
 	 *                               an array of width and height values in pixels (in that order).
+	 * @param WP_Post      $post     Attachment post object.
 	 */
-	$out = apply_filters( 'image_downsize', false, $id, $size );
+	$out = apply_filters( 'image_downsize', false, $post->ID, $size, $post );
 
 	if ( $out ) {
 		return $out;
 	}
 
-	$img_url          = wp_get_attachment_url( $id );
-	$meta             = wp_get_attachment_metadata( $id );
+	$img_url          = wp_get_attachment_url( $post );
+	$meta             = wp_get_attachment_metadata( $post->ID );
 	$width            = 0;
 	$height           = 0;
 	$is_intermediate  = false;
@@ -231,7 +241,7 @@
 	}
 
 	// Try for a new style intermediate size.
-	$intermediate = image_get_intermediate_size( $id, $size );
+	$intermediate = image_get_intermediate_size( $post, $size );
 
 	if ( $intermediate ) {
 		$img_url         = str_replace( $img_url_basename, $intermediate['file'], $img_url );
@@ -240,7 +250,7 @@
 		$is_intermediate = true;
 	} elseif ( 'thumbnail' === $size ) {
 		// Fall back to the old thumbnail.
-		$thumb_file = wp_get_attachment_thumb_file( $id );
+		$thumb_file = wp_get_attachment_thumb_file( $post );
 		$info       = null;
 
 		if ( $thumb_file ) {
@@ -361,17 +371,25 @@
  * content.
  *
  * @since 2.5.0
+ * @since 5.6.0 allow WP_Post object to be passed.
  *
- * @param int          $id    Attachment ID.
+ * @param int|WP_Post  $attachment Attachment post or post ID.
  * @param string       $alt   Image description for the alt attribute.
  * @param string       $title Image description for the title attribute.
  * @param string       $align Part of the class name for aligning the image.
  * @param string|int[] $size  Optional. Image size. Accepts any registered image size name, or an array of
- *                            width and height values in pixels (in that order). Default 'medium'.
+ *                           width and height values in pixels (in that order). Default 'medium'.
  * @return string HTML IMG element for given image attachment
  */
-function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
+function get_image_tag( $attachment, $alt, $title, $align, $size = 'medium' ) {
 
+	$post = get_post( $attachment );
+
+	if ( ! $post || 'attachment' !== $post->post_type ) {
+		return '';
+	}
+
+	$id = $post->ID;
 	list( $img_src, $width, $height ) = image_downsize( $id, $size );
 	$hwstring                         = image_hwstring( $width, $height );
 
@@ -384,6 +402,7 @@
 	 * Filters the value of the attachment's image tag class attribute.
 	 *
 	 * @since 2.6.0
+	 * @since 5.6.0 add $post parameter.
 	 *
 	 * @param string       $class CSS class name or space-separated list of classes.
 	 * @param int          $id    Attachment ID.
@@ -390,8 +409,9 @@
 	 * @param string       $align Part of the class name for aligning the image.
 	 * @param string|int[] $size  Requested image size. Can be any registered image size name, or
 	 *                            an array of width and height values in pixels (in that order).
+	 * @param WP_Post      $post  Attachment post object.
 	 */
-	$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );
+	$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size, $post );
 
 	$html = '<img src="' . esc_attr( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
 
@@ -399,6 +419,7 @@
 	 * Filters the HTML content for the image tag.
 	 *
 	 * @since 2.6.0
+	 * @since 5.6.0 add $post parameter.
 	 *
 	 * @param string       $html  HTML content for the image.
 	 * @param int          $id    Attachment ID.
@@ -407,8 +428,9 @@
 	 * @param string       $align Part of the class name for aligning the image.
 	 * @param string|int[] $size  Requested image size. Can be any registered image size name, or
 	 *                            an array of width and height values in pixels (in that order).
+	 * @param WP_Post      $post  Attachment post object.
 	 */
-	return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
+	return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size, $post );
 }
 
 /**
@@ -741,10 +763,11 @@
  * browser scale down the image.
  *
  * @since 2.5.0
+ * @since 5.6.0 allow WP_Post object to be passed.
  *
- * @param int          $post_id Attachment ID.
- * @param string|int[] $size    Optional. Image size. Accepts any registered image size name, or an array
- *                              of width and height values in pixels (in that order). Default 'thumbnail'.
+ * @param int|WP_Post  $attachment Attachment post or post ID.
+ * @param string|int[] $size       Optional. Image size. Accepts any registered image size name, or an array
+ *                                  of width and height values in pixels (in that order). Default 'thumbnail'.
  * @return array|false {
  *     Array of file relative path, width, and height on success. Additionally includes absolute
  *     path and URL if registered size is passed to `$size` parameter. False on failure.
@@ -756,15 +779,21 @@
  *     @type string $url    URL of image.
  * }
  */
-function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
-	$imagedata = wp_get_attachment_metadata( $post_id );
+function image_get_intermediate_size( $attachment, $size = 'thumbnail' ) {
 
-	if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
+	$data = array();
+	$post = get_post( $attachment );
+
+	if ( ! $size || ! $post || 'attachment' !== $post->post_type ) {
 		return false;
 	}
 
-	$data = array();
+	$imagedata = wp_get_attachment_metadata( $post );
 
+	if ( ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
+		return false;
+	}
+
 	// Find the best match when '$size' is an array.
 	if ( is_array( $size ) ) {
 		$candidates = array();
@@ -828,7 +857,7 @@
 
 	// Include the full filesystem path of the intermediate file.
 	if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
-		$file_url     = wp_get_attachment_url( $post_id );
+		$file_url     = wp_get_attachment_url( $post );
 		$data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] );
 		$data['url']  = path_join( dirname( $file_url ), $data['file'] );
 	}
@@ -837,16 +866,18 @@
 	 * Filters the output of image_get_intermediate_size()
 	 *
 	 * @since 4.4.0
+	 * @since 5.6.0 add $post parameter.
 	 *
 	 * @see image_get_intermediate_size()
 	 *
 	 * @param array        $data    Array of file relative path, width, and height on success. May also include
 	 *                              file absolute path and URL.
-	 * @param int          $post_id The ID of the image attachment.
+	 * @param int                   The ID of the image attachment.
 	 * @param string|int[] $size    Requested image size. Can be any registered image size name, or
 	 *                              an array of width and height values in pixels (in that order).
+	 * @param WP_Post      $post    Attachment post object.
 	 */
-	return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
+	return apply_filters( 'image_get_intermediate_size', $data, $post->ID, $size, $post );
 }
 
 /**
@@ -934,8 +965,9 @@
  * Retrieves an image to represent an attachment.
  *
  * @since 2.5.0
+ * @since 5.6.0 allow WP_Post object to be passed.
  *
- * @param int          $attachment_id Image attachment ID.
+ * @param int|WP_Post  $attachment    Attachment post or post ID.
  * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
  *                                    width and height values in pixels (in that order). Default 'thumbnail'.
  * @param bool         $icon          Optional. Whether the image should fall back to a mime type icon. Default false.
@@ -948,8 +980,16 @@
  *     @type bool   $3 Whether the image is a resized image.
  * }
  */
-function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
+function wp_get_attachment_image_src( $attachment, $size = 'thumbnail', $icon = false ) {
+
+	$post = get_post( $attachment );
+
+	if ( ! $post || 'attachment' !== $post->post_type ) {
+		return false;
+	}
+
 	// Get a thumbnail or intermediate image if there is one.
+	$attachment_id = $post->ID;
 	$image = image_downsize( $attachment_id, $size );
 	if ( ! $image ) {
 		$src = false;
@@ -974,6 +1014,7 @@
 	 * Filters the attachment image source result.
 	 *
 	 * @since 4.3.0
+	 * @since 5.6.0 add $post parameter.
 	 *
 	 * @param array|false  $image         {
 	 *     Array of image data, or boolean false if no image is available.
@@ -987,8 +1028,9 @@
 	 * @param string|int[] $size          Requested image size. Can be any registered image size name, or
 	 *                                    an array of width and height values in pixels (in that order).
 	 * @param bool         $icon          Whether the image should be treated as an icon.
+	 * @param WP_Post      $post          Attachment post object.
 	 */
-	return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
+	return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon, $post );
 }
 
 /**
@@ -1002,8 +1044,9 @@
  * @since 2.5.0
  * @since 4.4.0 The `$srcset` and `$sizes` attributes were added.
  * @since 5.5.0 The `$loading` attribute was added.
+ * @since 5.6.0 allow WP_Post object to be passed.
  *
- * @param int          $attachment_id Image attachment ID.
+ * @param int|WP_Post  $attachment    Attachment post or post ID.
  * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array
  *                                    of width and height values in pixels (in that order). Default 'thumbnail'.
  * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
@@ -1023,14 +1066,14 @@
  * }
  * @return string HTML img element or empty string on failure.
  */
-function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
+function wp_get_attachment_image( $attachment, $size = 'thumbnail', $icon = false, $attr = '' ) {
 	$html  = '';
-	$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
+	$image = wp_get_attachment_image_src( $attachment, $size, $icon );
 
 	if ( $image ) {
 		list( $src, $width, $height ) = $image;
 
-		$attachment = get_post( $attachment_id );
+		$post       = get_post( $attachment );
 		$hwstring   = image_hwstring( $width, $height );
 		$size_class = $size;
 
@@ -1041,7 +1084,7 @@
 		$default_attr = array(
 			'src'   => $src,
 			'class' => "attachment-$size_class size-$size_class",
-			'alt'   => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
+			'alt'   => trim( strip_tags( get_post_meta( $post->ID, '_wp_attachment_image_alt', true ) ) ),
 		);
 
 		// Add `loading` attribute.
@@ -1059,12 +1102,12 @@
 
 		// Generate 'srcset' and 'sizes' if not already present.
 		if ( empty( $attr['srcset'] ) ) {
-			$image_meta = wp_get_attachment_metadata( $attachment_id );
+			$image_meta = wp_get_attachment_metadata( $post->ID );
 
 			if ( is_array( $image_meta ) ) {
 				$size_array = array( absint( $width ), absint( $height ) );
-				$srcset     = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
-				$sizes      = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );
+				$srcset     = wp_calculate_image_srcset( $size_array, $src, $image_meta, $post->ID );
+				$sizes      = wp_calculate_image_sizes( $size_array, $src, $image_meta, $post->ID );
 
 				if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
 					$attr['srcset'] = $srcset;
@@ -1083,11 +1126,11 @@
 		 *
 		 * @param string[]     $attr       Array of attribute values for the image markup, keyed by attribute name.
 		 *                                 See wp_get_attachment_image().
-		 * @param WP_Post      $attachment Image attachment post.
+		 * @param WP_Post      $post       Image attachment post.
 		 * @param string|int[] $size       Requested image size. Can be any registered image size name, or
 		 *                                 an array of width and height values in pixels (in that order).
 		 */
-		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
+		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $post, $size );
 
 		$attr = array_map( 'esc_attr', $attr );
 		$html = rtrim( "<img $hwstring" );
@@ -1112,7 +1155,7 @@
 	 * @param string[]     $attr          Array of attribute values for the image markup, keyed by attribute name.
 	 *                                    See wp_get_attachment_image().
 	 */
-	return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr );
+	return apply_filters( 'wp_get_attachment_image', $html, $attachment, $size, $icon, $attr );
 }
 
 /**
@@ -1197,7 +1240,7 @@
  *
  * @see wp_calculate_image_srcset()
  *
- * @param int          $attachment_id Image attachment ID.
+ * @param int          $attachment    Attachment post or post ID.
  * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
  *                                    width and height values in pixels (in that order). Default 'medium'.
  * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
@@ -1204,15 +1247,17 @@
  *                                    Default null.
  * @return string|false A 'srcset' value string or false.
  */
-function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
-	$image = wp_get_attachment_image_src( $attachment_id, $size );
+function wp_get_attachment_image_srcset( $attachment, $size = 'medium', $image_meta = null ) {
+	$image = wp_get_attachment_image_src( $attachment, $size );
 
 	if ( ! $image ) {
 		return false;
 	}
 
+	$post = get_post( $attachment );
+
 	if ( ! is_array( $image_meta ) ) {
-		$image_meta = wp_get_attachment_metadata( $attachment_id );
+		$image_meta = wp_get_attachment_metadata( $post->ID );
 	}
 
 	$image_src  = $image[0];
@@ -1221,7 +1266,7 @@
 		absint( $image[2] ),
 	);
 
-	return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
+	return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $post->ID );
 }
 
 /**
@@ -1438,7 +1483,7 @@
  *
  * @see wp_calculate_image_sizes()
  *
- * @param int          $attachment_id Image attachment ID.
+ * @param int          $attachment    Attachment post or post ID.
  * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
  *                                    width and height values in pixels (in that order). Default 'medium'.
  * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
@@ -1445,15 +1490,17 @@
  *                                    Default null.
  * @return string|false A valid source size value for use in a 'sizes' attribute or false.
  */
-function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
-	$image = wp_get_attachment_image_src( $attachment_id, $size );
+function wp_get_attachment_image_sizes( $attachment, $size = 'medium', $image_meta = null ) {
+	$image = wp_get_attachment_image_src( $attachment, $size );
 
 	if ( ! $image ) {
 		return false;
 	}
 
+	$post = get_post( $attachment );
+
 	if ( ! is_array( $image_meta ) ) {
-		$image_meta = wp_get_attachment_metadata( $attachment_id );
+		$image_meta = wp_get_attachment_metadata( $post->ID );
 	}
 
 	$image_src  = $image[0];
@@ -1462,7 +1509,7 @@
 		absint( $image[2] ),
 	);
 
-	return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
+	return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $post->ID );
 }
 
 /**
Index: post.php
===================================================================
--- post.php	(revision 51266)
+++ post.php	(working copy)
@@ -6289,9 +6289,11 @@
  * Retrieves attachment metadata for attachment ID.
  *
  * @since 2.1.0
+ * @since 5.6.0 allow WP_Post object to be passed.
  *
- * @param int  $attachment_id Attachment post ID. Defaults to global $post.
- * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
+ * @param int|WP_Post   $attachment    Attachment post or post ID.
+ * @param bool          $unfiltered    Optional. If true, filters are not run. Default false.
+ *
  * @return array|false {
  *     Attachment metadata. False on failure.
  *
@@ -6378,24 +6380,18 @@
  *
  * @global string $pagenow
  *
- * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
+ * @param int|WP_Post $attachment Attachment post or post ID. Defaults to global $post.
  * @return string|false Attachment URL, otherwise false.
  */
-function wp_get_attachment_url( $attachment_id = 0 ) {
+function wp_get_attachment_url( $attachment = 0 ) {
 	global $pagenow;
 
-	$attachment_id = (int) $attachment_id;
+	$post = get_post( $attachment );
 
-	$post = get_post( $attachment_id );
-
-	if ( ! $post ) {
+	if( ! $post || 'attachment' !== $post->post_type ) {
 		return false;
 	}
 
-	if ( 'attachment' !== $post->post_type ) {
-		return false;
-	}
-
 	$url = '';
 	// Get attached file.
 	$file = get_post_meta( $post->ID, '_wp_attached_file', true );
@@ -6434,11 +6430,13 @@
 	 * Filters the attachment URL.
 	 *
 	 * @since 2.1.0
+	 * @since 5.6.0 add $post parameter.
 	 *
-	 * @param string $url           URL for the given attachment.
-	 * @param int    $attachment_id Attachment post ID.
+	 * @param string  $url              URL for the given attachment.
+	 * @param int     $attachment_id    Attachment post ID.
+	 * @param WP_Post $post             Attachment WP_Post object.
 	 */
-	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
+	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID, $post );
 
 	if ( ! $url ) {
 		return false;
@@ -6510,11 +6508,13 @@
 			 * Filters the attachment thumbnail file path.
 			 *
 			 * @since 2.1.0
+			 * @since 5.6.0 add $post parameter.
 			 *
-			 * @param string $thumbfile File path to the attachment thumbnail.
-			 * @param int    $post_id   Attachment ID.
+			 * @param string  $thumbfile       File path to the attachment thumbnail.
+			 * @param int     $attachment_id   Attachment ID.
+			 * @param WP_Post $post            Attachment post object.
 			 */
-			return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
+			return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID, $post );
 		}
 	}
 	return false;
@@ -6541,7 +6541,7 @@
 		return false;
 	}
 
-	$sized = image_downsize( $post_id, 'thumbnail' );
+	$sized = image_downsize( $post->ID, 'thumbnail' );
 	if ( $sized ) {
 		return $sized[0];
 	}
@@ -6557,11 +6557,13 @@
 	 * Filters the attachment thumbnail URL.
 	 *
 	 * @since 2.1.0
+	 * @since 5.6.0 add $post parameter.
 	 *
-	 * @param string $url     URL for the attachment thumbnail.
-	 * @param int    $post_id Attachment ID.
+	 * @param string  $url              URL for the attachment thumbnail.
+	 * @param int     $attachment_id    Attachment ID.
+	 * @param WP_Post $post             Attachment post object.
 	 */
-	return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
+	return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID, $post );
 }
 
 /**
