| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Modules\Animation; |
| 4 |
|
| 5 |
use \Elementor\Controls_Manager; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) exit; |
| 8 |
|
| 9 |
if (!class_exists('MasterAddons\Modules\Animation\Particles')) { |
| 10 |
class Particles |
| 11 |
{ |
| 12 |
private static $_instance = null; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
// Register controls |
| 17 |
add_action('elementor/element/after_section_end', [$this, 'register_controls'], 10, 3); |
| 18 |
|
| 19 |
// Editor preview scripts |
| 20 |
add_action('elementor/preview/enqueue_scripts', [$this, 'enqueue_editor_scripts']); |
| 21 |
|
| 22 |
// Editor template |
| 23 |
add_action('elementor/section/print_template', [$this, '_print_template'], 10, 2); |
| 24 |
add_action('elementor/column/print_template', [$this, '_print_template'], 10, 2); |
| 25 |
add_action('elementor/container/print_template', [$this, '_print_template'], 10, 2); |
| 26 |
|
| 27 |
// Frontend render |
| 28 |
add_action('elementor/frontend/section/before_render', [$this, '_before_render'], 10, 1); |
| 29 |
add_action('elementor/frontend/column/before_render', [$this, '_before_render'], 10, 1); |
| 30 |
add_action('elementor/frontend/container/before_render', [$this, '_before_render'], 10, 1); |
| 31 |
|
| 32 |
// Inline CSS |
| 33 |
add_action('elementor/frontend/section/after_render', [$this, 'after_render']); |
| 34 |
add_action('elementor/frontend/column/after_render', [$this, 'after_render']); |
| 35 |
add_action('elementor/frontend/container/after_render', [$this, 'after_render']); |
| 36 |
} |
| 37 |
|
| 38 |
public function enqueue_editor_scripts() |
| 39 |
{ |
| 40 |
wp_enqueue_script('master-addons-particles'); |
| 41 |
} |
| 42 |
|
| 43 |
public function register_controls($element, $section_id, $args) |
| 44 |
{ |
| 45 |
if ( |
| 46 |
('section' === $element->get_name() && 'section_background' === $section_id) || |
| 47 |
('column' === $element->get_name() && 'section_style' === $section_id) || |
| 48 |
('container' === $element->get_name() && 'section_background' === $section_id) |
| 49 |
) { |
| 50 |
$element->start_controls_section( |
| 51 |
'ma_el_particles', |
| 52 |
[ |
| 53 |
'tab' => Controls_Manager::TAB_STYLE, |
| 54 |
'label' => __('Particles', 'master-addons') . JLTMA_EXTENSION_BADGE |
| 55 |
] |
| 56 |
); |
| 57 |
|
| 58 |
$element->add_control( |
| 59 |
'ma_el_enable_particles', |
| 60 |
[ |
| 61 |
'type' => Controls_Manager::SWITCHER, |
| 62 |
'label' => __('Enable Particle Background', 'master-addons'), |
| 63 |
'default' => '', |
| 64 |
'label_on' => __('Yes', 'master-addons'), |
| 65 |
'label_off' => __('No', 'master-addons'), |
| 66 |
'return_value' => 'yes', |
| 67 |
'prefix_class' => 'jltma-particle-', |
| 68 |
'render_type' => 'template', |
| 69 |
] |
| 70 |
); |
| 71 |
|
| 72 |
$element->add_control( |
| 73 |
'ma_el_particle_area_zindex', |
| 74 |
[ |
| 75 |
'label' => __('Z-index', 'master-addons'), |
| 76 |
'type' => Controls_Manager::NUMBER, |
| 77 |
'default' => 0, |
| 78 |
'condition' => [ |
| 79 |
'ma_el_enable_particles' => 'yes', |
| 80 |
], |
| 81 |
'frontend_available' => true, |
| 82 |
] |
| 83 |
); |
| 84 |
|
| 85 |
$element->add_control( |
| 86 |
'ma_el_enable_particles_alert', |
| 87 |
[ |
| 88 |
'type' => Controls_Manager::RAW_HTML, |
| 89 |
'content_classes' => 'elementor-control-field-description', |
| 90 |
'raw' => __('<a href="https://vincentgarreau.com/particles.js/" target="_blank">Click here</a> to generate JSON for the below field.', 'master-addons'), |
| 91 |
'separator' => 'none', |
| 92 |
'condition' => [ |
| 93 |
'ma_el_enable_particles' => 'yes', |
| 94 |
], |
| 95 |
] |
| 96 |
); |
| 97 |
|
| 98 |
$element->add_control( |
| 99 |
'ma_el_particle_json', |
| 100 |
[ |
| 101 |
'type' => Controls_Manager::CODE, |
| 102 |
'label' => __('Particle JSON', 'master-addons'), |
| 103 |
'default' => '{"particles":{"number":{"value":80,"density":{"enable":true,"value_area":800}},"color":{"value":"#ffffff"},"shape":{"type":"circle","stroke":{"width":0,"color":"#000000"}},"opacity":{"value":0.5,"random":false},"size":{"value":3,"random":true},"line_linked":{"enable":true,"distance":150,"color":"#ffffff","opacity":0.4,"width":1},"move":{"enable":true,"speed":6,"direction":"none","random":false,"straight":false,"out_mode":"out","bounce":false}},"interactivity":{"detect_on":"canvas","events":{"onhover":{"enable":true,"mode":"repulse"},"onclick":{"enable":true,"mode":"push"},"resize":true},"modes":{"repulse":{"distance":200,"duration":0.4},"push":{"particles_nb":4}}},"retina_detect":true}', |
| 104 |
'render_type' => 'template', |
| 105 |
'condition' => [ |
| 106 |
'ma_el_enable_particles' => 'yes' |
| 107 |
] |
| 108 |
] |
| 109 |
); |
| 110 |
|
| 111 |
$element->end_controls_section(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public function _before_render($element) |
| 116 |
{ |
| 117 |
if (!in_array($element->get_name(), ['section', 'column', 'container'])) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
$settings = $element->get_settings(); |
| 122 |
|
| 123 |
if ($settings['ma_el_enable_particles'] === 'yes') { |
| 124 |
$particle_json = $settings['ma_el_particle_json']; |
| 125 |
$particle_data = json_decode($particle_json, true); |
| 126 |
|
| 127 |
if ($particle_data) { |
| 128 |
$element->add_render_attribute('_wrapper', 'data-jltma-particle', wp_json_encode($particle_data)); |
| 129 |
} |
| 130 |
$element->add_render_attribute('_wrapper', 'data-jltma-particle-zindex', $settings['ma_el_particle_area_zindex']); |
| 131 |
|
| 132 |
// Enqueue particles vendor library + module script |
| 133 |
wp_enqueue_script('jltma-particles'); |
| 134 |
wp_enqueue_script('master-addons-particles'); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public function _print_template($template, $widget) |
| 139 |
{ |
| 140 |
if (!in_array($widget->get_name(), ['section', 'column', 'container'])) { |
| 141 |
return $template; |
| 142 |
} |
| 143 |
|
| 144 |
ob_start(); |
| 145 |
?> |
| 146 |
<# if (settings.ma_el_enable_particles === 'yes') { |
| 147 |
var zindex = settings.ma_el_particle_area_zindex || 0; |
| 148 |
#> |
| 149 |
<div class="jltma-particle-wrapper" id="jltma-particle-{{ view.getID() }}" data-jltma-particles-editor="{{ settings.ma_el_particle_json }}" style="position:absolute;top:0;left:0;width:100%;height:100%;overflow:hidden;pointer-events:none;z-index:{{ zindex }};"></div> |
| 150 |
<# } #> |
| 151 |
<?php |
| 152 |
return $template . ob_get_clean(); |
| 153 |
} |
| 154 |
|
| 155 |
public function after_render($element) |
| 156 |
{ |
| 157 |
$data = $element->get_data(); |
| 158 |
$settings = $element->get_settings_for_display(); |
| 159 |
$type = $data['elType']; |
| 160 |
|
| 161 |
if (!in_array($type, ['section', 'column', 'container']) || $settings['ma_el_enable_particles'] !== 'yes') { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$id = $element->get_id(); |
| 166 |
$z = !empty($settings['ma_el_particle_area_zindex']) ? (int) $settings['ma_el_particle_area_zindex'] : 0; |
| 167 |
?> |
| 168 |
<style> |
| 169 |
.elementor-element-<?php echo esc_attr($id); ?>.jltma-particle-yes{position:relative;overflow:hidden;} |
| 170 |
.elementor-element-<?php echo esc_attr($id); ?> .jltma-particle-wrapper{position:absolute;top:0;left:0;width:100%;height:100%;overflow:hidden;pointer-events:none;z-index:<?php echo esc_attr($z); ?>;} |
| 171 |
.elementor-element-<?php echo esc_attr($id); ?> .jltma-particle-wrapper>canvas{position:absolute;top:0;left:0;width:100%!important;height:100%!important;pointer-events:none;} |
| 172 |
</style> |
| 173 |
<?php |
| 174 |
} |
| 175 |
|
| 176 |
public static function instance() |
| 177 |
{ |
| 178 |
if (is_null(self::$_instance)) { |
| 179 |
self::$_instance = new self(); |
| 180 |
} |
| 181 |
return self::$_instance; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
Particles::instance(); |
| 187 |
|