函数文档

wp_image_file_matches_image_meta()

💡 云策文档标注

概述

wp_image_file_matches_image_meta() 函数用于判断给定的图像文件路径或 URI 是否与图像元数据匹配,常用于处理附件 ID 变更的场景,如网站迁移。

关键要点

  • 函数通过比较图像路径与元数据中的文件路径来确定匹配性,支持原始图像、缩放版本和子目录检查。
  • 参数包括 $image_location(图像路径或 URI)、$image_meta(从 wp_get_attachment_metadata() 获取的元数据)和可选的 $attachment_id。
  • 返回布尔值表示匹配结果,并可通过 wp_image_file_matches_image_meta 过滤器进行自定义修改。
  • 内部使用 _wp_get_attachment_relative_path() 和 trailingslashit() 辅助函数处理路径。

代码示例

function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) {
    $match = false;
    if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) {
        list( $image_location ) = explode( '?', $image_location );
        if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) {
            $match = true;
        } else {
            $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
            if ( $dirname ) {
                $dirname = trailingslashit( $dirname );
            }
            if ( ! empty( $image_meta['original_image'] ) ) {
                $relative_path = $dirname . $image_meta['original_image'];
                if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
                    $match = true;
                }
            }
            if ( ! $match && ! empty( $image_meta['sizes'] ) ) {
                foreach ( $image_meta['sizes'] as $image_size_data ) {
                    $relative_path = $dirname . $image_size_data['file'];
                    if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
                        $match = true;
                        break;
                    }
                }
            }
        }
    }
    return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id );
}

注意事项

  • 该函数在 WordPress 5.5.0 版本中引入,主要用于处理附件 ID 不一致的情况,如网站导入导出。
  • 使用前需确保 $image_meta 参数有效,包含 'file' 键且长度大于 4。
  • 函数会自动移除 $image_location 中的查询参数,并优先检查元数据中的主文件路径,然后依次检查原始图像和缩放版本。

📄 原文内容

Determines if the image meta data is for the image source file.

Description

The image meta data is retrieved by attachment post ID. In some cases the post IDs may change.
For example when the website is exported and imported at another website. Then the attachment post IDs that are in post_content for the exported website may not match the same attachments at the new website.

Parameters

$image_locationstringrequired
The full path or URI to the image file.
$image_metaarrayrequired
The attachment meta data as returned by ‘wp_get_attachment_metadata() ‘.
$attachment_idintoptional
The image attachment ID. Default 0.

Return

bool Whether the image meta is for this image file.

Source

function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) {
	$match = false;

	// Ensure the $image_meta is valid.
	if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) {
		// Remove query args in image URI.
		list( $image_location ) = explode( '?', $image_location );

		// Check if the relative image path from the image meta is at the end of $image_location.
		if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) {
			$match = true;
		} else {
			// Retrieve the uploads sub-directory from the full size image.
			$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );

			if ( $dirname ) {
				$dirname = trailingslashit( $dirname );
			}

			if ( ! empty( $image_meta['original_image'] ) ) {
				$relative_path = $dirname . $image_meta['original_image'];

				if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
					$match = true;
				}
			}

			if ( ! $match && ! empty( $image_meta['sizes'] ) ) {
				foreach ( $image_meta['sizes'] as $image_size_data ) {
					$relative_path = $dirname . $image_size_data['file'];

					if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
						$match = true;
						break;
					}
				}
			}
		}
	}

	/**
	 * Filters whether an image path or URI matches image meta.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $match          Whether the image relative path from the image meta
	 *                               matches the end of the URI or path to the image file.
	 * @param string $image_location Full path or URI to the tested image file.
	 * @param array  $image_meta     The image meta data as returned by 'wp_get_attachment_metadata()'.
	 * @param int    $attachment_id  The image attachment ID or 0 if not supplied.
	 */
	return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id );
}

Hooks

apply_filters( ‘wp_image_file_matches_image_meta’, bool $match, string $image_location, array $image_meta, int $attachment_id )

Filters whether an image path or URI matches image meta.

Changelog

Version Description
5.5.0 Introduced.