Constants
2 weeks ago
Contracts
2 weeks ago
Hooks
2 weeks ago
Models
2 weeks ago
BaseHook.php
2 weeks ago
HookServiceProvider.php
2 weeks ago
Menu.php
2 weeks ago
User.php
2 weeks ago
UserMeta.php
2 weeks ago
Menu.php
152 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Base class for declaring WordPress admin menu pages with title, capability, slug, and icon properties. |
| 5 | * Supports main menu and submenu types with a render method calling add_menu_page or add_submenu_page. |
| 6 | * Subclasses define is_displayable for conditional visibility. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Wordpress |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Wordpress; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Kirki\Framework\Wordpress\Constants\MenuTypes; |
| 16 | use Kirki\Framework\Supports\Arr; |
| 17 | use Exception; |
| 18 | use function Kirki\Framework\collection; |
| 19 | class Menu |
| 20 | { |
| 21 | /** |
| 22 | * The page menu type i.e. main menu or submenu |
| 23 | * |
| 24 | * @var MenuTypes |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | protected $menu_type = MenuTypes::MAIN_MENU; |
| 29 | /** |
| 30 | * The page title |
| 31 | * |
| 32 | * @var string |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | protected $page_title = null; |
| 37 | /** |
| 38 | * The menu title |
| 39 | * |
| 40 | * @var string |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | */ |
| 44 | protected $menu_title = null; |
| 45 | /** |
| 46 | * The capabilities |
| 47 | * |
| 48 | * @var string |
| 49 | * |
| 50 | * @since 1.0.0 |
| 51 | */ |
| 52 | protected $capabilities = null; |
| 53 | /** |
| 54 | * The menu slug |
| 55 | * |
| 56 | * @var string |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | */ |
| 60 | protected $menu_slug = null; |
| 61 | /** |
| 62 | * The callback |
| 63 | * |
| 64 | * @var string |
| 65 | * |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | protected $callback = '__return_false'; |
| 69 | /** |
| 70 | * The icon url |
| 71 | * |
| 72 | * @var string |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | protected $icon_url = null; |
| 77 | /** |
| 78 | * The position |
| 79 | * |
| 80 | * @var int |
| 81 | * |
| 82 | * @since 1.0.0 |
| 83 | */ |
| 84 | protected $position = null; |
| 85 | /** |
| 86 | * The parent slug |
| 87 | * |
| 88 | * @var string |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | */ |
| 92 | protected $parent_slug = null; |
| 93 | /** |
| 94 | * Create a new instance. |
| 95 | * |
| 96 | * @return void |
| 97 | * |
| 98 | * @throws \Exception |
| 99 | * |
| 100 | * @since 1.0.0 |
| 101 | */ |
| 102 | public function __construct() |
| 103 | { |
| 104 | if (!$this->check_required_properties()) { |
| 105 | throw new Exception('Missing required properties for making a menu item'); |
| 106 | } |
| 107 | } |
| 108 | /** |
| 109 | * Determine whether the displayable. |
| 110 | * |
| 111 | * @return bool |
| 112 | * |
| 113 | * @since 1.0.0 |
| 114 | */ |
| 115 | public function is_displayable() |
| 116 | { |
| 117 | return \true; |
| 118 | } |
| 119 | /** |
| 120 | * Check required properties. |
| 121 | * |
| 122 | * @return void |
| 123 | * |
| 124 | * @since 1.0.0 |
| 125 | */ |
| 126 | protected function check_required_properties() |
| 127 | { |
| 128 | $properties = [$this->page_title, $this->menu_title, $this->capabilities, $this->menu_slug]; |
| 129 | if (MenuTypes::SUB_MENU === $this->menu_type) { |
| 130 | $properties[] = $this->parent_slug; |
| 131 | } |
| 132 | return collection($properties)->every(function ($property) { |
| 133 | return !empty($property); |
| 134 | }); |
| 135 | } |
| 136 | /** |
| 137 | * Render the menu |
| 138 | * |
| 139 | * @return void |
| 140 | * |
| 141 | * @since 1.0.0 |
| 142 | */ |
| 143 | public function render() |
| 144 | { |
| 145 | if ($this->menu_type === MenuTypes::MAIN_MENU) { |
| 146 | add_menu_page($this->page_title, $this->menu_title, $this->capabilities, $this->menu_slug, $this->callback, $this->icon_url, $this->position); |
| 147 | } else { |
| 148 | add_submenu_page($this->parent_slug, $this->page_title, $this->menu_title, $this->capabilities, $this->menu_slug, $this->callback, $this->position); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 |