Created
March 4, 2014 21:56
-
-
Save dobbyloo/9356636 to your computer and use it in GitHub Desktop.
Wordpress: wrapper class to register/enqueue styles and scripts in a theme.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace PaintedCloud\WP\Classes; | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
class AssetRegister { | |
public $scripts_base_uri; | |
public $styles_base_uri; | |
protected $scripts; | |
protected $stylesheets; | |
public function __construct( $scripts_base_uri = '', $styles_base_uri = '', $location = "front", $action_priority = 10 ) | |
{ | |
$this->scripts_base_uri = $scripts_base_uri; | |
$this->styles_base_uri = $styles_base_uri; | |
$this->scripts = array(); | |
$this->stylesheets = array(); | |
if($location == "admin") { | |
add_action('admin_enqueue_scripts', array(&$this, 'enqueue_scripts'), $action_priority); | |
} elseif ($location == "login") { | |
if (!has_action('login_enqueue_scripts', 'wp_print_styles')) { | |
add_action( 'login_enqueue_scripts', 'wp_print_styles', 11 ); | |
} | |
add_action('login_enqueue_scripts', array(&$this, 'enqueue_scripts'), $action_priority); | |
} else { | |
// Default is to add it to the front | |
add_action('wp_enqueue_scripts', array(&$this, 'enqueue_scripts'), $action_priority); | |
} | |
} | |
public function register_script( $handle, $filename, $deps = array(), $enqueue= true, $ver = false, $in_footer = true ) | |
{ | |
$this->scripts[$handle] = array( | |
'deps' => $deps, | |
'src' => $this->scripts_base_uri . $filename, | |
'enqueue' => $enqueue, | |
'ver' => $ver, | |
'in_footer' => $in_footer | |
); | |
} | |
public function register_cdn_script( $handle, $src, $deps = array(), $enqueue= true, $ver = false, $in_footer = true ) | |
{ | |
$this->scripts[$handle] = array( | |
'deps' => $deps, | |
'src' => $src, | |
'enqueue' => $enqueue, | |
'ver' => $ver, | |
'in_footer' => $in_footer | |
); | |
} | |
public function register_style( $handle, $filename, $deps = array(), $enqueue = true, $ver = false, $media = 'all') | |
{ | |
$this->stylesheets[$handle] = array( | |
'deps' => $deps, | |
'src' => $this->styles_base_uri . $filename, | |
'enqueue' => $enqueue, | |
'ver' => $ver, | |
'media' => $media | |
); | |
} | |
public function register_cdn_style( $handle, $src, $deps = array(), $enqueue = true, $ver = false, $media = 'all') | |
{ | |
$this->stylesheets[$handle] = array( | |
'deps' => $deps, | |
'src' => $src, | |
'enqueue' => $enqueue, | |
'ver' => $ver, | |
'media' => $media | |
); | |
} | |
public function deregister_script( $key ) | |
{ | |
if(wp_script_is($key, 'registered')) { | |
wp_deregister_script($key); | |
} | |
} | |
public function deregister_style( $key ) | |
{ | |
if(wp_style_is($key, 'registered')) { | |
wp_deregister_style($key); | |
} | |
} | |
public function enqueue_scripts() | |
{ | |
// Process Scripts | |
foreach($this->scripts as $key => $script) { | |
$this->deregister_script($key); | |
wp_register_script( $key, $script['src'], $script['deps'], $script['ver'], $script['in_footer'] ); | |
if($script['enqueue']) { | |
wp_enqueue_script($key); | |
} | |
} | |
// Process Stylesheets | |
foreach($this->stylesheets as $key => $style) { | |
$this->deregister_style($key); | |
wp_register_style( $key, $style['src'], $style['deps'], $style['ver'], $style['media']); | |
if($style['enqueue']) { | |
wp_enqueue_style($key); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment