| 1 |
<?php |
| 2 |
/** |
| 3 |
* Users — the `<os-user-profile>` companion bundle. |
| 4 |
* |
| 5 |
* The profile surface (`apps/users/profile/`) is one bundle, |
| 6 |
* `assets/js/apps/user-profile[.min].js`, registered under the handle |
| 7 |
* `openstation-user-profile` and loaded as a first-open companion of |
| 8 |
* BOTH windows that host the element — the Users app (its Profile |
| 9 |
* tab) and the User Edit app — instead of a copy compiled into each |
| 10 |
* app's client view. The element takes its facts, its REST access |
| 11 |
* and its toast from properties the hosting app sets from |
| 12 |
* `updated()`, so the bundle carries no config of its own. |
| 13 |
* |
| 14 |
* @package OpenStation |
| 15 |
*/ |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
/** The companion script handle. */ |
| 20 |
const OPENSTATION_USER_PROFILE_HANDLE = 'openstation-user-profile'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Register the bundle. Hooked after the framework has loaded the apps |
| 24 |
* (the parts load DURING `init` @10, so a hook at 10 would never fire). |
| 25 |
*/ |
| 26 |
function openstation_users_profile_register_script() { |
| 27 |
$suffix = openstation_asset_suffix(); |
| 28 |
$path = OPENSTATION_DIR . 'assets/js/apps/user-profile' . $suffix . '.js'; |
| 29 |
wp_register_script( |
| 30 |
OPENSTATION_USER_PROFILE_HANDLE, |
| 31 |
OPENSTATION_URL . 'assets/js/apps/user-profile' . $suffix . '.js', |
| 32 |
array( 'wp-i18n' ), |
| 33 |
file_exists( $path ) ? (string) filemtime( $path ) : OPENSTATION_VERSION, |
| 34 |
true |
| 35 |
); |
| 36 |
wp_set_script_translations( OPENSTATION_USER_PROFILE_HANDLE, 'desktop-mode', OPENSTATION_DIR . 'languages' ); |
| 37 |
} |
| 38 |
add_action( 'init', 'openstation_users_profile_register_script', 11 ); |
| 39 |
|
| 40 |
/** |
| 41 |
* Ride the two app windows: append the bundle to their companion |
| 42 |
* scripts, so it is in the tab before the runtime mounts either. |
| 43 |
* |
| 44 |
* @param array<string,mixed> $window_args `openstation_register_window()` args. |
| 45 |
* @param string $app_id App id. |
| 46 |
* @return array<string,mixed> |
| 47 |
*/ |
| 48 |
function openstation_users_profile_window_args( $window_args, $app_id ) { |
| 49 |
if ( ! is_array( $window_args ) || ! in_array( (string) $app_id, array( 'desktop-mode-users', 'desktop-mode-user-edit' ), true ) ) { |
| 50 |
return $window_args; |
| 51 |
} |
| 52 |
// The handle registers on `init`; a caller that rebuilt `$wp_scripts` |
| 53 |
// since (a script manager, a test) would otherwise hand the window |
| 54 |
// a handle nothing resolves. |
| 55 |
if ( ! wp_script_is( OPENSTATION_USER_PROFILE_HANDLE, 'registered' ) ) { |
| 56 |
openstation_users_profile_register_script(); |
| 57 |
} |
| 58 |
$scripts = isset( $window_args['scripts'] ) ? (array) $window_args['scripts'] : array(); |
| 59 |
if ( ! in_array( OPENSTATION_USER_PROFILE_HANDLE, $scripts, true ) ) { |
| 60 |
$scripts[] = OPENSTATION_USER_PROFILE_HANDLE; |
| 61 |
} |
| 62 |
$window_args['scripts'] = $scripts; |
| 63 |
return $window_args; |
| 64 |
} |
| 65 |
add_filter( 'openstation_app_window_args', 'openstation_users_profile_window_args', 10, 2 ); |
| 66 |
|