Completed
Push — develop ( c3d8ee...97838c )
by Aristeides
03:16
created

Kirki_Fonts::get_all_variants()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 27
rs 8.8571
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) 2016, 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();
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'  => 'Helvetica,Arial,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
		if ( null === self::$google_fonts || empty( self::$google_fonts ) ) {
124
125
			$fonts = include wp_normalize_path( Kirki::$path . '/core/webfonts.php' );
126
127
			$google_fonts = array();
128
			if ( is_array( $fonts ) ) {
129
				foreach ( $fonts['items'] as $font ) {
130
					$google_fonts[ $font['family'] ] = array(
131
						'label'    => $font['family'],
132
						'variants' => $font['variants'],
133
						'subsets'  => $font['subsets'],
134
						'category' => $font['category'],
135
					);
136
				}
137
			}
138
139
			self::$google_fonts = apply_filters( 'kirki/fonts/google_fonts', $google_fonts );
140
141
		}
142
143
		return self::$google_fonts;
144
145
	}
146
147
	/**
148
	 * Dummy function to avoid issues with backwards-compatibility.
149
	 * This is not functional, but it will prevent PHP Fatal errors.
150
	 *
151
	 * @static
152
	 * @access public
153
	 */
154
	public static function get_google_font_uri() {}
155
156
	/**
157
	 * Returns an array of all available subsets.
158
	 *
159
	 * @static
160
	 * @access public
161
	 * @return array
162
	 */
163
	public static function get_google_font_subsets() {
164
		return array(
165
			'cyrillic'     => 'Cyrillic',
166
			'cyrillic-ext' => 'Cyrillic Extended',
167
			'devanagari'   => 'Devanagari',
168
			'greek'        => 'Greek',
169
			'greek-ext'    => 'Greek Extended',
170
			'khmer'        => 'Khmer',
171
			'latin'        => 'Latin',
172
			'latin-ext'    => 'Latin Extended',
173
			'vietnamese'   => 'Vietnamese',
174
			'hebrew'       => 'Hebrew',
175
			'arabic'       => 'Arabic',
176
			'bengali'      => 'Bengali',
177
			'gujarati'     => 'Gujarati',
178
			'tamil'        => 'Tamil',
179
			'telugu'       => 'Telugu',
180
			'thai'         => 'Thai',
181
		);
182
	}
183
184
	/**
185
	 * Returns an array of all available variants.
186
	 *
187
	 * @static
188
	 * @access public
189
	 * @return array
190
	 */
191
	public static function get_all_variants() {
192
		return apply_filters( 'kirki/l10n', array(
193
			'100'       => esc_attr__( 'Ultra-Light 100', 'kirki' ),
194
			'100light'  => esc_attr__( 'Ultra-Light 100', 'kirki' ),
195
			'100italic' => esc_attr__( 'Ultra-Light 100 Italic', 'kirki' ),
196
			'200'       => esc_attr__( 'Light 200', 'kirki' ),
197
			'200italic' => esc_attr__( 'Light 200 Italic', 'kirki' ),
198
			'300'       => esc_attr__( 'Book 300', 'kirki' ),
199
			'300italic' => esc_attr__( 'Book 300 Italic', 'kirki' ),
200
			'400'       => esc_attr__( 'Normal 400', 'kirki' ),
201
			'regular'   => esc_attr__( 'Normal 400', 'kirki' ),
202
			'italic'    => esc_attr__( 'Normal 400 Italic', 'kirki' ),
203
			'500'       => esc_attr__( 'Medium 500', 'kirki' ),
204
			'500italic' => esc_attr__( 'Medium 500 Italic', 'kirki' ),
205
			'600'       => esc_attr__( 'Semi-Bold 600', 'kirki' ),
206
			'600bold'   => esc_attr__( 'Semi-Bold 600', 'kirki' ),
207
			'600italic' => esc_attr__( 'Semi-Bold 600 Italic', 'kirki' ),
208
			'700'       => esc_attr__( 'Bold 700', 'kirki' ),
209
			'700italic' => esc_attr__( 'Bold 700 Italic', 'kirki' ),
210
			'800'       => esc_attr__( 'Extra-Bold 800', 'kirki' ),
211
			'800bold'   => esc_attr__( 'Extra-Bold 800', 'kirki' ),
212
			'800italic' => esc_attr__( 'Extra-Bold 800 Italic', 'kirki' ),
213
			'900'       => esc_attr__( 'Ultra-Bold 900', 'kirki' ),
214
			'900bold'   => esc_attr__( 'Ultra-Bold 900', 'kirki' ),
215
			'900italic' => esc_attr__( 'Ultra-Bold 900 Italic', 'kirki' ),
216
		) );
217
	}
218
219
	/**
220
	 * Determine if a font-name is a valid google font or not.
221
	 *
222
	 * @static
223
	 * @access public
224
	 * @param string $fontname The name of the font we want to check.
225
	 * @return bool
226
	 */
227
	public static function is_google_font( $fontname ) {
228
		return ( array_key_exists( $fontname, self::$google_fonts ) );
229
	}
230
231
	/**
232
	 * Gets available options for a font.
233
	 *
234
	 * @static
235
	 * @access public
236
	 * @return array
237
	 */
238
	public static function get_font_choices() {
239
		$fonts = self::get_all_fonts();
240
		$fonts_array = array();
241
		foreach ( $fonts as $key => $args ) {
242
			$fonts_array[ $key ] = $key;
243
		}
244
		return $fonts_array;
245
	}
246
}
247