| 1 |
<?php |
| 2 |
namespace Elementor\Core\Page_Assets; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Module; |
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Control_Animation; |
| 7 |
use Elementor\Control_Exit_Animation; |
| 8 |
use Elementor\Control_Hover_Animation; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Elementor assets loader. |
| 16 |
* |
| 17 |
* A class that is responsible for conditionally enqueuing styles and script assets that were pre-enabled. |
| 18 |
* |
| 19 |
* @since 3.3.0 |
| 20 |
*/ |
| 21 |
class Loader extends Module { |
| 22 |
private array $assets = []; |
| 23 |
|
| 24 |
public function get_name(): string { |
| 25 |
return 'assets-loader'; |
| 26 |
} |
| 27 |
|
| 28 |
private function init_assets(): void { |
| 29 |
$assets = [ |
| 30 |
'styles' => $this->init_styles(), |
| 31 |
'scripts' => [], |
| 32 |
]; |
| 33 |
|
| 34 |
$this->assets = $assets; |
| 35 |
} |
| 36 |
|
| 37 |
private function init_styles(): array { |
| 38 |
$styles = [ |
| 39 |
'e-shapes' => [ |
| 40 |
'src' => $this->get_css_assets_url( 'shapes', 'assets/css/conditionals/' ), |
| 41 |
'version' => ELEMENTOR_VERSION, |
| 42 |
'dependencies' => [], |
| 43 |
], |
| 44 |
'e-swiper' => [ |
| 45 |
'src' => $this->get_css_assets_url( 'e-swiper', 'assets/css/conditionals/' ), |
| 46 |
'version' => ELEMENTOR_VERSION, |
| 47 |
'dependencies' => [ 'swiper' ], |
| 48 |
], |
| 49 |
'swiper' => [ |
| 50 |
'src' => $this->get_css_assets_url( 'swiper', 'assets/lib/swiper/v8/css/' ), |
| 51 |
'version' => '8.4.5', |
| 52 |
'dependencies' => [], |
| 53 |
], |
| 54 |
]; |
| 55 |
|
| 56 |
return array_merge( $styles, $this->get_animation_styles() ); |
| 57 |
} |
| 58 |
|
| 59 |
private function get_animations(): array { |
| 60 |
$grouped_animations = Control_Animation::get_default_animations(); |
| 61 |
$grouped_animations['hover'] = Control_Hover_Animation::get_default_animations(); |
| 62 |
$exit_animations = Control_Exit_Animation::get_default_animations(); |
| 63 |
|
| 64 |
$grouped_animations = array_merge( $grouped_animations, $exit_animations ); |
| 65 |
|
| 66 |
$animations = []; |
| 67 |
|
| 68 |
foreach ( $grouped_animations as $group_label => $group ) { |
| 69 |
foreach ( $group as $animation_key => $animation_label ) { |
| 70 |
$animations[ $animation_key ] = $group_label; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $animations; |
| 75 |
} |
| 76 |
|
| 77 |
private function get_animation_styles(): array { |
| 78 |
$animations = $this->get_animations(); |
| 79 |
$styles = []; |
| 80 |
|
| 81 |
foreach ( $animations as $animation => $group_label ) { |
| 82 |
$style_prefix = 'hover' === $group_label ? 'e-animation-' : ''; |
| 83 |
|
| 84 |
$styles[ 'e-animation-' . $animation ] = [ |
| 85 |
'src' => $this->get_css_assets_url( $style_prefix . $animation, 'assets/lib/animations/styles/' ), |
| 86 |
'version' => ELEMENTOR_VERSION, |
| 87 |
'dependencies' => [], |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
return $styles; |
| 92 |
} |
| 93 |
|
| 94 |
public function get_assets(): array { |
| 95 |
if ( ! $this->assets ) { |
| 96 |
$this->init_assets(); |
| 97 |
} |
| 98 |
|
| 99 |
return $this->assets; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param array $assets_data { |
| 104 |
* @type array 'styles' |
| 105 |
* @type array 'scripts' |
| 106 |
* } |
| 107 |
*/ |
| 108 |
public function enable_assets( array $assets_data ): void { |
| 109 |
if ( ! $this->assets ) { |
| 110 |
$this->init_assets(); |
| 111 |
} |
| 112 |
|
| 113 |
foreach ( $assets_data as $assets_type => $assets_list ) { |
| 114 |
foreach ( $assets_list as $asset_name ) { |
| 115 |
$this->assets[ $assets_type ][ $asset_name ]['enabled'] = true; |
| 116 |
|
| 117 |
if ( 'scripts' === $assets_type ) { |
| 118 |
wp_enqueue_script( $asset_name ); |
| 119 |
} else { |
| 120 |
wp_enqueue_style( $asset_name ); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param array $assets { |
| 128 |
* @type array 'styles' |
| 129 |
* @type array 'scripts' |
| 130 |
* } |
| 131 |
*/ |
| 132 |
public function add_assets( array $assets ): void { |
| 133 |
if ( ! $this->assets ) { |
| 134 |
$this->init_assets(); |
| 135 |
} |
| 136 |
|
| 137 |
$this->assets = array_replace_recursive( $this->assets, $assets ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @deprecated 3.22.0 |
| 142 |
*/ |
| 143 |
public function enqueue_assets(): void { |
| 144 |
$assets = $this->get_assets(); |
| 145 |
$is_preview_mode = Plugin::$instance->preview->is_preview_mode(); |
| 146 |
|
| 147 |
foreach ( $assets as $assets_type => $assets_type_data ) { |
| 148 |
foreach ( $assets_type_data as $asset_name => $asset_data ) { |
| 149 |
if ( empty( $asset_data['src'] ) ) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
if ( ! empty( $asset_data['enabled'] ) || $is_preview_mode ) { |
| 154 |
if ( 'scripts' === $assets_type ) { |
| 155 |
wp_enqueue_script( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'], true ); |
| 156 |
} else { |
| 157 |
wp_enqueue_style( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'] ); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
private function register_assets(): void { |
| 165 |
$assets = $this->get_assets(); |
| 166 |
|
| 167 |
foreach ( $assets as $assets_type => $assets_type_data ) { |
| 168 |
foreach ( $assets_type_data as $asset_name => $asset_data ) { |
| 169 |
if ( 'scripts' === $assets_type ) { |
| 170 |
wp_register_script( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'], true ); |
| 171 |
} else { |
| 172 |
wp_register_style( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'] ); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
public function __construct() { |
| 179 |
parent::__construct(); |
| 180 |
|
| 181 |
$this->register_assets(); |
| 182 |
} |
| 183 |
} |
| 184 |
|