| 1 |
<?php |
| 2 |
namespace UiCoreElements; |
| 3 |
use Elementor\Plugin; |
| 4 |
|
| 5 |
/** |
| 6 |
* Scripts and Styles Class |
| 7 |
*/ |
| 8 |
class Assets { |
| 9 |
|
| 10 |
function __construct() { |
| 11 |
if ( is_admin() ) { |
| 12 |
add_action( 'admin_enqueue_scripts', [ $this, 'register' ], 5 ); |
| 13 |
add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'enqueue' ], 5 ); |
| 14 |
} else { |
| 15 |
add_action( 'wp_enqueue_scripts', [ $this, 'register' ], 5 ); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Register our app scripts and styles |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function register() { |
| 25 |
$this->register_scripts( $this->get_scripts() ); |
| 26 |
$this->register_styles( $this->get_styles() ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Enqueue our app scripts directly in the Elementor editor |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function enqueue() { |
| 35 |
if ( Plugin::instance()->editor->is_edit_mode() ) { |
| 36 |
wp_enqueue_script('ui-nested-elements', UICORE_ELEMENTS_ASSETS . '/js/utils/nested-elements.js', [], UICORE_ELEMENTS_VERSION, true); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register scripts |
| 42 |
* |
| 43 |
* @param array $scripts |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
private function register_scripts( $scripts ) { |
| 48 |
foreach ( $scripts as $handle => $script ) { |
| 49 |
$deps = isset( $script['deps'] ) ? $script['deps'] : false; |
| 50 |
$in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : false; |
| 51 |
$version = isset( $script['version'] ) ? $script['version'] : UICORE_ELEMENTS_VERSION; |
| 52 |
|
| 53 |
wp_register_script( $handle, $script['src'], $deps, $version, $in_footer ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Register styles |
| 59 |
* |
| 60 |
* @param array $styles |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function register_styles( $styles ) { |
| 65 |
foreach ( $styles as $handle => $style ) { |
| 66 |
$deps = isset( $style['deps'] ) ? $style['deps'] : false; |
| 67 |
|
| 68 |
wp_register_style( $handle, $style['src'], $deps, UICORE_ELEMENTS_VERSION ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get all registered scripts |
| 74 |
* |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function get_scripts() { |
| 78 |
$prefix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 79 |
|
| 80 |
$scripts = [ |
| 81 |
// libs |
| 82 |
'ui-e-countup' => [ |
| 83 |
'src' => UICORE_ELEMENTS_ASSETS . '/js/lib/countUp.umd.js', |
| 84 |
'version' => UICORE_ELEMENTS_VERSION, |
| 85 |
'in_footer' => true |
| 86 |
], |
| 87 |
'ui-e-odometer' => [ |
| 88 |
'src' => UICORE_ELEMENTS_ASSETS . '/js/lib/odometer.js', |
| 89 |
'version' => UICORE_ELEMENTS_VERSION, |
| 90 |
'in_footer' => true |
| 91 |
], |
| 92 |
// utils |
| 93 |
'ui-e-entrance' => [ |
| 94 |
'src' => UICORE_ELEMENTS_ASSETS . '/js/utils/global-entrances.js', |
| 95 |
'version' => UICORE_ELEMENTS_VERSION, |
| 96 |
'in_footer' => true |
| 97 |
], |
| 98 |
'ui-e-ajax-request' => [ |
| 99 |
'src' => UICORE_ELEMENTS_ASSETS . '/js/utils/ajax-request.js', |
| 100 |
'version' => UICORE_ELEMENTS_VERSION, |
| 101 |
'in_footer' => true |
| 102 |
], |
| 103 |
'ui-e-recaptcha' => [ |
| 104 |
'src' => 'https://www.google.com/recaptcha/api.js?render=explicit', |
| 105 |
'version' => UICORE_ELEMENTS_VERSION, |
| 106 |
'in_footer' => false |
| 107 |
], |
| 108 |
'ui-e-recaptcha-v3' => [ |
| 109 |
'src' => 'https://www.google.com/recaptcha/api.js?render=' . get_option('uicore_elements_recaptcha_site_key'), |
| 110 |
'version' => UICORE_ELEMENTS_VERSION, |
| 111 |
'in_footer' => false |
| 112 |
], |
| 113 |
'ui-e-repeater-custom-key' => [ |
| 114 |
'src' => UICORE_ELEMENTS_ASSETS . '/js/utils/repeater-custom-key.js', |
| 115 |
'version' => UICORE_ELEMENTS_VERSION, |
| 116 |
'in_footer' => true |
| 117 |
], |
| 118 |
// components |
| 119 |
'ui-e-counter' => [ |
| 120 |
'src' => UICORE_ELEMENTS_ASSETS . '/js/utils/global-counter.js', |
| 121 |
'version' => UICORE_ELEMENTS_VERSION, |
| 122 |
'deps' => ['ui-e-countup'], |
| 123 |
'in_footer' => true |
| 124 |
], |
| 125 |
'ui-e-carousel' => [ |
| 126 |
'src' => UICORE_ELEMENTS_ASSETS . '/js/utils/global-carousel.js', |
| 127 |
'version' => UICORE_ELEMENTS_VERSION, |
| 128 |
'deps' => ['elementor-frontend'], |
| 129 |
'in_footer' => true |
| 130 |
], |
| 131 |
]; |
| 132 |
|
| 133 |
return $scripts; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get registered styles |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function get_styles() { |
| 142 |
|
| 143 |
$styles = [ |
| 144 |
'ui-e-animation' => [ |
| 145 |
'src' => UICORE_ELEMENTS_ASSETS . '/css/utils/global-animations.css' |
| 146 |
], |
| 147 |
'ui-e-entrance' => [ |
| 148 |
'src' => UICORE_ELEMENTS_ASSETS . '/css/utils/global-entrances.css' |
| 149 |
], |
| 150 |
]; |
| 151 |
|
| 152 |
return $styles; |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|