PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.8.7
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.8.7
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 0.8.7 All 34 releases
desktop-mode / includes / toast-types.php

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

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