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 |
||
| 17 | class Atomic_Admin_Menu extends Admin_Menu { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Atomic_Admin_Menu constructor. |
||
| 21 | */ |
||
| 22 | protected function __construct() { |
||
| 23 | parent::__construct(); |
||
| 24 | |||
| 25 | add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); |
||
| 26 | add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); |
||
| 27 | add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) ); |
||
| 28 | |||
| 29 | add_action( |
||
| 30 | 'admin_menu', |
||
| 31 | function () { |
||
| 32 | remove_action( 'admin_menu', 'gutenberg_menu', 9 ); |
||
| 33 | }, |
||
| 34 | 0 |
||
| 35 | ); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Dequeues unnecessary scripts. |
||
| 40 | */ |
||
| 41 | public function dequeue_scripts() { |
||
| 42 | wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php. |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Determines whether the current locale is right-to-left (RTL). |
||
| 47 | * |
||
| 48 | * Performs the check against the current locale set on the WordPress.com's account settings. |
||
| 49 | * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`. |
||
| 50 | */ |
||
| 51 | public function is_rtl() { |
||
| 52 | return get_user_option( 'jetpack_wpcom_is_rtl' ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create the desired menu output. |
||
| 57 | */ |
||
| 58 | View Code Duplication | public function reregister_menu_items() { |
|
| 59 | parent::reregister_menu_items(); |
||
| 60 | |||
| 61 | $this->add_my_home_menu(); |
||
| 62 | |||
| 63 | // Not needed outside of wp-admin. |
||
| 64 | if ( ! $this->is_api_request ) { |
||
| 65 | $this->add_browse_sites_link(); |
||
| 66 | $this->add_site_card_menu(); |
||
| 67 | $this->add_new_site_link(); |
||
| 68 | } |
||
| 69 | |||
| 70 | ksort( $GLOBALS['menu'] ); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Forces Posts menu to WPAdmin for Atomic sites only. |
||
| 75 | * Overloads `add_posts_menu` in parent class. |
||
| 76 | * |
||
| 77 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 78 | */ |
||
| 79 | public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 80 | return false; // return explicit `false` to force WPAdmin links. |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Forces Pages menu to WPAdmin for Atomic sites only. |
||
| 85 | * Overloads `add_page_menu` in parent class. |
||
| 86 | * |
||
| 87 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 88 | */ |
||
| 89 | public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 90 | return false; // return explicit `false` to force WPAdmin links. |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Adds Plugins menu. |
||
| 95 | * |
||
| 96 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 97 | */ |
||
| 98 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 99 | // Plugins on Atomic sites are always managed on WP Admin. |
||
| 100 | parent::add_plugins_menu( true ); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Adds the site switcher link if user has more than one site. |
||
| 105 | */ |
||
| 106 | public function add_browse_sites_link() { |
||
| 107 | $site_count = get_user_option( 'wpcom_site_count' ); |
||
| 108 | if ( ! $site_count || $site_count < 2 ) { |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | // Add the menu item. |
||
| 113 | add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
||
| 114 | add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) ); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Adds a custom element class for Site Switcher menu item. |
||
| 119 | * |
||
| 120 | * @param array $menu Associative array of administration menu items. |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 125 | foreach ( $menu as $key => $menu_item ) { |
||
| 126 | if ( 'site-switcher' !== $menu_item[3] ) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | |||
| 130 | $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] ); |
||
| 131 | break; |
||
| 132 | } |
||
| 133 | |||
| 134 | return $menu; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds a link to the menu to create a new site. |
||
| 139 | */ |
||
| 140 | public function add_new_site_link() { |
||
| 141 | $site_count = get_user_option( 'wpcom_site_count' ); |
||
| 142 | if ( $site_count && $site_count > 1 ) { |
||
| 143 | return; |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->add_admin_menu_separator(); |
||
| 147 | add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' ); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Adds site card component. |
||
| 152 | */ |
||
| 153 | public function add_site_card_menu() { |
||
| 154 | $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>' ); |
||
| 155 | $icon = get_site_icon_url( 32, $default ); |
||
| 156 | $blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain; |
||
| 157 | |||
| 158 | $badge = ''; |
||
| 159 | if ( function_exists( 'site_is_private' ) && site_is_private() ) { |
||
| 160 | $badge .= sprintf( |
||
| 161 | '<span class="site__badge site__badge-private">%s</span>', |
||
| 162 | site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | $site_card = ' |
||
| 167 | <div class="site__info"> |
||
| 168 | <div class="site__title">%1$s</div> |
||
| 169 | <div class="site__domain">%2$s</div> |
||
| 170 | %3$s |
||
| 171 | </div>'; |
||
| 172 | |||
| 173 | $site_card = sprintf( |
||
| 174 | $site_card, |
||
| 175 | $blog_name, |
||
| 176 | $this->domain, |
||
| 177 | $badge |
||
| 178 | ); |
||
| 179 | |||
| 180 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
| 181 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Adds a custom element class and id for Site Card's menu item. |
||
| 186 | * |
||
| 187 | * @param array $menu Associative array of administration menu items. |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | public function set_site_card_menu_class( array $menu ) { |
||
| 192 | foreach ( $menu as $key => $menu_item ) { |
||
| 193 | if ( 'site-card' !== $menu_item[3] ) { |
||
| 194 | continue; |
||
| 195 | } |
||
| 196 | |||
| 197 | $classes = ' toplevel_page_site-card'; |
||
| 198 | |||
| 199 | // webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon. |
||
| 200 | if ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) { |
||
| 201 | $classes .= ' has-site-icon'; |
||
| 202 | } |
||
| 203 | |||
| 204 | $menu[ $key ][4] = $menu_item[4] . $classes; |
||
| 205 | $menu[ $key ][5] = 'toplevel_page_site_card'; |
||
| 206 | break; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $menu; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Adds Upgrades menu. |
||
| 214 | */ |
||
| 215 | public function add_upgrades_menu() { |
||
| 216 | parent::add_upgrades_menu(); |
||
| 217 | |||
| 218 | add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 1 ); |
||
| 219 | add_submenu_page( 'paid-upgrades.php', __( 'Emails', 'jetpack' ), __( 'Emails', 'jetpack' ), 'manage_options', 'https://wordpress.com/email/' . $this->domain, null, 2 ); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Adds Tools menu. |
||
| 224 | * |
||
| 225 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 226 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 227 | */ |
||
| 228 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Adds Settings menu. |
||
| 235 | * |
||
| 236 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 237 | */ |
||
| 238 | public function add_options_menu( $wp_admin = false ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Adds Appearance menu. |
||
| 252 | * |
||
| 253 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 254 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 255 | */ |
||
| 256 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Adds Users menu. |
||
| 265 | * |
||
| 266 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 267 | */ |
||
| 268 | public function add_users_menu( $wp_admin = false ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Also remove the Gutenberg plugin menu. |
||
| 276 | * |
||
| 277 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 278 | */ |
||
| 279 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
| 287 | */ |
||
| 288 | public function ajax_sidebar_state() { |
||
| 289 | $expanded = filter_var( $_REQUEST['expanded'], FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 290 | Client::wpcom_json_api_request_as_user( |
||
| 291 | '/me/preferences', |
||
| 292 | '2', |
||
| 293 | array( |
||
| 294 | 'method' => 'POST', |
||
| 295 | ), |
||
| 296 | (object) array( 'calypso_preferences' => (object) array( 'sidebarCollapsed' => ! $expanded ) ), |
||
| 297 | 'wpcom' |
||
| 302 | } |
||
| 303 | |||
| 304 |