| Total Complexity | 42 |
| Total Lines | 245 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 | |||
| 77 | $config = apply_filters( 'kirki_config', array() ); |
||
| 78 | |||
| 79 | // If we have set $config['disable_google_fonts'] to true then do not proceed any further. |
||
| 80 | if ( isset( $config['disable_google_fonts'] ) && true === $config['disable_google_fonts'] ) { |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | add_action( 'wp_ajax_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) ); |
||
| 85 | add_action( 'wp_ajax_nopriv_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) ); |
||
| 86 | add_action( 'wp_ajax_kirki_fonts_standard_all_get', array( $this, 'get_standardfonts_json' ) ); |
||
| 87 | add_action( 'wp_ajax_nopriv_kirki_fonts_standard_all_get', array( $this, 'get_standardfonts_json' ) ); |
||
| 88 | |||
| 89 | // Populate the array of google fonts. |
||
| 90 | $this->google_fonts = Kirki_Fonts::get_google_fonts(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the one, true instance of this class. |
||
| 95 | * Prevents performance issues since this is only loaded once. |
||
| 96 | * |
||
| 97 | * @return object Kirki_Fonts_Google |
||
| 98 | */ |
||
| 99 | public static function get_instance() { |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Processes the arguments of a field |
||
| 108 | * determines if it's a typography field |
||
| 109 | * and if it is, then takes appropriate actions. |
||
| 110 | * |
||
| 111 | * @param array $args The field arguments. |
||
| 112 | */ |
||
| 113 | public function generate_google_font( $args ) { |
||
| 114 | |||
| 115 | global $wp_customize; |
||
| 116 | |||
| 117 | // Process typography fields. |
||
| 118 | if ( isset( $args['type'] ) && 'kirki-typography' === $args['type'] ) { |
||
| 119 | |||
| 120 | // Get the value. |
||
| 121 | $value = Kirki_Values::get_sanitized_field_value( $args ); |
||
| 122 | |||
| 123 | if ( isset( $value['downloadFont'] ) && $value['downloadFont'] ) { |
||
| 124 | $this->hosted_fonts[] = $value['font-family']; |
||
| 125 | } |
||
| 126 | |||
| 127 | // If we don't have a font-family then we can skip this. |
||
| 128 | if ( ! $wp_customize && ( ! isset( $value['font-family'] ) || in_array( $value['font-family'], $this->hosted_fonts ) ) ) { |
||
| 129 | return; |
||
| 130 | } |
||
| 131 | |||
| 132 | // If not a google-font, then we can skip this. |
||
| 133 | if ( ! Kirki_Fonts::is_google_font( $value['font-family'] ) ) { |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | // Set a default value for variants. |
||
| 138 | if ( ! isset( $value['variant'] ) ) { |
||
| 139 | $value['variant'] = 'regular'; |
||
| 140 | } |
||
| 141 | |||
| 142 | // Add the requested google-font. |
||
| 143 | if ( ! isset( $this->fonts[ $value['font-family'] ] ) ) { |
||
| 144 | $this->fonts[ $value['font-family'] ] = array(); |
||
| 145 | } |
||
| 146 | if ( ! in_array( $value['variant'], $this->fonts[ $value['font-family'] ], true ) ) { |
||
| 147 | $this->fonts[ $value['font-family'] ][] = $value['variant']; |
||
| 148 | } |
||
| 149 | // Are we force-loading all variants? |
||
| 150 | if ( true === self::$force_load_all_variants ) { |
||
| 151 | $all_variants = Kirki_Fonts::get_all_variants(); |
||
| 152 | $args['choices']['variant'] = array_keys( $all_variants ); |
||
| 153 | } |
||
| 154 | |||
| 155 | if ( ! empty( $args['choices']['variant'] ) && is_array( $args['choices']['variant'] ) ) { |
||
| 156 | foreach ( $args['choices']['variant'] as $extra_variant ) { |
||
| 157 | $this->fonts[ $value['font-family'] ][] = $extra_variant; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | // Process non-typography fields. |
||
| 164 | if ( isset( $args['output'] ) && is_array( $args['output'] ) ) { |
||
| 165 | foreach ( $args['output'] as $output ) { |
||
| 166 | |||
| 167 | // If we don't have a typography-related output argument we can skip this. |
||
| 168 | if ( ! isset( $output['property'] ) || ! in_array( $output['property'], array( 'font-family', 'font-weight' ), true ) ) { |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | // Get the value. |
||
| 173 | $value = Kirki_Values::get_sanitized_field_value( $args ); |
||
| 174 | |||
| 175 | if ( is_string( $value ) ) { |
||
| 176 | if ( 'font-family' === $output['property'] ) { |
||
| 177 | if ( ! array_key_exists( $value, $this->fonts ) ) { |
||
| 178 | $this->fonts[ $value ] = array(); |
||
| 179 | } |
||
| 180 | } elseif ( 'font-weight' === $output['property'] ) { |
||
| 181 | foreach ( $this->fonts as $font => $variants ) { |
||
| 182 | if ( ! in_array( $value, $variants, true ) ) { |
||
| 183 | $this->fonts[ $font ][] = $value; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } // End foreach(). |
||
| 189 | } // End if(). |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Determines the vbalidity of the selected font as well as its properties. |
||
| 194 | * This is vital to make sure that the google-font script that we'll generate later |
||
| 195 | * does not contain any invalid options. |
||
| 196 | */ |
||
| 197 | public function process_fonts() { |
||
| 198 | |||
| 199 | // Early exit if font-family is empty. |
||
| 200 | if ( empty( $this->fonts ) ) { |
||
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | foreach ( $this->fonts as $font => $variants ) { |
||
| 205 | |||
| 206 | // Determine if this is indeed a google font or not. |
||
| 207 | // If it's not, then just remove it from the array. |
||
| 208 | if ( ! array_key_exists( $font, $this->google_fonts ) ) { |
||
| 209 | unset( $this->fonts[ $font ] ); |
||
| 210 | continue; |
||
| 211 | } |
||
| 212 | |||
| 213 | // Get all valid font variants for this font. |
||
| 214 | $font_variants = array(); |
||
| 215 | if ( isset( $this->google_fonts[ $font ]['variants'] ) ) { |
||
| 216 | $font_variants = $this->google_fonts[ $font ]['variants']; |
||
| 217 | } |
||
| 218 | foreach ( $variants as $variant ) { |
||
| 219 | |||
| 220 | // If this is not a valid variant for this font-family |
||
| 221 | // then unset it and move on to the next one. |
||
| 222 | if ( ! in_array( $variant, $font_variants, true ) ) { |
||
| 223 | $variant_key = array_search( $variant, $this->fonts[ $font ], true ); |
||
| 224 | unset( $this->fonts[ $font ][ $variant_key ] ); |
||
| 225 | continue; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Gets the googlefonts JSON file. |
||
| 233 | * |
||
| 234 | * @since 3.0.17 |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | public function get_googlefonts_json() { |
||
| 238 | include wp_normalize_path( dirname( __FILE__ ) . '/webfonts.json' ); |
||
| 239 | wp_die(); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the standard fonts JSON. |
||
| 244 | * |
||
| 245 | * @since 3.0.17 |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | public function get_standardfonts_json() { |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Gets $this->hosted_fonts. |
||
| 255 | * |
||
| 256 | * @access public |
||
| 257 | * @since 3.0.32 |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function get_hosted_fonts() { |
||
| 262 | } |
||
| 263 | } |
||
| 264 |
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..