Last active
April 6, 2016 18:13
-
-
Save torounit/9887350 to your computer and use it in GitHub Desktop.
WordPressで、css, jsをうまく使うためのClass. dns-prefetchも勝手に登録する。Singletonはhttp://d.hatena.ne.jp/Yudoufu/20090811/1250021010から。
This file contains hidden or 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 | |
Class Assets_Init extends Singleton { | |
public $styles; | |
public $scripts; | |
public $prefetch; | |
protected function initialize() { | |
$styles = array(); | |
$scripts = array(); | |
$prefetch = array(); | |
$this->add_hook(); | |
} | |
private function add_hook() { | |
add_action( "wp_enqueue_scripts", array( $this, "enqueue_styles") ); | |
add_action( "wp_enqueue_scripts", array( $this, "enqueue_scripts") ); | |
add_action( "wp_head", array($this, "dns_prefetch"),1); | |
} | |
/** | |
* | |
* Add DNS prefetch. | |
* | |
* */ | |
public function dns_prefetch() { | |
if( !empty($this->prefetch) ): | |
?> | |
<meta http-equiv="x-dns-prefetch-control" content="on"> | |
<link rel="dns-prefetch" href="//cdnjs.cloudflare.com"> | |
<link rel="dns-prefetch" href="//code.jquery.com"> | |
<?php | |
foreach($this->prefetch as $url):?> | |
<link rel="dns-prefetch" href="//<?php echo $url;?>"> | |
<?php | |
endforeach; | |
endif; | |
} | |
/** | |
* | |
* Enqueue CSS. | |
* | |
* */ | |
public function add_prefetch($url) { | |
$url = preg_replace("/^\/\//", "http://", $url); | |
$param = parse_url($url); | |
$host = $param["host"]; | |
$myhost = parse_url(home_url()); | |
$myhost = $myhost["host"]; | |
if( empty($this->prefetch) or in_array($url, $this->prefetch) ) { | |
$this->prefetch[] = $host; | |
} | |
} | |
public function add_style($name, $url) { | |
$this->styles[] = array("name" => $name, "url" => $url); | |
$this->add_prefetch($url); | |
} | |
public function enqueue_styles() { | |
if(!empty($this->styles)) { | |
foreach ($this->styles as $style ) { | |
wp_enqueue_style( $style["name"], $style["url"], array() ); | |
} | |
} | |
} | |
/** | |
* | |
* Enqueue JavaScript. | |
* | |
* */ | |
public function add_script($name,$url, $dep = array(), $footer = false) { | |
$this->scripts[] = array("name" => $name, "url" => $url, "dep" => $dep, "footer" => $footer); | |
$this->add_prefetch($url); | |
} | |
public function enqueue_scripts() { | |
if(!empty($this->scripts)) { | |
foreach ( $this->scripts as $script ) { | |
wp_deregister_script($script["name"]); | |
wp_enqueue_script($script["name"], $script["url"], $script["dep"], null, $script["footer"] ); | |
} | |
} | |
} | |
} |
This file contains hidden or 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 | |
require_once "inc/Singleton.php"; | |
require_once 'inc/Assets_Init.php'; | |
if(!is_admin()) { | |
$assets = Assets_Init::getInstance(); | |
$assets->add_style("base", get_template_directory_uri() ."/common/css/base.css"); | |
$assets->add_style("style", get_template_directory_uri() ."/common/css/style.css"); | |
$assets->add_script("modernizr", "//cdnjs.cloudflare.com/ajax/libs/modernizr/2.7.1/modernizr.min.js"); | |
$assets->add_script("jquery", "//code.jquery.com/jquery-2.1.0.min.js"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment