| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — `folder` file type. |
| 4 |
* |
| 5 |
* Folders are first-class files. The reference is the folder's |
| 6 |
* row id in `{$wpdb->prefix}desktop_mode_folders`. When the row |
| 7 |
* is missing, title() falls back to a generic "Folder" label and |
| 8 |
* `exists()` returns false. |
| 9 |
* |
| 10 |
* @package OpenStation |
| 11 |
*/ |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* The `folder` desktop file type. |
| 17 |
*/ |
| 18 |
class OpenStation_Folder_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 'folder'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Whether the folder row still exists in the database. |
| 31 |
* |
| 32 |
* @return bool |
| 33 |
*/ |
| 34 |
public function exists(): bool { |
| 35 |
return null !== $this->folder(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get the folder name, falling back to a generic label. |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public function title(): string { |
| 44 |
$row = $this->folder(); |
| 45 |
if ( ! $row ) { |
| 46 |
return __( 'Folder', 'desktop-mode' ); |
| 47 |
} |
| 48 |
return '' !== (string) $row['name'] ? (string) $row['name'] : __( 'Folder', 'desktop-mode' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get the Dashicon class for folder tiles. |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public function icon(): string { |
| 57 |
return 'dashicons-portfolio'; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Check whether the user owns the folder, has a direct share, |
| 62 |
* or reaches it via cascade from a parent folder. |
| 63 |
* |
| 64 |
* @param int $user_id |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public function can_read( int $user_id ): bool { |
| 68 |
$row = $this->folder(); |
| 69 |
if ( ! $row ) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
if ( (int) $row['owner_id'] === (int) $user_id ) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
// The capability resolver is the authority — |
| 76 |
// it knows about direct shares, role decisions, AND cascade |
| 77 |
// (a folder nested inside a shared folder is reachable). |
| 78 |
if ( function_exists( 'openstation_folder_share_user_capability' ) ) { |
| 79 |
$cap = openstation_folder_share_user_capability( (int) $row['id'], (int) $user_id ); |
| 80 |
if ( 'none' !== $cap ) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
} |
| 84 |
// Back-compat fallback for legacy `share_meta` rows that |
| 85 |
// pre-date the shares table. |
| 86 |
$visible_ids = wp_list_pluck( openstation_files_get_visible_folders( $user_id ), 'id' ); |
| 87 |
return in_array( (int) $row['id'], array_map( 'intval', (array) $visible_ids ), true ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Augment the base serialized shape with owner id, share mode and |
| 92 |
* the share summary. |
| 93 |
* |
| 94 |
* `shareSummary` is the same shape the folder response carries |
| 95 |
* (see {@see openstation_files_folder_share_summary()}) and it |
| 96 |
* belongs here for one reason: a folder on the desktop is a |
| 97 |
* PLACEMENT, and a placement's `file` is whatever this method |
| 98 |
* returns. Without it the shared badge had nothing to read — the |
| 99 |
* summary existed only on the separate folder response, which the |
| 100 |
* tile renderer never sees — so an accepted share was invisible |
| 101 |
* to owner and recipient alike. |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
public function serialize(): array { |
| 106 |
$shape = parent::serialize(); |
| 107 |
$row = $this->folder(); |
| 108 |
$shape['ownerId'] = $row ? (int) $row['owner_id'] : 0; |
| 109 |
$shape['shareMode'] = $row ? (string) $row['share_mode'] : 'private'; |
| 110 |
if ( $row && function_exists( 'openstation_files_folder_share_summary' ) ) { |
| 111 |
$shape['shareSummary'] = openstation_files_folder_share_summary( $row ); |
| 112 |
} |
| 113 |
return $shape; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Lazily resolve the folder row from the stored ref. |
| 118 |
* |
| 119 |
* @return array|null Null when the folder is gone or the ref is invalid. |
| 120 |
*/ |
| 121 |
private function folder(): ?array { |
| 122 |
$id = (int) $this->ref; |
| 123 |
if ( $id <= 0 ) { |
| 124 |
return null; |
| 125 |
} |
| 126 |
if ( ! function_exists( 'openstation_files_get_folder' ) ) { |
| 127 |
return null; |
| 128 |
} |
| 129 |
return openstation_files_get_folder( $id ); |
| 130 |
} |
| 131 |
} |
| 132 |
|