| 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 |
// TODO: Remove the 'e-animations' registration in v3.26.0 [ED-15471]. |
| 40 |
'e-animations' => [ |
| 41 |
'src' => $this->get_css_assets_url( 'animations', 'assets/lib/animations/', true ), |
| 42 |
'version' => ELEMENTOR_VERSION, |
| 43 |
'dependencies' => [], |
| 44 |
], |
| 45 |
'e-shapes' => [ |
| 46 |
'src' => $this->get_css_assets_url( 'shapes', 'assets/css/conditionals/' ), |
| 47 |
'version' => ELEMENTOR_VERSION, |
| 48 |
'dependencies' => [], |
| 49 |
], |
| 50 |
'e-swiper' => [ |
| 51 |
'src' => $this->get_css_assets_url( 'e-swiper', 'assets/css/conditionals/' ), |
| 52 |
'version' => ELEMENTOR_VERSION, |
| 53 |
'dependencies' => [ 'swiper' ], |
| 54 |
], |
| 55 |
'swiper' => [ |
| 56 |
'src' => $this->get_css_assets_url( 'swiper', $this->getSwiperPath() ), |
| 57 |
'version' => $this->getSwiperVersion(), |
| 58 |
'dependencies' => [], |
| 59 |
], |
| 60 |
]; |
| 61 |
|
| 62 |
return array_merge( $styles, $this->get_animation_styles() ); |
| 63 |
} |
| 64 |
|
| 65 |
private function getSwiperPath(): string { |
| 66 |
return Plugin::$instance->experiments->is_feature_active( 'e_swiper_latest' ) ? 'assets/lib/swiper/v8/css/' : 'assets/lib/swiper/css/'; |
| 67 |
} |
| 68 |
|
| 69 |
private function getSwiperVersion(): string { |
| 70 |
return Plugin::$instance->experiments->is_feature_active( 'e_swiper_latest' ) ? '8.4.5' : '5.3.6'; |
| 71 |
} |
| 72 |
|
| 73 |
private function get_animations(): array { |
| 74 |
$grouped_animations = Control_Animation::get_default_animations(); |
| 75 |
$grouped_animations['hover'] = Control_Hover_Animation::get_default_animations(); |
| 76 |
$exit_animations = Control_Exit_Animation::get_default_animations(); |
| 77 |
|
| 78 |
$grouped_animations = array_merge( $grouped_animations, $exit_animations ); |
| 79 |
|
| 80 |
$animations = []; |
| 81 |
|
| 82 |
foreach ( $grouped_animations as $group_label => $group ) { |
| 83 |
foreach ( $group as $animation_key => $animation_label ) { |
| 84 |
$animations[ $animation_key ] = $group_label; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return $animations; |
| 89 |
} |
| 90 |
|
| 91 |
private function get_animation_styles(): array { |
| 92 |
$animations = $this->get_animations(); |
| 93 |
$styles = []; |
| 94 |
|
| 95 |
foreach ( $animations as $animation => $group_label ) { |
| 96 |
$style_prefix = 'hover' === $group_label ? 'e-animation-' : ''; |
| 97 |
|
| 98 |
$styles[ 'e-animation-' . $animation ] = [ |
| 99 |
'src' => $this->get_css_assets_url( $style_prefix . $animation, 'assets/lib/animations/styles/' ), |
| 100 |
'version' => ELEMENTOR_VERSION, |
| 101 |
'dependencies' => [], |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
return $styles; |
| 106 |
} |
| 107 |
|
| 108 |
public function get_assets(): array { |
| 109 |
if ( ! $this->assets ) { |
| 110 |
$this->init_assets(); |
| 111 |
} |
| 112 |
|
| 113 |
return $this->assets; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @param array $assets { |
| 118 |
* @type array 'styles' |
| 119 |
* @type array 'scripts' |
| 120 |
* } |
| 121 |
*/ |
| 122 |
public function enable_assets( array $assets_data ): void { |
| 123 |
if ( ! $this->assets ) { |
| 124 |
$this->init_assets(); |
| 125 |
} |
| 126 |
|
| 127 |
foreach ( $assets_data as $assets_type => $assets_list ) { |
| 128 |
foreach ( $assets_list as $asset_name ) { |
| 129 |
$this->assets[ $assets_type ][ $asset_name ]['enabled'] = true; |
| 130 |
|
| 131 |
if ( 'scripts' === $assets_type ) { |
| 132 |
wp_enqueue_script( $asset_name ); |
| 133 |
} else { |
| 134 |
wp_enqueue_style( $asset_name ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @param array $assets { |
| 142 |
* @type array 'styles' |
| 143 |
* @type array 'scripts' |
| 144 |
* } |
| 145 |
*/ |
| 146 |
public function add_assets( array $assets ): void { |
| 147 |
if ( ! $this->assets ) { |
| 148 |
$this->init_assets(); |
| 149 |
} |
| 150 |
|
| 151 |
$this->assets = array_replace_recursive( $this->assets, $assets ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @deprecated 3.22.0 |
| 156 |
*/ |
| 157 |
public function enqueue_assets(): void { |
| 158 |
$assets = $this->get_assets(); |
| 159 |
$is_preview_mode = Plugin::$instance->preview->is_preview_mode(); |
| 160 |
|
| 161 |
foreach ( $assets as $assets_type => $assets_type_data ) { |
| 162 |
foreach ( $assets_type_data as $asset_name => $asset_data ) { |
| 163 |
if ( empty( $asset_data['src'] ) ) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
if ( ! empty( $asset_data['enabled'] ) || $is_preview_mode ) { |
| 168 |
if ( 'scripts' === $assets_type ) { |
| 169 |
wp_enqueue_script( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'], true ); |
| 170 |
} else { |
| 171 |
// TODO: Remove the 'e-animations' registration in v3.26.0 [ED-15471]. |
| 172 |
if ( $this->skip_animations_style( $asset_name ) ) { |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
wp_enqueue_style( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'] ); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// TODO: Remove the 'e-animations' registration in v3.26.0 [ED-15471]. |
| 184 |
private function skip_animations_style( $asset_name ): bool { |
| 185 |
$is_preview = Plugin::$instance->preview->is_preview_mode(); |
| 186 |
|
| 187 |
return $is_preview && 'e-animations' === $asset_name; |
| 188 |
} |
| 189 |
|
| 190 |
private function register_assets(): void { |
| 191 |
$assets = $this->get_assets(); |
| 192 |
|
| 193 |
foreach ( $assets as $assets_type => $assets_type_data ) { |
| 194 |
foreach ( $assets_type_data as $asset_name => $asset_data ) { |
| 195 |
if ( 'scripts' === $assets_type ) { |
| 196 |
wp_register_script( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'], true ); |
| 197 |
} else { |
| 198 |
wp_register_style( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'] ); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
public function __construct() { |
| 205 |
parent::__construct(); |
| 206 |
|
| 207 |
$this->register_assets(); |
| 208 |
} |
| 209 |
} |
| 210 |
|