Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_Styles 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 WP_Styles, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class WP_Styles extends WP_Dependencies { |
||
| 19 | /** |
||
| 20 | * Base URL for styles. |
||
| 21 | * |
||
| 22 | * Full URL with trailing slash. |
||
| 23 | * |
||
| 24 | * @since 2.6.0 |
||
| 25 | * @access public |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $base_url; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * URL of the content directory. |
||
| 32 | * |
||
| 33 | * @since 2.8.0 |
||
| 34 | * @access public |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public $content_url; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Default version string for stylesheets. |
||
| 41 | * |
||
| 42 | * @since 2.6.0 |
||
| 43 | * @access public |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $default_version; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The current text direction. |
||
| 50 | * |
||
| 51 | * @since 2.6.0 |
||
| 52 | * @access public |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $text_direction = 'ltr'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds a list of style handles which will be concatenated. |
||
| 59 | * |
||
| 60 | * @since 2.8.0 |
||
| 61 | * @access public |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | public $concat = ''; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Holds a string which contains style handles and their version. |
||
| 68 | * |
||
| 69 | * @since 2.8.0 |
||
| 70 | * @deprecated 3.4.0 |
||
| 71 | * @access public |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | public $concat_version = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Whether to perform concatenation. |
||
| 78 | * |
||
| 79 | * @since 2.8.0 |
||
| 80 | * @access public |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | public $do_concat = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Holds HTML markup of styles and additional data if concatenation |
||
| 87 | * is enabled. |
||
| 88 | * |
||
| 89 | * @since 2.8.0 |
||
| 90 | * @access public |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | public $print_html = ''; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Holds inline styles if concatenation is enabled. |
||
| 97 | * |
||
| 98 | * @since 3.3.0 |
||
| 99 | * @access public |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | public $print_code = ''; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * List of default directories. |
||
| 106 | * |
||
| 107 | * @since 2.8.0 |
||
| 108 | * @access public |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | public $default_dirs; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Constructor. |
||
| 115 | * |
||
| 116 | * @since 2.6.0 |
||
| 117 | * @access public |
||
| 118 | */ |
||
| 119 | public function __construct() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Processes a style dependency. |
||
| 132 | * |
||
| 133 | * @since 2.6.0 |
||
| 134 | * @access public |
||
| 135 | * |
||
| 136 | * @see WP_Dependencies::do_item() |
||
| 137 | * |
||
| 138 | * @param string $handle The style's registered handle. |
||
| 139 | * @return bool True on success, false on failure. |
||
| 140 | */ |
||
| 141 | public function do_item( $handle ) { |
||
| 142 | if ( !parent::do_item($handle) ) |
||
| 143 | return false; |
||
| 144 | |||
| 145 | $obj = $this->registered[$handle]; |
||
| 146 | View Code Duplication | if ( null === $obj->ver ) |
|
| 147 | $ver = ''; |
||
| 148 | else |
||
| 149 | $ver = $obj->ver ? $obj->ver : $this->default_version; |
||
| 150 | |||
| 151 | View Code Duplication | if ( isset($this->args[$handle]) ) |
|
| 152 | $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; |
||
| 153 | |||
| 154 | if ( $this->do_concat ) { |
||
| 155 | if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) { |
||
| 156 | $this->concat .= "$handle,"; |
||
| 157 | $this->concat_version .= "$handle$ver"; |
||
|
|
|||
| 158 | |||
| 159 | $this->print_code .= $this->print_inline_style( $handle, false ); |
||
| 160 | |||
| 161 | return true; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | if ( isset($obj->args) ) |
||
| 166 | $media = esc_attr( $obj->args ); |
||
| 167 | else |
||
| 168 | $media = 'all'; |
||
| 169 | |||
| 170 | // A single item may alias a set of items, by having dependencies, but no source. |
||
| 171 | if ( ! $obj->src ) { |
||
| 172 | if ( $inline_style = $this->print_inline_style( $handle, false ) ) { |
||
| 173 | $inline_style = sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style ); |
||
| 174 | if ( $this->do_concat ) { |
||
| 175 | $this->print_html .= $inline_style; |
||
| 176 | } else { |
||
| 177 | echo $inline_style; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | return true; |
||
| 181 | } |
||
| 182 | |||
| 183 | $href = $this->_css_href( $obj->src, $ver, $handle ); |
||
| 184 | if ( ! $href ) { |
||
| 185 | return true; |
||
| 186 | } |
||
| 187 | |||
| 188 | $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet'; |
||
| 189 | $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : ''; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Filters the HTML link tag of an enqueued style. |
||
| 193 | * |
||
| 194 | * @since 2.6.0 |
||
| 195 | * @since 4.3.0 Introduced the `$href` parameter. |
||
| 196 | * @since 4.5.0 Introduced the `$media` parameter. |
||
| 197 | * |
||
| 198 | * @param string $html The link tag for the enqueued style. |
||
| 199 | * @param string $handle The style's registered handle. |
||
| 200 | * @param string $href The stylesheet's source URL. |
||
| 201 | * @param string $media The stylesheet's media attribute. |
||
| 202 | */ |
||
| 203 | $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle, $href, $media); |
||
| 204 | if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) { |
||
| 205 | if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) { |
||
| 206 | $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : ''; |
||
| 207 | $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" )); |
||
| 208 | } else { |
||
| 209 | $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" ); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** This filter is documented in wp-includes/class.wp-styles.php */ |
||
| 213 | $rtl_tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-rtl-css' $title href='$rtl_href' type='text/css' media='$media' />\n", $handle, $rtl_href, $media ); |
||
| 214 | |||
| 215 | if ( $obj->extra['rtl'] === 'replace' ) { |
||
| 216 | $tag = $rtl_tag; |
||
| 217 | } else { |
||
| 218 | $tag .= $rtl_tag; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | $conditional_pre = $conditional_post = ''; |
||
| 223 | if ( isset( $obj->extra['conditional'] ) && $obj->extra['conditional'] ) { |
||
| 224 | $conditional_pre = "<!--[if {$obj->extra['conditional']}]>\n"; |
||
| 225 | $conditional_post = "<![endif]-->\n"; |
||
| 226 | } |
||
| 227 | |||
| 228 | if ( $this->do_concat ) { |
||
| 229 | $this->print_html .= $conditional_pre; |
||
| 230 | $this->print_html .= $tag; |
||
| 231 | if ( $inline_style = $this->print_inline_style( $handle, false ) ) { |
||
| 232 | $this->print_html .= sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style ); |
||
| 233 | } |
||
| 234 | $this->print_html .= $conditional_post; |
||
| 235 | } else { |
||
| 236 | echo $conditional_pre; |
||
| 237 | echo $tag; |
||
| 238 | $this->print_inline_style( $handle ); |
||
| 239 | echo $conditional_post; |
||
| 240 | } |
||
| 241 | |||
| 242 | return true; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Adds extra CSS styles to a registered stylesheet. |
||
| 247 | * |
||
| 248 | * @since 3.3.0 |
||
| 249 | * @access public |
||
| 250 | * |
||
| 251 | * @param string $handle The style's registered handle. |
||
| 252 | * @param string $code String containing the CSS styles to be added. |
||
| 253 | * @return bool True on success, false on failure. |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function add_inline_style( $handle, $code ) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Prints extra CSS styles of a registered stylesheet. |
||
| 272 | * |
||
| 273 | * @since 3.3.0 |
||
| 274 | * @access public |
||
| 275 | * |
||
| 276 | * @param string $handle The style's registered handle. |
||
| 277 | * @param bool $echo Optional. Whether to echo the inline style instead of just returning it. |
||
| 278 | * Default true. |
||
| 279 | * @return string|bool False if no data exists, inline styles if `$echo` is true, true otherwise. |
||
| 280 | */ |
||
| 281 | View Code Duplication | public function print_inline_style( $handle, $echo = true ) { |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Determines style dependencies. |
||
| 301 | * |
||
| 302 | * @since 2.6.0 |
||
| 303 | * @access public |
||
| 304 | * |
||
| 305 | * @see WP_Dependencies::all_deps() |
||
| 306 | * |
||
| 307 | * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
||
| 308 | * @param bool $recursion Internal flag that function is calling itself. |
||
| 309 | * @param int|false $group Group level: (int) level, (false) no groups. |
||
| 310 | * @return bool True on success, false on failure. |
||
| 311 | */ |
||
| 312 | View Code Duplication | public function all_deps( $handles, $recursion = false, $group = false ) { |
|
| 313 | $r = parent::all_deps( $handles, $recursion, $group ); |
||
| 314 | if ( ! $recursion ) { |
||
| 315 | /** |
||
| 316 | * Filters the array of enqueued styles before processing for output. |
||
| 317 | * |
||
| 318 | * @since 2.6.0 |
||
| 319 | * |
||
| 320 | * @param array $to_do The list of enqueued styles about to be processed. |
||
| 321 | */ |
||
| 322 | $this->to_do = apply_filters( 'print_styles_array', $this->to_do ); |
||
| 323 | } |
||
| 324 | return $r; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Generates an enqueued style's fully-qualified URL. |
||
| 329 | * |
||
| 330 | * @since 2.6.0 |
||
| 331 | * @access public |
||
| 332 | * |
||
| 333 | * @param string $src The source of the enqueued style. |
||
| 334 | * @param string $ver The version of the enqueued style. |
||
| 335 | * @param string $handle The style's registered handle. |
||
| 336 | * @return string Style's fully-qualified URL. |
||
| 337 | */ |
||
| 338 | public function _css_href( $src, $ver, $handle ) { |
||
| 339 | View Code Duplication | if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) { |
|
| 340 | $src = $this->base_url . $src; |
||
| 341 | } |
||
| 342 | |||
| 343 | if ( !empty($ver) ) |
||
| 344 | $src = add_query_arg('ver', $ver, $src); |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Filters an enqueued style's fully-qualified URL. |
||
| 348 | * |
||
| 349 | * @since 2.6.0 |
||
| 350 | * |
||
| 351 | * @param string $src The source URL of the enqueued style. |
||
| 352 | * @param string $handle The style's registered handle. |
||
| 353 | */ |
||
| 354 | $src = apply_filters( 'style_loader_src', $src, $handle ); |
||
| 355 | return esc_url( $src ); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Whether a handle's source is in a default directory. |
||
| 360 | * |
||
| 361 | * @since 2.8.0 |
||
| 362 | * @access public |
||
| 363 | * |
||
| 364 | * @param string $src The source of the enqueued style. |
||
| 365 | * @return bool True if found, false if not. |
||
| 366 | */ |
||
| 367 | public function in_default_dir( $src ) { |
||
| 368 | if ( ! $this->default_dirs ) |
||
| 369 | return true; |
||
| 370 | |||
| 371 | foreach ( (array) $this->default_dirs as $test ) { |
||
| 372 | if ( 0 === strpos($src, $test) ) |
||
| 373 | return true; |
||
| 374 | } |
||
| 375 | return false; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Processes items and dependencies for the footer group. |
||
| 380 | * |
||
| 381 | * HTML 5 allows styles in the body, grab late enqueued items and output them in the footer. |
||
| 382 | * |
||
| 383 | * @since 3.3.0 |
||
| 384 | * @access public |
||
| 385 | * |
||
| 386 | * @see WP_Dependencies::do_items() |
||
| 387 | * |
||
| 388 | * @return array Handles of items that have been processed. |
||
| 389 | */ |
||
| 390 | public function do_footer_items() { |
||
| 391 | $this->do_items(false, 1); |
||
| 392 | return $this->done; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Resets class properties. |
||
| 397 | * |
||
| 398 | * @since 3.3.0 |
||
| 399 | * @access public |
||
| 400 | */ |
||
| 401 | public function reset() { |
||
| 407 | } |
||
| 408 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.