Completed
Branch master (3e1132)
by Aristeides
03:12
created

Kirki_GoogleFonts_Manager::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
class Kirki_GoogleFonts_Manager {
4
5
	/**
6
	 * The one true instance of this object
7
	 */
8
	private static $instance = null;
9
10
	/**
11
	 * All requested subsets
12
	 */
13
	public static $subsets = array();
14
15
	/**
16
	 * Additional font-weights
17
	 */
18
	public static $font_weights = array();
19
20
	/**
21
	 * The font-families including their font-weights and subsets
22
	 */
23
	protected static $fonts = array();
24
25
	/**
26
	 * Get the one, true instance of this class.
27
	 * If the class has not yet been instantiated then we create a new instance.
28
	 *
29
	 * @return Kirki_Google_Fonts_Manager object
30
	 */
31
	public static function get_instance() {
32
33
		if ( null === self::$instance ) {
34
			self::$instance = new self();
35
		}
36
		return self::$instance;
37
38
	}
39
40
	/**
41
	 * adds a font and specifies its properties
42
	 */
43
	public static function add_font( $family = '', $weight = 400, $style = '', $subsets = array( 'latin' ) ) {
44
		// Early exit if font-family is empty
45
		if ( '' == $family ) {
46
			return;
47
		}
48
49
		$google_fonts = Kirki_Fonts::get_google_fonts();
50
51
		// Make sure the class is instantiated
52
		self::get_instance();
53
54
		$family = esc_attr( $family );
55
56
		// Determine if this is indeed a google font or not.
57
		$is_google_font = false;
58
		if ( array_key_exists( $family, $google_fonts ) ) {
59
			$is_google_font = true;
60
		}
61
62
		// If we're not dealing with a valid google font then we can exit.
63
		if ( ! $is_google_font ) {
64
			return;
65
		}
66
67
		// Get all valid font variants for this font
68
		$font_variants = array();
69
		if ( isset( $google_fonts[ $family ]['variants'] ) ) {
70
			$font_variants = $google_fonts[ $family ]['variants'];
71
		}
72
73
		// format the requested variant
74
		$requested_variant = $weight . $style;
75
76
		// Is this a valid variant for this font?
77
		$variant_is_valid = false;
78
		if ( in_array( $requested_variant, $font_variants ) ) {
79
			$variant_is_valid = true;
80
		}
81
82
		// Get all available subsets for this font
83
		$font_subsets = array();
84
		if ( isset( $google_fonts[ $family ]['subsets'] ) ) {
85
			$font_subsets = $google_fonts[ $family ]['subsets'];
86
		}
87
88
		// Get the valid subsets for this font
89
		$requested_subsets = array_intersect( $subsets, $font_subsets );
90
91
		// If this font only has 1 subset, then use it.
92
		if ( 1 == count( $font_subsets ) ) {
93
			$requested_subsets = $font_subsets;
94
		}
95
96
		self::$fonts[ $family ] = array(
97
			'subsets' => $requested_subsets,
98
		);
99
		if ( ! isset( self::$fonts[ $family ]['variants'] ) ) {
100
			self::$fonts[ $family ]['variants'] = array();
101
		}
102
		if ( $variant_is_valid ) {
103
			self::$fonts[ $family ]['variants'][] =$requested_variant;
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
104
		}
105
106
	}
107
108
}
109