nonce }` for every action the * shell needs to keep alive past `nonce_life`. The set is * filterable so other native windows / third-party plugins can * extend it; the only requirement is that the action string match * whatever was passed to `wp_create_nonce()` at registration. * * @since 0.8.7 * * @return array Map of nonce-action => current nonce value. */ function desktop_mode_nonce_refresh_build_payload() { $actions = array( 'wp_rest', 'desktop-mode-plugins', 'updates', ); /** * Filter the set of nonce actions refreshed on every Heartbeat tick. * * Each entry must be a literal nonce action string (the same value * passed to `wp_create_nonce()` wherever the original was minted). * * @since 0.8.7 * * @param string[] $actions Default nonce actions. */ $actions = (array) apply_filters( 'desktop_mode_nonce_refresh_actions', $actions ); $payload = array(); foreach ( $actions as $action ) { if ( ! is_string( $action ) || $action === '' ) { continue; } $payload[ $action ] = wp_create_nonce( $action ); } return $payload; } /** * Heartbeat handler — attach the fresh nonce map to every tick * from a user who has Desktop Mode enabled. * * Gated on `desktop_mode_is_enabled()` (not just `is_user_logged_in()`) * so users on classic admin screens — editors on post-edit pages, * subscribers reading the front-end heartbeat — don't carry the * payload around. The shell's nonces only need refreshing for * users who actually run the shell. * * The cost is tiny when fired (three `wp_create_nonce()` calls, * all hot-cached inside a single request) — the gate is about * not shipping irrelevant data to non-shell users on every tick. * * @since 0.8.7 * * @param array $response Heartbeat response (filter return value). * @param array $data Client-sent payload. Unused here. * @return array */ function desktop_mode_nonce_refresh_heartbeat_received( $response, $data ) { unset( $data ); if ( ! is_array( $response ) ) { $response = array(); } if ( ! function_exists( 'desktop_mode_is_enabled' ) || ! desktop_mode_is_enabled() ) { return $response; } $response[ DESKTOP_MODE_NONCE_REFRESH_FIELD ] = desktop_mode_nonce_refresh_build_payload(); return $response; } add_filter( 'heartbeat_received', 'desktop_mode_nonce_refresh_heartbeat_received', 5, 2 );