| 1 |
<?php |
| 2 |
/** |
| 3 |
* Users — the profile facts both apps ship as their config extra. |
| 4 |
* |
| 5 |
* The Users app's Profile tab and the User Edit window host the same |
| 6 |
* `<os-user-profile>`, and both windows register on every request |
| 7 |
* that builds the app manifests; the catalogue work behind the facts |
| 8 |
* (a language directory scan, the admin colour-scheme registry, the |
| 9 |
* editable-roles walk) is done once per request per viewer here and |
| 10 |
* handed to both config callables. |
| 11 |
* |
| 12 |
* @package OpenStation |
| 13 |
*/ |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* The static facts `<os-user-profile>` and the Users list read |
| 19 |
* (`ctx.extra`), for the acting user. Memoised per request and per |
| 20 |
* viewer, so switching users mid-request (a test, a `switch_to_user`) |
| 21 |
* recomputes. |
| 22 |
* |
| 23 |
* @return array<string,mixed> |
| 24 |
*/ |
| 25 |
function openstation_users_profile_facts() { |
| 26 |
static $cache = array(); |
| 27 |
$viewer_id = (int) get_current_user_id(); |
| 28 |
// Neither app is registered for a visitor; skip the catalogue work. |
| 29 |
if ( $viewer_id <= 0 ) { |
| 30 |
return array(); |
| 31 |
} |
| 32 |
if ( isset( $cache[ $viewer_id ] ) ) { |
| 33 |
return $cache[ $viewer_id ]; |
| 34 |
} |
| 35 |
$cache[ $viewer_id ] = array( |
| 36 |
'currentUserId' => $viewer_id, |
| 37 |
// Capability flags — the UI hides actions the viewer can't |
| 38 |
// perform; every action and route re-checks, so a tampered flag |
| 39 |
// changes nothing. |
| 40 |
'canEdit' => current_user_can( 'edit_users' ), |
| 41 |
'canPromote' => current_user_can( 'promote_users' ), |
| 42 |
'canCreate' => current_user_can( 'create_users' ), |
| 43 |
'canDelete' => is_multisite() ? current_user_can( 'remove_users' ) : current_user_can( 'delete_users' ), |
| 44 |
'isMultisite' => is_multisite(), |
| 45 |
// Roles the viewer may assign — the dropdowns list only these, |
| 46 |
// so a narrowed `editable_roles` never yields "pick a role, hit |
| 47 |
// save, get rejected". `allRoles` is the label catalogue. |
| 48 |
'assignableRoles' => openstation_users_window_role_label_map( $viewer_id ), |
| 49 |
'allRoles' => openstation_users_window_all_roles_map(), |
| 50 |
'locales' => openstation_users_window_locales_map(), |
| 51 |
'defaultRole' => (string) get_option( 'default_role', 'subscriber' ), |
| 52 |
'contactMethods' => (array) wp_get_user_contact_methods(), |
| 53 |
'colorSchemes' => openstation_user_edit_window_color_schemes(), |
| 54 |
); |
| 55 |
return $cache[ $viewer_id ]; |
| 56 |
} |
| 57 |
|