| 1 |
<?php |
| 2 |
use Elementor\Controls_Manager; |
| 3 |
use Elementor\Repeater; |
| 4 |
use Elementor\Icons_Manager; |
| 5 |
use Elementor\Group_Control_Background; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Easyel_FAQ_Accordion_Widget extends \Elementor\Widget_Base { |
| 12 |
|
| 13 |
public function get_style_depends() { |
| 14 |
$handle = 'eel-faq-accordion-style'; |
| 15 |
$css_path = plugin_dir_path( __FILE__ ) . 'css/faq.min.css'; |
| 16 |
|
| 17 |
if ( ! wp_style_is( $handle, 'registered' ) && file_exists( $css_path ) ) { |
| 18 |
wp_register_style( $handle, plugins_url( 'css/faq.min.css', __FILE__ ), [], defined( 'WP_DEBUG' ) && WP_DEBUG ? filemtime( $css_path ) : EASYELEMENTS_VER ); |
| 19 |
} |
| 20 |
return [ $handle ]; |
| 21 |
} |
| 22 |
|
| 23 |
public function get_script_depends() { |
| 24 |
$handle = 'eel-faq-accordion-script'; |
| 25 |
$js_path = plugin_dir_path( __FILE__ ) . 'js/faq.js'; |
| 26 |
|
| 27 |
if ( ! wp_script_is( $handle, 'registered' ) && file_exists( $js_path ) ) { |
| 28 |
wp_register_script( $handle, plugins_url( 'js/faq.js', __FILE__ ), [ 'jquery' ], defined( 'WP_DEBUG' ) && WP_DEBUG ? filemtime( $js_path ) : EASYELEMENTS_VER, true ); |
| 29 |
} |
| 30 |
return [ $handle ]; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_name() { |
| 34 |
return 'eel-faq-accordion'; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_title() { |
| 38 |
return esc_html__( 'Accordion', 'easy-elements' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function get_icon() { |
| 42 |
return 'easyicon easyelIcon-faq-1'; |
| 43 |
} |
| 44 |
|
| 45 |
public function get_categories() { |
| 46 |
return [ 'easyelements_category' ]; |
| 47 |
} |
| 48 |
|
| 49 |
public function get_keywords() { |
| 50 |
return [ 'faq', 'accordion', 'question', 'answer', 'text' ]; |
| 51 |
} |
| 52 |
|
| 53 |
protected function register_controls() { |
| 54 |
$this->start_controls_section( |
| 55 |
'section_faq_content', |
| 56 |
[ 'label' => esc_html__( 'Easy FAQ Items', 'easy-elements' ) ] |
| 57 |
); |
| 58 |
|
| 59 |
$repeater = new Repeater(); |
| 60 |
|
| 61 |
$repeater->add_control( 'title', [ |
| 62 |
'label' => esc_html__( 'Question', 'easy-elements' ), |
| 63 |
'type' => Controls_Manager::TEXT, |
| 64 |
'label_block' => true, |
| 65 |
'default' => esc_html__( 'What is Easy Elements?', 'easy-elements' ), |
| 66 |
] ); |
| 67 |
|
| 68 |
$repeater->add_control( 'description', [ |
| 69 |
'label' => esc_html__( 'Answer', 'easy-elements' ), |
| 70 |
'type' => Controls_Manager::TEXTAREA, |
| 71 |
'default' => esc_html__( 'Easy Elements is a best plugin for Elementor.', 'easy-elements' ), |
| 72 |
] ); |
| 73 |
|
| 74 |
$repeater->add_control( 'active_toggle', [ |
| 75 |
'label' => esc_html__( 'Active by Default', 'easy-elements' ), |
| 76 |
'type' => Controls_Manager::SWITCHER, |
| 77 |
'label_on' => esc_html__( 'Yes', 'easy-elements' ), |
| 78 |
'label_off' => esc_html__( 'No', 'easy-elements' ), |
| 79 |
'return_value' => 'yes', |
| 80 |
'default' => 'no', |
| 81 |
] ); |
| 82 |
|
| 83 |
$repeater->add_control( 'icon_open', [ |
| 84 |
'label' => esc_html__( 'Open Icon', 'easy-elements' ), |
| 85 |
'type' => Controls_Manager::ICONS, |
| 86 |
'default' => [ '' ], |
| 87 |
] ); |
| 88 |
|
| 89 |
$repeater->add_control( 'icon_close', [ |
| 90 |
'label' => esc_html__( 'Close Icon', 'easy-elements' ), |
| 91 |
'type' => Controls_Manager::ICONS, |
| 92 |
'default' => [ '' ], |
| 93 |
] ); |
| 94 |
|
| 95 |
$this->add_control( 'faq_items', [ |
| 96 |
'label' => esc_html__( 'FAQ List', 'easy-elements' ), |
| 97 |
'type' => Controls_Manager::REPEATER, |
| 98 |
'fields' => $repeater->get_controls(), |
| 99 |
'default' => [ |
| 100 |
[ |
| 101 |
'title' => esc_html__( 'What is Easy Elements?', 'easy-elements' ), |
| 102 |
'description' => esc_html__( 'Easy Elements is a custom Elementor addon plugin that offers useful widgets.', 'easy-elements' ), |
| 103 |
], |
| 104 |
[ |
| 105 |
'title' => esc_html__( 'Does it work with Elementor Free?', 'easy-elements' ), |
| 106 |
'description' => esc_html__( 'Yes, it fully supports Elementor Free version without requiring Elementor Pro.', 'easy-elements' ), |
| 107 |
], |
| 108 |
[ |
| 109 |
'title' => esc_html__( 'How to install Easy Elements?', 'easy-elements' ), |
| 110 |
'description' => esc_html__( 'Upload the plugin via WordPress Dashboard or FTP and activate it.', 'easy-elements' ), |
| 111 |
], |
| 112 |
[ |
| 113 |
'title' => esc_html__( 'What widgets are included?', 'easy-elements' ), |
| 114 |
'description' => esc_html__( 'Widgets include logo grid, testimonials, CTA sections, pricing tables, and more.', 'easy-elements' ), |
| 115 |
], |
| 116 |
[ |
| 117 |
'title' => esc_html__( 'How do I get support or updates?', 'easy-elements' ), |
| 118 |
'description' => esc_html__( 'Support and updates are available via the official website or marketplace.', 'easy-elements' ), |
| 119 |
], |
| 120 |
], |
| 121 |
'title_field' => '{{{ title }}}', |
| 122 |
] ); |
| 123 |
|
| 124 |
$this->add_control( 'title_tag', [ |
| 125 |
'label' => esc_html__( 'Title HTML Tag', 'easy-elements' ), |
| 126 |
'type' => Controls_Manager::SELECT, |
| 127 |
'default' => 'h4', |
| 128 |
'options' => [ |
| 129 |
'h1' => 'H1', |
| 130 |
'h2' => 'H2', |
| 131 |
'h3' => 'H3', |
| 132 |
'h4' => 'H4', |
| 133 |
'h5' => 'H5', |
| 134 |
'h6' => 'H6', |
| 135 |
'div' => 'div', |
| 136 |
'span' => 'span', |
| 137 |
'p' => 'p', |
| 138 |
], |
| 139 |
] ); |
| 140 |
|
| 141 |
$this->add_control( |
| 142 |
'icon_position', |
| 143 |
[ |
| 144 |
'label' => esc_html__( 'Icon Position', 'easy-elements' ), |
| 145 |
'type' => \Elementor\Controls_Manager::CHOOSE, |
| 146 |
'options' => [ |
| 147 |
'row-reverse' => [ |
| 148 |
'title' => esc_html__( 'Left', 'easy-elements' ), |
| 149 |
'icon' => 'eicon-h-align-left', |
| 150 |
], |
| 151 |
'row' => [ |
| 152 |
'title' => esc_html__( 'Right', 'easy-elements' ), |
| 153 |
'icon' => 'eicon-h-align-right', |
| 154 |
], |
| 155 |
], |
| 156 |
'default' => 'row', |
| 157 |
'toggle' => true, |
| 158 |
'selectors' => [ |
| 159 |
'{{WRAPPER}} .eel-faq-question' => 'flex-direction: {{VALUE}}; justify-content: left;', |
| 160 |
], |
| 161 |
] |
| 162 |
); |
| 163 |
|
| 164 |
|
| 165 |
$this->add_control( 'open_all_toggle', [ |
| 166 |
'label' => esc_html__( 'Open All FAQs by Default', 'easy-elements' ), |
| 167 |
'type' => Controls_Manager::SWITCHER, |
| 168 |
'label_on' => esc_html__( 'Yes', 'easy-elements' ), |
| 169 |
'label_off' => esc_html__( 'No', 'easy-elements' ), |
| 170 |
'return_value' => 'yes', |
| 171 |
'default' => 'no', |
| 172 |
] ); |
| 173 |
|
| 174 |
$this->add_control( 'enable_sticky', [ |
| 175 |
'label' => esc_html__( 'Enable Sticky', 'easy-elements' ), |
| 176 |
'type' => Controls_Manager::SWITCHER, |
| 177 |
'label_on' => esc_html__( 'Yes', 'easy-elements' ), |
| 178 |
'label_off' => esc_html__( 'No', 'easy-elements' ), |
| 179 |
'return_value' => 'yes', |
| 180 |
'default' => 'no', |
| 181 |
'condition' => [ |
| 182 |
'open_all_toggle' => 'yes', |
| 183 |
], |
| 184 |
] ); |
| 185 |
|
| 186 |
$this->add_control( 'enable_faq_schema', [ |
| 187 |
'label' => esc_html__( 'Enable FAQ Schema', 'easy-elements' ), |
| 188 |
'type' => Controls_Manager::SWITCHER, |
| 189 |
'label_on' => esc_html__( 'Yes', 'easy-elements' ), |
| 190 |
'label_off' => esc_html__( 'No', 'easy-elements' ), |
| 191 |
'return_value' => 'yes', |
| 192 |
'default' => 'no', |
| 193 |
] ); |
| 194 |
|
| 195 |
$this->end_controls_section(); |
| 196 |
|
| 197 |
$this->start_controls_section( |
| 198 |
'title_style_section', |
| 199 |
[ |
| 200 |
'label' => esc_html__( 'Title', 'easy-elements' ), |
| 201 |
'tab' => Controls_Manager::TAB_STYLE, |
| 202 |
] |
| 203 |
); |
| 204 |
|
| 205 |
// Title Color |
| 206 |
$this->add_control( 'title_color', [ |
| 207 |
'label' => esc_html__( 'Color', 'easy-elements' ), |
| 208 |
'type' => Controls_Manager::COLOR, |
| 209 |
'selectors' => [ |
| 210 |
'{{WRAPPER}} .eel-faq-title' => 'color: {{VALUE}};', |
| 211 |
], |
| 212 |
] ); |
| 213 |
|
| 214 |
// Title Typography |
| 215 |
$this->add_group_control( \Elementor\Group_Control_Typography::get_type(), [ |
| 216 |
'name' => 'title_typography', |
| 217 |
'label' => esc_html__( 'Typography', 'easy-elements' ), |
| 218 |
'selector' => '{{WRAPPER}} .eel-faq-title', |
| 219 |
] ); |
| 220 |
|
| 221 |
$this->end_controls_section(); |
| 222 |
$this->start_controls_section( |
| 223 |
'description_style_section', |
| 224 |
[ |
| 225 |
'label' => esc_html__( 'Description', 'easy-elements' ), |
| 226 |
'tab' => Controls_Manager::TAB_STYLE, |
| 227 |
] |
| 228 |
); |
| 229 |
|
| 230 |
// Description Color |
| 231 |
$this->add_control( 'description_color', [ |
| 232 |
'label' => esc_html__( 'Color', 'easy-elements' ), |
| 233 |
'type' => Controls_Manager::COLOR, |
| 234 |
'selectors' => [ |
| 235 |
'{{WRAPPER}} .eel-faq-answer' => 'color: {{VALUE}};', |
| 236 |
], |
| 237 |
] ); |
| 238 |
|
| 239 |
// Description Typography |
| 240 |
$this->add_group_control( \Elementor\Group_Control_Typography::get_type(), [ |
| 241 |
'name' => 'description_typography', |
| 242 |
'label' => esc_html__( 'Typography', 'easy-elements' ), |
| 243 |
'selector' => '{{WRAPPER}} .eel-faq-answer', |
| 244 |
] ); |
| 245 |
|
| 246 |
$this->end_controls_section(); |
| 247 |
|
| 248 |
$this->start_controls_section( |
| 249 |
'icon_style_section', |
| 250 |
[ |
| 251 |
'label' => esc_html__( 'Icon', 'easy-elements' ), |
| 252 |
'tab' => Controls_Manager::TAB_STYLE, |
| 253 |
] |
| 254 |
); |
| 255 |
|
| 256 |
$this->add_control( 'icon_color', [ |
| 257 |
'label' => esc_html__( 'Color', 'easy-elements' ), |
| 258 |
'type' => Controls_Manager::COLOR, |
| 259 |
'selectors' => [ |
| 260 |
'{{WRAPPER}} .eel-faq-icon, {{WRAPPER}} .eel-faq-icon svg' => 'color: {{VALUE}}; fill: {{VALUE}};', |
| 261 |
], |
| 262 |
] ); |
| 263 |
|
| 264 |
$this->add_responsive_control( |
| 265 |
'icon_position_y', |
| 266 |
[ |
| 267 |
'label' => esc_html__( 'Vertical Position', 'easy-elements' ), |
| 268 |
'type' => \Elementor\Controls_Manager::SLIDER, |
| 269 |
'size_units' => [ 'px', '%' ], |
| 270 |
'range' => [ |
| 271 |
'px' => [ |
| 272 |
'min' => -100, |
| 273 |
'max' => 100, |
| 274 |
'step' => 1, |
| 275 |
], |
| 276 |
'%' => [ |
| 277 |
'min' => -100, |
| 278 |
'max' => 100, |
| 279 |
], |
| 280 |
], |
| 281 |
'default' => [ |
| 282 |
'unit' => 'px', |
| 283 |
'size' => 0, |
| 284 |
], |
| 285 |
'selectors' => [ |
| 286 |
'{{WRAPPER}} .eel-faq-icon' => 'transform: translateY({{SIZE}}{{UNIT}});', |
| 287 |
], |
| 288 |
] |
| 289 |
); |
| 290 |
|
| 291 |
$this->add_responsive_control( |
| 292 |
'icon_position_y_active', |
| 293 |
[ |
| 294 |
'label' => esc_html__( 'Vertical Position Active', 'easy-elements' ), |
| 295 |
'type' => \Elementor\Controls_Manager::SLIDER, |
| 296 |
'size_units' => [ 'px', '%' ], |
| 297 |
'range' => [ |
| 298 |
'px' => [ |
| 299 |
'min' => -100, |
| 300 |
'max' => 100, |
| 301 |
'step' => 1, |
| 302 |
], |
| 303 |
'%' => [ |
| 304 |
'min' => -100, |
| 305 |
'max' => 100, |
| 306 |
], |
| 307 |
], |
| 308 |
'default' => [ |
| 309 |
'unit' => 'px', |
| 310 |
'size' => 0, |
| 311 |
], |
| 312 |
'selectors' => [ |
| 313 |
'{{WRAPPER}} .eel-faq-item.active .eel-faq-icon' => 'transform: translateY({{SIZE}}{{UNIT}});', |
| 314 |
], |
| 315 |
] |
| 316 |
); |
| 317 |
|
| 318 |
$this->end_controls_section(); |
| 319 |
|
| 320 |
$this->start_controls_section( |
| 321 |
'item_box_style_section', |
| 322 |
[ |
| 323 |
'label' => esc_html__( 'Items', 'easy-elements' ), |
| 324 |
'tab' => Controls_Manager::TAB_STYLE, |
| 325 |
] |
| 326 |
); |
| 327 |
|
| 328 |
$this->add_group_control( |
| 329 |
Group_Control_Background::get_type(), |
| 330 |
[ |
| 331 |
'name' => 'faq_background', |
| 332 |
'label' => esc_html__( 'Background Type', 'easy-elements' ), |
| 333 |
'types' => [ 'classic', 'gradient' ], |
| 334 |
'selector' => '{{WRAPPER}} .eel-faq-item', |
| 335 |
] |
| 336 |
); |
| 337 |
|
| 338 |
// Border Radius |
| 339 |
$this->add_responsive_control( 'item_border_radius', [ |
| 340 |
'label' => esc_html__( 'Border Radius', 'easy-elements' ), |
| 341 |
'type' => Controls_Manager::DIMENSIONS, |
| 342 |
'size_units' => [ 'px', '%', 'em' ], |
| 343 |
'selectors' => [ |
| 344 |
'{{WRAPPER}} .eel-faq-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 345 |
], |
| 346 |
] ); |
| 347 |
|
| 348 |
// Border |
| 349 |
$this->add_group_control( \Elementor\Group_Control_Border::get_type(), [ |
| 350 |
'name' => 'item_border', |
| 351 |
'label' => esc_html__( 'Border', 'easy-elements' ), |
| 352 |
'selector' => '{{WRAPPER}} .eel-faq-item', |
| 353 |
] ); |
| 354 |
|
| 355 |
// Shadow |
| 356 |
$this->add_group_control( \Elementor\Group_Control_Box_Shadow::get_type(), [ |
| 357 |
'name' => 'item_shadow', |
| 358 |
'label' => esc_html__( 'Box Shadow', 'easy-elements' ), |
| 359 |
'selector' => '{{WRAPPER}} .eel-faq-item', |
| 360 |
] ); |
| 361 |
|
| 362 |
$this->add_responsive_control( |
| 363 |
'item_padding', |
| 364 |
[ |
| 365 |
'label' => esc_html__('Padding', 'easy-elements'), |
| 366 |
'type' => Controls_Manager::DIMENSIONS, |
| 367 |
'size_units' => ['px'], |
| 368 |
'selectors' => [ |
| 369 |
'{{WRAPPER}} .eel-faq-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 370 |
], |
| 371 |
] |
| 372 |
); |
| 373 |
|
| 374 |
$this->add_responsive_control( |
| 375 |
'item_margin', |
| 376 |
[ |
| 377 |
'label' => esc_html__('Space Between Items', 'easy-elements'), |
| 378 |
'type' => Controls_Manager::SLIDER, |
| 379 |
'size_units' => ['px'], |
| 380 |
'selectors' => [ |
| 381 |
'{{WRAPPER}} .eel-faq-accordion' => 'gap: {{SIZE}}{{UNIT}};', |
| 382 |
], |
| 383 |
] |
| 384 |
); |
| 385 |
|
| 386 |
|
| 387 |
$this->end_controls_section(); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
protected function render() { |
| 392 |
$settings = $this->get_settings_for_display(); |
| 393 |
if ( empty( $settings['faq_items'] ) ) return; |
| 394 |
|
| 395 |
$title_tag = tag_escape( $settings['title_tag'] ?? 'h4' ); |
| 396 |
$open_all_class = ( $settings['open_all_toggle'] === 'yes' ) ? 'eel-faq-open-all' : ''; |
| 397 |
$enable_sticky = ( $settings['enable_sticky'] === 'yes' ) ? 'eel-faq-sticky' : ''; |
| 398 |
|
| 399 |
|
| 400 |
// FAQ Schema Output |
| 401 |
if ( isset( $settings['enable_faq_schema'] ) && $settings['enable_faq_schema'] === 'yes' && ! empty( $settings['faq_items'] ) ) { |
| 402 |
|
| 403 |
$schema = [ |
| 404 |
'@context' => 'https://schema.org', |
| 405 |
'@type' => 'FAQPage', |
| 406 |
'mainEntity' => array_map( function ( $item ) { |
| 407 |
return [ |
| 408 |
'@type' => 'Question', |
| 409 |
'name' => isset( $item['title'] ) ? wp_strip_all_tags( $item['title'] ) : '', |
| 410 |
'acceptedAnswer' => [ |
| 411 |
'@type' => 'Answer', |
| 412 |
'text' => isset( $item['description'] ) ? wp_kses_post( $item['description'] ) : '', |
| 413 |
], |
| 414 |
]; |
| 415 |
}, $settings['faq_items'] ), |
| 416 |
]; |
| 417 |
|
| 418 |
echo '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) . '</script>'; |
| 419 |
} |
| 420 |
|
| 421 |
?> |
| 422 |
|
| 423 |
<div class="eel-faq-accordion <?php echo esc_attr( $open_all_class ); ?> <?php echo esc_attr( $enable_sticky ); ?>" id="<?php echo esc_attr( $this->get_id() ); ?>"> |
| 424 |
<?php foreach ( $settings['faq_items'] as $index => $item ) : |
| 425 |
$is_active = ( ! empty( $item['active_toggle'] ) && $item['active_toggle'] === 'yes' ) ? 'active' : ''; |
| 426 |
$title = isset( $item['title'] ) ? $item['title'] : ''; |
| 427 |
$description = isset( $item['description'] ) ? $item['description'] : ''; |
| 428 |
$icon_open = isset( $item['icon_open'] ) ? $item['icon_open'] : array(); |
| 429 |
$icon_close = isset( $item['icon_close'] ) ? $item['icon_close'] : array(); |
| 430 |
?> |
| 431 |
<div class="eel-faq-item <?php echo esc_attr( $is_active ); ?>"> |
| 432 |
<div class="eel-faq-question"> |
| 433 |
<<?php echo esc_html( $title_tag ); ?> class="eel-faq-title" tabindex="0"> |
| 434 |
<?php echo esc_html( $title ); ?> |
| 435 |
</<?php echo esc_html( $title_tag ); ?>> |
| 436 |
<span class="eel-faq-icon eel-faq-icon-open"> |
| 437 |
<?php |
| 438 |
if ( ! empty( $icon_open ) && ! empty( $icon_open['value'] ) ) { |
| 439 |
Icons_Manager::render_icon( $icon_open, [ 'aria-hidden' => 'true' ] ); |
| 440 |
} else { |
| 441 |
echo '<i class="unicon-close" aria-hidden="true"></i>'; |
| 442 |
} |
| 443 |
?> |
| 444 |
</span> |
| 445 |
<span class="eel-faq-icon eel-faq-icon-close"> |
| 446 |
<?php |
| 447 |
if ( ! empty( $icon_close ) && ! empty( $icon_close['value'] ) ) { |
| 448 |
Icons_Manager::render_icon( $icon_close, [ 'aria-hidden' => 'true' ] ); |
| 449 |
} else { |
| 450 |
echo '<i class="unicon-add" aria-hidden="true"></i>'; |
| 451 |
} |
| 452 |
?> |
| 453 |
</span> |
| 454 |
</div> |
| 455 |
<div class="eel-faq-answer"> |
| 456 |
<?php echo wp_kses_post( $description ); ?> |
| 457 |
</div> |
| 458 |
</div> |
| 459 |
<?php endforeach; ?> |
| 460 |
</div> |
| 461 |
<?php |
| 462 |
} |
| 463 |
} |
| 464 |
?> |