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 Jetpack_Custom_CSS_Enhancements 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 Jetpack_Custom_CSS_Enhancements, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Jetpack_Custom_CSS_Enhancements { |
||
| 14 | /** |
||
| 15 | * Set up the actions and filters needed for our compatability layer on top of core's Custom CSS implementation. |
||
| 16 | */ |
||
| 17 | public static function add_hooks() { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Things that we do on init. |
||
| 54 | */ |
||
| 55 | public static function init() { |
||
| 56 | $min = '.min'; |
||
|
|
|||
| 57 | if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { |
||
| 58 | $min = ''; |
||
| 59 | } |
||
| 60 | |||
| 61 | wp_register_style( 'jetpack-codemirror', plugins_url( 'custom-css/css/codemirror.css', __FILE__ ), array(), '20120905' ); |
||
| 62 | wp_register_style( 'jetpack-customizer-css', plugins_url( 'custom-css/css/customizer-control.css', __FILE__ ), array( 'jetpack-codemirror' ), '20140728' ); |
||
| 63 | wp_register_script( 'jetpack-codemirror', plugins_url( 'custom-css/js/codemirror.min.js', __FILE__ ), array(), '3.16', true ); |
||
| 64 | wp_register_script( 'jetpack-customizer-css', plugins_url( 'custom-css/js/core-customizer-css.js', __FILE__ ), array( 'customize-controls', 'underscore', 'jetpack-codemirror' ), JETPACK__VERSION, true ); |
||
| 65 | |||
| 66 | wp_register_script( 'jetpack-customizer-css-preview', plugins_url( 'custom-css/js/core-customizer-css-preview.js', __FILE__ ), array( 'customize-selective-refresh' ), JETPACK__VERSION, true ); |
||
| 67 | |||
| 68 | remove_action( 'wp_head', 'wp_custom_css_cb', 11 ); // 4.7.0 had it at 11, 4.7.1 moved it to 101. |
||
| 69 | remove_action( 'wp_head', 'wp_custom_css_cb', 101 ); |
||
| 70 | add_action( 'wp_head', array( __CLASS__, 'wp_custom_css_cb' ), 101 ); |
||
| 71 | |||
| 72 | if ( isset( $_GET['custom-css'] ) ) { |
||
| 73 | self::print_linked_custom_css(); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Things that we do on init when the Customize Preview is loading. |
||
| 79 | */ |
||
| 80 | public static function customize_preview_init() { |
||
| 81 | add_filter( 'wp_get_custom_css', array( __CLASS__, 'customize_preview_wp_get_custom_css' ) ); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Print the current Custom CSS. This is for linking instead of printing directly. |
||
| 86 | */ |
||
| 87 | public static function print_linked_custom_css() { |
||
| 88 | header( 'Content-type: text/css' ); |
||
| 89 | header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + YEAR_IN_SECONDS ) . ' GMT' ); |
||
| 90 | echo wp_get_custom_css(); |
||
| 91 | exit; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Re-map the Edit CSS capability. |
||
| 96 | * |
||
| 97 | * Core, by default, restricts this to users that have `unfiltered_html` which |
||
| 98 | * would make the feature unusable in multi-site by non-super-admins, due to Core |
||
| 99 | * not shipping any solid sanitization. |
||
| 100 | * |
||
| 101 | * We're expanding who can use it, and then conditionally applying CSSTidy |
||
| 102 | * sanitization to users that do not have the `unfiltered_html` capability. |
||
| 103 | * |
||
| 104 | * @param array $caps Returns the user's actual capabilities. |
||
| 105 | * @param string $cap Capability name. |
||
| 106 | * |
||
| 107 | * @return array $caps |
||
| 108 | */ |
||
| 109 | public static function map_meta_cap( $caps, $cap ) { |
||
| 110 | if ( 'edit_css' === $cap ) { |
||
| 111 | $caps = array( 'edit_theme_options' ); |
||
| 112 | } |
||
| 113 | return $caps; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Handle our admin menu item and legacy page declaration. |
||
| 118 | */ |
||
| 119 | public static function admin_menu() { |
||
| 120 | // Add in our legacy page to support old bookmarks and such. |
||
| 121 | add_submenu_page( null, __( 'CSS', 'jetpack' ), __( 'Edit CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'admin_page' ) ); |
||
| 122 | |||
| 123 | // Add in our new page slug that will redirect to the customizer. |
||
| 124 | $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Edit CSS', 'jetpack' ), 'edit_theme_options', 'editcss-customizer-redirect', array( __CLASS__, 'admin_page' ) ); |
||
| 125 | add_action( "load-{$hook}", array( __CLASS__, 'customizer_redirect' ) ); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Handle the redirect for the customizer. This is necessary because |
||
| 130 | * we can't directly add customizer links to the admin menu. |
||
| 131 | * |
||
| 132 | * There is a core patch in trac that would make this unnecessary. |
||
| 133 | * |
||
| 134 | * @link https://core.trac.wordpress.org/ticket/39050 |
||
| 135 | */ |
||
| 136 | public static function customizer_redirect() { |
||
| 137 | wp_safe_redirect( self::customizer_link( array( |
||
| 138 | 'return_url' => wp_get_referer(), |
||
| 139 | ) ) ); |
||
| 140 | exit; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Shows Preprocessor code in the Revisions screen, and ensures that post_content_filtered |
||
| 145 | * is maintained on revisions |
||
| 146 | * |
||
| 147 | * @param array $fields Post fields pertinent to revisions. |
||
| 148 | * @param array $post A post array being processed for insertion as a post revision. |
||
| 149 | * |
||
| 150 | * @return array $fields Modified array to include post_content_filtered. |
||
| 151 | */ |
||
| 152 | public static function _wp_post_revision_fields( $fields, $post ) { |
||
| 153 | // None of the fields in $post are required to be passed in this filter. |
||
| 154 | if ( ! isset( $post['post_type'], $post['ID'] ) ) { |
||
| 155 | return $fields; |
||
| 156 | } |
||
| 157 | |||
| 158 | // If we're passed in a revision, go get the main post instead. |
||
| 159 | if ( 'revision' === $post['post_type'] ) { |
||
| 160 | $main_post_id = wp_is_post_revision( $post['ID'] ); |
||
| 161 | $post = get_post( $main_post_id, ARRAY_A ); |
||
| 162 | } |
||
| 163 | if ( 'custom_css' === $post['post_type'] ) { |
||
| 164 | $fields['post_content'] = __( 'CSS', 'jetpack' ); |
||
| 165 | $fields['post_content_filtered'] = __( 'Preprocessor', 'jetpack' ); |
||
| 166 | } |
||
| 167 | return $fields; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get the published custom CSS post. |
||
| 172 | * |
||
| 173 | * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme. |
||
| 174 | * @return WP_Post|null |
||
| 175 | */ |
||
| 176 | public static function get_css_post( $stylesheet = '' ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Override Core's `wp_custom_css_cb` method to provide linking to custom css. |
||
| 182 | */ |
||
| 183 | public static function wp_custom_css_cb() { |
||
| 184 | $styles = wp_get_custom_css(); |
||
| 185 | if ( strlen( $styles ) > 2000 && ! is_customize_preview() ) : |
||
| 186 | // Add a cache buster to the url. |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the ID of a Custom CSS post tying to a given stylesheet. |
||
| 200 | * |
||
| 201 | * @param string $stylesheet Stylesheet name. |
||
| 202 | * |
||
| 203 | * @return int $post_id Post ID. |
||
| 204 | */ |
||
| 205 | public static function post_id( $stylesheet = '' ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Partial for use in the Customizer. |
||
| 215 | */ |
||
| 216 | public static function echo_custom_css_partial() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Admin page! |
||
| 222 | * |
||
| 223 | * This currently has two main uses -- firstly to display the css for an inactive |
||
| 224 | * theme if there are no revisions attached it to a legacy bug, and secondly to |
||
| 225 | * handle folks that have bookmarkes in their browser going to the old page for |
||
| 226 | * managing Custom CSS in Jetpack. |
||
| 227 | * |
||
| 228 | * If we ever add back in a non-Customizer CSS editor, this would be the place. |
||
| 229 | */ |
||
| 230 | public static function admin_page() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Build the URL to deep link to the Customizer. |
||
| 317 | * |
||
| 318 | * You can modify the return url via $args. |
||
| 319 | * |
||
| 320 | * @param array $args Array of parameters. |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public static function customizer_link( $args = array() ) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Handle the enqueueing and localizing for scripts to be used in the Customizer. |
||
| 343 | */ |
||
| 344 | public static function customize_controls_enqueue_scripts() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Check whether there are CSS Revisions for a given theme. |
||
| 375 | * |
||
| 376 | * Going forward, there should always be, but this was necessitated |
||
| 377 | * early on by https://core.trac.wordpress.org/ticket/30854 |
||
| 378 | * |
||
| 379 | * @param string $stylesheet Stylesheet name. |
||
| 380 | * |
||
| 381 | * @return bool|null|WP_Post |
||
| 382 | */ |
||
| 383 | public static function are_there_css_revisions( $stylesheet = '' ) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Core doesn't have a function to get the revisions url for a given post ID. |
||
| 393 | * |
||
| 394 | * @param string $stylesheet Stylesheet name. |
||
| 395 | * |
||
| 396 | * @return null|string|void |
||
| 397 | */ |
||
| 398 | public static function get_revisions_url( $stylesheet = '' ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get a map of all theme names and theme stylesheets for mapping stuff. |
||
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | public static function get_themes() { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * When we need to get all themes that have Custom CSS saved. |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | public static function get_all_themes_with_custom_css() { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Handle the enqueueing of scripts for customize previews. |
||
| 463 | */ |
||
| 464 | public static function wp_enqueue_scripts() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Sanitize the CSS for users without `unfiltered_html`. |
||
| 476 | * |
||
| 477 | * @param string $css Input CSS. |
||
| 478 | * @param array $args Array of CSS options. |
||
| 479 | * |
||
| 480 | * @return mixed|string |
||
| 481 | */ |
||
| 482 | public static function sanitize_css( $css, $args = array() ) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Override $content_width in customizer previews. |
||
| 554 | */ |
||
| 555 | public static function preview_content_width() { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Filter the current theme's stylesheet for potentially nullifying it. |
||
| 574 | * |
||
| 575 | * @param string $current Stylesheet URI for the current theme/child theme. |
||
| 576 | * |
||
| 577 | * @return mixed|void |
||
| 578 | */ |
||
| 579 | static function style_filter( $current ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Determine whether or not we should have the theme skip its main stylesheet. |
||
| 594 | * |
||
| 595 | * @return mixed The truthiness of this value determines whether the stylesheet should be skipped. |
||
| 596 | */ |
||
| 597 | static function skip_stylesheet() { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Override $content_width in customizer previews. |
||
| 614 | * |
||
| 615 | * Runs on `safecss_skip_stylesheet` filter. |
||
| 616 | * |
||
| 617 | * @param bool $skip_value Should the stylesheet be skipped. |
||
| 618 | * |
||
| 619 | * @return null|bool |
||
| 620 | */ |
||
| 621 | public static function preview_skip_stylesheet( $skip_value ) { |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Add Custom CSS section and controls. |
||
| 642 | * |
||
| 643 | * @param WP_Customize_Manager $wp_customize WP_Customize_Manager instance. |
||
| 644 | */ |
||
| 645 | public static function customize_register( $wp_customize ) { |
||
| 745 | |||
| 746 | /** |
||
| 747 | * The callback to handle sanitizing the CSS. Takes different arguments, hence the proxy function. |
||
| 748 | * |
||
| 749 | * @param mixed $css Value of the setting. |
||
| 750 | * @param WP_Customize_Setting $setting WP_Customize_Setting instance. |
||
| 751 | * |
||
| 752 | * @return mixed|string |
||
| 753 | */ |
||
| 754 | public static function sanitize_css_callback( $css, $setting ) { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Flesh out for wpcom. |
||
| 763 | * |
||
| 764 | * @todo |
||
| 765 | * |
||
| 766 | * @return bool |
||
| 767 | */ |
||
| 768 | public static function is_freetrial() { |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Flesh out for wpcom. |
||
| 774 | * |
||
| 775 | * @todo |
||
| 776 | * |
||
| 777 | * @return bool |
||
| 778 | */ |
||
| 779 | public static function is_preview() { |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Output the custom css for customize preview. |
||
| 785 | * |
||
| 786 | * @param string $css Custom CSS content. |
||
| 787 | * |
||
| 788 | * @return mixed |
||
| 789 | */ |
||
| 790 | public static function customize_preview_wp_get_custom_css( $css ) { |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Add CSS preprocessing to our CSS if it is supported. |
||
| 811 | * |
||
| 812 | * @param mixed $css Value of the setting. |
||
| 813 | * @param WP_Customize_Setting $setting WP_Customize_Setting instance. |
||
| 814 | * |
||
| 815 | * @return string |
||
| 816 | */ |
||
| 817 | public static function customize_value_custom_css( $css, $setting ) { |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Store the original pre-processed CSS in `post_content_filtered` |
||
| 842 | * and then store processed CSS in `post_content`. |
||
| 843 | * |
||
| 844 | * @param array $args Content post args. |
||
| 845 | * @param string $css Original CSS being updated. |
||
| 846 | * @param WP_Customize_Custom_CSS_Setting $setting Custom CSS Setting. |
||
| 847 | * |
||
| 848 | * @return mixed |
||
| 849 | */ |
||
| 850 | public static function customize_update_custom_css_post_content_args( $args, $css, $setting ) { |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Filter to handle the processing of preprocessed css on save. |
||
| 876 | * |
||
| 877 | * @param array $args Custom CSS options. |
||
| 878 | * @param string $stylesheet Original CSS to be updated. |
||
| 879 | * |
||
| 880 | * @return mixed |
||
| 881 | */ |
||
| 882 | public static function update_custom_css_data( $args, $stylesheet ) { |
||
| 906 | |||
| 907 | /** |
||
| 908 | * When on the edit screen, make sure the custom content width |
||
| 909 | * setting is applied to the large image size. |
||
| 910 | * |
||
| 911 | * @param array $dims Array of image dimensions (width and height). |
||
| 912 | * @param string $size Size of the resulting image. |
||
| 913 | * @param null $context Context the image is being resized for. `edit` or `display`. |
||
| 914 | * |
||
| 915 | * @return array |
||
| 916 | */ |
||
| 917 | View Code Duplication | static function editor_max_image_size( $dims, $size = 'medium', $context = null ) { |
|
| 926 | |||
| 927 | /** |
||
| 928 | * Override the content_width with a custom value if one is set. |
||
| 929 | * |
||
| 930 | * @param int $content_width Content Width value to be updated. |
||
| 931 | * |
||
| 932 | * @return int |
||
| 933 | */ |
||
| 934 | static function jetpack_content_width( $content_width ) { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Currently this filter function gets called on |
||
| 951 | * 'template_redirect' action and |
||
| 952 | * 'admin_init' action |
||
| 953 | */ |
||
| 954 | View Code Duplication | static function set_content_width() { |
|
| 962 | |||
| 963 | /** |
||
| 964 | * Make sure the preprocessor we're saving is one we know about. |
||
| 965 | * |
||
| 966 | * @param string $preprocessor The preprocessor to sanitize. |
||
| 967 | * |
||
| 968 | * @return null|string |
||
| 969 | */ |
||
| 970 | public static function sanitize_preprocessor( $preprocessor ) { |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Get the base10 intval. |
||
| 981 | * |
||
| 982 | * This is used as a setting's sanitize_callback; we can't use just plain |
||
| 983 | * intval because the second argument is not what intval() expects. |
||
| 984 | * |
||
| 985 | * @access public |
||
| 986 | * |
||
| 987 | * @param mixed $value Number to convert. |
||
| 988 | * @return int Integer. |
||
| 989 | */ |
||
| 990 | public static function intval_base10( $value ) { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Add a footer action on revision.php to print some customizations for the theme switcher. |
||
| 996 | */ |
||
| 997 | public static function load_revision_php() { |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Print the theme switcher on revision.php and move it into place. |
||
| 1003 | */ |
||
| 1004 | public static function revision_admin_footer() { |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * The HTML for the theme revision switcher box. |
||
| 1066 | * |
||
| 1067 | * @param string $stylesheet Stylesheet name. |
||
| 1068 | */ |
||
| 1069 | public static function revisions_switcher_box( $stylesheet = '' ) { |
||
| 1100 | } |
||
| 1101 | |||
| 1145 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.