Completed
Push — develop ( 9ea387...c79963 )
by Aristeides
03:40
created

Kirki_Fonts::get_google_font_subsets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
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     http://opensource.org/licenses/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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new self() of type Kirki_Fonts is incompatible with the declared type object|null of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
74
		return apply_filters( 'kirki_fonts_all', array_merge( $standard_fonts, $google_fonts ) );
75
	}
76
77
	/**
78
	 * Return an array of standard websafe fonts.
79
	 *
80
	 * @return array    Standard websafe fonts.
81
	 */
82
	public static function get_standard_fonts() {
83
		$standard_fonts = array(
84
			'serif'      => array(
85
				'label' => 'Serif',
86
				'stack' => 'Georgia,Times,"Times New Roman",serif',
87
			),
88
			'sans-serif' => array(
89
				'label' => 'Sans Serif',
90
				'stack' => '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif',
91
			),
92
			'monospace'  => array(
93
				'label' => 'Monospace',
94
				'stack' => 'Monaco,"Lucida Sans Typewriter","Lucida Typewriter","Courier New",Courier,monospace',
95
			),
96
		);
97
		return apply_filters( 'kirki_fonts_standard_fonts', $standard_fonts );
98
	}
99
100
	/**
101
	 * Return an array of backup fonts based on the font-category
102
	 *
103
	 * @return array
104
	 */
105
	public static function get_backup_fonts() {
106
		$backup_fonts = array(
107
			'sans-serif'  => 'Helvetica, Arial, sans-serif',
108
			'serif'       => 'Georgia, serif',
109
			'display'     => '"Comic Sans MS", cursive, sans-serif',
110
			'handwriting' => '"Comic Sans MS", cursive, sans-serif',
111
			'monospace'   => '"Lucida Console", Monaco, monospace',
112
		);
113
		return apply_filters( 'kirki_fonts_backup_fonts', $backup_fonts );
114
	}
115
116
	/**
117
	 * Return an array of all available Google Fonts.
118
	 *
119
	 * @return array    All Google Fonts.
120
	 */
121
	public static function get_google_fonts() {
122
123
		// Get fonts from cache.
124
		self::$google_fonts = get_site_transient( 'kirki_googlefonts_cache' );
125
126
		// If we're debugging, don't use cached.
127
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
0 ignored issues
show
Bug introduced by
The constant WP_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
			self::$google_fonts = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object|null of property $google_fonts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
129
		}
130
131
		// If cache is populated, return cached fonts array.
132
		if ( self::$google_fonts ) {
133
			return self::$google_fonts;
134
		}
135
136
		// If we got this far, cache was empty so we need to get from JSON.
137
		ob_start();
138
		include wp_normalize_path( dirname( __FILE__ ) . '/webfonts.json' );
139
140
		$fonts_json = ob_get_clean();
141
		$fonts      = json_decode( $fonts_json, true );
142
143
		$google_fonts = array();
144
		if ( is_array( $fonts ) ) {
145
			foreach ( $fonts['items'] as $font ) {
146
				$google_fonts[ $font['family'] ] = array(
147
					'label'    => $font['family'],
148
					'variants' => $font['variants'],
149
					'category' => $font['category'],
150
				);
151
			}
152
		}
153
154
		// Apply the 'kirki_fonts_google_fonts' filter.
155
		self::$google_fonts = apply_filters( 'kirki_fonts_google_fonts', $google_fonts );
156
157
		// Save the array in cache.
158
		$cache_time = apply_filters( 'kirki_googlefonts_transient_time', HOUR_IN_SECONDS );
159
		set_site_transient( 'kirki_googlefonts_cache', self::$google_fonts, $cache_time );
160
161
		return self::$google_fonts;
162
	}
163
164
	/**
165
	 * Returns an array of all available subsets.
166
	 *
167
	 * @static
168
	 * @access public
169
	 * @return array
170
	 */
