PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.0.1
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.0.1
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
desktop-mode / includes / desktop-files / types / class-openstation-attachment-file.php

class-openstation-attachment-file.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.0.1, at includes/desktop-files/types/class-openstation-attachment-file.php

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