Complex classes like Kirki_Modules_PostMessage 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_Modules_PostMessage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Kirki_Modules_PostMessage { |
||
| 22 | /** |
||
| 23 | * The object instance. |
||
| 24 | * |
||
| 25 | * @static |
||
| 26 | * @access private |
||
| 27 | * @since 3.0.0 |
||
| 28 | * @var object |
||
| 29 | */ |
||
| 30 | private static $instance; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The script. |
||
| 34 | * |
||
| 35 | * @access protected |
||
| 36 | * @since 3.0.0 |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $script = ''; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor. |
||
| 43 | * |
||
| 44 | * @access protected |
||
| 45 | * @since 3.0.0 |
||
| 46 | */ |
||
| 47 | protected function __construct() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Gets an instance of this object. |
||
| 53 | * Prevents duplicate instances which avoid artefacts and improves performance. |
||
| 54 | * |
||
| 55 | * @static |
||
| 56 | * @access public |
||
| 57 | * @since 3.0.0 |
||
| 58 | * @return object |
||
| 59 | */ |
||
| 60 | public static function get_instance() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Enqueues the postMessage script |
||
| 69 | * and adds variables to it using the wp_localize_script function. |
||
| 70 | * The rest is handled via JS. |
||
| 71 | */ |
||
| 72 | public function postmessage() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Generates script for a single field. |
||
| 88 | * |
||
| 89 | * @access protected |
||
| 90 | * @since 3.0.0 |
||
| 91 | * @param array $args The arguments. |
||
| 92 | */ |
||
| 93 | protected function script( $args ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Generates script for a single js_var when using "html" as function. |
||
| 146 | * |
||
| 147 | * @access protected |
||
| 148 | * @since 3.0.0 |
||
| 149 | * @param array $args The arguments for this js_var. |
||
| 150 | */ |
||
| 151 | protected function script_html_var( $args ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Generates script for a single js_var. |
||
| 163 | * |
||
| 164 | * @access protected |
||
| 165 | * @since 3.0.0 |
||
| 166 | * @param array $args The arguments for this js_var. |
||
| 167 | */ |
||
| 168 | protected function script_var( $args ) { |
||
| 169 | $script = ''; |
||
| 170 | $property_script = ''; |
||
| 171 | |||
| 172 | $value_key = 'newval' . $args['index_key']; |
||
| 173 | $property_script .= $value_key . '=newval;'; |
||
| 174 | |||
| 175 | $args = $this->get_args( $args ); |
||
| 176 | |||
| 177 | // Apply callback to the value if a callback is defined. |
||
| 178 | if ( ! empty( $args['js_callback'] ) && is_array( $args['js_callback'] ) && isset( $args['js_callback'][0] ) && ! empty( $args['js_callback'][0] ) ) { |
||
| 179 | $script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');'; |
||
| 180 | } |
||
| 181 | |||
| 182 | // Apply the value_pattern. |
||
| 183 | if ( '' !== $args['value_pattern'] ) { |
||
| 184 | $script .= $this->value_pattern_replacements( $value_key, $args ); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Tweak to add url() for background-images. |
||
| 188 | if ( 'background-image' === $args['property'] && ( ! isset( $args['value_pattern'] ) || false === strpos( $args['value_pattern'], 'gradient' ) ) ) { |
||
| 189 | $script .= 'if(-1===' . $value_key . '.indexOf(\'url(\')){' . $value_key . '=\'url("\'+' . $value_key . '+\'");}'; |
||
| 190 | } |
||
| 191 | |||
| 192 | // Apply prefix. |
||
| 193 | $value = $value_key; |
||
| 194 | if ( '' !== $args['prefix'] ) { |
||
| 195 | $value = $args['prefix'] . '+' . $value_key; |
||
| 196 | } |
||
| 197 | $css = $args['element'] . '{' . $args['property'] . ':\'+' . $value . '+\'' . $args['units'] . $args['suffix'] . ';}'; |
||
| 198 | if ( isset( $args['media_query'] ) ) { |
||
| 199 | $css = $args['media_query'] . '{' . $css . '}'; |
||
| 200 | } |
||
| 201 | return array( |
||
| 202 | 'script' => $property_script . $script, |
||
| 203 | 'css' => $css, |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Processes script generation for fields that save an array. |
||
| 209 | * |
||
| 210 | * @access protected |
||
| 211 | * @since 3.0.0 |
||
| 212 | * @param array $args The arguments for this js_var. |
||
| 213 | */ |
||
| 214 | protected function script_var_array( $args ) { |
||
| 215 | |||
| 216 | $script = ( 0 === $args['index_key'] ) ? 'css=\'\';' : ''; |
||
| 217 | $property_script = ''; |
||
| 218 | |||
| 219 | // Define choice. |
||
| 220 | $choice = ( isset( $args['choice'] ) && '' !== $args['choice'] ) ? $args['choice'] : ''; |
||
| 221 | |||
| 222 | $value_key = 'newval' . $args['index_key']; |
||
| 223 | $property_script .= $value_key . '=newval;'; |
||
| 224 | |||
| 225 | $args = $this->get_args( $args ); |
||
| 226 | |||
| 227 | // Apply callback to the value if a callback is defined. |
||
| 228 | if ( ! empty( $args['js_callback'] ) && is_array( $args['js_callback'] ) && isset( $args['js_callback'][0] ) && ! empty( $args['js_callback'][0] ) ) { |
||
| 229 | $script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');'; |
||
| 230 | } |
||
| 231 | $script .= '_.each(' . $value_key . ', function(subValue,subKey){'; |
||
| 232 | |||
| 233 | // Apply the value_pattern. |
||
| 234 | if ( '' !== $args['value_pattern'] ) { |
||
| 235 | $script .= $this->value_pattern_replacements( 'subValue', $args ); |
||
| 236 | } |
||
| 237 | |||
| 238 | // Tweak to add url() for background-images. |
||
| 239 | if ( '' === $choice || 'background-image' === $choice ) { |
||
| 240 | $script .= 'if(\'background-image\'===\'' . $args['property'] . '\'||\'background-image\'===subKey){'; |
||
| 241 | $script .= 'if(-1===subValue.indexOf(\'url(\')){subValue=\'url("\'+subValue+\'")\';}'; |
||
| 242 | $script .= '}'; |
||
| 243 | } |
||
| 244 | |||
| 245 | // Apply prefix. |
||
| 246 | $value = $value_key; |
||
| 247 | if ( '' !== $args['prefix'] ) { |
||
| 248 | $value = '\'' . $args['prefix'] . '\'+subValue'; |
||
| 249 | } |
||
| 250 | |||
| 251 | // Mostly used for padding, margin & position properties. |
||
| 252 | $direction_script = 'if(_.contains([\'top\',\'bottom\',\'left\',\'right\'],subKey)){'; |
||
| 253 | $direction_script .= 'css+=\'' . $args['element'] . '{' . $args['property'] . '-\'+subKey+\':\'+subValue+\'' . $args['units'] . $args['suffix'] . ';}\';}'; |
||
| 254 | // Allows us to apply this just for a specific choice in the array of the values. |
||
| 255 | if ( '' !== $choice ) { |
||
| 256 | $choice_is_direction = ( false !== strpos( $choice, 'top' ) || false !== strpos( $choice, 'bottom' ) || false !== strpos( $choice, 'left' ) || false !== strpos( $choice, 'right' ) ); |
||
| 257 | $script .= 'if(\'' . $choice . '\'===subKey){'; |
||
| 258 | $script .= ( $choice_is_direction ) ? $direction_script . 'else{' : ''; |
||
| 259 | $script .= 'css+=\'' . $args['element'] . '{' . $args['property'] . ':\'+subValue+\';}\';'; |
||
| 260 | $script .= ( $choice_is_direction ) ? '}' : ''; |
||
| 261 | $script .= '}'; |
||
| 262 | } else { |
||
| 263 | $script .= $direction_script . 'else{'; |
||
| 264 | |||
| 265 | // This is where most object-based fields will go. |
||
| 266 | $script .= 'css+=\'' . $args['element'] . '{\'+subKey+\':\'+subValue+\'' . $args['units'] . $args['suffix'] . ';}\';'; |
||
| 267 | $script .= '}'; |
||
| 268 | } |
||
| 269 | $script .= '});'; |
||
| 270 | |||
| 271 | if ( isset( $args['media_query'] ) ) { |
||
| 272 | $script .= 'css=\'' . $args['media_query'] . '{\'+css+\'}\';'; |
||
| 273 | } |
||
| 274 | |||
| 275 | return array( |
||
| 276 | 'script' => $property_script . $script, |
||
| 277 | 'css' => 'css', |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Processes script generation for typography fields. |
||
| 283 | * |
||
| 284 | * @access protected |
||
| 285 | * @since 3.0.0 |
||
| 286 | * @param array $args The arguments for this js_var. |
||
| 287 | */ |
||
| 288 | protected function script_var_typography( $args ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Adds anything we need before the main script. |
||
| 337 | * |
||
| 338 | * @access private |
||
| 339 | * @since 3.0.0 |
||
| 340 | * @param array $args The field args. |
||
| 341 | * @return string; |
||
|
|
|||
| 342 | */ |
||
| 343 | private function before_script( $args ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Sanitizes the arguments and makes sure they are all there. |
||
| 374 | * |
||
| 375 | * @access private |
||
| 376 | * @since 3.0.0 |
||
| 377 | * @param array $args The arguments. |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | private function get_args( $args ) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns script for value_pattern & replacements. |
||
| 408 | * |
||
| 409 | * @access private |
||
| 410 | * @since 3.0.0 |
||
| 411 | * @param string $value The value placeholder. |
||
| 412 | * @param array $js_vars The js_vars argument. |
||
| 413 | * @return string The script. |
||
| 414 | */ |
||
| 415 | private function value_pattern_replacements( $value, $js_vars ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the callback function/method we're going to use for this field. |
||
| 433 | * |
||
| 434 | * @access private |
||
| 435 | * @since 3.0.0 |
||
| 436 | * @param array $args The field args. |
||
| 437 | * @return string|array A callable function or method. |
||
| 438 | */ |
||
| 439 | protected function get_callback( $args ) { |
||
| 456 | } |
||
| 457 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.