| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — `user` file type. |
| 4 |
* |
| 5 |
* @package OpenStation |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* The `user` desktop file type. |
| 12 |
*/ |
| 13 |
class OpenStation_User_File extends OpenStation_File { |
| 14 |
|
| 15 |
public static function type(): string { |
| 16 |
return 'user'; |
| 17 |
} |
| 18 |
|
| 19 |
public function exists(): bool { |
| 20 |
return $this->user() instanceof WP_User; |
| 21 |
} |
| 22 |
|
| 23 |
public function title(): string { |
| 24 |
$user = $this->user(); |
| 25 |
if ( ! $user ) { |
| 26 |
return __( '(missing user)', 'desktop-mode' ); |
| 27 |
} |
| 28 |
return (string) $user->display_name; |
| 29 |
} |
| 30 |
|
| 31 |
public function icon(): string { |
| 32 |
return 'dashicons-admin-users'; |
| 33 |
} |
| 34 |
|
| 35 |
public function preview_url(): string { |
| 36 |
$user = $this->user(); |
| 37 |
if ( ! $user ) { |
| 38 |
return ''; |
| 39 |
} |
| 40 |
$avatar = get_avatar_url( $user->ID, array( 'size' => 96 ) ); |
| 41 |
return is_string( $avatar ) ? $avatar : ''; |
| 42 |
} |
| 43 |
|
| 44 |
public function can_read( int $user_id ): bool { |
| 45 |
// Reading the existence of another user is gated on |
| 46 |
// `list_users` to avoid leaking the user roster via shared |
| 47 |
// folders. Tighter than `read` (every logged-in user has |
| 48 |
// `read`) but looser than `edit_users`. |
| 49 |
return user_can( $user_id, 'list_users' ); |
| 50 |
} |
| 51 |
|
| 52 |
public function serialize(): array { |
| 53 |
$shape = parent::serialize(); |
| 54 |
$user = $this->user(); |
| 55 |
$shape['roles'] = $user ? array_values( (array) $user->roles ) : array(); |
| 56 |
// Author archive URL — surfaced so cross-frame drag handlers |
| 57 |
// can build a `<a href>` to the author's posts on drop into |
| 58 |
// Gutenberg without a REST roundtrip. |
| 59 |
$shape['link'] = $user ? (string) get_author_posts_url( (int) $user->ID ) : ''; |
| 60 |
|
| 61 |
// OpenStation agent surface. `isAgent` marks the tile; the |
| 62 |
// drag-trigger entity kinds ship inline so the tile drop |
| 63 |
// handler can gate synchronously without an agents REST |
| 64 |
// roundtrip: null = no drag trigger configured (tile rejects |
| 65 |
// drops), [] = drag trigger with no filter (accepts every |
| 66 |
// entity kind). |
| 67 |
// |
| 68 |
// The two guards test DIFFERENT things and both are load- |
| 69 |
// bearing. `openstation_agent_is_agent()` lives in the agents |
| 70 |
// guard, which loads unconditionally — being an agent is a |
| 71 |
// property of the user row, and the row survives the feature |
| 72 |
// being switched off. The definition getters live in store.php, |
| 73 |
// which does NOT load then. Turning the `agents` extended |
| 74 |
// option off while an agent tile sits on someone's desktop |
| 75 |
// otherwise fatals the whole admin on the next boot payload. |
| 76 |
if ( |
| 77 |
$user |
| 78 |
&& function_exists( 'openstation_agent_is_agent' ) |
| 79 |
&& openstation_agent_is_agent( $user->ID ) |
| 80 |
) { |
| 81 |
$shape['isAgent'] = true; |
| 82 |
|
| 83 |
// Named one by one rather than probing a single getter as |
| 84 |
// a proxy for "store.php loaded": the proxy reads as an |
| 85 |
// unguarded call at every other site, and this is a rule a |
| 86 |
// test has to be able to check mechanically. |
| 87 |
$has_definition = function_exists( 'openstation_agent_get_description' ) |
| 88 |
&& function_exists( 'openstation_agent_get_triggers' ); |
| 89 |
// The "when to use" line — the chat window's subtitle when |
| 90 |
// the tile opener starts a conversation. |
| 91 |
$shape['agentDescription'] = $has_definition |
| 92 |
? openstation_agent_get_description( (int) $user->ID ) |
| 93 |
: ''; |
| 94 |
|
| 95 |
// Null while the framework is off: the tile still reads as |
| 96 |
// an agent, but rejects every drop, because nothing can run |
| 97 |
// to receive it. |
| 98 |
$drag_kinds = null; |
| 99 |
$triggers = $has_definition |
| 100 |
? openstation_agent_get_triggers( (int) $user->ID ) |
| 101 |
: array(); |
| 102 |
foreach ( $triggers as $trigger ) { |
| 103 |
if ( 'drag' !== ( isset( $trigger['kind'] ) ? $trigger['kind'] : '' ) ) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
$kinds = isset( $trigger['config']['entityKinds'] ) && is_array( $trigger['config']['entityKinds'] ) |
| 107 |
? $trigger['config']['entityKinds'] |
| 108 |
: array(); |
| 109 |
$drag_kinds = array_values( array_map( 'strval', $kinds ) ); |
| 110 |
break; |
| 111 |
} |
| 112 |
$shape['agentDragKinds'] = $drag_kinds; |
| 113 |
} |
| 114 |
|
| 115 |
return $shape; |
| 116 |
} |
| 117 |
|
| 118 |
private function user(): ?WP_User { |
| 119 |
$id = (int) $this->ref; |
| 120 |
if ( $id <= 0 ) { |
| 121 |
return null; |
| 122 |
} |
| 123 |
$user = get_userdata( $id ); |
| 124 |
return $user instanceof WP_User ? $user : null; |
| 125 |
} |
| 126 |
} |
| 127 |
|