*/ private const SKIP_EXIF_KEYS = array( 'MakerNote', 'UndefinedTag', 'UserComment', 'ComponentsConfiguration', 'FileSource', 'SceneType', 'CFAPattern', 'Exif_IFD_Pointer', 'GPS_IFD_Pointer', 'InteroperabilityOffset', 'THUMBNAIL', 'COMPUTED', ); /** * @return array{ * file: array, * wordpress: array, * image: array, * exif: list, * ai: array * } */ public function for_attachment( int $attachment_id, ?array $asset_row = null ): array { $post = get_post( $attachment_id ); $file = get_attached_file( $attachment_id ); $url = wp_get_attachment_url( $attachment_id ) ?: ''; $meta = wp_get_attachment_metadata( $attachment_id ); $meta = is_array( $meta ) ? $meta : array(); $exists = is_string( $file ) && $file !== '' && file_exists( $file ); $size = $exists ? (int) filesize( $file ) : (int) ( $asset_row['file_size'] ?? 0 ); $filename = is_string( $file ) && $file !== '' ? wp_basename( $file ) : ( $post ? $post->post_name : '' ); $ext = $filename !== '' ? strtolower( (string) pathinfo( $filename, PATHINFO_EXTENSION ) ) : ''; $uploads = wp_get_upload_dir(); $basedir = (string) ( $uploads['basedir'] ?? '' ); $rel_path = ''; if ( $exists && $basedir !== '' && str_starts_with( $file, $basedir ) ) { $rel_path = ltrim( substr( $file, strlen( $basedir ) ), '/\\' ); } elseif ( ! empty( $meta['file'] ) ) { $rel_path = (string) $meta['file']; } $width = isset( $meta['width'] ) ? (int) $meta['width'] : ( isset( $asset_row['width'] ) ? (int) $asset_row['width'] : 0 ); $height = isset( $meta['height'] ) ? (int) $meta['height'] : ( isset( $asset_row['height'] ) ? (int) $asset_row['height'] : 0 ); $author_id = $post ? (int) $post->post_author : 0; $author_name = ''; if ( $author_id > 0 ) { $user = get_userdata( $author_id ); $author_name = $user ? (string) $user->display_name : ''; } $sizes = array(); if ( ! empty( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { foreach ( $meta['sizes'] as $name => $size_meta ) { if ( ! is_array( $size_meta ) ) { continue; } $src = wp_get_attachment_image_src( $attachment_id, (string) $name ); $sizes[] = array( 'name' => (string) $name, 'width' => isset( $size_meta['width'] ) ? (int) $size_meta['width'] : null, 'height' => isset( $size_meta['height'] ) ? (int) $size_meta['height'] : null, 'file' => isset( $size_meta['file'] ) ? (string) $size_meta['file'] : '', 'mime' => isset( $size_meta['mime-type'] ) ? (string) $size_meta['mime-type'] : '', 'url' => is_array( $src ) && ! empty( $src[0] ) ? (string) $src[0] : '', ); } } $image_meta = isset( $meta['image_meta'] ) && is_array( $meta['image_meta'] ) ? $meta['image_meta'] : array(); $exif_rows = $this->build_exif_rows( $file, $exists, $image_meta ); return array( 'file' => array( 'filename' => $filename, 'extension' => $ext, 'path' => $rel_path, 'url' => $url, 'mimeType' => $post ? (string) $post->post_mime_type : '', 'fileSize' => $size > 0 ? $size : null, 'fileSizeLabel' => $size > 0 ? size_format( $size ) : '', 'exists' => $exists, ), 'wordpress' => array( 'attachmentId' => $attachment_id, 'editUrl' => get_edit_post_link( $attachment_id, 'raw' ) ?: '', 'uploadedAt' => $post ? (string) $post->post_date : '', 'modifiedAt' => $post ? (string) $post->post_modified : '', 'authorId' => $author_id > 0 ? $author_id : null, 'author' => $author_name, 'guid' => $post ? (string) $post->guid : '', 'postStatus' => $post ? (string) $post->post_status : '', ), 'image' => array( 'width' => $width > 0 ? $width : null, 'height' => $height > 0 ? $height : null, 'dimensions' => ( $width > 0 && $height > 0 ) ? sprintf( '%d × %d', $width, $height ) : '', 'megapixels' => ( $width > 0 && $height > 0 ) ? round( ( $width * $height ) / 1000000, 2 ) : null, 'orientation' => isset( $image_meta['orientation'] ) ? (string) $image_meta['orientation'] : '', 'sizes' => $sizes, ), 'exif' => $exif_rows, 'ai' => array( 'status' => (string) ( $asset_row['ai_status'] ?? 'pending' ), 'description' => isset( $asset_row['ai_description'] ) ? (string) $asset_row['ai_description'] : '', 'provider' => isset( $asset_row['ai_provider'] ) ? (string) $asset_row['ai_provider'] : '', 'model' => isset( $asset_row['ai_model'] ) ? (string) $asset_row['ai_model'] : '', 'analyzedAt' => isset( $asset_row['analyzed_at'] ) ? (string) $asset_row['analyzed_at'] : '', 'sha256' => isset( $asset_row['sha256'] ) ? (string) $asset_row['sha256'] : '', 'phash' => isset( $asset_row['perceptual_hash'] ) ? (string) $asset_row['perceptual_hash'] : '', 'duplicate' => (string) ( $asset_row['duplicate_status'] ?? 'unknown' ), 'optimization' => (string) ( $asset_row['optimization_status'] ?? 'none' ), 'usageCount' => isset( $asset_row['usage_count'] ) ? (int) $asset_row['usage_count'] : 0, ), ); } /** * @param array $image_meta * @return list */ private function build_exif_rows( ?string $file, bool $exists, array $image_meta ): array { $rows = array(); $seen = array(); $wp_map = array( 'camera' => __( 'Camera', 'foldrocket' ), 'created_timestamp' => __( 'Taken', 'foldrocket' ), 'aperture' => __( 'Aperture', 'foldrocket' ), 'shutter_speed' => __( 'Shutter speed', 'foldrocket' ), 'focal_length' => __( 'Focal length', 'foldrocket' ), 'iso' => __( 'ISO', 'foldrocket' ), 'credit' => __( 'Credit', 'foldrocket' ), 'copyright' => __( 'Copyright', 'foldrocket' ), 'title' => __( 'EXIF title', 'foldrocket' ), 'caption' => __( 'EXIF caption', 'foldrocket' ), 'orientation' => __( 'Orientation', 'foldrocket' ), ); foreach ( $wp_map as $key => $label ) { if ( ! isset( $image_meta[ $key ] ) || $image_meta[ $key ] === '' || $image_meta[ $key ] === null ) { continue; } $value = $this->format_wp_image_meta( $key, $image_meta[ $key ] ); if ( $value === '' ) { continue; } $rows[] = array( 'key' => $key, 'label' => $label, 'value' => $value, ); $seen[ $key ] = true; } if ( $exists && is_string( $file ) && function_exists( 'exif_read_data' ) ) { $mime = wp_check_filetype( $file ); $type = (string) ( $mime['type'] ?? '' ); if ( str_starts_with( $type, 'image/' ) && ! in_array( $type, array( 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/svg+xml' ), true ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $raw = @exif_read_data( $file, null, true ); if ( is_array( $raw ) ) { foreach ( $raw as $section => $fields ) { if ( ! is_array( $fields ) ) { continue; } if ( in_array( (string) $section, self::SKIP_EXIF_KEYS, true ) ) { continue; } foreach ( $fields as $key => $value ) { $key_s = (string) $key; if ( isset( $seen[ $key_s ] ) || in_array( $key_s, self::SKIP_EXIF_KEYS, true ) ) { continue; } if ( str_starts_with( $key_s, 'UndefinedTag' ) ) { continue; } $formatted = $this->format_raw_exif( $key_s, $value ); if ( $formatted === '' ) { continue; } $rows[] = array( 'key' => strtolower( (string) $section ) . '.' . $key_s, 'label' => $this->humanize_exif_key( $key_s ), 'value' => $formatted, ); $seen[ $key_s ] = true; } } } } } return $rows; } private function format_wp_image_meta( string $key, mixed $value ): string { if ( is_array( $value ) ) { return ''; } if ( 'created_timestamp' === $key && is_numeric( $value ) ) { $ts = (int) $value; if ( $ts > 0 ) { return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $ts ) ?: (string) $value; } } if ( 'aperture' === $key && is_numeric( $value ) ) { return 'f/' . rtrim( rtrim( number_format( (float) $value, 1, '.', '' ), '0' ), '.' ); } if ( 'focal_length' === $key && is_numeric( $value ) ) { return rtrim( rtrim( number_format( (float) $value, 1, '.', '' ), '0' ), '.' ) . ' mm'; } if ( 'shutter_speed' === $key && is_numeric( $value ) ) { $n = (float) $value; if ( $n > 0 && $n < 1 ) { return '1/' . (string) max( 1, (int) round( 1 / $n ) ) . ' s'; } return rtrim( rtrim( number_format( $n, 2, '.', '' ), '0' ), '.' ) . ' s'; } if ( 'iso' === $key ) { return (string) $value; } return trim( (string) $value ); } private function format_raw_exif( string $key, mixed $value ): string { if ( is_array( $value ) ) { $flat = array(); foreach ( $value as $v ) { if ( is_scalar( $v ) ) { $flat[] = (string) $v; } } if ( ! $flat ) { return ''; } $value = implode( ', ', $flat ); } if ( ! is_scalar( $value ) ) { return ''; } $str = trim( (string) $value ); if ( $str === '' || $str === '0' && in_array( $key, array( 'GPSLatitude', 'GPSLongitude' ), true ) ) { return ''; } // Skip binary-looking blobs. if ( strlen( $str ) > 240 ) { return ''; } if ( preg_match( '/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/', $str ) ) { return ''; } if ( in_array( $key, array( 'DateTime', 'DateTimeOriginal', 'DateTimeDigitized' ), true ) ) { $ts = strtotime( $str ); if ( $ts ) { return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $ts ) ?: $str; } } if ( 'FNumber' === $key || 'ApertureValue' === $key ) { if ( str_contains( $str, '/' ) ) { $parts = explode( '/', $str, 2 ); if ( count( $parts ) === 2 && (float) $parts[1] > 0 ) { $n = (float) $parts[0] / (float) $parts[1]; return 'f/' . rtrim( rtrim( number_format( $n, 1, '.', '' ), '0' ), '.' ); } } if ( is_numeric( $str ) ) { return 'f/' . $str; } } if ( 'ExposureTime' === $key && str_contains( $str, '/' ) ) { return $str . ' s'; } if ( 'FocalLength' === $key ) { if ( str_contains( $str, '/' ) ) { $parts = explode( '/', $str, 2 ); if ( count( $parts ) === 2 && (float) $parts[1] > 0 ) { $n = (float) $parts[0] / (float) $parts[1]; return rtrim( rtrim( number_format( $n, 1, '.', '' ), '0' ), '.' ) . ' mm'; } } if ( is_numeric( $str ) ) { return $str . ' mm'; } } return $str; } private function humanize_exif_key( string $key ): string { $map = array( 'Make' => __( 'Make', 'foldrocket' ), 'Model' => __( 'Model', 'foldrocket' ), 'Software' => __( 'Software', 'foldrocket' ), 'DateTime' => __( 'File date', 'foldrocket' ), 'DateTimeOriginal' => __( 'Date taken', 'foldrocket' ), 'DateTimeDigitized' => __( 'Date digitized', 'foldrocket' ), 'ExposureTime' => __( 'Exposure time', 'foldrocket' ), 'FNumber' => __( 'F-number', 'foldrocket' ), 'ISOSpeedRatings' => __( 'ISO', 'foldrocket' ), 'PhotographicSensitivity' => __( 'ISO', 'foldrocket' ), 'FocalLength' => __( 'Focal length', 'foldrocket' ), 'FocalLengthIn35mmFilm' => __( 'Focal length (35mm)', 'foldrocket' ), 'Flash' => __( 'Flash', 'foldrocket' ), 'WhiteBalance' => __( 'White balance', 'foldrocket' ), 'MeteringMode' => __( 'Metering mode', 'foldrocket' ), 'ExposureMode' => __( 'Exposure mode', 'foldrocket' ), 'ExposureProgram' => __( 'Exposure program', 'foldrocket' ), 'LensModel' => __( 'Lens', 'foldrocket' ), 'Artist' => __( 'Artist', 'foldrocket' ), 'Copyright' => __( 'Copyright', 'foldrocket' ), 'ImageDescription' => __( 'Image description', 'foldrocket' ), 'GPSLatitude' => __( 'GPS latitude', 'foldrocket' ), 'GPSLongitude' => __( 'GPS longitude', 'foldrocket' ), 'GPSAltitude' => __( 'GPS altitude', 'foldrocket' ), 'ColorSpace' => __( 'Color space', 'foldrocket' ), 'XResolution' => __( 'X resolution', 'foldrocket' ), 'YResolution' => __( 'Y resolution', 'foldrocket' ), 'ResolutionUnit' => __( 'Resolution unit', 'foldrocket' ), 'Orientation' => __( 'Orientation', 'foldrocket' ), ); if ( isset( $map[ $key ] ) ) { return $map[ $key ]; } $spaced = preg_replace( '/([a-z])([A-Z])/', '$1 $2', $key ); return $spaced ? (string) $spaced : $key; } }