| Total Complexity | 50 |
| Total Lines | 203 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Kirki_Field_Typography 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_Field_Typography, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Kirki_Field_Typography extends Kirki_Field { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Sets the control type. |
||
| 19 | * |
||
| 20 | * @access protected |
||
| 21 | */ |
||
| 22 | protected function set_type() { |
||
| 23 | |||
| 24 | $this->type = 'kirki-typography'; |
||
| 25 | |||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The class constructor. |
||
| 30 | * Parses and sanitizes all field arguments. |
||
| 31 | * Then it adds the field to Kirki::$fields. |
||
| 32 | * |
||
| 33 | * @access public |
||
| 34 | * @param string $config_id The ID of the config we want to use. |
||
| 35 | * Defaults to "global". |
||
| 36 | * Configs are handled by the Kirki_Config class. |
||
| 37 | * @param array $args The arguments of the field. |
||
| 38 | */ |
||
| 39 | public function __construct( $config_id = 'global', $args = array() ) { |
||
| 40 | parent::__construct( $config_id, $args ); |
||
| 41 | $this->set_default(); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sets the default value. |
||
| 46 | * |
||
| 47 | * @access protected |
||
| 48 | */ |
||
| 49 | protected function set_default() { |
||
| 50 | |||
| 51 | // Accomodate the use of font-weight and convert to variant. |
||
| 52 | if ( isset( $this->default['font-weight'] ) ) { |
||
| 53 | $this->default['variant'] = ( 'regular' === $this->default['font-weight'] ) ? 400 : (string) intval( $this->default['font-weight'] ); |
||
| 54 | } |
||
| 55 | |||
| 56 | // Make sure letter-spacing has units. |
||
| 57 | if ( isset( $this->default['letter-spacing'] ) && is_numeric( $this->default['letter-spacing'] ) && $this->default['letter-spacing'] ) { |
||
| 58 | $this->default['letter-spacing'] .= 'px'; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sets the $sanitize_callback |
||
| 64 | * |
||
| 65 | * @access protected |
||
| 66 | */ |
||
| 67 | protected function set_sanitize_callback() { |
||
| 68 | |||
| 69 | // If a custom sanitize_callback has been defined, |
||
| 70 | // then we don't need to proceed any further. |
||
| 71 | if ( ! empty( $this->sanitize_callback ) ) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | $this->sanitize_callback = array( __CLASS__, 'sanitize' ); |
||
| 75 | |||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Sets the $js_vars |
||
| 80 | * |
||
| 81 | * @access protected |
||
| 82 | */ |
||
| 83 | protected function set_js_vars() { |
||
| 127 | |||
| 128 | } |
||
| 129 | |||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Sanitizes typography controls |
||
| 134 | * |
||
| 135 | * @static |
||
| 136 | * @since 2.2.0 |
||
| 137 | * @param array $value The value. |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public static function sanitize( $value ) { |
||
| 141 | |||
| 142 | if ( ! is_array( $value ) ) { |
||
| 143 | return array(); |
||
| 144 | } |
||
| 145 | |||
| 146 | foreach ( $value as $key => $val ) { |
||
| 147 | switch ( $key ) { |
||
| 148 | case 'font-family': |
||
| 149 | $value['font-family'] = esc_attr( $val ); |
||
| 150 | break; |
||
| 151 | case 'font-weight': |
||
| 152 | if ( isset( $value['variant'] ) ) { |
||
| 153 | break; |
||
| 154 | } |
||
| 155 | $value['variant'] = $val; |
||
| 156 | if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) { |
||
| 157 | $value['variant'] = ( '400' !== $val || 400 !== $val ) ? $value['variant'] . 'italic' : 'italic'; |
||
| 158 | } |
||
| 159 | break; |
||
| 160 | case 'variant': |
||
| 161 | // Use 'regular' instead of 400 for font-variant. |
||
| 162 | $value['variant'] = ( 400 === $val || '400' === $val ) ? 'regular' : $val; |
||
| 163 | // Get font-weight from variant. |
||
| 164 | $value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
||
| 165 | $value['font-weight'] = ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) ? 400 : absint( $value['font-weight'] ); |
||
| 166 | // Get font-style from variant. |
||
| 167 | if ( ! isset( $value['font-style'] ) ) { |
||
| 168 | $value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic'; |
||
| 169 | } |
||
| 170 | break; |
||
| 171 | case 'font-size': |
||
| 172 | case 'letter-spacing': |
||
| 173 | case 'word-spacing': |
||
| 174 | case 'line-height': |
||
| 175 | $value[ $key ] = '' === trim( $value[ $key ] ) ? '' : sanitize_text_field( $val ); |
||
| 176 | break; |
||
| 177 | case 'text-align': |
||
| 178 | if ( ! in_array( $val, array( '', 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) { |
||
| 179 | $value['text-align'] = ''; |
||
| 180 | } |
||
| 181 | break; |
||
| 182 | case 'text-transform': |
||
| 183 | if ( ! in_array( $val, array( '', 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) { |
||
| 184 | $value['text-transform'] = ''; |
||
| 185 | } |
||
| 186 | break; |
||
| 187 | case 'text-decoration': |
||
| 188 | if ( ! in_array( $val, array( ''. 'none', 'underline', 'overline', 'line-through', 'initial', 'inherit' ), true ) ) { |
||
| 189 | $value['text-transform'] = ''; |
||
| 190 | } |
||
| 191 | break; |
||
| 192 | case 'color': |
||
| 193 | $value['color'] = '' === $value['color'] ? '' : ariColor::newColor( $val )->toCSS( 'hex' ); |
||
| 194 | break; |
||
| 195 | } // End switch(). |
||
| 196 | } // End foreach(). |
||
| 197 | |||
| 198 | return $value; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Sets the $choices |
||
| 203 | * |
||
| 204 | * @access protected |
||
| 205 | * @since 3.0.0 |
||
| 206 | */ |
||
| 207 | protected function set_choices() { |
||
| 218 | ), |
||
| 219 | ) |
||
| 223 |