diff --git a/class-RegisterJS.php b/class-RegisterJS.php index daf0416..f14d88a 100644 --- a/class-RegisterJS.php +++ b/class-RegisterJS.php @@ -1,133 +1,132 @@ . /** * Register and enqueue JS libraries */ class RegisterJS { /** * Default position */ public static $DEFAULT = 'header'; /** * Script names */ private $js = []; - private $generated = [ false, false ]; - /** * Get the singleton instance * * @return self */ public static function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * Register a new script name * * @param string $name JS name, like: "jquery" - * @param string $url Script url, like "http://example.org/lib/jquery.js" + * @param mixed $url Script url, like "http://example.org/lib/jquery.js" * @param string $position header|footer */ public function register( $uid, $url, $position = null ) { if( isset( $this->js[ $uid ] ) ) { $this->js[ $uid ]->url = $url; } else { $this->js[ $uid ] = new JS( $url, $position ); } } /** * Enqueue a previous registered JS name * * @param $name string JS name * @param $position string Place it in the head of the page or not */ public function enqueue( $uid, $position = null ) { if( isset( $this->js[ $uid ] ) ) { $this->js[ $uid ]->enqueue( $position ); } else { error( "unregistered JS $uid" ); } } /** * Print all the elements from the specified position * * @param $position string */ public function printAll( $position ) { foreach( $this->js as $uid => $js ) { if( $js->enqueue && $js->position === $position ) { echo "\n"; if( $position === 'header' ) { echo "\t"; } $url = $js->url; if( CACHE_BUSTER ) { $url .= false === strpos( $url, '?' ) ? '?' : '&'; $url .= CACHE_BUSTER; } echo ""; if( DEBUG ) { echo ""; } } } - $this->generated[ $position ] = true; - } - - public function isGenerated( $position ) { - return $this->generated[ $position ]; } } class JS { public $url; public $position; public $enqueue = false; + /** + * Construct + * + * @param $url string + * @param $position string header|footer + */ public function __construct( $url, $position = null ) { if( ! $position ) { $position = RegisterJS::$DEFAULT; } $this->url = $url; $this->position = $position; } /** * @param $position string header|footer */ public function enqueue( $position = null ) { $this->enqueue = true; if( $position !== null ) { $this->position = $position; } } }