wpr-custom-css.php
11 months ago
wpr-parallax.php
11 months ago
wpr-particles.php
11 months ago
wpr-sticky-section.php
11 months ago
wpr-particles.php
184 lines
| 1 | <?php |
| 2 | use Elementor\Controls_Manager; |
| 3 | use WprAddons\Classes\Utilities; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; // Exit if accessed directly. |
| 7 | } |
| 8 | |
| 9 | class Wpr_Particles { |
| 10 | |
| 11 | private static $_instance = null; |
| 12 | |
| 13 | public $default_particles = '{"particles":{"number":{"value":80,"density":{"enable":true,"value_area":800}},"color":{"value":"#000000"},"shape":{"type":"circle","stroke":{"width":0,"color":"#000000"},"polygon":{"nb_sides":5},"image":{"src":"img/github.svg","width":100,"height":100}},"opacity":{"value":0.5,"random":false,"anim":{"enable":false,"speed":1,"opacity_min":0.1,"sync":false}},"size":{"value":3,"random":true,"anim":{"enable":false,"speed":40,"size_min":0.1,"sync":false}},"line_linked":{"enable":true,"distance":150,"color":"#000000","opacity":0.4,"width":1},"move":{"enable":true,"speed":6,"direction":"none","random":false,"straight":false,"out_mode":"out","bounce":false,"attract":{"enable":false,"rotateX":600,"rotateY":1200}}},"interactivity":{"detect_on":"window","events":{"onhover":{"enable":true,"mode":"repulse"},"onclick":{"enable":true,"mode":"push"},"resize":true},"modes":{"grab":{"distance":400,"line_linked":{"opacity":1}},"bubble":{"distance":400,"size":40,"duration":2,"opacity":8,"speed":3},"repulse":{"distance":200,"duration":0.4},"push":{"particles_nb":4},"remove":{"particles_nb":2}}},"retina_detect":true}' ; |
| 14 | |
| 15 | public function __construct() { |
| 16 | add_action( 'elementor/element/section/section_background/after_section_end', [ $this, 'register_controls' ], 10 ); |
| 17 | add_action( 'elementor/frontend/section/before_render', [ $this, '_before_render' ], 10, 1 ); |
| 18 | add_action( 'elementor/section/print_template', [ $this, '_print_template' ], 10, 2 ); |
| 19 | add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 20 | |
| 21 | // FLEXBOX |
| 22 | add_action( 'elementor/element/container/section_layout/after_section_end', [$this, 'register_controls'], 10 ); |
| 23 | add_action( 'elementor/frontend/container/before_render', [$this, '_before_render'], 10, 1 ); |
| 24 | add_action( 'elementor/container/print_template', [ $this, '_print_template' ], 10, 2 ); |
| 25 | } |
| 26 | |
| 27 | public function register_controls( $element ) { |
| 28 | |
| 29 | if ( ( 'section' === $element->get_name() || 'container' === $element->get_name() ) ) { |
| 30 | |
| 31 | $element->start_controls_section ( |
| 32 | 'wpr_section_particles', |
| 33 | [ |
| 34 | 'tab' => Controls_Manager::TAB_STYLE, |
| 35 | 'label' => sprintf(esc_html__('Particles - %s', 'wpr-addons'), Utilities::get_plugin_name()), |
| 36 | ] |
| 37 | ); |
| 38 | |
| 39 | $element->add_control( |
| 40 | 'wpr_particles_apply_changes', |
| 41 | [ |
| 42 | 'type' => Controls_Manager::RAW_HTML, |
| 43 | 'raw' => '<div class="elementor-update-preview-button editor-wpr-preview-update"><span>Update changes to Preview</span><button class="elementor-button elementor-button-success" onclick="elementor.reloadPreview();">Apply</button></div>', |
| 44 | 'separator' => 'after' |
| 45 | ] |
| 46 | ); |
| 47 | |
| 48 | $element->add_control( |
| 49 | 'particles_video_tutorial', |
| 50 | [ |
| 51 | 'raw' => '<br><a href="https://www.youtube.com/watch?v=8OdnaoFSj94" target="_blank">Watch Video Tutorial <span class="dashicons dashicons-video-alt3"></span></a>', |
| 52 | 'type' => Controls_Manager::RAW_HTML, |
| 53 | ] |
| 54 | ); |
| 55 | |
| 56 | $element->add_control ( |
| 57 | 'wpr_enable_particles', |
| 58 | [ |
| 59 | 'type' => Controls_Manager::SWITCHER, |
| 60 | 'label' => esc_html__( 'Enable Particles Background', 'wpr-addons' ), |
| 61 | 'default' => 'no', |
| 62 | 'return_value' => 'yes', |
| 63 | 'prefix_class' => 'wpr-particle-', |
| 64 | 'render_type' => 'template', |
| 65 | ] |
| 66 | ); |
| 67 | |
| 68 | if ( defined('WPR_ADDONS_PRO_VERSION') && wpr_fs()->can_use_premium_code() ) { |
| 69 | \WprAddonsPro\Extensions\Wpr_Particles_Pro::add_control_which_particle($element); |
| 70 | } else { |
| 71 | $element->add_control ( |
| 72 | 'which_particle', |
| 73 | [ |
| 74 | 'label' => __( 'Select Style', 'plugin-domain' ), |
| 75 | 'type' => Controls_Manager::SELECT, |
| 76 | 'default' => 'wpr_particle_json_custom', |
| 77 | 'options' => [ |
| 78 | 'wpr_particle_json_custom' => __( 'Custom', 'plugin-domain' ), |
| 79 | 'pro-pjs' => __( 'Predefined (Pro)', 'plugin-domain' ), |
| 80 | ], |
| 81 | 'condition' => [ |
| 82 | 'wpr_enable_particles' => 'yes' |
| 83 | ] |
| 84 | ] |
| 85 | ); |
| 86 | |
| 87 | // Upgrade to Pro Notice |
| 88 | Utilities::upgrade_pro_notice( $element, Controls_Manager::RAW_HTML, 'particles', 'which_particle', ['pro-pjs'] ); |
| 89 | } |
| 90 | |
| 91 | $this->custom_json_particles( $this->default_particles, $element ); |
| 92 | |
| 93 | if ( defined('WPR_ADDONS_PRO_VERSION') && wpr_fs()->can_use_premium_code() ) { |
| 94 | \WprAddonsPro\Extensions\Wpr_Particles_Pro::add_control_group_predefined_particles($element); |
| 95 | } |
| 96 | |
| 97 | $element->end_controls_section(); |
| 98 | |
| 99 | } // end if() |
| 100 | |
| 101 | } |
| 102 | |
| 103 | public function custom_json_particles($array, $element) { |
| 104 | $element->add_control( |
| 105 | 'wpr_particle_json_custom_notice', |
| 106 | [ |
| 107 | 'type' => Controls_Manager::RAW_HTML, |
| 108 | 'content_classes' => 'elementor-control-field-description', |
| 109 | 'raw' => __('<a href="https://vincentgarreau.com/particles.js/" target="_blank">Click here</a> to generate JSON for the below field.', 'wpr-addons'), |
| 110 | 'condition' => [ |
| 111 | 'which_particle' => 'wpr_particle_json_custom', |
| 112 | 'wpr_enable_particles' => 'yes' |
| 113 | ], |
| 114 | ] |
| 115 | ); |
| 116 | |
| 117 | $element->add_control( |
| 118 | 'wpr_particle_json_custom', |
| 119 | [ |
| 120 | 'type' => Controls_Manager::CODE, |
| 121 | 'label' => esc_html__( 'Enter Custom JSON', 'wpr-addons' ), |
| 122 | 'default' => $array, |
| 123 | 'render_type' => 'template', |
| 124 | 'condition' => [ |
| 125 | 'which_particle' => 'wpr_particle_json_custom', |
| 126 | 'wpr_enable_particles' => 'yes' |
| 127 | ], |
| 128 | ] |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | public function _print_template( $template, $widget ) { |
| 133 | if ( $widget->get_name() !== 'section' && $widget->get_name() !== 'container' ) { |
| 134 | return $template; |
| 135 | } |
| 136 | |
| 137 | ob_start(); |
| 138 | |
| 139 | echo '<div class="wpr-particle-wrapper" id="wpr-particle-{{ view.getID() }}" data-wpr-particles-editor="{{ settings[settings.which_particle] }}" particle-source="{{settings.which_particle}}" wpr-quantity="{{settings.quantity}}" wpr-color="{{settings.particles_color}}" wpr-speed="{{settings.particles_speed}}" wpr-shape="{{settings.particles_shape}}" wpr-size="{{settings.particles_size}}"></div>'; |
| 140 | |
| 141 | $particles_content = ob_get_contents(); |
| 142 | |
| 143 | ob_end_clean(); |
| 144 | |
| 145 | return $template . $particles_content; |
| 146 | } |
| 147 | |
| 148 | public function _before_render( $element ) { |
| 149 | if ( $element->get_name() !== 'section' && $element->get_name() !== 'container' ) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | $settings = $element->get_settings(); |
| 154 | |
| 155 | if ( $settings['wpr_enable_particles'] === 'yes' ) { |
| 156 | $settings['which_particle'] = 'pro-pjs' === $settings['which_particle'] ? 'wpr_particle_json_custom' : $settings['which_particle']; |
| 157 | |
| 158 | if ( !defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code() ) { |
| 159 | $element->add_render_attribute( '_wrapper', [ |
| 160 | 'data-wpr-particles' => $settings[$settings['which_particle']], |
| 161 | 'particle-source' => $settings['which_particle'], |
| 162 | ] ); |
| 163 | } else { |
| 164 | $element->add_render_attribute( '_wrapper', [ |
| 165 | 'data-wpr-particles' => $settings[$settings['which_particle']], |
| 166 | 'particle-source' => $settings['which_particle'], |
| 167 | 'wpr-quantity' => $settings['quantity'], |
| 168 | 'wpr-color' => $settings['particles_color'], |
| 169 | 'wpr-speed' => $settings['particles_speed'], |
| 170 | 'wpr-shape' => $settings['particles_shape'], |
| 171 | 'wpr-size' => $settings['particles_size'] |
| 172 | ] ); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | public function enqueue_scripts() { |
| 178 | wp_enqueue_script( 'wpr-particles', WPR_ADDONS_URL . 'assets/js/lib/particles/particles.js', [ 'jquery' ], '3.0.6', true ); |
| 179 | } |
| 180 | |
| 181 | } |
| 182 | |
| 183 | new Wpr_Particles(); |
| 184 |