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 |
||
| 5 | class A8C_WPCOM_Masterbar { |
||
| 6 | /** |
||
| 7 | * Use for testing changes made to remotely enqueued scripts and styles on your sandbox. |
||
| 8 | * If not set it will default to loading the ones from WordPress.com. |
||
| 9 | * |
||
| 10 | * @var string $sandbox_url |
||
| 11 | */ |
||
| 12 | private $sandbox_url = ''; |
||
| 13 | |||
| 14 | private $locale; |
||
| 15 | |||
| 16 | private $user_id; |
||
| 17 | private $user_data; |
||
| 18 | private $user_login; |
||
| 19 | private $user_email; |
||
| 20 | private $display_name; |
||
| 21 | private $primary_site_slug; |
||
| 22 | private $user_text_direction; |
||
| 23 | private $user_site_count; |
||
| 24 | |||
| 25 | function __construct() { |
||
| 76 | |||
| 77 | public function maybe_logout_user_from_wpcom() { |
||
| 82 | |||
| 83 | public function get_rtl_admin_bar_class() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Adds CSS classes to admin body tag. |
||
| 89 | * |
||
| 90 | * @since 5.1 |
||
| 91 | * |
||
| 92 | * @param string $admin_body_classes CSS classes that will be added. |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function admin_body_class( $admin_body_classes ) { |
||
| 99 | |||
| 100 | public function remove_core_styles() { |
||
| 103 | |||
| 104 | public function is_rtl() { |
||
| 107 | |||
| 108 | public function add_styles_and_scripts() { |
||
| 109 | |||
| 110 | if ( $this->is_rtl() ) { |
||
| 111 | 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 ); |
||
| 112 | 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 ); |
||
| 113 | } else { |
||
| 114 | wp_enqueue_style( 'a8c-wpcom-masterbar', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/wpcom-admin-bar.css' ), array(), JETPACK__VERSION ); |
||
| 115 | wp_enqueue_style( 'a8c-wpcom-masterbar-overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.css' ), array(), JETPACK__VERSION ); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Local overrides |
||
| 119 | wp_enqueue_style( 'a8c_wpcom_css_override', plugins_url( 'overrides.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 120 | |||
| 121 | if ( ! Jetpack::is_module_active( 'notes ' ) ) { |
||
| 122 | // Masterbar is relying on some icons from noticons.css |
||
| 123 | wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK__VERSION . '-' . gmdate( 'oW' ) ); |
||
| 124 | } |
||
| 125 | |||
| 126 | wp_enqueue_script( |
||
| 127 | 'jetpack-accessible-focus', |
||
| 128 | Jetpack::get_file_url_for_environment( '_inc/build/accessible-focus.min.js', '_inc/accessible-focus.js' ), |
||
| 129 | array(), |
||
| 130 | JETPACK__VERSION |
||
| 131 | ); |
||
| 132 | wp_enqueue_script( 'a8c_wpcom_masterbar_tracks_events', plugins_url( 'tracks-events.js', __FILE__ ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 133 | |||
| 134 | wp_enqueue_script( 'a8c_wpcom_masterbar_overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.js' ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 135 | } |
||
| 136 | |||
| 137 | function wpcom_static_url( $file ) { |
||
| 138 | if ( ! empty( $this->sandbox_url ) ) { |
||
| 139 | // For testing undeployed changes to remotely enqueued scripts and styles. |
||
| 140 | return set_url_scheme( $this->sandbox_url . $file, 'https'); |
||
| 141 | } |
||
| 142 | |||
| 143 | $i = hexdec( substr( md5( $file ), - 1 ) ) % 2; |
||
| 144 | $url = 'https://s' . $i . '.wp.com' . $file; |
||
| 145 | |||
| 146 | return set_url_scheme( $url, 'https'); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function replace_core_masterbar() { |
||
| 150 | global $wp_admin_bar; |
||
| 151 | |||
| 152 | if ( ! is_object( $wp_admin_bar ) ) { |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->clear_core_masterbar( $wp_admin_bar ); |
||
| 157 | $this->build_wpcom_masterbar( $wp_admin_bar ); |
||
| 158 | } |
||
| 159 | |||
| 160 | // Remove all existing toolbar entries from core Masterbar |
||
| 161 | public function clear_core_masterbar( $wp_admin_bar ) { |
||
| 162 | foreach ( $wp_admin_bar->get_nodes() as $node ) { |
||
| 163 | $wp_admin_bar->remove_node( $node->id ); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | // Add entries corresponding to WordPress.com Masterbar |
||
| 168 | public function build_wpcom_masterbar( $wp_admin_bar ) { |
||
| 169 | // Menu groups |
||
| 170 | $this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar ); |
||
| 171 | |||
| 172 | // Left part |
||
| 173 | $this->add_my_sites_submenu( $wp_admin_bar ); |
||
| 174 | $this->add_reader_submenu( $wp_admin_bar ); |
||
| 175 | |||
| 176 | // Right part |
||
| 177 | if ( Jetpack::is_module_active( 'notes' ) ) { |
||
| 178 | $this->add_notifications( $wp_admin_bar ); |
||
| 179 | } |
||
| 180 | |||
| 181 | $this->add_me_submenu( $wp_admin_bar ); |
||
| 182 | $this->add_write_button( $wp_admin_bar ); |
||
| 183 | } |
||
| 184 | |||
| 185 | public function get_locale() { |
||
| 186 | $wpcom_locale = get_locale(); |
||
| 187 | |||
| 188 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
| 189 | if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
| 190 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | View Code Duplication | if ( class_exists( 'GP_Locales' ) ) { |
|
| 195 | $wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() ); |
||
| 196 | if ( $wpcom_locale_object instanceof GP_Locale ) { |
||
| 197 | $wpcom_locale = $wpcom_locale_object->slug; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | return $wpcom_locale; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function add_notifications( $wp_admin_bar ) { |
||
| 205 | $wp_admin_bar->add_node( array( |
||
| 206 | 'id' => 'notes', |
||
| 207 | 'title' => '<span id="wpnt-notes-unread-count" class="wpnt-loading wpn-read"></span> |
||
| 208 | <span class="screen-reader-text">' . esc_html__( 'Notifications', 'jetpack' ) . '</span> |
||
| 209 | <span class="noticon noticon-bell"></span>', |
||
| 210 | 'meta' => array( |
||
| 211 | 'html' => '<div id="wpnt-notes-panel2" style="display:none" lang="'. esc_attr( $this->locale ) . '" dir="' . ( $this->is_rtl() ? 'rtl' : 'ltr' ) . '">' . |
||
| 212 | '<div class="wpnt-notes-panel-header">' . |
||
| 213 | '<span class="wpnt-notes-header">' . |
||
| 214 | esc_html__( 'Notifications', 'jetpack' ) . |
||
| 215 | '</span>' . |
||
| 216 | '<span class="wpnt-notes-panel-link">' . |
||
| 217 | '</span>' . |
||
| 218 | '</div>' . |
||
| 219 | '</div>', |
||
| 220 | 'class' => 'menupop mb-trackable', |
||
| 221 | ), |
||
| 222 | 'parent' => 'top-secondary', |
||
| 223 | ) ); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function add_reader_submenu( $wp_admin_bar ) { |
||
| 227 | $wp_admin_bar->add_menu( array( |
||
| 228 | 'parent' => 'root-default', |
||
| 229 | 'id' => 'newdash', |
||
| 230 | 'title' => esc_html__( 'Reader', 'jetpack' ), |
||
| 231 | 'href' => '#', |
||
| 232 | 'meta' => array( |
||
| 233 | 'class' => 'mb-trackable', |
||
| 234 | ) |
||
| 235 | ) ); |
||
| 236 | |||
| 237 | $wp_admin_bar->add_menu( array( |
||
| 238 | 'parent' => 'newdash', |
||
| 239 | 'id' => 'streams-header', |
||
| 240 | 'title' => esc_html_x( |
||
| 241 | 'Streams', |
||
| 242 | 'Title for Reader sub-menu that contains followed sites, likes, and recommendations', |
||
| 243 | 'jetpack' |
||
| 244 | ), |
||
| 245 | 'meta' => array( |
||
| 246 | 'class' => 'ab-submenu-header', |
||
| 247 | ) |
||
| 248 | ) ); |
||
| 249 | |||
| 250 | $following_title = $this->create_menu_item_pair( |
||
| 251 | array( |
||
| 252 | 'url' => 'https://wordpress.com/', |
||
| 253 | 'id' => 'wp-admin-bar-followed-sites', |
||
| 254 | 'label' => esc_html__( 'Followed Sites', 'jetpack' ), |
||
| 255 | ), |
||
| 256 | array( |
||
| 257 | 'url' => 'https://wordpress.com/following/edit', |
||
| 258 | 'id' => 'wp-admin-bar-reader-followed-sites-manage', |
||
| 259 | 'label' => esc_html__( 'Manage', 'jetpack' ), |
||
| 260 | ) |
||
| 261 | ); |
||
| 262 | |||
| 263 | $wp_admin_bar->add_menu( array( |
||
| 264 | 'parent' => 'newdash', |
||
| 265 | 'id' => 'following', |
||
| 266 | 'title' => $following_title, |
||
| 267 | 'meta' => array( 'class' => 'inline-action' ) |
||
| 268 | ) ); |
||
| 269 | |||
| 270 | $wp_admin_bar->add_menu( array( |
||
| 271 | 'parent' => 'newdash', |
||
| 272 | 'id' => 'discover-discover', |
||
| 273 | 'title' => esc_html__( 'Discover', 'jetpack' ), |
||
| 274 | 'href' => 'https://wordpress.com/discover', |
||
| 275 | 'meta' => array( |
||
| 276 | 'class' => 'mb-icon-spacer', |
||
| 277 | ) |
||
| 278 | ) ); |
||
| 279 | |||
| 280 | $wp_admin_bar->add_menu( array( |
||
| 281 | 'parent' => 'newdash', |
||
| 282 | 'id' => 'discover-search', |
||
| 283 | 'title' => esc_html__( 'Search', 'jetpack' ), |
||
| 284 | 'href' => 'https://wordpress.com/read/search', |
||
| 285 | 'meta' => array( |
||
| 286 | 'class' => 'mb-icon-spacer', |
||
| 287 | ) |
||
| 288 | ) ); |
||
| 289 | |||
| 290 | $wp_admin_bar->add_menu( array( |
||
| 291 | 'parent' => 'newdash', |
||
| 292 | 'id' => 'discover-recommended-blogs', |
||
| 293 | 'title' => esc_html__( 'Recommendations', 'jetpack' ), |
||
| 294 | 'href' => 'https://wordpress.com/recommendations', |
||
| 295 | 'meta' => array( |
||
| 296 | 'class' => 'mb-icon-spacer', |
||
| 297 | ) |
||
| 298 | ) ); |
||
| 299 | |||
| 300 | $wp_admin_bar->add_menu( array( |
||
| 301 | 'parent' => 'newdash', |
||
| 302 | 'id' => 'my-activity-my-likes', |
||
| 303 | 'title' => esc_html__( 'My Likes', 'jetpack' ), |
||
| 304 | 'href' => 'https://wordpress.com/activities/likes', |
||
| 305 | 'meta' => array( |
||
| 306 | 'class' => 'mb-icon-spacer', |
||
| 307 | ) |
||
| 308 | ) ); |
||
| 309 | |||
| 310 | } |
||
| 311 | |||
| 312 | public function create_menu_item_pair( $primary, $secondary ) { |
||
| 313 | $primary_class = 'ab-item ab-primary mb-icon'; |
||
| 314 | $secondary_class = 'ab-secondary'; |
||
| 315 | |||
| 316 | $primary_anchor = $this->create_menu_item_anchor( $primary_class, $primary['url'], $primary['label'], $primary['id'] ); |
||
| 317 | $secondary_anchor = $this->create_menu_item_anchor( $secondary_class, $secondary['url'], $secondary['label'], $secondary['id'] ); |
||
| 318 | |||
| 319 | return $primary_anchor . $secondary_anchor; |
||
| 320 | } |
||
| 321 | |||
| 322 | public function create_menu_item_anchor( $class, $url, $label, $id ) { |
||
| 323 | return '<a href="' . $url . '" class="' . $class . '" id="' . $id . '">' . $label . '</a>'; |
||
| 324 | } |
||
| 325 | |||
| 326 | public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) { |
||
| 327 | $wp_admin_bar->add_group( array( |
||
| 328 | 'id' => 'root-default', |
||
| 329 | 'meta' => array( |
||
| 330 | 'class' => 'ab-top-menu', |
||
| 331 | ), |
||
| 332 | ) ); |
||
| 333 | |||
| 334 | $wp_admin_bar->add_group( array( |
||
| 335 | 'parent' => 'blog', |
||
| 336 | 'id' => 'blog-secondary', |
||
| 337 | 'meta' => array( |
||
| 338 | 'class' => 'ab-sub-secondary', |
||
| 339 | ), |
||
| 340 | ) ); |
||
| 341 | |||
| 342 | $wp_admin_bar->add_group( array( |
||
| 343 | 'id' => 'top-secondary', |
||
| 344 | 'meta' => array( |
||
| 345 | 'class' => 'ab-top-secondary', |
||
| 346 | ), |
||
| 347 | ) ); |
||
| 348 | } |
||
| 349 | |||
| 350 | public function add_me_submenu( $wp_admin_bar ) { |
||
| 351 | $user_id = get_current_user_id(); |
||
| 352 | if ( empty( $user_id ) ) { |
||
| 353 | return; |
||
| 354 | } |
||
| 355 | |||
| 356 | $avatar = get_avatar( $this->user_email, 32, 'mm', '', array( 'force_display' => true ) ); |
||
| 357 | $class = empty( $avatar ) ? 'mb-trackable' : 'with-avatar mb-trackable'; |
||
| 358 | |||
| 359 | // Add the 'Me' menu |
||
| 360 | $wp_admin_bar->add_menu( array( |
||
| 361 | 'id' => 'my-account', |
||
| 362 | 'parent' => 'top-secondary', |
||
| 363 | 'title' => $avatar . '<span class="ab-text">' . esc_html__( 'Me', 'jetpack' ) . '</span>', |
||
| 364 | 'href' => '#', |
||
| 365 | 'meta' => array( |
||
| 366 | 'class' => $class, |
||
| 367 | ), |
||
| 368 | ) ); |
||
| 369 | |||
| 370 | $id = 'user-actions'; |
||
| 371 | $wp_admin_bar->add_group( array( |
||
| 372 | 'parent' => 'my-account', |
||
| 373 | 'id' => $id, |
||
| 374 | ) ); |
||
| 375 | |||
| 376 | $settings_url = 'https://wordpress.com/me/account'; |
||
| 377 | |||
| 378 | $logout_url = wp_logout_url(); |
||
| 379 | $logout_url = add_query_arg( 'context', 'masterbar', $logout_url ); |
||
| 380 | |||
| 381 | $user_info = get_avatar( $this->user_email, 128, 'mm', '', array( 'force_display' => true ) ); |
||
| 382 | $user_info .= '<span class="display-name">' . $this->display_name . '</span>'; |
||
| 383 | $user_info .= '<a class="username" href="http://gravatar.com/' . $this->user_login . '">@' . $this->user_login . '</a>'; |
||
| 384 | $user_info .= '<form action="' . $logout_url . '" method="post"><button class="ab-sign-out" type="submit">' . esc_html__( 'Sign Out', 'jetpack' ) . '</button></form>'; |
||
| 385 | |||
| 386 | $wp_admin_bar->add_menu( array( |
||
| 387 | 'parent' => $id, |
||
| 388 | 'id' => 'user-info', |
||
| 389 | 'title' => $user_info, |
||
| 390 | 'meta' => array( |
||
| 391 | 'class' => 'user-info user-info-item', |
||
| 392 | 'tabindex' => -1, |
||
| 393 | ), |
||
| 394 | ) ); |
||
| 395 | |||
| 396 | $wp_admin_bar->add_menu( array( |
||
| 397 | 'parent' => $id, |
||
| 398 | 'id' => 'profile-header', |
||
| 399 | 'title' => esc_html__( 'Profile', 'jetpack' ), |
||
| 400 | 'meta' => array( |
||
| 401 | 'class' => 'ab-submenu-header', |
||
| 402 | ), |
||
| 403 | ) ); |
||
| 404 | |||
| 405 | $wp_admin_bar->add_menu( array( |
||
| 406 | 'parent' => $id, |
||
| 407 | 'id' => 'my-profile', |
||
| 408 | 'title' => esc_html__( 'My Profile', 'jetpack' ), |
||
| 409 | 'href' => 'https://wordpress.com/me', |
||
| 410 | 'meta' => array( |
||
| 411 | 'class' => 'mb-icon', |
||
| 412 | ), |
||
| 413 | ) ); |
||
| 414 | |||
| 415 | $wp_admin_bar->add_menu( array( |
||
| 416 | 'parent' => $id, |
||
| 417 | 'id' => 'account-settings', |
||
| 418 | 'title' => esc_html__( 'Account Settings', 'jetpack' ), |
||
| 419 | 'href' => $settings_url, |
||
| 420 | 'meta' => array( |
||
| 421 | 'class' => 'mb-icon', |
||
| 422 | ), |
||
| 423 | ) ); |
||
| 424 | |||
| 425 | $wp_admin_bar->add_menu( array( |
||
| 426 | 'parent' => $id, |
||
| 427 | 'id' => 'billing', |
||
| 428 | 'title' => esc_html__( 'Manage Purchases', 'jetpack' ), |
||
| 429 | 'href' => 'https://wordpress.com/me/purchases', |
||
| 430 | 'meta' => array( |
||
| 431 | 'class' => 'mb-icon', |
||
| 432 | ), |
||
| 433 | ) ); |
||
| 434 | |||
| 435 | $wp_admin_bar->add_menu( array( |
||
| 436 | 'parent' => $id, |
||
| 437 | 'id' => 'security', |
||
| 438 | 'title' => esc_html__( 'Security', 'jetpack' ), |
||
| 439 | 'href' => 'https://wordpress.com/me/security', |
||
| 440 | 'meta' => array( |
||
| 441 | 'class' => 'mb-icon', |
||
| 442 | ), |
||
| 443 | ) ); |
||
| 444 | |||
| 445 | $wp_admin_bar->add_menu( array( |
||
| 446 | 'parent' => $id, |
||
| 447 | 'id' => 'notifications', |
||
| 448 | 'title' => esc_html__( 'Notifications', 'jetpack' ), |
||
| 449 | 'href' => 'https://wordpress.com/me/notifications', |
||
| 450 | 'meta' => array( |
||
| 451 | 'class' => 'mb-icon', |
||
| 452 | ), |
||
| 453 | ) ); |
||
| 454 | |||
| 455 | $wp_admin_bar->add_menu( array( |
||
| 456 | 'parent' => $id, |
||
| 457 | 'id' => 'special-header', |
||
| 458 | 'title' => esc_html_x( |
||
| 459 | 'Special', |
||
| 460 | 'Title for Me sub-menu that contains Get Apps, Next Steps, and Help options', |
||
| 461 | 'jetpack' |
||
| 462 | ), |
||
| 463 | 'meta' => array( |
||
| 464 | 'class' => 'ab-submenu-header', |
||
| 465 | ), |
||
| 466 | ) ); |
||
| 467 | |||
| 468 | $wp_admin_bar->add_menu( array( |
||
| 469 | 'parent' => $id, |
||
| 470 | 'id' => 'get-apps', |
||
| 471 | 'title' => esc_html__( 'Get Apps', 'jetpack' ), |
||
| 472 | 'href' => 'https://wordpress.com/me/get-apps', |
||
| 473 | 'meta' => array( |
||
| 474 | 'class' => 'mb-icon user-info-item', |
||
| 475 | ), |
||
| 476 | ) ); |
||
| 477 | |||
| 478 | $wp_admin_bar->add_menu( array( |
||
| 479 | 'parent' => $id, |
||
| 480 | 'id' => 'next-steps', |
||
| 481 | 'title' => esc_html__( 'Next Steps', 'jetpack' ), |
||
| 482 | 'href' => 'https://wordpress.com/me/next', |
||
| 483 | 'meta' => array( |
||
| 484 | 'class' => 'mb-icon user-info-item', |
||
| 485 | ), |
||
| 486 | ) ); |
||
| 487 | |||
| 488 | $help_link = 'https://jetpack.com/support/'; |
||
| 489 | |||
| 490 | if ( jetpack_is_atomic_site() ) { |
||
| 491 | $help_link = 'https://wordpress.com/help'; |
||
| 492 | } |
||
| 493 | |||
| 494 | $wp_admin_bar->add_menu( array( |
||
| 495 | 'parent' => $id, |
||
| 496 | 'id' => 'help', |
||
| 497 | 'title' => esc_html__( 'Help', 'jetpack' ), |
||
| 498 | 'href' => $help_link, |
||
| 499 | 'meta' => array( |
||
| 500 | 'class' => 'mb-icon user-info-item', |
||
| 501 | ), |
||
| 502 | ) ); |
||
| 503 | } |
||
| 504 | |||
| 505 | public function add_write_button( $wp_admin_bar ) { |
||
| 531 | |||
| 532 | public function add_my_sites_submenu( $wp_admin_bar ) { |
||
| 1001 | } |
||
| 1002 |
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: