| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons\Wishlist; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Handles HTML rendering for wishlist components. |
| 11 |
*/ |
| 12 |
class Wishlist_Renderer |
| 13 |
{ |
| 14 |
private Wishlist_Service $service; |
| 15 |
|
| 16 |
/** |
| 17 |
* Setup renderer with service dependency. |
| 18 |
* |
| 19 |
* @param Wishlist_Service $service Wishlist service instance. |
| 20 |
*/ |
| 21 |
public function __construct(Wishlist_Service $service) |
| 22 |
{ |
| 23 |
$this->service = $service; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Render wishlist button markup. |
| 28 |
* |
| 29 |
* @param array<string, mixed> $args Rendering arguments. |
| 30 |
* @return string Rendered HTML. |
| 31 |
*/ |
| 32 |
public function render_button(array $args = []): string |
| 33 |
{ |
| 34 |
if (!Wishlist_Settings::is_enabled()) { |
| 35 |
return ''; |
| 36 |
} |
| 37 |
|
| 38 |
if (!Wishlist_Settings::guests_allowed() && !is_user_logged_in()) { |
| 39 |
$message = Wishlist_Settings::get('guest_block_text'); |
| 40 |
return '<div class="king-addons-wishlist__notice">' . esc_html($message) . '</div>'; |
| 41 |
} |
| 42 |
|
| 43 |
$defaults = [ |
| 44 |
'product_id' => get_the_ID(), |
| 45 |
'variation_id' => 0, |
| 46 |
'wishlist_id' => null, |
| 47 |
'class' => '', |
| 48 |
]; |
| 49 |
$args = wp_parse_args($args, $defaults); |
| 50 |
|
| 51 |
$product_id = absint($args['product_id']); |
| 52 |
$variation_id = absint($args['variation_id']); |
| 53 |
if ($product_id <= 0) { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
$wishlist_id = $args['wishlist_id'] ? sanitize_title($args['wishlist_id']) : $this->service->get_active_wishlist_id(); |
| 58 |
|
| 59 |
$in_list = $this->service->has_item($product_id, $variation_id, $wishlist_id); |
| 60 |
$label_default = $args['label_default'] ?? Wishlist_Settings::get('button_add_text'); |
| 61 |
$label_added = $args['label_added'] ?? Wishlist_Settings::get('button_added_text'); |
| 62 |
$label = $in_list ? $label_added : $label_default; |
| 63 |
$state_class = $in_list ? 'king-addons-wishlist-button--added' : 'king-addons-wishlist-button--default'; |
| 64 |
|
| 65 |
$display_mode = $args['display_mode'] ?? Wishlist_Settings::get('button_display_mode', 'icon_text'); |
| 66 |
$show_icon = in_array($display_mode, ['icon', 'icon_text'], true); |
| 67 |
$show_label = $display_mode === 'icon_text'; |
| 68 |
$icon_class = $args['icon_class'] ?? Wishlist_Settings::get('icon_choice', 'eicon-heart'); |
| 69 |
|
| 70 |
$classes = [ |
| 71 |
'king-addons-wishlist-button', |
| 72 |
$state_class, |
| 73 |
sanitize_html_class($args['class']), |
| 74 |
]; |
| 75 |
|
| 76 |
$icon_html = $show_icon ? '<span class="king-addons-wishlist-button__icon" aria-hidden="true"><i class="' . esc_attr($icon_class) . '"></i></span>' : ''; |
| 77 |
$label_html = $show_label ? '<span class="king-addons-wishlist-button__label">' . esc_html($label) . '</span>' : ''; |
| 78 |
|
| 79 |
return '<button type="button" class="' . esc_attr(implode(' ', array_filter($classes))) . '" data-product-id="' . esc_attr($product_id) . '" data-variation-id="' . esc_attr($variation_id) . '" data-wishlist-id="' . esc_attr($wishlist_id) . '" data-state="' . esc_attr($in_list ? 'added' : 'default') . '" aria-pressed="' . ($in_list ? 'true' : 'false') . '">' . $icon_html . $label_html . '</button>'; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Render wishlist counter element. |
| 84 |
* |
| 85 |
* @param array<string, mixed> $args Rendering arguments. |
| 86 |
* @return string Rendered HTML. |
| 87 |
*/ |
| 88 |
public function render_counter(array $args = []): string |
| 89 |
{ |
| 90 |
if (!Wishlist_Settings::is_enabled()) { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
$wishlist_id = $args['wishlist_id'] ? sanitize_title($args['wishlist_id']) : $this->service->get_active_wishlist_id(); |
| 95 |
$count = $this->service->get_count($wishlist_id); |
| 96 |
|
| 97 |
$classes = [ |
| 98 |
'king-addons-wishlist-counter', |
| 99 |
isset($args['class']) ? sanitize_html_class($args['class']) : '', |
| 100 |
]; |
| 101 |
|
| 102 |
return '<span class="' . esc_attr(implode(' ', array_filter($classes))) . '" data-wishlist-id="' . esc_attr($wishlist_id) . '" data-count="' . esc_attr($count) . '">' . esc_html($count) . '</span>'; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|