PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.6
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.6
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-desktop-mode-upload-file.php

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

110 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — `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 WPDesktopMode
11 * @since 0.9.6
12 */
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * @since 0.9.6
18 */
19 class Desktop_Mode_Upload_File extends Desktop_Mode_File {
20
21 public static function type(): string {
22 return 'upload';
23 }
24
25 public function exists(): bool {
26 return null !== $this->row();
27 }
28
29 public function title(): string {
30 $row = $this->row();
31 if ( ! $row ) {
32 return __( '(missing file)', 'desktop-mode' );
33 }
34 return '' !== (string) $row['display_name'] ? (string) $row['display_name'] : __( 'file', 'desktop-mode' );
35 }
36
37 public function icon(): string {
38 switch ( $this->kind() ) {
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 case 'pdf':
46 return 'dashicons-pdf';
47 case 'archive':
48 return 'dashicons-archive';
49 case 'text':
50 return 'dashicons-media-text';
51 default:
52 return 'dashicons-media-default';
53 }
54 }
55
56 public function can_read( int $user_id ): bool {
57 $row = $this->row();
58 if ( ! $row ) {
59 return false;
60 }
61 return desktop_mode_stored_file_user_can_read( (int) $row['id'], $user_id );
62 }
63
64 public function serialize(): array {
65 $shape = parent::serialize();
66 $row = $this->row();
67 $shape['ownerId'] = $row ? (int) $row['owner_id'] : 0;
68 $shape['sizeBytes'] = $row ? (int) $row['size_bytes'] : 0;
69 $shape['mime'] = $row ? (string) $row['mime'] : '';
70 $shape['kind'] = $this->kind();
71 return $shape;
72 }
73
74 /**
75 * Coarse mime-category slug used for the tile icon and by the
76 * JS type for rendering decisions.
77 *
78 * @return string image|video|audio|pdf|archive|text|file
79 */
80 private function kind(): string {
81 $row = $this->row();
82 $mime = $row ? strtolower( (string) $row['mime'] ) : '';
83 if ( '' === $mime ) {
84 return 'file';
85 }
86 $major = strtok( $mime, '/' );
87 if ( in_array( $major, array( 'image', 'video', 'audio' ), true ) ) {
88 return $major;
89 }
90 if ( 'application/pdf' === $mime ) {
91 return 'pdf';
92 }
93 if ( preg_match( '#^application/(zip|x-tar|x-gzip|gzip|x-7z-compressed|x-rar-compressed|x-bzip2?)$#', $mime ) ) {
94 return 'archive';
95 }
96 if ( 'text' === $major || in_array( $mime, array( 'application/json', 'application/xml' ), true ) ) {
97 return 'text';
98 }
99 return 'file';
100 }
101
102 private function row(): ?array {
103 $id = (int) $this->ref;
104 if ( $id <= 0 || ! function_exists( 'desktop_mode_stored_files_get' ) ) {
105 return null;
106 }
107 return desktop_mode_stored_files_get( $id );
108 }
109 }
110