| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMailScoped\YayCommerce\AdminShell\Menu; |
| 4 |
|
| 5 |
use YayMailScoped\YayCommerce\AdminShell\Support\AdminContext; |
| 6 |
/** |
| 7 |
* Strategy (a) — removes legacy per-plugin top-level menu slugs before |
| 8 |
* the shared YayCommerce menu is registered. |
| 9 |
* |
| 10 |
* NOTE: In practice, the real protection against duplicate top-level menus |
| 11 |
* is the isset($admin_page_hooks['yaycommerce']) guard in TopLevelMenu. |
| 12 |
* This class is a safety backstop for edge cases where other plugins register |
| 13 |
* a top-level menu under a DIFFERENT slug that conflicts with our nav tree. |
| 14 |
* |
| 15 |
* The one known legacy slug is 'yaycommerce' itself — handled by TopLevelMenu's |
| 16 |
* guard. The suppressor list here exists for future-proofing. |
| 17 |
* |
| 18 |
* Fallback path: if strategy (a) causes problems in Phase 02 pilot, |
| 19 |
* disable this suppressor and coexist with legacy menus (strategy b). |
| 20 |
*/ |
| 21 |
class MenuSuppressor |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Known legacy top-level menu slugs that should be suppressed. |
| 25 |
* Add slugs here if future plugins register conflicting top-level menus. |
| 26 |
* |
| 27 |
* @var string[] |
| 28 |
*/ |
| 29 |
private array $slugs_to_suppress; |
| 30 |
/** |
| 31 |
* @param string[]|null $slugs Override the default list (mainly for testing). |
| 32 |
*/ |
| 33 |
public function __construct(?array $slugs = null) |
| 34 |
{ |
| 35 |
// Default: empty — the 'yaycommerce' slug is already handled by |
| 36 |
// TopLevelMenu's first-to-register guard. Keep this list explicit. |
| 37 |
$this->slugs_to_suppress = $slugs ?? []; |
| 38 |
} |
| 39 |
/** |
| 40 |
* Register hooks. Runs on admin_menu / network_admin_menu priority 5 |
| 41 |
* (before standard priority 10). |
| 42 |
*/ |
| 43 |
public function init(): void |
| 44 |
{ |
| 45 |
AdminContext::bind_menu([$this, 'suppress_legacy_menus'], 5); |
| 46 |
} |
| 47 |
/** |
| 48 |
* Remove any legacy top-level menus from the slug list. |
| 49 |
*/ |
| 50 |
public function suppress_legacy_menus(): void |
| 51 |
{ |
| 52 |
foreach ($this->slugs_to_suppress as $slug) { |
| 53 |
remove_menu_page($slug); |
| 54 |
} |
| 55 |
} |
| 56 |
/** |
| 57 |
* Return the list of slugs this suppressor will remove. |
| 58 |
* |
| 59 |
* @return string[] |
| 60 |
*/ |
| 61 |
public function get_slugs(): array |
| 62 |
{ |
| 63 |
return $this->slugs_to_suppress; |
| 64 |
} |
| 65 |
} |
| 66 |
|