PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.11
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.11
1.1.11 1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 All 35 releases
desktop-mode / includes / toast-types.php

toast-types.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.1.11, at includes/toast-types.php

117 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation — Toast notification type registry.
4 *
5 * The shell surfaces user-visible toast notifications (save succeeded,
6 * upload failed, a plugin crashed, …). Each toast carries a `type`
7 * slug that the shell maps to a color + icon. The defaults cover
8 * `success`, `warning`, `error`, and `shell-error`; plugins and themes
9 * extend the list via the {@see 'openstation_toast_types'} filter to
10 * register their own slugs (e.g. `update-available`).
11 *
12 * @package OpenStation
13 */
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Returns the map of toast-notification type slugs shipped to the shell.
19 *
20 * Each entry is an array shaped `{ id, label, icon, tone }`:
21 *
22 * - `id` is a stable slug plugins call with `wp.os.toast( id, … )`.
23 * - `label` is the default aria-label used when a toast omits one.
24 * - `icon` is a Dashicons class rendered alongside the message.
25 * - `tone` is one of `positive | warning | critical | neutral` — the
26 * shell maps this to its color palette.
27 *
28 * @return array<int, array{id: string, label: string, icon: string, tone: string}>
29 */
30 function openstation_get_toast_types() {
31 // The shell paints a toast from `tone` alone; `label` and `icon` ride
32 // along for plugins that list or render the types themselves.
33 $defaults = array(
34 array(
35 'id' => 'success',
36 'label' => __( 'Success', 'desktop-mode' ),
37 'icon' => 'dashicons-yes-alt',
38 'tone' => 'positive',
39 ),
40 array(
41 'id' => 'warning',
42 'label' => __( 'Warning', 'desktop-mode' ),
43 'icon' => 'dashicons-warning',
44 'tone' => 'warning',
45 ),
46 array(
47 'id' => 'error',
48 'label' => __( 'Error', 'desktop-mode' ),
49 'icon' => 'dashicons-dismiss',
50 'tone' => 'critical',
51 ),
52 array(
53 'id' => 'shell-error',
54 'label' => __( 'Shell error', 'desktop-mode' ),
55 'icon' => 'dashicons-bug',
56 'tone' => 'critical',
57 ),
58 );
59
60 $allowed_tones = array( 'positive', 'warning', 'critical', 'neutral' );
61
62 /**
63 * Filters the toast-notification type map.
64 *
65 * ```php
66 * add_filter( 'openstation_toast_types', function ( $types ) {
67 * $types[] = array(
68 * 'id' => 'update-available',
69 * 'label' => __( 'Update available', 'my-plugin' ),
70 * 'icon' => 'dashicons-update',
71 * 'tone' => 'neutral',
72 * );
73 * return $types;
74 * } );
75 * ```
76 *
77 * Non-array returns fall back to defaults. Entries whose `id` is
78 * empty or whose `tone` is not one of `positive|warning|critical|
79 * neutral` are dropped. An `icon` that doesn't survive
80 * `sanitize_html_class()` falls back to `dashicons-info`; a missing
81 * `label` falls back to the ucfirst'd `id`.
82 *
83 * @param array $defaults Built-in toast types.
84 */
85 $filtered = apply_filters( 'openstation_toast_types', $defaults );
86 if ( ! is_array( $filtered ) ) {
87 return $defaults;
88 }
89
90 $clean = array();
91 $seen = array();
92 foreach ( $filtered as $entry ) {
93 if ( ! is_array( $entry ) ) {
94 continue;
95 }
96 $id = isset( $entry['id'] ) ? sanitize_key( (string) $entry['id'] ) : '';
97 $label = isset( $entry['label'] ) ? wp_strip_all_tags( (string) $entry['label'] ) : '';
98 $icon = isset( $entry['icon'] ) ? sanitize_html_class( (string) $entry['icon'] ) : '';
99 $tone = isset( $entry['tone'] ) ? sanitize_key( (string) $entry['tone'] ) : '';
100 if ( '' === $id || '' === $tone || ! in_array( $tone, $allowed_tones, true ) ) {
101 continue;
102 }
103 if ( isset( $seen[ $id ] ) ) {
104 continue;
105 }
106 $seen[ $id ] = true;
107 $clean[] = array(
108 'id' => $id,
109 'label' => '' !== $label ? $label : ucfirst( $id ),
110 'icon' => '' !== $icon ? $icon : 'dashicons-info',
111 'tone' => $tone,
112 );
113 }
114
115 return empty( $clean ) ? $defaults : $clean;
116 }
117