AdminBarMenu.php
221 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\WP\WPMenu; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 6 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 7 | |
| 8 | /** |
| 9 | * Class AdminBarMenu |
| 10 | * |
| 11 | * @package AmeliaBooking\Infrastructure\WP\WPMenu |
| 12 | */ |
| 13 | class AdminBarMenu |
| 14 | { |
| 15 | /** @var SettingsService $settingsService */ |
| 16 | private $settingsService; |
| 17 | |
| 18 | /** @var bool $scriptAdded */ |
| 19 | private static $scriptAdded = false; |
| 20 | |
| 21 | /** |
| 22 | * AdminBarMenu constructor. |
| 23 | * |
| 24 | * @param SettingsService $settingsService |
| 25 | */ |
| 26 | public function __construct($settingsService) |
| 27 | { |
| 28 | $this->settingsService = $settingsService; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Add Amelia menu to WordPress admin bar |
| 33 | */ |
| 34 | public function addAdminBarMenu(\WP_Admin_Bar $wpAdminBar) |
| 35 | { |
| 36 | if (!current_user_can('amelia_read_menu')) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | $icon = $this->getAmeliaIcon(); |
| 41 | |
| 42 | $wpAdminBar->add_menu([ |
| 43 | 'id' => 'amelia-menu', |
| 44 | 'title' => '<span class="ab-icon">' . $icon . '</span><span class="ab-label">Amelia</span>', |
| 45 | 'href' => admin_url('admin.php?page=wpamelia-dashboard'), |
| 46 | 'meta' => [ |
| 47 | 'title' => 'Amelia Booking', |
| 48 | 'class' => 'amelia-admin-bar-menu' |
| 49 | ] |
| 50 | ]); |
| 51 | |
| 52 | if (current_user_can('amelia_read_calendar')) { |
| 53 | $wpAdminBar->add_node([ |
| 54 | 'parent' => 'amelia-menu', |
| 55 | 'id' => 'amelia-calendar', |
| 56 | 'title' => BackendStrings::get('calendar'), |
| 57 | 'href' => admin_url('admin.php?page=wpamelia-calendar'), |
| 58 | 'meta' => [ |
| 59 | 'title' => BackendStrings::get('calendar') |
| 60 | ] |
| 61 | ]); |
| 62 | } |
| 63 | |
| 64 | if (current_user_can('amelia_write_appointments') || current_user_can('amelia_write_events')) { |
| 65 | $wpAdminBar->add_node([ |
| 66 | 'parent' => 'amelia-menu', |
| 67 | 'id' => 'amelia-add-booking', |
| 68 | 'title' => BackendStrings::get('add_booking'), |
| 69 | 'href' => '#', |
| 70 | 'meta' => [ |
| 71 | 'title' => BackendStrings::get('add_booking') |
| 72 | ] |
| 73 | ]); |
| 74 | |
| 75 | if (current_user_can('amelia_write_appointments')) { |
| 76 | $wpAdminBar->add_node([ |
| 77 | 'parent' => 'amelia-add-booking', |
| 78 | 'id' => 'amelia-add-appointment', |
| 79 | 'title' => BackendStrings::get('add_appointment'), |
| 80 | 'href' => admin_url('admin.php?page=wpamelia-bookings#/book-appointment'), |
| 81 | 'meta' => [ |
| 82 | 'title' => BackendStrings::get('add_appointment') |
| 83 | ] |
| 84 | ]); |
| 85 | |
| 86 | if ($this->settingsService->isFeatureEnabled('packages')) { |
| 87 | $wpAdminBar->add_node([ |
| 88 | 'parent' => 'amelia-add-booking', |
| 89 | 'id' => 'amelia-add-package', |
| 90 | 'title' => BackendStrings::get('add_package'), |
| 91 | 'href' => admin_url('admin.php?page=wpamelia-bookings#/book-package'), |
| 92 | 'meta' => [ |
| 93 | 'title' => BackendStrings::get('add_package') |
| 94 | ] |
| 95 | ]); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (current_user_can('amelia_write_events')) { |
| 100 | $wpAdminBar->add_node([ |
| 101 | 'parent' => 'amelia-add-booking', |
| 102 | 'id' => 'amelia-add-event', |
| 103 | 'title' => BackendStrings::get('add_event'), |
| 104 | 'href' => admin_url('admin.php?page=wpamelia-bookings#/book-event'), |
| 105 | 'meta' => [ |
| 106 | 'title' => BackendStrings::get('add_event') |
| 107 | ] |
| 108 | ]); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (current_user_can('amelia_write_customers')) { |
| 113 | $wpAdminBar->add_node([ |
| 114 | 'parent' => 'amelia-menu', |
| 115 | 'id' => 'amelia-add-customer', |
| 116 | 'title' => BackendStrings::get('add_customer'), |
| 117 | 'href' => admin_url('admin.php?page=wpamelia-customers#/manage'), |
| 118 | 'meta' => [ |
| 119 | 'title' => BackendStrings::get('add_customer') |
| 120 | ] |
| 121 | ]); |
| 122 | } |
| 123 | |
| 124 | if (current_user_can('amelia_read_settings')) { |
| 125 | $wpAdminBar->add_node([ |
| 126 | 'parent' => 'amelia-menu', |
| 127 | 'id' => 'amelia-settings', |
| 128 | 'title' => BackendStrings::get('settings'), |
| 129 | 'href' => admin_url('admin.php?page=wpamelia-settings'), |
| 130 | 'meta' => [ |
| 131 | 'title' => BackendStrings::get('settings') |
| 132 | ] |
| 133 | ]); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the inline SVG icon for Amelia |
| 139 | */ |
| 140 | private function getAmeliaIcon(): string |
| 141 | { |
| 142 | $svgPath1 = 'M246.903 104.295V211.599C246.903 220.45 251.629 228.61 259.285 233.004L352.076 286.197'; |
| 143 | $svgPath1 .= 'C368.474 295.598 388.878 283.725 388.878 264.792V157.996C388.878 149.188 384.194 141.049 '; |
| 144 | $svgPath1 .= '376.582 136.645L283.791 82.9336C267.392 73.4457 246.892 85.3083 246.892 104.284L246.903 104.295Z'; |
| 145 | |
| 146 | $svgPath2 = 'M221.57 104.295V211.599C221.57 220.45 216.844 228.61 209.188 233.004L116.397 286.197'; |
| 147 | $svgPath2 .= 'C99.9988 295.598 79.5951 283.725 79.5951 264.792V157.996C79.5951 149.188 84.2788 141.049 '; |
| 148 | $svgPath2 .= '91.8912 136.645L184.682 82.9336C201.08 73.4457 221.581 85.3083 221.581 104.284L221.57 104.295Z'; |
| 149 | |
| 150 | $svgPath3 = 'M220.666 254.228L127.347 307.723C110.874 317.168 110.82 340.969 127.261 350.479L220.58 404.492'; |
| 151 | $svgPath3 .= 'C228.192 408.896 237.57 408.896 245.172 404.492L338.49 350.479C354.932 340.969 354.878 '; |
| 152 | $svgPath3 .= '317.168 338.404 307.723L245.086 254.228C237.516 249.889 228.235 249.889 220.666 254.228Z'; |
| 153 | |
| 154 | return '<svg width="20" height="20" viewBox="40 62 390 370" fill="none" xmlns="http://www.w3.org/2000/svg" ' |
| 155 | . 'style="vertical-align: middle; position: relative; top: 2px;">' |
| 156 | . '<path d="' . $svgPath1 . '" fill="currentColor"/>' |
| 157 | . '<path d="' . $svgPath2 . '" fill="currentColor"/>' |
| 158 | . '<path d="' . $svgPath3 . '" fill="currentColor"/>' |
| 159 | . '</svg>'; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Enqueue admin bar menu script for both admin and frontend |
| 164 | */ |
| 165 | public static function enqueueScripts() |
| 166 | { |
| 167 | if (self::$scriptAdded) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | self::$scriptAdded = true; |
| 172 | |
| 173 | $enqueueCallback = function () { |
| 174 | if (is_admin_bar_showing() && current_user_can('amelia_read_menu')) { |
| 175 | wp_enqueue_script( |
| 176 | 'amelia-admin-bar-menu', |
| 177 | AMELIA_URL . 'public/js/admin-bar/admin-bar-menu.js', |
| 178 | ['jquery'], |
| 179 | AMELIA_VERSION, |
| 180 | true |
| 181 | ); |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | add_action('admin_enqueue_scripts', $enqueueCallback); |
| 186 | add_action('wp_enqueue_scripts', $enqueueCallback); |
| 187 | add_action('admin_head', [self::class, 'addAdminBarStyles']); |
| 188 | add_action('wp_head', [self::class, 'addAdminBarStyles']); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Add admin bar icon styling |
| 193 | */ |
| 194 | public static function addAdminBarStyles() |
| 195 | { |
| 196 | $isRtl = is_rtl(); |
| 197 | |
| 198 | $css = ' |
| 199 | #wpadminbar #wp-admin-bar-amelia-menu > .ab-item svg { |
| 200 | color: #a7aaad; |
| 201 | } |
| 202 | |
| 203 | #wpadminbar #wp-admin-bar-amelia-menu:hover > .ab-item svg { |
| 204 | color: #72aee6; |
| 205 | } |
| 206 | '; |
| 207 | |
| 208 | // Add RTL-specific styles - rotate arrow 180 degrees |
| 209 | if ($isRtl) { |
| 210 | $css .= ' |
| 211 | /* RTL Support: Flip submenu arrow to point left */ |
| 212 | #wpadminbar li#wp-admin-bar-amelia-add-booking .ab-item .wp-admin-bar-arrow:before { |
| 213 | content: "\f141" !important; |
| 214 | } |
| 215 | '; |
| 216 | } |
| 217 | |
| 218 | echo '<style>' . $css . '</style>'; |
| 219 | } |
| 220 | } |
| 221 |