| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Archive Banner widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Image_Size; |
| 12 |
use Elementor\Group_Control_Typography; |
| 13 |
use Elementor\Utils; |
| 14 |
|
| 15 |
if (!defined('ABSPATH')) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Displays archive banner. |
| 21 |
*/ |
| 22 |
class Woo_Archive_Banner extends Abstract_Archive_Widget |
| 23 |
{ |
| 24 |
public function get_name(): string |
| 25 |
{ |
| 26 |
return 'woo_archive_banner'; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_title(): string |
| 30 |
{ |
| 31 |
return esc_html__('Archive Banner', 'king-addons'); |
| 32 |
} |
| 33 |
|
| 34 |
public function get_icon(): string |
| 35 |
{ |
| 36 |
return 'king-addons-icon king-addons-woo-archive-banner'; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_categories(): array |
| 40 |
{ |
| 41 |
return ['king-addons-woo-builder']; |
| 42 |
} |
| 43 |
|
| 44 |
public function get_style_depends(): array |
| 45 |
{ |
| 46 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-archive-banner-style']; |
| 47 |
} |
| 48 |
|
| 49 |
protected function register_controls(): void |
| 50 |
{ |
| 51 |
$this->start_controls_section( |
| 52 |
'section_content', |
| 53 |
[ |
| 54 |
'label' => esc_html__('Content', 'king-addons'), |
| 55 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 56 |
] |
| 57 |
); |
| 58 |
|
| 59 |
$this->add_control( |
| 60 |
'image', |
| 61 |
[ |
| 62 |
'label' => esc_html__('Image', 'king-addons'), |
| 63 |
'type' => Controls_Manager::MEDIA, |
| 64 |
'dynamic' => ['active' => true], |
| 65 |
'default' => [ |
| 66 |
'url' => Utils::get_placeholder_image_src(), |
| 67 |
], |
| 68 |
] |
| 69 |
); |
| 70 |
|
| 71 |
$this->add_group_control( |
| 72 |
Group_Control_Image_Size::get_type(), |
| 73 |
[ |
| 74 |
'name' => 'image_size', |
| 75 |
'default' => 'large', |
| 76 |
] |
| 77 |
); |
| 78 |
|
| 79 |
$this->add_control( |
| 80 |
'title', |
| 81 |
[ |
| 82 |
'label' => esc_html__('Title', 'king-addons'), |
| 83 |
'type' => Controls_Manager::TEXT, |
| 84 |
'dynamic' => ['active' => true], |
| 85 |
'default' => esc_html__('Archive Banner', 'king-addons'), |
| 86 |
] |
| 87 |
); |
| 88 |
|
| 89 |
$this->add_control( |
| 90 |
'description', |
| 91 |
[ |
| 92 |
'label' => esc_html__('Description', 'king-addons'), |
| 93 |
'type' => Controls_Manager::TEXTAREA, |
| 94 |
'dynamic' => ['active' => true], |
| 95 |
'default' => esc_html__('Highlight your category or shop with a banner.', 'king-addons'), |
| 96 |
] |
| 97 |
); |
| 98 |
|
| 99 |
$this->add_control( |
| 100 |
'button_text', |
| 101 |
[ |
| 102 |
'label' => esc_html__('Button Text', 'king-addons'), |
| 103 |
'type' => Controls_Manager::TEXT, |
| 104 |
'dynamic' => ['active' => true], |
| 105 |
'default' => '', |
| 106 |
] |
| 107 |
); |
| 108 |
|
| 109 |
$this->add_control( |
| 110 |
'button_url', |
| 111 |
[ |
| 112 |
'label' => esc_html__('Button URL', 'king-addons'), |
| 113 |
'type' => Controls_Manager::URL, |
| 114 |
'dynamic' => ['active' => true], |
| 115 |
'placeholder' => 'https://', |
| 116 |
] |
| 117 |
); |
| 118 |
|
| 119 |
$this->add_control( |
| 120 |
'use_term_meta', |
| 121 |
[ |
| 122 |
'label' => sprintf(__('Use term meta (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 123 |
'type' => Controls_Manager::SWITCHER, |
| 124 |
'return_value' => 'yes', |
| 125 |
] |
| 126 |
); |
| 127 |
|
| 128 |
$this->add_control( |
| 129 |
'title_format', |
| 130 |
[ |
| 131 |
'label' => sprintf(__('Title format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 132 |
'type' => Controls_Manager::TEXT, |
| 133 |
'description' => esc_html__('Use {title} and {count}.', 'king-addons'), |
| 134 |
] |
| 135 |
); |
| 136 |
|
| 137 |
$this->add_control( |
| 138 |
'desc_format', |
| 139 |
[ |
| 140 |
'label' => sprintf(__('Description format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 141 |
'type' => Controls_Manager::TEXTAREA, |
| 142 |
'description' => esc_html__('Use {description}, {title}, {count}.', 'king-addons'), |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
$this->end_controls_section(); |
| 147 |
|
| 148 |
$this->start_controls_section( |
| 149 |
'section_style', |
| 150 |
[ |
| 151 |
'label' => esc_html__('Style', 'king-addons'), |
| 152 |
'tab' => Controls_Manager::TAB_STYLE, |
| 153 |
] |
| 154 |
); |
| 155 |
|
| 156 |
$this->add_group_control( |
| 157 |
Group_Control_Typography::get_type(), |
| 158 |
[ |
| 159 |
'name' => 'title_typo', |
| 160 |
'selector' => '{{WRAPPER}} .ka-woo-archive-banner__title', |
| 161 |
] |
| 162 |
); |
| 163 |
|
| 164 |
$this->add_control( |
| 165 |
'title_color', |
| 166 |
[ |
| 167 |
'label' => esc_html__('Title Color', 'king-addons'), |
| 168 |
'type' => Controls_Manager::COLOR, |
| 169 |
'selectors' => [ |
| 170 |
'{{WRAPPER}} .ka-woo-archive-banner__title' => 'color: {{VALUE}};', |
| 171 |
], |
| 172 |
] |
| 173 |
); |
| 174 |
|
| 175 |
$this->add_group_control( |
| 176 |
Group_Control_Typography::get_type(), |
| 177 |
[ |
| 178 |
'name' => 'desc_typo', |
| 179 |
'selector' => '{{WRAPPER}} .ka-woo-archive-banner__desc', |
| 180 |
] |
| 181 |
); |
| 182 |
|
| 183 |
$this->add_control( |
| 184 |
'desc_color', |
| 185 |
[ |
| 186 |
'label' => esc_html__('Description Color', 'king-addons'), |
| 187 |
'type' => Controls_Manager::COLOR, |
| 188 |
'selectors' => [ |
| 189 |
'{{WRAPPER}} .ka-woo-archive-banner__desc' => 'color: {{VALUE}};', |
| 190 |
], |
| 191 |
] |
| 192 |
); |
| 193 |
|
| 194 |
$this->add_responsive_control( |
| 195 |
'padding', |
| 196 |
[ |
| 197 |
'label' => esc_html__('Padding', 'king-addons'), |
| 198 |
'type' => Controls_Manager::DIMENSIONS, |
| 199 |
'size_units' => ['px', 'em', '%'], |
| 200 |
'selectors' => [ |
| 201 |
'{{WRAPPER}} .ka-woo-archive-banner__inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 202 |
], |
| 203 |
] |
| 204 |
); |
| 205 |
|
| 206 |
$this->end_controls_section(); |
| 207 |
} |
| 208 |
|
| 209 |
protected function render(): void |
| 210 |
{ |
| 211 |
if (!class_exists('WooCommerce') || !function_exists('is_shop') || !function_exists('is_product_taxonomy')) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
if (!$this->should_render()) { |
| 216 |
$this->render_missing_archive_notice(); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
$settings = $this->get_settings_for_display(); |
| 221 |
$can_pro = king_addons_can_use_pro(); |
| 222 |
|
| 223 |
$title = $settings['title'] ?? ''; |
| 224 |
$desc = $settings['description'] ?? ''; |
| 225 |
$img_html = ''; |
| 226 |
|
| 227 |
if (!empty($settings['use_term_meta']) && $can_pro && is_product_taxonomy()) { |
| 228 |
$term = get_queried_object(); |
| 229 |
$term_img_id = get_term_meta($term->term_id, 'banner_image', true); |
| 230 |
$term_title = get_term_meta($term->term_id, 'banner_title', true); |
| 231 |
$term_desc = get_term_meta($term->term_id, 'banner_desc', true); |
| 232 |
if ($term_img_id) { |
| 233 |
// The third argument is the name of a settings key, not an |
| 234 |
// attachment id - passing the id made Elementor look up |
| 235 |
// $settings[128], warn, and return nothing. Feed it a synthetic |
| 236 |
// key so the chosen image size still applies. |
| 237 |
$img_settings = $settings; |
| 238 |
$img_settings['ka_term_banner_image'] = [ |
| 239 |
'id' => (int) $term_img_id, |
| 240 |
'url' => wp_get_attachment_image_url((int) $term_img_id, 'full') ?: '', |
| 241 |
]; |
| 242 |
$img_html = Group_Control_Image_Size::get_attachment_image_html($img_settings, 'image_size', 'ka_term_banner_image'); |
| 243 |
} |
| 244 |
if ($term_title) { |
| 245 |
$title = $term_title; |
| 246 |
} |
| 247 |
if ($term_desc) { |
| 248 |
$desc = $term_desc; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if ($can_pro) { |
| 253 |
global $wp_query; |
| 254 |
$count = isset($wp_query->found_posts) ? (int) $wp_query->found_posts : 0; |
| 255 |
if (!empty($settings['title_format'])) { |
| 256 |
$title = str_replace( |
| 257 |
['{title}', '{count}'], |
| 258 |
[$title, number_format_i18n($count)], |
| 259 |
$settings['title_format'] |
| 260 |
); |
| 261 |
} |
| 262 |
if (!empty($settings['desc_format'])) { |
| 263 |
$desc = str_replace( |
| 264 |
['{description}', '{title}', '{count}'], |
| 265 |
[$desc, $title, number_format_i18n($count)], |
| 266 |
$settings['desc_format'] |
| 267 |
); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
if (empty($img_html) && !empty($settings['image']['id'])) { |
| 272 |
$img_html = Group_Control_Image_Size::get_attachment_image_html($settings, 'image_size', 'image'); |
| 273 |
} |
| 274 |
|
| 275 |
// With no image, title, description or button there is nothing but an |
| 276 |
// empty shell - which in the editor looks like the widget failed. |
| 277 |
$has_button = !empty($settings['button_text']) && !empty($settings['button_url']['url']); |
| 278 |
if (!$img_html && '' === trim((string) $title) && '' === trim((string) $desc) && !$has_button) { |
| 279 |
if (\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 280 |
echo '<div class="king-addons-woo-builder-notice">' |
| 281 |
. esc_html__('Set a title, description, image or button for the banner.', 'king-addons') |
| 282 |
. '</div>'; |
| 283 |
} |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
echo '<div class="ka-woo-archive-banner">'; |
| 288 |
if ($img_html) { |
| 289 |
echo '<div class="ka-woo-archive-banner__image">' . $img_html . '</div>'; |
| 290 |
} |
| 291 |
echo '<div class="ka-woo-archive-banner__inner">'; |
| 292 |
if ($title) { |
| 293 |
echo '<h3 class="ka-woo-archive-banner__title">' . esc_html($title) . '</h3>'; |
| 294 |
} |
| 295 |
if ($desc) { |
| 296 |
echo '<div class="ka-woo-archive-banner__desc">' . wp_kses_post($desc) . '</div>'; |
| 297 |
} |
| 298 |
if ($has_button) { |
| 299 |
// is_external is absent until the author opens the link options, so |
| 300 |
// reading it directly warned on a plain URL. |
| 301 |
$target = !empty($settings['button_url']['is_external']) ? ' target="_blank" rel="noopener noreferrer"' : ''; |
| 302 |
echo '<a class="ka-woo-archive-banner__btn" href="' . esc_url($settings['button_url']['url']) . '"' . $target . '>' . esc_html($settings['button_text']) . '</a>'; |
| 303 |
} |
| 304 |
echo '</div>'; |
| 305 |
echo '</div>'; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|