| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — `attachment` file type. |
| 4 |
* |
| 5 |
* @package WPDesktopMode |
| 6 |
* @since 0.9.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 0.9.0 |
| 13 |
*/ |
| 14 |
class Desktop_Mode_Attachment_File extends Desktop_Mode_File { |
| 15 |
|
| 16 |
public static function type(): string { |
| 17 |
return 'attachment'; |
| 18 |
} |
| 19 |
|
| 20 |
public function exists(): bool { |
| 21 |
return $this->attachment() instanceof WP_Post; |
| 22 |
} |
| 23 |
|
| 24 |
public function title(): string { |
| 25 |
$post = $this->attachment(); |
| 26 |
if ( ! $post ) { |
| 27 |
return __( '(missing media)', 'desktop-mode' ); |
| 28 |
} |
| 29 |
$title = get_the_title( $post ); |
| 30 |
return wp_strip_all_tags( '' !== $title ? $title : basename( (string) get_attached_file( $post->ID ) ) ); |
| 31 |
} |
| 32 |
|
| 33 |
public function icon(): string { |
| 34 |
$post = $this->attachment(); |
| 35 |
if ( ! $post ) { |
| 36 |
return 'dashicons-warning'; |
| 37 |
} |
| 38 |
$type = strtok( (string) $post->post_mime_type, '/' ); |
| 39 |
switch ( $type ) { |
| 40 |
case 'image': |
| 41 |
return 'dashicons-format-image'; |
| 42 |
case 'video': |
| 43 |
return 'dashicons-format-video'; |
| 44 |
case 'audio': |
| 45 |
return 'dashicons-format-audio'; |
| 46 |
default: |
| 47 |
return 'dashicons-media-default'; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public function preview_url(): string { |
| 52 |
$post = $this->attachment(); |
| 53 |
if ( ! $post ) { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
$src = wp_get_attachment_image_src( $post->ID, 'thumbnail' ); |
| 57 |
return is_array( $src ) ? (string) $src[0] : ''; |
| 58 |
} |
| 59 |
|
| 60 |
public function can_read( int $user_id ): bool { |
| 61 |
$post = $this->attachment(); |
| 62 |
if ( ! $post ) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
return user_can( $user_id, 'read_post', $post->ID ); |
| 66 |
} |
| 67 |
|
| 68 |
public function serialize(): array { |
| 69 |
$shape = parent::serialize(); |
| 70 |
$post = $this->attachment(); |
| 71 |
$shape['mime'] = $post ? (string) $post->post_mime_type : ''; |
| 72 |
// Full-size source URL + alt text — surfaced so cross-frame |
| 73 |
// drag handlers can build `core/image` / `core/video` / |
| 74 |
// `core/audio` / `core/file` blocks on drop into Gutenberg |
| 75 |
// without a REST roundtrip. `previewUrl` is a thumbnail and |
| 76 |
// not the right attribute for the inserted block. |
| 77 |
$shape['sourceUrl'] = $post ? (string) wp_get_attachment_url( $post->ID ) : ''; |
| 78 |
$shape['alt'] = $post ? (string) get_post_meta( $post->ID, '_wp_attachment_image_alt', true ) : ''; |
| 79 |
return $shape; |
| 80 |
} |
| 81 |
|
| 82 |
private function attachment(): ?WP_Post { |
| 83 |
$id = (int) $this->ref; |
| 84 |
if ( $id <= 0 ) { |
| 85 |
return null; |
| 86 |
} |
| 87 |
$post = get_post( $id ); |
| 88 |
if ( ! $post instanceof WP_Post || 'attachment' !== $post->post_type ) { |
| 89 |
return null; |
| 90 |
} |
| 91 |
return $post; |
| 92 |
} |
| 93 |
} |
| 94 |
|