| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Product Countdown 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 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Displays countdown until sale ends. |
| 19 |
*/ |
| 20 |
class Woo_Product_Countdown extends Abstract_Single_Widget |
| 21 |
{ |
| 22 |
public function get_name(): string |
| 23 |
{ |
| 24 |
return 'woo_product_countdown'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_title(): string |
| 28 |
{ |
| 29 |
return esc_html__('Product Sale Countdown', 'king-addons'); |
| 30 |
} |
| 31 |
|
| 32 |
public function get_icon(): string |
| 33 |
{ |
| 34 |
return 'king-addons-icon king-addons-woo-product-countdown'; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_categories(): array |
| 38 |
{ |
| 39 |
return ['king-addons-woo-builder']; |
| 40 |
} |
| 41 |
|
| 42 |
public function get_style_depends(): array |
| 43 |
{ |
| 44 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-countdown-style']; |
| 45 |
} |
| 46 |
|
| 47 |
public function get_script_depends(): array |
| 48 |
{ |
| 49 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-countdown-script']; |
| 50 |
} |
| 51 |
|
| 52 |
protected function register_controls(): void |
| 53 |
{ |
| 54 |
$this->start_controls_section( |
| 55 |
'section_content', |
| 56 |
[ |
| 57 |
'label' => esc_html__('Content', 'king-addons'), |
| 58 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 59 |
] |
| 60 |
); |
| 61 |
|
| 62 |
$this->add_control( |
| 63 |
'custom_date', |
| 64 |
[ |
| 65 |
'label' => sprintf(__('Custom end date %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 66 |
'type' => Controls_Manager::DATE_TIME, |
| 67 |
] |
| 68 |
); |
| 69 |
|
| 70 |
$this->add_control( |
| 71 |
'display_format', |
| 72 |
[ |
| 73 |
'label' => sprintf(__('Display format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 74 |
'type' => Controls_Manager::SELECT, |
| 75 |
'options' => [ |
| 76 |
'blocks' => esc_html__('Blocks', 'king-addons'), |
| 77 |
'inline' => esc_html__('Inline (Pro)', 'king-addons'), |
| 78 |
], |
| 79 |
'default' => 'blocks', |
| 80 |
] |
| 81 |
); |
| 82 |
|
| 83 |
$this->add_control( |
| 84 |
'show_seconds', |
| 85 |
[ |
| 86 |
'label' => sprintf(__('Show seconds %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 87 |
'type' => Controls_Manager::SWITCHER, |
| 88 |
'return_value' => 'yes', |
| 89 |
] |
| 90 |
); |
| 91 |
|
| 92 |
$this->add_control( |
| 93 |
'expired_behavior', |
| 94 |
[ |
| 95 |
'label' => sprintf(__('After expire %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 96 |
'type' => Controls_Manager::SELECT, |
| 97 |
'options' => [ |
| 98 |
'hide' => esc_html__('Hide', 'king-addons'), |
| 99 |
'text' => esc_html__('Show text', 'king-addons'), |
| 100 |
'zero' => esc_html__('Freeze at 0', 'king-addons'), |
| 101 |
], |
| 102 |
'default' => 'hide', |
| 103 |
] |
| 104 |
); |
| 105 |
|
| 106 |
$this->add_control( |
| 107 |
'expired_text', |
| 108 |
[ |
| 109 |
'label' => esc_html__('Expired text (Pro)', 'king-addons'), |
| 110 |
'type' => Controls_Manager::TEXT, |
| 111 |
'dynamic' => ['active' => true], |
| 112 |
'default' => esc_html__('Offer ended', 'king-addons'), |
| 113 |
] |
| 114 |
); |
| 115 |
|
| 116 |
$this->end_controls_section(); |
| 117 |
|
| 118 |
$this->start_controls_section( |
| 119 |
'section_style', |
| 120 |
[ |
| 121 |
'label' => esc_html__('Style', 'king-addons'), |
| 122 |
'tab' => Controls_Manager::TAB_STYLE, |
| 123 |
] |
| 124 |
); |
| 125 |
|
| 126 |
$this->add_group_control( |
| 127 |
Group_Control_Typography::get_type(), |
| 128 |
[ |
| 129 |
'name' => 'numbers_typo', |
| 130 |
'selector' => '{{WRAPPER}} .ka-woo-countdown__number', |
| 131 |
'label' => esc_html__('Numbers Typography', 'king-addons'), |
| 132 |
] |
| 133 |
); |
| 134 |
|
| 135 |
$this->add_control( |
| 136 |
'numbers_color', |
| 137 |
[ |
| 138 |
'label' => esc_html__('Numbers Color', 'king-addons'), |
| 139 |
'type' => Controls_Manager::COLOR, |
| 140 |
'selectors' => [ |
| 141 |
'{{WRAPPER}} .ka-woo-countdown__number' => 'color: {{VALUE}};', |
| 142 |
], |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
$this->add_group_control( |
| 147 |
Group_Control_Typography::get_type(), |
| 148 |
[ |
| 149 |
'name' => 'labels_typo', |
| 150 |
'selector' => '{{WRAPPER}} .ka-woo-countdown__label', |
| 151 |
'label' => esc_html__('Labels Typography', 'king-addons'), |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
$this->add_control( |
| 156 |
'labels_color', |
| 157 |
[ |
| 158 |
'label' => esc_html__('Labels Color', 'king-addons'), |
| 159 |
'type' => Controls_Manager::COLOR, |
| 160 |
'selectors' => [ |
| 161 |
'{{WRAPPER}} .ka-woo-countdown__label' => 'color: {{VALUE}};', |
| 162 |
], |
| 163 |
] |
| 164 |
); |
| 165 |
|
| 166 |
$this->add_responsive_control( |
| 167 |
'gap', |
| 168 |
[ |
| 169 |
'label' => esc_html__('Items Gap', 'king-addons'), |
| 170 |
'type' => Controls_Manager::SLIDER, |
| 171 |
'range' => [ |
| 172 |
'px' => ['min' => 0, 'max' => 40], |
| 173 |
], |
| 174 |
'selectors' => [ |
| 175 |
'{{WRAPPER}} .ka-woo-countdown' => 'gap: {{SIZE}}{{UNIT}};', |
| 176 |
], |
| 177 |
] |
| 178 |
); |
| 179 |
|
| 180 |
$this->end_controls_section(); |
| 181 |
} |
| 182 |
|
| 183 |
protected function render(): void |
| 184 |
{ |
| 185 |
$product = $this->get_product(); |
| 186 |
if (!$product) { |
| 187 |
$this->render_missing_product_notice(); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$settings = $this->get_settings_for_display(); |
| 192 |
$can_pro = king_addons_can_use_pro(); |
| 193 |
|
| 194 |
// Both values land in markup the script switches on, so anything off |
| 195 |
// the list has to fall back. |
| 196 |
$format = (string) ($settings['display_format'] ?? 'blocks'); |
| 197 |
if (!in_array($format, ['blocks', 'inline'], true)) { |
| 198 |
$format = 'blocks'; |
| 199 |
} |
| 200 |
if ('inline' === $format && !$can_pro) { |
| 201 |
$format = 'blocks'; |
| 202 |
} |
| 203 |
|
| 204 |
$end_timestamp = 0; |
| 205 |
$custom_date = $settings['custom_date'] ?? ''; |
| 206 |
if (!empty($custom_date) && $can_pro) { |
| 207 |
$end_timestamp = strtotime($custom_date); |
| 208 |
} |
| 209 |
|
| 210 |
if (!$end_timestamp) { |
| 211 |
$end_timestamp = (int) get_post_meta($product->get_id(), '_sale_price_dates_to', true); |
| 212 |
} |
| 213 |
|
| 214 |
if (!$end_timestamp || $end_timestamp <= time()) { |
| 215 |
$behavior = $settings['expired_behavior'] ?? 'hide'; |
| 216 |
if (!empty($settings['expired_text']) && 'text' === $behavior && $can_pro) { |
| 217 |
echo '<div class="ka-woo-countdown ka-woo-countdown--expired">' . esc_html($settings['expired_text']) . '</div>'; |
| 218 |
} elseif (\Elementor\Plugin::$instance->editor->is_edit_mode() && 'zero' !== $behavior) { |
| 219 |
// Hiding is the right answer on the front end, but a blank |
| 220 |
// widget in the editor reads as a fault. |
| 221 |
echo '<div class="king-addons-woo-builder-notice">' |
| 222 |
. esc_html__('The countdown needs a sale end date on the product, or a custom date.', 'king-addons') |
| 223 |
. '</div>'; |
| 224 |
} elseif ('zero' === $behavior) { |
| 225 |
echo '<div class="ka-woo-countdown ka-woo-countdown--expired"><div class="ka-woo-countdown__item"><span class="ka-woo-countdown__number">0</span><span class="ka-woo-countdown__label">' . esc_html__('Time', 'king-addons') . '</span></div></div>'; |
| 226 |
} |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
$show_seconds = !empty($settings['show_seconds']) && $can_pro; |
| 231 |
|
| 232 |
$behavior = (string) ($settings['expired_behavior'] ?? 'hide'); |
| 233 |
if (!in_array($behavior, ['hide', 'zero', 'text'], true)) { |
| 234 |
$behavior = 'hide'; |
| 235 |
} |
| 236 |
$expired_text = (!empty($settings['expired_text']) && $can_pro) ? $settings['expired_text'] : ''; |
| 237 |
|
| 238 |
$classes = ['ka-woo-countdown', 'ka-woo-countdown--' . $format]; |
| 239 |
|
| 240 |
echo '<div class="' . esc_attr(implode(' ', $classes)) . '" data-end="' . esc_attr((string) $end_timestamp) . '" data-seconds="' . ($show_seconds ? 'yes' : 'no') . '" data-expire="' . esc_attr($behavior) . '" data-expire-text="' . esc_attr($expired_text) . '">'; |
| 241 |
|
| 242 |
if ('inline' === $format) { |
| 243 |
echo '<div class="ka-woo-countdown__inline">'; |
| 244 |
echo '<span class="ka-woo-countdown__number" data-unit="days">0</span><span class="ka-woo-countdown__label">d</span>'; |
| 245 |
echo '<span class="ka-woo-countdown__sep">:</span>'; |
| 246 |
echo '<span class="ka-woo-countdown__number" data-unit="hours">00</span><span class="ka-woo-countdown__label">h</span>'; |
| 247 |
echo '<span class="ka-woo-countdown__sep">:</span>'; |
| 248 |
echo '<span class="ka-woo-countdown__number" data-unit="minutes">00</span><span class="ka-woo-countdown__label">m</span>'; |
| 249 |
if ($show_seconds) { |
| 250 |
echo '<span class="ka-woo-countdown__sep">:</span>'; |
| 251 |
echo '<span class="ka-woo-countdown__number" data-unit="seconds">00</span><span class="ka-woo-countdown__label">s</span>'; |
| 252 |
} |
| 253 |
echo '</div>'; |
| 254 |
} else { |
| 255 |
echo '<div class="ka-woo-countdown__item"><span class="ka-woo-countdown__number" data-unit="days">0</span><span class="ka-woo-countdown__label">' . esc_html__('Days', 'king-addons') . '</span></div>'; |
| 256 |
echo '<div class="ka-woo-countdown__item"><span class="ka-woo-countdown__number" data-unit="hours">0</span><span class="ka-woo-countdown__label">' . esc_html__('Hours', 'king-addons') . '</span></div>'; |
| 257 |
echo '<div class="ka-woo-countdown__item"><span class="ka-woo-countdown__number" data-unit="minutes">0</span><span class="ka-woo-countdown__label">' . esc_html__('Minutes', 'king-addons') . '</span></div>'; |
| 258 |
if ($show_seconds) { |
| 259 |
echo '<div class="ka-woo-countdown__item"><span class="ka-woo-countdown__number" data-unit="seconds">0</span><span class="ka-woo-countdown__label">' . esc_html__('Seconds', 'king-addons') . '</span></div>'; |
| 260 |
} |
| 261 |
} |
| 262 |
echo '</div>'; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|