1 | <?php |
||
2 | /** |
||
3 | * A simple object containing properties for fonts. |
||
4 | * |
||
5 | * @package Kirki |
||
6 | * @category Core |
||
7 | * @author Aristeides Stathopoulos |
||
8 | * @copyright Copyright (c) 2017, Aristeides Stathopoulos |
||
9 | * @license https://opensource.org/licenses/MIT |
||
10 | * @since 1.0 |
||
11 | */ |
||
12 | |||
13 | /** |
||
14 | * The Kirki_Fonts object. |
||
15 | */ |
||
16 | final class Kirki_Fonts { |
||
17 | |||
18 | /** |
||
19 | * The mode we'll be using to add google fonts. |
||
20 | * This is a todo item, not yet functional. |
||
21 | * |
||
22 | * @static |
||
23 | * @todo |
||
24 | * @access public |
||
25 | * @var string |
||
26 | */ |
||
27 | public static $mode = 'link'; |
||
28 | |||
29 | /** |
||
30 | * Holds a single instance of this object. |
||
31 | * |
||
32 | * @static |
||
33 | * @access private |
||
34 | * @var null|object |
||
35 | */ |
||
36 | private static $instance = null; |
||
37 | |||
38 | /** |
||
39 | * An array of our google fonts. |
||
40 | * |
||
41 | * @static |
||
42 | * @access public |
||
43 | * @var null|object |
||
44 | */ |
||
45 | public static $google_fonts = null; |
||
46 | |||
47 | /** |
||
48 | * The class constructor. |
||
49 | */ |
||
50 | private function __construct() {} |
||
51 | |||
52 | /** |
||
53 | * Get the one, true instance of this class. |
||
54 | * Prevents performance issues since this is only loaded once. |
||
55 | * |
||
56 | * @return object Kirki_Fonts |
||
57 | */ |
||
58 | public static function get_instance() { |
||
59 | if ( null === self::$instance ) { |
||
60 | self::$instance = new self(); |
||
61 | } |
||
62 | return self::$instance; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Compile font options from different sources. |
||
67 | * |
||
68 | * @return array All available fonts. |
||
69 | */ |
||
70 | public static function get_all_fonts() { |
||
71 | $standard_fonts = self::get_standard_fonts(); |
||
72 | $google_fonts = self::get_google_fonts(); |
||
73 | return apply_filters( 'kirki_fonts_all', array_merge( $standard_fonts, $google_fonts ) ); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Return an array of standard websafe fonts. |
||
78 | * |
||
79 | * @return array Standard websafe fonts. |
||
80 | */ |
||
81 | public static function get_standard_fonts() { |
||
82 | $standard_fonts = array( |
||
83 | 'serif' => array( |
||
84 | 'label' => 'Serif', |
||
85 | 'stack' => 'Georgia,Times,"Times New Roman",serif', |
||
86 | ), |
||
87 | 'sans-serif' => array( |
||
88 | 'label' => 'Sans Serif', |
||
89 | 'stack' => '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif', |
||
90 | ), |
||
91 | 'monospace' => array( |
||
92 | 'label' => 'Monospace', |
||
93 | 'stack' => 'Monaco,"Lucida Sans Typewriter","Lucida Typewriter","Courier New",Courier,monospace', |
||
94 | ), |
||
95 | ); |
||
96 | return apply_filters( 'kirki_fonts_standard_fonts', $standard_fonts ); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Return an array of backup fonts based on the font-category |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | public static function get_backup_fonts() { |
||
105 | $backup_fonts = array( |
||
106 | 'sans-serif' => 'Helvetica, Arial, sans-serif', |
||
107 | 'serif' => 'Georgia, serif', |
||
108 | 'display' => '"Comic Sans MS", cursive, sans-serif', |
||
109 | 'handwriting' => '"Comic Sans MS", cursive, sans-serif', |
||
110 | 'monospace' => '"Lucida Console", Monaco, monospace', |
||
111 | ); |
||
112 | return apply_filters( 'kirki_fonts_backup_fonts', $backup_fonts ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Return an array of all available Google Fonts. |
||
117 | * |
||
118 | * @return array All Google Fonts. |
||
119 | */ |
||
120 | public static function get_google_fonts() { |
||
121 | |||
122 | // Get fonts from cache. |
||
123 | self::$google_fonts = get_site_transient( 'kirki_googlefonts_cache' ); |
||
124 | |||
125 | // If we're debugging, don't use cached. |
||
126 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
127 | self::$google_fonts = false; |
||
128 | } |
||
129 | |||
130 | // If cache is populated, return cached fonts array. |
||
131 | if ( self::$google_fonts ) { |
||
132 | return self::$google_fonts; |
||
133 | } |
||
134 | |||
135 | // If we got this far, cache was empty so we need to get from JSON. |
||
136 | ob_start(); |
||
137 | include wp_normalize_path( dirname( __FILE__ ) . '/webfonts.json' ); |
||
138 | |||
139 | $fonts_json = ob_get_clean(); |
||
140 | $fonts = json_decode( $fonts_json, true ); |
||
141 | |||
142 | $google_fonts = array(); |
||
143 | if ( is_array( $fonts ) ) { |
||
144 | foreach ( $fonts['items'] as $font ) { |
||
145 | $google_fonts[ $font['family'] ] = array( |
||
146 | 'label' => $font['family'], |
||
147 | 'variants' => $font['variants'], |
||
148 | 'category' => $font['category'], |
||
149 | ); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // Apply the 'kirki_fonts_google_fonts' filter. |
||
154 | self::$google_fonts = apply_filters( 'kirki_fonts_google_fonts', $google_fonts ); |
||
155 | |||
156 | // Save the array in cache. |
||
157 | $cache_time = apply_filters( 'kirki_googlefonts_transient_time', HOUR_IN_SECONDS ); |
||
158 | set_site_transient( 'kirki_googlefonts_cache', self::$google_fonts, $cache_time ); |
||
159 | |||
160 | return self::$google_fonts; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Returns an array of all available subsets. |
||
165 | * |
||
166 | * @static |
||
167 | * @access public |
||
168 | * @return array |
||
169 | */ |
||
170 | public static function get_google_font_subsets() { |
||
171 | return array( |
||
172 | 'cyrillic' => 'Cyrillic', |
||
173 | 'cyrillic-ext' => 'Cyrillic Extended', |
||
174 | 'devanagari' => 'Devanagari', |
||
175 | 'greek' => 'Greek', |
||
176 | 'greek-ext' => 'Greek Extended', |
||
177 | 'khmer' => 'Khmer', |
||
178 | 'latin' => 'Latin', |
||
179 | 'latin-ext' => 'Latin Extended', |
||
180 | 'vietnamese' => 'Vietnamese', |
||
181 | 'hebrew' => 'Hebrew', |
||
182 | 'arabic' => 'Arabic', |
||
183 | 'bengali' => 'Bengali', |
||
184 | 'gujarati' => 'Gujarati', |
||
185 | 'tamil' => 'Tamil', |
||
186 | 'telugu' => 'Telugu', |
||
187 | 'thai' => 'Thai', |
||
188 | ); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Dummy function to avoid issues with backwards-compatibility. |
||
193 | * This is not functional, but it will prevent PHP Fatal errors. |
||
194 | * |
||
195 | * @static |
||
196 | * @access public |
||
197 | */ |
||
198 | public static function get_google_font_uri() {} |
||
199 | |||
200 | /** |
||
201 | * Returns an array of all available variants. |
||
202 | * |
||
203 | * @static |
||
204 | * @access public |
||
205 | * @return array |
||
206 | */ |
||
207 | public static function get_all_variants() { |
||
208 | return array( |
||
209 | '100' => esc_html__( 'Ultra-Light 100', 'kirki' ), |
||
210 | '100light' => esc_html__( 'Ultra-Light 100', 'kirki' ), |
||
211 | '100italic' => esc_html__( 'Ultra-Light 100 Italic', 'kirki' ), |
||
212 | '200' => esc_html__( 'Light 200', 'kirki' ), |
||
213 | '200italic' => esc_html__( 'Light 200 Italic', 'kirki' ), |
||
214 | '300' => esc_html__( 'Book 300', 'kirki' ), |
||
215 | '300italic' => esc_html__( 'Book 300 Italic', 'kirki' ), |
||
216 | '400' => esc_html__( 'Normal 400', 'kirki' ), |
||
217 | 'regular' => esc_html__( 'Normal 400', 'kirki' ), |
||
218 | 'italic' => esc_html__( 'Normal 400 Italic', 'kirki' ), |
||
219 | '500' => esc_html__( 'Medium 500', 'kirki' ), |
||
220 | '500italic' => esc_html__( 'Medium 500 Italic', 'kirki' ), |
||
221 | '600' => esc_html__( 'Semi-Bold 600', 'kirki' ), |
||
222 | '600bold' => esc_html__( 'Semi-Bold 600', 'kirki' ), |
||
223 | '600italic' => esc_html__( 'Semi-Bold 600 Italic', 'kirki' ), |
||
224 | '700' => esc_html__( 'Bold 700', 'kirki' ), |
||
225 | '700italic' => esc_html__( 'Bold 700 Italic', 'kirki' ), |
||
226 | '800' => esc_html__( 'Extra-Bold 800', 'kirki' ), |
||
227 | '800bold' => esc_html__( 'Extra-Bold 800', 'kirki' ), |
||
228 | '800italic' => esc_html__( 'Extra-Bold 800 Italic', 'kirki' ), |
||
229 | '900' => esc_html__( 'Ultra-Bold 900', 'kirki' ), |
||
230 | '900bold' => esc_html__( 'Ultra-Bold 900', 'kirki' ), |
||
231 | '900italic' => esc_html__( 'Ultra-Bold 900 Italic', 'kirki' ), |
||
232 | ); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Determine if a font-name is a valid google font or not. |
||
237 | * |
||
238 | * @static |
||
239 | * @access public |
||
240 | * @param string $fontname The name of the font we want to check. |
||
241 | * @return bool |
||
242 | */ |
||
243 | public static function is_google_font( $fontname ) { |
||
244 | return ( array_key_exists( $fontname, self::$google_fonts ) ); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Gets available options for a font. |
||
249 | * |
||
250 | * @static |
||
251 | * @access public |
||
252 | * @return array |
||
253 | */ |
||
254 | public static function get_font_choices() { |
||
255 | $fonts = self::get_all_fonts(); |
||
256 | $fonts_array = array(); |
||
257 | foreach ( $fonts as $key => $args ) { |
||
258 | $fonts_array[ $key ] = $key; |
||
259 | } |
||
260 | return $fonts_array; |
||
261 | } |
||
262 | } |
||
263 |