PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.49
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.49
51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / widgets / TB_Archive_Result_Count / TB_Archive_Result_Count.php

TB_Archive_Result_Count.php in King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder 51.1.49, at includes/widgets/TB_Archive_Result_Count/TB_Archive_Result_Count.php

282 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
94 * Register controls.
95 *
96 * @return void
97 */
98 public function register_controls(): void
99 {
100 $this->register_content_controls(false);
101 $this->register_style_controls(false);
102 $this->register_pro_notice_controls();
103 }
104
105 /**
106 * Render output.
107 *
108 * @return void
109 */
110 public function render(): void
111 {
112 $settings = $this->get_settings_for_display();
113 $this->render_output($settings, false);
114 }
115
116 /**
117 * Content controls.
118 *
119 * @param bool $is_pro Whether Pro controls are enabled.
120 *
121 * @return void
122 */
123 protected function register_content_controls(bool $is_pro): void
124 {
125 $this->start_controls_section(
126 'kng_content_section',
127 [
128 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content', 'king-addons'),
129 'tab' => Controls_Manager::TAB_CONTENT,
130 ]
131 );
132
133 $this->add_control(
134 'kng_format',
135 [
136 'label' => esc_html__('Format', 'king-addons'),
137 'type' => Controls_Manager::TEXT,
138 'default' => esc_html__('Showing {from}–{to} of {total} results', 'king-addons'),
139 ]
140 );
141
142 $this->add_control(
143 'kng_custom_format',
144 [
145 'label' => $is_pro ?
146 esc_html__('Custom Format', 'king-addons') :
147 sprintf(__('Custom Format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
148 'type' => Controls_Manager::TEXT,
149 'placeholder' => esc_html__('Results {from}-{to} ({total})', 'king-addons'),
150 'classes' => $is_pro ? '' : 'king-addons-pro-control',
151 ]
152 );
153
154 $this->end_controls_section();
155 }
156
157 /**
158 * Style controls.
159 *
160 * @param bool $is_pro Whether Pro controls are enabled.
161 *
162 * @return void
163 */
164 protected function register_style_controls(bool $is_pro): void
165 {
166 $this->start_controls_section(
167 'kng_style_section',
168 [
169 'label' => esc_html__('Style', 'king-addons'),
170 'tab' => Controls_Manager::TAB_STYLE,
171 ]
172 );
173
174 $this->add_group_control(
175 Group_Control_Typography::get_type(),
176 [
177 'name' => 'kng_typography',
178 'selector' => '{{WRAPPER}} .king-addons-tb-archive-result-count',
179 ]
180 );
181
182 $this->add_control(
183 'kng_color',
184 [
185 'label' => esc_html__('Color', 'king-addons'),
186 'type' => Controls_Manager::COLOR,
187 'selectors' => [
188 '{{WRAPPER}} .king-addons-tb-archive-result-count' => 'color: {{VALUE}};',
189 ],
190 ]
191 );
192
193 $this->add_responsive_control(
194 'kng_alignment',
195 [
196 'label' => esc_html__('Alignment', 'king-addons'),
197 'type' => Controls_Manager::CHOOSE,
198 'options' => [
199 'left' => [
200 'title' => esc_html__('Left', 'king-addons'),
201 'icon' => 'eicon-text-align-left',
202 ],
203 'center' => [
204 'title' => esc_html__('Center', 'king-addons'),
205 'icon' => 'eicon-text-align-center',
206 ],
207 'right' => [
208 'title' => esc_html__('Right', 'king-addons'),
209 'icon' => 'eicon-text-align-right',
210 ],
211 ],
212 'selectors' => [
213 '{{WRAPPER}} .king-addons-tb-archive-result-count' => 'text-align: {{VALUE}};',
214 ],
215 ]
216 );
217
218 $this->end_controls_section();
219 }
220
221 /**
222 * Pro upsell.
223 *
224 * @return void
225 */
226 protected function register_pro_notice_controls(): void
227 {
228 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
229 return;
230 }
231
232 Core::renderProFeaturesSection(
233 $this,
234 '',
235 Controls_Manager::RAW_HTML,
236 'tb-archive-result-count',
237 [
238 'Custom template format placeholders',
239 'Conditional display options',
240 ]
241 );
242 }
243
244 /**
245 * Render output helper.
246 *
247 * @param array<string, mixed> $settings Settings.
248 * @param bool $is_pro Pro flag.
249 *
250 * @return void
251 */
252 protected function render_output(array $settings, bool $is_pro): void
253 {
254 global $wp_query;
255 if (!$wp_query instanceof \WP_Query || 0 === (int) $wp_query->found_posts) {
256 return;
257 }
258
259 $total = (int) $wp_query->found_posts;
260 $per_page = (int) ($wp_query->get('posts_per_page') ?: 1);
261 $paged = max(1, get_query_var('paged', 1));
262
263 $from = (($paged - 1) * $per_page) + 1;
264 $to = min($total, $paged * $per_page);
265
266 $format = $settings['kng_format'] ?? '';
267 if ($is_pro && !empty($settings['kng_custom_format'])) {
268 $format = $settings['kng_custom_format'];
269 }
270
271 $replacements = [
272 '{from}' => number_format_i18n($from),
273 '{to}' => number_format_i18n($to),
274 '{total}' => number_format_i18n($total),
275 ];
276
277 $text = strtr($format, $replacements);
278
279 echo '<div class="king-addons-tb-archive-result-count">' . esc_html($text) . '</div>';
280 }
281 }
282