PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.8.8
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.8.8
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 / accents.php

accents.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.8.8, at includes/accents.php

101 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 — Accent color swatches.
4 *
5 * The OS Settings panel lets users pick an accent color that the shell
6 * applies to `--wp-admin-theme-color` on the parent frame. Themes and
7 * plugins can extend or restrict the swatch list via the
8 * {@see 'desktop_mode_accent_colors'} filter — e.g. a brand theme that
9 * injects its corporate palette, or a compliance plugin that collapses
10 * the list to a single approved value.
11 *
12 * @package WPDesktopMode
13 * @since 0.11.0
14 */
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Returns the list of accent-color swatches shown in OS Settings.
20 *
21 * Each entry is an array shaped `{ id, label, value }`:
22 *
23 * - `id` is a stable slug (persisted in `localStorage`).
24 * - `label` is the human-readable name used in the picker tooltip.
25 * - `value` is a hex color applied to `--wp-admin-theme-color`.
26 *
27 * The defaults ship with six WordPress-adjacent swatches. Consumers
28 * mutate the list via the `desktop_mode_accent_colors` filter — entries
29 * whose `value` fails `sanitize_hex_color()` are dropped silently so a
30 * bad filter return can't inject arbitrary CSS into the shell.
31 *
32 * @since 0.11.0
33 *
34 * @return array<int, array{id: string, label: string, value: string}>
35 */
36 function desktop_mode_get_accent_colors() {
37 $defaults = array(
38 array( 'id' => 'wp-blue', 'label' => __( 'WordPress Blue', 'desktop-mode' ), 'value' => '#2271b1' ),
39 array( 'id' => 'indigo', 'label' => __( 'Indigo', 'desktop-mode' ), 'value' => '#3858e9' ),
40 array( 'id' => 'teal', 'label' => __( 'Teal', 'desktop-mode' ), 'value' => '#04a4cc' ),
41 array( 'id' => 'emerald', 'label' => __( 'Emerald', 'desktop-mode' ), 'value' => '#059669' ),
42 array( 'id' => 'amber', 'label' => __( 'Amber', 'desktop-mode' ), 'value' => '#d97706' ),
43 array( 'id' => 'rose', 'label' => __( 'Rose', 'desktop-mode' ), 'value' => '#e11d48' ),
44 );
45
46 /**
47 * Filters the list of accent-color swatches offered in OS Settings.
48 *
49 * ```php
50 * add_filter( 'desktop_mode_accent_colors', function ( $colors ) {
51 * $colors[] = array(
52 * 'id' => 'brand',
53 * 'label' => __( 'Brand', 'my-plugin' ),
54 * 'value' => '#ff00ff',
55 * );
56 * return $colors;
57 * } );
58 * ```
59 *
60 * Non-array return values fall back to the built-in defaults.
61 * Individual entries missing an `id`, `label`, or whose `value`
62 * isn't a valid hex color are dropped — the shell can't render
63 * half-formed swatches safely.
64 *
65 * @since 0.11.0
66 *
67 * @param array $defaults Built-in swatches.
68 */
69 $filtered = apply_filters( 'desktop_mode_accent_colors', $defaults );
70 if ( ! is_array( $filtered ) ) {
71 return $defaults;
72 }
73
74 $clean = array();
75 $seen = array();
76 foreach ( $filtered as $entry ) {
77 if ( ! is_array( $entry ) ) {
78 continue;
79 }
80 $id = isset( $entry['id'] ) ? sanitize_key( (string) $entry['id'] ) : '';
81 $label = isset( $entry['label'] ) ? wp_strip_all_tags( (string) $entry['label'] ) : '';
82 $value = isset( $entry['value'] ) ? sanitize_hex_color( (string) $entry['value'] ) : '';
83 if ( '' === $id || '' === $label || null === $value || '' === $value ) {
84 continue;
85 }
86 if ( isset( $seen[ $id ] ) ) {
87 continue;
88 }
89 $seen[ $id ] = true;
90 $clean[] = array(
91 'id' => $id,
92 'label' => $label,
93 'value' => $value,
94 );
95 }
96
97 // If every entry was dropped (e.g. a filter returned all garbage),
98 // fall back to defaults so the picker is never empty.
99 return empty( $clean ) ? $defaults : $clean;
100 }
101