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:
| 1 | <?php |
||
| 15 | class Atomic_Admin_Menu extends Admin_Menu { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Atomic_Admin_Menu constructor. |
||
| 19 | */ |
||
| 20 | protected function __construct() { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create the desired menu output. |
||
| 34 | */ |
||
| 35 | public function reregister_menu_items() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Adds Plugins menu. |
||
| 52 | * |
||
| 53 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 54 | */ |
||
| 55 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Adds the site switcher link if user has more than one site. |
||
| 62 | */ |
||
| 63 | public function add_browse_sites_link() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Adds a custom element class for Site Switcher menu item. |
||
| 76 | * |
||
| 77 | * @param array $menu Associative array of administration menu items. |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 82 | foreach ( $menu as $key => $menu_item ) { |
||
| 83 | if ( 'site-switcher' !== $menu_item[3] ) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] ); |
||
| 88 | break; |
||
| 89 | } |
||
| 90 | |||
| 91 | return $menu; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Adds a link to the menu to create a new site. |
||
| 96 | */ |
||
| 97 | public function add_new_site_link() { |
||
| 98 | global $menu; |
||
| 99 | |||
| 100 | $site_count = get_user_option( 'wpcom_site_count' ); |
||
| 101 | if ( $site_count && $site_count > 1 ) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | // Attempt to get last position. |
||
| 106 | $position = 1000; |
||
| 107 | while ( isset( $menu[ $position ] ) ) { |
||
| 108 | $position++; |
||
| 109 | } |
||
| 110 | |||
| 111 | $this->add_admin_menu_separator( ++$position ); |
||
| 112 | add_menu_page( __( 'Add new site', 'jetpack' ), __( 'Add new site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt', ++$position ); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Adds site card component. |
||
| 117 | */ |
||
| 118 | public function add_site_card_menu() { |
||
| 119 | $default = 'data:image/svg+xml,' . rawurlencode( '<svg class="gridicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Globe</title><rect fill-opacity="0" x="0" width="24" height="24"/><g><path fill="#fff" d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm0 18l2-2 1-1v-2h-2v-1l-1-1H9v3l2 2v1.93c-3.94-.494-7-3.858-7-7.93l1 1h2v-2h2l3-3V6h-2L9 5v-.41C9.927 4.21 10.94 4 12 4s2.073.212 3 .59V6l-1 1v2l1 1 3.13-3.13c.752.897 1.304 1.964 1.606 3.13H18l-2 2v2l1 1h2l.286.286C18.03 18.06 15.24 20 12 20z"/></g></svg>' ); |
||
| 120 | $icon = get_site_icon_url( 32, $default ); |
||
| 121 | |||
| 122 | $badge = ''; |
||
| 123 | if ( function_exists( 'site_is_private' ) && site_is_private() ) { |
||
| 124 | $badge .= sprintf( |
||
| 125 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 126 | site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | $site_card = ' |
||
| 131 | <div class="site__info"> |
||
| 132 | <div class="site__title">%1$s</div> |
||
| 133 | <div class="site__domain">%2$s</div> |
||
| 134 | %3$s |
||
| 135 | </div>'; |
||
| 136 | |||
| 137 | $site_card = sprintf( |
||
| 138 | $site_card, |
||
| 139 | get_option( 'blogname' ), |
||
| 140 | $this->domain, |
||
| 141 | $badge |
||
| 142 | ); |
||
| 143 | |||
| 144 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
| 145 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Adds a custom element class and id for Site Card's menu item. |
||
| 150 | * |
||
| 151 | * @param array $menu Associative array of administration menu items. |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function set_site_card_menu_class( array $menu ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Adds Upgrades menu. |
||
| 178 | */ |
||
| 179 | public function add_upgrades_menu() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Adds Tools menu. |
||
| 187 | * |
||
| 188 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 189 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 190 | */ |
||
| 191 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Adds Settings menu. |
||
| 198 | * |
||
| 199 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 200 | */ |
||
| 201 | public function add_options_menu( $wp_admin = false ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Adds Appearance menu. |
||
| 215 | * |
||
| 216 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 217 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 218 | */ |
||
| 219 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Adds Users menu. |
||
| 228 | * |
||
| 229 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 230 | */ |
||
| 231 | public function add_users_menu( $wp_admin = false ) { |
||
| 241 | } |
||
| 242 |