Total Complexity | 43 |
Total Lines | 244 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 0 |
Complex classes like Kirki_Fonts_Google often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Kirki_Fonts_Google, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 | * DUMMY. DOESN'T DO ANYTHING, SIMPLY BACKWARDS-COMPATIBILITY. |
||
31 | * |
||
32 | * @static |
||
33 | * @access public |
||
34 | * @var bool |
||
35 | */ |
||
36 | public static $force_load_all_subsets = false; |
||
37 | |||
38 | /** |
||
39 | * If set to true, forces loading ALL variants. |
||
40 | * |
||
41 | * @static |
||
42 | * @access public |
||
43 | * @var bool |
||
44 | */ |
||
45 | public static $force_load_all_variants = 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 | * An array of fonts that should be hosted locally instead of served via the google-CDN. |
||
65 | * |
||
66 | * @access protected |
||
67 | * @since 3.0.32 |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $hosted_fonts = array(); |
||
71 | |||
72 | /** |
||
73 | * The class constructor. |
||
74 | */ |
||
75 | private function __construct() { |
||
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 | add_action( 'wp_ajax_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) ); |
||
84 | add_action( 'wp_ajax_nopriv_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) ); |
||
85 | add_action( 'wp_ajax_kirki_fonts_standard_all_get', array( $this, 'get_standardfonts_json' ) ); |
||
86 | add_action( 'wp_ajax_nopriv_kirki_fonts_standard_all_get', array( $this, 'get_standardfonts_json' ) ); |
||
87 | |||
88 | // Populate the array of google fonts. |
||
89 | $this->google_fonts = Kirki_Fonts::get_google_fonts(); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Get the one, true instance of this class. |
||
94 | * Prevents performance issues since this is only loaded once. |
||
95 | * |
||
96 | * @return object Kirki_Fonts_Google |
||
97 | */ |
||
98 | public static function get_instance() { |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Processes the arguments of a field |
||
107 | * determines if it's a typography field |
||
108 | * and if it is, then takes appropriate actions. |
||
109 | * |
||
110 | * @param array $args The field arguments. |
||
111 | */ |
||
112 | public function generate_google_font( $args ) { |
||
113 | global $wp_customize; |
||
114 | |||
115 | // Process typography fields. |
||
116 | if ( isset( $args['type'] ) && 'kirki-typography' === $args['type'] ) { |
||
117 | |||
118 | // Get the value. |
||
119 | $value = Kirki_Values::get_sanitized_field_value( $args ); |
||
120 | |||
121 | if ( isset( $value['downloadFont'] ) && $value['downloadFont'] ) { |
||
122 | $this->hosted_fonts[] = $value['font-family']; |
||
123 | } |
||
124 | |||
125 | // If we don't have a font-family then we can skip this. |
||
126 | if ( ! $wp_customize && ( ! isset( $value['font-family'] ) || in_array( $value['font-family'], $this->hosted_fonts, true ) ) ) { |
||
127 | return; |
||
128 | } |
||
129 | |||
130 | // If not a google-font, then we can skip this. |
||
131 | if ( ! isset( $value['font-family'] ) || ! Kirki_Fonts::is_google_font( $value['font-family'] ) ) { |
||
132 | return; |
||
133 | } |
||
134 | |||
135 | // Set a default value for variants. |
||
136 | if ( ! isset( $value['variant'] ) ) { |
||
137 | $value['variant'] = 'regular'; |
||
138 | } |
||
139 | |||
140 | // Add the requested google-font. |
||
141 | if ( ! isset( $this->fonts[ $value['font-family'] ] ) ) { |
||
142 | $this->fonts[ $value['font-family'] ] = array(); |
||
143 | } |
||
144 | if ( ! in_array( $value['variant'], $this->fonts[ $value['font-family'] ], true ) ) { |
||
145 | $this->fonts[ $value['font-family'] ][] = $value['variant']; |
||
146 | } |
||
147 | |||
148 | // Are we force-loading all variants? |
||
149 | if ( true === self::$force_load_all_variants ) { |
||
150 | $all_variants = Kirki_Fonts::get_all_variants(); |
||
151 | $args['choices']['variant'] = array_keys( $all_variants ); |
||
152 | } |
||
153 | |||
154 | if ( ! empty( $args['choices']['variant'] ) && is_array( $args['choices']['variant'] ) ) { |
||
155 | foreach ( $args['choices']['variant'] as $extra_variant ) { |
||
156 | $this->fonts[ $value['font-family'] ][] = $extra_variant; |
||
157 | } |
||
158 | } |
||
159 | return; |
||
160 | } |
||
161 | |||
162 | // Process non-typography fields. |
||
163 | if ( isset( $args['output'] ) && is_array( $args['output'] ) ) { |
||
164 | foreach ( $args['output'] as $output ) { |
||
165 | |||
166 | // If we don't have a typography-related output argument we can skip this. |
||
167 | if ( ! isset( $output['property'] ) || ! in_array( $output['property'], array( 'font-family', 'font-weight' ), true ) ) { |
||
168 | continue; |
||
169 | } |
||
170 | |||
171 | // Get the value. |
||
172 | $value = Kirki_Values::get_sanitized_field_value( $args ); |
||
173 | |||
174 | if ( is_string( $value ) ) { |
||
175 | if ( 'font-family' === $output['property'] ) { |
||
176 | if ( ! array_key_exists( $value, $this->fonts ) ) { |
||
177 | $this->fonts[ $value ] = array(); |
||
178 | } |
||
179 | } elseif ( 'font-weight' === $output['property'] ) { |
||
180 | foreach ( $this->fonts as $font => $variants ) { |
||
181 | if ( ! in_array( $value, $variants, true ) ) { |
||
182 | $this->fonts[ $font ][] = $value; |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Determines the vbalidity of the selected font as well as its properties. |
||
193 | * This is vital to make sure that the google-font script that we'll generate later |
||
194 | * does not contain any invalid options. |
||
195 | */ |
||
196 | public function process_fonts() { |
||
197 | |||
198 | // Early exit if font-family is empty. |
||
199 | if ( empty( $this->fonts ) ) { |
||
200 | return; |
||
201 | } |
||
202 | |||
203 | foreach ( $this->fonts as $font => $variants ) { |
||
204 | |||
205 | // Determine if this is indeed a google font or not. |
||
206 | // If it's not, then just remove it from the array. |
||
207 | if ( ! array_key_exists( $font, $this->google_fonts ) ) { |
||
208 | unset( $this->fonts[ $font ] ); |
||
209 | continue; |
||
210 | } |
||
211 | |||
212 | // Get all valid font variants for this font. |
||
213 | $font_variants = array(); |
||
214 | if ( isset( $this->google_fonts[ $font ]['variants'] ) ) { |
||
215 | $font_variants = $this->google_fonts[ $font ]['variants']; |
||
216 | } |
||
217 | foreach ( $variants as $variant ) { |
||
218 | |||
219 | // If this is not a valid variant for this font-family |
||
220 | // then unset it and move on to the next one. |
||
221 | if ( ! in_array( $variant, $font_variants, true ) ) { |
||
222 | $variant_key = array_search( $variant, $this->fonts[ $font ], true ); |
||
223 | unset( $this->fonts[ $font ][ $variant_key ] ); |
||
224 | continue; |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Gets the googlefonts JSON file. |
||
232 | * |
||
233 | * @since 3.0.17 |
||
234 | * @return void |
||
235 | */ |
||
236 | public function get_googlefonts_json() { |
||
237 | include wp_normalize_path( dirname( __FILE__ ) . '/webfonts.json' ); |
||
238 | wp_die(); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Get the standard fonts JSON. |
||
243 | * |
||
244 | * @since 3.0.17 |
||
245 | * @return void |
||
246 | */ |
||
247 | public function get_standardfonts_json() { |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Gets $this->hosted_fonts. |
||
254 | * |
||
255 | * @access public |
||
256 | * @since 3.0.32 |
||
257 | * @return array |
||
258 | */ |
||
259 | public function get_hosted_fonts() { |
||
261 | } |
||
262 | } |
||
263 |