| 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 |
$defaults = array( |
| 32 |
array( |
| 33 |
'id' => 'success', |
| 34 |
'label' => __( 'Success', 'desktop-mode' ), |
| 35 |
'icon' => 'dashicons-yes-alt', |
| 36 |
'tone' => 'positive', |
| 37 |
), |
| 38 |
array( |
| 39 |
'id' => 'warning', |
| 40 |
'label' => __( 'Warning', 'desktop-mode' ), |
| 41 |
'icon' => 'dashicons-warning', |
| 42 |
'tone' => 'warning', |
| 43 |
), |
| 44 |
array( |
| 45 |
'id' => 'error', |
| 46 |
'label' => __( 'Error', 'desktop-mode' ), |
| 47 |
'icon' => 'dashicons-dismiss', |
| 48 |
'tone' => 'critical', |
| 49 |
), |
| 50 |
array( |
| 51 |
'id' => 'shell-error', |
| 52 |
'label' => __( 'Shell error', 'desktop-mode' ), |
| 53 |
'icon' => 'dashicons-bug', |
| 54 |
'tone' => 'critical', |
| 55 |
), |
| 56 |
); |
| 57 |
|
| 58 |
$allowed_tones = array( 'positive', 'warning', 'critical', 'neutral' ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Filters the toast-notification type map. |
| 62 |
* |
| 63 |
* ```php |
| 64 |
* add_filter( 'openstation_toast_types', function ( $types ) { |
| 65 |
* $types[] = array( |
| 66 |
* 'id' => 'update-available', |
| 67 |
* 'label' => __( 'Update available', 'my-plugin' ), |
| 68 |
* 'icon' => 'dashicons-update', |
| 69 |
* 'tone' => 'neutral', |
| 70 |
* ); |
| 71 |
* return $types; |
| 72 |
* } ); |
| 73 |
* ``` |
| 74 |
* |
| 75 |
* Non-array returns fall back to defaults. Entries whose `id` is |
| 76 |
* empty or whose `tone` is not one of `positive|warning|critical| |
| 77 |
* neutral` are dropped. An `icon` that doesn't survive |
| 78 |
* `sanitize_html_class()` falls back to `dashicons-info`; a missing |
| 79 |
* `label` falls back to the ucfirst'd `id`. |
| 80 |
* |
| 81 |
* @param array $defaults Built-in toast types. |
| 82 |
*/ |
| 83 |
$filtered = apply_filters( 'openstation_toast_types', $defaults ); |
| 84 |
if ( ! is_array( $filtered ) ) { |
| 85 |
return $defaults; |
| 86 |
} |
| 87 |
|
| 88 |
$clean = array(); |
| 89 |
$seen = array(); |
| 90 |
foreach ( $filtered as $entry ) { |
| 91 |
if ( ! is_array( $entry ) ) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
$id = isset( $entry['id'] ) ? sanitize_key( (string) $entry['id'] ) : ''; |
| 95 |
$label = isset( $entry['label'] ) ? wp_strip_all_tags( (string) $entry['label'] ) : ''; |
| 96 |
$icon = isset( $entry['icon'] ) ? sanitize_html_class( (string) $entry['icon'] ) : ''; |
| 97 |
$tone = isset( $entry['tone'] ) ? sanitize_key( (string) $entry['tone'] ) : ''; |
| 98 |
if ( '' === $id || '' === $tone || ! in_array( $tone, $allowed_tones, true ) ) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
if ( isset( $seen[ $id ] ) ) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
$seen[ $id ] = true; |
| 105 |
$clean[] = array( |
| 106 |
'id' => $id, |
| 107 |
'label' => '' !== $label ? $label : ucfirst( $id ), |
| 108 |
'icon' => '' !== $icon ? $icon : 'dashicons-info', |
| 109 |
'tone' => $tone, |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
return empty( $clean ) ? $defaults : $clean; |
| 114 |
} |
| 115 |
|