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