` markup.
*/
function openstation_games_icon_svg() {
return '';
}
/**
* Whether the current user can use desktop games.
*
* @return bool
*/
function openstation_games_user_can_use() {
$can = is_user_logged_in() && current_user_can( 'read' );
/**
* Filter whether the current user can see the Games window,
* icon, and play games.
*
* @param bool $can Default: logged-in + `read` capability.
*/
return (bool) apply_filters( 'openstation_games_user_can_use', $can );
}
/**
* Echoes the Games window's template body.
*
* The `data-os-games-*` hooks are the contract the JS
* render callback relies on — keep them intact (or rename via the
* filter) when customizing the layout.
*/
function openstation_games_render_template() {
ob_start();
?>
__( 'Games', 'desktop-mode' ),
'icon' => $icon_uri,
'template' => 'openstation_games_render_template',
'script' => 'desktop-mode-games',
// Companion styles load with the games bundle on first open.
// Every game window (`os-game-`) is opened by
// `launchGame()`, which lives in that bundle — so a sheet
// riding it here is guaranteed in the tab before any game
// paints. Built-in games append their own via the
// `openstation_games_window_args` filter below.
'styles' => array( 'desktop-mode-games' ),
'width' => 900,
'height' => 600,
'min_width' => 560,
'min_height' => 400,
// No dock tile from the window registration: the desktop icon
// below is this app's one launcher. Registering both used to
// mint two entries for one app, each with its own default, and
// the dock painted a tile while Preferences said "On the
// desktop". The navigation model collapses them either way —
// this keeps Games matching how My WordPress and Corkboard
// already register.
'placement' => 'none',
);
/**
* Filter the args used to register the Games native window.
*
* @param array $window_args Args passed to `openstation_register_window()`.
*/
$window_args = (array) apply_filters( 'openstation_games_window_args', $window_args );
$registered = openstation_register_window( 'desktop-mode-games', $window_args );
if ( is_wp_error( $registered ) ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( '[openstation] Games window registration failed: ' . $registered->get_error_message() );
return;
}
$icon_args = array(
'title' => __( 'Games', 'desktop-mode' ),
'icon_svg' => openstation_games_icon_svg(),
'window' => 'desktop-mode-games',
'position' => 85,
);
/**
* Filter the args used to register the Games desktop icon.
*
* @param array $icon_args Args passed to `openstation_register_icon()`.
*/
$icon_args = (array) apply_filters( 'openstation_games_icon_args', $icon_args );
openstation_register_icon( 'desktop-mode-games', $icon_args );
}
add_action( 'init', 'openstation_games_register_window', 20 );
/**
* Localize REST endpoints for the JS bundle.
*
* The bundle reads its config off `window.openStationGamesConfig`
* and never hardcodes URLs.
*/
function openstation_games_localize_config() {
if ( ! openstation_games_user_can_use() ) {
return;
}
wp_localize_script(
'desktop-mode-games',
'openStationGamesConfig',
array(
'restNonce' => wp_create_nonce( 'wp_rest' ),
'gamesUrlBase' => esc_url_raw( rest_url( 'desktop-mode/v1/games' ) ),
'challengesUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/games/challenges' ) ),
'usersSearchUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/games/users/search' ) ),
)
);
}
// Priority 5 for the same reason as the recycle bin: the lazy-load payload is
// built at priority 10, and localize data attached after that never reaches a
// bundle that loads on window open.
add_action( 'admin_enqueue_scripts', 'openstation_games_localize_config', 5 );