|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Grzechu\CustomAdminPage; |
| 5 | + |
| 6 | +abstract class AdminPage |
| 7 | +{ |
| 8 | + private static $instances; |
| 9 | + |
| 10 | + /** |
| 11 | + * @var string |
| 12 | + */ |
| 13 | + protected $pageTitle; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $menuTitle; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $capability; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $menuSlug; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + private $iconUrl; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var int |
| 37 | + */ |
| 38 | + private $position; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string|null |
| 42 | + */ |
| 43 | + private $parentSlug; |
| 44 | + |
| 45 | + final public static function getInstance(): self |
| 46 | + { |
| 47 | + $className = get_called_class(); |
| 48 | + |
| 49 | + if (isset(self::$instances[$className]) === false) { |
| 50 | + self::$instances[$className] = new static(); |
| 51 | + } |
| 52 | + |
| 53 | + return self::$instances[$className]; |
| 54 | + } |
| 55 | + |
| 56 | + public function __toString() |
| 57 | + { |
| 58 | + return self::class; |
| 59 | + } |
| 60 | + |
| 61 | + final private function __construct() |
| 62 | + { |
| 63 | + $this->pageTitle = $this->getPageTitle(); |
| 64 | + $this->menuTitle = $this->getMenuTitle(); |
| 65 | + $this->capability = $this->getCapability(); |
| 66 | + $this->menuSlug = $this->getMenuSlug(); |
| 67 | + $this->iconUrl = $this->getIconUrl(); |
| 68 | + $this->position = $this->getPosition(); |
| 69 | + $this->parentSlug = $this->getParentSlug(); |
| 70 | + |
| 71 | + add_action( |
| 72 | + 'in_admin_header', |
| 73 | + [$this, 'dismissAllNotices'], |
| 74 | + 1000 |
| 75 | + ); |
| 76 | + $this->registerMenu(); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + abstract public function getPageTitle(): string; |
| 81 | + abstract public function getMenuTitle(): string; |
| 82 | + abstract public function getCapability(): string; |
| 83 | + abstract public function getMenuSlug(): string; |
| 84 | + abstract public function callableFunction(); |
| 85 | + |
| 86 | + public function getPosition(): int |
| 87 | + { |
| 88 | + return 1; |
| 89 | + } |
| 90 | + |
| 91 | + public function getIconUrl(): string |
| 92 | + { |
| 93 | + return ''; |
| 94 | + } |
| 95 | + |
| 96 | + public function getParentSlug() |
| 97 | + { |
| 98 | + return ''; |
| 99 | + } |
| 100 | + |
| 101 | + public function adminMenu() |
| 102 | + { |
| 103 | + add_menu_page( |
| 104 | + $this->pageTitle, |
| 105 | + $this->menuTitle, |
| 106 | + $this->capability, |
| 107 | + $this->menuSlug, |
| 108 | + [$this, 'callableFunction'], |
| 109 | + $this->iconUrl, |
| 110 | + $this->position |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + public function adminSubMenu() |
| 115 | + { |
| 116 | + add_submenu_page( |
| 117 | + $this->parentSlug, |
| 118 | + $this->pageTitle, |
| 119 | + $this->menuTitle, |
| 120 | + $this->capability, |
| 121 | + $this->menuSlug, |
| 122 | + [$this, 'callableFunction'] |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + public function dismissAllNotices() |
| 127 | + { |
| 128 | + if (!isset($_GET['page']) || $_GET['page'] !== $this->menuSlug) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + remove_all_actions('admin_notices'); |
| 133 | + remove_all_actions('all_admin_notices'); |
| 134 | + } |
| 135 | + |
| 136 | + private function registerMenu() |
| 137 | + { |
| 138 | + if ($this->parentSlug !== '') { |
| 139 | + add_action('admin_menu', [$this, 'adminSubMenu']); |
| 140 | + |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + add_action('admin_menu', [$this, 'adminMenu']); |
| 145 | + } |
| 146 | + |
| 147 | + final private function __clone() |
| 148 | + { |
| 149 | + } |
| 150 | + |
| 151 | + final private function __wakeup() |
| 152 | + { |
| 153 | + } |
| 154 | +} |
0 commit comments