PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.84
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.84
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / widgets / Woo_My_Account_Navigation / Woo_My_Account_Navigation.php

Woo_My_Account_Navigation.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.84, at includes/widgets/Woo_My_Account_Navigation/Woo_My_Account_Navigation.php

332 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Woo My Account Navigation widget.
4 *
5 * @package King_Addons
6 */
7
8 namespace King_Addons;
9
10 use Elementor\Controls_Manager;
11 use Elementor\Group_Control_Border;
12 use Elementor\Group_Control_Box_Shadow;
13 use Elementor\Group_Control_Typography;
14 use King_Addons\Woo_Builder\Context as Woo_Context;
15
16 if (!defined('ABSPATH')) {
17 exit;
18 }
19
20 /**
21 * Renders my account navigation.
22 */
23 class Woo_My_Account_Navigation extends Abstract_My_Account_Widget
24 {
25 public function get_name(): string
26 {
27 return 'woo_my_account_navigation';
28 }
29
30 public function get_title(): string
31 {
32 return esc_html__('My Account Navigation', 'king-addons');
33 }
34
35 public function get_icon(): string
36 {
37 return 'king-addons-icon king-addons-woo-my-account-navigation';
38 }
39
40 public function get_categories(): array
41 {
42 return ['king-addons-woo-builder'];
43 }
44
45 public function get_style_depends(): array
46 {
47 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-my-account-navigation-style'];
48 }
49
50 protected function register_controls(): void
51 {
52 $this->start_controls_section(
53 'section_content',
54 [
55 'label' => esc_html__('Content', 'king-addons'),
56 'tab' => Controls_Manager::TAB_CONTENT,
57 ]
58 );
59
60 $this->add_control(
61 'sticky',
62 [
63 'label' => sprintf(__('Sticky (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
64 'type' => Controls_Manager::SWITCHER,
65 'return_value' => 'yes',
66 ]
67 );
68
69 $this->add_control(
70 'sticky_offset',
71 [
72 'label' => sprintf(__('Sticky Offset (px) (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
73 'type' => Controls_Manager::NUMBER,
74 'default' => 20,
75 'condition' => [
76 'sticky' => 'yes',
77 ],
78 ]
79 );
80
81 $this->add_control(
82 'show_icons',
83 [
84 'label' => esc_html__('Show Icons', 'king-addons'),
85 'type' => Controls_Manager::SWITCHER,
86 'default' => '',
87 ]
88 );
89
90 $this->add_control(
91 'show_counts',
92 [
93 'label' => esc_html__('Show Counts (orders/downloads)', 'king-addons'),
94 'type' => Controls_Manager::SWITCHER,
95 'default' => '',
96 ]
97 );
98
99 $this->add_control(
100 'menu_items',
101 [
102 'label' => esc_html__('Menu Items (reorder/hide/label)', 'king-addons'),
103 'type' => Controls_Manager::REPEATER,
104 'fields' => [
105 [
106 'name' => 'endpoint',
107 'label' => esc_html__('Endpoint / slug', 'king-addons'),
108 'type' => Controls_Manager::TEXT,
109 'placeholder' => 'orders',
110 ],
111 [
112 'name' => 'label',
113 'label' => esc_html__('Custom label', 'king-addons'),
114 'type' => Controls_Manager::TEXT,
115 ],
116 [
117 'name' => 'position',
118 'label' => esc_html__('Order', 'king-addons'),
119 'type' => Controls_Manager::NUMBER,
120 'default' => 20,
121 ],
122 [
123 'name' => 'hide',
124 'label' => esc_html__('Hide', 'king-addons'),
125 'type' => Controls_Manager::SWITCHER,
126 'return_value' => 'yes',
127 ],
128 [
129 'name' => 'type',
130 'label' => esc_html__('Type', 'king-addons'),
131 'type' => Controls_Manager::SELECT,
132 'options' => [
133 'endpoint' => esc_html__('Endpoint', 'king-addons'),
134 'custom' => esc_html__('Custom URL', 'king-addons'),
135 ],
136 'default' => 'endpoint',
137 ],
138 [
139 'name' => 'custom_url',
140 'label' => esc_html__('Custom URL', 'king-addons'),
141 'type' => Controls_Manager::TEXT,
142 'placeholder' => 'https://example.com',
143 'condition' => [
144 'type' => 'custom',
145 ],
146 ],
147 ],
148 'title_field' => '{{ endpoint }}',
149 ]
150 );
151
152 $this->end_controls_section();
153
154 $this->start_controls_section(
155 'section_style',
156 [
157 'label' => esc_html__('Links', 'king-addons'),
158 'tab' => Controls_Manager::TAB_STYLE,
159 ]
160 );
161
162 $this->add_group_control(
163 Group_Control_Typography::get_type(),
164 [
165 'name' => 'links_typography',
166 'selector' => '{{WRAPPER}} .woocommerce-MyAccount-navigation',
167 ]
168 );
169
170 $this->add_control(
171 'link_color',
172 [
173 'label' => esc_html__('Link Color', 'king-addons'),
174 'type' => Controls_Manager::COLOR,
175 'selectors' => [
176 '{{WRAPPER}} .woocommerce-MyAccount-navigation a' => 'color: {{VALUE}};',
177 ],
178 ]
179 );
180
181 $this->add_control(
182 'link_color_active',
183 [
184 'label' => esc_html__('Active Color', 'king-addons'),
185 'type' => Controls_Manager::COLOR,
186 'selectors' => [
187 '{{WRAPPER}} .woocommerce-MyAccount-navigation .is-active a' => 'color: {{VALUE}};',
188 ],
189 ]
190 );
191
192 $this->add_group_control(
193 Group_Control_Border::get_type(),
194 [
195 'name' => 'nav_border',
196 'selector' => '{{WRAPPER}} .woocommerce-MyAccount-navigation',
197 ]
198 );
199
200 $this->add_group_control(
201 Group_Control_Box_Shadow::get_type(),
202 [
203 'name' => 'nav_shadow',
204 'selector' => '{{WRAPPER}} .woocommerce-MyAccount-navigation',
205 ]
206 );
207
208 $this->end_controls_section();
209 }
210
211 protected function render(): void
212 {
213 if (!Woo_Context::maybe_render_context_notice('my_account')) {
214 return;
215 }
216
217 $in_builder = class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('my_account');
218 if (!function_exists('is_account_page') || (!is_account_page() && !$in_builder)) {
219 return;
220 }
221
222 if (!function_exists('wc_get_account_menu_items')) {
223 return;
224 }
225
226 $settings = $this->get_settings_for_display();
227 $can_pro = king_addons_can_use_pro();
228 $show_icons = ($settings['show_icons'] ?? '') === 'yes';
229 $show_counts = ($settings['show_counts'] ?? '') === 'yes';
230
231 $this->add_render_attribute('nav', 'class', 'woocommerce-MyAccount-navigation');
232 if (!empty($settings['sticky']) && $can_pro) {
233 // A cleared field is '' rather than unset, which pinned the menu to
234 // the very top instead of using the default offset.
235 $offset = max(0, (int) (($settings['sticky_offset'] ?? null) ?: 20));
236 $this->add_render_attribute('nav', 'style', 'position: sticky; top: ' . $offset . 'px;');
237 }
238
239 $items = wc_get_account_menu_items();
240 $custom = $settings['menu_items'] ?? [];
241 $custom_links = [];
242 if (!empty($custom) && is_array($custom)) {
243 $ordered = [];
244 foreach ($custom as $item) {
245 $endpoint = sanitize_title($item['endpoint'] ?? '');
246 if ('' === $endpoint) {
247 continue;
248 }
249 if (!empty($item['hide']) && 'yes' === $item['hide']) {
250 unset($items[$endpoint]);
251 continue;
252 }
253 if (isset($items[$endpoint]) && isset($item['label']) && $item['label'] !== '') {
254 $items[$endpoint] = $item['label'];
255 }
256 $position = isset($item['position']) ? (int) $item['position'] : 20;
257 if (($item['type'] ?? 'endpoint') === 'custom') {
258 // A URL control stores an array; take its url so esc_url()
259 // never receives one and throws.
260 $custom_url = $item['custom_url'] ?? '#';
261 if (is_array($custom_url)) {
262 $custom_url = $custom_url['url'] ?? '#';
263 }
264 $custom_links[$position . ':' . $endpoint] = [
265 'label' => $item['label'] ?: $endpoint,
266 'url' => is_scalar($custom_url) ? (string) $custom_url : '#',
267 ];
268 continue;
269 }
270 $ordered[$position . ':' . $endpoint] = [$endpoint, $items[$endpoint] ?? ($item['label'] ?? $endpoint)];
271 }
272 if (!empty($ordered)) {
273 ksort($ordered, SORT_NATURAL);
274 $new_items = [];
275 foreach ($ordered as $pack) {
276 [$endpoint, $label] = $pack;
277 $new_items[$endpoint] = $label;
278 }
279 // append any not mentioned endpoints at the end
280 foreach ($items as $endpoint => $label) {
281 if (!isset($new_items[$endpoint])) {
282 $new_items[$endpoint] = $label;
283 }
284 }
285 $items = $new_items;
286 }
287 }
288 $counts = [
289 'orders' => function_exists('wc_get_customer_order_count') ? wc_get_customer_order_count(get_current_user_id()) : 0,
290 'downloads' => function_exists('wc_get_customer_available_downloads') ? count(wc_get_customer_available_downloads(get_current_user_id())) : 0,
291 ];
292
293 echo '<nav ' . $this->get_render_attribute_string('nav') . ' data-ka-myaccount-nav>';
294 echo '<ul>';
295 foreach ($items as $endpoint => $label) {
296 $classes = wc_get_account_menu_item_classes($endpoint);
297 $icon = $show_icons ? '<span class="ka-myaccount-nav__icon" aria-hidden="true">•</span>' : '';
298 $count = '';
299 if ($show_counts && in_array($endpoint, ['orders', 'downloads'], true)) {
300 $val = $counts[$endpoint] ?? 0;
301 $count = '<span class="ka-myaccount-nav__count">' . esc_html((string) $val) . '</span>';
302 }
303 printf(
304 '<li class="%s"><a href="%s">%s<span class="ka-myaccount-nav__label">%s</span>%s</a></li>',
305 esc_attr($classes),
306 esc_url(wc_get_account_endpoint_url($endpoint)),
307 $icon,
308 esc_html($label),
309 $count
310 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
311 }
312 if (!empty($custom_links)) {
313 ksort($custom_links, SORT_NATURAL);
314 foreach ($custom_links as $data) {
315 printf(
316 '<li class="ka-myaccount-nav__custom"><a href="%s"><span class="ka-myaccount-nav__label">%s</span></a></li>',
317 esc_url($data['url']),
318 esc_html($data['label'])
319 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
320 }
321 }
322 echo '</ul>';
323 echo '</nav>';
324 }
325 }
326
327
328
329
330
331
332