PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.10
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.10
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-upload-file.php

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

149 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation — `upload` file type.
4 *
5 * A real uploaded file with bytes on the server. The reference is
6 * the row id in `{$wpdb->prefix}desktop_mode_stored_files`. Unlike
7 * every other built-in type, the placement OWNS the entity — see
8 * the deletion contract in `stored-files-store.php`.
9 *
10 * @package OpenStation
11 */
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * The `upload` desktop file type.
17 */
18 class OpenStation_Upload_File extends OpenStation_File {
19
20 /**
21 * Get the file type identifier.
22 *
23 * @return string
24 */
25 public static function type(): string {
26 return 'upload';
27 }
28
29 /**
30 * Whether the stored-file row still exists in the database.
31 *
32 * @return bool
33 */
34 public function exists(): bool {
35 return null !== $this->row();
36 }
37
38 /**
39 * Get the display name, falling back to a generic label.
40 *
41 * @return string
42 */
43 public function title(): string {
44 $row = $this->row();
45 if ( ! $row ) {
46 return __( '(missing file)', 'desktop-mode' );
47 }
48 return '' !== (string) $row['display_name'] ? (string) $row['display_name'] : __( 'file', 'desktop-mode' );
49 }
50
51 /**
52 * Resolve a Dashicon class based on the coarse mime category.
53 *
54 * @return string
55 */
56 public function icon(): string {
57 switch ( $this->kind() ) {
58 case 'image':
59 return 'dashicons-format-image';
60 case 'video':
61 return 'dashicons-format-video';
62 case 'audio':
63 return 'dashicons-format-audio';
64 case 'pdf':
65 return 'dashicons-pdf';
66 case 'archive':
67 return 'dashicons-archive';
68 case 'text':
69 return 'dashicons-media-text';
70 default:
71 return 'dashicons-media-default';
72 }
73 }
74
75 /**
76 * Delegate to the stored-file capability resolver.
77 *
78 * @param int $user_id
79 * @return bool
80 */
81 public function can_read( int $user_id ): bool {
82 $row = $this->row();
83 if ( ! $row ) {
84 return false;
85 }
86 return openstation_stored_file_user_can_read( (int) $row['id'], $user_id );
87 }
88
89 /**
90 * Augment the base serialized shape with owner id, size, mime,
91 * kind slug, and whether the Media Library would accept the file.
92 *
93 * @return array
94 */
95 public function serialize(): array {
96 $shape = parent::serialize();
97 $row = $this->row();
98 $shape['ownerId'] = $row ? (int) $row['owner_id'] : 0;
99 $shape['sizeBytes'] = $row ? (int) $row['size_bytes'] : 0;
100 $shape['mime'] = $row ? (string) $row['mime'] : '';
101 $shape['kind'] = $this->kind();
102 $shape['isMedia'] = $row && function_exists( 'openstation_stored_file_is_media' )
103 ? openstation_stored_file_is_media( $row )
104 : false;
105 return $shape;
106 }
107
108 /**
109 * Coarse mime-category slug used for the tile icon and by the
110 * JS type for rendering decisions.
111 *
112 * @return string image|video|audio|pdf|archive|text|file
113 */
114 private function kind(): string {
115 $row = $this->row();
116 $mime = $row ? strtolower( (string) $row['mime'] ) : '';
117 if ( '' === $mime ) {
118 return 'file';
119 }
120 $major = strtok( $mime, '/' );
121 if ( in_array( $major, array( 'image', 'video', 'audio' ), true ) ) {
122 return $major;
123 }
124 if ( 'application/pdf' === $mime ) {
125 return 'pdf';
126 }
127 if ( preg_match( '#^application/(zip|x-tar|x-gzip|gzip|x-7z-compressed|x-rar-compressed|x-bzip2?)$#', $mime ) ) {
128 return 'archive';
129 }
130 if ( 'text' === $major || in_array( $mime, array( 'application/json', 'application/xml' ), true ) ) {
131 return 'text';
132 }
133 return 'file';
134 }
135
136 /**
137 * Lazily resolve the stored-file row from the database.
138 *
139 * @return array|null Null when the row is gone or the ref is invalid.
140 */
141 private function row(): ?array {
142 $id = (int) $this->ref;
143 if ( $id <= 0 || ! function_exists( 'openstation_stored_files_get' ) ) {
144 return null;
145 }
146 return openstation_stored_files_get( $id );
147 }
148 }
149