171
	public static function get_google_font_subsets() {
172
		return array(
173
			'cyrillic'     => 'Cyrillic',
174
			'cyrillic-ext' => 'Cyrillic Extended',
175
			'devanagari'   => 'Devanagari',
176
			'greek'        => 'Greek',
177
			'greek-ext'    => 'Greek Extended',
178
			'khmer'        => 'Khmer',
179
			'latin'        => 'Latin',
180
			'latin-ext'    => 'Latin Extended',
181
			'vietnamese'   => 'Vietnamese',
182
			'hebrew'       => 'Hebrew',
183
			'arabic'       => 'Arabic',
184
			'bengali'      => 'Bengali',
185
			'gujarati'     => 'Gujarati',
186
			'tamil'        => 'Tamil',
187
			'telugu'       => 'Telugu',
188
			'thai'         => 'Thai',
189
		);
190
	}
191
192
	/**
193
	 * Dummy function to avoid issues with backwards-compatibility.
194
	 * This is not functional, but it will prevent PHP Fatal errors.
195
	 *
196
	 * @static
197
	 * @access public
198
	 */
199
	public static function get_google_font_uri() {}
200
201
	/**
202
	 * Returns an array of all available variants.
203
	 *
204
	 * @static
205
	 * @access public
206
	 * @return array
207
	 */
208
	public static function get_all_variants() {
209
		return array(
210
			'100'       => esc_attr__( 'Ultra-Light 100', 'kirki' ),
211
			'100light'  => esc_attr__( 'Ultra-Light 100', 'kirki' ),
212
			'100italic' => esc_attr__( 'Ultra-Light 100 Italic', 'kirki' ),
213
			'200'       => esc_attr__( 'Light 200', 'kirki' ),
214
			'200italic' => esc_attr__( 'Light 200 Italic', 'kirki' ),
215
			'300'       => esc_attr__( 'Book 300', 'kirki' ),
216
			'300italic' => esc_attr__( 'Book 300 Italic', 'kirki' ),
217
			'400'       => esc_attr__( 'Normal 400', 'kirki' ),
218
			'regular'   => esc_attr__( 'Normal 400', 'kirki' ),
219
			'italic'    => esc_attr__( 'Normal 400 Italic', 'kirki' ),
220
			'500'       => esc_attr__( 'Medium 500', 'kirki' ),
221
			'500italic' => esc_attr__( 'Medium 500 Italic', 'kirki' ),
222
			'600'       => esc_attr__( 'Semi-Bold 600', 'kirki' ),
223
			'600bold'   => esc_attr__( 'Semi-Bold 600', 'kirki' ),
224
			'600italic' => esc_attr__( 'Semi-Bold 600 Italic', 'kirki' ),
225
			'700'       => esc_attr__( 'Bold 700', 'kirki' ),
226
			'700italic' => esc_attr__( 'Bold 700 Italic', 'kirki' ),
227
			'800'       => esc_attr__( 'Extra-Bold 800', 'kirki' ),
228
			'800bold'   => esc_attr__( 'Extra-Bold 800', 'kirki' ),
229
			'800italic' => esc_attr__( 'Extra-Bold 800 Italic', 'kirki' ),
230
			'900'       => esc_attr__( 'Ultra-Bold 900', 'kirki' ),
231
			'900bold'   => esc_attr__( 'Ultra-Bold 900', 'kirki' ),
232
			'900italic' => esc_attr__( 'Ultra-Bold 900 Italic', 'kirki' ),
233
		);
234
	}
235
236
	/**
237
	 * Determine if a font-name is a valid google font or not.
238
	 *
239
	 * @static
240
	 * @access public
241
	 * @param string $fontname The name of the font we want to check.
242
	 * @return bool
243
	 */
244
	public static function is_google_font( $fontname ) {
245
		return ( array_key_exists( $fontname, self::$google_fonts ) );
0 ignored issues
show
Bug introduced by
self::google_fonts of type object|null is incompatible with the type array expected by parameter $search of array_key_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

245
		return ( array_key_exists( $fontname, /** @scrutinizer ignore-type */ self::$google_fonts ) );
Loading history...
246
	}
247
248
	/**
249
	 * Gets available options for a font.
250
	 *
251
	 * @static
252
	 * @access public
253
	 * @return array
254
	 */
255
	public static function get_font_choices() {
256
		$fonts       = self::get_all_fonts();
257
		$fonts_array = array();
258
		foreach ( $fonts as $key => $args ) {
259
			$fonts_array[ $key ] = $key;
260
		}
261
		return $fonts_array;
262
	}
263
}
264