| 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 |
|