loader.php (2918B)
1 <?php 2 namespace Elementor\Core\Page_Assets; 3 4 use Elementor\Core\Base\Module; 5 use Elementor\Plugin; 6 7 if ( ! defined( 'ABSPATH' ) ) { 8 exit; // Exit if accessed directly 9 } 10 11 /** 12 * Elementor assets loader. 13 * 14 * A class that is responsible for conditionally enqueuing styles and script assets that were pre-enabled. 15 * 16 * @since 3.3.0 17 */ 18 class Loader extends Module { 19 private $assets; 20 21 public function get_name() { 22 return 'assets-loader'; 23 } 24 25 private function init_assets() { 26 $this->assets = [ 27 'styles' => [ 28 'e-animations' => [ 29 'src' => $this->get_css_assets_url( 'animations', 'assets/lib/animations/', true ), 30 'version' => ELEMENTOR_VERSION, 31 'dependencies' => [], 32 ], 33 ], 34 'scripts' => [], 35 ]; 36 } 37 38 public function get_assets() { 39 if ( ! $this->assets ) { 40 $this->init_assets(); 41 } 42 43 return $this->assets; 44 } 45 46 /** 47 * @param array $assets { 48 * @type array 'styles' 49 * @type array 'scripts' 50 * } 51 */ 52 public function enable_assets( array $assets_data ) { 53 if ( ! $this->assets ) { 54 $this->init_assets(); 55 } 56 57 foreach ( $assets_data as $assets_type => $assets_list ) { 58 foreach ( $assets_list as $asset_name ) { 59 $this->assets[ $assets_type ][ $asset_name ]['enabled'] = true; 60 } 61 } 62 } 63 64 /** 65 * @param array $assets { 66 * @type array 'styles' 67 * @type array 'scripts' 68 * } 69 */ 70 public function add_assets( array $assets ) { 71 if ( ! $this->assets ) { 72 $this->init_assets(); 73 } 74 75 $this->assets = array_replace_recursive( $this->assets, $assets ); 76 } 77 78 public function enqueue_assets() { 79 $assets = $this->get_assets(); 80 $is_preview_mode = Plugin::$instance->preview->is_preview_mode(); 81 $is_optimized_assets_loading = Plugin::$instance->experiments->is_feature_active( 'e_optimized_assets_loading' ); 82 83 foreach ( $assets as $assets_type => $assets_type_data ) { 84 foreach ( $assets_type_data as $asset_name => $asset_data ) { 85 if ( ! empty( $asset_data['enabled'] ) || $is_preview_mode || ! $is_optimized_assets_loading ) { 86 if ( 'scripts' === $assets_type ) { 87 wp_enqueue_script( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'], true ); 88 } else { 89 wp_enqueue_style( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'] ); 90 } 91 } 92 } 93 } 94 } 95 96 private function register_assets() { 97 $assets = $this->get_assets(); 98 99 foreach ( $assets as $assets_type => $assets_type_data ) { 100 foreach ( $assets_type_data as $asset_name => $asset_data ) { 101 if ( 'scripts' === $assets_type ) { 102 wp_register_script( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'], true ); 103 } else { 104 wp_register_style( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'] ); 105 } 106 } 107 } 108 } 109 110 public function __construct() { 111 parent::__construct(); 112 113 $this->register_assets(); 114 } 115 }