| 1 |
<?php |
| 2 |
/** |
| 3 |
* Section Registry. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services\Settings; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Interfaces\Settings_Section_Interface; |
| 11 |
|
| 12 |
/** |
| 13 |
* The Section Registry — the seam where Settings Sections are registered. |
| 14 |
* |
| 15 |
* The free plugin registers its core sections; Pro and extensions register |
| 16 |
* theirs via the `woocommerce_pos_register_settings_sections` action instead |
| 17 |
* of hooking ad-hoc filters. Registering an existing id replaces the previous |
| 18 |
* section (last-wins), which is also the supported override mechanism. |
| 19 |
* Override sections should extend Abstract_Section (not implement the |
| 20 |
* interface directly) so the typed accessors' default fallback |
| 21 |
* (Settings::section_value()) keeps working. |
| 22 |
*/ |
| 23 |
class Section_Registry { |
| 24 |
/** |
| 25 |
* Registered sections, keyed by id. |
| 26 |
* |
| 27 |
* @var array<string, Settings_Section_Interface> |
| 28 |
*/ |
| 29 |
private $sections = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Register (or replace) a section. |
| 33 |
* |
| 34 |
* @param Settings_Section_Interface $section The section. |
| 35 |
*/ |
| 36 |
public function register( Settings_Section_Interface $section ): void { |
| 37 |
$this->sections[ $section->id() ] = $section; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get a section by id. |
| 42 |
* |
| 43 |
* @param string $id Section id. |
| 44 |
* |
| 45 |
* @return Settings_Section_Interface|null |
| 46 |
*/ |
| 47 |
public function get( string $id ): ?Settings_Section_Interface { |
| 48 |
return $this->sections[ $id ] ?? null; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Whether a section is registered. |
| 53 |
* |
| 54 |
* @param string $id Section id. |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public function has( string $id ): bool { |
| 59 |
return isset( $this->sections[ $id ] ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* All registered sections, keyed by id. |
| 64 |
* |
| 65 |
* @return array<string, Settings_Section_Interface> |
| 66 |
*/ |
| 67 |
public function all(): array { |
| 68 |
return $this->sections; |
| 69 |
} |
| 70 |
} |
| 71 |
|