| 1 |
<?php /** @noinspection PhpUnused, SpellCheckingInspection */ |
| 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 Rotating_Animation |
| 13 |
{ |
| 14 |
private static ?Rotating_Animation $_instance = null; |
| 15 |
|
| 16 |
public static function instance(): Rotating_Animation |
| 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_action('elementor/element/container/section_layout/after_section_end', [__CLASS__, 'addControls'], 1); |
| 28 |
add_action('elementor/element/column/section_advanced/after_section_end', [__CLASS__, 'addControls'], 1); |
| 29 |
add_action('elementor/element/section/section_advanced/after_section_end', [__CLASS__, 'addControls'], 1); |
| 30 |
add_action('elementor/element/common/_section_style/after_section_end', [__CLASS__, 'addControls'], 1); |
| 31 |
add_action('elementor/preview/enqueue_scripts', [__CLASS__, 'enqueueScripts'], 1); |
| 32 |
add_action('elementor/frontend/before_render', [__CLASS__, 'renderAnimation'], 1); |
| 33 |
} |
| 34 |
|
| 35 |
public static function enqueueScripts(): void |
| 36 |
{ |
| 37 |
if (!wp_script_is(KING_ADDONS_ASSETS_UNIQUE_KEY . '- ' . 'rotating-animation' . '-' . 'preview-handler')) { |
| 38 |
wp_enqueue_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . 'rotating-animation' . '-' . 'preview-handler', '', array('jquery'), KING_ADDONS_VERSION); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public static function addControls(Element_Base $element): void |
| 43 |
{ |
| 44 |
$element->start_controls_section( |
| 45 |
'kng_rotating_animation_section', |
| 46 |
[ |
| 47 |
'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Rotating Animation', 'king-addons'), |
| 48 |
'tab' => Controls_Manager::TAB_ADVANCED |
| 49 |
] |
| 50 |
); |
| 51 |
|
| 52 |
$element->add_control( |
| 53 |
'kng_rotating_animation_switch', |
| 54 |
[ |
| 55 |
'label' => esc_html__('Enable Rotating Animation', 'king-addons'), |
| 56 |
'type' => Controls_Manager::SWITCHER, |
| 57 |
'prefix_class' => 'kng-rotating-animation-', |
| 58 |
'frontend_available' => true |
| 59 |
] |
| 60 |
); |
| 61 |
|
| 62 |
$element->add_control( |
| 63 |
'kng_rotating_animation_duration', |
| 64 |
[ |
| 65 |
'label' => esc_html__('Duration of single spin', 'king-addons') . ' (ms)', |
| 66 |
'type' => Controls_Manager::NUMBER, |
| 67 |
'frontend_available' => true, |
| 68 |
'min' => 0, |
| 69 |
'step' => 1, |
| 70 |
'default' => 8000, |
| 71 |
'condition' => [ |
| 72 |
'kng_rotating_animation_switch!' => '' |
| 73 |
] |
| 74 |
] |
| 75 |
); |
| 76 |
|
| 77 |
$element->add_control( |
| 78 |
'kng_rotating_animation_delay', |
| 79 |
[ |
| 80 |
'label' => esc_html__('Animation delay', 'king-addons') . ' (ms)', |
| 81 |
'type' => Controls_Manager::NUMBER, |
| 82 |
'frontend_available' => true, |
| 83 |
'min' => 0, |
| 84 |
'step' => 1, |
| 85 |
'default' => 0, |
| 86 |
'condition' => [ |
| 87 |
'kng_rotating_animation_switch!' => '' |
| 88 |
] |
| 89 |
] |
| 90 |
); |
| 91 |
|
| 92 |
$element->end_controls_section(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Renders the inline CSS for the rotating animation if enabled. |
| 97 |
* Hooked into 'elementor/frontend/before_render'. |
| 98 |
* Prints a <style> tag directly into the HTML output. |
| 99 |
* |
| 100 |
* @param Element_Base $element The current Elementor element object. |
| 101 |
*/ |
| 102 |
public static function renderAnimation(Element_Base $element): void |
| 103 |
{ |
| 104 |
$settings = $element->get_settings_for_display(); |
| 105 |
$element_id = $element->get_id(); |
| 106 |
|
| 107 |
// 1. CHECK THE SWITCHER CONTROL (use 'yes') |
| 108 |
if (empty($settings['kng_rotating_animation_switch']) || 'yes' !== $settings['kng_rotating_animation_switch']) { |
| 109 |
return; // Animation is disabled |
| 110 |
} |
| 111 |
|
| 112 |
// 2. GET ANIMATION VALUES |
| 113 |
$duration = $settings['kng_rotating_animation_duration'] ?? null; |
| 114 |
$delay = $settings['kng_rotating_animation_delay'] ?? 0; |
| 115 |
|
| 116 |
// 3. VALIDATE DURATION |
| 117 |
if (empty($duration) || intval($duration) <= 0) { |
| 118 |
return; // Needs a positive duration |
| 119 |
} |
| 120 |
|
| 121 |
// 4. SANITIZE NUMERIC VALUES (use absint for non-negative ms) |
| 122 |
$duration_sanitized = absint($duration); |
| 123 |
$delay_sanitized = absint($delay); |
| 124 |
|
| 125 |
// 5. GENERATE THE CSS STRING using sprintf |
| 126 |
$animation_name = 'rotating-animation-' . $element_id; |
| 127 |
$selector = '.elementor-element-' . $element_id; |
| 128 |
|
| 129 |
$css = sprintf( |
| 130 |
// Add a unique ID to the style tag |
| 131 |
'<style id="rotating-anim-%1$s"> |
| 132 |
@keyframes %2$s { |
| 133 |
0%% { transform: rotate(0deg); } |
| 134 |
100%% { transform: rotate(360deg); } |
| 135 |
} |
| 136 |
%3$s { |
| 137 |
animation-name: %2$s; |
| 138 |
animation-duration: %4$dms; |
| 139 |
animation-timing-function: linear; /* Linear is suitable for rotation */ |
| 140 |
animation-iteration-count: infinite; |
| 141 |
animation-delay: %5$dms; |
| 142 |
} |
| 143 |
</style>', |
| 144 |
esc_attr($element_id), // %1$s: Sanitized ID for the <style> tag attribute |
| 145 |
$animation_name, // %2$s: Animation name |
| 146 |
$selector, // %3$s: Element selector |
| 147 |
$duration_sanitized, // %4$s: Sanitized duration (non-negative integer) |
| 148 |
$delay_sanitized // %5$s: Sanitized delay (non-negative integer) |
| 149 |
); |
| 150 |
|
| 151 |
// 6. OUTPUT THE GENERATED CSS directly into HTML |
| 152 |
print $css; |
| 153 |
} |
| 154 |
} |