| 1 |
<?php |
| 2 |
/** |
| 3 |
* Registry factory — OpenStation. |
| 4 |
* |
| 5 |
* **Why this exists.** Across the plugin, ~15 PHP "registry" |
| 6 |
* functions follow the same shape: |
| 7 |
* |
| 8 |
* function openstation_<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 OpenStation |
| 28 |
*/ |
| 29 |
|
| 30 |
defined( 'ABSPATH' ) || exit; |
| 31 |
|
| 32 |
/** |
| 33 |
* Build a per-id metadata registry. |
| 34 |
* |
| 35 |
* Returns a closure with the canonical registry signature: |
| 36 |
* |
| 37 |
* $store — `( '' )` → full map |
| 38 |
* read — `( $id )` → entry or null |
| 39 |
* write — `( $id, $entry )` → store + return entry |
| 40 |
* flush — `( '__flush__' )` → clear store; return empty array |
| 41 |
* |
| 42 |
* Each call to this factory creates an INDEPENDENT closure with its |
| 43 |
* own private `$state` array — closures in PHP capture by reference |
| 44 |
* via the `use ( &$state )` clause. Every registry instance is |
| 45 |
* therefore isolated; different registries cannot stomp on each other. |
| 46 |
* |
| 47 |
* @param array $initial Optional initial entries keyed by id. |
| 48 |
* @return callable Registry closure. |
| 49 |
*/ |
| 50 |
function openstation_create_registry( $initial = array() ) { |
| 51 |
$state = is_array( $initial ) ? $initial : array(); |
| 52 |
return static function ( $id = '', $entry = null ) use ( &$state ) { |
| 53 |
// Flush — used by tests + the deactivation path that needs to |
| 54 |
// rebuild the registry after a plugin unloaded its callbacks. |
| 55 |
if ( '__flush__' === (string) $id ) { |
| 56 |
$state = array(); |
| 57 |
return array(); |
| 58 |
} |
| 59 |
// Read all. |
| 60 |
if ( '' === (string) $id ) { |
| 61 |
return $state; |
| 62 |
} |
| 63 |
// Write. |
| 64 |
if ( null !== $entry ) { |
| 65 |
$state[ (string) $id ] = $entry; |
| 66 |
return $entry; |
| 67 |
} |
| 68 |
// Read one. |
| 69 |
$key = (string) $id; |
| 70 |
return isset( $state[ $key ] ) ? $state[ $key ] : null; |
| 71 |
}; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Build a script-handle registry — boolean-flagged set of WP script |
| 76 |
* handles that the live-refresh payload should advertise so the |
| 77 |
* client can lazy-load them. |
| 78 |
* |
| 79 |
* Same shape as {@see openstation_create_registry()} but values are |
| 80 |
* coerced to bool, and the read-one path returns `false` (not `null`) |
| 81 |
* when the handle is unknown — matching the existing convention used |
| 82 |
* across `*_script_registry()` functions. |
| 83 |
* |
| 84 |
* @param array $initial Optional initial flags keyed by handle. |
| 85 |
* @return callable Script-registry closure. |
| 86 |
*/ |
| 87 |
function openstation_create_script_registry( $initial = array() ) { |
| 88 |
$state = is_array( $initial ) ? $initial : array(); |
| 89 |
return static function ( $handle = '', $value = null ) use ( &$state ) { |
| 90 |
if ( '__flush__' === (string) $handle ) { |
| 91 |
$state = array(); |
| 92 |
return array(); |
| 93 |
} |
| 94 |
if ( '' === (string) $handle ) { |
| 95 |
return $state; |
| 96 |
} |
| 97 |
if ( null !== $value ) { |
| 98 |
$state[ (string) $handle ] = (bool) $value; |
| 99 |
return (bool) $value; |
| 100 |
} |
| 101 |
$key = (string) $handle; |
| 102 |
return isset( $state[ $key ] ) ? $state[ $key ] : false; |
| 103 |
}; |
| 104 |
} |
| 105 |
|