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_Likes 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_Likes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Jetpack_Likes { |
||
| 27 | public static function init() { |
||
| 28 | static $instance = NULL; |
||
| 29 | |||
| 30 | if ( ! $instance ) { |
||
| 31 | $instance = new Jetpack_Likes; |
||
| 32 | } |
||
| 33 | |||
| 34 | return $instance; |
||
| 35 | } |
||
| 36 | |||
| 37 | function __construct() { |
||
| 38 | $this->in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true; |
||
|
|
|||
| 39 | $this->settings = new Jetpack_Likes_Settings(); |
||
| 40 | |||
| 41 | add_action( 'init', array( &$this, 'action_init' ) ); |
||
| 42 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
| 43 | |||
| 44 | if ( $this->in_jetpack ) { |
||
| 45 | add_action( 'jetpack_activate_module_likes', array( $this, 'set_social_notifications_like' ) ); |
||
| 46 | add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) ); |
||
| 47 | |||
| 48 | Jetpack::enable_module_configurable( __FILE__ ); |
||
| 49 | Jetpack::module_configuration_load( __FILE__, array( $this, 'configuration_redirect' ) ); |
||
| 50 | |||
| 51 | add_action( 'admin_print_scripts-settings_page_sharing', array( &$this, 'load_jp_css' ) ); |
||
| 52 | add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) ); |
||
| 53 | |||
| 54 | $active = Jetpack::get_active_modules(); |
||
| 55 | |||
| 56 | View Code Duplication | if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) { |
|
| 57 | // we don't have a sharing page yet |
||
| 58 | add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) ); |
||
| 59 | } |
||
| 60 | |||
| 61 | View Code Duplication | if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) { |
|
| 62 | // we have a sharing page but not the global options area |
||
| 63 | add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 ); |
||
| 64 | add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 ); |
||
| 65 | } |
||
| 66 | |||
| 67 | View Code Duplication | if( ! in_array( 'sharedaddy', $active ) ) { |
|
| 68 | add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) ); |
||
| 69 | add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 ); |
||
| 70 | add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 ); |
||
| 71 | add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) ); |
||
| 72 | } else { |
||
| 73 | add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) ); |
||
| 74 | add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) ); |
||
| 75 | } |
||
| 76 | } else { // wpcom |
||
| 77 | add_action( 'wpmu_new_blog', array( $this, 'enable_comment_likes' ), 10, 1 ); |
||
| 78 | add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) ); |
||
| 79 | add_action( 'end_likes_meta_box_content', array( $this->settings, 'sharing_meta_box_content' ) ); |
||
| 80 | add_filter( 'likes_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) ); |
||
| 81 | } |
||
| 82 | |||
| 83 | add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications |
||
| 84 | |||
| 85 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 ); |
||
| 86 | |||
| 87 | add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) ); |
||
| 88 | |||
| 89 | add_action( 'save_post', array( $this->settings, 'meta_box_save' ) ); |
||
| 90 | add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) ); |
||
| 91 | add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 ); |
||
| 92 | add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_callback' ), 20 ); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set the social_notifications_like option to `on` when the Likes module is activated. |
||
| 97 | * |
||
| 98 | * @since 3.7.0 |
||
| 99 | * |
||
| 100 | * @return null |
||
| 101 | */ |
||
| 102 | function set_social_notifications_like() { |
||
| 103 | update_option( 'social_notifications_like', 'on' ); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Delete the social_notifications_like option that was set to `on` on module activation. |
||
| 108 | * |
||
| 109 | * @since 3.7.0 |
||
| 110 | * |
||
| 111 | * @return null |
||
| 112 | */ |
||
| 113 | function delete_social_notifications_like() { |
||
| 114 | delete_option( 'social_notifications_like' ); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Redirects to the likes section of the sharing page. |
||
| 119 | */ |
||
| 120 | function configuration_redirect() { |
||
| 121 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) ); |
||
| 122 | die(); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable |
||
| 127 | */ |
||
| 128 | function load_jp_css() { |
||
| 129 | // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. |
||
| 130 | // Jetpack::init()->admin_styles(); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Load scripts and styles for front end. |
||
| 135 | * @return null |
||
| 136 | */ |
||
| 137 | function load_styles_register_scripts() { |
||
| 138 | if ( $this->in_jetpack ) { |
||
| 139 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 140 | $this->register_scripts(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box |
||
| 146 | * @param string $html row heading for the sharedaddy "which page" setting |
||
| 147 | * @return string html with the jetpack-targetable class and likes id. tbody gets closed after the like settings |
||
| 148 | */ |
||
| 149 | function configuration_target_area( $html = '' ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog) |
||
| 156 | */ |
||
| 157 | function sharing_meta_box_content( $post ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page) |
||
| 171 | */ |
||
| 172 | |||
| 173 | View Code Duplication | function admin_discussion_likes_settings_init() { |
|
| 180 | |||
| 181 | function admin_discussion_likes_settings_section() { |
||
| 201 | |||
| 202 | function admin_likes_get_option( $option ) { |
||
| 211 | |||
| 212 | function admin_discussion_likes_settings_field() { |
||
| 218 | |||
| 219 | function admin_discussion_likes_settings_validate( $input ) { |
||
| 227 | |||
| 228 | function admin_init() { |
||
| 236 | |||
| 237 | function action_init() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Register scripts |
||
| 267 | */ |
||
| 268 | function register_scripts() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Load the CSS needed for the wp-admin area. |
||
| 276 | */ |
||
| 277 | function load_admin_css() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Load the JS required for loading the like counts. |
||
| 319 | */ |
||
| 320 | function enqueue_admin_scripts() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Add "Likes" column data to the post edit table in wp-admin. |
||
| 335 | * |
||
| 336 | * @param string $column_name |
||
| 337 | * @param int $post_id |
||
| 338 | */ |
||
| 339 | function likes_edit_column( $column_name, $post_id ) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Add a "Likes" column header to the post edit table in wp-admin. |
||
| 358 | * |
||
| 359 | * @param array $columns |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | function add_like_count_column( $columns ) { |
||
| 371 | |||
| 372 | function post_likes( $content ) { |
||
| 417 | |||
| 418 | function post_flair_service_enabled_like( $classes ) { |
||
| 422 | |||
| 423 | function is_admin_bar_button_visible() { |
||
| 449 | |||
| 450 | function admin_bar_likes() { |
||
| 489 | } |
||
| 490 | |||
| 491 | Jetpack_Likes::init(); |
||
| 492 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: