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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 | * 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 | add_action( 'wp_ajax_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) ); |
||
84 | add_action( 'wp_ajax_noprinv_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) ); |
||
85 | |||
86 | // Populate the array of google fonts. |
||
87 | $this->google_fonts = Kirki_Fonts::get_google_fonts(); |
||
88 | |||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get the one, true instance of this class. |
||
93 | * Prevents performance issues since this is only loaded once. |
||
94 | * |
||
95 | * @return object Kirki_Fonts_Google |
||
96 | */ |
||
97 | public static function get_instance() { |
||
103 | |||
104 | /** |
||
105 | * Processes the arguments of a field |
||
106 | * determines if it's a typography field |
||
107 | * and if it is, then takes appropriate actions. |
||
108 | * |
||
109 | * @param array $args The field arguments. |
||
110 | */ |
||
111 | public function generate_google_font( $args ) { |
||
112 | |||
113 | // Process typography fields. |
||
114 | if ( isset( $args['type'] ) && 'kirki-typography' === $args['type'] ) { |
||
115 | |||
116 | // Get the value. |
||
117 | $value = Kirki_Values::get_sanitized_field_value( $args ); |
||
118 | |||
119 | // If we don't have a font-family then we can skip this. |
||
120 | if ( ! isset( $value['font-family'] ) ) { |
||
121 | return; |
||
122 | } |
||
123 | |||
124 | // If not a google-font, then we can skip this. |
||
125 | if ( ! Kirki_Fonts::is_google_font( $value['font-family'] ) ) { |
||
126 | return; |
||
127 | } |
||
128 | |||
129 | // Set a default value for variants. |
||
130 | if ( ! isset( $value['variant'] ) ) { |
||
131 | $value['variant'] = 'regular'; |
||
132 | } |
||
133 | if ( isset( $value['subsets'] ) ) { |
||
134 | |||
135 | // Add the subset directly to the array of subsets in the Kirki_GoogleFonts_Manager object. |
||
136 | // Subsets must be applied to ALL fonts if possible. |
||
137 | if ( ! is_array( $value['subsets'] ) ) { |
||
138 | $this->subsets[] = $value['subsets']; |
||
139 | } else { |
||
140 | foreach ( $value['subsets'] as $subset ) { |
||
141 | $this->subsets[] = $subset; |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | // Add the requested google-font. |
||
147 | if ( ! isset( $this->fonts[ $value['font-family'] ] ) ) { |
||
148 | $this->fonts[ $value['font-family'] ] = array(); |
||
149 | } |
||
150 | if ( ! in_array( $value['variant'], $this->fonts[ $value['font-family'] ], true ) ) { |
||
151 | $this->fonts[ $value['font-family'] ][] = $value['variant']; |
||
152 | } |
||
153 | // Are we force-loading all variants? |
||
154 | if ( true === self::$force_load_all_variants ) { |
||
155 | $all_variants = Kirki_Fonts::get_all_variants(); |
||
156 | $args['choices']['variant'] = array_keys( $all_variants ); |
||
157 | } |
||
158 | |||
159 | if ( ! empty( $args['choices']['variant'] ) && is_array( $args['choices']['variant'] ) ) { |
||
160 | foreach ( $args['choices']['variant'] as $extra_variant ) { |
||
161 | $this->fonts[ $value['font-family'] ][] = $extra_variant; |
||
162 | } |
||
163 | } |
||
164 | } else { |
||
165 | |||
166 | // Process non-typography fields. |
||
167 | if ( isset( $args['output'] ) && is_array( $args['output'] ) ) { |
||
168 | foreach ( $args['output'] as $output ) { |
||
169 | |||
170 | // If we don't have a typography-related output argument we can skip this. |
||
171 | if ( ! isset( $output['property'] ) || ! in_array( $output['property'], array( 'font-family', 'font-weight', 'font-subset', 'subset', 'subsets' ), true ) ) { |
||
172 | continue; |
||
173 | } |
||
174 | |||
175 | // Get the value. |
||
176 | $value = Kirki_Values::get_sanitized_field_value( $args ); |
||
177 | |||
178 | if ( is_string( $value ) ) { |
||
179 | if ( 'font-family' === $output['property'] ) { |
||
180 | if ( ! array_key_exists( $value, $this->fonts ) ) { |
||
181 | $this->fonts[ $value ] = array(); |
||
182 | } |
||
183 | } elseif ( 'font-weight' === $output['property'] ) { |
||
184 | foreach ( $this->fonts as $font => $variants ) { |
||
185 | if ( ! in_array( $value, $variants, true ) ) { |
||
186 | $this->fonts[ $font ][] = $value; |
||
187 | } |
||
188 | } |
||
189 | } elseif ( 'font-subset' === $output['property'] || 'subset' === $output['property'] || 'subsets' === $output['property'] ) { |
||
190 | if ( ! is_array( $value ) ) { |
||
191 | if ( ! in_array( $value, $this->subsets, true ) ) { |
||
192 | $this->subsets[] = $value; |
||
193 | } |
||
194 | } else { |
||
195 | foreach ( $value as $subset ) { |
||
196 | if ( ! in_array( $subset, $this->subsets, true ) ) { |
||
197 | $this->subsets[] = $subset; |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | } // End foreach(). |
||
204 | } // End if(). |
||
205 | } // End if(). |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Determines the vbalidity of the selected font as well as its properties. |
||
210 | * This is vital to make sure that the google-font script that we'll generate later |
||
211 | * does not contain any invalid options. |
||
212 | */ |
||
213 | public function process_fonts() { |
||
258 | |||
259 | /** |
||
260 | * Gets the googlefonts JSON file. |
||
261 | * |
||
262 | * @since 3.0.17 |
||
263 | * @return void |
||
264 | */ |
||
265 | public function get_googlefonts_json() { |
||
269 | } |
||
270 |