PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.2
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.2
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.2, at includes/desktop-files/types/class-openstation-upload-file.php

146 lines 3.5 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 * and kind slug.
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 return $shape;
103 }
104
105 /**
106 * Coarse mime-category slug used for the tile icon and by the
107 * JS type for rendering decisions.
108 *
109 * @return string image|video|audio|pdf|archive|text|file
110 */
111 private function kind(): string {
112 $row = $this->row();
113 $mime = $row ? strtolower( (string) $row['mime'] ) : '';
114 if ( '' === $mime ) {
115 return 'file';
116 }
117 $major = strtok( $mime, '/' );
118 if ( in_array( $major, array( 'image', 'video', 'audio' ), true ) ) {
119 return $major;
120 }
121 if ( 'application/pdf' === $mime ) {
122 return 'pdf';
123 }
124 if ( preg_match( '#^application/(zip|x-tar|x-gzip|gzip|x-7z-compressed|x-rar-compressed|x-bzip2?)$#', $mime ) ) {
125 return 'archive';
126 }
127 if ( 'text' === $major || in_array( $mime, array( 'application/json', 'application/xml' ), true ) ) {
128 return 'text';
129 }
130 return 'file';
131 }
132
133 /**
134 * Lazily resolve the stored-file row from the database.
135 *
136 * @return array|null Null when the row is gone or the ref is invalid.
137 */
138 private function row(): ?array {
139 $id = (int) $this->ref;
140 if ( $id <= 0 || ! function_exists( 'openstation_stored_files_get' ) ) {
141 return null;
142 }
143 return openstation_stored_files_get( $id );
144 }
145 }
146