| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo My Account Content widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Typography; |
| 12 |
use King_Addons\Woo_Builder\Context as Woo_Context; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Renders my account content area. |
| 20 |
*/ |
| 21 |
class Woo_My_Account_Content extends Abstract_My_Account_Widget |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Cached custom endpoints config. |
| 25 |
* |
| 26 |
* @var array<string,array<string,mixed>> |
| 27 |
*/ |
| 28 |
private static array $custom_endpoints = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* Whether hooks were added. |
| 32 |
* |
| 33 |
* @var bool |
| 34 |
*/ |
| 35 |
private static bool $hooks_added = false; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor. |
| 39 |
* |
| 40 |
* @param array<mixed> $data Data. |
| 41 |
* @param array<mixed> $args Args. |
| 42 |
*/ |
| 43 |
public function __construct($data = [], $args = null) |
| 44 |
{ |
| 45 |
parent::__construct($data, $args); |
| 46 |
|
| 47 |
if (!self::$hooks_added) { |
| 48 |
add_action('init', [self::class, 'register_endpoints']); |
| 49 |
add_filter('woocommerce_account_menu_items', [self::class, 'filter_menu_items'], 20); |
| 50 |
self::$hooks_added = true; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public function get_name(): string |
| 55 |
{ |
| 56 |
return 'woo_my_account_content'; |
| 57 |
} |
| 58 |
|
| 59 |
public function get_title(): string |
| 60 |
{ |
| 61 |
return esc_html__('My Account Content', 'king-addons'); |
| 62 |
} |
| 63 |
|
| 64 |
public function get_icon(): string |
| 65 |
{ |
| 66 |
return 'eicon-library-download'; |
| 67 |
} |
| 68 |
|
| 69 |
public function get_categories(): array |
| 70 |
{ |
| 71 |
return ['king-addons-woo-builder']; |
| 72 |
} |
| 73 |
|
| 74 |
public function get_style_depends(): array |
| 75 |
{ |
| 76 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-my-account-content-style']; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Register controls. |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
protected function register_controls(): void |
| 85 |
{ |
| 86 |
$this->start_controls_section( |
| 87 |
'section_content', |
| 88 |
[ |
| 89 |
'label' => esc_html__('Custom Endpoints (Pro)', 'king-addons'), |
| 90 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 91 |
] |
| 92 |
); |
| 93 |
|
| 94 |
$this->add_control( |
| 95 |
'endpoints', |
| 96 |
[ |
| 97 |
'label' => esc_html__('Endpoints', 'king-addons'), |
| 98 |
'type' => Controls_Manager::REPEATER, |
| 99 |
'fields' => [ |
| 100 |
[ |
| 101 |
'name' => 'slug', |
| 102 |
'label' => esc_html__('Slug', 'king-addons'), |
| 103 |
'type' => Controls_Manager::TEXT, |
| 104 |
'placeholder' => 'my-endpoint', |
| 105 |
], |
| 106 |
[ |
| 107 |
'name' => 'label', |
| 108 |
'label' => esc_html__('Menu label', 'king-addons'), |
| 109 |
'type' => Controls_Manager::TEXT, |
| 110 |
'placeholder' => esc_html__('My Endpoint', 'king-addons'), |
| 111 |
], |
| 112 |
[ |
| 113 |
'name' => 'position', |
| 114 |
'label' => esc_html__('Menu position', 'king-addons'), |
| 115 |
'type' => Controls_Manager::NUMBER, |
| 116 |
'default' => 90, |
| 117 |
], |
| 118 |
[ |
| 119 |
'name' => 'content', |
| 120 |
'label' => esc_html__('Content', 'king-addons'), |
| 121 |
'type' => Controls_Manager::WYSIWYG, |
| 122 |
], |
| 123 |
], |
| 124 |
'title_field' => '{{ label }} ({{ slug }})', |
| 125 |
] |
| 126 |
); |
| 127 |
|
| 128 |
$this->end_controls_section(); |
| 129 |
|
| 130 |
$this->start_controls_section( |
| 131 |
'section_style', |
| 132 |
[ |
| 133 |
'label' => esc_html__('Style', 'king-addons'), |
| 134 |
'tab' => Controls_Manager::TAB_STYLE, |
| 135 |
] |
| 136 |
); |
| 137 |
|
| 138 |
$this->add_group_control( |
| 139 |
Group_Control_Typography::get_type(), |
| 140 |
[ |
| 141 |
'name' => 'text_typography', |
| 142 |
'selector' => '{{WRAPPER}} .ka-woo-my-account-content', |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
$this->add_control( |
| 147 |
'text_color', |
| 148 |
[ |
| 149 |
'label' => esc_html__('Text Color', 'king-addons'), |
| 150 |
'type' => Controls_Manager::COLOR, |
| 151 |
'selectors' => [ |
| 152 |
'{{WRAPPER}} .ka-woo-my-account-content' => 'color: {{VALUE}};', |
| 153 |
], |
| 154 |
] |
| 155 |
); |
| 156 |
|
| 157 |
$this->end_controls_section(); |
| 158 |
} |
| 159 |
|
| 160 |
protected function render(): void |
| 161 |
{ |
| 162 |
if (!Woo_Context::maybe_render_context_notice('my_account')) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
if (!$this->should_render()) { |
| 167 |
$this->render_missing_account_notice(); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
$settings = $this->get_settings_for_display(); |
| 172 |
$can_pro = king_addons_can_use_pro(); |
| 173 |
|
| 174 |
if ($can_pro && !empty($settings['endpoints']) && is_array($settings['endpoints'])) { |
| 175 |
foreach ($settings['endpoints'] as $ep) { |
| 176 |
$slug = sanitize_title($ep['slug'] ?? ''); |
| 177 |
if (empty($slug)) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
self::$custom_endpoints[$slug] = [ |
| 181 |
'label' => $ep['label'] ?? $slug, |
| 182 |
'content' => $ep['content'] ?? '', |
| 183 |
'position' => isset($ep['position']) ? (int) $ep['position'] : 90, |
| 184 |
]; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if ($this->maybe_render_login_form()) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
echo '<div class="ka-woo-my-account-content">'; |
| 193 |
woocommerce_output_all_notices(); |
| 194 |
woocommerce_account_content(); |
| 195 |
echo '</div>'; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Register rewrite endpoints for custom ones. |
| 200 |
* |
| 201 |
* @return void |
| 202 |
*/ |
| 203 |
public static function register_endpoints(): void |
| 204 |
{ |
| 205 |
if (empty(self::$custom_endpoints)) { |
| 206 |
return; |
| 207 |
} |
| 208 |
foreach (array_keys(self::$custom_endpoints) as $slug) { |
| 209 |
add_rewrite_endpoint($slug, EP_ROOT | EP_PAGES); |
| 210 |
$hook = 'woocommerce_account_' . $slug . '_endpoint'; |
| 211 |
add_action( |
| 212 |
$hook, |
| 213 |
static function () use ($slug): void { |
| 214 |
$data = self::$custom_endpoints[$slug] ?? []; |
| 215 |
$content = $data['content'] ?? ''; |
| 216 |
if ($content) { |
| 217 |
echo '<div class="ka-woo-my-account-content__custom">' . do_shortcode(wp_kses_post($content)) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 218 |
} else { |
| 219 |
echo '<div class="ka-woo-my-account-content__custom">' . esc_html__('Content not set.', 'king-addons') . '</div>'; |
| 220 |
} |
| 221 |
} |
| 222 |
); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Inject custom endpoints into account menu. |
| 228 |
* |
| 229 |
* @param array<string,string> $items Menu items. |
| 230 |
* |
| 231 |
* @return array<string,string> |
| 232 |
*/ |
| 233 |
public static function filter_menu_items(array $items): array |
| 234 |
{ |
| 235 |
if (empty(self::$custom_endpoints)) { |
| 236 |
return $items; |
| 237 |
} |
| 238 |
$ordered = []; |
| 239 |
foreach ($items as $slug => $label) { |
| 240 |
$ordered[ sprintf('%05d:%s', 50, $slug) ] = [$slug, $label]; |
| 241 |
} |
| 242 |
foreach (self::$custom_endpoints as $slug => $data) { |
| 243 |
$pos = $data['position'] ?? 90; |
| 244 |
$label = $data['label'] ?? $slug; |
| 245 |
$ordered[ sprintf('%05d:%s', (int) $pos, $slug) ] = [$slug, $label]; |
| 246 |
} |
| 247 |
ksort($ordered, SORT_NATURAL); |
| 248 |
$result = []; |
| 249 |
foreach ($ordered as $pack) { |
| 250 |
[$slug, $label] = $pack; |
| 251 |
$result[$slug] = $label; |
| 252 |
} |
| 253 |
return $result; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|