| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Singleton class to manage Google Web Fonts embedding, |
| 5 |
* add the fonts with the weight and style |
| 6 |
* |
| 7 |
*/ |
| 8 |
class VP_Site_GoogleWebFont |
| 9 |
{ |
| 10 |
private $_fonts = array(); |
| 11 |
|
| 12 |
private static $_instance = null; |
| 13 |
|
| 14 |
public static function instance() |
| 15 |
{ |
| 16 |
if(self::$_instance == null) |
| 17 |
{ |
| 18 |
self::$_instance = new self(); |
| 19 |
} |
| 20 |
return self::$_instance; |
| 21 |
} |
| 22 |
|
| 23 |
public function add($name, $weights = 'normal', $styles = 'normal') |
| 24 |
{ |
| 25 |
|
| 26 |
if(empty($name)) |
| 27 |
return; |
| 28 |
|
| 29 |
$weights = (array) $weights; |
| 30 |
$styles = (array) $styles; |
| 31 |
$name = str_replace(' ', '+', $name); |
| 32 |
|
| 33 |
if(!isset($this->_fonts[$name])) |
| 34 |
$this->_fonts[$name] = array(); |
| 35 |
|
| 36 |
foreach ($weights as $weight) |
| 37 |
{ |
| 38 |
foreach ($styles as $style) |
| 39 |
{ |
| 40 |
// set it to empty if style is equal to normal |
| 41 |
if($style === 'normal') |
| 42 |
$style = ''; |
| 43 |
|
| 44 |
if($style != '') |
| 45 |
if($weight === 'normal') $weight = ''; |
| 46 |
|
| 47 |
// skip if both are empty |
| 48 |
if($style === '' and $weight === '') |
| 49 |
continue; |
| 50 |
|
| 51 |
$couple = $weight . $style; |
| 52 |
|
| 53 |
if(!in_array($couple, $this->_fonts[$name])) |
| 54 |
$this->_fonts[$name][] = $couple; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function register() |
| 60 |
{ |
| 61 |
$links = $this->get_font_links(); |
| 62 |
foreach ($links as $name => $link) |
| 63 |
{ |
| 64 |
wp_register_style( $name, $link); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function enqueue() |
| 69 |
{ |
| 70 |
$names = $this->get_names(); |
| 71 |
foreach ($names as $name) |
| 72 |
{ |
| 73 |
wp_enqueue_style( $name ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public function register_and_enqueue() |
| 78 |
{ |
| 79 |
$this->register(); |
| 80 |
$this->enqueue(); |
| 81 |
} |
| 82 |
|
| 83 |
public function get_font_links() |
| 84 |
{ |
| 85 |
$links = array(); |
| 86 |
foreach ($this->_fonts as $name => $atts) |
| 87 |
{ |
| 88 |
$param = implode(',', $atts); |
| 89 |
$link = "http://fonts.googleapis.com/css?family=$name" . ($param !== '' ? ":$param" : ''); |
| 90 |
$links[$name] = $link; |
| 91 |
} |
| 92 |
return $links; |
| 93 |
} |
| 94 |
|
| 95 |
public function get_fonts() |
| 96 |
{ |
| 97 |
return $this->_fonts; |
| 98 |
} |
| 99 |
|
| 100 |
public function get_names() |
| 101 |
{ |
| 102 |
return array_keys($this->_fonts); |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* EOF |
| 109 |
*/ |