PluginProbe
Ultimate Store Kit / 3.1.2
Ultimate Store Kit v3.1.2
3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.0.7 3.0.5 3.0.4 3.0.3 3.0.2 trunk 1.5.0 1.5.1 1.5.2 1.6.1 1.6.2 1.6.3 1.6.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 92 releases
ultimate-store-kit / src / admin / utils.js

utils.js in Ultimate Store Kit 3.1.2, at src/admin/utils.js

193 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from '@wordpress/i18n';
2 import { applyFilters } from '@wordpress/hooks';
3
4 /**
5 * Whether a widget counts as enabled in the admin (saved "on" plus Pro license
6 * and required plugin active — same rules as the widget cards).
7 *
8 * @param {object} widget Widget definition from PHP (may include dependency).
9 * @param {string|undefined} savedValue Value from settings for this widget key.
10 * @param {boolean} isPro Licensed Pro user.
11 * @returns {boolean}
12 */
13 export const isWidgetEffectivelyOn = (widget, savedValue, isPro) => {
14 if (widget.widget_type === 'pro' && !isPro) {
15 return false;
16 }
17 const dep = widget.dependency;
18 if (dep && (!dep.isInstalled || !dep.isActive)) {
19 return false;
20 }
21 if (savedValue !== undefined && savedValue !== null) {
22 return savedValue === 'on';
23 }
24 return widget.default === 'on';
25 };
26
27 const defaultPlaceholders = [
28 {
29 id: 'currency-switcher',
30 label: __('Currency Switcher', 'ultimate-store-kit'),
31 description: __('Enable to allow currency switching on your store.', 'ultimate-store-kit'),
32 icon: 'currency-switcher',
33 badge: 'Pro',
34 proPlaceholder: true,
35 },
36 {
37 id: 'variation-swatches',
38 label: __('Variation Swatches', 'ultimate-store-kit'),
39 description: __('Enable to display product variation swatches on your store.', 'ultimate-store-kit'),
40 icon: 'variation-swatches',
41 badge: 'Pro',
42 proPlaceholder: true,
43 },
44 {
45 id: 'live-visitor-count',
46 label: __('Live Visitor Count', 'ultimate-store-kit'),
47 description: __('Display live visitor count on product pages to create urgency.', 'ultimate-store-kit'),
48 icon: 'live-visitor-count',
49 badge: 'Pro',
50 proPlaceholder: true,
51 },
52 {
53 id: 'upsell-cross-sell',
54 label: __('Upsell & Cross-Sell', 'ultimate-store-kit'),
55 description: __('Display upsell and cross-sell products on product pages to increase sales.', 'ultimate-store-kit'),
56 icon: 'upsell-cross-sell',
57 badge: 'Pro',
58 proPlaceholder: true,
59 },
60 ];
61
62 const getPlaceholderModules = () =>
63 applyFilters('usk.admin.placeholderModules', defaultPlaceholders);
64
65 const navItems = [
66 {
67 id: 'dashboard',
68 group: __('Dashboard', 'ultimate-store-kit'),
69 items: [
70 {
71 id: 'welcome',
72 label: __('Welcome', 'ultimate-store-kit'),
73 icon: 'home',
74 },
75 ],
76 },
77 {
78 id: 'widgets',
79 group: __('Widgets', 'ultimate-store-kit'),
80 items: [
81 {
82 id: 'woocommerce-widgets',
83 label: __('WooCommerce', 'ultimate-store-kit'),
84 icon: 'woocommerce',
85 },
86 {
87 id: 'edd-widgets',
88 label: __('EDD', 'ultimate-store-kit'),
89 icon: 'edd',
90 },
91 {
92 id: 'other-widgets',
93 label: __('Others', 'ultimate-store-kit'),
94 icon: 'other',
95 },
96 ],
97 },
98 {
99 id: 'modules',
100 group: __('Modules', 'ultimate-store-kit'),
101 items: [],
102 },
103 {
104 id: 'support',
105 group: __('Support', 'ultimate-store-kit'),
106 items: [
107 {
108 id: 'get-pro',
109 label: __('Get Pro', 'ultimate-store-kit'),
110 icon: 'star',
111 },
112 {
113 id: 'about',
114 label: __('About & Info', 'ultimate-store-kit'),
115 icon: 'info',
116 },
117 ],
118 },
119 ];
120
121 const groupHeadingClass =
122 'mb-3 px-2 text-xs font-semibold uppercase tracking-widest text-gray-400';
123
124 const getModuleSidebarIcon = (group) => {
125 const id = (group.id || '').toLowerCase();
126 const label = (group.label || '').toLowerCase();
127 const haystack = `${id} ${label}`;
128 if (haystack.includes('currency')) {
129 return 'currency-switcher';
130 }
131 if (haystack.includes('swatch') || haystack.includes('variation')) {
132 return 'variation-swatches';
133 }
134 if (haystack.includes('visitor') || haystack.includes('live')) {
135 return 'live-visitor-count';
136 }
137 return 'grid';
138 };
139
140 const getAdminBarHeight = () =>
141 document.getElementById('wpadminbar')?.offsetHeight || 0;
142
143 const getNavItems = () => applyFilters('usk.admin.navItems', navItems);
144
145 const buildSidebarSections = ({
146 isPro,
147 settingsGroups = [],
148 }) => {
149 const sections = getNavItems();
150 const placeholders = getPlaceholderModules();
151 const placeholderIds = placeholders.map((p) => p.id);
152
153 return sections.map((section) => {
154 if (section.id === 'modules') {
155 const dynamicItems = settingsGroups.map((group) => ({
156 id: group.id,
157 label: group.label,
158 icon: getModuleSidebarIcon(group),
159 }));
160
161 if (isPro) {
162 // Licensed: show real pro module items (injected via filters),
163 // hide placeholder duplicates.
164 return {
165 ...section,
166 items: [...dynamicItems, ...section.items],
167 };
168 }
169
170 // Not licensed: show placeholders, filter out any filter-injected
171 // items whose IDs overlap with placeholders to avoid duplicates.
172 const filtered = section.items.filter(
173 (item) => !placeholderIds.includes(item.id)
174 );
175 return {
176 ...section,
177 items: [...dynamicItems, ...placeholders, ...filtered],
178 };
179 }
180 return section;
181 });
182 };
183
184 export {
185 buildSidebarSections,
186 getAdminBarHeight,
187 getModuleSidebarIcon,
188 getNavItems,
189 getPlaceholderModules,
190 groupHeadingClass,
191 navItems,
192 };
193