| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Archive Posts 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 current archive/search main query. |
| 20 |
*/ |
| 21 |
class TB_Archive_Posts 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-archive-posts'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('TB - Archive Posts', '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-archive-posts'; |
| 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-archive-posts-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-archive-posts-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 ['archive', 'loop', 'posts', 'search', 'king-addons']; |
| 91 |
} |
| 92 |
|
| 93 |
public function get_custom_help_url() |
| 94 |
{ |
| 95 |
return 'mailto:bug@kingaddons.com?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_layout_section', |
| 132 |
[ |
| 133 |
'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Layout', 'king-addons'), |
| 134 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 135 |
] |
| 136 |
); |
| 137 |
|
| 138 |
$this->add_control( |
| 139 |
'kng_layout', |
| 140 |
[ |
| 141 |
'label' => esc_html__('Layout', 'king-addons'), |
| 142 |
'type' => Controls_Manager::SELECT, |
| 143 |
'default' => 'grid', |
| 144 |
'options' => [ |
| 145 |
'grid' => esc_html__('Grid', 'king-addons'), |
| 146 |
'list' => esc_html__('List', 'king-addons'), |
| 147 |
'cards' => $is_pro ? esc_html__('Cards (Pro)', 'king-addons') : sprintf(__('Cards %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 148 |
], |
| 149 |
] |
| 150 |
); |
| 151 |
|
| 152 |
$this->add_responsive_control( |
| 153 |
'kng_columns', |
| 154 |
[ |
| 155 |
'label' => esc_html__('Columns', 'king-addons'), |
| 156 |
'type' => Controls_Manager::SLIDER, |
| 157 |
'size_units' => ['px'], |
| 158 |
'range' => [ |
| 159 |
'px' => ['min' => 1, 'max' => 4, 'step' => 1], |
| 160 |
], |
| 161 |
'default' => [ |
| 162 |
'size' => 3, |
| 163 |
'unit' => 'px', |
| 164 |
], |
| 165 |
] |
| 166 |
); |
| 167 |
|
| 168 |
$this->add_responsive_control( |
| 169 |
'kng_gap', |
| 170 |
[ |
| 171 |
'label' => esc_html__('Gap', 'king-addons'), |
| 172 |
'type' => Controls_Manager::SLIDER, |
| 173 |
'size_units' => ['px'], |
| 174 |
'range' => [ |
| 175 |
'px' => ['min' => 0, 'max' => 60], |
| 176 |
], |
| 177 |
'default' => [ |
| 178 |
'size' => 16, |
| 179 |
'unit' => 'px', |
| 180 |
], |
| 181 |
'selectors' => [ |
| 182 |
'{{WRAPPER}} .king-addons-tb-archive-posts__grid' => 'gap: {{SIZE}}{{UNIT}};', |
| 183 |
], |
| 184 |
] |
| 185 |
); |
| 186 |
|
| 187 |
$this->add_control( |
| 188 |
'kng_show_image', |
| 189 |
[ |
| 190 |
'label' => esc_html__('Show Featured Image', 'king-addons'), |
| 191 |
'type' => Controls_Manager::SWITCHER, |
| 192 |
'return_value' => 'yes', |
| 193 |
'default' => 'yes', |
| 194 |
] |
| 195 |
); |
| 196 |
|
| 197 |
$this->add_control( |
| 198 |
'kng_show_title', |
| 199 |
[ |
| 200 |
'label' => esc_html__('Show Title', 'king-addons'), |
| 201 |
'type' => Controls_Manager::SWITCHER, |
| 202 |
'return_value' => 'yes', |
| 203 |
'default' => 'yes', |
| 204 |
] |
| 205 |
); |
| 206 |
|
| 207 |
$this->add_control( |
| 208 |
'kng_show_meta', |
| 209 |
[ |
| 210 |
'label' => esc_html__('Show Meta', 'king-addons'), |
| 211 |
'type' => Controls_Manager::SWITCHER, |
| 212 |
'return_value' => 'yes', |
| 213 |
'default' => 'yes', |
| 214 |
] |
| 215 |
); |
| 216 |
|
| 217 |
$this->add_control( |
| 218 |
'kng_show_excerpt', |
| 219 |
[ |
| 220 |
'label' => esc_html__('Show Excerpt', 'king-addons'), |
| 221 |
'type' => Controls_Manager::SWITCHER, |
| 222 |
'return_value' => 'yes', |
| 223 |
'default' => '', |
| 224 |
] |
| 225 |
); |
| 226 |
|
| 227 |
$this->add_control( |
| 228 |
'kng_show_button', |
| 229 |
[ |
| 230 |
'label' => esc_html__('Show Read More', 'king-addons'), |
| 231 |
'type' => Controls_Manager::SWITCHER, |
| 232 |
'return_value' => 'yes', |
| 233 |
'default' => '', |
| 234 |
] |
| 235 |
); |
| 236 |
|
| 237 |
$this->end_controls_section(); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Style controls. |
| 242 |
* |
| 243 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 244 |
* |
| 245 |
* @return void |
| 246 |
*/ |
| 247 |
protected function register_style_controls(bool $is_pro): void |
| 248 |
{ |
| 249 |
$this->start_controls_section( |
| 250 |
'kng_style_section', |
| 251 |
[ |
| 252 |
'label' => esc_html__('Style', 'king-addons'), |
| 253 |
'tab' => Controls_Manager::TAB_STYLE, |
| 254 |
] |
| 255 |
); |
| 256 |
|
| 257 |
$this->add_group_control( |
| 258 |
Group_Control_Typography::get_type(), |
| 259 |
[ |
| 260 |
'name' => 'kng_title_typography', |
| 261 |
'label' => esc_html__('Title Typography', 'king-addons'), |
| 262 |
'selector' => '{{WRAPPER}} .king-addons-tb-archive-posts__title', |
| 263 |
] |
| 264 |
); |
| 265 |
|
| 266 |
$this->add_group_control( |
| 267 |
Group_Control_Typography::get_type(), |
| 268 |
[ |
| 269 |
'name' => 'kng_meta_typography', |
| 270 |
'label' => esc_html__('Meta Typography', 'king-addons'), |
| 271 |
'selector' => '{{WRAPPER}} .king-addons-tb-archive-posts__meta', |
| 272 |
] |
| 273 |
); |
| 274 |
|
| 275 |
$this->add_control( |
| 276 |
'kng_title_color', |
| 277 |
[ |
| 278 |
'label' => esc_html__('Title Color', 'king-addons'), |
| 279 |
'type' => Controls_Manager::COLOR, |
| 280 |
'selectors' => [ |
| 281 |
'{{WRAPPER}} .king-addons-tb-archive-posts__title a' => 'color: {{VALUE}};', |
| 282 |
], |
| 283 |
] |
| 284 |
); |
| 285 |
|
| 286 |
$this->add_control( |
| 287 |
'kng_meta_color', |
| 288 |
[ |
| 289 |
'label' => esc_html__('Meta Color', 'king-addons'), |
| 290 |
'type' => Controls_Manager::COLOR, |
| 291 |
'selectors' => [ |
| 292 |
'{{WRAPPER}} .king-addons-tb-archive-posts__meta' => 'color: {{VALUE}};', |
| 293 |
], |
| 294 |
] |
| 295 |
); |
| 296 |
|
| 297 |
$this->end_controls_section(); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Pro upsell. |
| 302 |
* |
| 303 |
* @return void |
| 304 |
*/ |
| 305 |
protected function register_pro_notice_controls(): void |
| 306 |
{ |
| 307 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
Core::renderProFeaturesSection( |
| 312 |
$this, |
| 313 |
'', |
| 314 |
Controls_Manager::RAW_HTML, |
| 315 |
'tb-archive-posts', |
| 316 |
[ |
| 317 |
'Card presets and animations', |
| 318 |
'Built-in pagination and skins', |
| 319 |
] |
| 320 |
); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Render output helper. |
| 325 |
* |
| 326 |
* @param array<string, mixed> $settings Settings. |
| 327 |
* @param bool $is_pro Pro flag. |
| 328 |
* |
| 329 |
* @return void |
| 330 |
*/ |
| 331 |
protected function render_output(array $settings, bool $is_pro): void |
| 332 |
{ |
| 333 |
global $wp_query; |
| 334 |
if (!$wp_query instanceof \WP_Query || !$wp_query->have_posts()) { |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
$wp_query->rewind_posts(); |
| 339 |
|
| 340 |
$columns = isset($settings['kng_columns']['size']) ? max(1, (int) $settings['kng_columns']['size']) : 3; |
| 341 |
$layout = $settings['kng_layout'] ?? 'grid'; |
| 342 |
|
| 343 |
$classes = [ |
| 344 |
'king-addons-tb-archive-posts', |
| 345 |
'king-addons-tb-archive-posts--' . $layout, |
| 346 |
]; |
| 347 |
|
| 348 |
echo '<div class="' . esc_attr(implode(' ', $classes)) . '" style="--kng-archive-columns:' . esc_attr((string) $columns) . '">'; |
| 349 |
echo '<div class="king-addons-tb-archive-posts__grid">'; |
| 350 |
|
| 351 |
while ($wp_query->have_posts()) { |
| 352 |
$wp_query->the_post(); |
| 353 |
$this->render_card($settings); |
| 354 |
} |
| 355 |
|
| 356 |
echo '</div></div>'; |
| 357 |
|
| 358 |
wp_reset_postdata(); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Render single card. |
| 363 |
* |
| 364 |
* @param array<string, mixed> $settings Settings. |
| 365 |
* |
| 366 |
* @return void |
| 367 |
*/ |
| 368 |
protected function render_card(array $settings): void |
| 369 |
{ |
| 370 |
echo '<article class="king-addons-tb-archive-posts__item">'; |
| 371 |
|
| 372 |
if ('yes' === ($settings['kng_show_image'] ?? 'yes') && has_post_thumbnail()) { |
| 373 |
echo '<a class="king-addons-tb-archive-posts__thumb" href="' . esc_url(get_permalink()) . '">'; |
| 374 |
the_post_thumbnail('medium'); |
| 375 |
echo '</a>'; |
| 376 |
} |
| 377 |
|
| 378 |
if ('yes' === ($settings['kng_show_title'] ?? 'yes')) { |
| 379 |
echo '<h3 class="king-addons-tb-archive-posts__title"><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></h3>'; |
| 380 |
} |
| 381 |
|
| 382 |
if ('yes' === ($settings['kng_show_meta'] ?? 'yes')) { |
| 383 |
echo '<div class="king-addons-tb-archive-posts__meta">' . esc_html(get_the_date()) . '</div>'; |
| 384 |
} |
| 385 |
|
| 386 |
if ('yes' === ($settings['kng_show_excerpt'] ?? '')) { |
| 387 |
echo '<div class="king-addons-tb-archive-posts__excerpt">' . esc_html(wp_trim_words(get_the_excerpt(), 18, '…')) . '</div>'; |
| 388 |
} |
| 389 |
|
| 390 |
if ('yes' === ($settings['kng_show_button'] ?? '')) { |
| 391 |
echo '<a class="king-addons-tb-archive-posts__button" href="' . esc_url(get_permalink()) . '">' . esc_html__('Read more', 'king-addons') . '</a>'; |
| 392 |
} |
| 393 |
|
| 394 |
echo '</article>'; |
| 395 |
} |
| 396 |
} |
| 397 |
|