PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Menu / MenuManager.php

MenuManager.php in The Innovative Form Builder – IvyForms 1.4, at backend/src/Services/Menu/MenuManager.php

154 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace IvyForms\Services\Menu;
9
10 use Closure;
11 use IvyForms\Services\Permissions\IvyFormsAccess;
12
13 class MenuManager
14 {
15 private MenuMapper $menu;
16 private MenuService $menuService;
17
18 public function __construct(
19 MenuService $menuService,
20 MenuMapper $menu
21 ) {
22 $this->menuService = $menuService; // Dependency injected by PHP-DI
23 $this->menu = $menu;
24 }
25
26 public function registerMenus(): void
27 {
28 // Hook into WordPress 'admin_menu' action
29 add_action('admin_menu', [$this, 'addOptionsPages']);
30
31 // Add admin CSS for upgrade menu styling
32 add_action('admin_enqueue_scripts', [$this, 'addUpgradeMenuStyles']);
33 }
34
35 public function addOptionsPages(): void
36 {
37 add_menu_page(
38 'IvyForms',
39 'IvyForms',
40 IvyFormsAccess::CAP_ACCESS_PLUGIN,
41 'ivyforms',
42 null, // @phpstan-ignore argument.type
43 IVYFORMS_URL . 'frontend/src/assets/images/logos/logo-only-admin.svg'
44 );
45
46 foreach (($this->menu)() as $menu) {
47 $this->handleMenuItem($menu);
48 }
49
50 remove_submenu_page('ivyforms', 'ivyforms');
51
52 // Add JavaScript to make upgrade menu open in new tab
53 add_action('admin_footer', [$this, 'addUpgradeMenuScript']);
54 }
55
56 /**
57 *
58 * @param mixed[] $menu
59 * */
60 public function handleMenuItem(array $menu): void
61 {
62 // For external links (like upgrade menu), just add without callback
63 if (isset($menu['isExternal']) && $menu['isExternal']) {
64 add_submenu_page(
65 $menu['parentSlug'],
66 $menu['pageTitle'],
67 $menu['menuTitle'],
68 $menu['capability'],
69 $menu['menuSlug'],
70 ''
71 );
72 return;
73 }
74
75 $this->addSubmenuPage(
76 $menu['parentSlug'],
77 $menu['pageTitle'],
78 $menu['menuTitle'],
79 $menu['capability'],
80 $menu['menuSlug'],
81 function () use ($menu) {
82 $this->menuService->render($menu['menuSlug']);
83 }
84 );
85 }
86
87 private function addSubmenuPage(
88 string $parentSlug,
89 string $pageTitle,
90 string $menuTitle,
91 string $capability,
92 string $menuSlug,
93 Closure $function
94 ): void {
95 add_submenu_page(
96 $parentSlug,
97 $pageTitle,
98 $menuTitle,
99 $capability,
100 $menuSlug,
101 $function
102 );
103 }
104
105 /**
106 * Add CSS styling for upgrade menu
107 */
108 public function addUpgradeMenuStyles(): void
109 {
110 // Register an empty / dummy stylesheet (NO file needed)
111 wp_register_style('ivyforms-admin-menu', false);
112 wp_enqueue_style('ivyforms-admin-menu');
113
114 $customCss = '
115 #adminmenu .ivyforms-upgrade-to-pro {
116 display: flex;
117 align-items: flex-start;
118 font-weight: 600 !important;
119 color: #ffffff !important;
120 }
121 #adminmenu .ivyforms-upgrade-to-pro svg {
122 width: 18px;
123 height: 16px;
124 flex-shrink: 0;
125 }
126 #adminmenu li a:has(.ivyforms-upgrade-to-pro) {
127 background-color: #F69D0B !important;
128 }
129 #adminmenu li a:has(.ivyforms-upgrade-to-pro):hover {
130 background-color: #E08D00 !important;
131 }
132 ';
133 wp_add_inline_style('ivyforms-admin-menu', $customCss);
134 }
135
136 /**
137 * Add JavaScript to make upgrade menu link open in new tab
138 */
139 public function addUpgradeMenuScript(): void
140 {
141 ?>
142 <script>
143 (function() {
144 const upgradeLink = document.querySelector('#adminmenu a[href*="ivyforms.com/pricing"]');
145 if (upgradeLink) {
146 upgradeLink.setAttribute('target', '_blank');
147 upgradeLink.setAttribute('rel', 'noopener nofollow noreferrer');
148 }
149 })();
150 </script>
151 <?php
152 }
153 }
154