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

Kirki_Fonts::get_all_subsets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
class Kirki_Fonts {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
4
5
	public static $mode = 'link';
6
7
	private static $instance = null;
8
9
	public static $google_fonts = null;
10
11
	private function __construct() {}
12
13
	public static function get_instance() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
14
		if ( null === self::$instance ) {
15
			self::$instance = new self();
16
		}
17
		return self::$instance;
18
	}
19
20
	/**
21
	 * Compile font options from different sources.
22
	 *
23
	 * @return array    All available fonts.
24
	 */
25
	public static function get_all_fonts() {
26
		$standard_fonts = self::get_standard_fonts();
27
		$google_fonts   = self::get_google_fonts();
28
29
		return apply_filters( 'kirki/fonts/all', array_merge( $standard_fonts, $google_fonts ) );
30
	}
31
32
	/**
33
	 * Return an array of standard websafe fonts.
34
	 *
35
	 * @return array    Standard websafe fonts.
36
	 */
37
	public static function get_standard_fonts() {
38
39
		$i18n = Kirki_Toolkit::i18n();
40
41
		return apply_filters( 'kirki/fonts/standard_fonts', array(
42
			'serif'     => array(
43
				'label' => $i18n['serif'],
44
				'stack' => 'Georgia,Times,"Times New Roman",serif',
45
			),
46
			'sans-serif' => array(
47
				'label'  => $i18n['sans-serif'],
48
				'stack'  => 'Helvetica,Arial,sans-serif',
49
			),
50
			'monospace' => array(
51
				'label' => $i18n['monospace'],
52
				'stack' => 'Monaco,"Lucida Sans Typewriter","Lucida Typewriter","Courier New",Courier,monospace',
53
			),
54
		) );
55
56
	}
57
58
	/**
59
	 * Return an array of backup fonts based on the font-category
60
	 *
61
	 * @return array
62
	 */
63
	public static function get_backup_fonts() {
64
		$backup_fonts = array(
65
			'sans-serif'  => 'Helvetica, Arial, sans-serif',
66
			'serif'       => 'Georgia, serif',
67
			'display'     => '"Comic Sans MS", cursive, sans-serif',
68
			'handwriting' => '"Comic Sans MS", cursive, sans-serif',
69
			'monospace'   => '"Lucida Console", Monaco, monospace',
70
		);
71
		return apply_filters( 'kirki/fonts/backup_fonts', $backup_fonts );
72
	}
73
74
	/**
75
	 * Return an array of all available Google Fonts.
76
	 *
77
	 * @return array    All Google Fonts.
78
	 */
79
	public static function get_google_fonts() {
80
81
		if ( null === self::$google_fonts || empty( self::$google_fonts ) ) {
82
83
			global $wp_filesystem;
84
			// Initialize the WP filesystem, no more using 'file-put-contents' function
85
			if ( empty( $wp_filesystem ) ) {
86
				require_once ( ABSPATH . '/wp-admin/includes/file.php' );
87
				WP_Filesystem();
88
			}
89
90
			$json_path = wp_normalize_path( dirname( dirname( __FILE__ ) ) . '/assets/json/webfonts.json' );
91
			$json      = $wp_filesystem->get_contents( $json_path );
92
			// Get the list of fonts from our json file and convert to an array
93
			$fonts = json_decode( $json, true );
94
95
			$google_fonts = array();
96
			if ( is_array( $fonts ) ) {
97
				foreach ( $fonts['items'] as $font ) {
98
					$google_fonts[ $font['family'] ] = array(
99
						'label'    => $font['family'],
100
						'variants' => $font['variants'],
101
						'subsets'  => $font['subsets'],
102
						'category' => $font['category'],
103
					);
104
				}
105
			}
106
107
			self::$google_fonts = apply_filters( 'kirki/fonts/google_fonts', $google_fonts );
108
109
		}
110
111
		return self::$google_fonts;
112
113
	}
114
115
	public static function get_all_subsets() {
116
		$i18n = Kirki_Toolkit::i18n();
117
		return array(
118
			'all'          => $i18n['all'],
119
			'cyrillic'     => $i18n['cyrillic'],
120
			'cyrillic-ext' => $i18n['cyrillic-ext'],
121
			'devanagari'   => $i18n['devanagari'],
122
			'greek'        => $i18n['greek'],
123
			'greek-ext'    => $i18n['greek-ext'],
124
			'khmer'        => $i18n['khmer'],
125
			'latin'        => $i18n['latin'],
126
			'latin-ext'    => $i18n['latin-ext'],
127
			'vietnamese'   => $i18n['vietnamese'],
128
			'hebrew'       => $i18n['hebrew'],
129
			'arabic'       => $i18n['arabic'],
130
			'bengali'      => $i18n['bengali'],
131
			'gujarati'     => $i18n['gujarati'],
132
			'tamil'        => $i18n['tamil'],
133
			'telugu'       => $i18n['telugu'],
134
			'thai'         => $i18n['thai'],
135
		);
136
	}
137
138
	public static function is_google_font( $fontname ) {
139
		if ( array_key_exists( $fontname, self::$google_fonts ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return array_key_exists(..., self::$google_fonts);.
Loading history...
140
			return true;
141
		}
142
		return false;
143
	}
144
145
	public static function get_font_choices() {
146
		$fonts = self::get_all_fonts();
147
		$fonts_array = array();
148
		foreach ( $fonts as $key => $args ) {
149
			$fonts_array[ $key ] = $key;
150
		}
151
		return $fonts_array;
152
	}
153
154
}
155