| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Archive Result Count 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 |
* Shows the result count for the current archive query. |
| 20 |
*/ |
| 21 |
class TB_Archive_Result_Count 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-result-count'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('TB - Archive Result Count', 'king-addons'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Widget icon. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function get_icon(): string |
| 49 |
{ |
| 50 |
return 'eicon-counter'; |
| 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-result-count-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-result-count-script']; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Categories. |
| 75 |
* |
| 76 |
* @return array<int, string> |
| 77 |
*/ |
| 78 |
public function get_categories(): array |
| 79 |
{ |
| 80 |
return ['king-addons']; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Keywords. |
| 85 |
* |
| 86 |
* @return array<int, string> |
| 87 |
*/ |
| 88 |
public function get_keywords(): array |
| 89 |
{ |
| 90 |
return ['results', 'count', 'archive', '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_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_control( |
| 139 |
'kng_format', |
| 140 |
[ |
| 141 |
'label' => esc_html__('Format', 'king-addons'), |
| 142 |
'type' => Controls_Manager::TEXT, |
| 143 |
'default' => esc_html__('Showing {from}–{to} of {total} results', 'king-addons'), |
| 144 |
] |
| 145 |
); |
| 146 |
|
| 147 |
$this->add_control( |
| 148 |
'kng_custom_format', |
| 149 |
[ |
| 150 |
'label' => $is_pro ? |
| 151 |
esc_html__('Custom Format', 'king-addons') : |
| 152 |
sprintf(__('Custom Format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 153 |
'type' => Controls_Manager::TEXT, |
| 154 |
'placeholder' => esc_html__('Results {from}-{to} ({total})', 'king-addons'), |
| 155 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 156 |
] |
| 157 |
); |
| 158 |
|
| 159 |
$this->end_controls_section(); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Style controls. |
| 164 |
* |
| 165 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 166 |
* |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
protected function register_style_controls(bool $is_pro): void |
| 170 |
{ |
| 171 |
$this->start_controls_section( |
| 172 |
'kng_style_section', |
| 173 |
[ |
| 174 |
'label' => esc_html__('Style', 'king-addons'), |
| 175 |
'tab' => Controls_Manager::TAB_STYLE, |
| 176 |
] |
| 177 |
); |
| 178 |
|
| 179 |
$this->add_group_control( |
| 180 |
Group_Control_Typography::get_type(), |
| 181 |
[ |
| 182 |
'name' => 'kng_typography', |
| 183 |
'selector' => '{{WRAPPER}} .king-addons-tb-archive-result-count', |
| 184 |
] |
| 185 |
); |
| 186 |
|
| 187 |
$this->add_control( |
| 188 |
'kng_color', |
| 189 |
[ |
| 190 |
'label' => esc_html__('Color', 'king-addons'), |
| 191 |
'type' => Controls_Manager::COLOR, |
| 192 |
'selectors' => [ |
| 193 |
'{{WRAPPER}} .king-addons-tb-archive-result-count' => 'color: {{VALUE}};', |
| 194 |
], |
| 195 |
] |
| 196 |
); |
| 197 |
|
| 198 |
$this->add_responsive_control( |
| 199 |
'kng_alignment', |
| 200 |
[ |
| 201 |
'label' => esc_html__('Alignment', 'king-addons'), |
| 202 |
'type' => Controls_Manager::CHOOSE, |
| 203 |
'options' => [ |
| 204 |
'left' => [ |
| 205 |
'title' => esc_html__('Left', 'king-addons'), |
| 206 |
'icon' => 'eicon-text-align-left', |
| 207 |
], |
| 208 |
'center' => [ |
| 209 |
'title' => esc_html__('Center', 'king-addons'), |
| 210 |
'icon' => 'eicon-text-align-center', |
| 211 |
], |
| 212 |
'right' => [ |
| 213 |
'title' => esc_html__('Right', 'king-addons'), |
| 214 |
'icon' => 'eicon-text-align-right', |
| 215 |
], |
| 216 |
], |
| 217 |
'selectors' => [ |
| 218 |
'{{WRAPPER}} .king-addons-tb-archive-result-count' => 'text-align: {{VALUE}};', |
| 219 |
], |
| 220 |
] |
| 221 |
); |
| 222 |
|
| 223 |
$this->end_controls_section(); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Pro upsell. |
| 228 |
* |
| 229 |
* @return void |
| 230 |
*/ |
| 231 |
protected function register_pro_notice_controls(): void |
| 232 |
{ |
| 233 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
Core::renderProFeaturesSection( |
| 238 |
$this, |
| 239 |
'', |
| 240 |
Controls_Manager::RAW_HTML, |
| 241 |
'tb-archive-result-count', |
| 242 |
[ |
| 243 |
'Custom template format placeholders', |
| 244 |
'Conditional display options', |
| 245 |
] |
| 246 |
); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Render output helper. |
| 251 |
* |
| 252 |
* @param array<string, mixed> $settings Settings. |
| 253 |
* @param bool $is_pro Pro flag. |
| 254 |
* |
| 255 |
* @return void |
| 256 |
*/ |
| 257 |
protected function render_output(array $settings, bool $is_pro): void |
| 258 |
{ |
| 259 |
global $wp_query; |
| 260 |
if (!$wp_query instanceof \WP_Query || 0 === (int) $wp_query->found_posts) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
$total = (int) $wp_query->found_posts; |
| 265 |
$per_page = (int) ($wp_query->get('posts_per_page') ?: 1); |
| 266 |
$paged = max(1, get_query_var('paged', 1)); |
| 267 |
|
| 268 |
$from = (($paged - 1) * $per_page) + 1; |
| 269 |
$to = min($total, $paged * $per_page); |
| 270 |
|
| 271 |
$format = $settings['kng_format'] ?? ''; |
| 272 |
if ($is_pro && !empty($settings['kng_custom_format'])) { |
| 273 |
$format = $settings['kng_custom_format']; |
| 274 |
} |
| 275 |
|
| 276 |
$replacements = [ |
| 277 |
'{from}' => number_format_i18n($from), |
| 278 |
'{to}' => number_format_i18n($to), |
| 279 |
'{total}' => number_format_i18n($total), |
| 280 |
]; |
| 281 |
|
| 282 |
$text = strtr($format, $replacements); |
| 283 |
|
| 284 |
echo '<div class="king-addons-tb-archive-result-count">' . esc_html($text) . '</div>'; |
| 285 |
} |
| 286 |
} |
| 287 |
|