| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Search Results widget (Free). |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Typography; |
| 12 |
use Elementor\Widget_Base; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Renders the results of the current search query, with an empty state. |
| 20 |
*/ |
| 21 |
class TB_Search_Results extends Widget_Base |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Widget slug. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_name(): string |
| 29 |
{ |
| 30 |
return 'king-addons-tb-search-results'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('TB - Search Results', 'king-addons'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Widget icon. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function get_icon(): string |
| 49 |
{ |
| 50 |
return 'king-addons-icon king-addons-tb-search-results'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Style dependencies. |
| 55 |
* |
| 56 |
* @return array<int, string> |
| 57 |
*/ |
| 58 |
public function get_style_depends(): array |
| 59 |
{ |
| 60 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-search-results-style']; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Script dependencies. |
| 65 |
* |
| 66 |
* @return array<int, string> |
| 67 |
*/ |
| 68 |
public function get_script_depends(): array |
| 69 |
{ |
| 70 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-search-results-script']; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Categories. |
| 75 |
* |
| 76 |
* @return array<int, string> |
| 77 |
*/ |
| 78 |
public function get_categories(): array |
| 79 |
{ |
| 80 |
return ['king-addons-theme-builder']; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Keywords. |
| 85 |
* |
| 86 |
* @return array<int, string> |
| 87 |
*/ |
| 88 |
public function get_keywords(): array |
| 89 |
{ |
| 90 |
return ['search', 'results', 'query', 'theme builder', 'king-addons']; |
| 91 |
} |
| 92 |
|
| 93 |
public function get_custom_help_url() |
| 94 |
{ |
| 95 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Register controls. |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function register_controls(): void |
| 104 |
{ |
| 105 |
$this->register_content_controls(false); |
| 106 |
$this->register_style_controls(false); |
| 107 |
$this->register_pro_notice_controls(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Render output. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function render(): void |
| 116 |
{ |
| 117 |
$settings = $this->get_settings_for_display(); |
| 118 |
$this->render_output($settings, false); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Content controls. |
| 123 |
* |
| 124 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
protected function register_content_controls(bool $is_pro): void |
| 129 |
{ |
| 130 |
$this->start_controls_section( |
| 131 |
'kng_content_section', |
| 132 |
[ |
| 133 |
'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content', 'king-addons'), |
| 134 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 135 |
] |
| 136 |
); |
| 137 |
|
| 138 |
$this->add_responsive_control( |
| 139 |
'kng_columns', |
| 140 |
[ |
| 141 |
'label' => esc_html__('Columns', 'king-addons'), |
| 142 |
'type' => Controls_Manager::SELECT, |
| 143 |
'default' => '1', |
| 144 |
'tablet_default' => '1', |
| 145 |
'mobile_default' => '1', |
| 146 |
'options' => [ |
| 147 |
'1' => '1', |
| 148 |
'2' => '2', |
| 149 |
'3' => '3', |
| 150 |
'4' => '4', |
| 151 |
], |
| 152 |
'selectors' => [ |
| 153 |
'{{WRAPPER}} .king-addons-tb-search-results__list' => 'grid-template-columns: repeat({{VALUE}}, minmax(0, 1fr));', |
| 154 |
], |
| 155 |
] |
| 156 |
); |
| 157 |
|
| 158 |
$this->add_control( |
| 159 |
'kng_show_thumbnail', |
| 160 |
[ |
| 161 |
'label' => esc_html__('Featured Image', 'king-addons'), |
| 162 |
'type' => Controls_Manager::SWITCHER, |
| 163 |
'default' => 'yes', |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
$this->add_control( |
| 168 |
'kng_show_excerpt', |
| 169 |
[ |
| 170 |
'label' => esc_html__('Excerpt', 'king-addons'), |
| 171 |
'type' => Controls_Manager::SWITCHER, |
| 172 |
'default' => 'yes', |
| 173 |
] |
| 174 |
); |
| 175 |
|
| 176 |
$this->add_control( |
| 177 |
'kng_excerpt_length', |
| 178 |
[ |
| 179 |
'label' => esc_html__('Excerpt Length', 'king-addons'), |
| 180 |
'type' => Controls_Manager::NUMBER, |
| 181 |
'default' => 25, |
| 182 |
'min' => 5, |
| 183 |
'max' => 100, |
| 184 |
'condition' => ['kng_show_excerpt' => 'yes'], |
| 185 |
] |
| 186 |
); |
| 187 |
|
| 188 |
$this->add_control( |
| 189 |
'kng_show_meta', |
| 190 |
[ |
| 191 |
'label' => esc_html__('Date', 'king-addons'), |
| 192 |
'type' => Controls_Manager::SWITCHER, |
| 193 |
'default' => 'yes', |
| 194 |
] |
| 195 |
); |
| 196 |
|
| 197 |
$this->add_control( |
| 198 |
'kng_read_more_text', |
| 199 |
[ |
| 200 |
'label' => esc_html__('Read More Text', 'king-addons'), |
| 201 |
'type' => Controls_Manager::TEXT, |
| 202 |
'dynamic' => ['active' => true], |
| 203 |
'default' => esc_html__('Read more', 'king-addons'), |
| 204 |
] |
| 205 |
); |
| 206 |
|
| 207 |
$this->add_control( |
| 208 |
'kng_nothing_found_text', |
| 209 |
[ |
| 210 |
'label' => esc_html__('Nothing Found Text', 'king-addons'), |
| 211 |
'type' => Controls_Manager::TEXTAREA, |
| 212 |
'dynamic' => ['active' => true], |
| 213 |
'default' => esc_html__('Nothing matched your search. Try a different phrase.', 'king-addons'), |
| 214 |
] |
| 215 |
); |
| 216 |
|
| 217 |
$this->add_control( |
| 218 |
'kng_custom_query', |
| 219 |
[ |
| 220 |
'label' => $is_pro ? |
| 221 |
esc_html__('Custom Query', 'king-addons') : |
| 222 |
sprintf(__('Custom Query %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 223 |
'type' => Controls_Manager::TEXT, |
| 224 |
'placeholder' => esc_html__('post_type=post&posts_per_page=6', 'king-addons'), |
| 225 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 226 |
] |
| 227 |
); |
| 228 |
|
| 229 |
$this->end_controls_section(); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Style controls. |
| 234 |
* |
| 235 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 236 |
* |
| 237 |
* @return void |
| 238 |
*/ |
| 239 |
protected function register_style_controls(bool $is_pro): void |
| 240 |
{ |
| 241 |
$this->start_controls_section( |
| 242 |
'kng_style_section', |
| 243 |
[ |
| 244 |
'label' => esc_html__('Style', 'king-addons'), |
| 245 |
'tab' => Controls_Manager::TAB_STYLE, |
| 246 |
] |
| 247 |
); |
| 248 |
|
| 249 |
$this->add_responsive_control( |
| 250 |
'kng_gap', |
| 251 |
[ |
| 252 |
'label' => esc_html__('Gap', 'king-addons'), |
| 253 |
'type' => Controls_Manager::SLIDER, |
| 254 |
'size_units' => ['px', 'em'], |
| 255 |
'range' => ['px' => ['min' => 0, 'max' => 120]], |
| 256 |
'default' => ['unit' => 'px', 'size' => 32], |
| 257 |
'selectors' => [ |
| 258 |
'{{WRAPPER}} .king-addons-tb-search-results__list' => 'gap: {{SIZE}}{{UNIT}};', |
| 259 |
], |
| 260 |
] |
| 261 |
); |
| 262 |
|
| 263 |
$this->add_group_control( |
| 264 |
Group_Control_Typography::get_type(), |
| 265 |
[ |
| 266 |
'name' => 'kng_title_typography', |
| 267 |
'selector' => '{{WRAPPER}} .king-addons-tb-search-results__title', |
| 268 |
] |
| 269 |
); |
| 270 |
|
| 271 |
$this->add_control( |
| 272 |
'kng_title_color', |
| 273 |
[ |
| 274 |
'label' => esc_html__('Title Color', 'king-addons'), |
| 275 |
'type' => Controls_Manager::COLOR, |
| 276 |
'selectors' => [ |
| 277 |
'{{WRAPPER}} .king-addons-tb-search-results__title a' => 'color: {{VALUE}};', |
| 278 |
], |
| 279 |
] |
| 280 |
); |
| 281 |
|
| 282 |
$this->add_control( |
| 283 |
'kng_excerpt_color', |
| 284 |
[ |
| 285 |
'label' => esc_html__('Excerpt Color', 'king-addons'), |
| 286 |
'type' => Controls_Manager::COLOR, |
| 287 |
'selectors' => [ |
| 288 |
'{{WRAPPER}} .king-addons-tb-search-results__excerpt' => 'color: {{VALUE}};', |
| 289 |
], |
| 290 |
] |
| 291 |
); |
| 292 |
|
| 293 |
$this->add_control( |
| 294 |
'kng_meta_color', |
| 295 |
[ |
| 296 |
'label' => esc_html__('Date Color', 'king-addons'), |
| 297 |
'type' => Controls_Manager::COLOR, |
| 298 |
'selectors' => [ |
| 299 |
'{{WRAPPER}} .king-addons-tb-search-results__meta' => 'color: {{VALUE}};', |
| 300 |
], |
| 301 |
] |
| 302 |
); |
| 303 |
|
| 304 |
$this->add_control( |
| 305 |
'kng_empty_color', |
| 306 |
[ |
| 307 |
'label' => esc_html__('Nothing Found Color', 'king-addons'), |
| 308 |
'type' => Controls_Manager::COLOR, |
| 309 |
'selectors' => [ |
| 310 |
'{{WRAPPER}} .king-addons-tb-search-results__empty' => 'color: {{VALUE}};', |
| 311 |
], |
| 312 |
] |
| 313 |
); |
| 314 |
|
| 315 |
$this->add_responsive_control( |
| 316 |
'kng_alignment', |
| 317 |
[ |
| 318 |
'label' => esc_html__('Alignment', 'king-addons'), |
| 319 |
'type' => Controls_Manager::CHOOSE, |
| 320 |
'options' => [ |
| 321 |
'left' => [ |
| 322 |
'title' => esc_html__('Left', 'king-addons'), |
| 323 |
'icon' => 'eicon-text-align-left', |
| 324 |
], |
| 325 |
'center' => [ |
| 326 |
'title' => esc_html__('Center', 'king-addons'), |
| 327 |
'icon' => 'eicon-text-align-center', |
| 328 |
], |
| 329 |
'right' => [ |
| 330 |
'title' => esc_html__('Right', 'king-addons'), |
| 331 |
'icon' => 'eicon-text-align-right', |
| 332 |
], |
| 333 |
], |
| 334 |
'selectors' => [ |
| 335 |
'{{WRAPPER}} .king-addons-tb-search-results' => 'text-align: {{VALUE}};', |
| 336 |
], |
| 337 |
] |
| 338 |
); |
| 339 |
|
| 340 |
$this->end_controls_section(); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Pro upsell. |
| 345 |
* |
| 346 |
* @return void |
| 347 |
*/ |
| 348 |
protected function register_pro_notice_controls(): void |
| 349 |
{ |
| 350 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
Core::renderProFeaturesSection( |
| 355 |
$this, |
| 356 |
'', |
| 357 |
Controls_Manager::RAW_HTML, |
| 358 |
'tb-search-results', |
| 359 |
[ |
| 360 |
'Custom query arguments', |
| 361 |
'Filter results by post type', |
| 362 |
'Card and list skins', |
| 363 |
] |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Render output helper. |
| 369 |
* |
| 370 |
* @param array<string, mixed> $settings Settings. |
| 371 |
* @param bool $is_pro Pro flag. |
| 372 |
* |
| 373 |
* @return void |
| 374 |
*/ |
| 375 |
protected function render_output(array $settings, bool $is_pro): void |
| 376 |
{ |
| 377 |
global $wp_query; |
| 378 |
|
| 379 |
$is_editor = \Elementor\Plugin::$instance->editor->is_edit_mode(); |
| 380 |
$results_query = $wp_query instanceof \WP_Query ? $wp_query : null; |
| 381 |
|
| 382 |
$raw_custom = $is_pro ? trim((string) ($settings['kng_custom_query'] ?? '')) : ''; |
| 383 |
if ($raw_custom && $results_query instanceof \WP_Query) { |
| 384 |
$extra = []; |
| 385 |
wp_parse_str(str_replace('&', '&', $raw_custom), $extra); |
| 386 |
if (is_array($extra)) { |
| 387 |
unset($extra['post_status'], $extra['suppress_filters']); |
| 388 |
$results_query = new \WP_Query(array_merge($results_query->query_vars, $extra)); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if ($results_query instanceof \WP_Query) { |
| 393 |
$results_query->rewind_posts(); |
| 394 |
} |
| 395 |
|
| 396 |
if (!$results_query instanceof \WP_Query || !$results_query->have_posts()) { |
| 397 |
if ($is_editor) { |
| 398 |
Core::renderEditorHint( |
| 399 |
esc_html__('Search results appear on the search page. Preview this template from a search URL to see them.', 'king-addons') |
| 400 |
); |
| 401 |
} |
| 402 |
|
| 403 |
$empty = (string) ($settings['kng_nothing_found_text'] ?? ''); |
| 404 |
if ('' !== $empty) { |
| 405 |
echo '<div class="king-addons-tb-search-results">' |
| 406 |
. '<p class="king-addons-tb-search-results__empty">' . esc_html($empty) . '</p>' |
| 407 |
. '</div>'; |
| 408 |
} |
| 409 |
|
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
$show_thumb = 'yes' === ($settings['kng_show_thumbnail'] ?? 'yes'); |
| 414 |
$show_excerpt = 'yes' === ($settings['kng_show_excerpt'] ?? 'yes'); |
| 415 |
$show_meta = 'yes' === ($settings['kng_show_meta'] ?? 'yes'); |
| 416 |
$excerpt_length = max(5, (int) ($settings['kng_excerpt_length'] ?? 25)); |
| 417 |
$read_more = (string) ($settings['kng_read_more_text'] ?? ''); |
| 418 |
|
| 419 |
echo '<div class="king-addons-tb-search-results">'; |
| 420 |
echo '<div class="king-addons-tb-search-results__list">'; |
| 421 |
|
| 422 |
while ($results_query->have_posts()) { |
| 423 |
$results_query->the_post(); |
| 424 |
|
| 425 |
echo '<article class="king-addons-tb-search-results__item">'; |
| 426 |
|
| 427 |
if ($show_thumb && has_post_thumbnail()) { |
| 428 |
echo '<a class="king-addons-tb-search-results__thumb" href="' . esc_url((string) get_permalink()) . '">'; |
| 429 |
the_post_thumbnail('medium'); |
| 430 |
echo '</a>'; |
| 431 |
} |
| 432 |
|
| 433 |
echo '<h3 class="king-addons-tb-search-results__title">' |
| 434 |
. '<a href="' . esc_url((string) get_permalink()) . '">' . esc_html((string) get_the_title()) . '</a>' |
| 435 |
. '</h3>'; |
| 436 |
|
| 437 |
if ($show_meta) { |
| 438 |
echo '<div class="king-addons-tb-search-results__meta">' . esc_html((string) get_the_date()) . '</div>'; |
| 439 |
} |
| 440 |
|
| 441 |
if ($show_excerpt) { |
| 442 |
$excerpt = wp_trim_words((string) get_the_excerpt(), $excerpt_length, '…'); |
| 443 |
if ('' !== $excerpt) { |
| 444 |
echo '<div class="king-addons-tb-search-results__excerpt">' . esc_html($excerpt) . '</div>'; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
if ('' !== $read_more) { |
| 449 |
echo '<a class="king-addons-tb-search-results__more" href="' . esc_url((string) get_permalink()) . '">' |
| 450 |
. esc_html($read_more) . '</a>'; |
| 451 |
} |
| 452 |
|
| 453 |
echo '</article>'; |
| 454 |
} |
| 455 |
|
| 456 |
echo '</div>'; |
| 457 |
echo '</div>'; |
| 458 |
|
| 459 |
wp_reset_postdata(); |
| 460 |
} |
| 461 |
} |
| 462 |
|