| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — `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` (added in |
| 7 |
* Phase 2). Until that table lands, instantiating a folder file |
| 8 |
* is harmless: title falls back to a constructor-provided label |
| 9 |
* stored on the placement, and `exists()` is gated on the |
| 10 |
* presence of the folder row. |
| 11 |
* |
| 12 |
* @package WPDesktopMode |
| 13 |
* @since 0.9.0 |
| 14 |
*/ |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
/** |
| 19 |
* @since 0.9.0 |
| 20 |
*/ |
| 21 |
class Desktop_Mode_Folder_File extends Desktop_Mode_File { |
| 22 |
|
| 23 |
public static function type(): string { |
| 24 |
return 'folder'; |
| 25 |
} |
| 26 |
|
| 27 |
public function exists(): bool { |
| 28 |
return null !== $this->folder(); |
| 29 |
} |
| 30 |
|
| 31 |
public function title(): string { |
| 32 |
$row = $this->folder(); |
| 33 |
if ( ! $row ) { |
| 34 |
return __( 'Folder', 'desktop-mode' ); |
| 35 |
} |
| 36 |
return '' !== (string) $row['name'] ? (string) $row['name'] : __( 'Folder', 'desktop-mode' ); |
| 37 |
} |
| 38 |
|
| 39 |
public function icon(): string { |
| 40 |
return 'dashicons-portfolio'; |
| 41 |
} |
| 42 |
|
| 43 |
public function can_read( int $user_id ): bool { |
| 44 |
$row = $this->folder(); |
| 45 |
if ( ! $row ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
if ( (int) $row['owner_id'] === (int) $user_id ) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
// Since 0.18.0 the capability resolver is the authority — |
| 52 |
// it knows about direct shares, role decisions, AND cascade |
| 53 |
// (a folder nested inside a shared folder is reachable). |
| 54 |
if ( function_exists( 'desktop_mode_folder_share_user_capability' ) ) { |
| 55 |
$cap = desktop_mode_folder_share_user_capability( (int) $row['id'], (int) $user_id ); |
| 56 |
if ( 'none' !== $cap ) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
} |
| 60 |
// Back-compat fallback for legacy `share_meta` rows that |
| 61 |
// pre-date the shares table. |
| 62 |
$visible_ids = wp_list_pluck( desktop_mode_files_get_visible_folders( $user_id ), 'id' ); |
| 63 |
return in_array( (int) $row['id'], array_map( 'intval', (array) $visible_ids ), true ); |
| 64 |
} |
| 65 |
|
| 66 |
public function serialize(): array { |
| 67 |
$shape = parent::serialize(); |
| 68 |
$row = $this->folder(); |
| 69 |
$shape['ownerId'] = $row ? (int) $row['owner_id'] : 0; |
| 70 |
$shape['shareMode'] = $row ? (string) $row['share_mode'] : 'private'; |
| 71 |
return $shape; |
| 72 |
} |
| 73 |
|
| 74 |
private function folder(): ?array { |
| 75 |
$id = (int) $this->ref; |
| 76 |
if ( $id <= 0 ) { |
| 77 |
return null; |
| 78 |
} |
| 79 |
if ( ! function_exists( 'desktop_mode_files_get_folder' ) ) { |
| 80 |
return null; |
| 81 |
} |
| 82 |
return desktop_mode_files_get_folder( $id ); |
| 83 |
} |
| 84 |
} |
| 85 |
|