PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.2
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.2
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 / core / registry-factory.php

registry-factory.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.2, at includes/core/registry-factory.php

110 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Registry factory — Desktop Mode.
4 *
5 * **Why this exists.** Across the plugin, ~15 PHP "registry"
6 * functions follow the same shape:
7 *
8 * function desktop_mode_<thing>_registry( $id = '', $entry = null ) {
9 * static $store = array();
10 * if ( '__flush__' === (string) $id ) { $store = array(); return array(); }
11 * if ( '' === (string) $id ) return $store;
12 * if ( null !== $entry ) $store[ $id ] = $entry;
13 * return isset( $store[ $id ] ) ? $store[ $id ] : null;
14 * }
15 *
16 * Native windows, widgets, wallpapers, icons, window-tabs, commands,
17 * settings tabs, window themes, window controls, window slots, window
18 * chrome, dock-rail renderers, palettes, title-bar buttons, toast
19 * types — all the same body with cosmetic differences. New registries
20 * (palettes, future surfaces) hand-copy the pattern each time.
21 *
22 * This file replaces the boilerplate with two factories. Existing
23 * registry functions in `includes/components.php`, `commands.php`,
24 * `settings-tabs.php`, `window-chrome.php`, etc. can become one-line
25 * forwarders calling these factories.
26 *
27 * @package Desktop_Mode
28 * @since 0.8.1
29 */
30
31 defined( 'ABSPATH' ) || exit;
32
33 /**
34 * Build a per-id metadata registry.
35 *
36 * Returns a closure with the canonical registry signature:
37 *
38 * $store — `( '' )` → full map
39 * read — `( $id )` → entry or null
40 * write — `( $id, $entry )` → store + return entry
41 * flush — `( '__flush__' )` → clear store; return empty array
42 *
43 * Each call to this factory creates an INDEPENDENT closure with its
44 * own private `$state` array — closures in PHP capture by reference
45 * via the `use ( &$state )` clause. Every registry instance is
46 * therefore isolated; different registries cannot stomp on each other.
47 *
48 * @since 0.8.1
49 *
50 * @param array $initial Optional initial entries keyed by id.
51 * @return callable Registry closure.
52 */
53 function desktop_mode_create_registry( $initial = array() ) {
54 $state = is_array( $initial ) ? $initial : array();
55 return static function ( $id = '', $entry = null ) use ( &$state ) {
56 // Flush — used by tests + the deactivation path that needs to
57 // rebuild the registry after a plugin unloaded its callbacks.
58 if ( '__flush__' === (string) $id ) {
59 $state = array();
60 return array();
61 }
62 // Read all.
63 if ( '' === (string) $id ) {
64 return $state;
65 }
66 // Write.
67 if ( null !== $entry ) {
68 $state[ (string) $id ] = $entry;
69 return $entry;
70 }
71 // Read one.
72 $key = (string) $id;
73 return isset( $state[ $key ] ) ? $state[ $key ] : null;
74 };
75 }
76
77 /**
78 * Build a script-handle registry — boolean-flagged set of WP script
79 * handles that the live-refresh payload should advertise so the
80 * client can lazy-load them.
81 *
82 * Same shape as {@see desktop_mode_create_registry()} but values are
83 * coerced to bool, and the read-one path returns `false` (not `null`)
84 * when the handle is unknown — matching the existing convention used
85 * across `*_script_registry()` functions.
86 *
87 * @since 0.8.1
88 *
89 * @param array $initial Optional initial flags keyed by handle.
90 * @return callable Script-registry closure.
91 */
92 function desktop_mode_create_script_registry( $initial = array() ) {
93 $state = is_array( $initial ) ? $initial : array();
94 return static function ( $handle = '', $value = null ) use ( &$state ) {
95 if ( '__flush__' === (string) $handle ) {
96 $state = array();
97 return array();
98 }
99 if ( '' === (string) $handle ) {
100 return $state;
101 }
102 if ( null !== $value ) {
103 $state[ (string) $handle ] = (bool) $value;
104 return (bool) $value;
105 }
106 $key = (string) $handle;
107 return isset( $state[ $key ] ) ? $state[ $key ] : false;
108 };
109 }
110