PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
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.0, at backend/src/Services/Menu/MenuManager.php

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