| 1 |
<?php |
| 2 |
|
| 3 |
$id = wp_unique_id( 'file-download-card-' ); |
| 4 |
|
| 5 |
$file = isset( $attributes['file'] ) && is_array( $attributes['file'] ) ? $attributes['file'] : []; |
| 6 |
|
| 7 |
$fileId = isset( $file['id'] ) ? absint( $file['id'] ) : 0; |
| 8 |
$fileUrl = isset( $file['url'] ) ? esc_url_raw( (string) $file['url'] ) : ''; |
| 9 |
$fileName = isset( $file['name'] ) ? (string) $file['name'] : ''; |
| 10 |
$fileMime = isset( $file['type'] ) ? (string) $file['type'] : ''; |
| 11 |
$fileSize = isset( $file['size'] ) ? (string) $file['size'] : ''; |
| 12 |
|
| 13 |
if ( $fileId > 0 ) { |
| 14 |
// Trust the attachment id over client-saved values. |
| 15 |
$resolvedUrl = wp_get_attachment_url( $fileId ); |
| 16 |
if ( is_string( $resolvedUrl ) && '' !== $resolvedUrl ) { |
| 17 |
$fileUrl = esc_url_raw( $resolvedUrl ); |
| 18 |
} |
| 19 |
|
| 20 |
$resolvedMime = get_post_mime_type( $fileId ); |
| 21 |
if ( is_string( $resolvedMime ) && '' !== $resolvedMime ) { |
| 22 |
$fileMime = $resolvedMime; |
| 23 |
} |
| 24 |
|
| 25 |
if ( '' === trim( $fileName ) ) { |
| 26 |
$attTitle = get_the_title( $fileId ); |
| 27 |
if ( is_string( $attTitle ) && '' !== $attTitle ) { |
| 28 |
$fileName = $attTitle; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// Resolve a real, human-readable size from the file on disk. |
| 33 |
$attPath = get_attached_file( $fileId ); |
| 34 |
if ( is_string( $attPath ) && '' !== $attPath && file_exists( $attPath ) ) { |
| 35 |
$bytes = filesize( $attPath ); |
| 36 |
if ( false !== $bytes && $bytes > 0 ) { |
| 37 |
$fileSize = size_format( $bytes, 1 ); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// Nothing usable to download — render nothing. |
| 43 |
if ( '' === $fileUrl ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// Merge the resolved file fields back into the attributes handed to React. |
| 48 |
$attributes['file'] = array_merge( |
| 49 |
$file, |
| 50 |
[ |
| 51 |
'url' => $fileUrl, |
| 52 |
'name' => $fileName, |
| 53 |
'type' => $fileMime, |
| 54 |
'size' => $fileSize, |
| 55 |
] |
| 56 |
); |
| 57 |
?> |
| 58 |
<div |
| 59 |
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() is properly escaped. ?> |
| 60 |
<?php echo get_block_wrapper_attributes(); ?> |
| 61 |
id='<?php echo esc_attr( $id ); ?>' |
| 62 |
data-attributes='<?php echo esc_attr( wp_json_encode( $attributes ) ); ?>' |
| 63 |
></div> |
| 64 |
|