Menus.php
259 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\WpMenuManager; |
| 4 | |
| 5 | use WP_Admin_Bar; |
| 6 | |
| 7 | class Menus |
| 8 | { |
| 9 | /** |
| 10 | * @var Manager |
| 11 | */ |
| 12 | private Manager $manager; |
| 13 | |
| 14 | public const MENU_SLUG = 'hostinger'; |
| 15 | |
| 16 | /** |
| 17 | * @return void |
| 18 | */ |
| 19 | public function init(): void |
| 20 | { |
| 21 | if (!$this->manager->checkCompatibility()) { |
| 22 | add_action('admin_bar_menu', [$this, 'modifyAdminBar'], 999); |
| 23 | add_filter('admin_body_class', [$this, 'addMenuClass']); |
| 24 | add_action('admin_menu', [$this, 'registerAdminMenu']); |
| 25 | } else { |
| 26 | $this->manager->maybeDoCompatibilityRedirect(); |
| 27 | add_action('admin_notices', [$this, 'compatibilityMessage'], 0); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param Manager $manager |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | public function setManager(Manager $manager): void |
| 37 | { |
| 38 | $this->manager = $manager; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param WP_Admin_Bar $bar |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function modifyAdminBar(WP_Admin_Bar $bar): void |
| 47 | { |
| 48 | if (!is_user_logged_in()) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $menu_items = apply_filters('hostinger_admin_menu_bar_items', []); |
| 53 | |
| 54 | if (!empty($menu_items)) { |
| 55 | $hostinger_icon = '<svg width="28" height="29" viewBox="0 0 28 29" fill="#9ca1a7" style="margin-right: 6px; max-height: 22px; float: left; margin-top: 4px;" xmlns="http://www.w3.org/2000/svg"> |
| 56 | <path fill-rule="evenodd" clip-rule="evenodd" d="M1.8669 13.6096V0.500465L8.48322 4.02842V9.93472L17.2419 9.93895L23.9655 13.6096H1.8669ZM19.033 8.85388V0.5L25.8277 3.94018V12.801L19.033 8.85388ZM19.033 24.8765V19.0211L10.2067 19.015C10.215 19.054 3.37149 15.2857 3.37149 15.2857L25.8277 15.3911V28.5L19.033 24.8765ZM1.86667 24.8765L1.8669 16.31L8.48322 20.1637V28.3164L1.86667 24.8765Z" fill="" /> |
| 57 | </svg>'; |
| 58 | |
| 59 | $bar->add_menu([ |
| 60 | 'id' => 'hostinger_admin_bar', |
| 61 | 'parent' => null, |
| 62 | 'group' => null, |
| 63 | 'title' => $hostinger_icon . esc_html__('Hostinger', 'hostinger-wp-menu-package'), |
| 64 | ]); |
| 65 | |
| 66 | foreach ($menu_items as $menu_item) { |
| 67 | $menu_item_data = [ |
| 68 | 'id' => $menu_item['id'], |
| 69 | 'parent' => 'hostinger_admin_bar', |
| 70 | 'group' => null, |
| 71 | 'title' => $menu_item['title'], |
| 72 | 'href' => $menu_item['href'], |
| 73 | ]; |
| 74 | |
| 75 | if (isset($menu_item['meta'])) { |
| 76 | $menu_item_data['meta'] = $menu_item['meta']; |
| 77 | } |
| 78 | |
| 79 | $bar->add_menu($menu_item_data); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param mixed $classes |
| 86 | * |
| 87 | * @return mixed |
| 88 | */ |
| 89 | public function addMenuClass(mixed $classes): mixed |
| 90 | { |
| 91 | |
| 92 | if (!is_string($classes)) { |
| 93 | return $classes; |
| 94 | } |
| 95 | |
| 96 | $classes .= ' hostinger-hide-main-menu-item'; |
| 97 | |
| 98 | if (!empty(self::isSubmenuItemsHidden())) { |
| 99 | $classes .= ' hostinger-hide-all-menu-items'; |
| 100 | } |
| 101 | |
| 102 | return $classes; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return bool |
| 107 | */ |
| 108 | public function registerAdminMenu(): bool |
| 109 | { |
| 110 | $submenus = self::getMenuSubpages(); |
| 111 | |
| 112 | if (empty($submenus)) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | $icon = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjEiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyMSAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0wLjAwMDE5OTY1MyAxMS4yMzY4VjAuMDAwMzk4MjM1TDUuNjcxMzMgMy4wMjQzNlY4LjA4NjkxTDEzLjE3ODggOC4wOTA1M0wxOC45NDE5IDExLjIzNjhIMC4wMDAxOTk2NTNaTTE0LjcxNCA3LjE2MDQ3VjBMMjAuNTM4IDIuOTQ4NzJWMTAuNTQzN0wxNC43MTQgNy4xNjA0N1pNMTQuNzE0IDIwLjg5NDJWMTUuODc1M0w3LjE0ODYyIDE1Ljg3QzcuMTU1NjggMTUuOTAzNCAxLjI4OTg0IDEyLjY3MzUgMS4yODk4NCAxMi42NzM1TDIwLjUzOCAxMi43NjM4VjI0TDE0LjcxNCAyMC44OTQyWk0wIDIwLjg5NDFMMC4wMDAyMDE3NjkgMTMuNTUxNEw1LjY3MTMzIDE2Ljg1NDZWMjMuODQyN0wwIDIwLjg5NDFaIiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4K'; |
| 117 | |
| 118 | add_menu_page( |
| 119 | __('Hostinger', 'hostinger-wp-menu-package'), |
| 120 | __('Hostinger', 'hostinger-wp-menu-package'), |
| 121 | 'edit_posts', |
| 122 | self::MENU_SLUG, |
| 123 | [$this, 'render'], |
| 124 | $icon, |
| 125 | 1 |
| 126 | ); |
| 127 | |
| 128 | $this->registerSubMenus(); |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @return void |
| 135 | */ |
| 136 | public function render(): void |
| 137 | { |
| 138 | if ($this->hasLoadedMainContent() && !empty(self::isSubmenuItemsHidden())) { |
| 139 | do_action('hostinger_main_menu_content'); |
| 140 | } else { |
| 141 | $submenus = self::getMenuSubpages(); |
| 142 | |
| 143 | if (!empty($submenus)) { |
| 144 | call_user_func($submenus[0]['callback']); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return void |
| 151 | */ |
| 152 | public static function renderMenuNavigation(): string |
| 153 | { |
| 154 | ob_start(); |
| 155 | |
| 156 | require_once __DIR__ . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'menu.php'; |
| 157 | |
| 158 | $content = ob_get_contents(); |
| 159 | |
| 160 | ob_end_clean(); |
| 161 | |
| 162 | return $content; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return array |
| 167 | */ |
| 168 | public static function getMenuSubpages(): array |
| 169 | { |
| 170 | return apply_filters('hostinger_menu_subpages', []); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @return bool |
| 175 | */ |
| 176 | public static function isSubmenuItemsHidden(): bool |
| 177 | { |
| 178 | return !empty(get_option('hostinger_hide_subpages')); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @return bool |
| 183 | */ |
| 184 | private function hasLoadedMainContent(): bool |
| 185 | { |
| 186 | return has_action('hostinger_main_menu_content'); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @return bool |
| 191 | */ |
| 192 | private function registerSubMenus(): bool |
| 193 | { |
| 194 | $submenus = self::getMenuSubpages(); |
| 195 | |
| 196 | if (empty($submenus)) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | foreach ($submenus as $submenu) { |
| 201 | add_submenu_page( |
| 202 | self::MENU_SLUG, |
| 203 | $submenu['page_title'], |
| 204 | $submenu['menu_title'], |
| 205 | $submenu['capability'], |
| 206 | $submenu['menu_slug'], |
| 207 | $submenu['callback'], |
| 208 | $submenu['menu_order'] |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @return void |
| 217 | */ |
| 218 | public function compatibilityMessage(): void |
| 219 | { |
| 220 | ?> |
| 221 | <div class="notice notice-error is-dismissible hts-theme-settings"> |
| 222 | <p> |
| 223 | <strong><?php echo __('Attention! Outdated Plugins Detected', 'hostinger-wp-menu-package') ?></strong> |
| 224 | </p> |
| 225 | <p> |
| 226 | <strong><?php echo __('Action Required:', 'hostinger-wp-menu-package') ?></strong> <?php echo __('Your website has some outdated plugins that might prevent new features from working properly.', 'hostinger-wp-menu-package') ?> |
| 227 | </p> |
| 228 | |
| 229 | <ul style="list-style: circle;margin-left: 18px;"> |
| 230 | <?php |
| 231 | |
| 232 | if (!empty($this->manager->getOutdatedPlugins())) { |
| 233 | ?> |
| 234 | <li> |
| 235 | <p><?php echo __('Outdated plugins:', 'hostinger-wp-menu-package') ?> <?php echo implode(', ', $this->manager->getOutdatedPlugins()); ?></p> |
| 236 | </li> |
| 237 | <?php |
| 238 | } |
| 239 | |
| 240 | if (!empty($this->manager->getAffectedActivePlugins())) { |
| 241 | ?> |
| 242 | <li> |
| 243 | <p><?php echo __('Affected plugins:', 'hostinger-wp-menu-package') ?> <?php echo implode(', ', $this->manager->getAffectedActivePlugins()); ?></p> |
| 244 | </li> |
| 245 | <?php |
| 246 | } |
| 247 | |
| 248 | ?> |
| 249 | </ul> |
| 250 | <p> |
| 251 | <a href="<?php echo esc_url(admin_url('update-core.php')); ?>" class="button-primary"> |
| 252 | <?php echo __('Update plugins', 'hostinger-wp-menu-package') ?> |
| 253 | </a> |
| 254 | </p> |
| 255 | </div> |
| 256 | <?php |
| 257 | } |
| 258 | } |
| 259 |