| 1 |
<?php |
| 2 |
/** |
| 3 |
* HootKit Assets Loader |
| 4 |
* This file is loaded during plugin load |
| 5 |
* |
| 6 |
* @package Hootkit |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace HootKit\Inc; |
| 10 |
|
| 11 |
// If this file is called directly, abort. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
die; |
| 14 |
} |
| 15 |
|
| 16 |
if ( ! class_exists( '\HootKit\Inc\Helper_Assets' ) ) : |
| 17 |
|
| 18 |
class Helper_Assets { |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Instance |
| 22 |
*/ |
| 23 |
private static $instance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Registered Assets |
| 27 |
* [ style => [ slug => [src,deps,ver,media] ] script => [ slug => [src,deps,ver,in_footer] ] ] |
| 28 |
*/ |
| 29 |
private static $assets = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Assets to Load |
| 33 |
* [ style => [ slug => [] ] script => [ slug => [] ] ] |
| 34 |
*/ |
| 35 |
private static $load = array(); |
| 36 |
|
| 37 |
/** |
| 38 |
* Admin Assets to Load |
| 39 |
* [ style => [ slug => [ hooks=>[] ] ] script => [ slug => [ hooks=>[] ] ] ] |
| 40 |
*/ |
| 41 |
private static $load_admin = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Screen hooks to Enqueue Media (admin screens) |
| 45 |
*/ |
| 46 |
private static $load_media_hooks = array(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Localize Data |
| 50 |
*/ |
| 51 |
private static $localize = array(); |
| 52 |
|
| 53 |
/** |
| 54 |
* Constructor |
| 55 |
*/ |
| 56 |
public function __construct() { |
| 57 |
if ( empty( self::$assets ) ) { |
| 58 |
add_action( 'plugins_loaded', array( $this, 'assets_list' ) ); |
| 59 |
} |
| 60 |
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue' ) , 10 ); |
| 61 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue' ) , 10 ); |
| 62 |
} |
| 63 |
|
| 64 |
public function wp_enqueue() { $this->enqueue(); } |
| 65 |
public function admin_enqueue( $hook ) { $this->enqueue( $hook, true ); } |
| 66 |
|
| 67 |
/** |
| 68 |
* Enqueue Scripts and Styles |
| 69 |
* |
| 70 |
* @since 2.0.0 |
| 71 |
* @access private |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
private function enqueue( $hook = false, $admin = false ) { |
| 75 |
|
| 76 |
if ( $admin && !empty( self::$load_media_hooks ) && \in_array( $hook, self::$load_media_hooks ) ) |
| 77 |
wp_enqueue_media(); |
| 78 |
|
| 79 |
$loadassets = ( $admin ) ? self::$load_admin : self::$load; |
| 80 |
|
| 81 |
foreach ( $loadassets as $slug => $props ) { |
| 82 |
|
| 83 |
$iscurrenthook = ( ( empty( $props['hooks'] ) || !\is_array( $props['hooks'] ) ) ? |
| 84 |
true : |
| 85 |
\in_array( $hook, $props['hooks'] ) |
| 86 |
); |
| 87 |
|
| 88 |
if ( $iscurrenthook && !empty( self::$assets[ $slug ] ) && is_array( self::$assets[ $slug ] ) ) { |
| 89 |
foreach ( self::$assets[ $slug ] as $type => $args ) { |
| 90 |
|
| 91 |
if ( $type == 'style' ) { |
| 92 |
$handle = !empty( $args['handle'] ) ? $args['handle'] : $slug; |
| 93 |
$src = !empty( $args['src'] ) ? $args['src'] : ''; |
| 94 |
$deps = !empty( $args['deps'] ) ? $args['deps'] : ''; |
| 95 |
$ver = !empty( $args['ver'] ) ? $args['ver'] : hootkit()->version; |
| 96 |
$media = !empty( $args['media'] ) ? $args['media'] : ''; |
| 97 |
if ( $src ) wp_enqueue_style( $handle, $src, $deps, $ver, $media ); |
| 98 |
else wp_enqueue_style( $slug ); |
| 99 |
} |
| 100 |
|
| 101 |
elseif ( $type == 'script' ) { |
| 102 |
$handle = !empty( $args['handle'] ) ? $args['handle'] : $slug; |
| 103 |
$src = !empty( $args['src'] ) ? $args['src'] : ''; |
| 104 |
$deps = !empty( $args['deps'] ) ? $args['deps'] : ''; |
| 105 |
$ver = !empty( $args['ver'] ) ? $args['ver'] : hootkit()->version; |
| 106 |
$footer = !empty( $args['in_footer'] ) ? $args['in_footer'] : ''; |
| 107 |
if ( $src ) wp_enqueue_script( $handle, $src, $deps, $ver, $footer ); |
| 108 |
else wp_enqueue_script( $slug ); |
| 109 |
|
| 110 |
if ( is_array( self::$localize ) && !empty( self::$localize[ $slug ] ) ) { |
| 111 |
foreach ( self::$localize[ $slug ] as $asset ) { |
| 112 |
$scriptdata = $this->localizedata( $asset ); |
| 113 |
if ( !empty( $scriptdata ) ) { |
| 114 |
$h = 'hootkitData' . ucfirst( str_replace([' ', '-'], '', $asset ) ); |
| 115 |
wp_localize_script( $handle, $h, $scriptdata ); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Localize data for asset |
| 131 |
*/ |
| 132 |
private function localizedata( $asset ) { |
| 133 |
$data = array(); |
| 134 |
switch ( $asset ) { |
| 135 |
case 'font-awesome': |
| 136 |
$data['icons'] = hoot_enum_icons('icons'); |
| 137 |
$data['sections'] = hoot_enum_icons('sections'); |
| 138 |
break; |
| 139 |
default: |
| 140 |
break; |
| 141 |
} |
| 142 |
return $data; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Add assets |
| 147 |
* |
| 148 |
* @since 2.0.3 |
| 149 |
* @access public |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public static function add_asset( $slug ) { |
| 153 |
self::$load[ $slug ] = array(); |
| 154 |
} |
| 155 |
public static function add_adminasset( $slug, $hooks = array(), $localize = array() ) { |
| 156 |
if ( $slug == 'wp-media' ) |
| 157 |
self::$load_media_hooks = array_merge( self::$load_media_hooks, $hooks ); |
| 158 |
else |
| 159 |
self::$load_admin[ $slug ] = array( 'hooks' => $hooks ); |
| 160 |
if ( !empty( $localize ) && is_array( $localize ) ) { |
| 161 |
foreach ( $localize as $asset ) { |
| 162 |
if ( !isset( self::$localize[ $slug ] ) || !is_array( self::$localize[ $slug ] ) ) |
| 163 |
self::$localize[ $slug ] = array(); |
| 164 |
if ( ! in_array( $asset, self::$localize[ $slug ] ) ) |
| 165 |
self::$localize[ $slug ][] = $asset; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get asset file uri |
| 172 |
* |
| 173 |
* @since 1.0.0 |
| 174 |
* @access public |
| 175 |
* @param string $location |
| 176 |
* @param string $type |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public static function get_uri( $location, $type ) { |
| 180 |
$dir = hootkit()->dir; |
| 181 |
$uri = hootkit()->uri; |
| 182 |
|
| 183 |
$location = str_replace( array( $dir, $uri ), '', $location ); |
| 184 |
|
| 185 |
// Return minified uri if not in debug mode and minified file is available |
| 186 |
if ( |
| 187 |
( ! defined( 'HKIT_DEBUG' ) || ! HKIT_DEBUG ) && |
| 188 |
( file_exists( $dir . "{$location}.min.{$type}" ) ) |
| 189 |
) { |
| 190 |
return $uri . "{$location}.min.{$type}"; |
| 191 |
} |
| 192 |
|
| 193 |
// Return uri if file is available |
| 194 |
if ( file_exists( $dir . "{$location}.{$type}" ) ) |
| 195 |
return $uri . "{$location}.{$type}"; |
| 196 |
elseif ( file_exists( $dir . "{$location}.min.{$type}" ) ) |
| 197 |
return $uri . "{$location}.min.{$type}"; |
| 198 |
|
| 199 |
return ''; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Array of available Scripts and Styles |
| 205 |
* >> after hootkit() is available (constructor executed) |
| 206 |
* |
| 207 |
* @since 2.0.0 |
| 208 |
* @access public |
| 209 |
* @return void |
| 210 |
*/ |
| 211 |
public function assets_list() { |
| 212 |
self::$assets = array( |
| 213 |
|
| 214 |
/*** frotnend ***/ |
| 215 |
|
| 216 |
'lightslider' => array( |
| 217 |
'style' => array( |
| 218 |
'handle' => 'lightSlider', |
| 219 |
'src' => self::get_uri( 'assets/lightSlider', 'css' ), |
| 220 |
'deps' => '', |
| 221 |
'ver' => '1.1.2', |
| 222 |
'media' => '', |
| 223 |
), |
| 224 |
'script' => array( |
| 225 |
'handle' => 'jquery-lightSlider', |
| 226 |
'src' => self::get_uri( 'assets/jquery.lightSlider', 'js' ), |
| 227 |
'deps' => array( 'jquery' ), |
| 228 |
'ver' => '1.1.2', |
| 229 |
'in_footer' => true, |
| 230 |
), |
| 231 |
), |
| 232 |
|
| 233 |
'font-awesome' => array( |
| 234 |
'style' => array( |
| 235 |
'src' => self::get_uri( 'assets/font-awesome', 'css' ), |
| 236 |
'ver' => '5.0.10', |
| 237 |
), |
| 238 |
), |
| 239 |
|
| 240 |
'circliful' => array( |
| 241 |
'script' => array( |
| 242 |
'handle' => 'jquery-circliful', |
| 243 |
'src' => self::get_uri( 'assets/jquery.circliful', 'js' ), |
| 244 |
'deps' => array( 'jquery' ), // ::=> Hootkit does not load Waypoints. It is upto the theme to deploy waypoints. |
| 245 |
'ver' => '20160309', |
| 246 |
'in_footer' => true, |
| 247 |
), |
| 248 |
), |
| 249 |
|
| 250 |
'widgets' => array( |
| 251 |
'script' => array( |
| 252 |
'handle' => hootkit()->slug . '-widgets', |
| 253 |
'src' => self::get_uri( 'assets/widgets', 'js' ), |
| 254 |
'deps' => array( 'jquery' ), |
| 255 |
'in_footer' => true, |
| 256 |
), |
| 257 |
), |
| 258 |
|
| 259 |
'miscmods' => array( |
| 260 |
'script' => array( |
| 261 |
'handle' => hootkit()->slug . '-miscmods', |
| 262 |
'src' => self::get_uri( 'assets/miscmods', 'js' ), |
| 263 |
'deps' => array( 'jquery' ), |
| 264 |
'in_footer' => true, |
| 265 |
), |
| 266 |
), |
| 267 |
|
| 268 |
/*** admin ***/ |
| 269 |
|
| 270 |
'select2' => array( |
| 271 |
'style' => array( |
| 272 |
'src' => self::get_uri( 'admin/assets/select2', 'css' ), |
| 273 |
'ver' => '4.0.13', |
| 274 |
), |
| 275 |
'script' => array( |
| 276 |
'src' => self::get_uri( 'admin/assets/select2', 'js' ), |
| 277 |
'deps' => array( 'jquery' ), |
| 278 |
'ver' => '4.0.13', |
| 279 |
'in_footer' => true, |
| 280 |
), |
| 281 |
), |
| 282 |
|
| 283 |
'adminsettings' => array( |
| 284 |
'style' => array( |
| 285 |
'handle' => hootkit()->slug . '-adminsettings', |
| 286 |
'src' => self::get_uri( 'admin/assets/settings', 'css' ), |
| 287 |
), |
| 288 |
'script' => array( |
| 289 |
'handle' => hootkit()->slug . '-adminsettings', |
| 290 |
'src' => self::get_uri( 'admin/assets/settings', 'js' ), |
| 291 |
'deps' => array( 'jquery' ), |
| 292 |
), |
| 293 |
), |
| 294 |
'adminsettingsplug' => array( |
| 295 |
'style' => array( |
| 296 |
'handle' => hootkit()->slug . '-adminsettings', |
| 297 |
'src' => self::get_uri( 'admin/assets/settingsplugin', 'css' ), |
| 298 |
), |
| 299 |
'script' => array( |
| 300 |
'handle' => hootkit()->slug . '-adminsettings', |
| 301 |
'src' => self::get_uri( 'admin/assets/settingsplugin', 'js' ), |
| 302 |
'deps' => array( 'jquery' ), |
| 303 |
), |
| 304 |
), |
| 305 |
|
| 306 |
'adminwidgets' => array( |
| 307 |
'style' => array( |
| 308 |
'handle' => hootkit()->slug . '-adminwidgets', |
| 309 |
'src' => self::get_uri( 'admin/assets/widgets', 'css' ), |
| 310 |
), |
| 311 |
'script' => array( |
| 312 |
'handle' => hootkit()->slug . '-adminwidgets', |
| 313 |
'src' => self::get_uri( 'admin/assets/widgets', 'js' ), |
| 314 |
'deps' => array( 'jquery', 'select2', 'wp-color-picker' ), |
| 315 |
'in_footer' => true, |
| 316 |
), |
| 317 |
), |
| 318 |
|
| 319 |
'wp-color-picker' => array( |
| 320 |
'style' => array(), |
| 321 |
'script' => array(), |
| 322 |
), |
| 323 |
|
| 324 |
/*** import ***/ |
| 325 |
|
| 326 |
'hootkitimport' => array( |
| 327 |
'style' => array( |
| 328 |
'handle' => hootkit()->slug . '-import', |
| 329 |
'src' => self::get_uri( 'misc/import/assets/hootkitimport', 'css' ), |
| 330 |
), |
| 331 |
'script' => array( |
| 332 |
'handle' => hootkit()->slug . '-import', |
| 333 |
'src' => self::get_uri( 'misc/import/assets/hootkitimport', 'js' ), |
| 334 |
'deps' => array( 'jquery', 'jquery-confirm' ), |
| 335 |
'in_footer' => true, |
| 336 |
), |
| 337 |
), |
| 338 |
|
| 339 |
'jquery-confirm' => array( |
| 340 |
'style' => array( |
| 341 |
'src' => self::get_uri( 'misc/import/assets/jquery-confirm', 'css' ), |
| 342 |
'ver' => '3.3.4', |
| 343 |
), |
| 344 |
'script' => array( |
| 345 |
'src' => self::get_uri( 'misc/import/assets/jquery-confirm', 'js' ), |
| 346 |
'deps' => array( 'jquery' ), |
| 347 |
'ver' => '3.3.4', |
| 348 |
'in_footer' => true, |
| 349 |
), |
| 350 |
), |
| 351 |
|
| 352 |
/*** customcode ***/ |
| 353 |
|
| 354 |
'hootkitcode' => array( |
| 355 |
'style' => array( |
| 356 |
'handle' => hootkit()->slug . '-code', |
| 357 |
'src' => self::get_uri( 'misc/code/assets/code', 'css' ), |
| 358 |
), |
| 359 |
'script' => array( |
| 360 |
'handle' => hootkit()->slug . '-code', |
| 361 |
'src' => self::get_uri( 'misc/code/assets/code', 'js' ), |
| 362 |
'deps' => array( 'jquery', 'wp-theme-plugin-editor' ), |
| 363 |
'in_footer' => true, |
| 364 |
), |
| 365 |
), |
| 366 |
|
| 367 |
); |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Returns the instance |
| 372 |
*/ |
| 373 |
public static function get_instance() { |
| 374 |
if ( ! isset( self::$instance ) ) { |
| 375 |
self::$instance = new self(); |
| 376 |
} |
| 377 |
return self::$instance; |
| 378 |
} |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
Helper_Assets::get_instance(); |
| 383 |
|
| 384 |
endif; |