PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.78
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.78
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_Post_Comments / TB_Post_Comments.php

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

345 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Theme Builder Post Comments 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 comments list and form for the current post.
20 */
21 class TB_Post_Comments 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-post-comments';
31 }
32
33 /**
34 * Widget title.
35 *
36 * @return string
37 */
38 public function get_title(): string
39 {
40 return esc_html__('TB - Post Comments', 'king-addons');
41 }
42
43 /**
44 * Widget icon.
45 *
46 * @return string
47 */
48 public function get_icon(): string
49 {
50 return 'eicon-comments';
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-post-comments-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-post-comments-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 ['comments', 'discussion', 'form', 'theme builder', '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_show',
140 [
141 'label' => esc_html__('Show', 'king-addons'),
142 'type' => Controls_Manager::SELECT,
143 'default' => 'both',
144 'options' => [
145 'list' => esc_html__('Comments List', 'king-addons'),
146 'form' => esc_html__('Comment Form', 'king-addons'),
147 'both' => esc_html__('Both', 'king-addons'),
148 ],
149 ]
150 );
151
152 $this->add_control(
153 'kng_hide_when_closed',
154 [
155 'label' => esc_html__('Hide When Closed', 'king-addons'),
156 'type' => Controls_Manager::SWITCHER,
157 'return_value' => 'yes',
158 'default' => '',
159 ]
160 );
161
162 $this->add_control(
163 'kng_no_comments_text',
164 [
165 'label' => $is_pro ?
166 esc_html__('No Comments Text', 'king-addons') :
167 sprintf(__('No Comments Text %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
168 'type' => Controls_Manager::TEXT,
169 'default' => esc_html__('No comments yet.', 'king-addons'),
170 'classes' => $is_pro ? '' : 'king-addons-pro-control',
171 ]
172 );
173
174 $this->add_control(
175 'kng_order',
176 [
177 'label' => $is_pro ?
178 esc_html__('Order', 'king-addons') :
179 sprintf(__('Order %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
180 'type' => Controls_Manager::SELECT,
181 'default' => 'ASC',
182 'options' => [
183 'ASC' => esc_html__('Oldest First', 'king-addons'),
184 'DESC' => esc_html__('Newest First', 'king-addons'),
185 ],
186 'classes' => $is_pro ? '' : 'king-addons-pro-control',
187 ]
188 );
189
190 $this->end_controls_section();
191 }
192
193 /**
194 * Style controls.
195 *
196 * @param bool $is_pro Whether Pro controls are enabled.
197 *
198 * @return void
199 */
200 protected function register_style_controls(bool $is_pro): void
201 {
202 $this->start_controls_section(
203 'kng_style_section',
204 [
205 'label' => esc_html__('Style', 'king-addons'),
206 'tab' => Controls_Manager::TAB_STYLE,
207 ]
208 );
209
210 $this->add_group_control(
211 Group_Control_Typography::get_type(),
212 [
213 'name' => 'kng_heading_typography',
214 'label' => esc_html__('Heading Typography', 'king-addons'),
215 'selector' => '{{WRAPPER}} .king-addons-tb-post-comments__heading',
216 ]
217 );
218
219 $this->add_group_control(
220 Group_Control_Typography::get_type(),
221 [
222 'name' => 'kng_body_typography',
223 'label' => esc_html__('Text Typography', 'king-addons'),
224 'selector' => '{{WRAPPER}} .king-addons-tb-post-comments, {{WRAPPER}} .king-addons-tb-post-comments a',
225 ]
226 );
227
228 $this->add_control(
229 'kng_text_color',
230 [
231 'label' => esc_html__('Text Color', 'king-addons'),
232 'type' => Controls_Manager::COLOR,
233 'selectors' => [
234 '{{WRAPPER}} .king-addons-tb-post-comments' => 'color: {{VALUE}};',
235 '{{WRAPPER}} .king-addons-tb-post-comments a' => 'color: {{VALUE}};',
236 ],
237 ]
238 );
239
240 $this->add_control(
241 'kng_link_hover_color',
242 [
243 'label' => esc_html__('Link Hover Color', 'king-addons'),
244 'type' => Controls_Manager::COLOR,
245 'selectors' => [
246 '{{WRAPPER}} .king-addons-tb-post-comments a:hover' => 'color: {{VALUE}};',
247 ],
248 ]
249 );
250
251 $this->end_controls_section();
252 }
253
254 /**
255 * Pro upsell.
256 *
257 * @return void
258 */
259 protected function register_pro_notice_controls(): void
260 {
261 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
262 return;
263 }
264
265 Core::renderProFeaturesSection(
266 $this,
267 '',
268 Controls_Manager::RAW_HTML,
269 'tb-post-comments',
270 [
271 'Custom empty text and ordering',
272 'Extended styling for lists and form',
273 ]
274 );
275 }
276
277 /**
278 * Render output helper.
279 *
280 * @param array<string, mixed> $settings Settings.
281 * @param bool $is_pro Whether Pro features are enabled.
282 *
283 * @return void
284 */
285 protected function render_output(array $settings, bool $is_pro): void
286 {
287 $post = get_post();
288 if (!$post instanceof \WP_Post) {
289 return;
290 }
291
292 if (!comments_open($post) && ('yes' === ($settings['kng_hide_when_closed'] ?? ''))) {
293 return;
294 }
295
296 $show = $settings['kng_show'] ?? 'both';
297 $show_list = in_array($show, ['list', 'both'], true);
298 $show_form = in_array($show, ['form', 'both'], true);
299
300 echo '<div class="king-addons-tb-post-comments">';
301
302 if ($show_list) {
303 $comments = get_comments(
304 [
305 'post_id' => $post->ID,
306 'status' => 'approve',
307 'order' => $is_pro ? ($settings['kng_order'] ?? 'ASC') : 'ASC',
308 ]
309 );
310
311 if (!empty($comments)) {
312 echo '<div class="king-addons-tb-post-comments__heading">' . esc_html__('Comments', 'king-addons') . '</div>';
313 echo '<ol class="king-addons-tb-post-comments__list">';
314 foreach ($comments as $comment) {
315 echo '<li class="king-addons-tb-post-comments__item">';
316 echo '<div class="king-addons-tb-post-comments__meta">' . esc_html(get_comment_author($comment)) . ' · ' . esc_html(get_comment_date('', $comment)) . '</div>';
317 echo '<div class="king-addons-tb-post-comments__content">' . wp_kses_post(get_comment_text($comment)) . '</div>';
318 echo '</li>';
319 }
320 echo '</ol>';
321 } else {
322 $empty_text = $is_pro ? ($settings['kng_no_comments_text'] ?? '') : '';
323 if ($empty_text) {
324 echo '<div class="king-addons-tb-post-comments__empty">' . esc_html($empty_text) . '</div>';
325 }
326 }
327 }
328
329 if ($show_form && comments_open($post)) {
330 echo '<div class="king-addons-tb-post-comments__form">';
331 comment_form(
332 [
333 'title_reply' => esc_html__('Leave a comment', 'king-addons'),
334 'label_submit' => esc_html__('Post Comment', 'king-addons'),
335 'comment_notes_after' => '',
336 ],
337 $post->ID
338 );
339 echo '</div>';
340 }
341
342 echo '</div>';
343 }
344 }
345