| 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 'eicon-editor-paragraph'; |
| 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-archive-description-style']; |
| 45 |
} |
| 46 |
|
| 47 |
protected function register_controls(): void |
| 48 |
{ |
| 49 |
$this->start_controls_section( |
| 50 |
'section_content', |
| 51 |
[ |
| 52 |
'label' => esc_html__('Content', 'king-addons'), |
| 53 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 54 |
] |
| 55 |
); |
| 56 |
|
| 57 |
$this->add_control( |
| 58 |
'trim_words', |
| 59 |
[ |
| 60 |
'label' => sprintf(__('Trim words %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 61 |
'type' => Controls_Manager::NUMBER, |
| 62 |
'min' => 10, |
| 63 |
] |
| 64 |
); |
| 65 |
|
| 66 |
$this->add_control( |
| 67 |
'read_more_text', |
| 68 |
[ |
| 69 |
'label' => esc_html__('Read more text (Pro)', 'king-addons'), |
| 70 |
'type' => Controls_Manager::TEXT, |
| 71 |
'default' => esc_html__('Read more', 'king-addons'), |
| 72 |
] |
| 73 |
); |
| 74 |
|
| 75 |
$this->add_control( |
| 76 |
'format', |
| 77 |
[ |
| 78 |
'label' => sprintf(__('Custom format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 79 |
'type' => Controls_Manager::TEXTAREA, |
| 80 |
'description' => esc_html__('Use {description}, {title}, {count}.', 'king-addons'), |
| 81 |
'rows' => 3, |
| 82 |
] |
| 83 |
); |
| 84 |
|
| 85 |
$this->add_control( |
| 86 |
'source', |
| 87 |
[ |
| 88 |
'label' => sprintf(__('Source %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 89 |
'type' => Controls_Manager::SELECT, |
| 90 |
'options' => [ |
| 91 |
'auto' => esc_html__('Auto (term/shop description)', 'king-addons'), |
| 92 |
'custom' => esc_html__('Custom text', 'king-addons'), |
| 93 |
], |
| 94 |
'default' => 'auto', |
| 95 |
] |
| 96 |
); |
| 97 |
|
| 98 |
$this->add_control( |
| 99 |
'custom_text', |
| 100 |
[ |
| 101 |
'label' => esc_html__('Custom text', 'king-addons'), |
| 102 |
'type' => Controls_Manager::TEXTAREA, |
| 103 |
'rows' => 4, |
| 104 |
'condition' => [ |
| 105 |
'source' => 'custom', |
| 106 |
], |
| 107 |
] |
| 108 |
); |
| 109 |
|
| 110 |
$this->end_controls_section(); |
| 111 |
|
| 112 |
$this->start_controls_section( |
| 113 |
'section_style', |
| 114 |
[ |
| 115 |
'label' => esc_html__('Style', 'king-addons'), |
| 116 |
'tab' => Controls_Manager::TAB_STYLE, |
| 117 |
] |
| 118 |
); |
| 119 |
|
| 120 |
$this->add_group_control( |
| 121 |
Group_Control_Typography::get_type(), |
| 122 |
[ |
| 123 |
'name' => 'typography', |
| 124 |
'selector' => '{{WRAPPER}} .ka-woo-archive-description', |
| 125 |
] |
| 126 |
); |
| 127 |
|
| 128 |
$this->add_control( |
| 129 |
'color', |
| 130 |
[ |
| 131 |
'label' => esc_html__('Color', 'king-addons'), |
| 132 |
'type' => Controls_Manager::COLOR, |
| 133 |
'selectors' => [ |
| 134 |
'{{WRAPPER}} .ka-woo-archive-description' => 'color: {{VALUE}};', |
| 135 |
], |
| 136 |
] |
| 137 |
); |
| 138 |
|
| 139 |
$this->add_responsive_control( |
| 140 |
'margin', |
| 141 |
[ |
| 142 |
'label' => esc_html__('Margin', 'king-addons'), |
| 143 |
'type' => Controls_Manager::DIMENSIONS, |
| 144 |
'size_units' => ['px', 'em', '%'], |
| 145 |
'selectors' => [ |
| 146 |
'{{WRAPPER}} .ka-woo-archive-description' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 147 |
], |
| 148 |
] |
| 149 |
); |
| 150 |
|
| 151 |
$this->end_controls_section(); |
| 152 |
} |
| 153 |
|
| 154 |
protected function render(): void |
| 155 |
{ |
| 156 |
if (!class_exists('WooCommerce') || !function_exists('is_shop') || !function_exists('is_product_taxonomy')) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
if (!$this->should_render()) { |
| 161 |
$this->render_missing_archive_notice(); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$settings = $this->get_settings_for_display(); |
| 166 |
$can_pro = king_addons_can_use_pro(); |
| 167 |
|
| 168 |
$desc = ''; |
| 169 |
$title = woocommerce_page_title(false); |
| 170 |
if ('custom' === ($settings['source'] ?? 'auto')) { |
| 171 |
$desc = $settings['custom_text'] ?? ''; |
| 172 |
} else { |
| 173 |
$desc = term_description(); |
| 174 |
if (is_shop()) { |
| 175 |
$shop_id = wc_get_page_id('shop'); |
| 176 |
$desc = get_post_field('post_content', $shop_id); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if (empty($desc)) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$trim = !empty($settings['trim_words']) && $can_pro ? (int) $settings['trim_words'] : 0; |
| 185 |
$trimmed = false; |
| 186 |
if ($trim > 0) { |
| 187 |
$trimmed = true; |
| 188 |
$desc = wp_trim_words(wp_strip_all_tags($desc), $trim, ''); |
| 189 |
$desc = wpautop($desc); |
| 190 |
} |
| 191 |
|
| 192 |
if (!empty($settings['format']) && $can_pro) { |
| 193 |
global $wp_query; |
| 194 |
$count = isset($wp_query->found_posts) ? (int) $wp_query->found_posts : 0; |
| 195 |
$desc = str_replace( |
| 196 |
['{description}', '{title}', '{count}'], |
| 197 |
[$desc, $title, number_format_i18n($count)], |
| 198 |
$settings['format'] |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
echo '<div class="ka-woo-archive-description">'; |
| 203 |
echo wp_kses_post($desc); |
| 204 |
if ($trimmed && !empty($settings['read_more_text']) && $can_pro) { |
| 205 |
echo '<a class="ka-woo-archive-description__readmore" href="#">' . esc_html($settings['read_more_text']) . '</a>'; |
| 206 |
} |
| 207 |
echo '</div>'; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|