Completed
Branch develop (98485c)
by Aristeides
12:56 queued 06:30
created

Kirki_Fonts_Google   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 210
rs 8.6
c 2
b 1
f 0
wmc 37

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_instance() 0 5 2
A get_googlefonts_json() 0 3 1
A get_strandardfonts_json() 0 3 1
A __construct() 0 16 3
C process_fonts() 0 29 7
C generate_google_font() 0 65 23
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
	 * The array of fonts
40
	 *
41
	 * @access public
42
	 * @var array
43
	 */
44
	public $fonts = array();
45
46
	/**
47
	 * An array of all google fonts.
48
	 *
49
	 * @access private
50
	 * @var array
51
	 */
52
	private $google_fonts = array();
53
54
	/**
55
	 * The class constructor.
56
	 */
57
	private function __construct() {
58
59
		$config = apply_filters( 'kirki_config', array() );
1 ignored issue
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

59
		$config = /** @scrutinizer ignore-call */ apply_filters( 'kirki_config', array() );
Loading history...
60
61
		// If we have set $config['disable_google_fonts'] to true then do not proceed any further.
62
		if ( isset( $config['disable_google_fonts'] ) && true === $config['disable_google_fonts'] ) {
63
			return;
64
		}
65
66
		add_action( 'wp_ajax_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) );
1 ignored issue
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

66
		/** @scrutinizer ignore-call */ 
67
  add_action( 'wp_ajax_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) );
Loading history...
67
		add_action( 'wp_ajax_noprinv_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) );
68
		add_action( 'wp_ajax_kirki_fonts_standard_all_get', array( $this, 'get_strandardfonts_json' ) );
69
		add_action( 'wp_ajax_noprinv_kirki_fonts_standard_all_get', array( $this, 'get_strandardfonts_json' ) );
70
71
		// Populate the array of google fonts.
72
		$this->google_fonts = Kirki_Fonts::get_google_fonts();
73
	}
74
75
	/**
76
	 * Get the one, true instance of this class.
77
	 * Prevents performance issues since this is only loaded once.
78
	 *
79
	 * @return object Kirki_Fonts_Google
80
	 */
81
	public static function get_instance() {
82
		if ( null === self::$instance ) {
83
			self::$instance = new Kirki_Fonts_Google();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Kirki_Fonts_Google() of type Kirki_Fonts_Google 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...
84
		}
85
		return self::$instance;
86
	}
87
88
	/**
89
	 * Processes the arguments of a field
90
	 * determines if it's a typography field
91
	 * and if it is, then takes appropriate actions.
92
	 *
93
	 * @param array $args The field arguments.
94
	 */
95
	public function generate_google_font( $args ) {
96
97
		// Process typography fields.
98
		if ( isset( $args['type'] ) && 'kirki-typography' === $args['type'] ) {
99
100
			// Get the value.
101
			$value = Kirki_Values::get_sanitized_field_value( $args );
102
103
			// If we don't have a font-family then we can skip this.
104
			if ( ! isset( $value['font-family'] ) ) {
105
				return;
106
			}
107
108
			// If not a google-font, then we can skip this.
109
			if ( ! Kirki_Fonts::is_google_font( $value['font-family'] ) ) {
110
				return;
111
			}
112
113
			// Set a default value for variants.
114
			if ( ! isset( $value['variant'] ) ) {
115
				$value['variant'] = 'regular';
116
			}
117
118
			// Add the requested google-font.
119
			if ( ! isset( $this->fonts[ $value['font-family'] ] ) ) {
120
				$this->fonts[ $value['font-family'] ] = array();
121
			}
122
			if ( ! in_array( $value['variant'], $this->fonts[ $value['font-family'] ], true ) ) {
123
				$this->fonts[ $value['font-family'] ][] = $value['variant'];
124
			}
125
			// Are we force-loading all variants?
126
			if ( true === self::$force_load_all_variants ) {
127
				$all_variants               = Kirki_Fonts::get_all_variants();
128
				$args['choices']['variant'] = array_keys( $all_variants );
129
			}
130
131
			if ( ! empty( $args['choices']['variant'] ) && is_array( $args['choices']['variant'] ) ) {
132
				foreach ( $args['choices']['variant'] as $extra_variant ) {
133
					$this->fonts[ $value['font-family'] ][] = $extra_variant;
134
				}
135
			}
136
			return;
137
		}
138
139
		// Process non-typography fields.
140
		if ( isset( $args['output'] ) && is_array( $args['output'] ) ) {
141
			foreach ( $args['output'] as $output ) {
142
143
				// If we don't have a typography-related output argument we can skip this.
144
				if ( ! isset( $output['property'] ) || ! in_array( $output['property'], array( 'font-family', 'font-weight' ), true ) ) {
145
					continue;
146
				}
147
148
				// Get the value.
149
				$value = Kirki_Values::get_sanitized_field_value( $args );
150
151
				if ( is_string( $value ) ) {
152
					if ( 'font-family' === $output['property'] ) {
153
						if ( ! array_key_exists( $value, $this->fonts ) ) {
154
							$this->fonts[ $value ] = array();
155
						}
156
					} elseif ( 'font-weight' === $output['property'] ) {
157
						foreach ( $this->fonts as $font => $variants ) {
158
							if ( ! in_array( $value, $variants, true ) ) {
159
								$this->fonts[ $font ][] = $value;
160
							}
161
						}
162
					}
163
				}
164
			} // End foreach().
165
		} // End if().
166
	}
167
168
	/**
169
	 * Determines the vbalidity of the selected font as well as its properties.
170
	 * This is vital to make sure that the google-font script that we'll generate later
171
	 * does not contain any invalid options.
172
	 */
173
	public function process_fonts() {
174
175
		// Early exit if font-family is empty.
176
		if ( empty( $this->fonts ) ) {
177
			return;
178
		}
179
180
		foreach ( $this->fonts as $font => $variants ) {
181
182
			// Determine if this is indeed a google font or not.
183
			// If it's not, then just remove it from the array.
184
			if ( ! array_key_exists( $font, $this->google_fonts ) ) {
185
				unset( $this->fonts[ $font ] );
186
				continue;
187
			}
188
189
			// Get all valid font variants for this font.
190
			$font_variants = array();
191
			if ( isset( $this->google_fonts[ $font ]['variants'] ) ) {
192
				$font_variants = $this->google_fonts[ $font ]['variants'];
193
			}
194
			foreach ( $variants as $variant ) {
195
196
				// If this is not a valid variant for this font-family
197
				// then unset it and move on to the next one.
198
				if ( ! in_array( $variant, $font_variants, true ) ) {
199
					$variant_key = array_search( $variant, $this->fonts[ $font ], true );
200
					unset( $this->fonts[ $font ][ $variant_key ] );
201
					continue;
202
				}
203
			}
204
		}
205
	}
206
207
	/**
208
	 * Gets the googlefonts JSON file.
209
	 *
210
	 * @since 3.0.17
211
	 * @return void
212
	 */
213
	public function get_googlefonts_json() {
214
		include wp_normalize_path( dirname( __FILE__ ) . '/webfonts.json' );
1 ignored issue
show
Bug introduced by
The function wp_normalize_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

214
		include /** @scrutinizer ignore-call */ wp_normalize_path( dirname( __FILE__ ) . '/webfonts.json' );
Loading history...
215
		wp_die();
1 ignored issue
show
Bug introduced by
The function wp_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

215
		/** @scrutinizer ignore-call */ 
216
  wp_die();
Loading history...
216
	}
217
218
	/**
219
	 * Get the standard fonts JSON.
220
	 *
221
	 * @since 3.0.17
222
	 * @return void
223
	 */
224
	public function get_strandardfonts_json() {
225
		echo wp_json_encode( Kirki_Fonts::get_standard_fonts() ); // WPCS: XSS ok.
1 ignored issue
show
Bug introduced by
The function wp_json_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

225
		echo /** @scrutinizer ignore-call */ wp_json_encode( Kirki_Fonts::get_standard_fonts() ); // WPCS: XSS ok.
Loading history...
226
		wp_die();
1 ignored issue
show
Bug introduced by
The function wp_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

226
		/** @scrutinizer ignore-call */ 
227
  wp_die();
Loading history...
227
	}
228
}
229