<?php
/**
* Scripts enqueue class.
*
* @since 3.1.1
*/
class Scripts extends Dependencies {
private static $instance;
public $async = array();
public static function newInstance()
{
if(!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
public function __construct()
{
parent::__construct();
}
/**
* Add script to be loaded
*
* @param type $id
* @param type $url
* @param type $dependencies mixed, it could be an array or a string
* @param boolean $force, force load script (available since osclass evo v. 4.4.0)
*/
public function registerScript($id, $url, $dependencies = null, $force = false)
{
$this->register($id, $url, $dependencies, $force);
}
/**
* Remove script to not be loaded
*
* @param type $id
*/
public function unregisterScript($id)
{
$this->unregister($id);
}
/**
* Enqueu script to be loaded
*
* @param type $id
*/
public function enqueuScript($id)
{
$this->queue[$id] = $id;
}
/**
* Remove script to not be loaded
*
* @param type $id
*/
public function removeScript($id)
{
unset($this->queue[$id]);
}
/**
* Get the scripts urls
*/
public function getScripts()
{
parent::order();
$scripts = array();
foreach($this->resolved as $i => $id) {
if( isset($this->registered[$id]['url']) ) {
$scripts[$i] = $this->registered[$id]['url'];
$scripts['async'][$i] = $this->registered[$id]['async'];
}
}
return $scripts;
}
/**
* Print the HTML tags to load the necessary script
*/
public function printScript($id)
{
if(isset($this->registered[$id]['url'])) {
$async = $this->registered[$id]['async'] ? 'defer' : '';
echo '<script type="text/javascript" src="' . osc_apply_filter('theme_url', $this->registered[$id]['url']) . '" ' . $async . '></script>' . PHP_EOL;
}
}
/**
* Print the HTML tags to load the scripts
*/
public function printScripts()
{
$scripts = $this->getScripts();
foreach($scripts as $i => $script) {
$async = (isset($this->getScripts()['async'][$i]) && $this->getScripts()['async'][$i] == true && !OC_ADMIN) ? 'defer' : '';
if($script && $i != 'async') {
echo '<script type="text/javascript" src="' . osc_apply_filter('theme_url', $script) . '" ' . $async . '></script>' . PHP_EOL;
}
}
}
}