Completed
Push — develop ( 38831a...4fffa2 )
by Aristeides
08:57 queued 03:28
created

Kirki_Fonts_Google::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Processes typography-related fields
4
 * and generates the google-font link.
5
 *
6
 * @package     Kirki
7
 * @category    Core
8
 * @author      Aristeides Stathopoulos
9
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
10
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
11
 * @since       1.0
12
 */
13
14
/**
15
 * Manages the way Google Fonts are enqueued.
16
 */
17
final class Kirki_Fonts_Google {
18
19
	/**
20
	 * The Kirki_Fonts_Google instance.
21
	 * We use the singleton pattern here to avoid loading the google-font array multiple times.
22
	 * This is mostly a performance tweak.
23
	 *
24
	 * @access private
25
	 * @var null|object
26
	 */
27
	private static $instance = null;
28
29
	/**
30
	 * If set to true, forces loading ALL variants.
31
	 *
32
	 * @static
33
	 * @access public
34
	 * @var bool
35
	 */
36
	public static $force_load_all_variants = false;
37
38
	/**
39
	 * If set to true, forces loading ALL subsets.
40
	 *
41
	 * @static
42
	 * @access public
43
	 * @var bool
44
	 */
45
	public static $force_load_all_subsets = false;
46
47
	/**
48
	 * The array of fonts
49
	 *
50
	 * @access public
51
	 * @var array
52
	 */
53
	public $fonts = array();
54
55
	/**
56
	 * An array of all google fonts.
57
	 *
58
	 * @access private
59
	 * @var array
60
	 */
61
	private $google_fonts = array();
62
63
	/**
64
	 * The array of subsets
65
	 *
66
	 * @access public
67
	 * @var array
68
	 */
69
	public $subsets = array();
70
71
	/**
72
	 * The class constructor.
73
	 */
74
	private function __construct() {
75
76
		$config = apply_filters( 'kirki/config', array() );
77
78
		// If we have set $config['disable_google_fonts'] to true then do not proceed any further.
79
		if ( isset( $config['disable_google_fonts'] ) && true === $config['disable_google_fonts'] ) {
80
			return;
81
		}
82
83
		// Populate the array of google fonts.
84
		$this->google_fonts = Kirki_Fonts::get_google_fonts();
85
86
	}
87
88
	/**
89
	 * Get the one, true instance of this class.
90
	 * Prevents performance issues since this is only loaded once.
91
	 *
92
	 * @return object Kirki_Fonts_Google
93
	 */
94
	public static function get_instance() {
95
		if ( null === self::$instance ) {
96
			self::$instance = new Kirki_Fonts_Google();
97
		}
98
		return self::$instance;
99
	}
100
101
	/**
102
	 * Processes the arguments of a field
103
	 * determines if it's a typography field
104
	 * and if it is, then takes appropriate actions.
105
	 *
106
	 * @param array $args The field arguments.
107
	 */
108
	public function generate_google_font( $args ) {
109
110
		// Process typography fields.
111
		if ( isset( $args['type'] ) && 'kirki-typography' === $args['type'] ) {
112
113
			// Get the value.
114
			$value = Kirki_Values::get_sanitized_field_value( $args );
115
116
			// If we don't have a font-family then we can skip this.
117
			if ( ! isset( $value['font-family'] ) ) {
118
				return;
119
			}
120
121
			// Add support for older formats of the typography control.
122
			// We used to have font-weight instead of variant.
123
			if ( isset( $value['font-weight'] ) && ( ! isset( $value['variant'] ) || empty( $value['variant'] ) ) ) {
124
				$value['variant'] = $value['font-weight'];
125
			}
126
127
			// Set a default value for variants.
128
			if ( ! isset( $value['variant'] ) ) {
129
				$value['variant'] = 'regular';
130
			}
131
			if ( isset( $value['subsets'] ) ) {
132
133
				// Add the subset directly to the array of subsets in the Kirki_GoogleFonts_Manager object.
134
				// Subsets must be applied to ALL fonts if possible.
135
				if ( ! is_array( $value['subsets'] ) ) {
136
					$this->subsets[] = $value['subsets'];
137
				} else {
138
					foreach ( $value['subsets'] as $subset ) {
139
						$this->subsets[] = $subset;
140
					}
141
				}
142
			}
143
144
			// Add the requested google-font.
145
			if ( ! isset( $this->fonts[ $value['font-family'] ] ) ) {
146
				$this->fonts[ $value['font-family'] ] = array();
147
			}
148
			if ( ! in_array( $value['variant'], $this->fonts[ $value['font-family'] ], true ) ) {
149
				$this->fonts[ $value['font-family'] ][] = $value['variant'];
150
			}
151
		} else {
152
153
			// Process non-typography fields.
154
			if ( isset( $args['output'] ) && is_array( $args['output'] ) ) {
155
				foreach ( $args['output'] as $output ) {
156
157
					// If we don't have a typography-related output argument we can skip this.
158
					if ( ! isset( $output['property'] ) || ! in_array( $output['property'], array( 'font-family', 'font-weight', 'font-subset', 'subset', 'subsets' ), true ) ) {
159
						continue;
160
					}
161
162
					// Get the value.
163
					$value = Kirki_Values::get_sanitized_field_value( $args );
164
165
					if ( 'font-family' === $output['property'] ) {
166
						if ( ! array_key_exists( $value, $this->fonts ) ) {
167
							$this->fonts[ $value ] = array();
168
						}
169
					} elseif ( 'font-weight' === $output['property'] ) {
170
						foreach ( $this->fonts as $font => $variants ) {
171
							if ( ! in_array( $value, $variants, true ) ) {
172
								$this->fonts[ $font ][] = $value;
173
							}
174
						}
175
					} elseif ( 'font-subset' === $output['property'] || 'subset' === $output['property'] || 'subsets' === $output['property'] ) {
176
						if ( ! is_array( $value ) ) {
177
							if ( ! in_array( $value, $this->subsets, true ) ) {
178
								$this->subsets[] = $value;
179
							}
180
						} else {
181
							foreach ( $value as $subset ) {
182
								if ( ! in_array( $subset, $this->subsets, true ) ) {
183
									$this->subsets[] = $subset;
184
								}
185
							}
186
						}
187
					}
188
				}
189
			} // End if().
190
		} // End if().
191
	}
192
193
	/**
194
	 * Determines the vbalidity of the selected font as well as its properties.
195
	 * This is vital to make sure that the google-font script that we'll generate later
196
	 * does not contain any invalid options.
197
	 */
198
	public function process_fonts() {
199
200
		// Early exit if font-family is empty.
201
		if ( empty( $this->fonts ) ) {
202
			return;
203
		}
204
205
		$valid_subsets = array();
206
		foreach ( $this->fonts as $font => $variants ) {
207
208
			// Determine if this is indeed a google font or not.
209
			// If it's not, then just remove it from the array.
210
			if ( ! array_key_exists( $font, $this->google_fonts ) ) {
211
				unset( $this->fonts[ $font ] );
212
				continue;
213
			}
214
215
			// Get all valid font variants for this font.
216
			$font_variants = array();
217
			if ( isset( $this->google_fonts[ $font ]['variants'] ) ) {
218
				$font_variants = $this->google_fonts[ $font ]['variants'];
219
			}
220
			foreach ( $variants as $variant ) {
221
222
				// If this is not a valid variant for this font-family
223
				// then unset it and move on to the next one.
224
				if ( ! in_array( $variant, $font_variants, true ) ) {
225
					$variant_key = array_search( $variant, $this->fonts[ $font ] );
226
					unset( $this->fonts[ $font ][ $variant_key ] );
227
					continue;
228
				}
229
			}
230
231
			// Check if the selected subsets exist, even in one of the selected fonts.
232
			// If they don't, then they have to be removed otherwise the link will fail.
233
			if ( isset( $this->google_fonts[ $font ]['subsets'] ) ) {
234
				foreach ( $this->subsets as $subset ) {
235
					if ( in_array( $subset, $this->google_fonts[ $font ]['subsets'], true ) ) {
236
						$valid_subsets[] = $subset;
237
					}
238
				}
239
			}
240
		}
241
		$this->subsets = $valid_subsets;
242
	}
243
}
244