| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — `attachment` file type. |
| 4 |
* |
| 5 |
* @package OpenStation |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* The `attachment` desktop file type. |
| 12 |
*/ |
| 13 |
class OpenStation_Attachment_File extends OpenStation_File { |
| 14 |
|
| 15 |
/** |
| 16 |
* Get the file type identifier. |
| 17 |
* |
| 18 |
* @return string |
| 19 |
*/ |
| 20 |
public static function type(): string { |
| 21 |
return 'attachment'; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Whether the attachment post still exists. |
| 26 |
* |
| 27 |
* @return bool |
| 28 |
*/ |
| 29 |
public function exists(): bool { |
| 30 |
return $this->attachment() instanceof WP_Post; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get a label from the attachment title or fall back to the |
| 35 |
* filename. |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function title(): string { |
| 40 |
$post = $this->attachment(); |
| 41 |
if ( ! $post ) { |
| 42 |
return __( '(missing media)', 'desktop-mode' ); |
| 43 |
} |
| 44 |
$title = get_the_title( $post ); |
| 45 |
return wp_strip_all_tags( '' !== $title ? $title : basename( (string) get_attached_file( $post->ID ) ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Resolve a Dashicon class based on the attachment's mime-type |
| 50 |
* category. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
public function icon(): string { |
| 55 |
$post = $this->attachment(); |
| 56 |
if ( ! $post ) { |
| 57 |
return 'dashicons-warning'; |
| 58 |
} |
| 59 |
$type = strtok( (string) $post->post_mime_type, '/' ); |
| 60 |
switch ( $type ) { |
| 61 |
case 'image': |
| 62 |
return 'dashicons-format-image'; |
| 63 |
case 'video': |
| 64 |
return 'dashicons-format-video'; |
| 65 |
case 'audio': |
| 66 |
return 'dashicons-format-audio'; |
| 67 |
default: |
| 68 |
return 'dashicons-media-default'; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get the attachment's thumbnail URL for tile previews. |
| 74 |
* |
| 75 |
* @return string Empty string when no image source is available. |
| 76 |
*/ |
| 77 |
public function preview_url(): string { |
| 78 |
$post = $this->attachment(); |
| 79 |
if ( ! $post ) { |
| 80 |
return ''; |
| 81 |
} |
| 82 |
$src = wp_get_attachment_image_src( $post->ID, 'thumbnail' ); |
| 83 |
return is_array( $src ) ? (string) $src[0] : ''; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Check whether the given user can read this attachment. |
| 88 |
* |
| 89 |
* @param int $user_id |
| 90 |
* @return bool |
| 91 |
*/ |
| 92 |
public function can_read( int $user_id ): bool { |
| 93 |
$post = $this->attachment(); |
| 94 |
if ( ! $post ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
return user_can( $user_id, 'read_post', $post->ID ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Augment the base serialized shape with mime type, source URL, |
| 102 |
* and alt text for block-editor drag handlers. |
| 103 |
* |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
public function serialize(): array { |
| 107 |
$shape = parent::serialize(); |
| 108 |
$post = $this->attachment(); |
| 109 |
$shape['mime'] = $post ? (string) $post->post_mime_type : ''; |
| 110 |
// Full-size source URL + alt text — surfaced so cross-frame |
| 111 |
// drag handlers can build `core/image` / `core/video` / |
| 112 |
// `core/audio` / `core/file` blocks on drop into Gutenberg |
| 113 |
// without a REST roundtrip. `previewUrl` is a thumbnail and |
| 114 |
// not the right attribute for the inserted block. |
| 115 |
$shape['sourceUrl'] = $post ? (string) wp_get_attachment_url( $post->ID ) : ''; |
| 116 |
$shape['alt'] = $post ? (string) get_post_meta( $post->ID, '_wp_attachment_image_alt', true ) : ''; |
| 117 |
return $shape; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Lazily resolve the underlying WP_Post, verifying it is an |
| 122 |
* attachment. |
| 123 |
* |
| 124 |
* @return WP_Post|null Null when the attachment is gone or the ref is invalid. |
| 125 |
*/ |
| 126 |
private function attachment(): ?WP_Post { |
| 127 |
$id = (int) $this->ref; |
| 128 |
if ( $id <= 0 ) { |
| 129 |
return null; |
| 130 |
} |
| 131 |
$post = get_post( $id ); |
| 132 |
if ( ! $post instanceof WP_Post || 'attachment' !== $post->post_type ) { |
| 133 |
return null; |
| 134 |
} |
| 135 |
return $post; |
| 136 |
} |
| 137 |
} |
| 138 |
|