Assets.php
159 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\WpMenuManager; |
| 4 | |
| 5 | use Hostinger\WpMenuManager\Menus; |
| 6 | use Hostinger\WpHelper\Utils; |
| 7 | |
| 8 | class Assets |
| 9 | { |
| 10 | /** |
| 11 | * @var Manager |
| 12 | */ |
| 13 | private Manager $manager; |
| 14 | private string $assetsPath; |
| 15 | |
| 16 | /** |
| 17 | * @return void |
| 18 | */ |
| 19 | public function init(): void |
| 20 | { |
| 21 | if (!$this->manager->checkCompatibility()) { |
| 22 | add_action('admin_enqueue_scripts', [$this, 'enqueueAdminAssets']); |
| 23 | add_action('admin_head', [$this, 'addMenuHidingCss']); |
| 24 | } |
| 25 | $this->assetsPath = '/vendor/hostinger/hostinger-wp-menu-manager/assets'; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @param Manager $manager |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function setManager(Manager $manager): void |
| 34 | { |
| 35 | $this->manager = $manager; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return void |
| 40 | */ |
| 41 | public function enqueueAdminAssets(): void |
| 42 | { |
| 43 | $defaultVersion = '1.0.0'; |
| 44 | |
| 45 | if ($this->isHostingerMenuPage()) { |
| 46 | $pluginInfo = $this->manager->getPluginInfo(); |
| 47 | $jsScriptPath = __DIR__ . '/../assets/js/menus.min.js'; |
| 48 | $cssStylePath = __DIR__ . '/../assets/css/style.min.css'; |
| 49 | |
| 50 | $jsVersion = $defaultVersion; |
| 51 | if (file_exists($jsScriptPath)) { |
| 52 | $jsVersion = filemtime($jsScriptPath) ?: $jsVersion; |
| 53 | } |
| 54 | |
| 55 | wp_enqueue_script( |
| 56 | 'hostinger_menu_scripts', |
| 57 | $pluginInfo . $this->assetsPath . '/js/menus.min.js', |
| 58 | [ 'jquery' ], |
| 59 | $jsVersion, |
| 60 | false |
| 61 | ); |
| 62 | |
| 63 | $cssVersion = $defaultVersion; |
| 64 | if (file_exists($cssStylePath)) { |
| 65 | $cssVersion = filemtime($cssStylePath) ?: $cssVersion; |
| 66 | } |
| 67 | |
| 68 | wp_enqueue_style( |
| 69 | 'hostinger_menu_styles', |
| 70 | $pluginInfo . $this->assetsPath . '/css/style.min.css', |
| 71 | [], |
| 72 | $cssVersion |
| 73 | ); |
| 74 | |
| 75 | // Hide notices and badges in Hostinger menu pages. |
| 76 | $hide_notices = '.notice { display: none !important; } .hostinger-notice { display: block !important; }'; |
| 77 | wp_add_inline_style('hostinger_menu_styles', $hide_notices); |
| 78 | |
| 79 | if (Utils::isPluginActive('wpforms')) { |
| 80 | $hide_wp_forms_counter = '.wp-admin #wpadminbar .wpforms-menu-notification-counter { display: none !important; }'; |
| 81 | wp_add_inline_style('hostinger_menu_styles', $hide_wp_forms_counter); |
| 82 | } |
| 83 | if (Utils::isPluginActive('googleanalytics')) { |
| 84 | $hide_monsterinsights_notification = '.wp-admin .monsterinsights-menu-notification-indicator { display: none !important; }'; |
| 85 | wp_add_inline_style('hostinger_menu_styles', $hide_monsterinsights_notification); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return void |
| 92 | */ |
| 93 | public function addMenuHidingCss(): void |
| 94 | { |
| 95 | // These CSS rules should be loaded on every page in WordPress admin. |
| 96 | ?> |
| 97 | <style type="text/css"> |
| 98 | body.hostinger-hide-main-menu-item #toplevel_page_hostinger .wp-submenu > .wp-first-item { |
| 99 | display: none; |
| 100 | } |
| 101 | |
| 102 | #wpadminbar #wp-admin-bar-hostinger_admin_bar .ab-item { |
| 103 | align-items: center; |
| 104 | display: flex; |
| 105 | } |
| 106 | |
| 107 | #wpadminbar #wp-admin-bar-hostinger_admin_bar .ab-sub-wrapper .ab-item svg { |
| 108 | fill: #9ca1a7; |
| 109 | margin-left: 3px; |
| 110 | max-height: 18px; |
| 111 | } |
| 112 | |
| 113 | body.hostinger-hide-all-menu-items #toplevel_page_hostinger .wp-submenu { |
| 114 | display: none !important; |
| 115 | } |
| 116 | |
| 117 | body.hostinger-hide-all-menu-items .hsr-onboarding-navbar__wrapper { |
| 118 | justify-content: center; |
| 119 | } |
| 120 | |
| 121 | body.hostinger-hide-all-menu-items .hsr-onboarding-navbar .hsr-mobile-menu-icon, |
| 122 | body.hostinger-hide-all-menu-items .hsr-onboarding-navbar .hsr-wrapper__list { |
| 123 | display: none !important; |
| 124 | } |
| 125 | </style> |
| 126 | <?php |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @return bool |
| 131 | */ |
| 132 | private function isHostingerMenuPage(): bool |
| 133 | { |
| 134 | $admin_path = parse_url(admin_url(), PHP_URL_PATH); |
| 135 | |
| 136 | $pages = [ |
| 137 | $admin_path . 'admin.php?page=' . Menus::MENU_SLUG |
| 138 | ]; |
| 139 | |
| 140 | $subpages = Menus::getMenuSubpages(); |
| 141 | |
| 142 | foreach ($subpages as $page) { |
| 143 | if (isset($page['menu_slug'])) { |
| 144 | $pages[] = $admin_path . 'admin.php?page=' . $page['menu_slug']; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | $utils = new Utils(); |
| 149 | |
| 150 | foreach ($pages as $page) { |
| 151 | if ($utils->isThisPage($page)) { |
| 152 | return true; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 |