| 1 |
<?php /** @noinspection SpellCheckingInspection, PhpUnused */ |
| 2 |
|
| 3 |
namespace King_Addons; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use Elementor\Element_Base; |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Collapse_Expand_Text |
| 13 |
{ |
| 14 |
private static ?Collapse_Expand_Text $_instance = null; |
| 15 |
|
| 16 |
public static function instance(): Collapse_Expand_Text |
| 17 |
{ |
| 18 |
if (is_null(self::$_instance)) { |
| 19 |
self::$_instance = new self(); |
| 20 |
} |
| 21 |
return self::$_instance; |
| 22 |
} |
| 23 |
|
| 24 |
/** @noinspection DuplicatedCode */ |
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
// Add controls to default text widget |
| 28 |
add_action('elementor/element/text-editor/section_style/after_section_end', [__CLASS__, 'addControls'], 1); |
| 29 |
// Add controls to styled text builder widget |
| 30 |
add_action('elementor/element/king-addons-styled-text-builder/kng_styled_txt_style_section_commmon_styles/after_section_end', [__CLASS__, 'addControls'], 1); |
| 31 |
// Add controls to other text-related widgets |
| 32 |
add_action('elementor/element/heading/section_style/after_section_end', [__CLASS__, 'addControls'], 1); |
| 33 |
// Add controls to common elements as well for flexibility |
| 34 |
add_action('elementor/element/common/_section_style/after_section_end', [__CLASS__, 'addControls'], 1); |
| 35 |
|
| 36 |
// Enqueue scripts for editor preview |
| 37 |
add_action('elementor/preview/enqueue_scripts', [__CLASS__, 'enqueueScripts'], 1); |
| 38 |
add_action('wp_enqueue_scripts', [__CLASS__, 'enqueueScripts'], 1); |
| 39 |
|
| 40 |
// Add CSS and data attributes to output |
| 41 |
add_action('elementor/frontend/before_render', [__CLASS__, 'renderCSS'], 1); |
| 42 |
add_action('elementor/frontend/before_render', [__CLASS__, 'addDataAttributes'], 1); |
| 43 |
} |
| 44 |
|
| 45 |
public static function enqueueScripts(): void |
| 46 |
{ |
| 47 |
$script_key = KING_ADDONS_ASSETS_UNIQUE_KEY . '-collapse-expand-text-script'; |
| 48 |
$preview_key = KING_ADDONS_ASSETS_UNIQUE_KEY . '-collapse-expand-text-preview-handler'; |
| 49 |
$style_key = KING_ADDONS_ASSETS_UNIQUE_KEY . '-collapse-expand-text-style'; |
| 50 |
|
| 51 |
if (!wp_script_is($script_key)) { |
| 52 |
wp_enqueue_script( |
| 53 |
$script_key, |
| 54 |
KING_ADDONS_URL . 'includes/features/Collapse_Expand_Text/script.js', |
| 55 |
array('jquery'), |
| 56 |
KING_ADDONS_VERSION, |
| 57 |
true |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
// Enqueue preview handler for editor mode |
| 62 |
if (!wp_script_is($preview_key)) { |
| 63 |
wp_enqueue_script( |
| 64 |
$preview_key, |
| 65 |
KING_ADDONS_URL . 'includes/features/Collapse_Expand_Text/preview-handler.js', |
| 66 |
array('jquery'), |
| 67 |
KING_ADDONS_VERSION, |
| 68 |
true |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
if (!wp_style_is($style_key)) { |
| 73 |
wp_enqueue_style( |
| 74 |
$style_key, |
| 75 |
KING_ADDONS_URL . 'includes/features/Collapse_Expand_Text/style.css', |
| 76 |
array(), |
| 77 |
KING_ADDONS_VERSION |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public static function addControls(Element_Base $element): void |
| 83 |
{ |
| 84 |
$element->start_controls_section( |
| 85 |
'kng_collapse_expand_text_section', |
| 86 |
[ |
| 87 |
'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Collapse & Expand Text', 'king-addons'), |
| 88 |
'tab' => Controls_Manager::TAB_ADVANCED |
| 89 |
] |
| 90 |
); |
| 91 |
|
| 92 |
$element->add_control( |
| 93 |
'kng_collapse_expand_text_enable', |
| 94 |
[ |
| 95 |
'label' => esc_html__('Enable Collapse & Expand', 'king-addons'), |
| 96 |
'type' => Controls_Manager::SWITCHER, |
| 97 |
'label_on' => esc_html__('Yes', 'king-addons'), |
| 98 |
'label_off' => esc_html__('No', 'king-addons'), |
| 99 |
'return_value' => 'yes', |
| 100 |
'default' => '', |
| 101 |
'prefix_class' => 'kng-collapse-expand-', |
| 102 |
'frontend_available' => true |
| 103 |
] |
| 104 |
); |
| 105 |
|
| 106 |
$element->add_control( |
| 107 |
'kng_collapse_expand_text_height', |
| 108 |
[ |
| 109 |
'label' => esc_html__('Collapsed Height', 'king-addons'), |
| 110 |
'type' => Controls_Manager::SLIDER, |
| 111 |
'size_units' => ['px'], |
| 112 |
'range' => [ |
| 113 |
'px' => [ |
| 114 |
'min' => 20, |
| 115 |
'max' => 500, |
| 116 |
'step' => 5, |
| 117 |
], |
| 118 |
], |
| 119 |
'default' => [ |
| 120 |
'unit' => 'px', |
| 121 |
'size' => 80, |
| 122 |
], |
| 123 |
'description' => esc_html__('Height to show when collapsed', 'king-addons'), |
| 124 |
'frontend_available' => true, |
| 125 |
'condition' => [ |
| 126 |
'kng_collapse_expand_text_enable' => 'yes' |
| 127 |
] |
| 128 |
] |
| 129 |
); |
| 130 |
|
| 131 |
$element->add_control( |
| 132 |
'kng_collapse_expand_text_show_more_text', |
| 133 |
[ |
| 134 |
'label' => esc_html__('Show More Text', 'king-addons'), |
| 135 |
'type' => Controls_Manager::TEXT, |
| 136 |
'default' => esc_html__('Read more', 'king-addons'), |
| 137 |
'placeholder' => esc_html__('Read more', 'king-addons'), |
| 138 |
'frontend_available' => true, |
| 139 |
'condition' => [ |
| 140 |
'kng_collapse_expand_text_enable' => 'yes' |
| 141 |
] |
| 142 |
] |
| 143 |
); |
| 144 |
|
| 145 |
$element->add_control( |
| 146 |
'kng_collapse_expand_text_show_less_text', |
| 147 |
[ |
| 148 |
'label' => esc_html__('Show Less Text', 'king-addons'), |
| 149 |
'type' => Controls_Manager::TEXT, |
| 150 |
'default' => esc_html__('Read less', 'king-addons'), |
| 151 |
'placeholder' => esc_html__('Read less', 'king-addons'), |
| 152 |
'frontend_available' => true, |
| 153 |
'condition' => [ |
| 154 |
'kng_collapse_expand_text_enable' => 'yes' |
| 155 |
] |
| 156 |
] |
| 157 |
); |
| 158 |
|
| 159 |
$element->add_control( |
| 160 |
'kng_collapse_expand_text_animation_duration', |
| 161 |
[ |
| 162 |
'label' => esc_html__('Animation Duration', 'king-addons'), |
| 163 |
'type' => Controls_Manager::SLIDER, |
| 164 |
'size_units' => ['ms'], |
| 165 |
'range' => [ |
| 166 |
'ms' => [ |
| 167 |
'min' => 100, |
| 168 |
'max' => 2000, |
| 169 |
'step' => 50, |
| 170 |
], |
| 171 |
], |
| 172 |
'default' => [ |
| 173 |
'unit' => 'ms', |
| 174 |
'size' => 300, |
| 175 |
], |
| 176 |
'frontend_available' => true, |
| 177 |
'condition' => [ |
| 178 |
'kng_collapse_expand_text_enable' => 'yes' |
| 179 |
] |
| 180 |
] |
| 181 |
); |
| 182 |
|
| 183 |
$element->add_control( |
| 184 |
'kng_collapse_expand_text_divider_style', |
| 185 |
[ |
| 186 |
'type' => Controls_Manager::DIVIDER, |
| 187 |
'condition' => [ |
| 188 |
'kng_collapse_expand_text_enable' => 'yes' |
| 189 |
] |
| 190 |
] |
| 191 |
); |
| 192 |
|
| 193 |
$element->add_control( |
| 194 |
'kng_collapse_expand_text_button_position', |
| 195 |
[ |
| 196 |
'label' => esc_html__('Button Position', 'king-addons'), |
| 197 |
'type' => Controls_Manager::SELECT, |
| 198 |
'default' => 'right', |
| 199 |
'options' => [ |
| 200 |
'left' => esc_html__('Left', 'king-addons'), |
| 201 |
'center' => esc_html__('Center', 'king-addons'), |
| 202 |
'right' => esc_html__('Right', 'king-addons'), |
| 203 |
], |
| 204 |
'frontend_available' => true, |
| 205 |
'condition' => [ |
| 206 |
'kng_collapse_expand_text_enable' => 'yes' |
| 207 |
] |
| 208 |
] |
| 209 |
); |
| 210 |
|
| 211 |
$element->add_control( |
| 212 |
'kng_collapse_expand_text_button_color', |
| 213 |
[ |
| 214 |
'label' => esc_html__('Button Color', 'king-addons'), |
| 215 |
'type' => Controls_Manager::COLOR, |
| 216 |
'default' => '#007cba', |
| 217 |
'selectors' => [ |
| 218 |
'{{WRAPPER}} .kng-collapse-expand-button' => 'color: {{VALUE}};', |
| 219 |
], |
| 220 |
'condition' => [ |
| 221 |
'kng_collapse_expand_text_enable' => 'yes' |
| 222 |
] |
| 223 |
] |
| 224 |
); |
| 225 |
|
| 226 |
$element->add_control( |
| 227 |
'kng_collapse_expand_text_button_hover_color', |
| 228 |
[ |
| 229 |
'label' => esc_html__('Button Hover Color', 'king-addons'), |
| 230 |
'type' => Controls_Manager::COLOR, |
| 231 |
'default' => '#005a87', |
| 232 |
'selectors' => [ |
| 233 |
'{{WRAPPER}} .kng-collapse-expand-button:hover' => 'color: {{VALUE}};', |
| 234 |
], |
| 235 |
'condition' => [ |
| 236 |
'kng_collapse_expand_text_enable' => 'yes' |
| 237 |
] |
| 238 |
] |
| 239 |
); |
| 240 |
|
| 241 |
$element->add_control( |
| 242 |
'kng_collapse_expand_text_button_font_size', |
| 243 |
[ |
| 244 |
'label' => esc_html__('Button Font Size', 'king-addons'), |
| 245 |
'type' => Controls_Manager::SLIDER, |
| 246 |
'size_units' => ['px', 'em', 'rem'], |
| 247 |
'range' => [ |
| 248 |
'px' => [ |
| 249 |
'min' => 10, |
| 250 |
'max' => 50, |
| 251 |
'step' => 1, |
| 252 |
], |
| 253 |
'em' => [ |
| 254 |
'min' => 0.5, |
| 255 |
'max' => 3, |
| 256 |
'step' => 0.1, |
| 257 |
], |
| 258 |
'rem' => [ |
| 259 |
'min' => 0.5, |
| 260 |
'max' => 3, |
| 261 |
'step' => 0.1, |
| 262 |
], |
| 263 |
], |
| 264 |
'default' => [ |
| 265 |
'unit' => 'px', |
| 266 |
'size' => 14, |
| 267 |
], |
| 268 |
'selectors' => [ |
| 269 |
'{{WRAPPER}} .kng-collapse-expand-button' => 'font-size: {{SIZE}}{{UNIT}};', |
| 270 |
], |
| 271 |
'condition' => [ |
| 272 |
'kng_collapse_expand_text_enable' => 'yes' |
| 273 |
] |
| 274 |
] |
| 275 |
); |
| 276 |
|
| 277 |
// $element->add_control( |
| 278 |
// 'kng_collapse_expand_text_fade_effect', |
| 279 |
// [ |
| 280 |
// 'label' => esc_html__('Fade Effect', 'king-addons'), |
| 281 |
// 'type' => Controls_Manager::SWITCHER, |
| 282 |
// 'label_on' => esc_html__('Yes', 'king-addons'), |
| 283 |
// 'label_off' => esc_html__('No', 'king-addons'), |
| 284 |
// 'return_value' => 'yes', |
| 285 |
// 'default' => 'yes', |
| 286 |
// 'description' => esc_html__('Add a fade effect to the collapsed text bottom', 'king-addons'), |
| 287 |
// 'frontend_available' => true, |
| 288 |
// 'condition' => [ |
| 289 |
// 'kng_collapse_expand_text_enable' => 'yes' |
| 290 |
// ] |
| 291 |
// ] |
| 292 |
// ); |
| 293 |
|
| 294 |
$element->end_controls_section(); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Render inline CSS for the collapse/expand functionality |
| 299 |
*/ |
| 300 |
public static function renderCSS(Element_Base $element): void |
| 301 |
{ |
| 302 |
$settings = $element->get_settings_for_display(); |
| 303 |
$element_id = $element->get_id(); |
| 304 |
|
| 305 |
// Only render CSS if the feature is enabled |
| 306 |
if (empty($settings['kng_collapse_expand_text_enable']) || 'yes' !== $settings['kng_collapse_expand_text_enable']) { |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
$collapsed_height = intval($settings['kng_collapse_expand_text_height']['size'] ?? 80); |
| 311 |
$animation_duration = intval($settings['kng_collapse_expand_text_animation_duration']['size'] ?? 300); |
| 312 |
$button_position = $settings['kng_collapse_expand_text_button_position'] ?? 'right'; |
| 313 |
$fade_effect = $settings['kng_collapse_expand_text_fade_effect'] ?? 'yes'; |
| 314 |
|
| 315 |
$css = sprintf( |
| 316 |
'<style id="kng-collapse-expand-%1$s"> |
| 317 |
.elementor-element-%1$s.kng-collapse-expand-yes { |
| 318 |
position: relative; |
| 319 |
} |
| 320 |
.elementor-element-%1$s .kng-collapse-expand-content { |
| 321 |
transition: max-height %2$dms ease; |
| 322 |
overflow: hidden; |
| 323 |
position: relative; |
| 324 |
} |
| 325 |
.elementor-element-%1$s .kng-collapse-expand-content.collapsed { |
| 326 |
max-height: %3$dpx; |
| 327 |
} |
| 328 |
.elementor-element-%1$s .kng-collapse-expand-content.expanded { |
| 329 |
max-height: none; |
| 330 |
} |
| 331 |
.elementor-element-%1$s .kng-collapse-expand-button-wrapper { |
| 332 |
text-align: %4$s; |
| 333 |
margin-top: 10px; |
| 334 |
} |
| 335 |
.elementor-element-%1$s .kng-collapse-expand-button { |
| 336 |
background: none; |
| 337 |
border: none; |
| 338 |
cursor: pointer; |
| 339 |
text-decoration: underline; |
| 340 |
font-family: inherit; |
| 341 |
padding: 5px 0; |
| 342 |
transition: color 0.3s ease; |
| 343 |
} |
| 344 |
.elementor-element-%1$s .kng-collapse-expand-button:focus { |
| 345 |
outline: 2px solid #007cba; |
| 346 |
outline-offset: 2px; |
| 347 |
}', |
| 348 |
esc_attr($element_id), |
| 349 |
$animation_duration, |
| 350 |
$collapsed_height, |
| 351 |
esc_attr($button_position) |
| 352 |
); |
| 353 |
|
| 354 |
// if ('yes' === $fade_effect) { |
| 355 |
// $css .= sprintf( |
| 356 |
// '.elementor-element-%1$s .kng-collapse-expand-content.collapsed::after { |
| 357 |
// content: ""; |
| 358 |
// position: absolute; |
| 359 |
// bottom: 0; |
| 360 |
// left: 0; |
| 361 |
// right: 0; |
| 362 |
// height: 1.6em; |
| 363 |
// background: linear-gradient(transparent, var(--e-global-color-background, #ffffff)); |
| 364 |
// pointer-events: none; |
| 365 |
// }', |
| 366 |
// esc_attr($element_id) |
| 367 |
// ); |
| 368 |
// } |
| 369 |
|
| 370 |
$css .= '</style>'; |
| 371 |
|
| 372 |
echo $css; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Add data attributes to elements for JavaScript |
| 377 |
*/ |
| 378 |
public static function addDataAttributes(Element_Base $element): void |
| 379 |
{ |
| 380 |
$settings = $element->get_settings_for_display(); |
| 381 |
|
| 382 |
// Only add attributes if the feature is enabled |
| 383 |
if (empty($settings['kng_collapse_expand_text_enable']) || 'yes' !== $settings['kng_collapse_expand_text_enable']) { |
| 384 |
return; |
| 385 |
} |
| 386 |
|
| 387 |
$collapsed_height = intval($settings['kng_collapse_expand_text_height']['size'] ?? 80); |
| 388 |
$show_more_text = esc_attr($settings['kng_collapse_expand_text_show_more_text'] ?? 'Read more'); |
| 389 |
$show_less_text = esc_attr($settings['kng_collapse_expand_text_show_less_text'] ?? 'Read less'); |
| 390 |
$animation_duration = intval($settings['kng_collapse_expand_text_animation_duration']['size'] ?? 300); |
| 391 |
$button_position = esc_attr($settings['kng_collapse_expand_text_button_position'] ?? 'right'); |
| 392 |
$fade_effect = $settings['kng_collapse_expand_text_fade_effect'] ?? 'yes'; |
| 393 |
|
| 394 |
// Add data attributes to the element |
| 395 |
$element->add_render_attribute('_wrapper', [ |
| 396 |
'data-kng-collapse-height' => $collapsed_height, |
| 397 |
'data-kng-collapse-show-more' => $show_more_text, |
| 398 |
'data-kng-collapse-show-less' => $show_less_text, |
| 399 |
'data-kng-collapse-duration' => $animation_duration, |
| 400 |
'data-kng-collapse-position' => $button_position, |
| 401 |
'data-kng-collapse-fade' => $fade_effect, |
| 402 |
]); |
| 403 |
} |
| 404 |
} |
| 405 |
|