| Total Complexity | 72 |
| Total Lines | 324 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Kirki_Output 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_Output, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Kirki_Output { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The Kirki configuration used in the field. |
||
| 19 | * |
||
| 20 | * @access protected |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $config_id = 'global'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The field's `output` argument. |
||
| 27 | * |
||
| 28 | * @access protected |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $output = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * An array of the generated styles. |
||
| 35 | * |
||
| 36 | * @access protected |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $styles = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The field. |
||
| 43 | * |
||
| 44 | * @access protected |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $field = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The value. |
||
| 51 | * |
||
| 52 | * @access protected |
||
| 53 | * @var string|array |
||
| 54 | */ |
||
| 55 | protected $value; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The class constructor. |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * @param string $config_id The config ID. |
||
| 62 | * @param array $output The output argument. |
||
| 63 | * @param string|array $value The value. |
||
| 64 | * @param array $field The field. |
||
| 65 | */ |
||
| 66 | public function __construct( $config_id, $output, $value, $field ) { |
||
| 67 | |||
| 68 | $this->config_id = $config_id; |
||
| 69 | $this->value = $value; |
||
| 70 | $this->output = $output; |
||
| 71 | $this->field = $field; |
||
| 72 | |||
| 73 | $this->parse_output(); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * If we have a sanitize_callback defined, apply it to the value. |
||
| 78 | * |
||
| 79 | * @param array $output The output args. |
||
| 80 | * @param string|array $value The value. |
||
| 81 | * |
||
| 82 | * @return string|array |
||
| 83 | */ |
||
| 84 | protected function apply_sanitize_callback( $output, $value ) { |
||
| 85 | |||
| 86 | if ( isset( $output['sanitize_callback'] ) && null !== $output['sanitize_callback'] ) { |
||
| 87 | |||
| 88 | // If the sanitize_callback is invalid, return the value. |
||
| 89 | if ( ! is_callable( $output['sanitize_callback'] ) ) { |
||
| 90 | return $value; |
||
| 91 | } |
||
| 92 | return call_user_func( $output['sanitize_callback'], $this->value ); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $value; |
||
| 96 | |||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * If we have a value_pattern defined, apply it to the value. |
||
| 101 | * |
||
| 102 | * @param array $output The output args. |
||
| 103 | * @param string|array $value The value. |
||
| 104 | * @return string|array |
||
| 105 | */ |
||
| 106 | protected function apply_value_pattern( $output, $value ) { |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * If we have a value_pattern defined, apply it to the value. |
||
| 133 | * |
||
| 134 | * @param array $output The output args. |
||
| 135 | * @param string|array $value The value. |
||
| 136 | * @return string|array |
||
| 137 | */ |
||
| 138 | protected function apply_pattern_replace( $output, $value ) { |
||
| 139 | if ( isset( $output['pattern_replace'] ) && is_array( $output['pattern_replace'] ) ) { |
||
| 140 | $option_type = ( '' !== Kirki::get_config_param( $this->config_id, 'option_type' ) ) ? Kirki::get_config_param( $this->config_id, 'option_type' ) : 'theme_mod'; |
||
| 141 | $option_name = Kirki::get_config_param( $this->config_id, 'option_name' ); |
||
| 142 | $options = array(); |
||
| 143 | if ( $option_name ) { |
||
| 144 | $options = ( 'site_option' === $option_type ) ? get_site_option( $option_name ) : get_option( $option_name ); |
||
| 145 | } |
||
| 146 | foreach ( $output['pattern_replace'] as $search => $replace ) { |
||
| 147 | $replacement = ''; |
||
| 148 | switch ( $option_type ) { |
||
| 149 | case 'option': |
||
| 150 | if ( is_array( $options ) ) { |
||
| 151 | if ( $option_name ) { |
||
| 152 | $subkey = str_replace( array( $option_name, '[', ']' ), '', $replace ); |
||
| 153 | $replacement = ( isset( $options[ $subkey ] ) ) ? $options[ $subkey ] : ''; |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | $replacement = ( isset( $options[ $replace ] ) ) ? $options[ $replace ] : ''; |
||
| 157 | break; |
||
| 158 | } |
||
| 159 | $replacement = get_option( $replace ); |
||
| 160 | break; |
||
| 161 | case 'site_option': |
||
| 162 | $replacement = ( is_array( $options ) && isset( $options[ $replace ] ) ) ? $options[ $replace ] : get_site_option( $replace ); |
||
| 163 | break; |
||
| 164 | case 'user_meta': |
||
| 165 | $user_id = get_current_user_id(); |
||
| 166 | if ( $user_id ) { |
||
| 167 | // @codingStandardsIgnoreLine |
||
| 168 | $replacement = get_user_meta( $user_id, $replace, true ); |
||
| 169 | } |
||
| 170 | break; |
||
| 171 | default: |
||
| 172 | $replacement = get_theme_mod( $replace ); |
||
| 173 | } |
||
| 174 | $replacement = ( false === $replacement ) ? '' : $replacement; |
||
| 175 | if ( is_array( $value ) ) { |
||
| 176 | foreach ( $value as $k => $v ) { |
||
| 177 | $_val = ( isset( $value[ $v ] ) ) ? $value[ $v ] : $v; |
||
| 178 | $value[ $k ] = str_replace( $search, $replacement, $_val ); |
||
| 179 | } |
||
| 180 | return $value; |
||
| 181 | } |
||
| 182 | $value = str_replace( $search, $replacement, $value ); |
||
| 183 | } // End foreach(). |
||
| 184 | } // End if(). |
||
| 185 | return $value; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Parses the output arguments. |
||
| 190 | * Calls the process_output method for each of them. |
||
| 191 | * |
||
| 192 | * @access protected |
||
| 193 | */ |
||
| 194 | protected function parse_output() { |
||
| 250 | } // End foreach(). |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Parses an output and creates the styles array for it. |
||
| 255 | * |
||
| 256 | * @access protected |
||
| 257 | * @param array $output The field output. |
||
| 258 | * @param string|array $value The value. |
||
| 259 | * |
||
| 260 | * @return null |
||
| 261 | */ |
||
| 262 | protected function process_output( $output, $value ) { |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Some CSS properties are unique. |
||
| 292 | * We need to tweak the value to make everything works as expected. |
||
| 293 | * |
||
| 294 | * @access protected |
||
| 295 | * @param string $property The CSS property. |
||
| 296 | * @param string|array $value The value. |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | protected function process_property_value( $property, $value ) { |
||
| 301 | $properties = apply_filters( |
||
| 302 | "kirki_{$this->config_id}_output_property_classnames", array( |
||
| 303 | 'font-family' => 'Kirki_Output_Property_Font_Family', |
||
| 304 | 'background-image' => 'Kirki_Output_Property_Background_Image', |
||
| 305 | 'background-position' => 'Kirki_Output_Property_Background_Position', |
||
| 306 | ) |
||
| 307 | ); |
||
| 308 | if ( array_key_exists( $property, $properties ) ) { |
||
| 309 | $classname = $properties[ $property ]; |
||
| 310 | $obj = new $classname( $property, $value ); |
||
| 311 | return $obj->get_value(); |
||
| 312 | } |
||
| 313 | return $value; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Returns the value. |
||
| 318 | * |
||
| 319 | * @access protected |
||
| 320 | * @param string|array $value The value. |
||
| 321 | * @param array $output The field "output". |
||
| 322 | * @return string|array |
||
| 323 | */ |
||
| 324 | protected function process_value( $value, $output ) { |
||
| 325 | if ( isset( $output['property'] ) ) { |
||
| 326 | return $this->process_property_value( $output['property'], $value ); |
||
| 327 | } |
||
| 328 | return $value; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Exploses the private $styles property to the world |
||
| 333 | * |
||
| 334 | * @access protected |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | public function get_styles() { |
||
| 339 | } |
||
| 340 | } |
||
| 341 |