| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — registers our icons with WordPress. |
| 4 |
* |
| 5 |
* Core owns the verbs, OpenStation owns the nouns. Save, search, trash and |
| 6 |
* settings come from WordPress; the eleven registered here are the station's |
| 7 |
* own vocabulary, the things that exist because this is a desktop and |
| 8 |
* wp-admin is not. |
| 9 |
* |
| 10 |
* Registering them puts them wherever Core's icons go: the icon picker in the |
| 11 |
* editor, the REST API, and `wp_get_icon( 'openstation/window' )` in PHP, for |
| 12 |
* us and for anyone extending the shell. |
| 13 |
* |
| 14 |
* The API landed in WordPress 7.1 and this plugin supports 6.0, so every call |
| 15 |
* is behind a feature check. On older versions the shell keeps drawing its own |
| 16 |
* inline SVG exactly as it does today; nothing here is load-bearing for the |
| 17 |
* desktop. |
| 18 |
* |
| 19 |
* Not to be confused with {@see openstation_register_icon()} in |
| 20 |
* `includes/registries/icons.php`, which registers a clickable shortcut tile |
| 21 |
* on the desktop wallpaper. That is a surface of the shell. This is artwork |
| 22 |
* handed to WordPress. |
| 23 |
* |
| 24 |
* @package OpenStation |
| 25 |
*/ |
| 26 |
|
| 27 |
defined( 'ABSPATH' ) || exit; |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns the eleven icons OpenStation draws itself, as slug => label. |
| 31 |
* |
| 32 |
* The labels are what a person reads in the icon picker, so they name the |
| 33 |
* concept rather than repeating the slug: `windows` is the overview of all of |
| 34 |
* them, `snap` is the tiling layout, `command` is the palette. |
| 35 |
* |
| 36 |
* @return array<string, string> Icon slug mapped to its translated label. |
| 37 |
*/ |
| 38 |
function openstation_wp_icon_collection() { |
| 39 |
return array( |
| 40 |
'window' => __( 'Window', 'desktop-mode' ), |
| 41 |
'windows' => __( 'All windows', 'desktop-mode' ), |
| 42 |
'dock' => __( 'Dock', 'desktop-mode' ), |
| 43 |
'spaces' => __( 'Spaces', 'desktop-mode' ), |
| 44 |
'copilot' => __( 'Copilot', 'desktop-mode' ), |
| 45 |
'snap' => __( 'Snap layout', 'desktop-mode' ), |
| 46 |
'command' => __( 'Command palette', 'desktop-mode' ), |
| 47 |
'apps' => __( 'Apps', 'desktop-mode' ), |
| 48 |
'widgets' => __( 'Widgets', 'desktop-mode' ), |
| 49 |
'user' => __( 'User', 'desktop-mode' ), |
| 50 |
'lock' => __( 'Lock', 'desktop-mode' ), |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Registers the `openstation` icon collection and the icons in it. |
| 56 |
* |
| 57 |
* Runs on `init` at the default priority: Core registers its own collection on |
| 58 |
* the same hook at priority 0, so the registry is ready by the time this runs. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
function openstation_register_wp_icons() { |
| 63 |
if ( ! function_exists( 'wp_register_icon_collection' ) || ! function_exists( 'wp_register_icon' ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* Ask before registering, rather than reading the return value. |
| 69 |
* |
| 70 |
* `wp_register_icon_collection()` does return false for a slug that is |
| 71 |
* already there, but it calls `_doing_it_wrong()` on the way out, so by |
| 72 |
* the time we see the false a notice has already been raised. Anything |
| 73 |
* that runs `init` twice in one request then trips it, and in a test run |
| 74 |
* WordPress turns that notice into a failure. |
| 75 |
* |
| 76 |
* `is_registered()` on the registry is the only way to ask without |
| 77 |
* triggering it: 7.1 ships no `wp_is_icon_collection_registered()` |
| 78 |
* wrapper. Guarded by `class_exists` so this stays safe if that changes. |
| 79 |
*/ |
| 80 |
if ( class_exists( 'WP_Icon_Collections_Registry' ) ) { |
| 81 |
$collections = WP_Icon_Collections_Registry::get_instance(); |
| 82 |
if ( $collections->is_registered( 'openstation' ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! wp_register_icon_collection( |
| 88 |
'openstation', |
| 89 |
array( |
| 90 |
'label' => __( 'OpenStation', 'desktop-mode' ), |
| 91 |
'description' => __( 'Icons for the desktop shell: windows, spaces, the dock, and the command palette.', 'desktop-mode' ), |
| 92 |
) |
| 93 |
) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
foreach ( openstation_wp_icon_collection() as $slug => $label ) { |
| 98 |
wp_register_icon( |
| 99 |
'openstation/' . $slug, |
| 100 |
array( |
| 101 |
'label' => $label, |
| 102 |
'file_path' => OPENSTATION_DIR . 'assets/icons/' . $slug . '.svg', |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
add_action( 'init', 'openstation_register_wp_icons' ); |
| 108 |
|