PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Services / Menu / MenuManager.php

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

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