| 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 |
// Old menu navigation removed. |
| 155 |
// Should cleanup this function in later versions. |
| 156 |
|
| 157 |
return ''; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
public static function getMenuSubpages(): array |
| 164 |
{ |
| 165 |
return apply_filters('hostinger_menu_subpages', []); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @return bool |
| 170 |
*/ |
| 171 |
public static function isSubmenuItemsHidden(): bool |
| 172 |
{ |
| 173 |
return !empty(get_option('hostinger_hide_subpages')); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @return bool |
| 178 |
*/ |
| 179 |
private function hasLoadedMainContent(): bool |
| 180 |
{ |
| 181 |
return has_action('hostinger_main_menu_content'); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @return bool |
| 186 |
*/ |
| 187 |
private function registerSubMenus(): bool |
| 188 |
{ |
| 189 |
$submenus = self::getMenuSubpages(); |
| 190 |
|
| 191 |
if (empty($submenus)) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
foreach ($submenus as $submenu) { |
| 196 |
add_submenu_page( |
| 197 |
self::MENU_SLUG, |
| 198 |
$submenu['page_title'], |
| 199 |
$submenu['menu_title'], |
| 200 |
$submenu['capability'], |
| 201 |
$submenu['menu_slug'], |
| 202 |
$submenu['callback'], |
| 203 |
$submenu['menu_order'] |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @return void |
| 212 |
*/ |
| 213 |
public function compatibilityMessage(): void |
| 214 |
{ |
| 215 |
?> |
| 216 |
<div class="notice notice-error is-dismissible hts-theme-settings"> |
| 217 |
<p> |
| 218 |
<strong><?php echo __('Attention! Outdated Plugins Detected', 'hostinger-wp-menu-package') ?></strong> |
| 219 |
</p> |
| 220 |
<p> |
| 221 |
<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') ?> |
| 222 |
</p> |
| 223 |
|
| 224 |
<ul style="list-style: circle;margin-left: 18px;"> |
| 225 |
<?php |
| 226 |
|
| 227 |
if (!empty($this->manager->getOutdatedPlugins())) { |
| 228 |
?> |
| 229 |
<li> |
| 230 |
<p><?php echo __('Outdated plugins:', 'hostinger-wp-menu-package') ?> <?php echo implode(', ', $this->manager->getOutdatedPlugins()); ?></p> |
| 231 |
</li> |
| 232 |
<?php |
| 233 |
} |
| 234 |
|
| 235 |
if (!empty($this->manager->getAffectedActivePlugins())) { |
| 236 |
?> |
| 237 |
<li> |
| 238 |
<p><?php echo __('Affected plugins:', 'hostinger-wp-menu-package') ?> <?php echo implode(', ', $this->manager->getAffectedActivePlugins()); ?></p> |
| 239 |
</li> |
| 240 |
<?php |
| 241 |
} |
| 242 |
|
| 243 |
?> |
| 244 |
</ul> |
| 245 |
<p> |
| 246 |
<a href="<?php echo esc_url(admin_url('update-core.php')); ?>" class="button-primary"> |
| 247 |
<?php echo __('Update plugins', 'hostinger-wp-menu-package') ?> |
| 248 |
</a> |
| 249 |
</p> |
| 250 |
</div> |
| 251 |
<?php |
| 252 |
} |
| 253 |
} |
| 254 |
|