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 A8C_WPCOM_Masterbar 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 A8C_WPCOM_Masterbar, and based on these observations, apply Extract Interface, too.
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName  | 
            ||
| 10 | class A8C_WPCOM_Masterbar { | 
            ||
| 11 | /**  | 
            ||
| 12 | * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.  | 
            ||
| 13 | * If not set it will default to loading the ones from WordPress.com.  | 
            ||
| 14 | *  | 
            ||
| 15 | * @var string $sandbox_url  | 
            ||
| 16 | */  | 
            ||
| 17 | private $sandbox_url = '';  | 
            ||
| 18 | |||
| 19 | /**  | 
            ||
| 20 | * Current locale.  | 
            ||
| 21 | *  | 
            ||
| 22 | * @var string  | 
            ||
| 23 | */  | 
            ||
| 24 | private $locale;  | 
            ||
| 25 | |||
| 26 | /**  | 
            ||
| 27 | * Current User ID.  | 
            ||
| 28 | *  | 
            ||
| 29 | * @var int  | 
            ||
| 30 | */  | 
            ||
| 31 | private $user_id;  | 
            ||
| 32 | /**  | 
            ||
| 33 | * WordPress.com user data of the connected user.  | 
            ||
| 34 | *  | 
            ||
| 35 | * @var array  | 
            ||
| 36 | */  | 
            ||
| 37 | private $user_data;  | 
            ||
| 38 | /**  | 
            ||
| 39 | * WordPress.com email address for the connected user.  | 
            ||
| 40 | *  | 
            ||
| 41 | * @var string  | 
            ||
| 42 | */  | 
            ||
| 43 | private $user_email;  | 
            ||
| 44 | /**  | 
            ||
| 45 | * Site URL sanitized for usage in WordPress.com slugs.  | 
            ||
| 46 | *  | 
            ||
| 47 | * @var string  | 
            ||
| 48 | */  | 
            ||
| 49 | private $primary_site_slug;  | 
            ||
| 50 | /**  | 
            ||
| 51 | * Text direction (ltr or rtl) based on connected WordPress.com user's interface settings.  | 
            ||
| 52 | *  | 
            ||
| 53 | * @var string  | 
            ||
| 54 | */  | 
            ||
| 55 | private $user_text_direction;  | 
            ||
| 56 | /**  | 
            ||
| 57 | * Number of sites owned by connected WordPress.com user.  | 
            ||
| 58 | *  | 
            ||
| 59 | * @var int  | 
            ||
| 60 | */  | 
            ||
| 61 | private $user_site_count;  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * Constructor  | 
            ||
| 65 | */  | 
            ||
| 66 | 	public function __construct() { | 
            ||
| 67 | add_action( 'admin_bar_init', array( $this, 'init' ) );  | 
            ||
| 68 | }  | 
            ||
| 69 | |||
| 70 | /**  | 
            ||
| 71 | * Initialize our masterbar.  | 
            ||
| 72 | */  | 
            ||
| 73 | 	public function init() { | 
            ||
| 74 | $this->locale = $this->get_locale();  | 
            ||
| 75 | $this->user_id = get_current_user_id();  | 
            ||
| 76 | |||
| 77 | // Limit the masterbar to be shown only to connected Jetpack users.  | 
            ||
| 78 | 		if ( ! Jetpack::is_user_connected( $this->user_id ) ) { | 
            ||
| 79 | return;  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | // Don't show the masterbar on WordPress mobile apps.  | 
            ||
| 83 | 		if ( Jetpack_User_Agent_Info::is_mobile_app() ) { | 
            ||
| 84 | add_filter( 'show_admin_bar', '__return_false' );  | 
            ||
| 85 | return;  | 
            ||
| 86 | }  | 
            ||
| 87 | |||
| 88 | // Disable the Masterbar on AMP views.  | 
            ||
| 89 | if (  | 
            ||
| 90 | class_exists( 'Jetpack_AMP_Support' )  | 
            ||
| 91 | && Jetpack_AMP_Support::is_amp_request()  | 
            ||
| 92 | 		) { | 
            ||
| 93 | return;  | 
            ||
| 94 | }  | 
            ||
| 95 | |||
| 96 | Jetpack::dns_prefetch(  | 
            ||
| 97 | array(  | 
            ||
| 98 | '//s0.wp.com',  | 
            ||
| 99 | '//s1.wp.com',  | 
            ||
| 100 | '//s2.wp.com',  | 
            ||
| 101 | '//0.gravatar.com',  | 
            ||
| 102 | '//1.gravatar.com',  | 
            ||
| 103 | '//2.gravatar.com',  | 
            ||
| 104 | )  | 
            ||
| 105 | );  | 
            ||
| 106 | |||
| 107 | // Atomic only.  | 
            ||
| 108 | 		if ( jetpack_is_atomic_site() ) { | 
            ||
| 109 | /*  | 
            ||
| 110 | * override user setting that hides masterbar from site's front.  | 
            ||
| 111 | * https://github.com/Automattic/jetpack/issues/7667  | 
            ||
| 112 | */  | 
            ||
| 113 | add_filter( 'show_admin_bar', '__return_true' );  | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 | $this->user_data = Jetpack::get_connected_user_data( $this->user_id );  | 
            ||
| 117 | $this->user_email = $this->user_data['email'];  | 
            ||
| 118 | $this->user_site_count = $this->user_data['site_count'];  | 
            ||
| 119 | |||
| 120 | // Used to build menu links that point directly to Calypso.  | 
            ||
| 121 | $this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() );  | 
            ||
| 122 | |||
| 123 | // We need to use user's setting here, instead of relying on current blog's text direction.  | 
            ||
| 124 | $this->user_text_direction = $this->user_data['text_direction'];  | 
            ||
| 125 | |||
| 126 | 		if ( $this->is_rtl() ) { | 
            ||
| 127 | // Extend core WP_Admin_Bar class in order to add rtl styles.  | 
            ||
| 128 | add_filter( 'wp_admin_bar_class', array( $this, 'get_rtl_admin_bar_class' ) );  | 
            ||
| 129 | }  | 
            ||
| 130 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );  | 
            ||
| 131 | |||
| 132 | add_action( 'wp_before_admin_bar_render', array( $this, 'replace_core_masterbar' ), 99999 );  | 
            ||
| 133 | |||
| 134 | add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );  | 
            ||
| 135 | add_action( 'admin_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );  | 
            ||
| 136 | |||
| 137 | add_action( 'wp_enqueue_scripts', array( $this, 'remove_core_styles' ) );  | 
            ||
| 138 | add_action( 'admin_enqueue_scripts', array( $this, 'remove_core_styles' ) );  | 
            ||
| 139 | |||
| 140 | 		if ( Jetpack::is_module_active( 'notes' ) && $this->is_rtl() ) { | 
            ||
| 141 | // Override Notification module to include RTL styles.  | 
            ||
| 142 | add_action( 'a8c_wpcom_masterbar_enqueue_rtl_notification_styles', '__return_true' );  | 
            ||
| 143 | }  | 
            ||
| 144 | }  | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * Get class name for RTL sites.  | 
            ||
| 148 | */  | 
            ||
| 149 | 	public function get_rtl_admin_bar_class() { | 
            ||
| 150 | return 'RTL_Admin_Bar';  | 
            ||
| 151 | }  | 
            ||
| 152 | |||
| 153 | /**  | 
            ||
| 154 | * Adds CSS classes to admin body tag.  | 
            ||
| 155 | *  | 
            ||
| 156 | * @since 5.1  | 
            ||
| 157 | *  | 
            ||
| 158 | * @param string $admin_body_classes CSS classes that will be added.  | 
            ||
| 159 | *  | 
            ||
| 160 | * @return string  | 
            ||
| 161 | */  | 
            ||
| 162 | 	public function admin_body_class( $admin_body_classes ) { | 
            ||
| 163 | return "$admin_body_classes jetpack-masterbar";  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Remove the default Admin Bar CSS.  | 
            ||
| 168 | */  | 
            ||
| 169 | 	public function remove_core_styles() { | 
            ||
| 170 | wp_dequeue_style( 'admin-bar' );  | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 173 | /**  | 
            ||
| 174 | * Check if the user settings are for an RTL language or not.  | 
            ||
| 175 | */  | 
            ||
| 176 | 	public function is_rtl() { | 
            ||
| 177 | return 'rtl' === $this->user_text_direction ? true : false;  | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | /**  | 
            ||
| 181 | * Enqueue our own CSS and JS to display our custom admin bar.  | 
            ||
| 182 | */  | 
            ||
| 183 | 	public function add_styles_and_scripts() { | 
            ||
| 184 | |||
| 185 | 		if ( $this->is_rtl() ) { | 
            ||
| 186 | wp_enqueue_style( 'a8c-wpcom-masterbar-rtl', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/rtl/wpcom-admin-bar-rtl.css' ), array(), JETPACK__VERSION );  | 
            ||
| 187 | wp_enqueue_style( 'a8c-wpcom-masterbar-overrides-rtl', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/rtl/masterbar-rtl.css' ), array(), JETPACK__VERSION );  | 
            ||
| 188 | 		} else { | 
            ||
| 189 | wp_enqueue_style( 'a8c-wpcom-masterbar', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/wpcom-admin-bar.css' ), array(), JETPACK__VERSION );  | 
            ||
| 190 | wp_enqueue_style( 'a8c-wpcom-masterbar-overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.css' ), array(), JETPACK__VERSION );  | 
            ||
| 191 | }  | 
            ||
| 192 | |||
| 193 | 		if ( ! Jetpack::is_module_active( 'notes ' ) ) { | 
            ||
| 194 | // Masterbar is relying on some icons from noticons.css.  | 
            ||
| 195 | wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK__VERSION . '-' . gmdate( 'oW' ) );  | 
            ||
| 196 | }  | 
            ||
| 197 | |||
| 198 | wp_enqueue_script(  | 
            ||
| 199 | 'jetpack-accessible-focus',  | 
            ||
| 200 | Assets::get_file_url_for_environment( '_inc/build/accessible-focus.min.js', '_inc/accessible-focus.js' ),  | 
            ||
| 201 | array(),  | 
            ||
| 202 | JETPACK__VERSION,  | 
            ||
| 203 | false  | 
            ||
| 204 | );  | 
            ||
| 205 | wp_enqueue_script(  | 
            ||
| 206 | 'a8c_wpcom_masterbar_tracks_events',  | 
            ||
| 207 | Assets::get_file_url_for_environment(  | 
            ||
| 208 | '_inc/build/masterbar/tracks-events.min.js',  | 
            ||
| 209 | 'modules/masterbar/tracks-events.js'  | 
            ||
| 210 | ),  | 
            ||
| 211 | array( 'jquery' ),  | 
            ||
| 212 | JETPACK__VERSION,  | 
            ||
| 213 | false  | 
            ||
| 214 | );  | 
            ||
| 215 | }  | 
            ||
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * Get base URL where our CSS and JS will come from.  | 
            ||
| 219 | *  | 
            ||
| 220 | * @param string $file File path for a static resource.  | 
            ||
| 221 | */  | 
            ||
| 222 | 	private function wpcom_static_url( $file ) { | 
            ||
| 223 | 		if ( ! empty( $this->sandbox_url ) ) { | 
            ||
| 224 | // For testing undeployed changes to remotely enqueued scripts and styles.  | 
            ||
| 225 | return set_url_scheme( $this->sandbox_url . $file, 'https' );  | 
            ||
| 226 | }  | 
            ||
| 227 | |||
| 228 | $i = hexdec( substr( md5( $file ), - 1 ) ) % 2;  | 
            ||
| 229 | $url = 'https://s' . $i . '.wp.com' . $file;  | 
            ||
| 230 | |||
| 231 | return set_url_scheme( $url, 'https' );  | 
            ||
| 232 | }  | 
            ||
| 233 | |||
| 234 | /**  | 
            ||
| 235 | * Remove the default admin bar items and replace it with our own admin bar.  | 
            ||
| 236 | */  | 
            ||
| 237 | 	public function replace_core_masterbar() { | 
            ||
| 238 | global $wp_admin_bar;  | 
            ||
| 239 | |||
| 240 | 		if ( ! is_object( $wp_admin_bar ) ) { | 
            ||
| 241 | return false;  | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 | $this->clear_core_masterbar( $wp_admin_bar );  | 
            ||
| 245 | $this->build_wpcom_masterbar( $wp_admin_bar );  | 
            ||
| 246 | }  | 
            ||
| 247 | |||
| 248 | /**  | 
            ||
| 249 | * Remove all existing toolbar entries from core Masterbar  | 
            ||
| 250 | *  | 
            ||
| 251 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.  | 
            ||
| 252 | */  | 
            ||
| 253 | 	public function clear_core_masterbar( $wp_admin_bar ) { | 
            ||
| 254 | 		foreach ( $wp_admin_bar->get_nodes() as $node ) { | 
            ||
| 255 | $wp_admin_bar->remove_node( $node->id );  | 
            ||
| 256 | }  | 
            ||
| 257 | }  | 
            ||
| 258 | |||
| 259 | /**  | 
            ||
| 260 | * Add entries corresponding to WordPress.com Masterbar  | 
            ||
| 261 | *  | 
            ||
| 262 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.  | 
            ||
| 263 | */  | 
            ||
| 264 | 	public function build_wpcom_masterbar( $wp_admin_bar ) { | 
            ||
| 265 | // Menu groups.  | 
            ||
| 266 | $this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar );  | 
            ||
| 267 | |||
| 268 | // Left part.  | 
            ||
| 269 | $this->add_my_sites_submenu( $wp_admin_bar );  | 
            ||
| 270 | $this->add_reader_submenu( $wp_admin_bar );  | 
            ||
| 271 | |||
| 272 | // Right part.  | 
            ||
| 273 | 		if ( Jetpack::is_module_active( 'notes' ) ) { | 
            ||
| 274 | $this->add_notifications( $wp_admin_bar );  | 
            ||
| 275 | }  | 
            ||
| 276 | |||
| 277 | $this->add_me_submenu( $wp_admin_bar );  | 
            ||
| 278 | $this->add_write_button( $wp_admin_bar );  | 
            ||
| 279 | |||
| 280 | // Recovery mode exit.  | 
            ||
| 281 | 		if ( function_exists( 'wp_admin_bar_recovery_mode_menu' ) ) { | 
            ||
| 282 | wp_admin_bar_recovery_mode_menu( $wp_admin_bar );  | 
            ||
| 283 | }  | 
            ||
| 284 | }  | 
            ||
| 285 | |||
| 286 | /**  | 
            ||
| 287 | * Get WordPress.com current locale name.  | 
            ||
| 288 | */  | 
            ||
| 289 | 	public function get_locale() { | 
            ||
| 307 | |||
| 308 | /**  | 
            ||
| 309 | * Add the Notifications menu item.  | 
            ||
| 310 | *  | 
            ||
| 311 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.  | 
            ||
| 312 | */  | 
            ||
| 313 | 	public function add_notifications( $wp_admin_bar ) { | 
            ||
| 336 | |||
| 337 | /**  | 
            ||
| 338 | * Add the "Reader" menu item in the root default group.  | 
            ||
| 339 | *  | 
            ||
| 340 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.  | 
            ||
| 341 | */  | 
            ||
| 342 | 	public function add_reader_submenu( $wp_admin_bar ) { | 
            ||
| 343 | $wp_admin_bar->add_menu(  | 
            ||
| 344 | array(  | 
            ||
| 345 | 'parent' => 'root-default',  | 
            ||
| 346 | 'id' => 'newdash',  | 
            ||
| 347 | 'title' => esc_html__( 'Reader', 'jetpack' ),  | 
            ||
| 355 | |||
| 356 | /**  | 
            ||
| 357 | * Add Secondary groups for submenu items.  | 
            ||
| 358 | *  | 
            ||
| 359 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.  | 
            ||
| 360 | */  | 
            ||
| 361 | 	public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) { | 
            ||
| 390 | |||
| 391 | /**  | 
            ||
| 392 | * Add User info menu item.  | 
            ||
| 393 | *  | 
            ||
| 394 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.  | 
            ||
| 395 | */  | 
            ||
| 396 | 	public function add_me_submenu( $wp_admin_bar ) { | 
            ||
| 418 | |||
| 419 | /**  | 
            ||
| 420 | * Add Write Menu item.  | 
            ||
| 421 | *  | 
            ||
| 422 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.  | 
            ||
| 423 | */  | 
            ||
| 424 | 	public function add_write_button( $wp_admin_bar ) { | 
            ||
| 450 | |||
| 451 | /**  | 
            ||
| 452 | * Add the "My Site" menu item in the root default group.  | 
            ||
| 453 | *  | 
            ||
| 454 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.  | 
            ||
| 455 | */  | 
            ||
| 456 | 	public function add_my_sites_submenu( $wp_admin_bar ) { | 
            ||
| 478 | }  | 
            ||
| 479 |