| 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 'eicon-menu-bar'; |
| 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 |
$offset = isset($settings['sticky_offset']) ? (int) $settings['sticky_offset'] : 20; |
| 234 |
$this->add_render_attribute('nav', 'style', 'position: sticky; top: ' . $offset . 'px;'); |
| 235 |
} |
| 236 |
|
| 237 |
$items = wc_get_account_menu_items(); |
| 238 |
$custom = $settings['menu_items'] ?? []; |
| 239 |
$custom_links = []; |
| 240 |
if (!empty($custom) && is_array($custom)) { |
| 241 |
$ordered = []; |
| 242 |
foreach ($custom as $item) { |
| 243 |
$endpoint = sanitize_title($item['endpoint'] ?? ''); |
| 244 |
if ('' === $endpoint) { |
| 245 |
continue; |
| 246 |
} |
| 247 |
if (!empty($item['hide']) && 'yes' === $item['hide']) { |
| 248 |
unset($items[$endpoint]); |
| 249 |
continue; |
| 250 |
} |
| 251 |
if (isset($items[$endpoint]) && isset($item['label']) && $item['label'] !== '') { |
| 252 |
$items[$endpoint] = $item['label']; |
| 253 |
} |
| 254 |
$position = isset($item['position']) ? (int) $item['position'] : 20; |
| 255 |
if (($item['type'] ?? 'endpoint') === 'custom') { |
| 256 |
$custom_links[$position . ':' . $endpoint] = [ |
| 257 |
'label' => $item['label'] ?: $endpoint, |
| 258 |
'url' => $item['custom_url'] ?? '#', |
| 259 |
]; |
| 260 |
continue; |
| 261 |
} |
| 262 |
$ordered[$position . ':' . $endpoint] = [$endpoint, $items[$endpoint] ?? ($item['label'] ?? $endpoint)]; |
| 263 |
} |
| 264 |
if (!empty($ordered)) { |
| 265 |
ksort($ordered, SORT_NATURAL); |
| 266 |
$new_items = []; |
| 267 |
foreach ($ordered as $pack) { |
| 268 |
[$endpoint, $label] = $pack; |
| 269 |
$new_items[$endpoint] = $label; |
| 270 |
} |
| 271 |
// append any not mentioned endpoints at the end |
| 272 |
foreach ($items as $endpoint => $label) { |
| 273 |
if (!isset($new_items[$endpoint])) { |
| 274 |
$new_items[$endpoint] = $label; |
| 275 |
} |
| 276 |
} |
| 277 |
$items = $new_items; |
| 278 |
} |
| 279 |
} |
| 280 |
$counts = [ |
| 281 |
'orders' => function_exists('wc_get_customer_order_count') ? wc_get_customer_order_count(get_current_user_id()) : 0, |
| 282 |
'downloads' => function_exists('wc_get_customer_available_downloads') ? count(wc_get_customer_available_downloads(get_current_user_id())) : 0, |
| 283 |
]; |
| 284 |
|
| 285 |
echo '<nav ' . $this->get_render_attribute_string('nav') . ' data-ka-myaccount-nav>'; |
| 286 |
echo '<ul>'; |
| 287 |
foreach ($items as $endpoint => $label) { |
| 288 |
$classes = wc_get_account_menu_item_classes($endpoint); |
| 289 |
$icon = $show_icons ? '<span class="ka-myaccount-nav__icon" aria-hidden="true">•</span>' : ''; |
| 290 |
$count = ''; |
| 291 |
if ($show_counts && in_array($endpoint, ['orders', 'downloads'], true)) { |
| 292 |
$val = $counts[$endpoint] ?? 0; |
| 293 |
$count = '<span class="ka-myaccount-nav__count">' . esc_html((string) $val) . '</span>'; |
| 294 |
} |
| 295 |
printf( |
| 296 |
'<li class="%s"><a href="%s">%s<span class="ka-myaccount-nav__label">%s</span>%s</a></li>', |
| 297 |
esc_attr($classes), |
| 298 |
esc_url(wc_get_account_endpoint_url($endpoint)), |
| 299 |
$icon, |
| 300 |
esc_html($label), |
| 301 |
$count |
| 302 |
); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 303 |
} |
| 304 |
if (!empty($custom_links)) { |
| 305 |
ksort($custom_links, SORT_NATURAL); |
| 306 |
foreach ($custom_links as $data) { |
| 307 |
printf( |
| 308 |
'<li class="ka-myaccount-nav__custom"><a href="%s"><span class="ka-myaccount-nav__label">%s</span></a></li>', |
| 309 |
esc_url($data['url']), |
| 310 |
esc_html($data['label']) |
| 311 |
); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 312 |
} |
| 313 |
} |
| 314 |
echo '</ul>'; |
| 315 |
echo '</nav>'; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|