| @@ -1,0 +1,118 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\WpInit; | |
| 4 | + | |
| 5 | +class Menu | |
| 6 | +{ | |
| 7 | + private Router $router; | |
| 8 | + | |
| 9 | + public function __construct($router) | |
| 10 | + { | |
| 11 | + $this->router = $router; | |
| 12 | + | |
| 13 | + if (defined('WP_ADMIN') && WP_ADMIN) { | |
| 14 | + // Add AcyMailing menu in the back-end's left menu of WordPress | |
| 15 | + add_action('admin_menu', [$this, 'addMenus'], 99); | |
| 16 | + } | |
| 17 | + | |
| 18 | + // Add "settings" link for acym on plugins listing | |
| 19 | + add_filter('plugin_action_links_'.plugin_basename(dirname(__DIR__).'/index.php'), [$this, 'addPluginLinks']); | |
| 20 | + } | |
| 21 | + | |
| 22 | + // Add AcyMailing menu to WP left menu and define controllers | |
| 23 | + public function addMenus() | |
| 24 | + { | |
| 25 | + // Everyone in WordPress can read, the real test is made bellow | |
| 26 | + $capability = 'read'; | |
| 27 | + | |
| 28 | + // Make sure the user is allowed to see admin features | |
| 29 | + $config = acym_config(); | |
| 30 | + $allowedGroups = explode(',', $config->get('wp_access', 'administrator')); | |
| 31 | + $userGroups = acym_getGroupsByUser(); | |
| 32 | + | |
| 33 | + $allowed = false; | |
| 34 | + foreach ($userGroups as $oneGroup) { | |
| 35 | + if ($oneGroup == 'administrator' || in_array($oneGroup, $allowedGroups)) { | |
| 36 | + $allowed = true; | |
| 37 | + break; | |
| 38 | + } | |
| 39 | + } | |
| 40 | + if (!$allowed) return; | |
| 41 | + | |
| 42 | + // Add the Acy menu items to the WP menu | |
| 43 | + $svg = acym_fileGetContent(ACYM_IMAGES.'logos/logo_grey.svg'); | |
| 44 | + add_menu_page( | |
| 45 | + acym_translation('ACYM_DASHBOARD'), | |
| 46 | + 'AcyMailing', | |
| 47 | + $capability, | |
| 48 | + ACYM_COMPONENT.'_dashboard', | |
| 49 | + [$this->router, 'router'], | |
| 50 | + 'data:image/svg+xml;base64,'.base64_encode( | |
| 51 | + $svg | |
| 52 | + ), | |
| 53 | + 42 | |
| 54 | + ); | |
| 55 | + | |
| 56 | + $menuExtra = []; | |
| 57 | + $menus = [ | |
| 58 | + 'ACYM_SUBSCRIPTION_FORMS' => 'forms', | |
| 59 | + 'ACYM_SUBSCRIBERS' => 'users', | |
| 60 | + 'ACYM_CUSTOM_FIELDS' => 'fields', | |
| 61 | + 'ACYM_LISTS' => 'lists', | |
| 62 | + ]; | |
| 63 | + $menus['ACYM_EMAILS'] = 'campaigns'; | |
| 64 | + $menus['ACYM_TEMPLATES'] = 'mails'; | |
| 65 | + $menus['ACYM_EMAILS_OVERRIDE'] = 'override'; | |
| 66 | + $menus['ACYM_QUEUE'] = 'queue'; | |
| 67 | + $menus['ACYM_STATISTICS'] = 'stats'; | |
| 68 | + $menus['ACYM_ADD_ONS'] = 'plugins'; | |
| 69 | + $menus['ACYM_CONFIGURATION'] = 'configuration'; | |
| 70 | + | |
| 71 | + if (!acym_level(ACYM_ESSENTIAL)) { | |
| 72 | + $menus['ACYM_GOPRO'] = 'gopro'; | |
| 73 | + $menuExtra['ACYM_GOPRO']['icon'] = '<i class="acymicon-star"></i>'; | |
| 74 | + } | |
| 75 | + | |
| 76 | + foreach ($menus as $title => $ctrl) { | |
| 77 | + if (!acym_isAllowed($ctrl)) continue; | |
| 78 | + | |
| 79 | + $text = acym_translation($title); | |
| 80 | + if (!empty($menuExtra[$title]['icon'])) $text .= ' '.$menuExtra[$title]['icon']; | |
| 81 | + add_submenu_page( | |
| 82 | + ACYM_COMPONENT.'_dashboard', | |
| 83 | + acym_translation($title), | |
| 84 | + $text, | |
| 85 | + $capability, | |
| 86 | + ACYM_COMPONENT.'_'.$ctrl, | |
| 87 | + [$this->router, 'router'] | |
| 88 | + ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + // Declare invisible menus | |
| 92 | + $controllers = ['dynamics', 'file', 'language']; | |
| 93 | + foreach ($controllers as $oneCtrl) { | |
| 94 | + add_submenu_page( | |
| 95 | + ACYM_COMPONENT.'_'.$oneCtrl, | |
| 96 | + $oneCtrl, | |
| 97 | + $oneCtrl, | |
| 98 | + $capability, | |
| 99 | + ACYM_COMPONENT.'_'.$oneCtrl, | |
| 100 | + [$this->router, 'router'] | |
| 101 | + ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + // In WordPress, the first submenu is called "AcyMailing" instead of "Dashboard" so we rename it manually | |
| 105 | + global $submenu; | |
| 106 | + if (isset($submenu[ACYM_COMPONENT.'_dashboard'])) { | |
| 107 | + $submenu[ACYM_COMPONENT.'_dashboard'][0][0] = acym_translation('ACYM_DASHBOARD'); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + // Add links on the plugins listing | |
| 112 | + public function addPluginLinks($links): array | |
| 113 | + { | |
| 114 | + $settings_link = '<a href="admin.php?page='.ACYM_COMPONENT.'_configuration">'.acym_translation('ACYM_SETTINGS').'</a>'; | |
| 115 | + | |
| 116 | + return array_merge([$settings_link], $links); | |
| 117 | + } | |
| 118 | +} | |