| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Archive Description 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 archive description. |
| 19 |
*/ |
| 20 |
class Woo_Archive_Description extends Abstract_Archive_Widget |
| 21 |
{ |
| 22 |
public function get_name(): string |
| 23 |
{ |
| 24 |
return 'woo_archive_description'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_title(): string |
| 28 |
{ |
| 29 |
return esc_html__('Archive Description', 'king-addons'); |
| 30 |
} |
| 31 |
|
| 32 |
public function get_icon(): string |
| 33 |
{ |
| 34 |
return 'king-addons-icon king-addons-woo-archive-description'; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_categories(): array |
| 38 |
{ |
| 39 |
return ['king-addons-woo-builder']; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Script handles. |
| 44 |
* |
| 45 |
* ModulesMap registers this file, but without declaring the dependency here |
| 46 |
* Elementor never enqueues it - the "Read more" toggle had no script behind |
| 47 |
* it on the page. |
| 48 |
* |
| 49 |
* @return array<int,string> |
| 50 |
*/ |
| 51 |
public function get_script_depends(): array |
| 52 |
{ |
| 53 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-archive-description-script']; |
| 54 |
} |
| 55 |
|
| 56 |
public function get_style_depends(): array |
| 57 |
{ |
| 58 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-archive-description-style']; |
| 59 |
} |
| 60 |
|
| 61 |
protected function register_controls(): void |
| 62 |
{ |
| 63 |
$this->start_controls_section( |
| 64 |
'section_content', |
| 65 |
[ |
| 66 |
'label' => esc_html__('Content', 'king-addons'), |
| 67 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 68 |
] |
| 69 |
); |
| 70 |
|
| 71 |
$this->add_control( |
| 72 |
'trim_words', |
| 73 |
[ |
| 74 |
'label' => sprintf(__('Trim words %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 75 |
'type' => Controls_Manager::NUMBER, |
| 76 |
'min' => 10, |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
$this->add_control( |
| 81 |
'read_more_text', |
| 82 |
[ |
| 83 |
'label' => esc_html__('Read more text (Pro)', 'king-addons'), |
| 84 |
'type' => Controls_Manager::TEXT, |
| 85 |
'dynamic' => ['active' => true], |
| 86 |
'default' => esc_html__('Read more', 'king-addons'), |
| 87 |
] |
| 88 |
); |
| 89 |
|
| 90 |
$this->add_control( |
| 91 |
'format', |
| 92 |
[ |
| 93 |
'label' => sprintf(__('Custom format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 94 |
'type' => Controls_Manager::TEXTAREA, |
| 95 |
'description' => esc_html__('Use {description}, {title}, {count}.', 'king-addons'), |
| 96 |
'rows' => 3, |
| 97 |
] |
| 98 |
); |
| 99 |
|
| 100 |
$this->add_control( |
| 101 |
'source', |
| 102 |
[ |
| 103 |
'label' => sprintf(__('Source %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 104 |
'type' => Controls_Manager::SELECT, |
| 105 |
'options' => [ |
| 106 |
'auto' => esc_html__('Auto (term/shop description)', 'king-addons'), |
| 107 |
'custom' => esc_html__('Custom text', 'king-addons'), |
| 108 |
], |
| 109 |
'default' => 'auto', |
| 110 |
] |
| 111 |
); |
| 112 |
|
| 113 |
$this->add_control( |
| 114 |
'custom_text', |
| 115 |
[ |
| 116 |
'label' => esc_html__('Custom text', 'king-addons'), |
| 117 |
'type' => Controls_Manager::TEXTAREA, |
| 118 |
'dynamic' => ['active' => true], |
| 119 |
'rows' => 4, |
| 120 |
'condition' => [ |
| 121 |
'source' => 'custom', |
| 122 |
], |
| 123 |
] |
| 124 |
); |
| 125 |
|
| 126 |
$this->end_controls_section(); |
| 127 |
|
| 128 |
$this->start_controls_section( |
| 129 |
'section_style', |
| 130 |
[ |
| 131 |
'label' => esc_html__('Style', 'king-addons'), |
| 132 |
'tab' => Controls_Manager::TAB_STYLE, |
| 133 |
] |
| 134 |
); |
| 135 |
|
| 136 |
$this->add_group_control( |
| 137 |
Group_Control_Typography::get_type(), |
| 138 |
[ |
| 139 |
'name' => 'typography', |
| 140 |
'selector' => '{{WRAPPER}} .ka-woo-archive-description', |
| 141 |
] |
| 142 |
); |
| 143 |
|
| 144 |
$this->add_control( |
| 145 |
'color', |
| 146 |
[ |
| 147 |
'label' => esc_html__('Color', 'king-addons'), |
| 148 |
'type' => Controls_Manager::COLOR, |
| 149 |
'selectors' => [ |
| 150 |
'{{WRAPPER}} .ka-woo-archive-description' => 'color: {{VALUE}};', |
| 151 |
], |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
$this->add_responsive_control( |
| 156 |
'margin', |
| 157 |
[ |
| 158 |
'label' => esc_html__('Margin', 'king-addons'), |
| 159 |
'type' => Controls_Manager::DIMENSIONS, |
| 160 |
'size_units' => ['px', 'em', '%'], |
| 161 |
'selectors' => [ |
| 162 |
'{{WRAPPER}} .ka-woo-archive-description' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 163 |
], |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
$this->end_controls_section(); |
| 168 |
} |
| 169 |
|
| 170 |
protected function render(): void |
| 171 |
{ |
| 172 |
if (!class_exists('WooCommerce') || !function_exists('is_shop') || !function_exists('is_product_taxonomy')) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
if (!$this->should_render()) { |
| 177 |
$this->render_missing_archive_notice(); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
$settings = $this->get_settings_for_display(); |
| 182 |
$can_pro = king_addons_can_use_pro(); |
| 183 |
|
| 184 |
$desc = ''; |
| 185 |
$title = woocommerce_page_title(false); |
| 186 |
if ('custom' === ($settings['source'] ?? 'auto')) { |
| 187 |
$desc = $settings['custom_text'] ?? ''; |
| 188 |
} else { |
| 189 |
$desc = term_description(); |
| 190 |
if (is_shop()) { |
| 191 |
$shop_id = wc_get_page_id('shop'); |
| 192 |
$desc = get_post_field('post_content', $shop_id); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if (empty($desc)) { |
| 197 |
// Nothing to show is a legitimate state, but a blank widget in the |
| 198 |
// editor reads as a fault. |
| 199 |
if (\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 200 |
echo '<div class="king-addons-woo-builder-notice">' |
| 201 |
. esc_html__('The description comes from the shop page or the category being viewed.', 'king-addons') |
| 202 |
. '</div>'; |
| 203 |
} |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
$full_desc = $desc; |
| 208 |
|
| 209 |
$trim = !empty($settings['trim_words']) && $can_pro ? (int) $settings['trim_words'] : 0; |
| 210 |
$trimmed = false; |
| 211 |
if ($trim > 0) { |
| 212 |
$trimmed = true; |
| 213 |
$desc = wp_trim_words(wp_strip_all_tags($desc), $trim, ''); |
| 214 |
$desc = wpautop($desc); |
| 215 |
} |
| 216 |
|
| 217 |
if (!empty($settings['format']) && $can_pro) { |
| 218 |
global $wp_query; |
| 219 |
$count = isset($wp_query->found_posts) ? (int) $wp_query->found_posts : 0; |
| 220 |
$desc = str_replace( |
| 221 |
['{description}', '{title}', '{count}'], |
| 222 |
[$desc, $title, number_format_i18n($count)], |
| 223 |
$settings['format'] |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
$has_more = $trimmed && !empty($settings['read_more_text']) && $can_pro; |
| 228 |
|
| 229 |
echo '<div class="ka-woo-archive-description"' . ($has_more ? ' data-ka-expandable="1"' : '') . '>'; |
| 230 |
echo '<div class="ka-woo-archive-description__short">' . wp_kses_post($desc) . '</div>'; |
| 231 |
|
| 232 |
if ($has_more) { |
| 233 |
// The link was an href="#" with no behaviour behind it: clicking |
| 234 |
// "Read more" jumped to the top of the page and never revealed |
| 235 |
// anything. The full text ships alongside, hidden until asked for - |
| 236 |
// and only when the toggle exists, so the page never carries the |
| 237 |
// same paragraph twice for nothing. |
| 238 |
echo '<div class="ka-woo-archive-description__full" hidden>' . wp_kses_post($full_desc) . '</div>'; |
| 239 |
echo '<button type="button" class="ka-woo-archive-description__readmore" aria-expanded="false">' |
| 240 |
. esc_html($settings['read_more_text']) . '</button>'; |
| 241 |
} |
| 242 |
|
| 243 |
echo '</div>'; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|