| Conditions | 25 |
| Paths | 757 |
| Total Lines | 64 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 109 | public function sanitize( $value ) { |
||
| 110 | |||
| 111 | if ( ! is_array( $value ) ) { |
||
| 112 | return array(); |
||
| 113 | } |
||
| 114 | |||
| 115 | // Escape the font-family. |
||
| 116 | if ( isset( $value['font-family'] ) ) { |
||
| 117 | $value['font-family'] = esc_attr( $value['font-family'] ); |
||
| 118 | } |
||
| 119 | |||
| 120 | // Make sure we're using a valid variant. |
||
| 121 | // We're adding checks for font-weight as well for backwards-compatibility |
||
| 122 | // Versions 2.0 - 2.2 were using an integer font-weight. |
||
| 123 | if ( isset( $value['variant'] ) || isset( $value['font-weight'] ) ) { |
||
| 124 | if ( isset( $value['font-weight'] ) && ! empty( $value['font-weight'] ) ) { |
||
| 125 | if ( ! isset( $value['variant'] ) || empty( $value['variant'] ) ) { |
||
| 126 | $value['variant'] = $value['font-weight']; |
||
| 127 | } |
||
| 128 | unset( $value['font-weight'] ); |
||
| 129 | } |
||
| 130 | $valid_variants = Kirki_Fonts::get_all_variants(); |
||
| 131 | if ( ! array_key_exists( $value['variant'], $valid_variants ) ) { |
||
| 132 | $value['variant'] = 'regular'; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // Make sure the saved value is "subsets" (plural) and not "subset". |
||
| 137 | // This is for compatibility with older versions. |
||
| 138 | if ( isset( $value['subset'] ) ) { |
||
| 139 | if ( ! empty( $value['subset'] ) && ! isset( $value['subsets'] ) || empty( $value['subset'] ) ) { |
||
| 140 | $value['subsets'] = $value['subset']; |
||
| 141 | } |
||
| 142 | unset( $value['subset'] ); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Make sure we're using a valid subset. |
||
| 146 | if ( isset( $value['subsets'] ) ) { |
||
| 147 | $valid_subsets = Kirki_Fonts::get_google_font_subsets(); |
||
| 148 | $subsets_ok = array(); |
||
| 149 | if ( is_array( $value['subsets'] ) ) { |
||
| 150 | foreach ( $value['subsets'] as $subset ) { |
||
| 151 | if ( array_key_exists( $subset, $valid_subsets ) ) { |
||
| 152 | $subsets_ok[] = $subset; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | $value['subsets'] = $subsets_ok; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | foreach ( $value as $key => $subvalue ) { |
||
| 160 | |||
| 161 | if ( in_array( $key, array( 'font-size', 'letter-spacing', 'word-spacing', 'line-height' ), true ) ) { |
||
| 162 | $value[ $key ] = Kirki_Sanitize_Values::css_dimension( $value[ $key ] ); |
||
| 163 | } elseif ( 'text-align' === $key && ! in_array( $value['text-align'], array( 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) { |
||
| 164 | $value['text-align'] = 'inherit'; |
||
| 165 | } elseif ( 'text-transform' === $key && ! in_array( $value['text-transform'], array( 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) { |
||
| 166 | $value['text-transform'] = 'none'; |
||
| 167 | } elseif ( 'color' === $key ) { |
||
| 168 | $value['color'] = ariColor::newColor( $value['color'] )->toCSS( 'hex' ); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | return $value; |
||
| 172 | } |
||
| 173 | |||
| 196 |